This commit is contained in:
syuilo 2020-02-15 01:03:59 +09:00
parent 84958af4ce
commit 65c0b6c7da
10 changed files with 123 additions and 9 deletions

View file

@ -1,7 +1,7 @@
import $ from 'cafy';
import define from '../../define';
import { genId } from '../../../../misc/gen-id';
import { Antennas, UserLists } from '../../../../models';
import { Antennas, UserLists, UserGroupJoinings } from '../../../../models';
import { ID } from '../../../../misc/cafy-id';
import { ApiError } from '../../error';
@ -18,13 +18,17 @@ export const meta = {
},
src: {
validator: $.str.or(['home', 'all', 'users', 'list'])
validator: $.str.or(['home', 'all', 'users', 'list', 'group'])
},
userListId: {
validator: $.nullable.optional.type(ID),
},
userGroupId: {
validator: $.nullable.optional.type(ID),
},
keywords: {
validator: $.arr($.arr($.str))
},
@ -55,12 +59,19 @@ export const meta = {
message: 'No such user list.',
code: 'NO_SUCH_USER_LIST',
id: '95063e93-a283-4b8b-9aa5-bcdb8df69a7f'
},
noSuchUserGroup: {
message: 'No such user group.',
code: 'NO_SUCH_USER_GROUP',
id: 'aa3c0b9a-8cae-47c0-92ac-202ce5906682'
}
}
};
export default define(meta, async (ps, user) => {
let userList;
let userGroupJoining;
if (ps.src === 'list') {
userList = await UserLists.findOne({
@ -71,6 +82,15 @@ export default define(meta, async (ps, user) => {
if (userList == null) {
throw new ApiError(meta.errors.noSuchUserList);
}
} else if (ps.src === 'group') {
userGroupJoining = await UserGroupJoinings.findOne({
userGroupId: ps.userGroupId,
userId: user.id,
});
if (userGroupJoining == null) {
throw new ApiError(meta.errors.noSuchUserGroup);
}
}
const antenna = await Antennas.save({
@ -80,6 +100,7 @@ export default define(meta, async (ps, user) => {
name: ps.name,
src: ps.src,
userListId: userList ? userList.id : null,
userGroupJoiningId: userGroupJoining ? userGroupJoining.id : null,
keywords: ps.keywords,
users: ps.users,
caseSensitive: ps.caseSensitive,