2018-04-24 19:34:03 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
2019-04-26 21:13:07 +00:00
|
|
|
import semver from 'semver';
|
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
|
|
|
|
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
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const getUpdatedSince = (allEmojis) => {
|
|
|
|
if (!allEmojis.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const ordered = orderBy(allEmojis.filter(item => item._updatedAt !== null), ['_updatedAt'], ['desc']);
|
|
|
|
return ordered && ordered[0]._updatedAt.toISOString();
|
2018-04-24 19:34:03 +00:00
|
|
|
};
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const updateEmojis = async({ update = [], remove = [], allRecords }) => {
|
|
|
|
if (!((update && update.length) || (remove && remove.length))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const db = database.active;
|
|
|
|
const emojisCollection = db.collections.get('custom_emojis');
|
|
|
|
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));
|
|
|
|
emojisToCreate = emojisToCreate.map(emoji => emojisCollection.prepareCreate((e) => {
|
|
|
|
e._raw = sanitizedRaw({ id: emoji._id }, emojisCollection.schema);
|
|
|
|
Object.assign(e, emoji);
|
|
|
|
}));
|
|
|
|
emojisToUpdate = emojisToUpdate.map((emoji) => {
|
|
|
|
const newEmoji = update.find(e => e._id === emoji.id);
|
|
|
|
return emoji.prepareUpdate((e) => {
|
|
|
|
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 {
|
|
|
|
await db.action(async() => {
|
|
|
|
await db.batch(
|
|
|
|
...emojisToCreate,
|
|
|
|
...emojisToUpdate,
|
|
|
|
...emojisToDelete
|
|
|
|
);
|
|
|
|
});
|
|
|
|
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;
|
|
|
|
const emojisCollection = db.collections.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] = {
|
|
|
|
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() {
|
2019-06-17 13:57:07 +00:00
|
|
|
return new Promise(async(resolve) => {
|
|
|
|
try {
|
|
|
|
const serverVersion = reduxStore.getState().server.version;
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
|
|
|
const emojisCollection = db.collections.get('custom_emojis');
|
|
|
|
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
|
2019-11-18 17:50:00 +00:00
|
|
|
if (serverVersion && semver.lt(serverVersion, '0.75.0')) {
|
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
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
InteractionManager.runAfterInteractions(async() => {
|
2019-06-17 13:57:07 +00:00
|
|
|
let { emojis } = result;
|
|
|
|
emojis = emojis.filter(emoji => !updatedSince || emoji._updatedAt > updatedSince);
|
2019-09-16 20:26:32 +00:00
|
|
|
const changedEmojis = await updateEmojis({ update: emojis, allRecords });
|
|
|
|
|
|
|
|
// `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
|
|
|
return resolve();
|
2019-02-25 16:23:17 +00:00
|
|
|
});
|
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
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
InteractionManager.runAfterInteractions(async() => {
|
|
|
|
const { emojis } = result;
|
|
|
|
const { update, remove } = emojis;
|
|
|
|
const changedEmojis = await updateEmojis({ update, remove, allRecords });
|
2019-04-26 21:13:07 +00:00
|
|
|
|
2019-09-16 20:26:32 +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
|
|
|
}
|