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

@ -24,13 +24,13 @@ export default class DbResolver {
const parsed = this.parseUri(value);
if (parsed.id) {
return (await Notes.findOne({
return (await Notes.findOneBy({
id: parsed.id,
})) || null;
}
if (parsed.uri) {
return (await Notes.findOne({
return (await Notes.findOneBy({
uri: parsed.uri,
})) || null;
}
@ -42,13 +42,13 @@ export default class DbResolver {
const parsed = this.parseUri(value);
if (parsed.id) {
return (await MessagingMessages.findOne({
return (await MessagingMessages.findOneBy({
id: parsed.id,
})) || null;
}
if (parsed.uri) {
return (await MessagingMessages.findOne({
return (await MessagingMessages.findOneBy({
uri: parsed.uri,
})) || null;
}
@ -63,13 +63,13 @@ export default class DbResolver {
const parsed = this.parseUri(value);
if (parsed.id) {
return (await Users.findOne({
return (await Users.findOneBy({
id: parsed.id,
})) || null;
}
if (parsed.uri) {
return (await Users.findOne({
return (await Users.findOneBy({
uri: parsed.uri,
})) || null;
}
@ -85,7 +85,7 @@ export default class DbResolver {
key: UserPublickey;
} | null> {
const key = await publicKeyCache.fetch(keyId, async () => {
const key = await UserPublickeys.findOne({
const key = await UserPublickeys.findOneBy({
keyId,
});
@ -97,7 +97,7 @@ export default class DbResolver {
if (key == null) return null;
return {
user: await userByIdCache.fetch(key.userId, () => Users.findOneOrFail(key.userId)) as CacheableRemoteUser,
user: await userByIdCache.fetch(key.userId, () => Users.findOneByOrFail({ id: key.userId })) as CacheableRemoteUser,
key,
};
}
@ -113,7 +113,7 @@ export default class DbResolver {
if (user == null) return null;
const key = await publicKeyByUserIdCache.fetch(user.id, () => UserPublickeys.findOne(user.id).then(x => x || null), v => v != null); // TODO: typeorm 3.0 にしたら.then(x => x || null)は消せる
const key = await publicKeyByUserIdCache.fetch(user.id, () => UserPublickeys.findOneBy({ userId: user.id }), v => v != null);
return {
user,

View File

@ -82,7 +82,7 @@ export default class DeliverManager {
for (const recipe of this.recipes) {
if (isFollowers(recipe)) {
// followers deliver
const followers = await Followings.find({
const followers = await Followings.findBy({
followeeId: this.actor.id,
});

View File

@ -18,6 +18,6 @@ export default async (actor: CacheableRemoteUser, activity: IBlock): Promise<str
return `skip: ブロックしようとしているユーザーはローカルユーザーではありません`;
}
await block(await Users.findOneOrFail(actor.id), blockee);
await block(await Users.findOneByOrFail({ id: actor.id }), blockee);
return `ok`;
};

View File

@ -12,7 +12,7 @@ export async function deleteActor(actor: CacheableRemoteUser, uri: string): Prom
return `skip: delete actor ${actor.uri} !== ${uri}`;
}
const user = await Users.findOneOrFail(actor.id);
const user = await Users.findOneByOrFail({ id: actor.id });
if (user.isDeleted) {
logger.info(`skip: already deleted`);
}

View File

@ -11,7 +11,7 @@ export default async (actor: CacheableRemoteUser, activity: IFlag): Promise<stri
const uris = getApIds(activity.object);
const userIds = uris.filter(uri => uri.startsWith(config.url + '/users/')).map(uri => uri.split('/').pop()!);
const users = await Users.find({
const users = await Users.findBy({
id: In(userIds),
});
if (users.length < 1) return `skip`;

View File

@ -13,7 +13,7 @@ export const performReadActivity = async (actor: CacheableRemoteUser, activity:
const messageId = id.split('/').pop();
const message = await MessagingMessages.findOne(messageId);
const message = await MessagingMessages.findOneBy({ id: messageId });
if (message == null) {
return `skip: message not found`;
}

View File

@ -13,7 +13,7 @@ export default async (actor: CacheableRemoteUser, activity: IAccept): Promise<st
return `skip: follower not found`;
}
const following = await Followings.findOne({
const following = await Followings.findOneBy({
followerId: follower.id,
followeeId: actor.id,
});

View File

@ -6,7 +6,7 @@ import deleteNote from '@/services/note/delete.js';
export const undoAnnounce = async (actor: CacheableRemoteUser, activity: IAnnounce): Promise<string> => {
const uri = getApId(activity);
const note = await Notes.findOne({
const note = await Notes.findOneBy({
uri,
});

View File

@ -16,6 +16,6 @@ export default async (actor: CacheableRemoteUser, activity: IBlock): Promise<str
return `skip: ブロック解除しようとしているユーザーはローカルユーザーではありません`;
}
await unblock(await Users.findOneOrFail(actor.id), blockee);
await unblock(await Users.findOneByOrFail({ id: actor.id }), blockee);
return `ok`;
};

View File

@ -17,12 +17,12 @@ export default async (actor: CacheableRemoteUser, activity: IFollow): Promise<st
return `skip: フォロー解除しようとしているユーザーはローカルユーザーではありません`;
}
const req = await FollowRequests.findOne({
const req = await FollowRequests.findOneBy({
followerId: actor.id,
followeeId: followee.id,
});
const following = await Followings.findOne({
const following = await Followings.findOneBy({
followerId: actor.id,
followeeId: followee.id,
});

View File

@ -47,7 +47,7 @@ export async function createImage(actor: CacheableRemoteUser, value: any): Promi
uri: image.url,
});
file = await DriveFiles.findOneOrFail(file.id);
file = await DriveFiles.findOneByOrFail({ id: file.id });
}
}

View File

@ -141,7 +141,7 @@ export async function createNote(value: string | IObject, resolver?: Resolver, s
const uri = getApId(note.inReplyTo);
if (uri.startsWith(config.url + '/')) {
const id = uri.split('/').pop();
const talk = await MessagingMessages.findOne(id);
const talk = await MessagingMessages.findOneBy({ id });
if (talk) {
isTalk = true;
return null;
@ -201,7 +201,7 @@ export async function createNote(value: string | IObject, resolver?: Resolver, s
// vote
if (reply && reply.hasPoll) {
const poll = await Polls.findOneOrFail(reply.id);
const poll = await Polls.findOneByOrFail({ noteId: reply.id });
const tryCreateVote = async (name: string, index: number): Promise<null> => {
if (poll.expiresAt && Date.now() > new Date(poll.expiresAt).getTime()) {
@ -306,7 +306,7 @@ export async function extractEmojis(tags: IObject | IObject[], host: string): Pr
const name = tag.name!.replace(/^:/, '').replace(/:$/, '');
tag.icon = toSingle(tag.icon);
const exists = await Emojis.findOne({
const exists = await Emojis.findOneBy({
host,
name,
});
@ -327,7 +327,7 @@ export async function extractEmojis(tags: IObject | IObject[], host: string): Pr
updatedAt: new Date(),
});
return await Emojis.findOne({
return await Emojis.findOneBy({
host,
name,
}) as Emoji;
@ -347,6 +347,6 @@ export async function extractEmojis(tags: IObject | IObject[], host: string): Pr
publicUrl: tag.icon!.url,
updatedAt: new Date(),
aliases: [],
} as Partial<Emoji>).then(x => Emojis.findOneOrFail(x.identifiers[0]));
} as Partial<Emoji>).then(x => Emojis.findOneByOrFail(x.identifiers[0]));
}));
}

View File

@ -24,7 +24,6 @@ import { UserPublickey } from '@/models/entities/user-publickey.js';
import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js';
import { toPuny } from '@/misc/convert-host.js';
import { UserProfile } from '@/models/entities/user-profile.js';
import { getConnection } from 'typeorm';
import { toArray } from '@/prelude/array.js';
import { fetchInstanceMetadata } from '@/services/fetch-instance-metadata.js';
import { normalizeForSearch } from '@/misc/normalize-for-search.js';
@ -32,6 +31,7 @@ import { truncate } from '@/misc/truncate.js';
import { StatusError } from '@/misc/fetch.js';
import { uriPersonCache } from '@/services/user-cache.js';
import { publishInternalEvent } from '@/services/stream.js';
import { db } from '@/db/postgre.js';
const logger = apLogger;
@ -102,13 +102,13 @@ export async function fetchPerson(uri: string, resolver?: Resolver): Promise<Cac
// URIがこのサーバーを指しているならデータベースからフェッチ
if (uri.startsWith(config.url + '/')) {
const id = uri.split('/').pop();
const u = await Users.findOne(id).then(x => x || null); // TODO: typeorm 3.0 にしたら .then(x => x || null) を消す
const u = await Users.findOneBy({ id });
if (u) uriPersonCache.set(uri, u);
return u;
}
//#region このサーバーに既に登録されていたらそれを返す
const exist = await Users.findOne({ uri });
const exist = await Users.findOneBy({ uri });
if (exist) {
uriPersonCache.set(uri, exist);
@ -151,7 +151,7 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise<Us
let user: IRemoteUser;
try {
// Start transaction
await getConnection().transaction(async transactionalEntityManager => {
await db.transaction(async transactionalEntityManager => {
user = await transactionalEntityManager.save(new User({
id: genId(),
avatarId: null,
@ -197,7 +197,7 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise<Us
// duplicate key error
if (isDuplicateKeyValueError(e)) {
// /users/@a => /users/:id のように入力がaliasなときにエラーになることがあるのを対応
const u = await Users.findOne({
const u = await Users.findOneBy({
uri: person.id,
});
@ -280,7 +280,7 @@ export async function updatePerson(uri: string, resolver?: Resolver | null, hint
}
//#region このサーバーに既に登録されているか
const exist = await Users.findOne({ uri }) as IRemoteUser;
const exist = await Users.findOneBy({ uri }) as IRemoteUser;
if (exist == null) {
return;
@ -451,7 +451,7 @@ export function analyzeAttachments(attachments: IObject | IObject[] | undefined)
}
export async function updateFeatured(userId: User['id']) {
const user = await Users.findOneOrFail(userId);
const user = await Users.findOneByOrFail({ id: userId });
if (!Users.isRemoteUser(user)) return;
if (!user.featured) return;
@ -474,7 +474,7 @@ export async function updateFeatured(userId: User['id']) {
.slice(0, 5)
.map(item => limit(() => resolveNote(item, resolver))));
await getConnection().transaction(async transactionalEntityManager => {
await db.transaction(async transactionalEntityManager => {
await transactionalEntityManager.delete(UserNotePining, { userId: user.id });
// とりあえずidを別の時間で生成して順番を維持

View File

@ -47,10 +47,10 @@ export async function updateQuestion(value: any) {
if (uri.startsWith(config.url + '/')) throw new Error('uri points local');
//#region このサーバーに既に登録されているか
const note = await Notes.findOne({ uri });
const note = await Notes.findOneBy({ uri });
if (note == null) throw new Error('Question is not registed');
const poll = await Polls.findOne({ noteId: note.id });
const poll = await Polls.findOneBy({ noteId: note.id });
if (poll == null) throw new Error('Question is not registed');
//#endregion

View File

@ -7,6 +7,6 @@ import { User } from '@/models/entities/user.js';
* @param id Follower|Followee ID
*/
export default async function renderFollowUser(id: User['id']): Promise<any> {
const user = await Users.findOneOrFail(id);
const user = await Users.findOneByOrFail({ id: id });
return Users.isLocalUser(user) ? `${config.url}/users/${user.id}` : user.uri;
}

View File

@ -2,6 +2,7 @@ import config from '@/config/index.js';
import { NoteReaction } from '@/models/entities/note-reaction.js';
import { Note } from '@/models/entities/note.js';
import { Emojis } from '@/models/index.js';
import { IsNull } from 'typeorm';
import renderEmoji from './emoji.js';
export const renderLike = async (noteReaction: NoteReaction, note: Note) => {
@ -18,9 +19,9 @@ export const renderLike = async (noteReaction: NoteReaction, note: Note) => {
if (reaction.startsWith(':')) {
const name = reaction.replace(/:/g, '');
const emoji = await Emojis.findOne({
const emoji = await Emojis.findOneBy({
name,
host: null,
host: IsNull(),
});
if (emoji) object.tag = [ renderEmoji(emoji) ];

View File

@ -7,25 +7,25 @@ import toHtml from '../misc/get-note-html.js';
import { Note, IMentionedRemoteUsers } from '@/models/entities/note.js';
import { DriveFile } from '@/models/entities/drive-file.js';
import { DriveFiles, Notes, Users, Emojis, Polls } from '@/models/index.js';
import { In } from 'typeorm';
import { In, IsNull } from 'typeorm';
import { Emoji } from '@/models/entities/emoji.js';
import { Poll } from '@/models/entities/poll.js';
export default async function renderNote(note: Note, dive = true, isTalk = false): Promise<Record<string, unknown>> {
const getPromisedFiles = async (ids: string[]) => {
if (!ids || ids.length === 0) return [];
const items = await DriveFiles.find({ id: In(ids) });
const items = await DriveFiles.findBy({ id: In(ids) });
return ids.map(id => items.find(item => item.id === id)).filter(item => item != null) as DriveFile[];
};
let inReplyTo;
let inReplyToNote: Note | undefined;
let inReplyToNote: Note | null;
if (note.replyId) {
inReplyToNote = await Notes.findOne(note.replyId);
inReplyToNote = await Notes.findOneBy({ id: note.replyId });
if (inReplyToNote != null) {
const inReplyToUser = await Users.findOne(inReplyToNote.userId);
const inReplyToUser = await Users.findOneBy({ id: inReplyToNote.userId });
if (inReplyToUser != null) {
if (inReplyToNote.uri) {
@ -46,7 +46,7 @@ export default async function renderNote(note: Note, dive = true, isTalk = false
let quote;
if (note.renoteId) {
const renote = await Notes.findOne(note.renoteId);
const renote = await Notes.findOneBy({ id: note.renoteId });
if (renote) {
quote = renote.uri ? renote.uri : `${config.url}/notes/${renote.id}`;
@ -73,7 +73,7 @@ export default async function renderNote(note: Note, dive = true, isTalk = false
to = mentions;
}
const mentionedUsers = note.mentions.length > 0 ? await Users.find({
const mentionedUsers = note.mentions.length > 0 ? await Users.findBy({
id: In(note.mentions),
}) : [];
@ -83,10 +83,10 @@ export default async function renderNote(note: Note, dive = true, isTalk = false
const files = await getPromisedFiles(note.fileIds);
const text = note.text;
let poll: Poll | undefined;
let poll: Poll | null;
if (note.hasPoll) {
poll = await Polls.findOne({ noteId: note.id });
poll = await Polls.findOneBy({ noteId: note.id });
}
let apText = text;
@ -156,9 +156,9 @@ export async function getEmojis(names: string[]): Promise<Emoji[]> {
if (names == null || names.length === 0) return [];
const emojis = await Promise.all(
names.map(name => Emojis.findOne({
names.map(name => Emojis.findOneBy({
name,
host: null,
host: IsNull(),
}))
);

View File

@ -17,9 +17,9 @@ export async function renderPerson(user: ILocalUser) {
const isSystem = !!user.username.match(/\./);
const [avatar, banner, profile] = await Promise.all([
user.avatarId ? DriveFiles.findOne(user.avatarId) : Promise.resolve(undefined),
user.bannerId ? DriveFiles.findOne(user.bannerId) : Promise.resolve(undefined),
UserProfiles.findOneOrFail(user.id),
user.avatarId ? DriveFiles.findOneBy({ id: user.avatarId }) : Promise.resolve(undefined),
user.bannerId ? DriveFiles.findOneBy({ id: user.bannerId }) : Promise.resolve(undefined),
UserProfiles.findOneByOrFail({ userId: user.id }),
]);
const attachment: {

View File

@ -7,15 +7,16 @@ import chalk from 'chalk';
import { User, IRemoteUser } from '@/models/entities/user.js';
import { Users } from '@/models/index.js';
import { toPuny } from '@/misc/convert-host.js';
import { IsNull } from 'typeorm';
const logger = remoteLogger.createSubLogger('resolve-user');
export async function resolveUser(username: string, host: string | null, option?: any, resync = false): Promise<User> {
export async function resolveUser(username: string, host: string | null): Promise<User> {
const usernameLower = username.toLowerCase();
if (host == null) {
logger.info(`return local user: ${usernameLower}`);
return await Users.findOne({ usernameLower, host: null }).then(u => {
return await Users.findOneBy({ usernameLower, host: IsNull() }).then(u => {
if (u == null) {
throw new Error('user not found');
} else {
@ -28,7 +29,7 @@ export async function resolveUser(username: string, host: string | null, option?
if (config.host === host) {
logger.info(`return local user: ${usernameLower}`);
return await Users.findOne({ usernameLower, host: null }).then(u => {
return await Users.findOneBy({ usernameLower, host: IsNull() }).then(u => {
if (u == null) {
throw new Error('user not found');
} else {
@ -37,7 +38,7 @@ export async function resolveUser(username: string, host: string | null, option?
});
}
const user = await Users.findOne({ usernameLower, host }, option) as IRemoteUser | null;
const user = await Users.findOneBy({ usernameLower, host }) as IRemoteUser | null;
const acctLower = `${usernameLower}@${host}`;
@ -48,8 +49,8 @@ export async function resolveUser(username: string, host: string | null, option?
return await createPerson(self.href);
}
// resyncオプション OR ユーザー情報が古い場合は、WebFilgerからやりなおして返す
if (resync || user.lastFetchedAt == null || Date.now() - user.lastFetchedAt.getTime() > 1000 * 60 * 60 * 24) {
// ユーザー情報が古い場合は、WebFilgerからやりなおして返す
if (user.lastFetchedAt == null || Date.now() - user.lastFetchedAt.getTime() > 1000 * 60 * 60 * 24) {
// 繋がらないインスタンスに何回も試行するのを防ぐ, 後続の同様処理の連続試行を防ぐ ため 試行前にも更新する
await Users.update(user.id, {
lastFetchedAt: new Date(),
@ -82,7 +83,7 @@ export async function resolveUser(username: string, host: string | null, option?
await updatePerson(self.href);
logger.info(`return resynced remote user: ${acctLower}`);
return await Users.findOne({ uri: self.href }).then(u => {
return await Users.findOneBy({ uri: self.href }).then(u => {
if (u == null) {
throw new Error('user not found');
} else {