refactor: migrate to typeorm 3.0 (#8443)

* wip

* wip

* wip

* Update following.ts

* wip

* wip

* wip

* Update resolve-user.ts

* maxQueryExecutionTime

* wip

* wip
This commit is contained in:
syuilo
2022-03-26 15:34:00 +09:00
committed by GitHub
parent 41c87074e6
commit 1c67c26bd8
325 changed files with 1314 additions and 1494 deletions

View File

@@ -2,7 +2,7 @@ import { Feed } from 'feed';
import config from '@/config/index.js';
import { User } from '@/models/entities/user.js';
import { Notes, DriveFiles, UserProfiles } from '@/models/index.js';
import { In } from 'typeorm';
import { In, IsNull } from 'typeorm';
export default async function(user: User) {
const author = {
@@ -10,12 +10,12 @@ export default async function(user: User) {
name: user.name || user.username,
};
const profile = await UserProfiles.findOneOrFail(user.id);
const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
const notes = await Notes.find({
where: {
userId: user.id,
renoteId: null,
renoteId: IsNull(),
visibility: In(['public', 'home']),
},
order: { createdAt: -1 },
@@ -39,7 +39,7 @@ export default async function(user: User) {
});
for (const note of notes) {
const files = note.fileIds.length > 0 ? await DriveFiles.find({
const files = note.fileIds.length > 0 ? await DriveFiles.findBy({
id: In(note.fileIds),
}) : [];
const file = files.find(file => file.type.startsWith('image/'));

View File

@@ -24,6 +24,7 @@ import { getNoteSummary } from '@/misc/get-note-summary.js';
import { urlPreviewHandler } from './url-preview.js';
import { manifestHandler } from './manifest.js';
import { queues } from '@/queue/queues.js';
import { IsNull } from 'typeorm';
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
@@ -46,7 +47,7 @@ app.use(async (ctx, next) => {
ctx.status = 401;
return;
}
const user = await Users.findOne({ token });
const user = await Users.findOneBy({ token });
if (user == null || !(user.isAdmin || user.isModerator)) {
ctx.status = 403;
return;
@@ -168,9 +169,9 @@ router.get('/api.json', async ctx => {
const getFeed = async (acct: string) => {
const { username, host } = Acct.parse(acct);
const user = await Users.findOne({
const user = await Users.findOneBy({
usernameLower: username.toLowerCase(),
host,
host: host ?? IsNull(),
isSuspended: false,
});
@@ -217,14 +218,14 @@ router.get('/@:user.json', async ctx => {
// User
router.get(['/@:user', '/@:user/:sub'], async (ctx, next) => {
const { username, host } = Acct.parse(ctx.params.user);
const user = await Users.findOne({
const user = await Users.findOneBy({
usernameLower: username.toLowerCase(),
host,
host: host ?? IsNull(),
isSuspended: false,
});
if (user != null) {
const profile = await UserProfiles.findOneOrFail(user.id);
const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
const meta = await fetchMeta();
const me = profile.fields
? profile.fields
@@ -248,9 +249,9 @@ router.get(['/@:user', '/@:user/:sub'], async (ctx, next) => {
});
router.get('/users/:user', async ctx => {
const user = await Users.findOne({
const user = await Users.findOneBy({
id: ctx.params.user,
host: null,
host: IsNull(),
isSuspended: false,
});
@@ -264,11 +265,11 @@ router.get('/users/:user', async ctx => {
// Note
router.get('/notes/:note', async (ctx, next) => {
const note = await Notes.findOne(ctx.params.note);
const note = await Notes.findOneBy({ id: ctx.params.note });
if (note) {
const _note = await Notes.pack(note);
const profile = await UserProfiles.findOneOrFail(note.userId);
const profile = await UserProfiles.findOneByOrFail({ userId: note.userId });
const meta = await fetchMeta();
await ctx.render('note', {
note: _note,
@@ -295,21 +296,21 @@ router.get('/notes/:note', async (ctx, next) => {
// Page
router.get('/@:user/pages/:page', async (ctx, next) => {
const { username, host } = Acct.parse(ctx.params.user);
const user = await Users.findOne({
const user = await Users.findOneBy({
usernameLower: username.toLowerCase(),
host,
host: host ?? IsNull(),
});
if (user == null) return;
const page = await Pages.findOne({
const page = await Pages.findOneBy({
name: ctx.params.page,
userId: user.id,
});
if (page) {
const _page = await Pages.pack(page);
const profile = await UserProfiles.findOneOrFail(page.userId);
const profile = await UserProfiles.findOneByOrFail({ userId: page.userId });
const meta = await fetchMeta();
await ctx.render('page', {
page: _page,
@@ -334,13 +335,13 @@ router.get('/@:user/pages/:page', async (ctx, next) => {
// Clip
// TODO: 非publicなclipのハンドリング
router.get('/clips/:clip', async (ctx, next) => {
const clip = await Clips.findOne({
const clip = await Clips.findOneBy({
id: ctx.params.clip,
});
if (clip) {
const _clip = await Clips.pack(clip);
const profile = await UserProfiles.findOneOrFail(clip.userId);
const profile = await UserProfiles.findOneByOrFail({ userId: clip.userId });
const meta = await fetchMeta();
await ctx.render('clip', {
clip: _clip,
@@ -360,11 +361,11 @@ router.get('/clips/:clip', async (ctx, next) => {
// Gallery post
router.get('/gallery/:post', async (ctx, next) => {
const post = await GalleryPosts.findOne(ctx.params.post);
const post = await GalleryPosts.findOneBy({ id: ctx.params.post });
if (post) {
const _post = await GalleryPosts.pack(post);
const profile = await UserProfiles.findOneOrFail(post.userId);
const profile = await UserProfiles.findOneByOrFail({ userId: post.userId });
const meta = await fetchMeta();
await ctx.render('gallery-post', {
post: _post,
@@ -384,7 +385,7 @@ router.get('/gallery/:post', async (ctx, next) => {
// Channel
router.get('/channels/:channel', async (ctx, next) => {
const channel = await Channels.findOne({
const channel = await Channels.findOneBy({
id: ctx.params.channel,
});
@@ -416,8 +417,8 @@ router.get('/_info_card_', async ctx => {
version: config.version,
host: config.host,
meta: meta,
originalUsersCount: await Users.count({ host: null }),
originalNotesCount: await Notes.count({ userHost: null }),
originalUsersCount: await Users.countBy({ host: IsNull() }),
originalNotesCount: await Notes.countBy({ userHost: IsNull() }),
});
});