2018-02-02 00:06:01 +01:00
|
|
|
import * as mongo from 'mongodb';
|
2018-04-24 11:13:06 +02:00
|
|
|
import $ from 'cafy'; import ID from '../../../../cafy-id';
|
2018-02-02 00:06:01 +01:00
|
|
|
import deepcopy = require('deepcopy');
|
2018-03-29 13:32:18 +02:00
|
|
|
import db from '../db/mongodb';
|
2018-04-07 19:30:37 +02:00
|
|
|
import Reaction from './note-reaction';
|
2018-02-02 00:06:01 +01:00
|
|
|
import { pack as packUser } from './user';
|
2017-03-19 20:24:19 +01:00
|
|
|
|
2018-04-07 19:30:37 +02:00
|
|
|
const NoteReaction = db.get<INoteReaction>('noteReactions');
|
|
|
|
NoteReaction.createIndex(['userId', 'noteId'], { unique: true });
|
|
|
|
export default NoteReaction;
|
2018-02-02 00:06:01 +01:00
|
|
|
|
2018-04-07 19:30:37 +02:00
|
|
|
export interface INoteReaction {
|
2018-02-02 00:06:01 +01:00
|
|
|
_id: mongo.ObjectID;
|
2018-03-29 07:48:47 +02:00
|
|
|
createdAt: Date;
|
2018-04-07 19:30:37 +02:00
|
|
|
noteId: mongo.ObjectID;
|
2018-03-29 07:48:47 +02:00
|
|
|
userId: mongo.ObjectID;
|
2018-02-04 06:52:33 +01:00
|
|
|
reaction: string;
|
2018-02-02 00:06:01 +01:00
|
|
|
}
|
|
|
|
|
2018-04-23 08:27:01 +02:00
|
|
|
export const validateReaction = $().string().or([
|
|
|
|
'like',
|
|
|
|
'love',
|
|
|
|
'laugh',
|
|
|
|
'hmm',
|
|
|
|
'surprise',
|
|
|
|
'congrats',
|
|
|
|
'angry',
|
|
|
|
'confused',
|
|
|
|
'pudding'
|
|
|
|
]);
|
|
|
|
|
2018-04-11 20:46:32 +02:00
|
|
|
/**
|
|
|
|
* NoteReactionを物理削除します
|
|
|
|
*/
|
|
|
|
export async function deleteNoteReaction(noteReaction: string | mongo.ObjectID | INoteReaction) {
|
|
|
|
let n: INoteReaction;
|
|
|
|
|
|
|
|
// Populate
|
|
|
|
if (mongo.ObjectID.prototype.isPrototypeOf(noteReaction)) {
|
|
|
|
n = await NoteReaction.findOne({
|
|
|
|
_id: noteReaction
|
|
|
|
});
|
|
|
|
} else if (typeof noteReaction === 'string') {
|
|
|
|
n = await NoteReaction.findOne({
|
|
|
|
_id: new mongo.ObjectID(noteReaction)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
n = noteReaction as INoteReaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == null) return;
|
|
|
|
|
|
|
|
// このNoteReactionを削除
|
|
|
|
await NoteReaction.remove({
|
|
|
|
_id: n._id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-02 00:06:01 +01: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
|
|
|
|
if (mongo.ObjectID.prototype.isPrototypeOf(reaction)) {
|
|
|
|
_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 07:48:47 +02:00
|
|
|
_reaction.user = await packUser(_reaction.userId, me);
|
2018-02-02 00:06:01 +01:00
|
|
|
|
|
|
|
resolve(_reaction);
|
|
|
|
});
|