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