Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
67 lines (57 sloc) 1.85 KB
import { publishMainStream } from './stream';
import pushSw from './push-notification';
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';
export async function createNotification(
notifieeId: User['id'],
notifierId: User['id'],
type: string,
content?: {
noteId?: Note['id'];
reaction?: string;
choice?: number;
}
) {
if (notifieeId === notifierId) {
return null;
}
const data = {
id: genId(),
createdAt: new Date(),
notifieeId: notifieeId,
notifierId: notifierId,
type: type,
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;
}
// Create notification
const notification = await Notifications.save(data);
const packed = await Notifications.pack(notification);
// Publish notification event
publishMainStream(notifieeId, 'notification', packed);
// 2็ง’็ตŒใฃใฆใ‚‚(ไปŠๅ›žไฝœๆˆใ—ใŸ)้€š็ŸฅใŒๆ—ข่ชญใซใชใ‚‰ใชใ‹ใฃใŸใ‚‰ใ€Œๆœช่ชญใฎ้€š็ŸฅใŒใ‚ใ‚Šใพใ™ใ‚ˆใ€ใ‚คใƒ™ใƒณใƒˆใ‚’็™บ่กŒใ™ใ‚‹
setTimeout(async () => {
const fresh = await Notifications.findOne(notification.id);
if (fresh == null) return; // ๆ—ขใซๅ‰Š้™คใ•ใ‚Œใฆใ„ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใชใ„
if (!fresh.isRead) {
//#region ใŸใ ใ—ใƒŸใƒฅใƒผใƒˆใ—ใฆใ„ใ‚‹ใƒฆใƒผใ‚ถใƒผใ‹ใ‚‰ใฎ้€š็Ÿฅใชใ‚‰็„ก่ฆ–
const mutings = await Mutings.find({
muterId: notifieeId
});
if (mutings.map(m => m.muteeId).includes(notifierId)) {
return;
}
//#endregion
publishMainStream(notifieeId, 'unreadNotification', packed);
pushSw(notifieeId, 'notification', packed);
}
}, 2000);
return notification;
}
You canโ€™t perform that action at this time.