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