feat: Cache supported versions timestamp to fetch from cloud once every 12h (#5292)

This commit is contained in:
Diego Mello 2023-10-23 16:35:18 -03:00 committed by GitHub
parent f1b7cfe510
commit 5175696555
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 3 deletions

View File

@ -81,6 +81,7 @@ export interface IServer {
E2E_Enable: boolean;
supportedVersions?: ISupportedVersionsData;
supportedVersionsWarningAt?: Date;
supportedVersionsUpdatedAt?: Date;
}
export type TServerModel = IServer & Model;

View File

@ -38,4 +38,6 @@ export default class Server extends Model {
@json('supported_versions', sanitizer) supportedVersions;
@date('supported_versions_warning_at') supportedVersionsWarningAt;
@date('supported_versions_updated_at') supportedVersionsUpdatedAt;
}

View File

@ -131,6 +131,21 @@ export default schemaMigrations({
]
})
]
},
{
toVersion: 15,
steps: [
addColumns({
table: 'servers',
columns: [
{
name: 'supported_versions_updated_at',
type: 'number',
isOptional: true
}
]
})
]
}
]
});

View File

@ -1,7 +1,7 @@
import { appSchema, tableSchema } from '@nozbe/watermelondb';
export default appSchema({
version: 14,
version: 15,
tables: [
tableSchema({
name: 'users',
@ -40,7 +40,8 @@ export default appSchema({
{ name: 'enterprise_modules', type: 'string', isOptional: true },
{ name: 'e2e_enable', type: 'boolean', isOptional: true },
{ name: 'supported_versions', type: 'string', isOptional: true },
{ name: 'supported_versions_warning_at', type: 'number', isOptional: true }
{ name: 'supported_versions_warning_at', type: 'number', isOptional: true },
{ name: 'supported_versions_updated_at', type: 'number', isOptional: true }
]
}),
tableSchema({

View File

@ -1,6 +1,7 @@
import RNFetchBlob from 'rn-fetch-blob';
import { settings as RocketChatSettings } from '@rocket.chat/sdk';
import { KJUR } from 'jsrsasign';
import moment from 'moment';
import { getSupportedVersionsCloud } from '../services/restApi';
import { TCloudInfo, IServerInfo, ISupportedVersions, ISupportedVersionsData, IApiServerInfo } from '../../definitions';
@ -8,6 +9,7 @@ import { selectServerFailure } from '../../actions/server';
import { store } from '../store/auxStore';
import I18n from '../../i18n';
import { SIGNED_SUPPORTED_VERSIONS_PUBLIC_KEY } from '../constants';
import { getServerById } from '../database/services/Server';
interface IServerInfoFailure {
success: false;
@ -20,6 +22,8 @@ interface IServerInfoSuccess extends IServerInfo {
export type TServerInfoResult = IServerInfoSuccess | IServerInfoFailure;
const SV_CLOUD_UPDATE_INTERVAL = 12; // hours
// Verifies if JWT is valid and returns the payload
const verifyJWT = (jwt?: string): ISupportedVersionsData | null => {
try {
@ -57,6 +61,18 @@ export async function getServerInfo(server: string): Promise<TServerInfoResult>
// if backend doesn't have supported versions or JWT is invalid, request from cloud
if (!supportedVersions) {
// fetches from cloud only every 12h
const serverRecord = await getServerById(server);
if (
serverRecord?.supportedVersionsUpdatedAt &&
moment(new Date()).diff(serverRecord?.supportedVersionsUpdatedAt, 'hours') <= SV_CLOUD_UPDATE_INTERVAL
) {
return {
...jsonRes,
success: true
};
}
const cloudInfo = await getCloudInfo(server);
// Makes use of signed JWT to get supported versions

View File

@ -73,6 +73,7 @@ const upsertServer = async function ({ server, serverInfo }: { server: string; s
r.version = serverVersion;
if (serverInfo.supportedVersions) {
r.supportedVersions = serverInfo.supportedVersions;
r.supportedVersionsUpdatedAt = new Date();
}
});
});
@ -83,10 +84,11 @@ const upsertServer = async function ({ server, serverInfo }: { server: string; s
await serversDB.write(async () => {
newRecord = await serversCollection.create(r => {
r._raw = sanitizedRaw({ id: server }, serversCollection.schema);
r.version = serverVersion;
if (serverInfo.supportedVersions) {
r.supportedVersions = serverInfo.supportedVersions;
r.supportedVersionsUpdatedAt = new Date();
}
r.version = serverVersion;
});
});
if (newRecord) {