2017-10-30 22:12:10 +09:00
|
|
|
import * as mongo from 'mongodb';
|
2018-05-26 16:04:47 +09:00
|
|
|
import * as deepcopy from 'deepcopy';
|
2018-03-29 20:32:18 +09:00
|
|
|
import db from '../db/mongodb';
|
2018-02-02 08:06:01 +09:00
|
|
|
import { IUser, pack as packUser } from './user';
|
2018-04-08 02:30:37 +09:00
|
|
|
import { pack as packNote } from './note';
|
2017-01-17 09:17:52 +09:00
|
|
|
|
2018-02-02 08:06:01 +09:00
|
|
|
const Notification = db.get<INotification>('notifications');
|
|
|
|
export default Notification;
|
2017-10-30 22:12:10 +09:00
|
|
|
|
|
|
|
export interface INotification {
|
|
|
|
_id: mongo.ObjectID;
|
2018-03-29 14:48:47 +09:00
|
|
|
createdAt: Date;
|
2017-11-11 00:27:02 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 通知の受信者
|
|
|
|
*/
|
|
|
|
notifiee?: IUser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 通知の受信者
|
|
|
|
*/
|
2018-03-29 14:48:47 +09:00
|
|
|
notifieeId: mongo.ObjectID;
|
2017-11-11 00:27:02 +09:00
|
|
|
|
|
|
|
/**
|
2017-11-11 02:25:24 +09:00
|
|
|
* イニシエータ(initiator)、Origin。通知を行う原因となったユーザー
|
2017-11-11 00:27:02 +09:00
|
|
|
*/
|
|
|
|
notifier?: IUser;
|
|
|
|
|
|
|
|
/**
|
2017-11-11 02:25:24 +09:00
|
|
|
* イニシエータ(initiator)、Origin。通知を行う原因となったユーザー
|
2017-11-11 00:27:02 +09:00
|
|
|
*/
|
2018-03-29 14:48:47 +09:00
|
|
|
notifierId: mongo.ObjectID;
|
2017-11-11 00:27:02 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 通知の種類。
|
|
|
|
* follow - フォローされた
|
|
|
|
* mention - 投稿で自分が言及された
|
|
|
|
* reply - (自分または自分がWatchしている)投稿が返信された
|
2018-04-08 02:30:37 +09:00
|
|
|
* renote - (自分または自分がWatchしている)投稿がRenoteされた
|
|
|
|
* quote - (自分または自分がWatchしている)投稿が引用Renoteされた
|
2017-11-11 00:27:02 +09:00
|
|
|
* reaction - (自分または自分がWatchしている)投稿にリアクションされた
|
|
|
|
* poll_vote - (自分または自分がWatchしている)投稿の投票に投票された
|
|
|
|
*/
|
2018-04-08 02:30:37 +09:00
|
|
|
type: 'follow' | 'mention' | 'reply' | 'renote' | 'quote' | 'reaction' | 'poll_vote';
|
2017-11-11 00:27:02 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 通知が読まれたかどうか
|
|
|
|
*/
|
2018-03-29 14:48:47 +09:00
|
|
|
isRead: Boolean;
|
2017-10-30 22:12:10 +09:00
|
|
|
}
|
2018-02-02 08:06:01 +09:00
|
|
|
|
2018-04-15 06:34:55 +09:00
|
|
|
/**
|
|
|
|
* Notificationを物理削除します
|
|
|
|
*/
|
|
|
|
export async function deleteNotification(notification: string | mongo.ObjectID | INotification) {
|
|
|
|
let n: INotification;
|
|
|
|
|
|
|
|
// Populate
|
|
|
|
if (mongo.ObjectID.prototype.isPrototypeOf(notification)) {
|
|
|
|
n = await Notification.findOne({
|
|
|
|
_id: notification
|
|
|
|
});
|
|
|
|
} else if (typeof notification === 'string') {
|
|
|
|
n = await Notification.findOne({
|
|
|
|
_id: new mongo.ObjectID(notification)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
n = notification as INotification;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == null) return;
|
|
|
|
|
|
|
|
// このNotificationを削除
|
|
|
|
await Notification.remove({
|
|
|
|
_id: n._id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-02 08:06:01 +09:00
|
|
|
/**
|
|
|
|
* Pack a notification for API response
|
|
|
|
*/
|
|
|
|
export const pack = (notification: any) => new Promise<any>(async (resolve, reject) => {
|
|
|
|
let _notification: any;
|
|
|
|
|
|
|
|
// Populate the notification if 'notification' is ID
|
|
|
|
if (mongo.ObjectID.prototype.isPrototypeOf(notification)) {
|
|
|
|
_notification = await Notification.findOne({
|
|
|
|
_id: notification
|
|
|
|
});
|
|
|
|
} else if (typeof notification === 'string') {
|
|
|
|
_notification = await Notification.findOne({
|
|
|
|
_id: new mongo.ObjectID(notification)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_notification = deepcopy(notification);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename _id to id
|
|
|
|
_notification.id = _notification._id;
|
|
|
|
delete _notification._id;
|
|
|
|
|
2018-03-29 14:48:47 +09:00
|
|
|
// Rename notifierId to userId
|
|
|
|
_notification.userId = _notification.notifierId;
|
|
|
|
delete _notification.notifierId;
|
2018-02-02 08:06:01 +09:00
|
|
|
|
2018-03-29 14:48:47 +09:00
|
|
|
const me = _notification.notifieeId;
|
|
|
|
delete _notification.notifieeId;
|
2018-02-02 08:06:01 +09:00
|
|
|
|
|
|
|
// Populate notifier
|
2018-03-29 14:48:47 +09:00
|
|
|
_notification.user = await packUser(_notification.userId, me);
|
2018-02-02 08:06:01 +09:00
|
|
|
|
|
|
|
switch (_notification.type) {
|
|
|
|
case 'follow':
|
2018-06-02 13:36:29 +09:00
|
|
|
case 'reciveFollowRequest':
|
2018-02-02 08:06:01 +09:00
|
|
|
// nope
|
|
|
|
break;
|
|
|
|
case 'mention':
|
|
|
|
case 'reply':
|
2018-04-08 02:30:37 +09:00
|
|
|
case 'renote':
|
2018-02-02 08:06:01 +09:00
|
|
|
case 'quote':
|
|
|
|
case 'reaction':
|
|
|
|
case 'poll_vote':
|
2018-04-08 02:30:37 +09:00
|
|
|
// Populate note
|
|
|
|
_notification.note = await packNote(_notification.noteId, me);
|
2018-02-02 08:06:01 +09:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.error(`Unknown type: ${_notification.type}`);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(_notification);
|
|
|
|
});
|