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

160 lines
4.1 KiB
TypeScript
Raw Normal View History

2018-11-01 19:32:24 +01:00
import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id';
2018-03-29 13:32:18 +02:00
import Message from '../../../../../models/messaging-message';
import { isValidText } from '../../../../../models/messaging-message';
import History from '../../../../../models/messaging-history';
2018-11-02 05:47:44 +01:00
import User from '../../../../../models/user';
2018-03-29 13:32:18 +02:00
import Mute from '../../../../../models/mute';
import DriveFile from '../../../../../models/drive-file';
import { pack } from '../../../../../models/messaging-message';
import { publishMainStream } from '../../../../../stream';
2018-07-07 12:19:00 +02:00
import { publishMessagingStream, publishMessagingIndexStream } from '../../../../../stream';
import pushSw from '../../../../../push-sw';
2018-11-02 05:47:44 +01:00
import define from '../../../define';
2016-12-28 23:49:51 +01:00
2018-07-16 21:36:44 +02:00
export const meta = {
desc: {
2018-08-28 23:59:43 +02:00
'ja-JP': '指定したユーザーへMessagingのメッセージを送信します。',
'en-US': 'Create a message of messaging.'
2018-07-16 21:36:44 +02:00
},
requireCredential: true,
2018-11-01 19:32:24 +01:00
kind: 'messaging-write',
params: {
userId: {
validator: $.type(ID),
transform: transform,
2018-11-03 14:49:36 +01:00
desc: {
'ja-JP': '対象のユーザーのID',
'en-US': 'Target user ID'
}
2018-11-01 19:32:24 +01:00
},
text: {
validator: $.str.optional.pipe(isValidText)
},
fileId: {
validator: $.type(ID).optional,
transform: transform,
}
}
2018-07-16 21:36:44 +02:00
};
2018-11-02 05:47:44 +01:00
export default define(meta, (ps, user) => new Promise(async (res, rej) => {
2017-03-03 00:24:48 +01:00
// Myself
2018-11-01 19:32:24 +01:00
if (ps.userId.equals(user._id)) {
2017-03-03 00:24:48 +01:00
return rej('cannot send message to myself');
}
2017-03-01 06:43:41 +01:00
2017-03-03 00:24:48 +01:00
// Fetch recipient
const recipient = await User.findOne({
2018-11-01 19:32:24 +01:00
_id: ps.userId
2017-03-03 00:24:48 +01:00
}, {
fields: {
_id: true
2017-03-01 06:43:41 +01:00
}
2017-03-03 00:24:48 +01:00
});
2017-03-01 06:43:41 +01:00
2017-03-03 00:24:48 +01:00
if (recipient === null) {
return rej('user not found');
2016-12-28 23:49:51 +01:00
}
2017-03-03 00:24:48 +01:00
let file = null;
2018-11-01 19:32:24 +01:00
if (ps.fileId != null) {
2016-12-28 23:49:51 +01:00
file = await DriveFile.findOne({
2018-11-01 19:32:24 +01:00
_id: ps.fileId,
2018-03-29 07:48:47 +02:00
'metadata.userId': user._id
2016-12-28 23:49:51 +01:00
});
if (file === null) {
return rej('file not found');
}
}
// テキストが無いかつ添付ファイルも無かったらエラー
2018-11-01 19:32:24 +01:00
if (ps.text == null && file == null) {
2016-12-28 23:49:51 +01:00
return rej('text or file is required');
}
// メッセージを作成
2017-01-20 09:38:05 +01:00
const message = await Message.insert({
2018-03-29 07:48:47 +02:00
createdAt: new Date(),
fileId: file ? file._id : undefined,
recipientId: recipient._id,
2018-11-01 19:32:24 +01:00
text: ps.text ? ps.text.trim() : undefined,
2018-03-29 07:48:47 +02:00
userId: user._id,
isRead: false
2016-12-28 23:49:51 +01:00
});
// Serialize
2018-02-02 00:21:30 +01:00
const messageObj = await pack(message);
2016-12-28 23:49:51 +01:00
// Reponse
res(messageObj);
// 自分のストリーム
2018-03-29 07:48:47 +02:00
publishMessagingStream(message.userId, message.recipientId, 'message', messageObj);
publishMessagingIndexStream(message.userId, 'message', messageObj);
publishMainStream(message.userId, 'messagingMessage', messageObj);
2016-12-28 23:49:51 +01:00
// 相手のストリーム
2018-03-29 07:48:47 +02:00
publishMessagingStream(message.recipientId, message.userId, 'message', messageObj);
publishMessagingIndexStream(message.recipientId, 'message', messageObj);
publishMainStream(message.recipientId, 'messagingMessage', messageObj);
2016-12-28 23:49:51 +01:00
2018-05-28 18:22:39 +02:00
// Update flag
User.update({ _id: recipient._id }, {
$set: {
hasUnreadMessagingMessage: true
}
});
2018-10-07 19:10:46 +02:00
// 2秒経っても(今回作成した)メッセージが既読にならなかったら「未読のメッセージがありますよ」イベントを発行する
2016-12-28 23:49:51 +01:00
setTimeout(async () => {
2018-03-29 07:48:47 +02:00
const freshMessage = await Message.findOne({ _id: message._id }, { isRead: true });
if (!freshMessage.isRead) {
2017-12-22 08:22:33 +01:00
//#region ただしミュートされているなら発行しない
2017-12-22 06:21:40 +01:00
const mute = await Mute.find({
2018-03-29 07:48:47 +02:00
muterId: recipient._id,
deletedAt: { $exists: false }
2017-12-22 06:21:40 +01:00
});
2018-03-29 07:48:47 +02:00
const mutedUserIds = mute.map(m => m.muteeId.toString());
2017-12-22 06:21:40 +01:00
if (mutedUserIds.indexOf(user._id.toString()) != -1) {
return;
}
//#endregion
publishMainStream(message.recipientId, 'unreadMessagingMessage', messageObj);
pushSw(message.recipientId, 'unreadMessagingMessage', messageObj);
2016-12-28 23:49:51 +01:00
}
2018-10-07 19:10:46 +02:00
}, 2000);
2016-12-28 23:49:51 +01:00
// 履歴作成(自分)
2017-01-17 03:11:22 +01:00
History.update({
2018-03-29 07:48:47 +02:00
userId: user._id,
partnerId: recipient._id
2016-12-28 23:49:51 +01:00
}, {
2018-03-29 07:48:47 +02:00
updatedAt: new Date(),
userId: user._id,
partnerId: recipient._id,
messageId: message._id
2016-12-28 23:49:51 +01:00
}, {
upsert: true
});
// 履歴作成(相手)
2017-01-17 03:11:22 +01:00
History.update({
2018-03-29 07:48:47 +02:00
userId: recipient._id,
partnerId: user._id
2016-12-28 23:49:51 +01:00
}, {
2018-03-29 07:48:47 +02:00
updatedAt: new Date(),
userId: recipient._id,
partnerId: user._id,
messageId: message._id
2016-12-28 23:49:51 +01:00
}, {
upsert: true
});
2018-11-02 05:47:44 +01:00
}));