Sharkey/src/server/api/endpoints/users/groups/update.ts

56 lines
1,014 B
TypeScript
Raw Normal View History

2019-05-22 05:06:52 +09:00
import $ from 'cafy';
import { ID } from '@/misc/cafy-id.js';
import define from '../../../define.js';
import { ApiError } from '../../../error.js';
import { UserGroups } from '@/models/index.js';
2019-05-22 05:06:52 +09:00
export const meta = {
tags: ['groups'],
2020-02-15 21:33:32 +09:00
requireCredential: true as const,
2019-05-22 05:06:52 +09:00
kind: 'write:user-groups',
params: {
groupId: {
validator: $.type(ID),
},
name: {
validator: $.str.range(1, 100),
}
},
2019-05-22 13:00:36 +09:00
res: {
2019-06-27 18:04:09 +09:00
type: 'object' as const,
optional: false as const, nullable: false as const,
2019-05-22 13:00:36 +09:00
ref: 'UserGroup',
},
2019-05-22 05:06:52 +09:00
errors: {
noSuchGroup: {
message: 'No such group.',
code: 'NO_SUCH_GROUP',
id: '9081cda3-7a9e-4fac-a6ce-908d70f282f6'
},
}
};
export default define(meta, async (ps, me) => {
// Fetch the group
const userGroup = await UserGroups.findOne({
id: ps.groupId,
userId: me.id
});
if (userGroup == null) {
throw new ApiError(meta.errors.noSuchGroup);
}
await UserGroups.update(userGroup.id, {
name: ps.name
});
return await UserGroups.pack(userGroup.id);
});