2019-06-10 18:36:56 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2019-06-10 18:36:56 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../database';
|
2019-06-10 18:36:56 +00:00
|
|
|
import log from '../../utils/log';
|
2019-09-16 20:26:32 +00:00
|
|
|
import protectedFunction from './helpers/protectedFunction';
|
2019-06-10 18:36:56 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
export default function() {
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
2019-06-17 13:57:07 +00:00
|
|
|
return new Promise(async(resolve) => {
|
|
|
|
try {
|
|
|
|
// RC 0.60.2
|
|
|
|
const result = await this.sdk.get('commands.list');
|
2019-06-10 18:36:56 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
if (!result.success) {
|
2019-08-30 12:43:23 +00:00
|
|
|
console.log(result);
|
2019-06-17 13:57:07 +00:00
|
|
|
return resolve();
|
|
|
|
}
|
2019-06-10 18:36:56 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
const { commands } = result;
|
2019-06-10 18:36:56 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
if (commands && commands.length) {
|
2019-09-16 20:26:32 +00:00
|
|
|
InteractionManager.runAfterInteractions(async() => {
|
|
|
|
await db.action(async() => {
|
|
|
|
const slashCommandsCollection = db.collections.get('slash_commands');
|
|
|
|
const allSlashCommandsRecords = await slashCommandsCollection.query().fetch();
|
|
|
|
|
|
|
|
// filter slash commands
|
|
|
|
let slashCommandsToCreate = commands.filter(i1 => !allSlashCommandsRecords.find(i2 => i1.command === i2.id));
|
|
|
|
let slashCommandsToUpdate = allSlashCommandsRecords.filter(i1 => commands.find(i2 => i1.id === i2.command));
|
|
|
|
|
|
|
|
// Create
|
|
|
|
slashCommandsToCreate = slashCommandsToCreate.map(command => slashCommandsCollection.prepareCreate(protectedFunction((s) => {
|
|
|
|
s._raw = sanitizedRaw({ id: command.command }, slashCommandsCollection.schema);
|
|
|
|
Object.assign(s, command);
|
|
|
|
})));
|
|
|
|
|
|
|
|
// Update
|
|
|
|
slashCommandsToUpdate = slashCommandsToUpdate.map((command) => {
|
|
|
|
const newCommand = commands.find(s => s.command === command.id);
|
|
|
|
return command.prepareUpdate(protectedFunction((s) => {
|
|
|
|
Object.assign(s, newCommand);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
const allRecords = [
|
|
|
|
...slashCommandsToCreate,
|
|
|
|
...slashCommandsToUpdate
|
|
|
|
];
|
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
try {
|
2019-09-16 20:26:32 +00:00
|
|
|
await db.batch(...allRecords);
|
2019-06-17 13:57:07 +00:00
|
|
|
} catch (e) {
|
2019-08-30 12:43:23 +00:00
|
|
|
log(e);
|
2019-06-17 13:57:07 +00:00
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
return allRecords.length;
|
|
|
|
});
|
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-06-10 18:36:56 +00:00
|
|
|
}
|
2019-06-17 13:57:07 +00:00
|
|
|
});
|
2019-06-10 18:36:56 +00:00
|
|
|
}
|