legacyMigrator: fix open legacy threads having no message logs

master
Dragory 2018-02-19 00:46:15 +02:00
parent 87a2c885f8
commit a76a675f4c
1 changed files with 21 additions and 2 deletions

View File

@ -2,7 +2,7 @@ const fs = require('fs');
const path = require('path');
const promisify = require('util').promisify;
const moment = require('moment');
const uuid = require('uuid');
const Eris = require('eris');
const knex = require('../knex');
const config = require('../config');
@ -68,6 +68,9 @@ async function shouldMigrate() {
}
async function migrateOpenThreads() {
const bot = new Eris.Client(config.token);
await bot.connect();
const oldThreads = await jsonDb.get('threads', []);
const promises = oldThreads.map(async oldThread => {
const existingOpenThread = await knex('threads')
@ -76,6 +79,12 @@ async function migrateOpenThreads() {
if (existingOpenThread) return;
const threadMessages = await bot.getChannel(oldThread.channelId).getMessages(1000);
const log = threadMessages.reverse().map(msg => {
const date = moment.utc(msg.timestamp, 'x').format('YYYY-MM-DD HH:mm:ss');
return `[${date}] ${msg.author.username}#${msg.author.discriminator}: ${msg.content}`;
}).join('\n') + '\n';
const newThread = {
status: THREAD_STATUS.OPEN,
user_id: oldThread.userId,
@ -84,7 +93,17 @@ async function migrateOpenThreads() {
is_legacy: 1
};
return threads.createThreadInDB(newThread);
const threadId = await threads.createThreadInDB(newThread);
await trx('thread_messages').insert({
thread_id: threadId,
message_type: THREAD_MESSAGE_TYPE.LEGACY,
user_id: oldThread.userId,
user_name: '',
body: log,
is_anonymous: 0,
created_at: moment.utc().format('YYYY-MM-DD HH:mm:ss')
});
});
return Promise.all(promises);