diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2019-05-18 20:36:33 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2019-05-18 20:36:33 +0900 |
| commit | c7cc3dcdfd2c0962a39e7186852a17dbd09b6a5b (patch) | |
| tree | c2e1671787c00daa8963c879dba6fbdab6f02d66 /src/server/api/endpoints/users/groups | |
| parent | Fix bug (diff) | |
| download | sharkey-c7cc3dcdfd2c0962a39e7186852a17dbd09b6a5b.tar.gz sharkey-c7cc3dcdfd2c0962a39e7186852a17dbd09b6a5b.tar.bz2 sharkey-c7cc3dcdfd2c0962a39e7186852a17dbd09b6a5b.zip | |
ユーザーグループ
Resolve #3218
Diffstat (limited to 'src/server/api/endpoints/users/groups')
| -rw-r--r-- | src/server/api/endpoints/users/groups/create.ts | 51 | ||||
| -rw-r--r-- | src/server/api/endpoints/users/groups/delete.ts | 49 | ||||
| -rw-r--r-- | src/server/api/endpoints/users/groups/joined.ts | 33 | ||||
| -rw-r--r-- | src/server/api/endpoints/users/groups/owned.ts | 33 | ||||
| -rw-r--r-- | src/server/api/endpoints/users/groups/pull.ts | 68 | ||||
| -rw-r--r-- | src/server/api/endpoints/users/groups/push.ts | 90 | ||||
| -rw-r--r-- | src/server/api/endpoints/users/groups/show.ts | 53 |
7 files changed, 377 insertions, 0 deletions
diff --git a/src/server/api/endpoints/users/groups/create.ts b/src/server/api/endpoints/users/groups/create.ts new file mode 100644 index 0000000000..ee6cade8d0 --- /dev/null +++ b/src/server/api/endpoints/users/groups/create.ts @@ -0,0 +1,51 @@ +import $ from 'cafy'; +import define from '../../../define'; +import { UserGroups, UserGroupJoinings } from '../../../../../models'; +import { genId } from '../../../../../misc/gen-id'; +import { UserGroup } from '../../../../../models/entities/user-group'; +import { types, bool } from '../../../../../misc/schema'; +import { UserGroupJoining } from '../../../../../models/entities/user-group-joining'; + +export const meta = { + desc: { + 'ja-JP': 'ユーザーグループを作成します。', + 'en-US': 'Create a user group.' + }, + + tags: ['groups'], + + requireCredential: true, + + kind: 'write:user-groups', + + params: { + name: { + validator: $.str.range(1, 100) + } + }, + + res: { + type: types.object, + optional: bool.false, nullable: bool.false, + ref: 'UserGroup', + }, +}; + +export default define(meta, async (ps, user) => { + const userGroup = await UserGroups.save({ + id: genId(), + createdAt: new Date(), + userId: user.id, + name: ps.name, + } as UserGroup); + + // Push the owner + await UserGroupJoinings.save({ + id: genId(), + createdAt: new Date(), + userId: user.id, + userGroupId: userGroup.id + } as UserGroupJoining); + + return await UserGroups.pack(userGroup); +}); diff --git a/src/server/api/endpoints/users/groups/delete.ts b/src/server/api/endpoints/users/groups/delete.ts new file mode 100644 index 0000000000..4f89c324a1 --- /dev/null +++ b/src/server/api/endpoints/users/groups/delete.ts @@ -0,0 +1,49 @@ +import $ from 'cafy'; +import { ID } from '../../../../../misc/cafy-id'; +import define from '../../../define'; +import { ApiError } from '../../../error'; +import { UserGroups } from '../../../../../models'; + +export const meta = { + desc: { + 'ja-JP': '指定したユーザーグループを削除します。', + 'en-US': 'Delete a user group' + }, + + tags: ['groups'], + + requireCredential: true, + + kind: 'write:user-groups', + + params: { + groupId: { + validator: $.type(ID), + desc: { + 'ja-JP': '対象となるユーザーグループのID', + 'en-US': 'ID of target user group' + } + } + }, + + errors: { + noSuchGroup: { + message: 'No such group.', + code: 'NO_SUCH_GROUP', + id: '63dbd64c-cd77-413f-8e08-61781e210b38' + } + } +}; + +export default define(meta, async (ps, user) => { + const userGroup = await UserGroups.findOne({ + id: ps.groupId, + userId: user.id + }); + + if (userGroup == null) { + throw new ApiError(meta.errors.noSuchGroup); + } + + await UserGroups.delete(userGroup.id); +}); diff --git a/src/server/api/endpoints/users/groups/joined.ts b/src/server/api/endpoints/users/groups/joined.ts new file mode 100644 index 0000000000..14561fce05 --- /dev/null +++ b/src/server/api/endpoints/users/groups/joined.ts @@ -0,0 +1,33 @@ +import define from '../../../define'; +import { UserGroups, UserGroupJoinings } from '../../../../../models'; +import { types, bool } from '../../../../../misc/schema'; + +export const meta = { + desc: { + 'ja-JP': '自分の所属するユーザーグループ一覧を取得します。' + }, + + tags: ['groups', 'account'], + + requireCredential: true, + + kind: 'read:user-groups', + + res: { + type: types.array, + optional: bool.false, nullable: bool.false, + items: { + type: types.object, + optional: bool.false, nullable: bool.false, + ref: 'UserGroup', + } + }, +}; + +export default define(meta, async (ps, me) => { + const joinings = await UserGroupJoinings.find({ + userId: me.id, + }); + + return await Promise.all(joinings.map(x => UserGroups.pack(x.userGroupId))); +}); diff --git a/src/server/api/endpoints/users/groups/owned.ts b/src/server/api/endpoints/users/groups/owned.ts new file mode 100644 index 0000000000..6cf39a142b --- /dev/null +++ b/src/server/api/endpoints/users/groups/owned.ts @@ -0,0 +1,33 @@ +import define from '../../../define'; +import { UserGroups } from '../../../../../models'; +import { types, bool } from '../../../../../misc/schema'; + +export const meta = { + desc: { + 'ja-JP': '自分の作成したユーザーグループ一覧を取得します。' + }, + + tags: ['groups', 'account'], + + requireCredential: true, + + kind: 'read:user-groups', + + res: { + type: types.array, + optional: bool.false, nullable: bool.false, + items: { + type: types.object, + optional: bool.false, nullable: bool.false, + ref: 'UserGroup', + } + }, +}; + +export default define(meta, async (ps, me) => { + const userGroups = await UserGroups.find({ + userId: me.id, + }); + + return await Promise.all(userGroups.map(x => UserGroups.pack(x))); +}); diff --git a/src/server/api/endpoints/users/groups/pull.ts b/src/server/api/endpoints/users/groups/pull.ts new file mode 100644 index 0000000000..5fc0c2fa5e --- /dev/null +++ b/src/server/api/endpoints/users/groups/pull.ts @@ -0,0 +1,68 @@ +import $ from 'cafy'; +import { ID } from '../../../../../misc/cafy-id'; +import define from '../../../define'; +import { ApiError } from '../../../error'; +import { getUser } from '../../../common/getters'; +import { UserGroups, UserGroupJoinings } from '../../../../../models'; + +export const meta = { + desc: { + 'ja-JP': '指定したユーザーグループから指定したユーザーを削除します。', + 'en-US': 'Remove a user to a user group.' + }, + + tags: ['groups', 'users'], + + requireCredential: true, + + kind: 'write:user-groups', + + params: { + groupId: { + validator: $.type(ID), + }, + + userId: { + validator: $.type(ID), + desc: { + 'ja-JP': '対象のユーザーのID', + 'en-US': 'Target user ID' + } + }, + }, + + errors: { + noSuchGroup: { + message: 'No such group.', + code: 'NO_SUCH_GROUP', + id: '4662487c-05b1-4b78-86e5-fd46998aba74' + }, + + noSuchUser: { + message: 'No such user.', + code: 'NO_SUCH_USER', + id: '0b5cc374-3681-41da-861e-8bc1146f7a55' + } + } +}; + +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); + } + + // Fetch the user + const user = await getUser(ps.userId).catch(e => { + if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser); + throw e; + }); + + // Pull the user + await UserGroupJoinings.delete({ userId: user.id }); +}); diff --git a/src/server/api/endpoints/users/groups/push.ts b/src/server/api/endpoints/users/groups/push.ts new file mode 100644 index 0000000000..5371580db0 --- /dev/null +++ b/src/server/api/endpoints/users/groups/push.ts @@ -0,0 +1,90 @@ +import $ from 'cafy'; +import { ID } from '../../../../../misc/cafy-id'; +import define from '../../../define'; +import { ApiError } from '../../../error'; +import { getUser } from '../../../common/getters'; +import { UserGroups, UserGroupJoinings } from '../../../../../models'; +import { genId } from '../../../../../misc/gen-id'; +import { UserGroupJoining } from '../../../../../models/entities/user-group-joining'; + +export const meta = { + desc: { + 'ja-JP': '指定したユーザーグループに指定したユーザーを追加します。', + 'en-US': 'Add a user to a user group.' + }, + + tags: ['groups', 'users'], + + requireCredential: true, + + kind: 'write:user-groups', + + params: { + groupId: { + validator: $.type(ID), + }, + + userId: { + validator: $.type(ID), + desc: { + 'ja-JP': '対象のユーザーのID', + 'en-US': 'Target user ID' + } + }, + }, + + errors: { + noSuchGroup: { + message: 'No such group.', + code: 'NO_SUCH_GROUP', + id: '583f8bc0-8eee-4b78-9299-1e14fc91e409' + }, + + noSuchUser: { + message: 'No such user.', + code: 'NO_SUCH_USER', + id: 'da52de61-002c-475b-90e1-ba64f9cf13a8' + }, + + alreadyAdded: { + message: 'That user has already been added to that group.', + code: 'ALREADY_ADDED', + id: '7e35c6a0-39b2-4488-aea6-6ee20bd5da2c' + } + } +}; + +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); + } + + // Fetch the user + const user = await getUser(ps.userId).catch(e => { + if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser); + throw e; + }); + + const exist = await UserGroupJoinings.findOne({ + userGroupId: userGroup.id, + userId: user.id + }); + + if (exist) { + throw new ApiError(meta.errors.alreadyAdded); + } + + // Push the user + await UserGroupJoinings.save({ + id: genId(), + createdAt: new Date(), + userId: user.id, + userGroupId: userGroup.id + } as UserGroupJoining); +}); diff --git a/src/server/api/endpoints/users/groups/show.ts b/src/server/api/endpoints/users/groups/show.ts new file mode 100644 index 0000000000..5f2c839881 --- /dev/null +++ b/src/server/api/endpoints/users/groups/show.ts @@ -0,0 +1,53 @@ +import $ from 'cafy'; +import { ID } from '../../../../../misc/cafy-id'; +import define from '../../../define'; +import { ApiError } from '../../../error'; +import { UserGroups } from '../../../../../models'; +import { types, bool } from '../../../../../misc/schema'; + +export const meta = { + desc: { + 'ja-JP': '指定したユーザーグループの情報を取得します。', + 'en-US': 'Show a user group.' + }, + + tags: ['groups', 'account'], + + requireCredential: true, + + kind: 'read:user-groups', + + params: { + groupId: { + validator: $.type(ID), + }, + }, + + res: { + type: types.object, + optional: bool.false, nullable: bool.false, + ref: 'UserGroup', + }, + + errors: { + noSuchGroup: { + message: 'No such group.', + code: 'NO_SUCH_GROUP', + id: 'ea04751e-9b7e-487b-a509-330fb6bd6b9b' + }, + } +}; + +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); + } + + return await UserGroups.pack(userGroup); +}); |