ユーザーマイグレ機能を実装

This commit is contained in:
Xeltica 2023-04-20 14:26:28 +09:00
parent d1ddda9b2c
commit 12a9a645c9
4 changed files with 78 additions and 8 deletions

View File

@ -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 (await import('./boot/server.js')).default();
(await import('./boot/worker.js')).default();
})();
})()

View File

@ -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(() => {
await migrateLegacyUser();
await startServer();
console.log('GET READY!');
console.log(`Server URL >> ${config.url}`);
});
};

View File

@ -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.');
};

View File

@ -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}`);
}
}