summaryrefslogtreecommitdiff
path: root/packages/backend/src/services/note
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2022-03-26 15:34:00 +0900
committerGitHub <noreply@github.com>2022-03-26 15:34:00 +0900
commit1c67c26bd87aae64fe0f2ef45140e12a78564699 (patch)
tree633a3fad1c5841ea20bc39d6b681b455bbdeabf3 /packages/backend/src/services/note
parent.js (diff)
downloadsharkey-1c67c26bd87aae64fe0f2ef45140e12a78564699.tar.gz
sharkey-1c67c26bd87aae64fe0f2ef45140e12a78564699.tar.bz2
sharkey-1c67c26bd87aae64fe0f2ef45140e12a78564699.zip
refactor: migrate to typeorm 3.0 (#8443)
* wip * wip * wip * Update following.ts * wip * wip * wip * Update resolve-user.ts * maxQueryExecutionTime * wip * wip
Diffstat (limited to 'packages/backend/src/services/note')
-rw-r--r--packages/backend/src/services/note/create.ts33
-rw-r--r--packages/backend/src/services/note/delete.ts4
-rw-r--r--packages/backend/src/services/note/polls/update.ts4
-rw-r--r--packages/backend/src/services/note/polls/vote.ts8
-rw-r--r--packages/backend/src/services/note/reaction/create.ts12
-rw-r--r--packages/backend/src/services/note/reaction/delete.ts4
-rw-r--r--packages/backend/src/services/note/read.ts8
-rw-r--r--packages/backend/src/services/note/unread.ts6
8 files changed, 40 insertions, 39 deletions
diff --git a/packages/backend/src/services/note/create.ts b/packages/backend/src/services/note/create.ts
index f4b0d52045..2ed194b7e9 100644
--- a/packages/backend/src/services/note/create.ts
+++ b/packages/backend/src/services/note/create.ts
@@ -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,
});
diff --git a/packages/backend/src/services/note/delete.ts b/packages/backend/src/services/note/delete.ts
index 1caac2b88f..ffd609dd84 100644
--- a/packages/backend/src/services/note/delete.ts
+++ b/packages/backend/src/services/note/delete.ts
@@ -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,
});
}
diff --git a/packages/backend/src/services/note/polls/update.ts b/packages/backend/src/services/note/polls/update.ts
index 88baf16b64..43ca3eff4d 100644
--- a/packages/backend/src/services/note/polls/update.ts
+++ b/packages/backend/src/services/note/polls/update.ts
@@ -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)) {
diff --git a/packages/backend/src/services/note/polls/vote.ts b/packages/backend/src/services/note/polls/vote.ts
index c758e38572..84d98769d9 100644
--- a/packages/backend/src/services/note/polls/vote.ts
+++ b/packages/backend/src/services/note/polls/vote.ts
@@ -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),
})
diff --git a/packages/backend/src/services/note/reaction/create.ts b/packages/backend/src/services/note/reaction/create.ts
index 236aa79938..77de29d794 100644
--- a/packages/backend/src/services/note/reaction/create.ts
+++ b/packages/backend/src/services/note/reaction/create.ts
@@ -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();
diff --git a/packages/backend/src/services/note/reaction/delete.ts b/packages/backend/src/services/note/reaction/delete.ts
index 62b00f56fd..a7cbcb1c17 100644
--- a/packages/backend/src/services/note/reaction/delete.ts
+++ b/packages/backend/src/services/note/reaction/delete.ts
@@ -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();
diff --git a/packages/backend/src/services/note/read.ts b/packages/backend/src/services/note/read.ts
index 28827c5965..915a9e9eef 100644
--- a/packages/backend/src/services/note/read.ts
+++ b/packages/backend/src/services/note/read.ts
@@ -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,
});
diff --git a/packages/backend/src/services/note/unread.ts b/packages/backend/src/services/note/unread.ts
index ef95dc7e8c..d9ed711e03 100644
--- a/packages/backend/src/services/note/unread.ts
+++ b/packages/backend/src/services/note/unread.ts
@@ -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;