2019-09-16 20:26:32 +00:00
|
|
|
import orderBy from 'lodash/orderBy';
|
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2022-04-04 19:15:29 +00:00
|
|
|
import { ICustomEmojis } from '../../../reducers/customEmojis';
|
2022-04-07 14:19:54 +00:00
|
|
|
import { store as reduxStore } from '../../store/auxStore';
|
2022-04-04 19:15:29 +00:00
|
|
|
import database from '../../database';
|
|
|
|
import log from '../../../utils/log';
|
|
|
|
import { setCustomEmojis as setCustomEmojisAction } from '../../../actions/customEmojis';
|
|
|
|
import { ICustomEmoji, TCustomEmojiModel } from '../../../definitions';
|
|
|
|
import sdk from '../services/sdk';
|
2022-04-07 13:13:19 +00:00
|
|
|
import { compareServerVersion } from '../../methods/helpers/compareServerVersion';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2022-02-18 16:20:16 +00:00
|
|
|
interface IUpdateEmojis {
|
|
|
|
update: TCustomEmojiModel[];
|
|
|
|
remove?: TCustomEmojiModel[];
|
|
|
|
allRecords: TCustomEmojiModel[];
|
|
|
|
}
|
|
|
|
|
|
|
|
const getUpdatedSince = (allEmojis: ICustomEmoji[]) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
if (!allEmojis.length) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
const ordered = orderBy(
|
|
|
|
allEmojis.filter(item => item._updatedAt !== null),
|
|
|
|
['_updatedAt'],
|
|
|
|
['desc']
|
|
|
|
);
|
2019-09-16 20:26:32 +00:00
|
|
|
return ordered && ordered[0]._updatedAt.toISOString();
|
2018-04-24 19:34:03 +00:00
|
|
|
};
|
|
|
|
|
2022-02-18 16:20:16 +00:00
|
|
|
const updateEmojis = async ({ update = [], remove = [], allRecords }: IUpdateEmojis) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
if (!((update && update.length) || (remove && remove.length))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const emojisCollection = db.get('custom_emojis');
|
2022-02-18 16:20:16 +00:00
|
|
|
let emojisToCreate: TCustomEmojiModel[] = [];
|
|
|
|
let emojisToUpdate: TCustomEmojiModel[] = [];
|
|
|
|
let emojisToDelete: TCustomEmojiModel[] = [];
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
// Create or update
|
|
|
|
if (update && update.length) {
|
2022-02-18 16:20:16 +00:00
|
|
|
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 =>
|
2021-09-13 20:41:05 +00:00
|
|
|
emojisCollection.prepareCreate(e => {
|
|
|
|
e._raw = sanitizedRaw({ id: emoji._id }, emojisCollection.schema);
|
|
|
|
Object.assign(e, emoji);
|
|
|
|
})
|
|
|
|
);
|
2022-02-18 16:20:16 +00:00
|
|
|
emojisToUpdate = filterEmojisToUpdate.map(emoji => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const newEmoji = update.find(e => e._id === emoji.id);
|
2021-09-13 20:41:05 +00:00
|
|
|
return emoji.prepareUpdate(e => {
|
2019-09-16 20:26:32 +00:00
|
|
|
Object.assign(e, newEmoji);
|
|
|
|
});
|
2019-04-26 21:13:07 +00:00
|
|
|
});
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
if (remove && remove.length) {
|
2022-02-18 16:20:16 +00:00
|
|
|
const filterEmojisToDelete = allRecords.filter(i1 => remove.find(i2 => i1.id === i2._id));
|
|
|
|
emojisToDelete = filterEmojisToDelete.map(emoji => emoji.prepareDestroyPermanently());
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-02-18 16:20:16 +00:00
|
|
|
await db.write(async () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
await db.batch(...emojisToCreate, ...emojisToUpdate, ...emojisToDelete);
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2019-04-26 21:13:07 +00:00
|
|
|
};
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
export async function setCustomEmojis() {
|
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const emojisCollection = db.get('custom_emojis');
|
2019-09-16 20:26:32 +00:00
|
|
|
const allEmojis = await emojisCollection.query().fetch();
|
2022-02-18 16:20:16 +00:00
|
|
|
const parsed = allEmojis.reduce((ret: ICustomEmojis, item) => {
|
|
|
|
if (item.name) {
|
|
|
|
ret[item.name] = {
|
2019-09-16 20:26:32 +00:00
|
|
|
name: item.name,
|
|
|
|
extension: item.extension
|
|
|
|
};
|
2022-02-18 16:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (item.aliases) {
|
|
|
|
item.aliases.forEach(alias => {
|
|
|
|
if (item.name) {
|
|
|
|
ret[alias] = {
|
|
|
|
name: item.name,
|
|
|
|
extension: item.extension
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
return ret;
|
|
|
|
}, {});
|
|
|
|
reduxStore.dispatch(setCustomEmojisAction(parsed));
|
|
|
|
}
|
2019-04-26 21:13:07 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
export function getCustomEmojis() {
|
2022-02-18 16:20:16 +00:00
|
|
|
return new Promise<void>(async resolve => {
|
2019-06-17 13:57:07 +00:00
|
|
|
try {
|
2022-02-18 16:20:16 +00:00
|
|
|
const serverVersion = reduxStore.getState().server.version as string;
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const emojisCollection = db.get('custom_emojis');
|
2019-09-16 20:26:32 +00:00
|
|
|
const allRecords = await emojisCollection.query().fetch();
|
|
|
|
const updatedSince = await getUpdatedSince(allRecords);
|
2019-04-26 21:13:07 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
// if server version is lower than 0.75.0, fetches from old api
|
2022-02-07 18:44:04 +00:00
|
|
|
if (compareServerVersion(serverVersion, 'lowerThan', '0.75.0')) {
|
2019-06-17 13:57:07 +00:00
|
|
|
// RC 0.61.0
|
2022-02-18 16:20:16 +00:00
|
|
|
// @ts-ignore
|
|
|
|
const result = await sdk.get('emoji-custom');
|
|
|
|
// @ts-ignore
|
2021-02-23 18:36:20 +00:00
|
|
|
let { emojis } = result;
|
2022-02-18 16:20:16 +00:00
|
|
|
emojis = emojis.filter((emoji: TCustomEmojiModel) => !updatedSince || emoji._updatedAt.toISOString() > updatedSince);
|
2021-02-23 18:36:20 +00:00
|
|
|
const changedEmojis = await updateEmojis({ update: emojis, allRecords });
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-02-23 18:36:20 +00:00
|
|
|
// `setCustomEmojis` is fired on selectServer
|
|
|
|
// We run it again only if emojis were changed
|
|
|
|
if (changedEmojis) {
|
|
|
|
setCustomEmojis();
|
|
|
|
}
|
|
|
|
return resolve();
|
2022-02-18 16:20:16 +00:00
|
|
|
}
|
|
|
|
const params: { updatedSince: string } = { updatedSince: '' };
|
|
|
|
if (updatedSince) {
|
|
|
|
params.updatedSince = updatedSince;
|
|
|
|
}
|
2019-04-26 21:13:07 +00:00
|
|
|
|
2022-02-18 16:20:16 +00:00
|
|
|
// RC 0.75.0
|
|
|
|
const result = await sdk.get('emoji-custom.list', params);
|
2019-04-26 21:13:07 +00:00
|
|
|
|
2022-02-18 16:20:16 +00:00
|
|
|
if (!result.success) {
|
|
|
|
return resolve();
|
|
|
|
}
|
2019-04-26 21:13:07 +00:00
|
|
|
|
2022-02-18 16:20:16 +00:00
|
|
|
const { emojis } = result;
|
|
|
|
// @ts-ignore
|
|
|
|
const { update, remove } = emojis;
|
|
|
|
const changedEmojis = await updateEmojis({ update, remove, allRecords });
|
2019-04-26 21:13:07 +00:00
|
|
|
|
2022-02-18 16:20:16 +00:00
|
|
|
// `setCustomEmojis` is fired on selectServer
|
|
|
|
// We run it again only if emojis were changed
|
|
|
|
if (changedEmojis) {
|
|
|
|
setCustomEmojis();
|
2019-06-17 13:57:07 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-06-17 13:57:07 +00:00
|
|
|
return resolve();
|
2019-04-26 21:13:07 +00:00
|
|
|
}
|
2019-06-17 13:57:07 +00:00
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|