From f4870d6e4acbcf98acc0add2e2a1463398fab5af Mon Sep 17 00:00:00 2001 From: okayurisotto Date: Tue, 11 Jul 2023 14:58:58 +0900 Subject: [PATCH 1/4] refactor(backend): Use `exist` to check existence MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor(backend): 存在確認の`findOneBy`を`exist`に置き換え * cleanup --- .../api/endpoints/admin/promo/create.ts | 4 ++-- .../src/server/api/endpoints/auth/accept.ts | 10 ++++++---- .../server/api/endpoints/blocking/create.ts | 10 ++++++---- .../server/api/endpoints/blocking/delete.ts | 10 ++++++---- .../server/api/endpoints/clips/add-note.ts | 10 ++++++---- .../endpoints/drive/files/check-existence.ts | 10 ++++++---- .../server/api/endpoints/following/create.ts | 10 ++++++---- .../server/api/endpoints/following/delete.ts | 10 ++++++---- .../api/endpoints/gallery/posts/like.ts | 10 ++++++---- .../api/endpoints/i/read-announcement.ts | 6 ++++-- .../server/api/endpoints/i/revoke-token.ts | 4 ++-- .../src/server/api/endpoints/mute/create.ts | 10 ++++++---- .../src/server/api/endpoints/notes/create.ts | 20 +++++++++++-------- .../api/endpoints/notes/favorites/create.ts | 10 ++++++---- .../src/server/api/endpoints/pages/like.ts | 10 ++++++---- .../src/server/api/endpoints/promo/read.ts | 10 ++++++---- .../server/api/endpoints/users/followers.ts | 10 ++++++---- .../server/api/endpoints/users/following.ts | 10 ++++++---- .../server/api/endpoints/users/lists/push.ts | 10 ++++++---- .../server/api/endpoints/users/lists/show.ts | 10 ++++++---- .../server/api/stream/channels/user-list.ts | 10 ++++++---- 21 files changed, 122 insertions(+), 82 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/admin/promo/create.ts b/packages/backend/src/server/api/endpoints/admin/promo/create.ts index a6d1f35191..00244a777a 100644 --- a/packages/backend/src/server/api/endpoints/admin/promo/create.ts +++ b/packages/backend/src/server/api/endpoints/admin/promo/create.ts @@ -40,9 +40,9 @@ export default define(meta, paramDef, async (ps, user) => { throw err; }); - const exist = await PromoNotes.findOneBy({ noteId: note.id }); + const exist = await PromoNotes.exist({ where: { noteId: note.id } }); - if (exist != null) { + if (exist) { throw new ApiError(meta.errors.alreadyPromoted); } diff --git a/packages/backend/src/server/api/endpoints/auth/accept.ts b/packages/backend/src/server/api/endpoints/auth/accept.ts index 35565e2560..8e6ad6527a 100644 --- a/packages/backend/src/server/api/endpoints/auth/accept.ts +++ b/packages/backend/src/server/api/endpoints/auth/accept.ts @@ -41,12 +41,14 @@ export default define(meta, paramDef, async (ps, user) => { const accessToken = secureRndstr(32, true); // Fetch exist access token - const exist = await AccessTokens.findOneBy({ - appId: session.appId, - userId: user.id, + const exist = await AccessTokens.exist({ + where: { + appId: session.appId, + userId: user.id, + }, }); - if (exist == null) { + if (!exist) { // Lookup app const app = await Apps.findOneByOrFail({ id: session.appId }); diff --git a/packages/backend/src/server/api/endpoints/blocking/create.ts b/packages/backend/src/server/api/endpoints/blocking/create.ts index 4bd58d5ef5..f00a2923d5 100644 --- a/packages/backend/src/server/api/endpoints/blocking/create.ts +++ b/packages/backend/src/server/api/endpoints/blocking/create.ts @@ -69,12 +69,14 @@ export default define(meta, paramDef, async (ps, user) => { }); // Check if already blocking - const exist = await Blockings.findOneBy({ - blockerId: blocker.id, - blockeeId: blockee.id, + const exist = await Blockings.exist({ + where: { + blockerId: blocker.id, + blockeeId: blockee.id, + }, }); - if (exist != null) { + if (exist) { throw new ApiError(meta.errors.alreadyBlocking); } diff --git a/packages/backend/src/server/api/endpoints/blocking/delete.ts b/packages/backend/src/server/api/endpoints/blocking/delete.ts index 6c4ca27755..037d5af22c 100644 --- a/packages/backend/src/server/api/endpoints/blocking/delete.ts +++ b/packages/backend/src/server/api/endpoints/blocking/delete.ts @@ -69,12 +69,14 @@ export default define(meta, paramDef, async (ps, user) => { }); // Check not blocking - const exist = await Blockings.findOneBy({ - blockerId: blocker.id, - blockeeId: blockee.id, + const exist = await Blockings.exist({ + where: { + blockerId: blocker.id, + blockeeId: blockee.id, + }, }); - if (exist == null) { + if (!exist) { throw new ApiError(meta.errors.notBlocking); } diff --git a/packages/backend/src/server/api/endpoints/clips/add-note.ts b/packages/backend/src/server/api/endpoints/clips/add-note.ts index b9d6b54c95..416af6faa2 100644 --- a/packages/backend/src/server/api/endpoints/clips/add-note.ts +++ b/packages/backend/src/server/api/endpoints/clips/add-note.ts @@ -57,12 +57,14 @@ export default define(meta, paramDef, async (ps, user) => { throw err; }); - const exist = await ClipNotes.findOneBy({ - noteId: note.id, - clipId: clip.id, + const exist = await ClipNotes.exist({ + where: { + noteId: note.id, + clipId: clip.id, + }, }); - if (exist != null) { + if (exist) { throw new ApiError(meta.errors.alreadyClipped); } diff --git a/packages/backend/src/server/api/endpoints/drive/files/check-existence.ts b/packages/backend/src/server/api/endpoints/drive/files/check-existence.ts index df89685201..e26da30eb5 100644 --- a/packages/backend/src/server/api/endpoints/drive/files/check-existence.ts +++ b/packages/backend/src/server/api/endpoints/drive/files/check-existence.ts @@ -26,10 +26,12 @@ export const paramDef = { } as const; export default define(meta, paramDef, async (ps, user) => { - const file = await DriveFiles.findOneBy({ - md5: ps.md5, - userId: user.id, + const exist = await DriveFiles.exist({ + where: { + md5: ps.md5, + userId: user.id, + }, }); - return file != null; + return exist; }); diff --git a/packages/backend/src/server/api/endpoints/following/create.ts b/packages/backend/src/server/api/endpoints/following/create.ts index e617c1ffb3..48ae6ae7af 100644 --- a/packages/backend/src/server/api/endpoints/following/create.ts +++ b/packages/backend/src/server/api/endpoints/following/create.ts @@ -82,12 +82,14 @@ export default define(meta, paramDef, async (ps, user) => { }); // Check if already following - const exist = await Followings.findOneBy({ - followerId: follower.id, - followeeId: followee.id, + const exist = await Followings.exist({ + where: { + followerId: follower.id, + followeeId: followee.id, + }, }); - if (exist != null) { + if (exist) { throw new ApiError(meta.errors.alreadyFollowing); } diff --git a/packages/backend/src/server/api/endpoints/following/delete.ts b/packages/backend/src/server/api/endpoints/following/delete.ts index 2eebe8a903..cbc6097f4d 100644 --- a/packages/backend/src/server/api/endpoints/following/delete.ts +++ b/packages/backend/src/server/api/endpoints/following/delete.ts @@ -69,12 +69,14 @@ export default define(meta, paramDef, async (ps, user) => { }); // Check not following - const exist = await Followings.findOneBy({ - followerId: follower.id, - followeeId: followee.id, + const exist = await Followings.exist({ + where: { + followerId: follower.id, + followeeId: followee.id, + }, }); - if (exist == null) { + if (!exist) { throw new ApiError(meta.errors.notFollowing); } diff --git a/packages/backend/src/server/api/endpoints/gallery/posts/like.ts b/packages/backend/src/server/api/endpoints/gallery/posts/like.ts index fd46406bdf..2506e40aaa 100644 --- a/packages/backend/src/server/api/endpoints/gallery/posts/like.ts +++ b/packages/backend/src/server/api/endpoints/gallery/posts/like.ts @@ -40,12 +40,14 @@ export default define(meta, paramDef, async (ps, user) => { } // if already liked - const exist = await GalleryLikes.findOneBy({ - postId: post.id, - userId: user.id, + const exist = await GalleryLikes.exist({ + where: { + postId: post.id, + userId: user.id, + }, }); - if (exist != null) { + if (exist) { throw new ApiError(meta.errors.alreadyLiked); } diff --git a/packages/backend/src/server/api/endpoints/i/read-announcement.ts b/packages/backend/src/server/api/endpoints/i/read-announcement.ts index 5218dba871..e44445b1ba 100644 --- a/packages/backend/src/server/api/endpoints/i/read-announcement.ts +++ b/packages/backend/src/server/api/endpoints/i/read-announcement.ts @@ -30,9 +30,11 @@ export const paramDef = { export default define(meta, paramDef, async (ps, user) => { // Check if announcement exists - const announcement = await Announcements.findOneBy({ id: ps.announcementId }); + const exist = await Announcements.findOneBy({ + where: { id: ps.announcementId }, + }); - if (announcement == null) { + if (!exist) { throw new ApiError(meta.errors.noSuchAnnouncement); } diff --git a/packages/backend/src/server/api/endpoints/i/revoke-token.ts b/packages/backend/src/server/api/endpoints/i/revoke-token.ts index 308442bf7b..3a410fa0e5 100644 --- a/packages/backend/src/server/api/endpoints/i/revoke-token.ts +++ b/packages/backend/src/server/api/endpoints/i/revoke-token.ts @@ -17,9 +17,9 @@ export const paramDef = { } as const; export default define(meta, paramDef, async (ps, user) => { - const token = await AccessTokens.findOneBy({ id: ps.tokenId }); + const exist = await AccessTokens.exist({ where: { id: ps.tokenId } }); - if (token) { + if (exist) { await AccessTokens.delete({ id: ps.tokenId, userId: user.id, diff --git a/packages/backend/src/server/api/endpoints/mute/create.ts b/packages/backend/src/server/api/endpoints/mute/create.ts index bacab9b458..7b2f109053 100644 --- a/packages/backend/src/server/api/endpoints/mute/create.ts +++ b/packages/backend/src/server/api/endpoints/mute/create.ts @@ -64,12 +64,14 @@ export default define(meta, paramDef, async (ps, user) => { }); // Check if already muting - const exist = await Mutings.findOneBy({ - muterId: muter.id, - muteeId: mutee.id, + const exist = await Mutings.exist({ + where: { + muterId: muter.id, + muteeId: mutee.id, + }, }); - if (exist != null) { + if (exist) { throw new ApiError(meta.errors.alreadyMuting); } diff --git a/packages/backend/src/server/api/endpoints/notes/create.ts b/packages/backend/src/server/api/endpoints/notes/create.ts index 41b8ab9796..e15221046f 100644 --- a/packages/backend/src/server/api/endpoints/notes/create.ts +++ b/packages/backend/src/server/api/endpoints/notes/create.ts @@ -224,11 +224,13 @@ export default define(meta, paramDef, async (ps, user) => { // Check blocking if (renote.userId !== user.id) { - const block = await Blockings.findOneBy({ - blockerId: renote.userId, - blockeeId: user.id, + const isBlocked = await Blockings.exist({ + where: { + blockerId: renote.userId, + blockeeId: user.id, + }, }); - if (block) { + if (isBlocked) { throw new ApiError(meta.errors.youHaveBeenBlocked); } } @@ -249,11 +251,13 @@ export default define(meta, paramDef, async (ps, user) => { // Check blocking if (reply.userId !== user.id) { - const block = await Blockings.findOneBy({ - blockerId: reply.userId, - blockeeId: user.id, + const isBlocked = await Blockings.exist({ + where: { + blockerId: reply.userId, + blockeeId: user.id, + }, }); - if (block) { + if (isBlocked) { throw new ApiError(meta.errors.youHaveBeenBlocked); } } diff --git a/packages/backend/src/server/api/endpoints/notes/favorites/create.ts b/packages/backend/src/server/api/endpoints/notes/favorites/create.ts index 835594f03a..64862a3733 100644 --- a/packages/backend/src/server/api/endpoints/notes/favorites/create.ts +++ b/packages/backend/src/server/api/endpoints/notes/favorites/create.ts @@ -43,12 +43,14 @@ export default define(meta, paramDef, async (ps, user) => { }); // if already favorited - const exist = await NoteFavorites.findOneBy({ - noteId: note.id, - userId: user.id, + const exist = await NoteFavorites.exist({ + where: { + noteId: note.id, + userId: user.id, + }, }); - if (exist != null) { + if (exist) { throw new ApiError(meta.errors.alreadyFavorited); } diff --git a/packages/backend/src/server/api/endpoints/pages/like.ts b/packages/backend/src/server/api/endpoints/pages/like.ts index f14ed39eb0..03482c9616 100644 --- a/packages/backend/src/server/api/endpoints/pages/like.ts +++ b/packages/backend/src/server/api/endpoints/pages/like.ts @@ -40,12 +40,14 @@ export default define(meta, paramDef, async (ps, user) => { } // if already liked - const exist = await PageLikes.findOneBy({ - pageId: page.id, - userId: user.id, + const exist = await PageLikes.exist({ + where: { + pageId: page.id, + userId: user.id, + }, }); - if (exist != null) { + if (exist) { throw new ApiError(meta.errors.alreadyLiked); } diff --git a/packages/backend/src/server/api/endpoints/promo/read.ts b/packages/backend/src/server/api/endpoints/promo/read.ts index 09c8cb6fab..5310382a43 100644 --- a/packages/backend/src/server/api/endpoints/promo/read.ts +++ b/packages/backend/src/server/api/endpoints/promo/read.ts @@ -33,12 +33,14 @@ export default define(meta, paramDef, async (ps, user) => { throw err; }); - const exist = await PromoReads.findOneBy({ - noteId: note.id, - userId: user.id, + const exist = await PromoReads.exist({ + where: { + noteId: note.id, + userId: user.id, + }, }); - if (exist != null) { + if (exist) { return; } diff --git a/packages/backend/src/server/api/endpoints/users/followers.ts b/packages/backend/src/server/api/endpoints/users/followers.ts index 855ab35736..31719bad32 100644 --- a/packages/backend/src/server/api/endpoints/users/followers.ts +++ b/packages/backend/src/server/api/endpoints/users/followers.ts @@ -98,11 +98,13 @@ export default define(meta, paramDef, async (ps, me) => { if (me == null) { throw new ApiError(meta.errors.forbidden); } else if (me.id !== user.id) { - const following = await Followings.findOneBy({ - followeeId: user.id, - followerId: me.id, + const isFollowed = await Followings.exist({ + where: { + followeeId: user.id, + followerId: me.id, + }, }); - if (following == null) { + if (!isFollowed) { throw new ApiError(meta.errors.nullFollowers); } } diff --git a/packages/backend/src/server/api/endpoints/users/following.ts b/packages/backend/src/server/api/endpoints/users/following.ts index 37d69b048b..1c1da0e117 100644 --- a/packages/backend/src/server/api/endpoints/users/following.ts +++ b/packages/backend/src/server/api/endpoints/users/following.ts @@ -97,11 +97,13 @@ export default define(meta, paramDef, async (ps, me) => { if (me == null) { throw new ApiError(meta.errors.forbidden); } else if (me.id !== user.id) { - const following = await Followings.findOneBy({ - followeeId: user.id, - followerId: me.id, + const isFollowing = await Followings.exist({ + where: { + followeeId: user.id, + followerId: me.id, + }, }); - if (following == null) { + if (!isFollowing) { throw new ApiError(meta.errors.cannot_find); } } diff --git a/packages/backend/src/server/api/endpoints/users/lists/push.ts b/packages/backend/src/server/api/endpoints/users/lists/push.ts index a14195bbc3..10b9acc428 100644 --- a/packages/backend/src/server/api/endpoints/users/lists/push.ts +++ b/packages/backend/src/server/api/endpoints/users/lists/push.ts @@ -52,12 +52,14 @@ export const paramDef = { export default define(meta, paramDef, async (ps, me) => { // Fetch the list - const userList = await UserLists.findOneBy({ - id: ps.listId, - userId: me.id, + const userList = await UserLists.exist({ + where: { + id: ps.listId, + userId: me.id, + }, }); - if (userList == null) { + if (!exist) { throw new ApiError(meta.errors.noSuchList); } diff --git a/packages/backend/src/server/api/endpoints/users/lists/show.ts b/packages/backend/src/server/api/endpoints/users/lists/show.ts index 716fd405dc..8479a4e03f 100644 --- a/packages/backend/src/server/api/endpoints/users/lists/show.ts +++ b/packages/backend/src/server/api/endpoints/users/lists/show.ts @@ -37,12 +37,14 @@ export const paramDef = { export default define(meta, paramDef, async (ps, me) => { // Fetch the list - const userList = await UserLists.findOneBy({ - id: ps.listId, - userId: me.id, + const exist = await UserLists.exist({ + where: { + id: ps.listId, + userId: me.id, + }, }); - if (userList == null) { + if (!exist) { throw new ApiError(meta.errors.noSuchList); } diff --git a/packages/backend/src/server/api/stream/channels/user-list.ts b/packages/backend/src/server/api/stream/channels/user-list.ts index 0b52f69127..d140319503 100644 --- a/packages/backend/src/server/api/stream/channels/user-list.ts +++ b/packages/backend/src/server/api/stream/channels/user-list.ts @@ -22,11 +22,13 @@ export default class extends Channel { this.listId = params.listId as string; // Check existence and owner - const list = await UserLists.findOneBy({ - id: this.listId, - userId: this.user!.id, + const exist = await UserLists.exist({ + where: { + id: this.listId, + userId: this.user!.id, + }, }); - if (!list) return; + if (!exist) return; // Subscribe stream this.subscriber.on(`userListStream:${this.listId}`, this.send); From 65dcff4a667f1c48cf04714e06d0d5b86c709637 Mon Sep 17 00:00:00 2001 From: naskya Date: Thu, 13 Jul 2023 15:28:44 +0000 Subject: [PATCH 2/4] Stop using the name 'exist' if it's not for existence check --- .../src/remote/activitypub/models/person.ts | 44 +++++++++---------- .../api/endpoints/gallery/posts/unlike.ts | 6 +-- .../src/server/api/endpoints/mute/delete.ts | 6 +-- .../api/endpoints/notes/favorites/delete.ts | 6 +-- .../src/server/api/endpoints/pages/unlike.ts | 6 +-- .../api/endpoints/renote-mute/delete.ts | 6 +-- .../src/server/api/endpoints/sw/register.ts | 10 ++--- .../api/endpoints/sw/show-registration.ts | 10 ++--- .../src/services/note/reaction/delete.ts | 14 +++--- 9 files changed, 53 insertions(+), 55 deletions(-) diff --git a/packages/backend/src/remote/activitypub/models/person.ts b/packages/backend/src/remote/activitypub/models/person.ts index c5519ba031..14abf3b1d3 100644 --- a/packages/backend/src/remote/activitypub/models/person.ts +++ b/packages/backend/src/remote/activitypub/models/person.ts @@ -147,11 +147,11 @@ export async function fetchPerson( } //#region Returns if already registered with this server - const exist = await Users.findOneBy({ uri }); + const user = await Users.findOneBy({ uri }); - if (exist) { - await uriPersonCache.set(uri, exist); - return exist; + if (user != null) { + await uriPersonCache.set(uri, user); + return user; } //#endregion @@ -396,9 +396,9 @@ export async function updatePerson( } //#region Already registered on this server? - const exist = (await Users.findOneBy({ uri })) as IRemoteUser; + const user = (await Users.findOneBy({ uri })) as IRemoteUser; - if (exist == null) { + if (user == null) { return; } //#endregion @@ -416,17 +416,15 @@ export async function updatePerson( [person.icon, person.image].map((img) => img == null ? Promise.resolve(null) - : resolveImage(exist, img).catch(() => null), + : resolveImage(user, img).catch(() => null), ), ); // Custom pictogram acquisition - const emojis = await extractEmojis(person.tag || [], exist.host).catch( - (e) => { - logger.info(`extractEmojis: ${e}`); - return [] as Emoji[]; - }, - ); + const emojis = await extractEmojis(person.tag || [], user.host).catch((e) => { + logger.info(`extractEmojis: ${e}`); + return [] as Emoji[]; + }); const emojiNames = emojis.map((emoji) => emoji.name); @@ -518,11 +516,11 @@ export async function updatePerson( } // Update user - await Users.update(exist.id, updates); + await Users.update(user.id, updates); if (person.publicKey) { await UserPublickeys.update( - { userId: exist.id }, + { userId: user.id }, { keyId: person.publicKey.id, keyPem: person.publicKey.publicKeyPem, @@ -531,7 +529,7 @@ export async function updatePerson( } await UserProfiles.update( - { userId: exist.id }, + { userId: user.id }, { url: url, fields, @@ -543,15 +541,15 @@ export async function updatePerson( }, ); - publishInternalEvent("remoteUserUpdated", { id: exist.id }); + publishInternalEvent("remoteUserUpdated", { id: user.id }); // Hashtag Update - updateUsertags(exist, tags); + updateUsertags(user, tags); // If the user in question is a follower, followers will also be updated. await Followings.update( { - followerId: exist.id, + followerId: user.id, }, { followerSharedInbox: @@ -560,7 +558,7 @@ export async function updatePerson( }, ); - await updateFeatured(exist.id, resolver).catch((err) => logger.error(err)); + await updateFeatured(user.id, resolver).catch((err) => logger.error(err)); } /** @@ -576,10 +574,10 @@ export async function resolvePerson( if (typeof uri !== "string") throw new Error("uri is not string"); //#region If already registered on this server, return it. - const exist = await fetchPerson(uri); + const user = await fetchPerson(uri); - if (exist) { - return exist; + if (user != null) { + return user; } //#endregion diff --git a/packages/backend/src/server/api/endpoints/gallery/posts/unlike.ts b/packages/backend/src/server/api/endpoints/gallery/posts/unlike.ts index 772dc92028..03bc299b94 100644 --- a/packages/backend/src/server/api/endpoints/gallery/posts/unlike.ts +++ b/packages/backend/src/server/api/endpoints/gallery/posts/unlike.ts @@ -38,17 +38,17 @@ export default define(meta, paramDef, async (ps, user) => { throw new ApiError(meta.errors.noSuchPost); } - const exist = await GalleryLikes.findOneBy({ + const like = await GalleryLikes.findOneBy({ postId: post.id, userId: user.id, }); - if (exist == null) { + if (like == null) { throw new ApiError(meta.errors.notLiked); } // Delete like - await GalleryLikes.delete(exist.id); + await GalleryLikes.delete(like.id); GalleryPosts.decrement({ id: post.id }, "likedCount", 1); }); diff --git a/packages/backend/src/server/api/endpoints/mute/delete.ts b/packages/backend/src/server/api/endpoints/mute/delete.ts index cc67a44c26..cd00c1a8ab 100644 --- a/packages/backend/src/server/api/endpoints/mute/delete.ts +++ b/packages/backend/src/server/api/endpoints/mute/delete.ts @@ -56,18 +56,18 @@ export default define(meta, paramDef, async (ps, user) => { }); // Check not muting - const exist = await Mutings.findOneBy({ + const muting = await Mutings.findOneBy({ muterId: muter.id, muteeId: mutee.id, }); - if (exist == null) { + if (muting == null) { throw new ApiError(meta.errors.notMuting); } // Delete mute await Mutings.delete({ - id: exist.id, + id: muting.id, }); publishUserEvent(user.id, "unmute", mutee); diff --git a/packages/backend/src/server/api/endpoints/notes/favorites/delete.ts b/packages/backend/src/server/api/endpoints/notes/favorites/delete.ts index 9a09767482..e05d04a969 100644 --- a/packages/backend/src/server/api/endpoints/notes/favorites/delete.ts +++ b/packages/backend/src/server/api/endpoints/notes/favorites/delete.ts @@ -42,15 +42,15 @@ export default define(meta, paramDef, async (ps, user) => { }); // if already favorited - const exist = await NoteFavorites.findOneBy({ + const favorite = await NoteFavorites.findOneBy({ noteId: note.id, userId: user.id, }); - if (exist == null) { + if (favorite == null) { throw new ApiError(meta.errors.notFavorited); } // Delete favorite - await NoteFavorites.delete(exist.id); + await NoteFavorites.delete(favorite.id); }); diff --git a/packages/backend/src/server/api/endpoints/pages/unlike.ts b/packages/backend/src/server/api/endpoints/pages/unlike.ts index 07bf3fbf48..e607d7a546 100644 --- a/packages/backend/src/server/api/endpoints/pages/unlike.ts +++ b/packages/backend/src/server/api/endpoints/pages/unlike.ts @@ -38,17 +38,17 @@ export default define(meta, paramDef, async (ps, user) => { throw new ApiError(meta.errors.noSuchPage); } - const exist = await PageLikes.findOneBy({ + const like = await PageLikes.findOneBy({ pageId: page.id, userId: user.id, }); - if (exist == null) { + if (like == null) { throw new ApiError(meta.errors.notLiked); } // Delete like - await PageLikes.delete(exist.id); + await PageLikes.delete(like.id); Pages.decrement({ id: page.id }, "likedCount", 1); }); diff --git a/packages/backend/src/server/api/endpoints/renote-mute/delete.ts b/packages/backend/src/server/api/endpoints/renote-mute/delete.ts index fb4c972af0..7a898141c3 100644 --- a/packages/backend/src/server/api/endpoints/renote-mute/delete.ts +++ b/packages/backend/src/server/api/endpoints/renote-mute/delete.ts @@ -45,18 +45,18 @@ export default define(meta, paramDef, async (ps, user) => { }); // Check not muting - const exist = await RenoteMutings.findOneBy({ + const muting = await RenoteMutings.findOneBy({ muterId: muter.id, muteeId: mutee.id, }); - if (exist == null) { + if (muting == null) { throw new ApiError(meta.errors.notMuting); } // Delete mute await RenoteMutings.delete({ - id: exist.id, + id: muting.id, }); // publishUserEvent(user.id, "unmute", mutee); diff --git a/packages/backend/src/server/api/endpoints/sw/register.ts b/packages/backend/src/server/api/endpoints/sw/register.ts index 41be4bf74a..6268ae26d7 100644 --- a/packages/backend/src/server/api/endpoints/sw/register.ts +++ b/packages/backend/src/server/api/endpoints/sw/register.ts @@ -57,8 +57,7 @@ export const paramDef = { } as const; export default define(meta, paramDef, async (ps, me) => { - // if already subscribed - const exist = await SwSubscriptions.findOneBy({ + const subscription = await SwSubscriptions.findOneBy({ userId: me.id, endpoint: ps.endpoint, auth: ps.auth, @@ -67,13 +66,14 @@ export default define(meta, paramDef, async (ps, me) => { const instance = await fetchMeta(true); - if (exist != null) { + // if already subscribed + if (subscription != null) { return { state: "already-subscribed" as const, key: instance.swPublicKey, userId: me.id, - endpoint: exist.endpoint, - sendReadMessage: exist.sendReadMessage, + endpoint: subscription.endpoint, + sendReadMessage: subscription.sendReadMessage, }; } diff --git a/packages/backend/src/server/api/endpoints/sw/show-registration.ts b/packages/backend/src/server/api/endpoints/sw/show-registration.ts index c7a9609cff..3ccb7de948 100644 --- a/packages/backend/src/server/api/endpoints/sw/show-registration.ts +++ b/packages/backend/src/server/api/endpoints/sw/show-registration.ts @@ -42,16 +42,16 @@ export const paramDef = { // eslint-disable-next-line import/no-default-export export default define(meta, paramDef, async (ps, me) => { - const exist = await SwSubscriptions.findOneBy({ + const subscription = await SwSubscriptions.findOneBy({ userId: me.id, endpoint: ps.endpoint, }); - if (exist != null) { + if (subscription != null) { return { - userId: exist.userId, - endpoint: exist.endpoint, - sendReadMessage: exist.sendReadMessage, + userId: subscription.userId, + endpoint: subscription.endpoint, + sendReadMessage: subscription.sendReadMessage, }; } diff --git a/packages/backend/src/services/note/reaction/delete.ts b/packages/backend/src/services/note/reaction/delete.ts index 82648249e6..15c6d1cf62 100644 --- a/packages/backend/src/services/note/reaction/delete.ts +++ b/packages/backend/src/services/note/reaction/delete.ts @@ -13,13 +13,13 @@ export default async ( user: { id: User["id"]; host: User["host"] }, note: Note, ) => { - // if already unreacted - const exist = await NoteReactions.findOneBy({ + const reaction = await NoteReactions.findOneBy({ noteId: note.id, userId: user.id, }); - if (exist == null) { + // if already unreacted + if (reaction == null) { throw new IdentifiableError( "60527ec9-b4cb-4a88-a6bd-32d3ad26817d", "not reacted", @@ -27,7 +27,7 @@ export default async ( } // Delete reaction - const result = await NoteReactions.delete(exist.id); + const result = await NoteReactions.delete(reaction.id); if (result.affected !== 1) { throw new IdentifiableError( @@ -37,7 +37,7 @@ export default async ( } // Decrement reactions count - const sql = `jsonb_set("reactions", '{${exist.reaction}}', (COALESCE("reactions"->>'${exist.reaction}', '0')::int - 1)::text::jsonb)`; + const sql = `jsonb_set("reactions", '{${reaction.reaction}}', (COALESCE("reactions"->>'${reaction.reaction}', '0')::int - 1)::text::jsonb)`; await Notes.createQueryBuilder() .update() .set({ @@ -49,14 +49,14 @@ export default async ( Notes.decrement({ id: note.id }, "score", 1); publishNoteStream(note.id, "unreacted", { - reaction: decodeReaction(exist.reaction).reaction, + reaction: decodeReaction(reaction.reaction).reaction, userId: user.id, }); //#region 配信 if (Users.isLocalUser(user) && !note.localOnly) { const content = renderActivity( - renderUndo(await renderLike(exist, note), user), + renderUndo(await renderLike(reaction, note), user), ); const dm = new DeliverManager(user, content); if (note.userHost !== null) { From ae70f02bb0e00b3f5cb836bccf95248e7bc974a8 Mon Sep 17 00:00:00 2001 From: naskya Date: Thu, 13 Jul 2023 15:31:40 +0000 Subject: [PATCH 3/4] More replacements which aren't covered by f4870d6e --- .../src/server/api/endpoints/following/invalidate.ts | 10 ++++++---- .../src/server/api/endpoints/renote-mute/create.ts | 10 ++++++---- packages/backend/src/services/note/unread.ts | 4 ++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/following/invalidate.ts b/packages/backend/src/server/api/endpoints/following/invalidate.ts index 979d298f7d..01ccc2761b 100644 --- a/packages/backend/src/server/api/endpoints/following/invalidate.ts +++ b/packages/backend/src/server/api/endpoints/following/invalidate.ts @@ -69,12 +69,14 @@ export default define(meta, paramDef, async (ps, user) => { }); // Check not following - const exist = await Followings.findOneBy({ - followerId: follower.id, - followeeId: followee.id, + const exist = await Followings.exist({ + where: { + followerId: follower.id, + followeeId: followee.id, + }, }); - if (exist == null) { + if (!exist) { throw new ApiError(meta.errors.notFollowing); } diff --git a/packages/backend/src/server/api/endpoints/renote-mute/create.ts b/packages/backend/src/server/api/endpoints/renote-mute/create.ts index 857cbd9756..f09f197c01 100644 --- a/packages/backend/src/server/api/endpoints/renote-mute/create.ts +++ b/packages/backend/src/server/api/endpoints/renote-mute/create.ts @@ -47,12 +47,14 @@ export default define(meta, paramDef, async (ps, user) => { }); // Check if already muting - const exist = await RenoteMutings.findOneBy({ - muterId: muter.id, - muteeId: mutee.id, + const exist = await RenoteMutings.exist({ + where: { + muterId: muter.id, + muteeId: mutee.id, + }, }); - if (exist != null) { + if (exist) { throw new ApiError(meta.errors.alreadyMuting); } diff --git a/packages/backend/src/services/note/unread.ts b/packages/backend/src/services/note/unread.ts index 275b230d47..cb87abe0e6 100644 --- a/packages/backend/src/services/note/unread.ts +++ b/packages/backend/src/services/note/unread.ts @@ -42,9 +42,9 @@ export async function insertNoteUnread( // 2秒経っても既読にならなかったら「未読の投稿がありますよ」イベントを発行する setTimeout(async () => { - const exist = await NoteUnreads.findOneBy({ id: unread.id }); + const exist = await NoteUnreads.exist({ where: { id: unread.id } }); - if (exist == null) return; + if (!exist) return; if (params.isMentioned) { publishMainStream(userId, "unreadMention", note.id); From 22102d6c3822b22dd1cd0eb229f667aa82ac451a Mon Sep 17 00:00:00 2001 From: naskya Date: Thu, 13 Jul 2023 16:10:13 +0000 Subject: [PATCH 4/4] fix --- .../api/endpoints/i/read-announcement.ts | 12 +++++----- .../server/api/endpoints/users/lists/push.ts | 22 +++++++++++-------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/i/read-announcement.ts b/packages/backend/src/server/api/endpoints/i/read-announcement.ts index e44445b1ba..d0dfa66579 100644 --- a/packages/backend/src/server/api/endpoints/i/read-announcement.ts +++ b/packages/backend/src/server/api/endpoints/i/read-announcement.ts @@ -30,7 +30,7 @@ export const paramDef = { export default define(meta, paramDef, async (ps, user) => { // Check if announcement exists - const exist = await Announcements.findOneBy({ + const exist = await Announcements.exist({ where: { id: ps.announcementId }, }); @@ -39,12 +39,14 @@ export default define(meta, paramDef, async (ps, user) => { } // Check if already read - const read = await AnnouncementReads.findOneBy({ - announcementId: ps.announcementId, - userId: user.id, + const read = await AnnouncementReads.exist({ + where: { + announcementId: ps.announcementId, + userId: user.id, + }, }); - if (read != null) { + if (read) { return; } diff --git a/packages/backend/src/server/api/endpoints/users/lists/push.ts b/packages/backend/src/server/api/endpoints/users/lists/push.ts index 10b9acc428..6f3e09fcbf 100644 --- a/packages/backend/src/server/api/endpoints/users/lists/push.ts +++ b/packages/backend/src/server/api/endpoints/users/lists/push.ts @@ -52,14 +52,14 @@ export const paramDef = { export default define(meta, paramDef, async (ps, me) => { // Fetch the list - const userList = await UserLists.exist({ + const listExists = await UserLists.exist({ where: { id: ps.listId, userId: me.id, }, }); - if (!exist) { + if (!listExists) { throw new ApiError(meta.errors.noSuchList); } @@ -72,18 +72,22 @@ export default define(meta, paramDef, async (ps, me) => { // Check blocking if (user.id !== me.id) { - const block = await Blockings.findOneBy({ - blockerId: user.id, - blockeeId: me.id, + const isBlocked = await Blockings.exist({ + where: { + blockerId: user.id, + blockeeId: me.id, + }, }); - if (block) { + if (isBlocked) { throw new ApiError(meta.errors.youHaveBeenBlocked); } } - const exist = await UserListJoinings.findOneBy({ - userListId: userList.id, - userId: user.id, + const exist = await UserListJoinings.exist({ + where: { + userListId: userList.id, + userId: user.id, + }, }); if (exist) {