62 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			62 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import $ from 'cafy'; import ID from '../../../../../misc/cafy-id';
 | |
| import Reaction from '../../../../../models/note-reaction';
 | |
| import Note from '../../../../../models/note';
 | |
| import { ILocalUser } from '../../../../../models/user';
 | |
| 
 | |
| export const meta = {
 | |
| 	desc: {
 | |
| 		ja: '指定した投稿へのリアクションを取り消します。',
 | |
| 		en: 'Unreact to a note.'
 | |
| 	},
 | |
| 
 | |
| 	requireCredential: true,
 | |
| 
 | |
| 	kind: 'reaction-write'
 | |
| };
 | |
| 
 | |
| export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
 | |
| 	// Get 'noteId' parameter
 | |
| 	const [noteId, noteIdErr] = $.type(ID).get(params.noteId);
 | |
| 	if (noteIdErr) return rej('invalid noteId param');
 | |
| 
 | |
| 	// Fetch unreactee
 | |
| 	const note = await Note.findOne({
 | |
| 		_id: noteId
 | |
| 	});
 | |
| 
 | |
| 	if (note === null) {
 | |
| 		return rej('note not found');
 | |
| 	}
 | |
| 
 | |
| 	// if already unreacted
 | |
| 	const exist = await Reaction.findOne({
 | |
| 		noteId: note._id,
 | |
| 		userId: user._id,
 | |
| 		deletedAt: { $exists: false }
 | |
| 	});
 | |
| 
 | |
| 	if (exist === null) {
 | |
| 		return rej('never reacted');
 | |
| 	}
 | |
| 
 | |
| 	// Delete reaction
 | |
| 	await Reaction.update({
 | |
| 		_id: exist._id
 | |
| 	}, {
 | |
| 			$set: {
 | |
| 				deletedAt: new Date()
 | |
| 			}
 | |
| 		});
 | |
| 
 | |
| 	// Send response
 | |
| 	res();
 | |
| 
 | |
| 	const dec: any = {};
 | |
| 	dec[`reactionCounts.${exist.reaction}`] = -1;
 | |
| 
 | |
| 	// Decrement reactions count
 | |
| 	Note.update({ _id: note._id }, {
 | |
| 		$inc: dec
 | |
| 	});
 | |
| });
 | 
