2019-02-05 11:48:08 +09:00
|
|
|
import $ from 'cafy';
|
2021-08-19 18:33:41 +09:00
|
|
|
import { ID } from '@/misc/cafy-id.js';
|
|
|
|
|
import define from '../../../define.js';
|
|
|
|
|
import { ApiError } from '../../../error.js';
|
|
|
|
|
import { getNote } from '../../../common/getters.js';
|
|
|
|
|
import { NoteFavorites } from '@/models/index.js';
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-07-17 04:36:44 +09:00
|
|
|
export const meta = {
|
2019-05-17 00:19:23 +09:00
|
|
|
tags: ['notes', 'favorites'],
|
2019-02-23 11:20:58 +09:00
|
|
|
|
2020-02-15 21:33:32 +09:00
|
|
|
requireCredential: true as const,
|
2018-07-17 04:36:44 +09:00
|
|
|
|
2019-04-15 12:10:40 +09:00
|
|
|
kind: 'write:favorites',
|
2018-10-22 05:16:27 +09:00
|
|
|
|
|
|
|
|
params: {
|
2018-11-02 03:32:24 +09:00
|
|
|
noteId: {
|
|
|
|
|
validator: $.type(ID),
|
|
|
|
|
}
|
2019-02-22 11:46:58 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
|
noSuchNote: {
|
|
|
|
|
message: 'No such note.',
|
|
|
|
|
code: 'NO_SUCH_NOTE',
|
|
|
|
|
id: '80848a2c-398f-4343-baa9-df1d57696c56'
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
notFavorited: {
|
|
|
|
|
message: 'You have not marked that note a favorite.',
|
|
|
|
|
code: 'NOT_FAVORITED',
|
|
|
|
|
id: 'b625fc69-635e-45e9-86f4-dbefbef35af5'
|
|
|
|
|
},
|
2018-10-22 05:16:27 +09:00
|
|
|
}
|
2018-07-17 04:36:44 +09:00
|
|
|
};
|
|
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
export default define(meta, async (ps, user) => {
|
2017-03-04 04:28:38 +09:00
|
|
|
// Get favoritee
|
2019-02-22 14:53:03 +09:00
|
|
|
const note = await getNote(ps.noteId).catch(e => {
|
|
|
|
|
if (e.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchNote);
|
|
|
|
|
throw e;
|
2017-03-04 04:28:38 +09:00
|
|
|
});
|
2017-02-27 16:50:36 +09:00
|
|
|
|
2017-03-04 04:28:38 +09:00
|
|
|
// if already favorited
|
2019-04-07 21:50:36 +09:00
|
|
|
const exist = await NoteFavorites.findOne({
|
|
|
|
|
noteId: note.id,
|
|
|
|
|
userId: user.id
|
2017-03-04 04:28:38 +09:00
|
|
|
});
|
2017-02-27 16:50:36 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
if (exist == null) {
|
2019-02-22 11:46:58 +09:00
|
|
|
throw new ApiError(meta.errors.notFavorited);
|
2017-03-04 04:28:38 +09:00
|
|
|
}
|
2017-02-27 16:50:36 +09:00
|
|
|
|
2017-03-04 04:28:38 +09:00
|
|
|
// Delete favorite
|
2019-04-07 21:50:36 +09:00
|
|
|
await NoteFavorites.delete(exist.id);
|
2019-02-22 11:46:58 +09:00
|
|
|
});
|