monkeeShark/src/server/api/endpoints/notes/delete.ts
2018-10-22 05:16:27 +09:00

49 lines
1 KiB
TypeScript

import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import Note from '../../../../models/note';
import deleteNote from '../../../../services/note/delete';
import User, { ILocalUser } from '../../../../models/user';
import getParams from '../../get-params';
export const meta = {
stability: 'stable',
desc: {
'ja-JP': '指定した投稿を削除します。',
'en-US': 'Delete a note.'
},
requireCredential: true,
kind: 'note-write',
params: {
noteId: $.type(ID).note({
desc: {
'ja-JP': '対象の投稿のID',
'en-US': 'Target note ID.'
}
})
}
};
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
const [ps, psErr] = getParams(meta, params);
if (psErr) return rej(psErr);
// Fetch note
const note = await Note.findOne({
_id: ps.noteId
});
if (note === null) {
return rej('note not found');
}
if (!user.isAdmin && !note.userId.equals(user._id)) {
return rej('access denied');
}
await deleteNote(await User.findOne({ _id: note.userId }), note);
res();
});