refactor(backend): Use `exist` to check existence

* refactor(backend): 存在確認の`findOneBy`を`exist`に置き換え

* cleanup
This commit is contained in:
okayurisotto 2023-07-11 14:58:58 +09:00 committed by naskya
parent 64322721b6
commit f4870d6e4a
No known key found for this signature in database
GPG Key ID: 164DFF24E2D40139
21 changed files with 122 additions and 82 deletions

View File

@ -40,9 +40,9 @@ export default define(meta, paramDef, async (ps, user) => {
throw err; 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); throw new ApiError(meta.errors.alreadyPromoted);
} }

View File

@ -41,12 +41,14 @@ export default define(meta, paramDef, async (ps, user) => {
const accessToken = secureRndstr(32, true); const accessToken = secureRndstr(32, true);
// Fetch exist access token // Fetch exist access token
const exist = await AccessTokens.findOneBy({ const exist = await AccessTokens.exist({
where: {
appId: session.appId, appId: session.appId,
userId: user.id, userId: user.id,
},
}); });
if (exist == null) { if (!exist) {
// Lookup app // Lookup app
const app = await Apps.findOneByOrFail({ id: session.appId }); const app = await Apps.findOneByOrFail({ id: session.appId });

View File

@ -69,12 +69,14 @@ export default define(meta, paramDef, async (ps, user) => {
}); });
// Check if already blocking // Check if already blocking
const exist = await Blockings.findOneBy({ const exist = await Blockings.exist({
where: {
blockerId: blocker.id, blockerId: blocker.id,
blockeeId: blockee.id, blockeeId: blockee.id,
},
}); });
if (exist != null) { if (exist) {
throw new ApiError(meta.errors.alreadyBlocking); throw new ApiError(meta.errors.alreadyBlocking);
} }

View File

@ -69,12 +69,14 @@ export default define(meta, paramDef, async (ps, user) => {
}); });
// Check not blocking // Check not blocking
const exist = await Blockings.findOneBy({ const exist = await Blockings.exist({
where: {
blockerId: blocker.id, blockerId: blocker.id,
blockeeId: blockee.id, blockeeId: blockee.id,
},
}); });
if (exist == null) { if (!exist) {
throw new ApiError(meta.errors.notBlocking); throw new ApiError(meta.errors.notBlocking);
} }

View File

@ -57,12 +57,14 @@ export default define(meta, paramDef, async (ps, user) => {
throw err; throw err;
}); });
const exist = await ClipNotes.findOneBy({ const exist = await ClipNotes.exist({
where: {
noteId: note.id, noteId: note.id,
clipId: clip.id, clipId: clip.id,
},
}); });
if (exist != null) { if (exist) {
throw new ApiError(meta.errors.alreadyClipped); throw new ApiError(meta.errors.alreadyClipped);
} }

View File

@ -26,10 +26,12 @@ export const paramDef = {
} as const; } as const;
export default define(meta, paramDef, async (ps, user) => { export default define(meta, paramDef, async (ps, user) => {
const file = await DriveFiles.findOneBy({ const exist = await DriveFiles.exist({
where: {
md5: ps.md5, md5: ps.md5,
userId: user.id, userId: user.id,
},
}); });
return file != null; return exist;
}); });

View File

@ -82,12 +82,14 @@ export default define(meta, paramDef, async (ps, user) => {
}); });
// Check if already following // Check if already following
const exist = await Followings.findOneBy({ const exist = await Followings.exist({
where: {
followerId: follower.id, followerId: follower.id,
followeeId: followee.id, followeeId: followee.id,
},
}); });
if (exist != null) { if (exist) {
throw new ApiError(meta.errors.alreadyFollowing); throw new ApiError(meta.errors.alreadyFollowing);
} }

View File

@ -69,12 +69,14 @@ export default define(meta, paramDef, async (ps, user) => {
}); });
// Check not following // Check not following
const exist = await Followings.findOneBy({ const exist = await Followings.exist({
where: {
followerId: follower.id, followerId: follower.id,
followeeId: followee.id, followeeId: followee.id,
},
}); });
if (exist == null) { if (!exist) {
throw new ApiError(meta.errors.notFollowing); throw new ApiError(meta.errors.notFollowing);
} }

View File

@ -40,12 +40,14 @@ export default define(meta, paramDef, async (ps, user) => {
} }
// if already liked // if already liked
const exist = await GalleryLikes.findOneBy({ const exist = await GalleryLikes.exist({
where: {
postId: post.id, postId: post.id,
userId: user.id, userId: user.id,
},
}); });
if (exist != null) { if (exist) {
throw new ApiError(meta.errors.alreadyLiked); throw new ApiError(meta.errors.alreadyLiked);
} }

View File

