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

159 lines
4.3 KiB
TypeScript
Raw Normal View History

2019-02-05 04:48:08 +02:00
import $ from 'cafy';
import ID, { transform } from '../../../../../misc/cafy-id';
2018-03-29 14:32:18 +03:00
import Message from '../../../../../models/messaging-message';
import { isValidText } from '../../../../../models/messaging-message';
2018-11-02 06:47:44 +02:00
import User 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';
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';
import { ApiError } from '../../../error';
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,
2018-11-01 20:32:24 +02:00
kind: 'messaging-write',
params: {
userId: {
validator: $.type(ID),
transform: transform,
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-02-13 09:33:07 +02:00
validator: $.optional.str.pipe(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
transform: transform,
}
},
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
};
export default define(meta, async (ps, user) => {
2017-03-03 01:24:48 +02:00
// Myself
2018-11-01 20:32:24 +02:00
if (ps.userId.equals(user._id)) {
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
const recipient = await User.findOne({
2018-11-01 20:32:24 +02:00
_id: ps.userId
2017-03-03 01:24:48 +02:00
}, {
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) {
throw new ApiError(meta.errors.noSuchUser);
2016-12-29 00:49:51 +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) {
2016-12-29 00:49:51 +02:00
file = await DriveFile.findOne({
2018-11-01 20:32:24 +02:00
_id: ps.fileId,
2018-03-29 08:48:47 +03:00
'metadata.userId': user._id
2016-12-29 00:49:51 +02:00
});
if (file === null) {
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) {
throw new ApiError(meta.errors.contentRequired);
2016-12-29 00:49:51 +02:00
}
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,
2018-11-01 20:32:24 +02:00
text: ps.text ? ps.text.trim() : undefined,
2018-03-29 08:48:47 +03:00
userId: user._id,
isRead: false
2016-12-29 00:49:51 +02:00
});
2018-02-02 01:21:30 +02:00
const messageObj = await 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);
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);
publishMainStream(message.recipientId, 'messagingMessage', 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
}
});
2018-10-07 20:10:46 +03:00
// 2秒経っても(今回作成した)メッセージが既読にならなかったら「未読のメッセージがありますよ」イベントを発行する
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 == null) return; // メッセージが削除されている場合もある
2018-03-29 08:48:47 +03:00
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
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);
return messageObj;
});