refactor(backend): 存在確認のfindOneByexistに置き換え (#11224)

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

* cleanup
This commit is contained in:
okayurisotto
2023-07-11 14:58:58 +09:00
committed by GitHub
parent 48d3341462
commit cf3e39178b
42 changed files with 287 additions and 200 deletions

View File

@@ -128,12 +128,12 @@ export class SignupApiService {
}
if (instance.emailRequiredForSignup) {
if (await this.usersRepository.findOneBy({ usernameLower: username.toLowerCase(), host: IsNull() })) {
if (await this.usersRepository.exist({ where: { usernameLower: username.toLowerCase(), host: IsNull() } })) {
throw new FastifyReplyError(400, 'DUPLICATED_USERNAME');
}
// Check deleted username duplication
if (await this.usedUsernamesRepository.findOneBy({ username: username.toLowerCase() })) {
if (await this.usedUsernamesRepository.exist({ where: { username: username.toLowerCase() } })) {
throw new FastifyReplyError(400, 'USED_USERNAME');
}

View File

@@ -50,9 +50,9 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
throw e;
});
const exist = await this.promoNotesRepository.findOneBy({ noteId: note.id });
const exist = await this.promoNotesRepository.exist({ where: { noteId: note.id } });
if (exist != null) {
if (exist) {
throw new ApiError(meta.errors.alreadyPromoted);
}

View File

@@ -69,8 +69,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
private globalEventService: GlobalEventService,
) {
super(meta, paramDef, async (ps) => {
const role = await this.rolesRepository.findOneBy({ id: ps.roleId });
if (role == null) {
const roleExist = await this.rolesRepository.exist({ where: { id: ps.roleId } });
if (!roleExist) {
throw new ApiError(meta.errors.noSuchRole);
}

View File

@@ -58,12 +58,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
const accessToken = secureRndstr(32);
// Fetch exist access token
const exist = await this.accessTokensRepository.findOneBy({
appId: session.appId,
userId: me.id,
const exist = await this.accessTokensRepository.exist({
where: {
appId: session.appId,
userId: me.id,
},
});
if (exist == null) {
if (!exist) {
const app = await this.appsRepository.findOneByOrFail({ id: session.appId });
// Generate Hash

View File

@@ -84,12 +84,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
});
// Check if already blocking
const exist = await this.blockingsRepository.findOneBy({
blockerId: blocker.id,
blockeeId: blockee.id,
const exist = await this.blockingsRepository.exist({
where: {
blockerId: blocker.id,
blockeeId: blockee.id,
},
});
if (exist != null) {
if (exist) {
throw new ApiError(meta.errors.alreadyBlocking);
}

View File

@@ -84,12 +84,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
});
// Check not blocking
const exist = await this.blockingsRepository.findOneBy({
blockerId: blocker.id,
blockeeId: blockee.id,
const exist = await this.blockingsRepository.exist({
where: {
blockerId: blocker.id,
blockeeId: blockee.id,
}
});
if (exist == null) {
if (!exist) {
throw new ApiError(meta.errors.notBlocking);
}

View File

@@ -87,12 +87,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
throw e;
});
const exist = await this.clipNotesRepository.findOneBy({
noteId: note.id,
clipId: clip.id,
const exist = await this.clipNotesRepository.exist({
where: {
noteId: note.id,
clipId: clip.id,
},
});
if (exist != null) {
if (exist) {
throw new ApiError(meta.errors.alreadyClipped);
}

View File

@@ -58,12 +58,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
throw new ApiError(meta.errors.noSuchClip);
}
const exist = await this.clipFavoritesRepository.findOneBy({
clipId: clip.id,
userId: me.id,
const exist = await this.clipFavoritesRepository.exist({
where: {
clipId: clip.id,
userId: me.id,
},
});
if (exist != null) {
if (exist) {
throw new ApiError(meta.errors.alreadyFavorited);
}

View File

@@ -34,12 +34,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
private driveFilesRepository: DriveFilesRepository,
) {
super(meta, paramDef, async (ps, me) => {
const file = await this.driveFilesRepository.findOneBy({
md5: ps.md5,
userId: me.id,
const exist = await this.driveFilesRepository.exist({
where: {
md5: ps.md5,
userId: me.id,
},
});
return file != null;
return exist;
});
}
}

View File

@@ -66,12 +66,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
}
// if already liked
const exist = await this.flashLikesRepository.findOneBy({
flashId: flash.id,
userId: me.id,
const exist = await this.flashLikesRepository.exist({
where: {
flashId: flash.id,
userId: me.id,
},
});
if (exist != null) {
if (exist) {
throw new ApiError(meta.errors.alreadyLiked);
}

View File

@@ -99,12 +99,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
});
// Check if already following
const exist = await this.followingsRepository.findOneBy({
followerId: follower.id,
followeeId: followee.id,
const exist = await this.followingsRepository.exist({
where: {
followerId: follower.id,
followeeId: followee.id,
},
});
if (exist != null) {
if (exist) {
throw new ApiError(meta.errors.alreadyFollowing);
}

View File

@@ -84,12 +84,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
});
// Check not following
const exist = await this.followingsRepository.findOneBy({
followerId: follower.id,
followeeId: followee.id,
const exist = await this.followingsRepository.exist({
where: {
followerId: follower.id,
followeeId: followee.id,
},
});
if (exist == null) {
if (!exist) {
throw new ApiError(meta.errors.notFollowing);
}

View File

@@ -66,12 +66,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
}
// if already liked
const exist = await this.galleryLikesRepository.findOneBy({
postId: post.id,
userId: me.id,
const exist = await this.galleryLikesRepository.exist({
where: {
postId: post.id,
userId: me.id,
},
});
if (exist != null) {
if (exist) {
throw new ApiError(meta.errors.alreadyLiked);
}

View File

@@ -66,8 +66,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
private downloadService: DownloadService,
) {
super(meta, paramDef, async (ps, me) => {
const users = await this.usersRepository.findOneBy({ id: me.id });
if (users === null) throw new ApiError(meta.errors.noSuchUser);
const userExist = await this.usersRepository.exist({ where: { id: me.id } });
if (!userExist) throw new ApiError(meta.errors.noSuchUser);
const file = await this.driveFilesRepository.findOneBy({ id: ps.fileId });
if (file === null) throw new ApiError(meta.errors.noSuchFile);
if (file.size === 0) throw new ApiError(meta.errors.emptyFile);

View File

@@ -47,19 +47,21 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
) {
super(meta, paramDef, async (ps, me) => {
// Check if announcement exists
const announcement = await this.announcementsRepository.findOneBy({ id: ps.announcementId });
const announcementExist = await this.announcementsRepository.exist({ where: { id: ps.announcementId } });
if (announcement == null) {
if (!announcementExist) {
throw new ApiError(meta.errors.noSuchAnnouncement);
}
// Check if already read
const read = await this.announcementReadsRepository.findOneBy({
announcementId: ps.announcementId,
userId: me.id,
const alreadyRead = await this.announcementReadsRepository.exist({
where: {
announcementId: ps.announcementId,
userId: me.id,
},
});
if (read != null) {
if (alreadyRead) {
return;
}

View File

@@ -28,9 +28,9 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
private globalEventService: GlobalEventService,
) {
super(meta, paramDef, async (ps, me) => {
const token = await this.accessTokensRepository.findOneBy({ id: ps.tokenId });
const tokenExist = await this.accessTokensRepository.exist({ where: { id: ps.tokenId } });
if (token) {
if (tokenExist) {
await this.accessTokensRepository.delete({
id: ps.tokenId,
userId: me.id,

View File

@@ -79,12 +79,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
});
// Check if already muting
const exist = await this.mutingsRepository.findOneBy({
muterId: muter.id,
muteeId: mutee.id,
const exist = await this.mutingsRepository.exist({
where: {
muterId: muter.id,
muteeId: mutee.id,
},
});
if (exist != null) {
if (exist) {
throw new ApiError(meta.errors.alreadyMuting);
}

View File

@@ -217,11 +217,13 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
// Check blocking
if (renote.userId !== me.id) {
const block = await this.blockingsRepository.findOneBy({
blockerId: renote.userId,
blockeeId: me.id,
const blockExist = await this.blockingsRepository.exist({
where: {
blockerId: renote.userId,
blockeeId: me.id,
},
});
if (block) {
if (blockExist) {
throw new ApiError(meta.errors.youHaveBeenBlocked);
}
}
@@ -240,11 +242,13 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
// Check blocking
if (reply.userId !== me.id) {
const block = await this.blockingsRepository.findOneBy({
blockerId: reply.userId,
blockeeId: me.id,
const blockExist = await this.blockingsRepository.exist({
where: {
blockerId: reply.userId,
blockeeId: me.id,
},
});
if (block) {
if (blockExist) {
throw new ApiError(meta.errors.youHaveBeenBlocked);
}
}

View File

@@ -63,12 +63,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
});
// if already favorited
const exist = await this.noteFavoritesRepository.findOneBy({
noteId: note.id,
userId: me.id,
const exist = await this.noteFavoritesRepository.exist({
where: {
noteId: note.id,
userId: me.id,
},
});
if (exist != null) {
if (exist) {
throw new ApiError(meta.errors.alreadyFavorited);
}

View File

@@ -66,12 +66,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
}
// if already liked
const exist = await this.pageLikesRepository.findOneBy({
pageId: page.id,
userId: me.id,
const exist = await this.pageLikesRepository.exist({
where: {
pageId: page.id,
userId: me.id,
},
});
if (exist != null) {
if (exist) {
throw new ApiError(meta.errors.alreadyLiked);
}

View File

@@ -44,12 +44,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
throw err;
});
const exist = await this.promoReadsRepository.findOneBy({
noteId: note.id,
userId: me.id,
const exist = await this.promoReadsRepository.exist({
where: {
noteId: note.id,
userId: me.id,
},
});
if (exist != null) {
if (exist) {
return;
}

View File

@@ -97,11 +97,13 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
if (me == null) {
throw new ApiError(meta.errors.forbidden);
} else if (me.id !== user.id) {
const following = await this.followingsRepository.findOneBy({
followeeId: user.id,
followerId: me.id,
const isFollowing = await this.followingsRepository.exist({
where: {
followeeId: user.id,
followerId: me.id,
},
});
if (following == null) {
if (!isFollowing) {
throw new ApiError(meta.errors.forbidden);
}
}

View File

@@ -97,11 +97,13 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
if (me == null) {
throw new ApiError(meta.errors.forbidden);
} else if (me.id !== user.id) {
const following = await this.followingsRepository.findOneBy({
followeeId: user.id,
followerId: me.id,
const isFollowing = await this.followingsRepository.exist({
where: {
followeeId: user.id,
followerId: me.id,
},
});
if (following == null) {
if (!isFollowing) {
throw new ApiError(meta.errors.forbidden);
}
}

View File

@@ -84,11 +84,13 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
private roleService: RoleService,
) {
super(meta, paramDef, async (ps, me) => {
const list = await this.userListsRepository.findOneBy({
id: ps.listId,
isPublic: true,
const listExist = await this.userListsRepository.exist({
where: {
id: ps.listId,
isPublic: true,
},
});
if (list === null) throw new ApiError(meta.errors.noSuchList);
if (!listExist) throw new ApiError(meta.errors.noSuchList);
const currentCount = await this.userListsRepository.countBy({
userId: me.id,
});
@@ -114,18 +116,22 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
});
if (currentUser.id !== me.id) {
const block = await this.blockingsRepository.findOneBy({
blockerId: currentUser.id,
blockeeId: me.id,
const blockExist = await this.blockingsRepository.exist({
where: {
blockerId: currentUser.id,
blockeeId: me.id,
},
});
if (block) {
if (blockExist) {
throw new ApiError(meta.errors.youHaveBeenBlocked);
}
}
const exist = await this.userListJoiningsRepository.findOneBy({
userListId: userList.id,
userId: currentUser.id,
const exist = await this.userListJoiningsRepository.exist({
where: {
userListId: userList.id,
userId: currentUser.id,
},
});
if (exist) {

View File

@@ -41,21 +41,25 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
private idService: IdService,
) {
super(meta, paramDef, async (ps, me) => {
const userList = await this.userListsRepository.findOneBy({
id: ps.listId,
isPublic: true,
const userListExist = await this.userListsRepository.exist({
where: {
id: ps.listId,
isPublic: true,
},
});
if (userList === null) {
if (!userListExist) {
throw new ApiError(meta.errors.noSuchList);
}
const exist = await this.userListFavoritesRepository.findOneBy({
userId: me.id,
userListId: ps.listId,
const exist = await this.userListFavoritesRepository.exist({
where: {
userId: me.id,
userListId: ps.listId,
},
});
if (exist !== null) {
if (exist) {
throw new ApiError(meta.errors.alreadyFavorited);
}

View File

@@ -100,18 +100,22 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
// Check blocking
if (user.id !== me.id) {
const block = await this.blockingsRepository.findOneBy({
blockerId: user.id,
blockeeId: me.id,
const blockExist = await this.blockingsRepository.exist({
where: {
blockerId: user.id,
blockeeId: me.id,
},
});
if (block) {
if (blockExist) {
throw new ApiError(meta.errors.youHaveBeenBlocked);
}
}
const exist = await this.userListJoiningsRepository.findOneBy({
userListId: userList.id,
userId: user.id,
const exist = await this.userListJoiningsRepository.exist({
where: {
userListId: userList.id,
userId: user.id,
},
});
if (exist) {

View File

@@ -69,10 +69,12 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
userListId: ps.listId,
});
if (me !== null) {
additionalProperties.isLiked = (await this.userListFavoritesRepository.findOneBy({
userId: me.id,
userListId: ps.listId,
}) !== null);
additionalProperties.isLiked = await this.userListFavoritesRepository.exist({
where: {
userId: me.id,
userListId: ps.listId,
},
});
} else {
additionalProperties.isLiked = false;
}

View File

@@ -39,12 +39,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
private userListFavoritesRepository: UserListFavoritesRepository,
) {
super(meta, paramDef, async (ps, me) => {
const userList = await this.userListsRepository.findOneBy({
id: ps.listId,
isPublic: true,
const userListExist = await this.userListsRepository.exist({
where: {
id: ps.listId,
isPublic: true,
},
});
if (userList === null) {
if (!userListExist) {
throw new ApiError(meta.errors.noSuchList);
}

View File

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