2017-10-30 07:12:10 -06:00
|
|
|
import * as mongo from 'mongodb';
|
2019-02-01 03:56:16 -07:00
|
|
|
import * as deepcopy from 'deepcopy';
|
2018-03-29 05:32:18 -06:00
|
|
|
import db from '../db/mongodb';
|
2018-10-15 20:38:09 -06:00
|
|
|
import isObjectId from '../misc/is-objectid';
|
2018-02-01 16:06:01 -07:00
|
|
|
import { IUser, pack as packUser } from './user';
|
2018-04-07 11:30:37 -06:00
|
|
|
import { pack as packNote } from './note';
|
2017-01-16 17:17:52 -07:00
|
|
|
|
2018-02-01 16:06:01 -07:00
|
|
|
const Notification = db.get<INotification>('notifications');
|
2018-08-01 18:29:46 -06:00
|
|
|
Notification.createIndex('notifieeId');
|
2018-02-01 16:06:01 -07:00
|
|
|
export default Notification;
|
2017-10-30 07:12:10 -06:00
|
|
|
|
|
|
|
export interface INotification {
|
|
|
|
_id: mongo.ObjectID;
|
2018-03-28 23:48:47 -06:00
|
|
|
createdAt: Date;
|
2017-11-10 08:27:02 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 通知の受信者
|
|
|
|
*/
|
|
|
|
notifiee?: IUser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 通知の受信者
|
|
|
|
*/
|
2018-03-28 23:48:47 -06:00
|
|
|
notifieeId: mongo.ObjectID;
|
2017-11-10 08:27:02 -07:00
|
|
|
|
|
|
|
/**
|
2017-11-10 10:25:24 -07:00
|
|
|
* イニシエータ(initiator)、Origin。通知を行う原因となったユーザー
|
2017-11-10 08:27:02 -07:00
|
|
|
*/
|
|
|
|
notifier?: IUser;
|
|
|
|
|
|
|
|
/**
|
2017-11-10 10:25:24 -07:00
|
|
|
* イニシエータ(initiator)、Origin。通知を行う原因となったユーザー
|
2017-11-10 08:27:02 -07:00
|
|
|
*/
|
2018-03-28 23:48:47 -06:00
|
|
|
notifierId: mongo.ObjectID;
|
2017-11-10 08:27:02 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 通知の種類。
|
|
|
|
* follow - フォローされた
|
|
|
|
* mention - 投稿で自分が言及された
|
|
|
|
* reply - (自分または自分がWatchしている)投稿が返信された
|
2018-04-07 11:30:37 -06:00
|
|
|
* renote - (自分または自分がWatchしている)投稿がRenoteされた
|
|
|
|
* quote - (自分または自分がWatchしている)投稿が引用Renoteされた
|
2017-11-10 08:27:02 -07:00
|
|
|
* reaction - (自分または自分がWatchしている)投稿にリアクションされた
|
|
|
|
* poll_vote - (自分または自分がWatchしている)投稿の投票に投票された
|
|
|
|
*/
|
2018-04-07 11:30:37 -06:00
|
|
|
type: 'follow' | 'mention' | 'reply' | 'renote' | 'quote' | 'reaction' | 'poll_vote';
|
2017-11-10 08:27:02 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 通知が読まれたかどうか
|
|
|
|
*/
|
2018-12-02 04:10:56 -07:00
|
|
|
isRead: boolean;
|
2017-10-30 07:12:10 -06:00
|
|
|
}
|
2018-02-01 16:06:01 -07:00
|
|
|
|
2018-10-30 20:22:49 -06:00
|
|
|
export const packMany = (
|
2018-10-03 22:53:48 -06:00
|
|
|
notifications: any[]
|
|
|
|
) => {
|
2018-10-30 20:22:49 -06:00
|
|
|
return Promise.all(notifications.map(n => pack(n)));
|
2018-10-03 22:53:48 -06:00
|
|
|
};
|
|
|
|
|
2018-02-01 16:06:01 -07: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
|
2018-10-15 20:38:09 -06:00
|
|
|
if (isObjectId(notification)) {
|
2018-02-01 16:06:01 -07:00
|
|
|
_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-28 23:48:47 -06:00
|
|
|
// Rename notifierId to userId
|
|
|
|
_notification.userId = _notification.notifierId;
|
|
|
|
delete _notification.notifierId;
|
2018-02-01 16:06:01 -07:00
|
|
|
|
2018-03-28 23:48:47 -06:00
|
|
|
const me = _notification.notifieeId;
|
|
|
|
delete _notification.notifieeId;
|
2018-02-01 16:06:01 -07:00
|
|
|
|
|
|
|
// Populate notifier
|
2018-03-28 23:48:47 -06:00
|
|
|
_notification.user = await packUser(_notification.userId, me);
|
2018-02-01 16:06:01 -07:00
|
|
|
|
|
|
|
switch (_notification.type) {
|
|
|
|
case 'follow':
|
2018-06-02 01:13:32 -06:00
|
|
|
case 'receiveFollowRequest':
|
2018-02-01 16:06:01 -07:00
|
|
|
// nope
|
|
|
|
break;
|
|
|
|
case 'mention':
|
|
|
|
case 'reply':
|
2018-04-07 11:30:37 -06:00
|
|
|
case 'renote':
|
2018-02-01 16:06:01 -07:00
|
|
|
case 'quote':
|
|
|
|
case 'reaction':
|
|
|
|
case 'poll_vote':
|
2018-04-07 11:30:37 -06:00
|
|
|
// Populate note
|
|
|
|
_notification.note = await packNote(_notification.noteId, me);
|
2018-10-03 22:53:48 -06:00
|
|
|
|
|
|
|
// (データベースの不具合などで)投稿が見つからなかったら
|
|
|
|
if (_notification.note == null) {
|
2018-10-10 11:19:21 -06:00
|
|
|
console.warn(`[DAMAGED DB] (missing) pkg: notification -> note :: ${_notification.id} (note ${_notification.noteId})`);
|
2018-10-03 22:53:48 -06:00
|
|
|
return resolve(null);
|
|
|
|
}
|
2018-02-01 16:06:01 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.error(`Unknown type: ${_notification.type}`);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(_notification);
|
|
|
|
});
|