整理した

This commit is contained in:
syuilo 2018-03-29 20:32:18 +09:00
parent 8a279a4656
commit cf33e483f7
552 changed files with 360 additions and 1311 deletions

View file

@ -1,107 +0,0 @@
import * as mongo from 'mongodb';
import deepcopy = require('deepcopy');
import db from '../../../db/mongodb';
import { IUser, pack as packUser } from './user';
import { pack as packPost } from './post';
const Notification = db.get<INotification>('notifications');
export default Notification;
export interface INotification {
_id: mongo.ObjectID;
createdAt: Date;
/**
*
*/
notifiee?: IUser;
/**
*
*/
notifieeId: mongo.ObjectID;
/**
* (initiator)Origin
*/
notifier?: IUser;
/**
* (initiator)Origin
*/
notifierId: mongo.ObjectID;
/**
*
* follow -
* mention - 稿
* reply - (Watchしている)稿
* repost - (Watchしている)稿Repostされた
* quote - (Watchしている)稿Repostされた
* reaction - (Watchしている)稿
* poll_vote - (Watchしている)稿
*/
type: 'follow' | 'mention' | 'reply' | 'repost' | 'quote' | 'reaction' | 'poll_vote';
/**
*
*/
isRead: Boolean;
}
/**
* 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;
// Rename notifierId to userId
_notification.userId = _notification.notifierId;
delete _notification.notifierId;
const me = _notification.notifieeId;
delete _notification.notifieeId;
// Populate notifier
_notification.user = await packUser(_notification.userId, me);
switch (_notification.type) {
case 'follow':
// nope
break;
case 'mention':
case 'reply':
case 'repost':
case 'quote':
case 'reaction':
case 'poll_vote':
// Populate post
_notification.post = await packPost(_notification.postId, me);
break;
default:
console.error(`Unknown type: ${_notification.type}`);
break;
}
resolve(_notification);
});