Chore: Migrate getCustomEmojis to TS (#3724)

* update customEmoji interface and getCustomEmoji

* add sdk

* updated emojiCustom rest definition

* minor refactor

* update params object
This commit is contained in:
Gerzon Z 2022-02-18 12:20:16 -04:00 committed by GitHub
parent 370798f100
commit 8bb248890c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 45 deletions

View File

@ -1,8 +1,9 @@
import Model from '@nozbe/watermelondb/Model'; import Model from '@nozbe/watermelondb/Model';
export interface ICustomEmoji { export interface ICustomEmoji {
_id: string;
name?: string; name?: string;
aliases?: string; aliases?: string[];
extension: string; extension: string;
_updatedAt: Date; _updatedAt: Date;
} }

View File

@ -9,7 +9,7 @@ export type EmojiCustomEndpoints = {
} & PaginatedResult; } & PaginatedResult;
}; };
'emoji-custom.list': { 'emoji-custom.list': {
GET: (params: { query: string }) => { GET: (params: { updatedSince: string }) => {
emojis?: { emojis?: {
update: ICustomEmojiDescriptor[]; update: ICustomEmojiDescriptor[];
}; };

View File

@ -1,13 +1,22 @@
import orderBy from 'lodash/orderBy'; import orderBy from 'lodash/orderBy';
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
import { ICustomEmojis } from '../../reducers/customEmojis';
import { compareServerVersion } from '../utils'; import { compareServerVersion } from '../utils';
import { store as reduxStore } from '../auxStore'; import { store as reduxStore } from '../auxStore';
import database from '../database'; import database from '../database';
import log from '../../utils/log'; import log from '../../utils/log';
import { setCustomEmojis as setCustomEmojisAction } from '../../actions/customEmojis'; 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) { if (!allEmojis.length) {
return null; return null;
} }
@ -19,27 +28,27 @@ const getUpdatedSince = allEmojis => {
return ordered && ordered[0]._updatedAt.toISOString(); 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))) { if (!((update && update.length) || (remove && remove.length))) {
return; return;
} }
const db = database.active; const db = database.active;
const emojisCollection = db.get('custom_emojis'); const emojisCollection = db.get('custom_emojis');
let emojisToCreate = []; let emojisToCreate: TCustomEmojiModel[] = [];
let emojisToUpdate = []; let emojisToUpdate: TCustomEmojiModel[] = [];
let emojisToDelete = []; let emojisToDelete: TCustomEmojiModel[] = [];
// Create or update // Create or update
if (update && update.length) { if (update && update.length) {
emojisToCreate = update.filter(i1 => !allRecords.find(i2 => i1._id === i2.id)); const filterEmojisToCreate = update.filter(i1 => !allRecords.find(i2 => i1._id === i2.id));
emojisToUpdate = allRecords.filter(i1 => update.find(i2 => i1.id === i2._id)); const filterEmojisToUpdate = allRecords.filter(i1 => update.find(i2 => i1.id === i2._id));
emojisToCreate = emojisToCreate.map(emoji => emojisToCreate = filterEmojisToCreate.map(emoji =>
emojisCollection.prepareCreate(e => { emojisCollection.prepareCreate(e => {
e._raw = sanitizedRaw({ id: emoji._id }, emojisCollection.schema); e._raw = sanitizedRaw({ id: emoji._id }, emojisCollection.schema);
Object.assign(e, emoji); Object.assign(e, emoji);
}) })
); );
emojisToUpdate = emojisToUpdate.map(emoji => { emojisToUpdate = filterEmojisToUpdate.map(emoji => {
const newEmoji = update.find(e => e._id === emoji.id); const newEmoji = update.find(e => e._id === emoji.id);
return emoji.prepareUpdate(e => { return emoji.prepareUpdate(e => {
Object.assign(e, newEmoji); Object.assign(e, newEmoji);
@ -48,12 +57,12 @@ const updateEmojis = async ({ update = [], remove = [], allRecords }) => {
} }
if (remove && remove.length) { if (remove && remove.length) {
emojisToDelete = allRecords.filter(i1 => remove.find(i2 => i1.id === i2._id)); const filterEmojisToDelete = allRecords.filter(i1 => remove.find(i2 => i1.id === i2._id));
emojisToDelete = emojisToDelete.map(emoji => emoji.prepareDestroyPermanently()); emojisToDelete = filterEmojisToDelete.map(emoji => emoji.prepareDestroyPermanently());
} }
try { try {
await db.action(async () => { await db.write(async () => {
await db.batch(...emojisToCreate, ...emojisToUpdate, ...emojisToDelete); await db.batch(...emojisToCreate, ...emojisToUpdate, ...emojisToDelete);
}); });
return true; return true;
@ -66,26 +75,34 @@ export async function setCustomEmojis() {
const db = database.active; const db = database.active;
const emojisCollection = db.get('custom_emojis'); const emojisCollection = db.get('custom_emojis');
const allEmojis = await emojisCollection.query().fetch(); const allEmojis = await emojisCollection.query().fetch();
const parsed = allEmojis.reduce((ret, item) => { const parsed = allEmojis.reduce((ret: ICustomEmojis, item) => {
if (item.name) {
ret[item.name] = { ret[item.name] = {
name: item.name, name: item.name,
extension: item.extension extension: item.extension
}; };
}
if (item.aliases) {
item.aliases.forEach(alias => { item.aliases.forEach(alias => {
if (item.name) {
ret[alias] = { ret[alias] = {
name: item.name, name: item.name,
extension: item.extension extension: item.extension
}; };
}
}); });
}
return ret; return ret;
}, {}); }, {});
reduxStore.dispatch(setCustomEmojisAction(parsed)); reduxStore.dispatch(setCustomEmojisAction(parsed));
} }
export function getCustomEmojis() { export function getCustomEmojis() {
return new Promise(async resolve => { return new Promise<void>(async resolve => {
try { try {
const serverVersion = reduxStore.getState().server.version; const serverVersion = reduxStore.getState().server.version as string;
const db = database.active; const db = database.active;
const emojisCollection = db.get('custom_emojis'); const emojisCollection = db.get('custom_emojis');
const allRecords = await emojisCollection.query().fetch(); 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 server version is lower than 0.75.0, fetches from old api
if (compareServerVersion(serverVersion, 'lowerThan', '0.75.0')) { if (compareServerVersion(serverVersion, 'lowerThan', '0.75.0')) {
// RC 0.61.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; 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 }); const changedEmojis = await updateEmojis({ update: emojis, allRecords });
// `setCustomEmojis` is fired on selectServer // `setCustomEmojis` is fired on selectServer
@ -106,20 +124,21 @@ export function getCustomEmojis() {
setCustomEmojis(); setCustomEmojis();
} }
return resolve(); return resolve();
} else { }
const params = {}; const params: { updatedSince: string } = { updatedSince: '' };
if (updatedSince) { if (updatedSince) {
params.updatedSince = updatedSince; params.updatedSince = updatedSince;
} }
// RC 0.75.0 // RC 0.75.0
const result = await this.sdk.get('emoji-custom.list', params); const result = await sdk.get('emoji-custom.list', params);
if (!result.success) { if (!result.success) {
return resolve(); return resolve();
} }
const { emojis } = result; const { emojis } = result;
// @ts-ignore
const { update, remove } = emojis; const { update, remove } = emojis;
const changedEmojis = await updateEmojis({ update, remove, allRecords }); const changedEmojis = await updateEmojis({ update, remove, allRecords });
@ -128,7 +147,6 @@ export function getCustomEmojis() {
if (changedEmojis) { if (changedEmojis) {
setCustomEmojis(); setCustomEmojis();
} }
}
} catch (e) { } catch (e) {
log(e); log(e);
return resolve(); return resolve();