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.
This commit is contained in:
Johann150 2023-03-21 21:13:11 +01:00
parent ae703cfe4b
commit 7b8333a21f
Signed by: Johann150
GPG key ID: 9EE6577A2A06F8F1
25 changed files with 31 additions and 98 deletions

View file

@ -4,6 +4,7 @@ import { Note } from '@/models/entities/note.js';
import { Notes, Users } from '@/models/index.js'; import { Notes, Users } from '@/models/index.js';
import { apiLogger } from '@/server/api/logger.js'; import { apiLogger } from '@/server/api/logger.js';
import { visibilityQuery } from './generate-visibility-query.js'; import { visibilityQuery } from './generate-visibility-query.js';
import { ApiError } from '@/server/api/error.js';
/** /**
* Get note for API processing, taking into account visibility. * 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 }); const user = await Users.findOneBy({ id: userId });
if (user == null) { if (user == null) {
throw new IdentifiableError('15348ddd-432d-49c2-8a5a-8069753becff', 'No such user.'); throw new ApiError('NO_SUCH_USER');
} }
return user; return user;
@ -44,7 +45,7 @@ export async function getRemoteUser(userId: User['id']) {
const user = await getUser(userId); const user = await getUser(userId);
if (!Users.isRemoteUser(user)) { if (!Users.isRemoteUser(user)) {
throw new Error('user is not a remote user'); throw new ApiError('NO_SUCH_USER');
} }
return user; return user;
@ -57,7 +58,7 @@ export async function getLocalUser(userId: User['id']) {
const user = await getUser(userId); const user = await getUser(userId);
if (!Users.isLocalUser(user)) { if (!Users.isLocalUser(user)) {
throw new Error('user is not a local user'); throw new ApiError('NO_SUCH_USER');
} }
return user; return user;

View file

@ -3,6 +3,7 @@ import { secureRndstr } from '@/misc/secure-rndstr.js';
import { Users, UserProfiles } from '@/models/index.js'; import { Users, UserProfiles } from '@/models/index.js';
import { ApiError } from '@/server/api/error.js'; import { ApiError } from '@/server/api/error.js';
import define from '@/server/api/define.js'; import define from '@/server/api/define.js';
import { getLocalUser } from '@/server/api/common/getters.js';
export const meta = { export const meta = {
tags: ['admin'], tags: ['admin'],
@ -34,11 +35,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps) => { export default define(meta, paramDef, async (ps) => {
const user = await Users.findOneBy({ id: ps.userId }); const user = await getLocalUser(ps.userId);
if (user == null) {
throw new ApiError('NO_SUCH_USER');
}
if (user.isAdmin) { if (user.isAdmin) {
throw new ApiError('IS_ADMIN'); throw new ApiError('IS_ADMIN');

View file

@ -1,6 +1,7 @@
import { Signins, UserProfiles, Users } from '@/models/index.js'; import { Signins, UserProfiles, Users } from '@/models/index.js';
import { ApiError } from '@/server/api/error.js'; import { ApiError } from '@/server/api/error.js';
import define from '@/server/api/define.js'; import define from '@/server/api/define.js';
import { getUser } from '@/server/api/common/getters.js';
export const meta = { export const meta = {
tags: ['admin'], tags: ['admin'],
@ -27,7 +28,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => { export default define(meta, paramDef, async (ps, me) => {
const [user, profile] = await Promise.all([ const [user, profile] = await Promise.all([
Users.findOneBy({ id: ps.userId }), getUser(ps.userId, true),
UserProfiles.findOneBy({ userId: ps.userId }), UserProfiles.findOneBy({ userId: ps.userId }),
]); ]);

View file

@ -3,6 +3,7 @@ import { ApiError } from '@/server/api/error.js';
import { insertModerationLog } from '@/services/insert-moderation-log.js'; import { insertModerationLog } from '@/services/insert-moderation-log.js';
import { publishInternalEvent } from '@/services/stream.js'; import { publishInternalEvent } from '@/services/stream.js';
import define from '@/server/api/define.js'; import define from '@/server/api/define.js';
import { getUser } from '@/server/api/common/getters.js';
export const meta = { export const meta = {
tags: ['admin'], tags: ['admin'],
@ -23,11 +24,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => { export default define(meta, paramDef, async (ps, me) => {
const user = await Users.findOneBy({ id: ps.userId }); const user = await getUser(ps.userId);
if (user == null) {
throw new ApiError('NO_SUCH_USER');
}
if (user.isAdmin) { if (user.isAdmin) {
throw new ApiError('IS_ADMIN'); throw new ApiError('IS_ADMIN');

View file

@ -42,10 +42,7 @@ export default define(meta, paramDef, async (ps, user) => {
if (user.id === ps.userId) throw new ApiError('BLOCKEE_IS_YOURSELF'); if (user.id === ps.userId) throw new ApiError('BLOCKEE_IS_YOURSELF');
// Get blockee // Get blockee
const blockee = await getUser(ps.userId).catch(e => { const blockee = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
// Check if already blocking // Check if already blocking
const blocked = await Blockings.countBy({ const blocked = await Blockings.countBy({

View file

@ -42,10 +42,7 @@ export default define(meta, paramDef, async (ps, user) => {
const blocker = await Users.findOneByOrFail({ id: user.id }); const blocker = await Users.findOneByOrFail({ id: user.id });
// Get blockee // Get blockee
const blockee = await getUser(ps.userId).catch(e => { const blockee = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
// Check not blocking // Check not blocking
const exist = await Blockings.countBy({ const exist = await Blockings.countBy({

View file

@ -43,10 +43,7 @@ export default define(meta, paramDef, async (ps, user) => {
if (user.id === ps.userId) throw new ApiError('FOLLOWEE_IS_YOURSELF'); if (user.id === ps.userId) throw new ApiError('FOLLOWEE_IS_YOURSELF');
// Get followee // Get followee
const followee = await getUser(ps.userId).catch(e => { const followee = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
// Check if already following // Check if already following
const exist = await Followings.countBy({ const exist = await Followings.countBy({

View file

@ -42,10 +42,7 @@ export default define(meta, paramDef, async (ps, user) => {
if (user.id === ps.userId) throw new ApiError('FOLLOWEE_IS_YOURSELF'); if (user.id === ps.userId) throw new ApiError('FOLLOWEE_IS_YOURSELF');
// Get followee // Get followee
const followee = await getUser(ps.userId).catch(e => { const followee = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
// Check not following // Check not following
const exist = await Followings.countBy({ const exist = await Followings.countBy({

View file

@ -42,10 +42,7 @@ export default define(meta, paramDef, async (ps, user) => {
if (user.id === ps.userId) throw new ApiError('FOLLOWER_IS_YOURSELF'); if (user.id === ps.userId) throw new ApiError('FOLLOWER_IS_YOURSELF');
// Get follower // Get follower
const follower = await getUser(ps.userId).catch(e => { const follower = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
// Check not following // Check not following
const exist = await Followings.countBy({ const exist = await Followings.countBy({

View file

@ -24,10 +24,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => { export default define(meta, paramDef, async (ps, user) => {
// Fetch follower // Fetch follower
const follower = await getUser(ps.userId).catch(e => { const follower = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
await acceptFollowRequest(user, follower).catch(e => { await acceptFollowRequest(user, follower).catch(e => {
if (e.id === '8884c2dd-5795-4ac9-b27e-6a01d38190f9') throw new ApiError('NO_SUCH_FOLLOW_REQUEST'); if (e.id === '8884c2dd-5795-4ac9-b27e-6a01d38190f9') throw new ApiError('NO_SUCH_FOLLOW_REQUEST');

View file

@ -32,10 +32,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => { export default define(meta, paramDef, async (ps, user) => {
// Fetch followee // Fetch followee
const followee = await getUser(ps.userId).catch(e => { const followee = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
try { try {
await cancelFollowRequest(followee, user); await cancelFollowRequest(followee, user);

View file

@ -24,10 +24,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => { export default define(meta, paramDef, async (ps, user) => {
// Fetch follower // Fetch follower
const follower = await getUser(ps.userId).catch(e => { const follower = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
await rejectFollowRequest(user, follower); await rejectFollowRequest(user, follower);

View file

@ -54,10 +54,7 @@ export const paramDef = {
export default define(meta, paramDef, async (ps, user) => { export default define(meta, paramDef, async (ps, user) => {
if (ps.userId != null) { if (ps.userId != null) {
// Fetch recipient (user) // Fetch recipient (user)
const recipient = await getUser(ps.userId).catch(e => { const recipient = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
const query = makePaginationQuery(MessagingMessages.createQueryBuilder('message'), ps.sinceId, ps.untilId) const query = makePaginationQuery(MessagingMessages.createQueryBuilder('message'), ps.sinceId, ps.untilId)
.andWhere(new Brackets(qb => { qb .andWhere(new Brackets(qb => { qb

View file

@ -100,10 +100,7 @@ export default define(meta, paramDef, async (ps, user) => {
if (ps.userId === user.id) throw new ApiError('RECIPIENT_IS_YOURSELF'); if (ps.userId === user.id) throw new ApiError('RECIPIENT_IS_YOURSELF');
// Fetch recipient (user) // Fetch recipient (user)
recipientUser = await getUser(ps.userId).catch(e => { recipientUser = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
// Check blocking // Check blocking
const block = await Blockings.countBy({ const block = await Blockings.countBy({

View file

@ -37,10 +37,7 @@ export default define(meta, paramDef, async (ps, user) => {
if (user.id === ps.userId) throw new ApiError('MUTEE_IS_YOURSELF'); if (user.id === ps.userId) throw new ApiError('MUTEE_IS_YOURSELF');
// Get mutee // Get mutee
const mutee = await getUser(ps.userId).catch(e => { const mutee = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
// Check if already muting // Check if already muting
const exist = await Mutings.countBy({ const exist = await Mutings.countBy({

View file

@ -30,10 +30,7 @@ export default define(meta, paramDef, async (ps, user) => {
if (user.id === ps.userId) throw new ApiError('MUTEE_IS_YOURSELF'); if (user.id === ps.userId) throw new ApiError('MUTEE_IS_YOURSELF');
// Get mutee // Get mutee
const mutee = await getUser(ps.userId).catch(e => { const mutee = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
// Check not muting // Check not muting
const exist = await Mutings.findOneBy({ const exist = await Mutings.findOneBy({

View file

@ -32,10 +32,7 @@ export default define(meta, paramDef, async (ps, user) => {
if (user.id === ps.userId) throw new ApiError('MUTEE_IS_YOURSELF'); if (user.id === ps.userId) throw new ApiError('MUTEE_IS_YOURSELF');
// Get mutee // Get mutee
const mutee = await getUser(ps.userId).catch(e => { const mutee = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
// Check if already muting // Check if already muting
const exist = await RenoteMutings.countBy({ const exist = await RenoteMutings.countBy({

View file

@ -30,10 +30,7 @@ export default define(meta, paramDef, async (ps, user) => {
if (user.id === ps.userId) throw new ApiError('MUTEE_IS_YOURSELF'); if (user.id === ps.userId) throw new ApiError('MUTEE_IS_YOURSELF');
// Get mutee // Get mutee
const mutee = await getUser(ps.userId).catch(e => { const mutee = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
// Check not muting // Check not muting
const exist = await RenoteMutings.findOneBy({ const exist = await RenoteMutings.findOneBy({

View file

@ -38,10 +38,7 @@ export default define(meta, paramDef, async (ps, me) => {
if (userGroup == null) throw new ApiError('NO_SUCH_GROUP'); if (userGroup == null) throw new ApiError('NO_SUCH_GROUP');
// Fetch the user // Fetch the user
const user = await getUser(ps.userId).catch(e => { const user = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
const joined = await UserGroupJoinings.countBy({ const joined = await UserGroupJoinings.countBy({
userGroupId: userGroup.id, userGroupId: userGroup.id,

View file

@ -35,10 +35,7 @@ export default define(meta, paramDef, async (ps, me) => {
if (userGroup == null) throw new ApiError('NO_SUCH_GROUP'); if (userGroup == null) throw new ApiError('NO_SUCH_GROUP');
// Fetch the user // Fetch the user
const user = await getUser(ps.userId).catch(e => { const user = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
if (user.id === userGroup.userId) throw new ApiError('GROUP_OWNER'); if (user.id === userGroup.userId) throw new ApiError('GROUP_OWNER');

View file

@ -41,10 +41,7 @@ export default define(meta, paramDef, async (ps, me) => {
if (userGroup == null) throw new ApiError('NO_SUCH_GROUP'); if (userGroup == null) throw new ApiError('NO_SUCH_GROUP');
// Fetch the user // Fetch the user
const user = await getUser(ps.userId).catch(e => { const user = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
const joined = await UserGroupJoinings.countBy({ const joined = await UserGroupJoinings.countBy({
userGroupId: userGroup.id, userGroupId: userGroup.id,

View file

@ -36,10 +36,7 @@ export default define(meta, paramDef, async (ps, me) => {
if (userList == null) throw new ApiError('NO_SUCH_USER_LIST'); if (userList == null) throw new ApiError('NO_SUCH_USER_LIST');
// Fetch the user // Fetch the user
const user = await getUser(ps.userId).catch(e => { const user = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
// Pull the user // Pull the user
await UserListJoinings.delete({ userListId: userList.id, userId: user.id }); await UserListJoinings.delete({ userListId: userList.id, userId: user.id });

View file

@ -36,10 +36,7 @@ export default define(meta, paramDef, async (ps, me) => {
if (userList == null) throw new ApiError('NO_SUCH_USER_LIST'); if (userList == null) throw new ApiError('NO_SUCH_USER_LIST');
// Fetch the user // Fetch the user
const user = await getUser(ps.userId).catch(e => { const user = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
// Check blocking // Check blocking
if (user.id !== me.id) { if (user.id !== me.id) {

View file

@ -49,10 +49,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => { export default define(meta, paramDef, async (ps, me) => {
// Lookup user // Lookup user
const user = await getUser(ps.userId).catch(e => { const user = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
//#region Construct query //#region Construct query
const query = makePaginationQuery(Notes.createQueryBuilder('note'), ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate) const query = makePaginationQuery(Notes.createQueryBuilder('note'), ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate)

View file

@ -32,10 +32,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => { export default define(meta, paramDef, async (ps, me) => {
// Lookup user // Lookup user
const user = await getUser(ps.userId).catch(e => { const user = await getUser(ps.userId);
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError('NO_SUCH_USER');
throw e;
});
if (user.id === me.id) throw new ApiError('CANNOT_REPORT_YOURSELF'); if (user.id === me.id) throw new ApiError('CANNOT_REPORT_YOURSELF');