2019-06-10 18:36:56 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
2019-10-07 20:56:30 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2019-06-10 18:36:56 +00:00
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
import database from '../database';
|
2019-06-10 18:36:56 +00:00
|
|
|
import log from '../../utils/log';
|
2019-10-07 20:56:30 +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-10-07 20:56:30 +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-09-03 19:27:57 +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-10-07 20:56:30 +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));
|
2020-02-19 19:43:47 +00:00
|
|
|
let slashCommandsToDelete = allSlashCommandsRecords
|
|
|
|
.filter(i1 => !slashCommandsToCreate.find(i2 => i2.command === i1.id) && !slashCommandsToUpdate.find(i2 => i2.id === i1.id));
|
2019-10-07 20:56:30 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2020-02-19 19:43:47 +00:00
|
|
|
// Delete
|
|
|
|
slashCommandsToDelete = slashCommandsToDelete.map(command => command.prepareDestroyPermanently());
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
const allRecords = [
|
|
|
|
...slashCommandsToCreate,
|
2020-02-19 19:43:47 +00:00
|
|
|
...slashCommandsToUpdate,
|
|
|
|
...slashCommandsToDelete
|
2019-10-07 20:56:30 +00:00
|
|
|
];
|
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
try {
|
2019-10-07 20:56:30 +00:00
|
|
|
await db.batch(...allRecords);
|
2019-06-17 13:57:07 +00:00
|
|
|
} catch (e) {
|
2019-09-03 19:27:57 +00:00
|
|
|
log(e);
|
2019-06-17 13:57:07 +00:00
|
|
|
}
|
2019-10-07 20:56:30 +00:00
|
|
|
return allRecords.length;
|
|
|
|
});
|
2019-06-17 13:57:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-09-03 19:27:57 +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
|
|
|
}
|