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;
});
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);
}

View file

@ -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 });

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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;
});

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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,

View file

@ -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);
}

View file

@ -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);
}
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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;
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);