forked from FoundKeyGang/FoundKey
Johann150
7b8333a21f
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.
60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
import { pushUserToUserList } from '@/services/user-list/push.js';
|
|
import { UserLists, UserListJoinings, Blockings } from '@/models/index.js';
|
|
import define from '@/server/api/define.js';
|
|
import { ApiError } from '@/server/api/error.js';
|
|
import { getUser } from '@/server/api/common/getters.js';
|
|
|
|
export const meta = {
|
|
tags: ['lists', 'users'],
|
|
|
|
requireCredential: true,
|
|
|
|
kind: 'write:account',
|
|
|
|
description: 'Add a user to an existing list.',
|
|
|
|
errors: ['ALREADY_ADDED', 'BLOCKED', 'NO_SUCH_USER', 'NO_SUCH_USER_LIST'],
|
|
} as const;
|
|
|
|
export const paramDef = {
|
|
type: 'object',
|
|
properties: {
|
|
listId: { type: 'string', format: 'misskey:id' },
|
|
userId: { type: 'string', format: 'misskey:id' },
|
|
},
|
|
required: ['listId', 'userId'],
|
|
} as const;
|
|
|
|
// eslint-disable-next-line import/no-default-export
|
|
export default define(meta, paramDef, async (ps, me) => {
|
|
// Fetch the list
|
|
const userList = await UserLists.findOneBy({
|
|
id: ps.listId,
|
|
userId: me.id,
|
|
});
|
|
|
|
if (userList == null) throw new ApiError('NO_SUCH_USER_LIST');
|
|
|
|
// Fetch the user
|
|
const user = await getUser(ps.userId);
|
|
|
|
// Check blocking
|
|
if (user.id !== me.id) {
|
|
const blocked = await Blockings.countBy({
|
|
blockerId: user.id,
|
|
blockeeId: me.id,
|
|
});
|
|
if (blocked) throw new ApiError('BLOCKED');
|
|
}
|
|
|
|
const exist = await UserListJoinings.countBy({
|
|
userListId: userList.id,
|
|
userId: user.id,
|
|
});
|
|
|
|
if (exist) throw new ApiError('ALREADY_ADDED');
|
|
|
|
// Push the user
|
|
await pushUserToUserList(user, userList);
|
|
});
|