2018-10-07 11:06:17 +09:00
|
|
|
import { publishMainStream } from './stream';
|
2019-02-05 14:14:23 +09:00
|
|
|
import pushSw from './push-notification';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { Notifications, Mutings } from '../models';
|
|
|
|
import { genId } from '../misc/gen-id';
|
|
|
|
import { User } from '../models/entities/user';
|
|
|
|
import { Note } from '../models/entities/note';
|
|
|
|
import { Notification } from '../models/entities/notification';
|
2020-01-30 04:37:25 +09:00
|
|
|
import { FollowRequest } from '../models/entities/follow-request';
|
2020-02-13 02:17:54 +09:00
|
|
|
import { UserGroupInvitation } from '../models/entities/user-group-invitation';
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
export async function createNotification(
|
|
|
|
notifieeId: User['id'],
|
|
|
|
notifierId: User['id'],
|
2020-02-13 02:17:54 +09:00
|
|
|
type: Notification['type'],
|
2019-04-07 21:50:36 +09:00
|
|
|
content?: {
|
|
|
|
noteId?: Note['id'];
|
|
|
|
reaction?: string;
|
|
|
|
choice?: number;
|
2020-01-30 04:37:25 +09:00
|
|
|
followRequestId?: FollowRequest['id'];
|
2020-02-13 02:17:54 +09:00
|
|
|
userGroupInvitationId?: UserGroupInvitation['id'];
|
2019-04-07 21:50:36 +09:00
|
|
|
}
|
|
|
|
) {
|
|
|
|
if (notifieeId === notifierId) {
|
|
|
|
return null;
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
const data = {
|
|
|
|
id: genId(),
|
2018-03-29 14:48:47 +09:00
|
|
|
createdAt: new Date(),
|
2019-04-07 21:50:36 +09:00
|
|
|
notifieeId: notifieeId,
|
|
|
|
notifierId: notifierId,
|
2016-12-29 07:49:51 +09:00
|
|
|
type: type,
|
2019-04-07 21:50:36 +09:00
|
|
|
isRead: false,
|
|
|
|
} as Partial<Notification>;
|
|
|
|
|
|
|
|
if (content) {
|
|
|
|
if (content.noteId) data.noteId = content.noteId;
|
|
|
|
if (content.reaction) data.reaction = content.reaction;
|
|
|
|
if (content.choice) data.choice = content.choice;
|
2020-01-30 04:37:25 +09:00
|
|
|
if (content.followRequestId) data.followRequestId = content.followRequestId;
|
2020-02-13 02:17:54 +09:00
|
|
|
if (content.userGroupInvitationId) data.userGroupInvitationId = content.userGroupInvitationId;
|
2019-04-07 21:50:36 +09:00
|
|
|
}
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
// Create notification
|
|
|
|
const notification = await Notifications.save(data);
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
const packed = await Notifications.pack(notification);
|
2018-06-21 11:35:28 +09:00
|
|
|
|
2016-12-29 07:49:51 +09:00
|
|
|
// Publish notification event
|
2019-04-07 21:50:36 +09:00
|
|
|
publishMainStream(notifieeId, 'notification', packed);
|
2018-05-29 01:22:39 +09:00
|
|
|
|
2018-10-08 02:10:46 +09:00
|
|
|
// 2秒経っても(今回作成した)通知が既読にならなかったら「未読の通知がありますよ」イベントを発行する
|
2017-11-20 09:09:11 +09:00
|
|
|
setTimeout(async () => {
|
2019-04-07 21:50:36 +09:00
|
|
|
const fresh = await Notifications.findOne(notification.id);
|
2019-04-13 01:43:22 +09:00
|
|
|
if (fresh == null) return; // 既に削除されているかもしれない
|
2018-03-29 14:48:47 +09:00
|
|
|
if (!fresh.isRead) {
|
2017-12-22 07:38:57 +09:00
|
|
|
//#region ただしミュートしているユーザーからの通知なら無視
|
2019-04-07 21:50:36 +09:00
|
|
|
const mutings = await Mutings.find({
|
|
|
|
muterId: notifieeId
|
2017-12-22 07:38:57 +09:00
|
|
|
});
|
2019-04-07 21:50:36 +09:00
|
|
|
if (mutings.map(m => m.muteeId).includes(notifierId)) {
|
2017-12-22 07:38:57 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
publishMainStream(notifieeId, 'unreadNotification', packed);
|
2018-06-21 11:35:28 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
pushSw(notifieeId, 'notification', packed);
|
2017-11-20 09:09:11 +09:00
|
|
|
}
|
2018-10-08 02:10:46 +09:00
|
|
|
}, 2000);
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
|
|
return notification;
|
|
|
|
}
|