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';
export interface ICustomEmoji {
_id: string;
name?: string;
aliases?: string;
aliases?: string[];
extension: string;
_updatedAt: Date;
}

View File

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

View File

@ -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) => {
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<void>(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,20 +124,21 @@ export function getCustomEmojis() {
setCustomEmojis();
}
return resolve();
} else {
const params = {};
}
const params: { updatedSince: string } = { updatedSince: '' };
if (updatedSince) {
params.updatedSince = updatedSince;
}
// 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) {
return resolve();
}
const { emojis } = result;
// @ts-ignore
const { update, remove } = emojis;
const changedEmojis = await updateEmojis({ update, remove, allRecords });
@ -128,7 +147,6 @@ export function getCustomEmojis() {
if (changedEmojis) {
setCustomEmojis();
}
}
} catch (e) {
log(e);
return resolve();