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 untrusted user: 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 { 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;

View file

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

View file

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

View file

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

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');
// 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({

View file

@ -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({

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');
// 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({

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');
// 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({

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');
// 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({

View file

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

View file

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

View file

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

View file

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

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');
// 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({

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');
// 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({

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');
// 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({

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');
// 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({

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');
// 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({

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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