From a76a675f4c1beb5a63341bc9289c05baccab4f95 Mon Sep 17 00:00:00 2001 From: Dragory Date: Mon, 19 Feb 2018 00:46:15 +0200 Subject: [PATCH] legacyMigrator: fix open legacy threads having no message logs --- src/legacy/legacyMigrator.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/legacy/legacyMigrator.js b/src/legacy/legacyMigrator.js index 249ffd9..be8df88 100644 --- a/src/legacy/legacyMigrator.js +++ b/src/legacy/legacyMigrator.js @@ -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);