未読の通知がある場合アイコンを表示するように
This commit is contained in:
52
src/api/common/read-notification.ts
Normal file
52
src/api/common/read-notification.ts
Normal file
@ -0,0 +1,52 @@
|
||||
import * as mongo from 'mongodb';
|
||||
import { default as Notification, INotification } from '../models/notification';
|
||||
import publishUserStream from '../event';
|
||||
|
||||
/**
|
||||
* Mark as read notification(s)
|
||||
*/
|
||||
export default (
|
||||
user: string | mongo.ObjectID,
|
||||
message: string | string[] | INotification | INotification[] | mongo.ObjectID | mongo.ObjectID[]
|
||||
) => new Promise<any>(async (resolve, reject) => {
|
||||
|
||||
const userId = mongo.ObjectID.prototype.isPrototypeOf(user)
|
||||
? user
|
||||
: new mongo.ObjectID(user);
|
||||
|
||||
const ids: mongo.ObjectID[] = Array.isArray(message)
|
||||
? mongo.ObjectID.prototype.isPrototypeOf(message[0])
|
||||
? (message as mongo.ObjectID[])
|
||||
: typeof message[0] === 'string'
|
||||
? (message as string[]).map(m => new mongo.ObjectID(m))
|
||||
: (message as INotification[]).map(m => m._id)
|
||||
: mongo.ObjectID.prototype.isPrototypeOf(message)
|
||||
? [(message as mongo.ObjectID)]
|
||||
: typeof message === 'string'
|
||||
? [new mongo.ObjectID(message)]
|
||||
: [(message as INotification)._id];
|
||||
|
||||
// Update documents
|
||||
await Notification.update({
|
||||
_id: { $in: ids },
|
||||
is_read: false
|
||||
}, {
|
||||
$set: {
|
||||
is_read: true
|
||||
}
|
||||
}, {
|
||||
multi: true
|
||||
});
|
||||
|
||||
// Calc count of my unread notifications
|
||||
const count = await Notification
|
||||
.count({
|
||||
notifiee_id: userId,
|
||||
is_read: false
|
||||
});
|
||||
|
||||
if (count == 0) {
|
||||
// 全ての(いままで未読だった)通知を(これで)読みましたよというイベントを発行
|
||||
publishUserStream(userId, 'read_all_notifications');
|
||||
}
|
||||
});
|
@ -195,6 +195,11 @@ const endpoints: Endpoint[] = [
|
||||
withCredential: true,
|
||||
kind: 'notification-read'
|
||||
},
|
||||
{
|
||||
name: 'notifications/get_unread_count',
|
||||
withCredential: true,
|
||||
kind: 'notification-read'
|
||||
},
|
||||
{
|
||||
name: 'notifications/delete',
|
||||
withCredential: true,
|
||||
@ -205,11 +210,6 @@ const endpoints: Endpoint[] = [
|
||||
withCredential: true,
|
||||
kind: 'notification-write'
|
||||
},
|
||||
{
|
||||
name: 'notifications/mark_as_read',
|
||||
withCredential: true,
|
||||
kind: 'notification-write'
|
||||
},
|
||||
{
|
||||
name: 'notifications/mark_as_read_all',
|
||||
withCredential: true,
|
||||
|
@ -5,6 +5,7 @@ import $ from 'cafy';
|
||||
import Notification from '../../models/notification';
|
||||
import serialize from '../../serializers/notification';
|
||||
import getFriends from '../../common/get-friends';
|
||||
import read from '../../common/read-notification';
|
||||
|
||||
/**
|
||||
* Get notifications
|
||||
@ -91,17 +92,6 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
|
||||
|
||||
// Mark as read all
|
||||
if (notifications.length > 0 && markAsRead) {
|
||||
const ids = notifications
|
||||
.filter(x => x.is_read == false)
|
||||
.map(x => x._id);
|
||||
|
||||
// Update documents
|
||||
await Notification.update({
|
||||
_id: { $in: ids }
|
||||
}, {
|
||||
$set: { is_read: true }
|
||||
}, {
|
||||
multi: true
|
||||
});
|
||||
read(user._id, notifications);
|
||||
}
|
||||
});
|
||||
|
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');
|
||||
});
|
@ -1,3 +1,8 @@
|
||||
import * as mongo from 'mongodb';
|
||||
import db from '../../db/mongodb';
|
||||
|
||||
export default db.get('notifications') as any; // fuck type definition
|
||||
|
||||
export interface INotification {
|
||||
_id: mongo.ObjectID;
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import * as debug from 'debug';
|
||||
|
||||
import User from '../models/user';
|
||||
import serializePost from '../serializers/post';
|
||||
import readNotification from '../common/read-notification';
|
||||
|
||||
const log = debug('misskey');
|
||||
|
||||
@ -45,6 +46,11 @@ export default function homeStream(request: websocket.request, connection: webso
|
||||
});
|
||||
break;
|
||||
|
||||
case 'read_notification':
|
||||
if (!msg.id) return;
|
||||
readNotification(user._id, msg.id);
|
||||
break;
|
||||
|
||||
case 'capture':
|
||||
if (!msg.id) return;
|
||||
const postId = msg.id;
|
||||
|
Reference in New Issue
Block a user