未読の通知がある場合アイコンを表示するように
This commit is contained in:
		
							
								
								
									
										23
									
								
								src/api/endpoints/notifications/get_unread_count.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/api/endpoints/notifications/get_unread_count.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| /** | ||||
|  * Module dependencies | ||||
|  */ | ||||
| import Notification from '../../models/notification'; | ||||
|  | ||||
| /** | ||||
|  * Get count of unread notifications | ||||
|  * | ||||
|  * @param {any} params | ||||
|  * @param {any} user | ||||
|  * @return {Promise<any>} | ||||
|  */ | ||||
| module.exports = (params, user) => new Promise(async (res, rej) => { | ||||
| 	const count = await Notification | ||||
| 		.count({ | ||||
| 			notifiee_id: user._id, | ||||
| 			is_read: false | ||||
| 		}); | ||||
|  | ||||
| 	res({ | ||||
| 		count: count | ||||
| 	}); | ||||
| }); | ||||
| @@ -1,47 +0,0 @@ | ||||
| /** | ||||
|  * Module dependencies | ||||
|  */ | ||||
| import $ from 'cafy'; | ||||
| import Notification from '../../models/notification'; | ||||
| import serialize from '../../serializers/notification'; | ||||
| import event from '../../event'; | ||||
|  | ||||
| /** | ||||
|  * Mark as read a notification | ||||
|  * | ||||
|  * @param {any} params | ||||
|  * @param {any} user | ||||
|  * @return {Promise<any>} | ||||
|  */ | ||||
| module.exports = (params, user) => new Promise(async (res, rej) => { | ||||
| 	const [notificationId, notificationIdErr] = $(params.notification_id).id().$; | ||||
| 	if (notificationIdErr) return rej('invalid notification_id param'); | ||||
|  | ||||
| 	// Get notification | ||||
| 	const notification = await Notification | ||||
| 		.findOne({ | ||||
| 			_id: notificationId, | ||||
| 			i: user._id | ||||
| 		}); | ||||
|  | ||||
| 	if (notification === null) { | ||||
| 		return rej('notification-not-found'); | ||||
| 	} | ||||
|  | ||||
| 	// Update | ||||
| 	notification.is_read = true; | ||||
| 	Notification.update({ _id: notification._id }, { | ||||
| 		$set: { | ||||
| 			is_read: true | ||||
| 		} | ||||
| 	}); | ||||
|  | ||||
| 	// Response | ||||
| 	res(); | ||||
|  | ||||
| 	// Serialize | ||||
| 	const notificationObj = await serialize(notification); | ||||
|  | ||||
| 	// Publish read_notification event | ||||
| 	event(user._id, 'read_notification', notificationObj); | ||||
| }); | ||||
							
								
								
									
										32
									
								
								src/api/endpoints/notifications/mark_as_read_all.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/api/endpoints/notifications/mark_as_read_all.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | ||||
| /** | ||||
|  * Module dependencies | ||||
|  */ | ||||
| import Notification from '../../models/notification'; | ||||
| import event from '../../event'; | ||||
|  | ||||
| /** | ||||
|  * Mark as read all notifications | ||||
|  * | ||||
|  * @param {any} params | ||||
|  * @param {any} user | ||||
|  * @return {Promise<any>} | ||||
|  */ | ||||
| module.exports = (params, user) => new Promise(async (res, rej) => { | ||||
| 	// Update documents | ||||
| 	await Notification.update({ | ||||
| 		notifiee_id: user._id, | ||||
| 		is_read: false | ||||
| 	}, { | ||||
| 		$set: { | ||||
| 			is_read: true | ||||
| 		} | ||||
| 	}, { | ||||
| 		multi: true | ||||
| 	}); | ||||
|  | ||||
| 	// Response | ||||
| 	res(); | ||||
|  | ||||
| 	// 全ての通知を読みましたよというイベントを発行 | ||||
| 	event(user._id, 'read_all_notifications'); | ||||
| }); | ||||
		Reference in New Issue
	
	Block a user
	 syuilo
					syuilo