From 8bb248890c434e693406e2a91105ee6a08525180 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Fri, 18 Feb 2022 12:20:16 -0400 Subject: [PATCH] Chore: Migrate getCustomEmojis to TS (#3724) * update customEmoji interface and getCustomEmoji * add sdk * updated emojiCustom rest definition * minor refactor * update params object --- app/definitions/ICustomEmoji.ts | 3 +- app/definitions/rest/v1/emojiCustom.ts | 2 +- ...{getCustomEmojis.js => getCustomEmojis.ts} | 104 ++++++++++-------- 3 files changed, 64 insertions(+), 45 deletions(-) rename app/lib/methods/{getCustomEmojis.js => getCustomEmojis.ts} (51%) diff --git a/app/definitions/ICustomEmoji.ts b/app/definitions/ICustomEmoji.ts index 6a6c131a7..348e66098 100644 --- a/app/definitions/ICustomEmoji.ts +++ b/app/definitions/ICustomEmoji.ts @@ -1,8 +1,9 @@ import Model from '@nozbe/watermelondb/Model'; export interface ICustomEmoji { + _id: string; name?: string; - aliases?: string; + aliases?: string[]; extension: string; _updatedAt: Date; } diff --git a/app/definitions/rest/v1/emojiCustom.ts b/app/definitions/rest/v1/emojiCustom.ts index 8ef956e5a..086589f2d 100644 --- a/app/definitions/rest/v1/emojiCustom.ts +++ b/app/definitions/rest/v1/emojiCustom.ts @@ -9,7 +9,7 @@ export type EmojiCustomEndpoints = { } & PaginatedResult; }; 'emoji-custom.list': { - GET: (params: { query: string }) => { + GET: (params: { updatedSince: string }) => { emojis?: { update: ICustomEmojiDescriptor[]; }; diff --git a/app/lib/methods/getCustomEmojis.js b/app/lib/methods/getCustomEmojis.ts similarity index 51% rename from app/lib/methods/getCustomEmojis.js rename to app/lib/methods/getCustomEmojis.ts index 55f7c4c44..87a549d18 100644 --- a/app/lib/methods/getCustomEmojis.js +++ b/app/lib/methods/getCustomEmojis.ts @@ -1,13 +1,22 @@ import orderBy from 'lodash/orderBy'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; +import { ICustomEmojis } from '../../reducers/customEmojis'; import { compareServerVersion } from '../utils'; import { store as reduxStore } from '../auxStore'; import database from '../database'; import log from '../../utils/log'; import { setCustomEmojis as setCustomEmojisAction } from '../../actions/customEmojis'; +import { ICustomEmoji, TCustomEmojiModel } from '../../definitions'; +import sdk from '../rocketchat/services/sdk'; -const getUpdatedSince = allEmojis => { +interface IUpdateEmojis { + update: TCustomEmojiModel[]; + remove?: TCustomEmojiModel[]; + allRecords: TCustomEmojiModel[]; +} + +const getUpdatedSince = (allEmojis: ICustomEmoji[]) => { if (!allEmojis.length) { return null; } @@ -19,27 +28,27 @@ const getUpdatedSince = allEmojis => { return ordered && ordered[0]._updatedAt.toISOString(); }; -const updateEmojis = async ({ update = [], remove = [], allRecords }) => { +const updateEmojis = async ({ update = [], remove = [], allRecords }: IUpdateEmojis) => { if (!((update && update.length) || (remove && remove.length))) { return; } const db = database.active; const emojisCollection = db.get('custom_emojis'); - let emojisToCreate = []; - let emojisToUpdate = []; - let emojisToDelete = []; + let emojisToCreate: TCustomEmojiModel[] = []; + let emojisToUpdate: TCustomEmojiModel[] = []; + let emojisToDelete: TCustomEmojiModel[] = []; // Create or update if (update && update.length) { - emojisToCreate = update.filter(i1 => !allRecords.find(i2 => i1._id === i2.id)); - emojisToUpdate = allRecords.filter(i1 => update.find(i2 => i1.id === i2._id)); - emojisToCreate = emojisToCreate.map(emoji => + const filterEmojisToCreate = update.filter(i1 => !allRecords.find(i2 => i1._id === i2.id)); + const filterEmojisToUpdate = allRecords.filter(i1 => update.find(i2 => i1.id === i2._id)); + emojisToCreate = filterEmojisToCreate.map(emoji => emojisCollection.prepareCreate(e => { e._raw = sanitizedRaw({ id: emoji._id }, emojisCollection.schema); Object.assign(e, emoji); }) ); - emojisToUpdate = emojisToUpdate.map(emoji => { + emojisToUpdate = filterEmojisToUpdate.map(emoji => { const newEmoji = update.find(e => e._id === emoji.id); return emoji.prepareUpdate(e => { Object.assign(e, newEmoji); @@ -48,12 +57,12 @@ const updateEmojis = async ({ update = [], remove = [], allRecords }) => { } if (remove && remove.length) { - emojisToDelete = allRecords.filter(i1 => remove.find(i2 => i1.id === i2._id)); - emojisToDelete = emojisToDelete.map(emoji => emoji.prepareDestroyPermanently()); + const filterEmojisToDelete = allRecords.filter(i1 => remove.find(i2 => i1.id === i2._id)); + emojisToDelete = filterEmojisToDelete.map(emoji => emoji.prepareDestroyPermanently()); } try { - await db.action(async () => { + await db.write(async () => { await db.batch(...emojisToCreate, ...emojisToUpdate, ...emojisToDelete); }); return true; @@ -66,26 +75,34 @@ export async function setCustomEmojis() { const db = database.active; const emojisCollection = db.get('custom_emojis'); const allEmojis = await emojisCollection.query().fetch(); - const parsed = allEmojis.reduce((ret, item) => { - ret[item.name] = { - name: item.name, - extension: item.extension - }; - item.aliases.forEach(alias => { - ret[alias] = { + const parsed = allEmojis.reduce((ret: ICustomEmojis, item) => { + if (item.name) { + ret[item.name] = { name: item.name, extension: item.extension }; - }); + } + + if (item.aliases) { + item.aliases.forEach(alias => { + if (item.name) { + ret[alias] = { + name: item.name, + extension: item.extension + }; + } + }); + } + return ret; }, {}); reduxStore.dispatch(setCustomEmojisAction(parsed)); } export function getCustomEmojis() { - return new Promise(async resolve => { + return new Promise(async resolve => { try { - const serverVersion = reduxStore.getState().server.version; + const serverVersion = reduxStore.getState().server.version as string; const db = database.active; const emojisCollection = db.get('custom_emojis'); const allRecords = await emojisCollection.query().fetch(); @@ -94,10 +111,11 @@ export function getCustomEmojis() { // if server version is lower than 0.75.0, fetches from old api if (compareServerVersion(serverVersion, 'lowerThan', '0.75.0')) { // RC 0.61.0 - const result = await this.sdk.get('emoji-custom'); - + // @ts-ignore + const result = await sdk.get('emoji-custom'); + // @ts-ignore let { emojis } = result; - emojis = emojis.filter(emoji => !updatedSince || emoji._updatedAt > updatedSince); + emojis = emojis.filter((emoji: TCustomEmojiModel) => !updatedSince || emoji._updatedAt.toISOString() > updatedSince); const changedEmojis = await updateEmojis({ update: emojis, allRecords }); // `setCustomEmojis` is fired on selectServer @@ -106,28 +124,28 @@ export function getCustomEmojis() { setCustomEmojis(); } return resolve(); - } else { - const params = {}; - if (updatedSince) { - params.updatedSince = updatedSince; - } + } + const params: { updatedSince: string } = { updatedSince: '' }; + if (updatedSince) { + params.updatedSince = updatedSince; + } - // RC 0.75.0 - const result = await this.sdk.get('emoji-custom.list', params); + // RC 0.75.0 + const result = await sdk.get('emoji-custom.list', params); - if (!result.success) { - return resolve(); - } + if (!result.success) { + return resolve(); + } - const { emojis } = result; - const { update, remove } = emojis; - const changedEmojis = await updateEmojis({ update, remove, allRecords }); + const { emojis } = result; + // @ts-ignore + const { update, remove } = emojis; + const changedEmojis = await updateEmojis({ update, remove, allRecords }); - // `setCustomEmojis` is fired on selectServer - // We run it again only if emojis were changed - if (changedEmojis) { - setCustomEmojis(); - } + // `setCustomEmojis` is fired on selectServer + // We run it again only if emojis were changed + if (changedEmojis) { + setCustomEmojis(); } } catch (e) { log(e);