@ -30,9 +30,11 @@ export const paramDef = {
export default define(meta, paramDef, async (ps, user) => { export default define(meta, paramDef, async (ps, user) => {
// Check if announcement exists // 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); throw new ApiError(meta.errors.noSuchAnnouncement);
} }

View File

@ -17,9 +17,9 @@ export const paramDef = {
} as const; } as const;
export default define(meta, paramDef, async (ps, user) => { 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({ await AccessTokens.delete({
id: ps.tokenId, id: ps.tokenId,
userId: user.id, userId: user.id,

View File

@ -64,12 +64,14 @@ export default define(meta, paramDef, async (ps, user) => {
}); });
// Check if already muting // Check if already muting
const exist = await Mutings.findOneBy({ const exist = await Mutings.exist({
where: {
muterId: muter.id, muterId: muter.id,
muteeId: mutee.id, muteeId: mutee.id,
},
}); });
if (exist != null) { if (exist) {
throw new ApiError(meta.errors.alreadyMuting); throw new ApiError(meta.errors.alreadyMuting);
} }

View File

@ -224,11 +224,13 @@ export default define(meta, paramDef, async (ps, user) => {
// Check blocking // Check blocking
if (renote.userId !== user.id) { if (renote.userId !== user.id) {
const block = await Blockings.findOneBy({ const isBlocked = await Blockings.exist({
where: {
blockerId: renote.userId, blockerId: renote.userId,
blockeeId: user.id, blockeeId: user.id,
},
}); });
if (block) { if (isBlocked) {
throw new ApiError(meta.errors.youHaveBeenBlocked); throw new ApiError(meta.errors.youHaveBeenBlocked);
} }
} }
@ -249,11 +251,13 @@ export default define(meta, paramDef, async (ps, user) => {
// Check blocking // Check blocking
if (reply.userId !== user.id) { if (reply.userId !== user.id) {
const block = await Blockings.findOneBy({ const isBlocked = await Blockings.exist({
where: {
blockerId: reply.userId, blockerId: reply.userId,
blockeeId: user.id, blockeeId: user.id,
},
}); });
if (block) { if (isBlocked) {
throw new ApiError(meta.errors.youHaveBeenBlocked); throw new ApiError(meta.errors.youHaveBeenBlocked);
} }
} }

View File

@ -43,12 +43,14 @@ export default define(meta, paramDef, async (ps, user) => {
}); });
// if already favorited // if already favorited
const exist = await NoteFavorites.findOneBy({ const exist = await NoteFavorites.exist({
where: {
noteId: note.id, noteId: note.id,
userId: user.id, userId: user.id,
},
}); });
if (exist != null) { if (exist) {
throw new ApiError(meta.errors.alreadyFavorited); throw new ApiError(meta.errors.alreadyFavorited);
} }

View File

@ -40,12 +40,14 @@ export default define(meta, paramDef, async (ps, user) => {
} }
// if already liked // if already liked
const exist = await PageLikes.findOneBy({ const exist = await PageLikes.exist({
where: {
pageId: page.id, pageId: page.id,
userId: user.id, userId: user.id,
},
}); });
if (exist != null) { if (exist) {
throw new ApiError(meta.errors.alreadyLiked); throw new ApiError(meta.errors.alreadyLiked);
} }

View File

@ -33,12 +33,14 @@ export default define(meta, paramDef, async (ps, user) => {
throw err; throw err;
}); });
const exist = await PromoReads.findOneBy({ const exist = await PromoReads.exist({
where: {
noteId: note.id, noteId: note.id,
userId: user.id, userId: user.id,
},
}); });
if (exist != null) { if (exist) {
return; return;
} }

View File

@ -98,11 +98,13 @@ export default define(meta, paramDef, async (ps, me) => {
if (me == null) { if (me == null) {
throw new ApiError(meta.errors.forbidden); throw new ApiError(meta.errors.forbidden);
} else if (me.id !== user.id) { } else if (me.id !== user.id) {
const following = await Followings.findOneBy({ const isFollowed = await Followings.exist({
where: {
followeeId: user.id, followeeId: user.id,
followerId: me.id, followerId: me.id,
},
}); });
if (following == null) { if (!isFollowed) {
throw new ApiError(meta.errors.nullFollowers); throw new ApiError(meta.errors.nullFollowers);
} }
} }

View File

@ -97,11 +97,13 @@ export default define(meta, paramDef, async (ps, me) => {
if (me == null) { if (me == null) {
throw new ApiError(meta.errors.forbidden); throw new ApiError(meta.errors.forbidden);
} else if (me.id !== user.id) { } else if (me.id !== user.id) {
const following = await Followings.findOneBy({ const isFollowing = await Followings.exist({
where: {
followeeId: user.id, followeeId: user.id,
followerId: me.id, followerId: me.id,
},
}); });
if (following == null) { if (!isFollowing) {
throw new ApiError(meta.errors.cannot_find); throw new ApiError(meta.errors.cannot_find);
} }
} }

View File

@ -52,12 +52,14 @@ export const paramDef = {
export default define(meta, paramDef, async (ps, me) => { export default define(meta, paramDef, async (ps, me) => {
// Fetch the list // Fetch the list
const userList = await UserLists.findOneBy({ const userList = await UserLists.exist({
where: {
id: ps.listId, id: ps.listId,
userId: me.id, userId: me.id,
},
}); });
if (userList == null) { if (!exist) {
throw new ApiError(meta.errors.noSuchList); throw new ApiError(meta.errors.noSuchList);
} }

View File

@ -37,12 +37,14 @@ export const paramDef = {
export default define(meta, paramDef, async (ps, me) => { export default define(meta, paramDef, async (ps, me) => {
// Fetch the list // Fetch the list
const userList = await UserLists.findOneBy({ const exist = await UserLists.exist({
where: {
id: ps.listId, id: ps.listId,
userId: me.id, userId: me.id,
},
}); });
if (userList == null) { if (!exist) {
throw new ApiError(meta.errors.noSuchList); throw new ApiError(meta.errors.noSuchList);
} }

View File

@ -22,11 +22,13 @@ export default class extends Channel {
this.listId = params.listId as string; this.listId = params.listId as string;
// Check existence and owner // Check existence and owner
const list = await UserLists.findOneBy({ const exist = await UserLists.exist({
where: {
id: this.listId, id: this.listId,
userId: this.user!.id, userId: this.user!.id,
},
}); });
if (!list) return; if (!exist) return;
// Subscribe stream // Subscribe stream
this.subscriber.on(`userListStream:${this.listId}`, this.send); this.subscriber.on(`userListStream:${this.listId}`, this.send);