wip
This commit is contained in:
parent
1dab37bdae
commit
9a282e37be
23 changed files with 920 additions and 973 deletions
|
|
@ -1,8 +1,11 @@
|
|||
import * as mongo from 'mongodb';
|
||||
import deepcopy = require('deepcopy');
|
||||
import db from '../../db/mongodb';
|
||||
import { IUser } from './user';
|
||||
import { IUser, pack as packUser } from './user';
|
||||
import { pack as packPost } from './post';
|
||||
|
||||
export default db.get('notifications') as any; // fuck type definition
|
||||
const Notification = db.get<INotification>('notifications');
|
||||
export default Notification;
|
||||
|
||||
export interface INotification {
|
||||
_id: mongo.ObjectID;
|
||||
|
|
@ -45,3 +48,60 @@ export interface INotification {
|
|||
*/
|
||||
is_read: 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 notifier_id to user_id
|
||||
_notification.user_id = _notification.notifier_id;
|
||||
delete _notification.notifier_id;
|
||||
|
||||
const me = _notification.notifiee_id;
|
||||
delete _notification.notifiee_id;
|
||||
|
||||
// Populate notifier
|
||||
_notification.user = await packUser(_notification.user_id, 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.post_id, me);
|
||||
break;
|
||||
default:
|
||||
console.error(`Unknown type: ${_notification.type}`);
|
||||
break;
|
||||
}
|
||||
|
||||
resolve(_notification);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue