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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
325 changed files with 1314 additions and 1494 deletions

View file

@ -19,7 +19,7 @@ import { Note, IMentionedRemoteUsers } from '@/models/entities/note.js';
import { Mutings, Users, NoteWatchings, Notes, Instances, UserProfiles, Antennas, Followings, MutedNotes, Channels, ChannelFollowings, Blockings, NoteThreadMutings } from '@/models/index.js';
import { DriveFile } from '@/models/entities/drive-file.js';
import { App } from '@/models/entities/app.js';
import { Not, getConnection, In } from 'typeorm';
import { Not, In } from 'typeorm';
import { User, ILocalUser, IRemoteUser } from '@/models/entities/user.js';
import { genId } from '@/misc/gen-id.js';
import { notesChart, perUserNotesChart, activeUsersChart, instanceChart } from '@/services/chart/index.js';
@ -37,6 +37,7 @@ import { getAntennas } from '@/misc/antenna-cache.js';
import { endedPollNotificationQueue } from '@/queue/queues.js';
import { Cache } from '@/misc/cache.js';
import { UserProfile } from '@/models/entities/user-profile.js';
import { db } from '@/db/postgre.js';
const mutedWordsCache = new Cache<{ userId: UserProfile['userId']; mutedWords: UserProfile['mutedWords']; }[]>(1000 * 60 * 5);
@ -78,7 +79,7 @@ class NotificationManager {
public async deliver() {
for (const x of this.queue) {
// ミュート情報を取得
const mentioneeMutes = await Mutings.find({
const mentioneeMutes = await Mutings.findBy({
muterId: x.target,
});
@ -128,7 +129,7 @@ export default async (user: { id: User['id']; username: User['username']; host:
// (クライアントサイドでやっても良い処理だと思うけどとりあえずサーバーサイドで)
if (data.reply && data.channel && data.reply.channelId !== data.channel.id) {
if (data.reply.channelId) {
data.channel = await Channels.findOne(data.reply.channelId);
data.channel = await Channels.findOneBy({ id: data.reply.channelId });
} else {
data.channel = null;
}
@ -137,7 +138,7 @@ export default async (user: { id: User['id']; username: User['username']; host:
// チャンネル内にリプライしたら対象のスコープに合わせる
// (クライアントサイドでやっても良い処理だと思うけどとりあえずサーバーサイドで)
if (data.reply && (data.channel == null) && data.reply.channelId) {
data.channel = await Channels.findOne(data.reply.channelId);
data.channel = await Channels.findOneBy({ id: data.reply.channelId });
}
if (data.createdAt == null) data.createdAt = new Date();
@ -210,7 +211,7 @@ export default async (user: { id: User['id']; username: User['username']; host:
tags = tags.filter(tag => Array.from(tag || '').length <= 128).splice(0, 32);
if (data.reply && (user.id !== data.reply.userId) && !mentionedUsers.some(u => u.id === data.reply!.userId)) {
mentionedUsers.push(await Users.findOneOrFail(data.reply!.userId));
mentionedUsers.push(await Users.findOneByOrFail({ id: data.reply!.userId }));
}
if (data.visibility === 'specified') {
@ -223,7 +224,7 @@ export default async (user: { id: User['id']; username: User['username']; host:
}
if (data.reply && !data.visibleUsers.some(x => x.id === data.reply!.userId)) {
data.visibleUsers.push(await Users.findOneOrFail(data.reply!.userId));
data.visibleUsers.push(await Users.findOneByOrFail({ id: data.reply!.userId }));
}
}
@ -283,7 +284,7 @@ export default async (user: { id: User['id']; username: User['username']; host:
// Channel
if (note.channelId) {
ChannelFollowings.find({ followeeId: note.channelId }).then(followings => {
ChannelFollowings.findBy({ followeeId: note.channelId }).then(followings => {
for (const following of followings) {
insertNoteUnread(following.followerId, note, {
isSpecified: false,
@ -356,7 +357,7 @@ export default async (user: { id: User['id']; username: User['username']; host:
// 通知
if (data.reply.userHost === null) {
const threadMuted = await NoteThreadMutings.findOne({
const threadMuted = await NoteThreadMutings.findOneBy({
userId: data.reply.userId,
threadId: data.reply.threadId || data.reply.id,
});
@ -403,13 +404,13 @@ export default async (user: { id: User['id']; username: User['username']; host:
// 投稿がリプライかつ投稿者がローカルユーザーかつリプライ先の投稿の投稿者がリモートユーザーなら配送
if (data.reply && data.reply.userHost !== null) {
const u = await Users.findOne(data.reply.userId);
const u = await Users.findOneBy({ id: data.reply.userId });
if (u && Users.isRemoteUser(u)) dm.addDirectRecipe(u);
}
// 投稿がRenoteかつ投稿者がローカルユーザーかつRenote元の投稿の投稿者がリモートユーザーなら配送
if (data.renote && data.renote.userHost !== null) {
const u = await Users.findOne(data.renote.userId);
const u = await Users.findOneBy({ id: data.renote.userId });
if (u && Users.isRemoteUser(u)) dm.addDirectRecipe(u);
}
@ -434,7 +435,7 @@ export default async (user: { id: User['id']; username: User['username']; host:
lastNotedAt: new Date(),
});
Notes.count({
Notes.countBy({
userId: user.id,
channelId: data.channel.id,
}).then(count => {
@ -514,7 +515,7 @@ async function insertNote(user: { id: User['id']; host: User['host']; }, data: O
// Append mentions data
if (mentionedUsers.length > 0) {
insert.mentions = mentionedUsers.map(u => u.id);
const profiles = await UserProfiles.find({ userId: In(insert.mentions) });
const profiles = await UserProfiles.findBy({ userId: In(insert.mentions) });
insert.mentionedRemoteUsers = JSON.stringify(mentionedUsers.filter(u => Users.isRemoteUser(u)).map(u => {
const profile = profiles.find(p => p.userId === u.id);
const url = profile != null ? profile.url : null;
@ -531,7 +532,7 @@ async function insertNote(user: { id: User['id']; host: User['host']; }, data: O
try {
if (insert.hasPoll) {
// Start transaction
await getConnection().transaction(async transactionalEntityManager => {
await db.transaction(async transactionalEntityManager => {
await transactionalEntityManager.insert(Note, insert);
const poll = new Poll({
@ -581,7 +582,7 @@ function index(note: Note) {
}
async function notifyToWatchersOfRenotee(renote: Note, user: { id: User['id']; }, nm: NotificationManager, type: NotificationType) {
const watchers = await NoteWatchings.find({
const watchers = await NoteWatchings.findBy({
noteId: renote.id,
userId: Not(user.id),
});
@ -592,7 +593,7 @@ async function notifyToWatchersOfRenotee(renote: Note, user: { id: User['id']; }
}
async function notifyToWatchersOfReplyee(reply: Note, user: { id: User['id']; }, nm: NotificationManager) {
const watchers = await NoteWatchings.find({
const watchers = await NoteWatchings.findBy({
noteId: reply.id,
userId: Not(user.id),
});
@ -604,7 +605,7 @@ async function notifyToWatchersOfReplyee(reply: Note, user: { id: User['id']; },
async function createMentionedEvents(mentionedUsers: MinimumUser[], note: Note, nm: NotificationManager) {
for (const u of mentionedUsers.filter(u => Users.isLocalUser(u))) {
const threadMuted = await NoteThreadMutings.findOne({
const threadMuted = await NoteThreadMutings.findOneBy({
userId: u.id,
threadId: note.threadId || note.id,
});

View file

@ -40,11 +40,11 @@ export default async function(user: { id: User['id']; uri: User['uri']; host: Us
//#region ローカルの投稿なら削除アクティビティを配送
if (Users.isLocalUser(user) && !note.localOnly) {
let renote: Note | undefined;
let renote: Note | null;
// if deletd note is renote
if (note.renoteId && note.text == null && !note.hasPoll && (note.fileIds == null || note.fileIds.length === 0)) {
renote = await Notes.findOne({
renote = await Notes.findOneBy({
id: note.renoteId,
});
}

View file

@ -7,10 +7,10 @@ import { deliverToFollowers } from '@/remote/activitypub/deliver-manager.js';
import { deliverToRelays } from '../../relay.js';
export async function deliverQuestionUpdate(noteId: Note['id']) {
const note = await Notes.findOne(noteId);
const note = await Notes.findOneBy({ id: noteId });
if (note == null) throw new Error('note not found');
const user = await Users.findOne(note.userId);
const user = await Users.findOneBy({ id: note.userId });
if (user == null) throw new Error('note not found');
if (Users.isLocalUser(user)) {

View file

@ -7,7 +7,7 @@ import { genId } from '@/misc/gen-id.js';
import { createNotification } from '../../create-notification.js';
export default async function(user: CacheableUser, note: Note, choice: number) {
const poll = await Polls.findOne(note.id);
const poll = await Polls.findOneBy({ noteId: note.id });
if (poll == null) throw new Error('poll not found');
@ -16,7 +16,7 @@ export default async function(user: CacheableUser, note: Note, choice: number) {
// Check blocking
if (note.userId !== user.id) {
const block = await Blockings.findOne({
const block = await Blockings.findOneBy({
blockerId: note.userId,
blockeeId: user.id,
});
@ -26,7 +26,7 @@ export default async function(user: CacheableUser, note: Note, choice: number) {
}
// if already voted
const exist = await PollVotes.find({
const exist = await PollVotes.findBy({
noteId: note.id,
userId: user.id,
});
@ -65,7 +65,7 @@ export default async function(user: CacheableUser, note: Note, choice: number) {
});
// Fetch watchers
NoteWatchings.find({
NoteWatchings.findBy({
noteId: note.id,
userId: Not(user.id),
})

View file

@ -6,7 +6,7 @@ import { toDbReaction, decodeReaction } from '@/misc/reaction-lib.js';
import { User, IRemoteUser } from '@/models/entities/user.js';
import { Note } from '@/models/entities/note.js';
import { NoteReactions, Users, NoteWatchings, Notes, Emojis, Blockings } from '@/models/index.js';
import { Not } from 'typeorm';
import { IsNull, Not } from 'typeorm';
import { perUserReactionsChart } from '@/services/chart/index.js';
import { genId } from '@/misc/gen-id.js';
import { createNotification } from '../../create-notification.js';
@ -18,7 +18,7 @@ import { IdentifiableError } from '@/misc/identifiable-error.js';
export default async (user: { id: User['id']; host: User['host']; }, note: Note, reaction?: string) => {
// Check blocking
if (note.userId !== user.id) {
const block = await Blockings.findOne({
const block = await Blockings.findOneBy({
blockerId: note.userId,
blockeeId: user.id,
});
@ -43,7 +43,7 @@ export default async (user: { id: User['id']; host: User['host']; }, note: Note,
await NoteReactions.insert(record);
} catch (e) {
if (isDuplicateKeyValueError(e)) {
const exists = await NoteReactions.findOneOrFail({
const exists = await NoteReactions.findOneByOrFail({
noteId: note.id,
userId: user.id,
});
@ -79,7 +79,7 @@ export default async (user: { id: User['id']; host: User['host']; }, note: Note,
const emoji = await Emojis.findOne({
where: {
name: decodedReaction.name,
host: decodedReaction.host,
host: decodedReaction.host ?? IsNull(),
},
select: ['name', 'host', 'originalUrl', 'publicUrl'],
});
@ -103,7 +103,7 @@ export default async (user: { id: User['id']; host: User['host']; }, note: Note,
}
// Fetch watchers
NoteWatchings.find({
NoteWatchings.findBy({
noteId: note.id,
userId: Not(user.id),
}).then(watchers => {
@ -121,7 +121,7 @@ export default async (user: { id: User['id']; host: User['host']; }, note: Note,
const content = renderActivity(await renderLike(record, note));
const dm = new DeliverManager(user, content);
if (note.userHost !== null) {
const reactee = await Users.findOne(note.userId);
const reactee = await Users.findOneBy({ id: note.userId });
dm.addDirectRecipe(reactee as IRemoteUser);
}
dm.addFollowersRecipe();

View file

@ -11,7 +11,7 @@ import { decodeReaction } from '@/misc/reaction-lib.js';
export default async (user: { id: User['id']; host: User['host']; }, note: Note) => {
// if already unreacted
const exist = await NoteReactions.findOne({
const exist = await NoteReactions.findOneBy({
noteId: note.id,
userId: user.id,
});
@ -48,7 +48,7 @@ export default async (user: { id: User['id']; host: User['host']; }, note: Note)
const content = renderActivity(renderUndo(await renderLike(exist, note), user));
const dm = new DeliverManager(user, content);
if (note.userHost !== null) {
const reactee = await Users.findOne(note.userId);
const reactee = await Users.findOneBy({ id: note.userId });
dm.addDirectRecipe(reactee as IRemoteUser);
}
dm.addFollowersRecipe();

View file

@ -68,7 +68,7 @@ export default async function(
// TODO: ↓まとめてクエリしたい
NoteUnreads.count({
NoteUnreads.countBy({
userId: userId,
isMentioned: true,
}).then(mentionsCount => {
@ -78,7 +78,7 @@ export default async function(
}
});
NoteUnreads.count({
NoteUnreads.countBy({
userId: userId,
isSpecified: true,
}).then(specifiedCount => {
@ -88,7 +88,7 @@ export default async function(
}
});
NoteUnreads.count({
NoteUnreads.countBy({
userId: userId,
noteChannelId: Not(IsNull()),
}).then(channelNoteCount => {
@ -113,7 +113,7 @@ export default async function(
// TODO: まとめてクエリしたい
for (const antenna of myAntennas) {
const count = await AntennaNotes.count({
const count = await AntennaNotes.countBy({
antennaId: antenna.id,
read: false,
});

View file

@ -11,14 +11,14 @@ export async function insertNoteUnread(userId: User['id'], note: Note, params: {
}) {
//#region ミュートしているなら無視
// TODO: 現在の仕様ではChannelにミュートは適用されないのでよしなにケアする
const mute = await Mutings.find({
const mute = await Mutings.findBy({
muterId: userId,
});
if (mute.map(m => m.muteeId).includes(note.userId)) return;
//#endregion
// スレッドミュート
const threadMute = await NoteThreadMutings.findOne({
const threadMute = await NoteThreadMutings.findOneBy({
userId: userId,
threadId: note.threadId || note.id,
});
@ -38,7 +38,7 @@ export async function insertNoteUnread(userId: User['id'], note: Note, params: {
// 2秒経っても既読にならなかったら「未読の投稿がありますよ」イベントを発行する
setTimeout(async () => {
const exist = await NoteUnreads.findOne(unread.id);
const exist = await NoteUnreads.findOneBy({ id: unread.id });
if (exist == null) return;