2019-02-05 04:48:08 +02:00
|
|
|
import $ from 'cafy';
|
2019-04-07 15:50:36 +03:00
|
|
|
import { ID } from '../../../../../misc/cafy-id';
|
2018-11-02 06:47:44 +02:00
|
|
|
import define from '../../../define';
|
2019-02-22 04:46:58 +02:00
|
|
|
import { ApiError } from '../../../error';
|
2019-02-22 07:02:56 +02:00
|
|
|
import { getUser } from '../../../common/getters';
|
2019-10-28 23:01:14 +02:00
|
|
|
import { MessagingMessages, DriveFiles, UserGroups, UserGroupJoinings } from '../../../../../models';
|
2019-05-18 14:36:33 +03:00
|
|
|
import { User } from '../../../../../models/entities/user';
|
|
|
|
import { UserGroup } from '../../../../../models/entities/user-group';
|
2019-10-28 23:01:14 +02:00
|
|
|
import { createMessage } from '../../../../../services/messages/create';
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-07-16 22:36:44 +03:00
|
|
|
export const meta = {
|
|
|
|
desc: {
|
2019-05-18 14:36:33 +03:00
|
|
|
'ja-JP': 'トークメッセージを送信します。',
|
2018-08-29 00:59:43 +03:00
|
|
|
'en-US': 'Create a message of messaging.'
|
2018-07-16 22:36:44 +03:00
|
|
|
},
|
|
|
|
|
2019-02-23 04:20:58 +02:00
|
|
|
tags: ['messaging'],
|
|
|
|
|
2020-02-15 14:33:32 +02:00
|
|
|
requireCredential: true as const,
|
2018-07-16 22:36:44 +03:00
|
|
|
|
2019-04-15 06:10:40 +03:00
|
|
|
kind: 'write:messaging',
|
2018-11-01 20:32:24 +02:00
|
|
|
|
|
|
|
params: {
|
|
|
|
userId: {
|
2019-05-18 14:36:33 +03:00
|
|
|
validator: $.optional.type(ID),
|
2018-11-03 15:49:36 +02:00
|
|
|
desc: {
|
|
|
|
'ja-JP': '対象のユーザーのID',
|
|
|
|
'en-US': 'Target user ID'
|
|
|
|
}
|
2018-11-01 20:32:24 +02:00
|
|
|
},
|
|
|
|
|
2019-05-18 14:36:33 +03:00
|
|
|
groupId: {
|
|
|
|
validator: $.optional.type(ID),
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '対象のグループのID',
|
|
|
|
'en-US': 'Target group ID'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
text: {
|
2019-06-14 18:07:41 +03:00
|
|
|
validator: $.optional.str.pipe(MessagingMessages.validateText)
|
2018-11-01 20:32:24 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
fileId: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.type(ID),
|
2018-11-01 20:32:24 +02:00
|
|
|
}
|
2019-02-22 04:46:58 +02:00
|
|
|
},
|
|
|
|
|
2019-02-24 20:43:19 +02:00
|
|
|
res: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
ref: 'MessagingMessage',
|
2019-02-24 20:43:19 +02:00
|
|
|
},
|
|
|
|
|
2019-02-22 04:46:58 +02:00
|
|
|
errors: {
|
|
|
|
recipientIsYourself: {
|
|
|
|
message: 'You can not send a message to yourself.',
|
|
|
|
code: 'RECIPIENT_IS_YOURSELF',
|
|
|
|
id: '17e2ba79-e22a-4cbc-bf91-d327643f4a7e'
|
|
|
|
},
|
|
|
|
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
|
|
|
id: '11795c64-40ea-4198-b06e-3c873ed9039d'
|
|
|
|
},
|
|
|
|
|
2019-05-18 14:36:33 +03:00
|
|
|
noSuchGroup: {
|
|
|
|
message: 'No such group.',
|
|
|
|
code: 'NO_SUCH_GROUP',
|
|
|
|
id: 'c94e2a5d-06aa-4914-8fa6-6a42e73d6537'
|
|
|
|
},
|
|
|
|
|
|
|
|
groupAccessDenied: {
|
|
|
|
message: 'You can not send messages to groups that you have not joined.',
|
|
|
|
code: 'GROUP_ACCESS_DENIED',
|
|
|
|
id: 'd96b3cca-5ad1-438b-ad8b-02f931308fbd'
|
|
|
|
},
|
|
|
|
|
2019-02-22 04:46:58 +02:00
|
|
|
noSuchFile: {
|
|
|
|
message: 'No such file.',
|
|
|
|
code: 'NO_SUCH_FILE',
|
|
|
|
id: '4372b8e2-185d-4146-8749-2f68864a3e5f'
|
|
|
|
},
|
|
|
|
|
|
|
|
contentRequired: {
|
|
|
|
message: 'Content required. You need to set text or fileId.',
|
|
|
|
code: 'CONTENT_REQUIRED',
|
|
|
|
id: '25587321-b0e6-449c-9239-f8925092942c'
|
|
|
|
}
|
2018-11-01 20:32:24 +02:00
|
|
|
}
|
2018-07-16 22:36:44 +03:00
|
|
|
};
|
|
|
|
|
2019-02-22 04:46:58 +02:00
|
|
|
export default define(meta, async (ps, user) => {
|
2019-05-18 14:36:33 +03:00
|
|
|
let recipientUser: User | undefined;
|
|
|
|
let recipientGroup: UserGroup | undefined;
|
2017-03-01 07:43:41 +02:00
|
|
|
|
2019-05-18 14:36:33 +03:00
|
|
|
if (ps.userId != null) {
|
|
|
|
// Myself
|
|
|
|
if (ps.userId === user.id) {
|
|
|
|
throw new ApiError(meta.errors.recipientIsYourself);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch recipient (user)
|
|
|
|
recipientUser = await getUser(ps.userId).catch(e => {
|
|
|
|
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser);
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
} else if (ps.groupId != null) {
|
|
|
|
// Fetch recipient (group)
|
|
|
|
recipientGroup = await UserGroups.findOne(ps.groupId);
|
|
|
|
|
|
|
|
if (recipientGroup == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check joined
|
|
|
|
const joining = await UserGroupJoinings.findOne({
|
|
|
|
userId: user.id,
|
|
|
|
userGroupId: recipientGroup.id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (joining == null) {
|
|
|
|
throw new ApiError(meta.errors.groupAccessDenied);
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 07:43:41 +02:00
|
|
|
|
2017-03-03 01:24:48 +02:00
|
|
|
let file = null;
|
2018-11-01 20:32:24 +02:00
|
|
|
if (ps.fileId != null) {
|
2019-04-07 15:50:36 +03:00
|
|
|
file = await DriveFiles.findOne({
|
|
|
|
id: ps.fileId,
|
|
|
|
userId: user.id
|
2016-12-29 00:49:51 +02:00
|
|
|
});
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
if (file == null) {
|
2019-02-22 04:46:58 +02:00
|
|
|
throw new ApiError(meta.errors.noSuchFile);
|
2016-12-29 00:49:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// テキストが無いかつ添付ファイルも無かったらエラー
|
2018-11-01 20:32:24 +02:00
|
|
|
if (ps.text == null && file == null) {
|
2019-02-22 04:46:58 +02:00
|
|
|
throw new ApiError(meta.errors.contentRequired);
|
2016-12-29 00:49:51 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 23:01:14 +02:00
|
|
|
return await createMessage(user, recipientUser, recipientGroup, ps.text, file);
|
2019-02-22 04:46:58 +02:00
|
|
|
});
|