mirror of
				https://codeberg.org/yeentown/barkey.git
				synced 2025-10-31 13:34:12 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			50 lines
		
	
	
	
		
			1.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			50 lines
		
	
	
	
		
			1.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import * as mongo from 'mongodb';
 | |
| import Notification from '../models/notification';
 | |
| import Mute from '../models/mute';
 | |
| import { pack } from '../models/notification';
 | |
| import stream from './stream';
 | |
| 
 | |
| export default (
 | |
| 	notifiee: mongo.ObjectID,
 | |
| 	notifier: mongo.ObjectID,
 | |
| 	type: string,
 | |
| 	content?: any
 | |
| ) => new Promise<any>(async (resolve, reject) => {
 | |
| 	if (notifiee.equals(notifier)) {
 | |
| 		return resolve();
 | |
| 	}
 | |
| 
 | |
| 	// Create notification
 | |
| 	const notification = await Notification.insert(Object.assign({
 | |
| 		createdAt: new Date(),
 | |
| 		notifieeId: notifiee,
 | |
| 		notifierId: notifier,
 | |
| 		type: type,
 | |
| 		isRead: false
 | |
| 	}, content));
 | |
| 
 | |
| 	resolve(notification);
 | |
| 
 | |
| 	// Publish notification event
 | |
| 	stream(notifiee, 'notification',
 | |
| 		await pack(notification));
 | |
| 
 | |
| 	// 3秒経っても(今回作成した)通知が既読にならなかったら「未読の通知がありますよ」イベントを発行する
 | |
| 	setTimeout(async () => {
 | |
| 		const fresh = await Notification.findOne({ _id: notification._id }, { isRead: true });
 | |
| 		if (!fresh.isRead) {
 | |
| 			//#region ただしミュートしているユーザーからの通知なら無視
 | |
| 			const mute = await Mute.find({
 | |
| 				muterId: notifiee,
 | |
| 				deletedAt: { $exists: false }
 | |
| 			});
 | |
| 			const mutedUserIds = mute.map(m => m.muteeId.toString());
 | |
| 			if (mutedUserIds.indexOf(notifier.toString()) != -1) {
 | |
| 				return;
 | |
| 			}
 | |
| 			//#endregion
 | |
| 
 | |
| 			stream(notifiee, 'unread_notification', await pack(notification));
 | |
| 		}
 | |
| 	}, 3000);
 | |
| });
 |