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
|
|
|
|
2021-03-18 13:33:35 +00:00
|
|
|
import { compareServerVersion, methods } from '../utils';
|
2018-04-24 19:34:03 +00:00
|
|
|
import reduxStore from '../createStore';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../database';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { setCustomEmojis as setCustomEmojisAction } from '../../actions/customEmojis';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const getUpdatedSince = allEmojis => {
|
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
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const updateEmojis = async ({ update = [], remove = [], allRecords }) => {
|
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');
|
2019-09-16 20:26:32 +00:00
|
|
|
let emojisToCreate = [];
|
|
|
|
let emojisToUpdate = [];
|
|
|
|
let emojisToDelete = [];
|
|
|
|
|
|
|
|
// 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));
|
2021-09-13 20:41:05 +00:00
|
|
|
emojisToCreate = emojisToCreate.map(emoji =>
|
|
|
|
emojisCollection.prepareCreate(e => {
|
|
|
|
e._raw = sanitizedRaw({ id: emoji._id }, emojisCollection.schema);
|
|
|
|
Object.assign(e, emoji);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
emojisToUpdate = emojisToUpdate.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) {
|
|
|
|
emojisToDelete = allRecords.filter(i1 => remove.find(i2 => i1.id === i2._id));
|
|
|
|
emojisToDelete = emojisToDelete.map(emoji => emoji.prepareDestroyPermanently());
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-09-13 20:41:05 +00:00
|
|
|
await db.action(async () => {
|
|
|
|
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();
|
|
|
|
const parsed = allEmojis.reduce((ret, item) => {
|
|
|
|
ret[item.name] = {
|
|
|
|
name: item.name,
|
|
|
|
extension: item.extension
|
|
|
|
};
|
2021-09-13 20:41:05 +00:00
|
|
|
item.aliases.forEach(alias => {
|
2019-09-16 20:26:32 +00:00
|
|
|
ret[alias] = {
|
|
|
|
name: item.name,
|
|
|
|
extension: item.extension
|
|
|
|
};
|
|
|
|
});
|
|
|
|
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() {
|
2021-09-13 20:41:05 +00:00
|
|
|
return new Promise(async resolve => {
|
2019-06-17 13:57:07 +00:00
|
|
|
try {
|
|
|
|
const serverVersion = reduxStore.getState().server.version;
|
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
|
2021-03-18 13:33:35 +00:00
|
|
|
if (compareServerVersion(serverVersion, '0.75.0', methods.lowerThan)) {
|
2019-06-17 13:57:07 +00:00
|
|
|
// RC 0.61.0
|
|
|
|
const result = await this.sdk.get('emoji-custom');
|
2019-04-26 21:13:07 +00:00
|
|
|
|
2021-02-23 18:36:20 +00:00
|
|
|
let { emojis } = result;
|
|
|
|
emojis = emojis.filter(emoji => !updatedSince || emoji._updatedAt > updatedSince);
|
|
|
|
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();
|
2019-06-17 13:57:07 +00:00
|
|
|
} else {
|
|
|
|
const params = {};
|
|
|
|
if (updatedSince) {
|
|
|
|
params.updatedSince = updatedSince;
|
|
|
|
}
|
2019-04-26 21:13:07 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
// RC 0.75.0
|
|
|
|
const result = await this.sdk.get('emoji-custom.list', params);
|
2019-04-26 21:13:07 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
if (!result.success) {
|
|
|
|
return resolve();
|
|
|
|
}
|
2019-04-26 21:13:07 +00:00
|
|
|
|
2021-02-23 18:36:20 +00:00
|
|
|
const { emojis } = result;
|
|
|
|
const { update, remove } = emojis;
|
|
|
|
const changedEmojis = await updateEmojis({ update, remove, allRecords });
|
2019-04-26 21:13:07 +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();
|
|
|
|
}
|
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
|
|
|
}
|