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';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
import reduxStore from '../createStore';
|
|
|
|
import database from '../realm';
|
|
|
|
import * as actions from '../../actions';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-04-26 21:13:07 +00:00
|
|
|
const getUpdatedSince = () => {
|
|
|
|
const emoji = database.objects('customEmojis').sorted('_updatedAt', true)[0];
|
|
|
|
return emoji && emoji._updatedAt.toISOString();
|
2018-04-24 19:34:03 +00:00
|
|
|
};
|
|
|
|
|
2019-04-26 21:13:07 +00:00
|
|
|
const create = (customEmojis) => {
|
|
|
|
if (customEmojis && customEmojis.length) {
|
|
|
|
customEmojis.forEach((emoji) => {
|
|
|
|
try {
|
|
|
|
database.create('customEmojis', emoji, true);
|
|
|
|
} catch (e) {
|
|
|
|
log('getEmojis create', e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
export default async function() {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2019-04-26 21:13:07 +00:00
|
|
|
const serverVersion = reduxStore.getState().server.version;
|
|
|
|
const updatedSince = getUpdatedSince();
|
|
|
|
|
|
|
|
// if server version is lower than 0.75.0, fetches from old api
|
|
|
|
if (semver.lt(serverVersion, '0.75.0')) {
|
|
|
|
// RC 0.61.0
|
|
|
|
const result = await this.sdk.get('emoji-custom');
|
|
|
|
|
|
|
|
InteractionManager.runAfterInteractions(() => {
|
|
|
|
let { emojis } = result;
|
|
|
|
emojis = emojis.filter(emoji => !updatedSince || emoji._updatedAt > updatedSince);
|
|
|
|
database.write(() => {
|
|
|
|
create(emojis);
|
2019-02-25 16:23:17 +00:00
|
|
|
});
|
2019-04-26 21:13:07 +00:00
|
|
|
reduxStore.dispatch(actions.setCustomEmojis(this.parseEmojis(result.emojis)));
|
2019-02-25 16:23:17 +00:00
|
|
|
});
|
2019-04-26 21:13:07 +00:00
|
|
|
} else {
|
|
|
|
const params = {};
|
|
|
|
if (updatedSince) {
|
|
|
|
params.updatedSince = updatedSince;
|
|
|
|
}
|
|
|
|
|
|
|
|
// RC 0.75.0
|
|
|
|
const result = await this.sdk.get('emoji-custom.list', params);
|
|
|
|
|
|
|
|
if (!result.success) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
InteractionManager.runAfterInteractions(
|
|
|
|
() => database.write(() => {
|
|
|
|
const { emojis } = result;
|
|
|
|
create(emojis.update);
|
|
|
|
|
|
|
|
if (emojis.delete && emojis.delete.length) {
|
|
|
|
emojis.delete.forEach((emoji) => {
|
|
|
|
try {
|
|
|
|
const emojiRecord = database.objectForPrimaryKey('customEmojis', emoji._id);
|
|
|
|
if (emojiRecord) {
|
|
|
|
database.delete(emojiRecord);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log('getEmojis delete', e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const allEmojis = database.objects('customEmojis');
|
|
|
|
reduxStore.dispatch(actions.setCustomEmojis(this.parseEmojis(allEmojis)));
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('getCustomEmojis', e);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|