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';
|
2019-02-05 07:14:23 +02:00
|
|
|
import { publishMainStream } from '../../../../../services/stream';
|
|
|
|
import { publishMessagingStream, publishMessagingIndexStream } from '../../../../../services/stream';
|
|
|
|
import pushSw from '../../../../../services/push-notification';
|
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-04-07 15:50:36 +03:00
|
|
|
import { MessagingMessages, DriveFiles, Mutings } from '../../../../../models';
|
|
|
|
import { MessagingMessage } from '../../../../../models/entities/messaging-message';
|
|
|
|
import { genId } from '../../../../../misc/gen-id';
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-07-16 22:36:44 +03:00
|
|
|
export const meta = {
|
|
|
|
desc: {
|
2018-08-29 00:59:43 +03:00
|
|
|
'ja-JP': '指定したユーザーへMessagingのメッセージを送信します。',
|
|
|
|
'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'],
|
|
|
|
|
2018-07-16 22:36:44 +03:00
|
|
|
requireCredential: true,
|
|
|
|
|
2019-04-15 06:10:40 +03:00
|
|
|
kind: 'write:messaging',
|
2018-11-01 20:32:24 +02:00
|
|
|
|
|
|
|
params: {
|
|
|
|
userId: {
|
|
|
|
validator: $.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
|
|
|
},
|
|
|
|
|
|
|
|
text: {
|
2019-04-07 15:50:36 +03:00
|
|
|
validator: $.optional.str.pipe(MessagingMessages.isValidText)
|
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: {
|
|
|
|
type: 'MessagingMessage',
|
|
|
|
},
|
|
|
|
|
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'
|
|
|
|
},
|
|
|
|
|
|
|
|
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) => {
|
2017-03-03 01:24:48 +02:00
|
|
|
// Myself
|
2019-04-07 15:50:36 +03:00
|
|
|
if (ps.userId === user.id) {
|
2019-02-22 04:46:58 +02:00
|
|
|
throw new ApiError(meta.errors.recipientIsYourself);
|
2017-03-03 01:24:48 +02:00
|
|
|
}
|
2017-03-01 07:43:41 +02:00
|
|
|
|
2017-03-03 01:24:48 +02:00
|
|
|
// Fetch recipient
|
2019-02-22 07:02:56 +02:00
|
|
|
const recipient = await getUser(ps.userId).catch(e => {
|
|
|
|
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser);
|
|
|
|
throw e;
|
2017-03-03 01:24:48 +02:00
|
|
|
});
|
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-04-07 15:50:36 +03:00
|
|
|
const message = await MessagingMessages.save({
|
|
|
|
id: genId(),
|
2018-03-29 08:48:47 +03:00
|
|
|
createdAt: new Date(),
|
2019-04-07 15:50:36 +03:00
|
|
|
fileId: file ? file.id : null,
|
|
|
|
recipientId: recipient.id,
|
|
|
|
text: ps.text ? ps.text.trim() : null,
|
|
|
|
userId: user.id,
|
2018-03-29 08:48:47 +03:00
|
|
|
isRead: false
|
2019-04-07 15:50:36 +03:00
|
|
|
} as MessagingMessage);
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
const messageObj = await MessagingMessages.pack(message);
|
2016-12-29 00:49:51 +02:00
|
|
|
|
|
|
|
// 自分のストリーム
|
2018-03-29 08:48:47 +03:00
|
|
|
publishMessagingStream(message.userId, message.recipientId, 'message', messageObj);
|
|
|
|
publishMessagingIndexStream(message.userId, 'message', messageObj);
|
2018-10-07 05:06:17 +03:00
|
|
|
publishMainStream(message.userId, 'messagingMessage', messageObj);
|
2016-12-29 00:49:51 +02:00
|
|
|
|
|
|
|
// 相手のストリーム
|
2018-03-29 08:48:47 +03:00
|
|
|
publishMessagingStream(message.recipientId, message.userId, 'message', messageObj);
|
|
|
|
publishMessagingIndexStream(message.recipientId, 'message', messageObj);
|
2018-10-07 05:06:17 +03:00
|
|
|
publishMainStream(message.recipientId, 'messagingMessage', messageObj);
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-10-07 20:10:46 +03:00
|
|
|
// 2秒経っても(今回作成した)メッセージが既読にならなかったら「未読のメッセージがありますよ」イベントを発行する
|
2016-12-29 00:49:51 +02:00
|
|
|
setTimeout(async () => {
|
2019-04-07 15:50:36 +03:00
|
|
|
const freshMessage = await MessagingMessages.findOne({ id: message.id });
|
2018-12-26 18:24:57 +02:00
|
|
|
if (freshMessage == null) return; // メッセージが削除されている場合もある
|
2018-03-29 08:48:47 +03:00
|
|
|
if (!freshMessage.isRead) {
|
2017-12-22 09:22:33 +02:00
|
|
|
//#region ただしミュートされているなら発行しない
|
2019-04-07 15:50:36 +03:00
|
|
|
const mute = await Mutings.find({
|
|
|
|
muterId: recipient.id,
|
2017-12-22 07:21:40 +02:00
|
|
|
});
|
2018-03-29 08:48:47 +03:00
|
|
|
const mutedUserIds = mute.map(m => m.muteeId.toString());
|
2019-04-07 15:50:36 +03:00
|
|
|
if (mutedUserIds.indexOf(user.id) != -1) {
|
2017-12-22 07:21:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2018-10-07 05:06:17 +03:00
|
|
|
publishMainStream(message.recipientId, 'unreadMessagingMessage', messageObj);
|
|
|
|
pushSw(message.recipientId, 'unreadMessagingMessage', messageObj);
|
2016-12-29 00:49:51 +02:00
|
|
|
}
|
2018-10-07 20:10:46 +03:00
|
|
|
}, 2000);
|
2019-02-22 04:46:58 +02:00
|
|
|
|
|
|
|
return messageObj;
|
|
|
|
});
|