2016-12-28 22:49:51 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2017-03-08 18:50:09 +00:00
|
|
|
import $ from 'cafy';
|
2016-12-28 22:49:51 +00:00
|
|
|
import Message from '../../../models/messaging-message';
|
2017-03-02 23:24:48 +00:00
|
|
|
import { isValidText } from '../../../models/messaging-message';
|
2016-12-28 22:49:51 +00:00
|
|
|
import History from '../../../models/messaging-history';
|
|
|
|
import User from '../../../models/user';
|
2017-12-22 05:21:40 +00:00
|
|
|
import Mute from '../../../models/mute';
|
2016-12-28 22:49:51 +00:00
|
|
|
import DriveFile from '../../../models/drive-file';
|
|
|
|
import serialize from '../../../serializers/messaging-message';
|
|
|
|
import publishUserStream from '../../../event';
|
2017-11-20 20:09:45 +00:00
|
|
|
import { publishMessagingStream, publishMessagingIndexStream, pushSw } from '../../../event';
|
2017-01-24 20:41:14 +00:00
|
|
|
import config from '../../../../conf';
|
2016-12-28 22:49:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a message
|
|
|
|
*
|
2017-03-01 08:37:01 +00:00
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @return {Promise<any>}
|
2016-12-28 22:49:51 +00:00
|
|
|
*/
|
2017-03-03 19:28:38 +00:00
|
|
|
module.exports = (params, user) => new Promise(async (res, rej) => {
|
2016-12-28 22:49:51 +00:00
|
|
|
// Get 'user_id' parameter
|
2017-03-08 18:50:09 +00:00
|
|
|
const [recipientId, recipientIdErr] = $(params.user_id).id().$;
|
2017-03-02 23:24:48 +00:00
|
|
|
if (recipientIdErr) return rej('invalid user_id param');
|
2017-03-01 05:49:04 +00:00
|
|
|
|
2017-03-02 23:24:48 +00:00
|
|
|
// Myself
|
|
|
|
if (recipientId.equals(user._id)) {
|
|
|
|
return rej('cannot send message to myself');
|
|
|
|
}
|
2017-03-01 05:43:41 +00:00
|
|
|
|
2017-03-02 23:24:48 +00:00
|
|
|
// Fetch recipient
|
|
|
|
const recipient = await User.findOne({
|
|
|
|
_id: recipientId
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
_id: true
|
2017-03-01 05:43:41 +00:00
|
|
|
}
|
2017-03-02 23:24:48 +00:00
|
|
|
});
|
2017-03-01 05:43:41 +00:00
|
|
|
|
2017-03-02 23:24:48 +00:00
|
|
|
if (recipient === null) {
|
|
|
|
return rej('user not found');
|
2016-12-28 22:49:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get 'text' parameter
|
2017-03-08 18:50:09 +00:00
|
|
|
const [text, textErr] = $(params.text).optional.string().pipe(isValidText).$;
|
2017-03-02 23:24:48 +00:00
|
|
|
if (textErr) return rej('invalid text');
|
2016-12-28 22:49:51 +00:00
|
|
|
|
|
|
|
// Get 'file_id' parameter
|
2017-03-08 18:50:09 +00:00
|
|
|
const [fileId, fileIdErr] = $(params.file_id).optional.id().$;
|
2017-03-02 23:24:48 +00:00
|
|
|
if (fileIdErr) return rej('invalid file_id param');
|
|
|
|
|
|
|
|
let file = null;
|
2017-03-05 18:50:27 +00:00
|
|
|
if (fileId !== undefined) {
|
2016-12-28 22:49:51 +00:00
|
|
|
file = await DriveFile.findOne({
|
2017-03-02 23:24:48 +00:00
|
|
|
_id: fileId,
|
2017-11-06 07:22:18 +00:00
|
|
|
'metadata.user_id': user._id
|
2016-12-28 22:49:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (file === null) {
|
|
|
|
return rej('file not found');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// テキストが無いかつ添付ファイルも無かったらエラー
|
2017-03-05 18:50:27 +00:00
|
|
|
if (text === undefined && file === null) {
|
2016-12-28 22:49:51 +00:00
|
|
|
return rej('text or file is required');
|
|
|
|
}
|
|
|
|
|
|
|
|
// メッセージを作成
|
2017-01-20 08:38:05 +00:00
|
|
|
const message = await Message.insert({
|
2016-12-28 22:49:51 +00:00
|
|
|
created_at: new Date(),
|
|
|
|
file_id: file ? file._id : undefined,
|
|
|
|
recipient_id: recipient._id,
|
|
|
|
text: text ? text : undefined,
|
|
|
|
user_id: user._id,
|
|
|
|
is_read: false
|
|
|
|
});
|
|
|
|
|
|
|
|
// Serialize
|
|
|
|
const messageObj = await serialize(message);
|
|
|
|
|
|
|
|
// Reponse
|
|
|
|
res(messageObj);
|
|
|
|
|
|
|
|
// 自分のストリーム
|
|
|
|
publishMessagingStream(message.user_id, message.recipient_id, 'message', messageObj);
|
2017-11-13 15:54:16 +00:00
|
|
|
publishMessagingIndexStream(message.user_id, 'message', messageObj);
|
2016-12-28 22:49:51 +00:00
|
|
|
publishUserStream(message.user_id, 'messaging_message', messageObj);
|
|
|
|
|
|
|
|
// 相手のストリーム
|
|
|
|
publishMessagingStream(message.recipient_id, message.user_id, 'message', messageObj);
|
2017-11-13 15:54:16 +00:00
|
|
|
publishMessagingIndexStream(message.recipient_id, 'message', messageObj);
|
2016-12-28 22:49:51 +00:00
|
|
|
publishUserStream(message.recipient_id, 'messaging_message', messageObj);
|
|
|
|
|
2017-06-12 18:42:45 +00:00
|
|
|
// 3秒経っても(今回作成した)メッセージが既読にならなかったら「未読のメッセージがありますよ」イベントを発行する
|
2016-12-28 22:49:51 +00:00
|
|
|
setTimeout(async () => {
|
|
|
|
const freshMessage = await Message.findOne({ _id: message._id }, { is_read: true });
|
|
|
|
if (!freshMessage.is_read) {
|
2017-12-22 05:21:40 +00:00
|
|
|
//#region ただしミュートしているユーザーからの通知なら無視
|
|
|
|
const mute = await Mute.find({
|
|
|
|
muter_id: recipient._id,
|
|
|
|
deleted_at: { $exists: false }
|
|
|
|
});
|
|
|
|
const mutedUserIds = mute.map(m => m.mutee_id.toString());
|
|
|
|
if (mutedUserIds.indexOf(user._id.toString()) != -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2016-12-28 22:49:51 +00:00
|
|
|
publishUserStream(message.recipient_id, 'unread_messaging_message', messageObj);
|
2017-11-20 20:09:45 +00:00
|
|
|
pushSw(message.recipient_id, 'unread_messaging_message', messageObj);
|
2016-12-28 22:49:51 +00:00
|
|
|
}
|
2017-06-12 18:42:45 +00:00
|
|
|
}, 3000);
|
2016-12-28 22:49:51 +00:00
|
|
|
|
|
|
|
// Register to search database
|
|
|
|
if (message.text && config.elasticsearch.enable) {
|
|
|
|
const es = require('../../../db/elasticsearch');
|
|
|
|
|
|
|
|
es.index({
|
|
|
|
index: 'misskey',
|
|
|
|
type: 'messaging_message',
|
|
|
|
id: message._id.toString(),
|
|
|
|
body: {
|
|
|
|
text: message.text
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// 履歴作成(自分)
|
2017-01-17 02:11:22 +00:00
|
|
|
History.update({
|
2016-12-28 22:49:51 +00:00
|
|
|
user_id: user._id,
|
|
|
|
partner: recipient._id
|
|
|
|
}, {
|
|
|
|
updated_at: new Date(),
|
|
|
|
user_id: user._id,
|
|
|
|
partner: recipient._id,
|
|
|
|
message: message._id
|
|
|
|
}, {
|
|
|
|
upsert: true
|
|
|
|
});
|
|
|
|
|
|
|
|
// 履歴作成(相手)
|
2017-01-17 02:11:22 +00:00
|
|
|
History.update({
|
2016-12-28 22:49:51 +00:00
|
|
|
user_id: recipient._id,
|
|
|
|
partner: user._id
|
|
|
|
}, {
|
|
|
|
updated_at: new Date(),
|
|
|
|
user_id: recipient._id,
|
|
|
|
partner: user._id,
|
|
|
|
message: message._id
|
|
|
|
}, {
|
|
|
|
upsert: true
|
|
|
|
});
|
|
|
|
});
|