Sharkey/src/server/api/endpoints/messaging/messages/create.ts

151 lines
4.2 KiB
TypeScript
Raw Normal View History

2018-07-07 13:19:00 +03:00
import $ from 'cafy'; import ID from '../../../../../misc/cafy-id';
2018-03-29 14:32:18 +03:00
import Message from '../../../../../models/messaging-message';
import { isValidText } from '../../../../../models/messaging-message';
import History from '../../../../../models/messaging-history';
2018-06-18 03:54:53 +03:00
import User, { ILocalUser } from '../../../../../models/user';
2018-03-29 14:32:18 +03:00
import Mute from '../../../../../models/mute';
import DriveFile from '../../../../../models/drive-file';
import { pack } from '../../../../../models/messaging-message';
2018-07-30 01:20:27 +03:00
import { publishUserStream } from '../../../../../stream';
2018-07-07 13:19:00 +03:00
import { publishMessagingStream, publishMessagingIndexStream } from '../../../../../stream';
import pushSw from '../../../../../push-sw';
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
},
requireCredential: true,
kind: 'messaging-write'
};
2018-07-05 20:58:29 +03:00
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
2018-03-29 08:48:47 +03:00
// Get 'userId' parameter
2018-05-02 12:06:16 +03:00
const [recipientId, recipientIdErr] = $.type(ID).get(params.userId);
2018-03-29 08:48:47 +03:00
if (recipientIdErr) return rej('invalid userId param');
2017-03-01 07:49:04 +02:00
2017-03-03 01:24:48 +02:00
// Myself
if (recipientId.equals(user._id)) {
return rej('cannot send message to myself');
}
2017-03-01 07:43:41 +02:00
2017-03-03 01:24:48 +02:00
// Fetch recipient
const recipient = await User.findOne({
_id: recipientId
}, {
fields: {
_id: true
2017-03-01 07:43:41 +02:00
}
2017-03-03 01:24:48 +02:00
});
2017-03-01 07:43:41 +02:00
2017-03-03 01:24:48 +02:00
if (recipient === null) {
return rej('user not found');
2016-12-29 00:49:51 +02:00
}
// Get 'text' parameter
2018-07-05 17:36:07 +03:00
const [text, textErr] = $.str.optional.pipe(isValidText).get(params.text);
2017-03-03 01:24:48 +02:00
if (textErr) return rej('invalid text');
2016-12-29 00:49:51 +02:00
2018-03-29 08:48:47 +03:00
// Get 'fileId' parameter
2018-07-05 17:36:07 +03:00
const [fileId, fileIdErr] = $.type(ID).optional.get(params.fileId);
2018-03-29 08:48:47 +03:00
if (fileIdErr) return rej('invalid fileId param');
2017-03-03 01:24:48 +02:00
let file = null;
2017-03-05 20:50:27 +02:00
if (fileId !== undefined) {
2016-12-29 00:49:51 +02:00
file = await DriveFile.findOne({
2017-03-03 01:24:48 +02:00
_id: fileId,
2018-03-29 08:48:47 +03:00
'metadata.userId': user._id
2016-12-29 00:49:51 +02:00
});
if (file === null) {
return rej('file not found');
}
}
// テキストが無いかつ添付ファイルも無かったらエラー
2017-03-05 20:50:27 +02:00
if (text === undefined && file === null) {
2016-12-29 00:49:51 +02:00
return rej('text or file is required');
}
// メッセージを作成
2017-01-20 10:38:05 +02:00
const message = await Message.insert({
2018-03-29 08:48:47 +03:00
createdAt: new Date(),
fileId: file ? file._id : undefined,
recipientId: recipient._id,
2016-12-29 00:49:51 +02:00
text: text ? text : undefined,
2018-03-29 08:48:47 +03:00
userId: user._id,
isRead: false
2016-12-29 00:49:51 +02:00
});
// Serialize
2018-02-02 01:21:30 +02:00
const messageObj = await pack(message);
2016-12-29 00:49:51 +02:00
// Reponse
res(messageObj);
// 自分のストリーム
2018-03-29 08:48:47 +03:00
publishMessagingStream(message.userId, message.recipientId, 'message', messageObj);
publishMessagingIndexStream(message.userId, 'message', messageObj);
publishUserStream(message.userId, 'messaging_message', 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);
publishUserStream(message.recipientId, 'messaging_message', messageObj);
2016-12-29 00:49:51 +02:00
2018-05-28 19:22:39 +03:00
// Update flag
User.update({ _id: recipient._id }, {
$set: {
hasUnreadMessagingMessage: true
}
});
2017-06-12 21:42:45 +03:00
// 3秒経っても(今回作成した)メッセージが既読にならなかったら「未読のメッセージがありますよ」イベントを発行する
2016-12-29 00:49:51 +02:00
setTimeout(async () => {
2018-03-29 08:48:47 +03:00
const freshMessage = await Message.findOne({ _id: message._id }, { isRead: true });
if (!freshMessage.isRead) {
2017-12-22 09:22:33 +02:00
//#region ただしミュートされているなら発行しない
2017-12-22 07:21:40 +02:00
const mute = await Mute.find({
2018-03-29 08:48:47 +03:00
muterId: recipient._id,
deletedAt: { $exists: false }
2017-12-22 07:21:40 +02:00
});
2018-03-29 08:48:47 +03:00
const mutedUserIds = mute.map(m => m.muteeId.toString());
2017-12-22 07:21:40 +02:00
if (mutedUserIds.indexOf(user._id.toString()) != -1) {
return;
}
//#endregion
2018-03-29 08:48:47 +03:00
publishUserStream(message.recipientId, 'unread_messaging_message', messageObj);
pushSw(message.recipientId, 'unread_messaging_message', messageObj);
2016-12-29 00:49:51 +02:00
}
2017-06-12 21:42:45 +03:00
}, 3000);
2016-12-29 00:49:51 +02:00
// 履歴作成(自分)
2017-01-17 04:11:22 +02:00
History.update({
2018-03-29 08:48:47 +03:00
userId: user._id,
partnerId: recipient._id
2016-12-29 00:49:51 +02:00
}, {
2018-03-29 08:48:47 +03:00
updatedAt: new Date(),
userId: user._id,
partnerId: recipient._id,
messageId: message._id
2016-12-29 00:49:51 +02:00
}, {
upsert: true
});
// 履歴作成(相手)
2017-01-17 04:11:22 +02:00
History.update({
2018-03-29 08:48:47 +03:00
userId: recipient._id,
partnerId: user._id
2016-12-29 00:49:51 +02:00
}, {
2018-03-29 08:48:47 +03:00
updatedAt: new Date(),
userId: recipient._id,
partnerId: user._id,
messageId: message._id
2016-12-29 00:49:51 +02:00
}, {
upsert: true
});
});