From 7b8333a21f3fe1698cf769271d44d080e424ef12 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Tue, 21 Mar 2023 21:13:11 +0100 Subject: [PATCH] server: refactor user getter to throw API error Instead of throwing an IdentifiableError which then just always gets converted into an ApiError, the getter can just throw the same ApiError directly. This makes it more convenient to use and thus more endpoints have been refactored to use it to reduce code repetition. --- packages/backend/src/server/api/common/getters.ts | 7 ++++--- .../src/server/api/endpoints/admin/users/reset-password.ts | 7 ++----- .../backend/src/server/api/endpoints/admin/users/show.ts | 3 ++- .../src/server/api/endpoints/admin/users/silence.ts | 7 ++----- .../backend/src/server/api/endpoints/blocking/create.ts | 5 +---- .../backend/src/server/api/endpoints/blocking/delete.ts | 5 +---- .../backend/src/server/api/endpoints/following/create.ts | 5 +---- .../backend/src/server/api/endpoints/following/delete.ts | 5 +---- .../src/server/api/endpoints/following/invalidate.ts | 5 +---- .../src/server/api/endpoints/following/requests/accept.ts | 5 +---- .../src/server/api/endpoints/following/requests/cancel.ts | 5 +---- .../src/server/api/endpoints/following/requests/reject.ts | 5 +---- .../backend/src/server/api/endpoints/messaging/messages.ts | 5 +---- .../src/server/api/endpoints/messaging/messages/create.ts | 5 +---- packages/backend/src/server/api/endpoints/mute/create.ts | 5 +---- packages/backend/src/server/api/endpoints/mute/delete.ts | 5 +---- .../backend/src/server/api/endpoints/renote-mute/create.ts | 5 +---- .../backend/src/server/api/endpoints/renote-mute/delete.ts | 5 +---- .../src/server/api/endpoints/users/groups/invite.ts | 5 +---- .../backend/src/server/api/endpoints/users/groups/pull.ts | 5 +---- .../src/server/api/endpoints/users/groups/transfer.ts | 5 +---- .../backend/src/server/api/endpoints/users/lists/pull.ts | 5 +---- .../backend/src/server/api/endpoints/users/lists/push.ts | 5 +---- packages/backend/src/server/api/endpoints/users/notes.ts | 5 +---- .../backend/src/server/api/endpoints/users/report-abuse.ts | 5 +---- 25 files changed, 31 insertions(+), 98 deletions(-) diff --git a/packages/backend/src/server/api/common/getters.ts b/packages/backend/src/server/api/common/getters.ts index ea1c26964..cc15a932b 100644 --- a/packages/backend/src/server/api/common/getters.ts +++ b/packages/backend/src/server/api/common/getters.ts @@ -4,6 +4,7 @@ import { Note } from '@/models/entities/note.js'; import { Notes, Users } from '@/models/index.js'; import { apiLogger } from '@/server/api/logger.js'; import { visibilityQuery } from './generate-visibility-query.js'; +import { ApiError } from '@/server/api/error.js'; /** * Get note for API processing, taking into account visibility. @@ -31,7 +32,7 @@ export async function getUser(userId: User['id']) { const user = await Users.findOneBy({ id: userId }); if (user == null) { - throw new IdentifiableError('15348ddd-432d-49c2-8a5a-8069753becff', 'No such user.'); + throw new ApiError('NO_SUCH_USER'); } return user; @@ -44,7 +45,7 @@ export async function getRemoteUser(userId: User['id']) { const user = await getUser(userId); if (!Users.isRemoteUser(user)) { - throw new Error('user is not a remote user'); + throw new ApiError('NO_SUCH_USER'); } return user; @@ -57,7 +58,7 @@ export async function getLocalUser(userId: User['id']) { const user = await getUser(userId); if (!Users.isLocalUser(user)) { - throw new Error('user is not a local user'); + throw new ApiError('NO_SUCH_USER'); } return user; diff --git a/packages/backend/src/server/api/endpoints/admin/users/reset-password.ts b/packages/backend/src/server/api/endpoints/admin/users/reset-password.ts index 7cef83c53..4858538ac 100644 --- a/packages/backend/src/server/api/endpoints/admin/users/reset-password.ts +++ b/packages/backend/src/server/api/endpoints/admin/users/reset-password.ts @@ -3,6 +3,7 @@ import { secureRndstr } from '@/misc/secure-rndstr.js'; import { Users, UserProfiles } from '@/models/index.js'; import { ApiError } from '@/server/api/error.js'; import define from '@/server/api/define.js'; +import { getLocalUser } from '@/server/api/common/getters.js'; export const meta = { tags: ['admin'], @@ -34,11 +35,7 @@ export const paramDef = { // eslint-disable-next-line import/no-default-export export default define(meta, paramDef, async (ps) => { - const user = await Users.findOneBy({ id: ps.userId }); - - if (user == null) { - throw new ApiError('NO_SUCH_USER'); - } + const user = await getLocalUser(ps.userId); if (user.isAdmin) { throw new ApiError('IS_ADMIN'); diff --git a/packages/backend/src/server/api/endpoints/admin/users/show.ts b/packages/backend/src/server/api/endpoints/admin/users/show.ts index 597839cc9..527b3fd63 100644 --- a/packages/backend/src/server/api/endpoints/admin/users/show.ts +++ b/packages/backend/src/server/api/endpoints/admin/users/show.ts @@ -1,6 +1,7 @@ import { Signins, UserProfiles, Users } from '@/models/index.js'; import { ApiError } from '@/server/api/error.js'; import define from '@/server/api/define.js'; +import { getUser } from '@/server/api/common/getters.js'; export const meta = { tags: ['admin'], @@ -27,7 +28,7 @@ export const paramDef = { // eslint-disable-next-line import/no-default-export export default define(meta, paramDef, async (ps, me) => { const [user, profile] = await Promise.all([ - Users.findOneBy({ id: ps.userId }), + getUser(ps.userId, true), UserProfiles.findOneBy({ userId: ps.userId }), ]); diff --git a/packages/backend/src/server/api/endpoints/admin/users/silence.ts b/packages/backend/src/server/api/endpoints/admin/users/silence.ts index 684925027..252c2cb63 100644 --- a/packages/backend/src/server/api/endpoints/admin/users/silence.ts +++ b/packages/backend/src/server/api/endpoints/admin/users/silence.ts @@ -3,6 +3,7 @@ import { ApiError } from '@/server/api/error.js'; import { insertModerationLog } from '@/services/insert-moderation-log.js'; import { publishInternalEvent } from '@/services/stream.js'; import define from '@/server/api/define.js'; +import { getUser } from '@/server/api/common/getters.js'; export const meta = { tags: ['admin'], @@ -23,11 +24,7 @@ export const paramDef = { // eslint-disable-next-line import/no-default-export export default define(meta, paramDef, async (ps, me) => { - const user = await Users.findOneBy({ id: ps.userId }); - - if (user == null) { - throw new ApiError('NO_SUCH_USER'); - } + const user = await getUser(ps.userId); if (user.isAdmin) { throw new ApiError('IS_ADMIN'); diff --git a/packages/backend/src/server/api/endpoints/blocking/create.ts b/packages/backend/src/server/api/endpoints/blocking/create.ts index bf97de4ba..d7c8d6c79 100644 --- a/packages/backend/src/server/api/endpoints/blocking/create.ts +++ b/packages/backend/src/server/api/endpoints/blocking/create.ts @@ -42,10 +42,7 @@ export default define(meta, paramDef, async (ps, user) => { if (user.id === ps.userId) throw new ApiError('BLOCKEE_IS_YOURSELF'); // Get blockee - const blockee = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const blockee = await getUser(ps.userId); // Check if already blocking const blocked = await Blockings.countBy({ diff --git a/packages/backend/src/server/api/endpoints/blocking/delete.ts b/packages/backend/src/server/api/endpoints/blocking/delete.ts index ef5d5f544..608e312b9 100644 --- a/packages/backend/src/server/api/endpoints/blocking/delete.ts +++ b/packages/backend/src/server/api/endpoints/blocking/delete.ts @@ -42,10 +42,7 @@ export default define(meta, paramDef, async (ps, user) => { const blocker = await Users.findOneByOrFail({ id: user.id }); // Get blockee - const blockee = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const blockee = await getUser(ps.userId); // Check not blocking const exist = await Blockings.countBy({ diff --git a/packages/backend/src/server/api/endpoints/following/create.ts b/packages/backend/src/server/api/endpoints/following/create.ts index 4a4e35171..9c8780103 100644 --- a/packages/backend/src/server/api/endpoints/following/create.ts +++ b/packages/backend/src/server/api/endpoints/following/create.ts @@ -43,10 +43,7 @@ export default define(meta, paramDef, async (ps, user) => { if (user.id === ps.userId) throw new ApiError('FOLLOWEE_IS_YOURSELF'); // Get followee - const followee = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const followee = await getUser(ps.userId); // Check if already following const exist = await Followings.countBy({ diff --git a/packages/backend/src/server/api/endpoints/following/delete.ts b/packages/backend/src/server/api/endpoints/following/delete.ts index d3cb507ba..cb5427bb5 100644 --- a/packages/backend/src/server/api/endpoints/following/delete.ts +++ b/packages/backend/src/server/api/endpoints/following/delete.ts @@ -42,10 +42,7 @@ export default define(meta, paramDef, async (ps, user) => { if (user.id === ps.userId) throw new ApiError('FOLLOWEE_IS_YOURSELF'); // Get followee - const followee = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const followee = await getUser(ps.userId); // Check not following const exist = await Followings.countBy({ diff --git a/packages/backend/src/server/api/endpoints/following/invalidate.ts b/packages/backend/src/server/api/endpoints/following/invalidate.ts index beeb1eb15..202f24866 100644 --- a/packages/backend/src/server/api/endpoints/following/invalidate.ts +++ b/packages/backend/src/server/api/endpoints/following/invalidate.ts @@ -42,10 +42,7 @@ export default define(meta, paramDef, async (ps, user) => { if (user.id === ps.userId) throw new ApiError('FOLLOWER_IS_YOURSELF'); // Get follower - const follower = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const follower = await getUser(ps.userId); // Check not following const exist = await Followings.countBy({ diff --git a/packages/backend/src/server/api/endpoints/following/requests/accept.ts b/packages/backend/src/server/api/endpoints/following/requests/accept.ts index cadac48f9..c2b2088ba 100644 --- a/packages/backend/src/server/api/endpoints/following/requests/accept.ts +++ b/packages/backend/src/server/api/endpoints/following/requests/accept.ts @@ -24,10 +24,7 @@ export const paramDef = { // eslint-disable-next-line import/no-default-export export default define(meta, paramDef, async (ps, user) => { // Fetch follower - const follower = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const follower = await getUser(ps.userId); await acceptFollowRequest(user, follower).catch(e => { if (e.id === '8884c2dd-5795-4ac9-b27e-6a01d38190f9') throw new ApiError('NO_SUCH_FOLLOW_REQUEST'); diff --git a/packages/backend/src/server/api/endpoints/following/requests/cancel.ts b/packages/backend/src/server/api/endpoints/following/requests/cancel.ts index 05736bad2..2db33ee50 100644 --- a/packages/backend/src/server/api/endpoints/following/requests/cancel.ts +++ b/packages/backend/src/server/api/endpoints/following/requests/cancel.ts @@ -32,10 +32,7 @@ export const paramDef = { // eslint-disable-next-line import/no-default-export export default define(meta, paramDef, async (ps, user) => { // Fetch followee - const followee = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const followee = await getUser(ps.userId); try { await cancelFollowRequest(followee, user); diff --git a/packages/backend/src/server/api/endpoints/following/requests/reject.ts b/packages/backend/src/server/api/endpoints/following/requests/reject.ts index 6e1cbbcf5..d7b9fc2c5 100644 --- a/packages/backend/src/server/api/endpoints/following/requests/reject.ts +++ b/packages/backend/src/server/api/endpoints/following/requests/reject.ts @@ -24,10 +24,7 @@ export const paramDef = { // eslint-disable-next-line import/no-default-export export default define(meta, paramDef, async (ps, user) => { // Fetch follower - const follower = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const follower = await getUser(ps.userId); await rejectFollowRequest(user, follower); diff --git a/packages/backend/src/server/api/endpoints/messaging/messages.ts b/packages/backend/src/server/api/endpoints/messaging/messages.ts index 19d02ed63..f00b84e49 100644 --- a/packages/backend/src/server/api/endpoints/messaging/messages.ts +++ b/packages/backend/src/server/api/endpoints/messaging/messages.ts @@ -54,10 +54,7 @@ export const paramDef = { export default define(meta, paramDef, async (ps, user) => { if (ps.userId != null) { // Fetch recipient (user) - const recipient = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const recipient = await getUser(ps.userId); const query = makePaginationQuery(MessagingMessages.createQueryBuilder('message'), ps.sinceId, ps.untilId) .andWhere(new Brackets(qb => { qb diff --git a/packages/backend/src/server/api/endpoints/messaging/messages/create.ts b/packages/backend/src/server/api/endpoints/messaging/messages/create.ts index 388e274f7..b7eebdfe1 100644 --- a/packages/backend/src/server/api/endpoints/messaging/messages/create.ts +++ b/packages/backend/src/server/api/endpoints/messaging/messages/create.ts @@ -100,10 +100,7 @@ export default define(meta, paramDef, async (ps, user) => { if (ps.userId === user.id) throw new ApiError('RECIPIENT_IS_YOURSELF'); // Fetch recipient (user) - recipientUser = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + recipientUser = await getUser(ps.userId); // Check blocking const block = await Blockings.countBy({ diff --git a/packages/backend/src/server/api/endpoints/mute/create.ts b/packages/backend/src/server/api/endpoints/mute/create.ts index ec7d92dbc..01af0d656 100644 --- a/packages/backend/src/server/api/endpoints/mute/create.ts +++ b/packages/backend/src/server/api/endpoints/mute/create.ts @@ -37,10 +37,7 @@ export default define(meta, paramDef, async (ps, user) => { if (user.id === ps.userId) throw new ApiError('MUTEE_IS_YOURSELF'); // Get mutee - const mutee = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const mutee = await getUser(ps.userId); // Check if already muting const exist = await Mutings.countBy({ diff --git a/packages/backend/src/server/api/endpoints/mute/delete.ts b/packages/backend/src/server/api/endpoints/mute/delete.ts index 6633236e2..d04a0597f 100644 --- a/packages/backend/src/server/api/endpoints/mute/delete.ts +++ b/packages/backend/src/server/api/endpoints/mute/delete.ts @@ -30,10 +30,7 @@ export default define(meta, paramDef, async (ps, user) => { if (user.id === ps.userId) throw new ApiError('MUTEE_IS_YOURSELF'); // Get mutee - const mutee = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const mutee = await getUser(ps.userId); // Check not muting const exist = await Mutings.findOneBy({ 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 b963d3595..da3903463 100644 --- a/packages/backend/src/server/api/endpoints/renote-mute/create.ts +++ b/packages/backend/src/server/api/endpoints/renote-mute/create.ts @@ -32,10 +32,7 @@ export default define(meta, paramDef, async (ps, user) => { if (user.id === ps.userId) throw new ApiError('MUTEE_IS_YOURSELF'); // Get mutee - const mutee = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const mutee = await getUser(ps.userId); // Check if already muting const exist = await RenoteMutings.countBy({ 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 78aefa29c..e5ce7f9b3 100644 --- a/packages/backend/src/server/api/endpoints/renote-mute/delete.ts +++ b/packages/backend/src/server/api/endpoints/renote-mute/delete.ts @@ -30,10 +30,7 @@ export default define(meta, paramDef, async (ps, user) => { if (user.id === ps.userId) throw new ApiError('MUTEE_IS_YOURSELF'); // Get mutee - const mutee = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const mutee = await getUser(ps.userId); // Check not muting const exist = await RenoteMutings.findOneBy({ diff --git a/packages/backend/src/server/api/endpoints/users/groups/invite.ts b/packages/backend/src/server/api/endpoints/users/groups/invite.ts index 05bc06c90..a39a331fe 100644 --- a/packages/backend/src/server/api/endpoints/users/groups/invite.ts +++ b/packages/backend/src/server/api/endpoints/users/groups/invite.ts @@ -38,10 +38,7 @@ export default define(meta, paramDef, async (ps, me) => { if (userGroup == null) throw new ApiError('NO_SUCH_GROUP'); // Fetch the user - const user = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const user = await getUser(ps.userId); const joined = await UserGroupJoinings.countBy({ userGroupId: userGroup.id, diff --git a/packages/backend/src/server/api/endpoints/users/groups/pull.ts b/packages/backend/src/server/api/endpoints/users/groups/pull.ts index 58049783b..f8df467ac 100644 --- a/packages/backend/src/server/api/endpoints/users/groups/pull.ts +++ b/packages/backend/src/server/api/endpoints/users/groups/pull.ts @@ -35,10 +35,7 @@ export default define(meta, paramDef, async (ps, me) => { if (userGroup == null) throw new ApiError('NO_SUCH_GROUP'); // Fetch the user - const user = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const user = await getUser(ps.userId); if (user.id === userGroup.userId) throw new ApiError('GROUP_OWNER'); diff --git a/packages/backend/src/server/api/endpoints/users/groups/transfer.ts b/packages/backend/src/server/api/endpoints/users/groups/transfer.ts index 52344a634..e5c1181e9 100644 --- a/packages/backend/src/server/api/endpoints/users/groups/transfer.ts +++ b/packages/backend/src/server/api/endpoints/users/groups/transfer.ts @@ -41,10 +41,7 @@ export default define(meta, paramDef, async (ps, me) => { if (userGroup == null) throw new ApiError('NO_SUCH_GROUP'); // Fetch the user - const user = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const user = await getUser(ps.userId); const joined = await UserGroupJoinings.countBy({ userGroupId: userGroup.id, diff --git a/packages/backend/src/server/api/endpoints/users/lists/pull.ts b/packages/backend/src/server/api/endpoints/users/lists/pull.ts index 8be80111d..f7b68a07f 100644 --- a/packages/backend/src/server/api/endpoints/users/lists/pull.ts +++ b/packages/backend/src/server/api/endpoints/users/lists/pull.ts @@ -36,10 +36,7 @@ export default define(meta, paramDef, async (ps, me) => { if (userList == null) throw new ApiError('NO_SUCH_USER_LIST'); // Fetch the user - const user = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const user = await getUser(ps.userId); // Pull the user await UserListJoinings.delete({ userListId: userList.id, userId: user.id }); 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 6126d7da2..8b3dfe4fb 100644 --- a/packages/backend/src/server/api/endpoints/users/lists/push.ts +++ b/packages/backend/src/server/api/endpoints/users/lists/push.ts @@ -36,10 +36,7 @@ export default define(meta, paramDef, async (ps, me) => { if (userList == null) throw new ApiError('NO_SUCH_USER_LIST'); // Fetch the user - const user = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const user = await getUser(ps.userId); // Check blocking if (user.id !== me.id) { diff --git a/packages/backend/src/server/api/endpoints/users/notes.ts b/packages/backend/src/server/api/endpoints/users/notes.ts index d1570f0c5..25fae7cc4 100644 --- a/packages/backend/src/server/api/endpoints/users/notes.ts +++ b/packages/backend/src/server/api/endpoints/users/notes.ts @@ -49,10 +49,7 @@ export const paramDef = { // eslint-disable-next-line import/no-default-export export default define(meta, paramDef, async (ps, me) => { // Lookup user - const user = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const user = await getUser(ps.userId); //#region Construct query const query = makePaginationQuery(Notes.createQueryBuilder('note'), ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate) diff --git a/packages/backend/src/server/api/endpoints/users/report-abuse.ts b/packages/backend/src/server/api/endpoints/users/report-abuse.ts index 454d919ab..17f8efe3d 100644 --- a/packages/backend/src/server/api/endpoints/users/report-abuse.ts +++ b/packages/backend/src/server/api/endpoints/users/report-abuse.ts @@ -32,10 +32,7 @@ export const paramDef = { // eslint-disable-next-line import/no-default-export export default define(meta, paramDef, async (ps, me) => { // Lookup user - const user = await getUser(ps.userId).catch(e => { - if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER'); - throw e; - }); + const user = await getUser(ps.userId); if (user.id === me.id) throw new ApiError('CANNOT_REPORT_YOURSELF');