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-05-18 14:36:33 +03:00
|
|
|
import { publishMainStream, publishGroupMessagingStream } from '../../../../../services/stream';
|
2019-02-05 07:14:23 +02:00
|
|
|
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-05-18 14:36:33 +03:00
|
|
|
import { MessagingMessages, DriveFiles, Mutings, UserGroups, UserGroupJoinings } from '../../../../../models';
|
2019-04-07 15:50:36 +03:00
|
|
|
import { MessagingMessage } from '../../../../../models/entities/messaging-message';
|
|
|
|
import { genId } from '../../../../../misc/gen-id';
|
2019-05-18 14:36:33 +03:00
|
|
|
import { User } from '../../../../../models/entities/user';
|
|
|
|
import { UserGroup } from '../../../../../models/entities/user-group';
|
|
|
|
import { Not } from 'typeorm';
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-07-16 22:36:44 +03:00
|
|
|
export const meta = {
|
|
|
|
desc: {
|
2019-05-18 14:36:33 +03:00
|
|
|
'ja-JP': 'トークメッセージを送信します。',
|
2018-08-29 00:59:43 +03:00
|
|
|
'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: {
|
2019-05-18 14:36:33 +03:00
|
|
|
validator: $.optional.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
|
|
|
},
|
|
|
|
|
2019-05-18 14:36:33 +03:00
|
|
|
groupId: {
|
|
|
|
validator: $.optional.type(ID),
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '対象のグループのID',
|
|
|
|
'en-US': 'Target group ID'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
text: {
|
2019-06-14 18:07:41 +03:00
|
|
|
validator: $.optional.str.pipe(MessagingMessages.validateText)
|
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: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
ref: 'MessagingMessage',
|
2019-02-24 20:43:19 +02:00
|
|
|
},
|
|
|
|
|
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'
|
|
|
|
},
|
|
|
|
|
2019-05-18 14:36:33 +03:00
|
|
|
noSuchGroup: {
|
|
|
|
message: 'No such group.',
|
|
|
|
code: 'NO_SUCH_GROUP',
|
|
|
|
id: 'c94e2a5d-06aa-4914-8fa6-6a42e73d6537'
|
|
|
|
},
|
|
|
|
|
|
|
|
groupAccessDenied: {
|
|
|
|
message: 'You can not send messages to groups that you have not joined.',
|
|
|
|
code: 'GROUP_ACCESS_DENIED',
|
|
|
|
id: 'd96b3cca-5ad1-438b-ad8b-02f931308fbd'
|
|
|
|
},
|
|
|
|
|
2019-02-22 04:46:58 +02:00
|
|
|
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) => {
|
2019-05-18 14:36:33 +03:00
|
|
|
let recipientUser: User | undefined;
|
|
|
|
let recipientGroup: UserGroup | undefined;
|
2017-03-01 07:43:41 +02:00
|
|
|
|
2019-05-18 14:36:33 +03:00
|
|
|
if (ps.userId != null) {
|
|
|
|
// Myself
|
|
|
|
if (ps.userId === user.id) {
|
|
|
|
throw new ApiError(meta.errors.recipientIsYourself);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch recipient (user)
|
|
|
|
recipientUser = await getUser(ps.userId).catch(e => {
|
|
|
|
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser);
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
} else if (ps.groupId != null) {
|
|
|
|
// Fetch recipient (group)
|
|
|
|
recipientGroup = await UserGroups.findOne(ps.groupId);
|
|
|
|
|
|
|
|
if (recipientGroup == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check joined
|
|
|
|
const joining = await UserGroupJoinings.findOne({
|
|
|
|
userId: user.id,
|
|
|
|
userGroupId: recipientGroup.id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (joining == null) {
|
|
|
|
throw new ApiError(meta.errors.groupAccessDenied);
|
|
|
|
}
|
|
|
|
}
|
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,
|
2019-05-18 14:36:33 +03:00
|
|
|
recipientId: recipientUser ? recipientUser.id : null,
|
|
|
|
groupId: recipientGroup ? recipientGroup.id : null,
|
2019-04-07 15:50:36 +03:00
|
|
|
text: ps.text ? ps.text.trim() : null,
|
|
|
|
userId: user.id,
|
2019-05-18 14:36:33 +03:00
|
|
|
isRead: false,
|
|
|
|
reads: [] as any[]
|
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
|
|
|
|
2019-05-18 14:36:33 +03:00
|
|
|
if (recipientUser) {
|
|
|
|
// 自分のストリーム
|
|
|
|
publishMessagingStream(message.userId, recipientUser.id, 'message', messageObj);
|
|
|
|
publishMessagingIndexStream(message.userId, 'message', messageObj);
|
|
|
|
publishMainStream(message.userId, 'messagingMessage', messageObj);
|
|
|
|
|
|
|
|
// 相手のストリーム
|
|
|
|
publishMessagingStream(recipientUser.id, message.userId, 'message', messageObj);
|
|
|
|
publishMessagingIndexStream(recipientUser.id, 'message', messageObj);
|
|
|
|
publishMainStream(recipientUser.id, 'messagingMessage', messageObj);
|
|
|
|
} else if (recipientGroup) {
|
|
|
|
// グループのストリーム
|
|
|
|
publishGroupMessagingStream(recipientGroup.id, 'message', messageObj);
|
|
|
|
|
|
|
|
// メンバーのストリーム
|
|
|
|
const joinings = await UserGroupJoinings.find({ userGroupId: recipientGroup.id });
|
|
|
|
for (const joining of joinings) {
|
|
|
|
publishMessagingIndexStream(joining.userId, 'message', messageObj);
|
|
|
|
publishMainStream(joining.userId, '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-05-18 14:36:33 +03:00
|
|
|
const freshMessage = await MessagingMessages.findOne(message.id);
|
2018-12-26 18:24:57 +02:00
|
|
|
if (freshMessage == null) return; // メッセージが削除されている場合もある
|
2019-05-18 14:36:33 +03:00
|
|
|
|
|
|
|
if (recipientUser) {
|
|
|
|
if (freshMessage.isRead) return; // 既読
|
|
|
|
|
2017-12-22 09:22:33 +02:00
|
|
|
//#region ただしミュートされているなら発行しない
|
2019-04-07 15:50:36 +03:00
|
|
|
const mute = await Mutings.find({
|
2019-05-18 14:36:33 +03:00
|
|
|
muterId: recipientUser.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
|
|
|
|
|
2019-05-18 14:36:33 +03:00
|
|
|
publishMainStream(recipientUser.id, 'unreadMessagingMessage', messageObj);
|
|
|
|
pushSw(recipientUser.id, 'unreadMessagingMessage', messageObj);
|
|
|
|
} else if (recipientGroup) {
|
|
|
|
const joinings = await UserGroupJoinings.find({ userGroupId: recipientGroup.id, userId: Not(user.id) });
|
|
|
|
for (const joining of joinings) {
|
|
|
|
if (freshMessage.reads.includes(joining.userId)) return; // 既読
|
|
|
|
publishMainStream(joining.userId, 'unreadMessagingMessage', messageObj);
|
|
|
|
pushSw(joining.userId, '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;
|
|
|
|
});
|