2018-02-02 08:06:01 +09:00
|
|
|
import * as mongo from 'mongodb';
|
2018-04-27 19:12:15 +09:00
|
|
|
import $ from 'cafy';
|
2019-02-01 19:56:16 +09:00
|
|
|
import * as deepcopy from 'deepcopy';
|
2018-03-29 20:32:18 +09:00
|
|
|
import db from '../db/mongodb';
|
2018-10-16 11:38:09 +09:00
|
|
|
import isObjectId from '../misc/is-objectid';
|
2018-04-08 02:30:37 +09:00
|
|
|
import Reaction from './note-reaction';
|
2018-02-02 08:06:01 +09:00
|
|
|
import { pack as packUser } from './user';
|
2017-03-20 04:24:19 +09:00
|
|
|
|
2018-04-08 02:30:37 +09:00
|
|
|
const NoteReaction = db.get<INoteReaction>('noteReactions');
|
2018-10-28 21:41:39 +09:00
|
|
|
NoteReaction.createIndex('noteId');
|
|
|
|
NoteReaction.createIndex('userId');
|
2018-04-08 02:30:37 +09:00
|
|
|
NoteReaction.createIndex(['userId', 'noteId'], { unique: true });
|
|
|
|
export default NoteReaction;
|
2018-02-02 08:06:01 +09:00
|
|
|
|
2018-04-08 02:30:37 +09:00
|
|
|
export interface INoteReaction {
|
2018-02-02 08:06:01 +09:00
|
|
|
_id: mongo.ObjectID;
|
2018-03-29 14:48:47 +09:00
|
|
|
createdAt: Date;
|
2018-04-08 02:30:37 +09:00
|
|
|
noteId: mongo.ObjectID;
|
2018-03-29 14:48:47 +09:00
|
|
|
userId: mongo.ObjectID;
|
2018-02-04 14:52:33 +09:00
|
|
|
reaction: string;
|
2018-02-02 08:06:01 +09:00
|
|
|
}
|
|
|
|
|
2018-05-02 18:06:16 +09:00
|
|
|
export const validateReaction = $.str.or([
|
2018-04-23 15:27:01 +09:00
|
|
|
'like',
|
|
|
|
'love',
|
|
|
|
'laugh',
|
|
|
|
'hmm',
|
|
|
|
'surprise',
|
|
|
|
'congrats',
|
|
|
|
'angry',
|
|
|
|
'confused',
|
2018-08-11 21:08:34 +09:00
|
|
|
'rip',
|
2018-04-23 15:27:01 +09:00
|
|
|
'pudding'
|
|
|
|
]);
|
|
|
|
|
2018-02-02 08:06:01 +09:00
|
|
|
/**
|
|
|
|
* Pack a reaction for API response
|
|
|
|
*/
|
|
|
|
export const pack = (
|
|
|
|
reaction: any,
|
|
|
|
me?: any
|
|
|
|
) => new Promise<any>(async (resolve, reject) => {
|
|
|
|
let _reaction: any;
|
|
|
|
|
|
|
|
// Populate the reaction if 'reaction' is ID
|
2018-10-16 11:38:09 +09:00
|
|
|
if (isObjectId(reaction)) {
|
2018-02-02 08:06:01 +09:00
|
|
|
_reaction = await Reaction.findOne({
|
|
|
|
_id: reaction
|
|
|
|
});
|
|
|
|
} else if (typeof reaction === 'string') {
|
|
|
|
_reaction = await Reaction.findOne({
|
|
|
|
_id: new mongo.ObjectID(reaction)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_reaction = deepcopy(reaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename _id to id
|
|
|
|
_reaction.id = _reaction._id;
|
|
|
|
delete _reaction._id;
|
|
|
|
|
|
|
|
// Populate user
|
2018-03-29 14:48:47 +09:00
|
|
|
_reaction.user = await packUser(_reaction.userId, me);
|
2018-02-02 08:06:01 +09:00
|
|
|
|
|
|
|
resolve(_reaction);
|
|
|
|
});
|