From 12a9a645c914063f1af3b341482daae630844eb4 Mon Sep 17 00:00:00 2001 From: Xeltica Date: Thu, 20 Apr 2023 14:26:28 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=A6=E3=83=BC=E3=82=B6=E3=83=BC=E3=83=9E?= =?UTF-8?q?=E3=82=A4=E3=82=B0=E3=83=AC=E6=A9=9F=E8=83=BD=E3=82=92=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/backend/src/app.ts | 7 +-- apps/backend/src/boot/server.ts | 11 +++-- .../src/services/migration/legacy-user.ts | 47 +++++++++++++++++++ apps/backend/src/utils/visibility.ts | 21 +++++++++ 4 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 apps/backend/src/services/migration/legacy-user.ts create mode 100644 apps/backend/src/utils/visibility.ts diff --git a/apps/backend/src/app.ts b/apps/backend/src/app.ts index 1e51bcf..e4f8911 100644 --- a/apps/backend/src/app.ts +++ b/apps/backend/src/app.ts @@ -1,6 +1,7 @@ import 'reflect-metadata'; import axios from 'axios'; import {config} from './config.js'; +import { prisma } from './libs/prisma.js'; export type { AppRouter } from '@/server/api/index.js'; @@ -11,6 +12,6 @@ axios.defaults.headers['Content-Type'] = 'application/json'; axios.defaults.validateStatus = (stat) => stat < 500; (async () => { - (await import('./boot/server.js')).default(); - (await import('./boot/worker.js')).default(); -})(); + await (await import('./boot/server.js')).default(); + (await import('./boot/worker.js')).default(); +})() diff --git a/apps/backend/src/boot/server.ts b/apps/backend/src/boot/server.ts index 512bf5e..03ff640 100644 --- a/apps/backend/src/boot/server.ts +++ b/apps/backend/src/boot/server.ts @@ -2,12 +2,13 @@ import 'reflect-metadata'; import { config, meta } from '@/config.js'; import { startServer } from '@/server/index.js'; +import { migrateLegacyUser } from '@/services/migration/legacy-user.js'; -export default (): void => { +export default async () => { console.log(`** Misskey Tools ${meta.version} **`); console.log('(C) Shrimpia Network'); - startServer().then(() => { - console.log('GET READY!'); - console.log(`Server URL >> ${config.url}`); - }); + await migrateLegacyUser(); + await startServer(); + console.log('GET READY!'); + console.log(`Server URL >> ${config.url}`); }; diff --git a/apps/backend/src/services/migration/legacy-user.ts b/apps/backend/src/services/migration/legacy-user.ts new file mode 100644 index 0000000..be7b4be --- /dev/null +++ b/apps/backend/src/services/migration/legacy-user.ts @@ -0,0 +1,47 @@ +import { prisma } from "@/libs/prisma.js"; +import { normalize } from "@/utils/visibility.js"; + +/** + * user テーブルを account, misskey_session, misshai_account テーブルに移行します。 + */ +export const migrateLegacyUser = async () => { + if ((await prisma.user.count()) === 0) return; + console.log('System will migrate legacy `user` table into `accounts` table.'); + const users = await prisma.user.findMany(); + for (const user of users) { + const account = await prisma.account.create({ + data: { + name: `@${user.username}@${user.host}`, + accessToken: user.misshaiToken, + } + }); + const session = await prisma.misskeySession.create({ + data: { + username: user.username, + host: user.host, + token: user.token, + tokenVersion: user.tokenVersion, + accountId: account.id, + }, + }); + await prisma.misshaiAccount.create({ + data: { + misskeySessionId: session.id, + alertAsNote: user.alertMode === 'note' || user.alertMode === 'both', + alertAsNotification: user.alertMode === 'notification' || user.alertMode === 'both', + noteVisibility: normalize(user.visibility), + noteLocalOnly: user.localOnly, + template: user.template, + bannedFromRanking: user.bannedFromRanking, + rankingVisible: user.useRanking, + } + }); + await prisma.user.delete({ + where: { + id: user.id, + } + }); + console.log(`Processed for ${user.username}@${user.host}`); + } + console.log('All done.'); +}; diff --git a/apps/backend/src/utils/visibility.ts b/apps/backend/src/utils/visibility.ts new file mode 100644 index 0000000..0f8d36b --- /dev/null +++ b/apps/backend/src/utils/visibility.ts @@ -0,0 +1,21 @@ +import { Visibility } from "@prisma/client"; + +export const noteVisibilities = [ + 'home', + 'followers', +] as const; + +export type NoteVisibility = typeof noteVisibilities[number]; + +export const normalize = (v: Visibility) => { + switch (v) { + case 'public': + case 'home': + case 'users': + return 'home'; + case 'followers': + return 'followers'; + default: + throw new Error(`Unknown visibility type ${v}`); + } +}