1
0
mirror of https://github.com/misskey-dev/misskey synced 2024-12-21 10:08:24 +09:00
misskey/src/server/api/models/notification.ts

108 lines
2.7 KiB
TypeScript
Raw Normal View History

import * as mongo from 'mongodb';
2018-02-02 08:06:01 +09:00
import deepcopy = require('deepcopy');
2018-03-29 01:20:40 +09:00
import db from '../../../db/mongodb';
2018-02-02 08:06:01 +09:00
import { IUser, pack as packUser } from './user';
import { pack as packPost } from './post';
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;
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している)稿
* repost - (Watchしている)稿Repostされた
* quote - (Watchしている)稿Repostされた
* reaction - (Watchしている)稿
* poll_vote - (Watchしている)稿
*/
type: 'follow' | 'mention' | 'reply' | 'repost' | 'quote' | 'reaction' | 'poll_vote';
/**
*
*/
2018-03-29 14:48:47 +09:00
isRead: Boolean;
}
2018-02-02 08:06:01 +09:00
/**
* Pack a notification for API response
*
* @param {any} notification
* @return {Promise<any>}
*/
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':
// nope
break;
case 'mention':
case 'reply':
case 'repost':
case 'quote':
case 'reaction':
case 'poll_vote':
// Populate post
2018-03-29 14:48:47 +09:00
_notification.post = await packPost(_notification.postId, me);
2018-02-02 08:06:01 +09:00
break;
default:
console.error(`Unknown type: ${_notification.type}`);
break;
}
resolve(_notification);
});