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';
|
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) {
|
2019-05-20 20:43:50 +00:00
|
|
|
// log('getEmojis create', e);
|
2019-04-26 21:13:07 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
export default function() {
|
|
|
|
return new Promise(async(resolve) => {
|
|
|
|
try {
|
|
|
|
const serverVersion = reduxStore.getState().server.version;
|
|
|
|
const updatedSince = getUpdatedSince();
|
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
|
|
|
|
if (semver.lt(serverVersion, '0.75.0')) {
|
|
|
|
// RC 0.61.0
|
|
|
|
const result = await this.sdk.get('emoji-custom');
|
2019-04-26 21:13:07 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
InteractionManager.runAfterInteractions(() => {
|
|
|
|
let { emojis } = result;
|
|
|
|
emojis = emojis.filter(emoji => !updatedSince || emoji._updatedAt > updatedSince);
|
|
|
|
database.write(() => {
|
|
|
|
create(emojis);
|
|
|
|
});
|
|
|
|
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-06-17 13:57:07 +00:00
|
|
|
InteractionManager.runAfterInteractions(
|
|
|
|
() => database.write(() => {
|
|
|
|
const { emojis } = result;
|
|
|
|
create(emojis.update);
|
2019-04-26 21:13:07 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
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) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-04-26 21:13:07 +00:00
|
|
|
}
|
2019-06-17 13:57:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return resolve();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} 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
|
|
|
}
|