misuzu/assets/misuzu.js/comments/api.js
2025-02-16 00:19:42 +00:00

110 lines
4 KiB
JavaScript

const MszCommentsApi = (() => {
return {
getCategory: async name => {
if(typeof name !== 'string')
throw 'name must be a string';
if(name.trim() === '')
throw 'name may not be empty';
const { status, body } = await $xhr.get(`/comments/categories/${name}`, { type: 'json' });
if(status === 404)
throw 'that category does not exist';
if(status !== 200)
throw 'something went wrong';
return body;
},
updateCategory: async (name, args) => {
if(typeof name !== 'string')
throw 'name must be a string';
if(name.trim() === '')
throw 'name may not be empty';
if(typeof args !== 'object' || args === null)
throw 'args must be a non-null object';
const { status } = await $xhr.patch(`/comments/categories/${name}`, { csrf: true }, args);
return status;
},
getPost: async post => {
if(typeof post !== 'string')
throw 'post id must be a string';
if(post.trim() === '')
throw 'post id may not be empty';
const { status, body } = await $xhr.get(`/comments/posts/${post}`, { type: 'json' });
if(status === 404)
throw 'that post does not exist';
if(status !== 200)
throw 'something went wrong';
return body;
},
getPostReplies: async post => {
if(typeof post !== 'string')
throw 'post id must be a string';
if(post.trim() === '')
throw 'post id may not be empty';
const { status, body } = await $xhr.get(`/comments/posts/${post}/replies`, { type: 'json' });
if(status === 404)
throw 'that post does not exist';
if(status !== 200)
throw 'something went wrong';
return body;
},
createPost: async (post, args) => {
if(typeof post !== 'string')
throw 'post id must be a string';
if(post.trim() === '')
throw 'post id may not be empty';
if(typeof args !== 'object' || args === null)
throw 'args must be a non-null object';
const { status, body } = await $xhr.post('/comments/posts', { csrf: true }, args);
return status;
},
updatePost: async (post, args) => {
//
},
deletePost: async post => {
//
},
createVote: async (post, vote) => {
if(typeof post !== 'string')
throw 'name must be a string';
if(post.trim() === '')
throw 'name may not be empty';
if(typeof vote === 'string')
vote = parseInt(vote);
if(typeof vote !== 'number' || isNaN(vote))
throw 'vote must be a number';
const { status } = await $xhr.post(`/comments/posts/${post}/vote`, { csrf: true }, { vote });
if(status === 400)
throw 'your vote is not acceptable';
if(status === 403)
throw 'you are not allowed to like or dislike comments';
if(status === 404)
throw 'that post does not exist';
if(status !== 200)
throw 'something went wrong';
},
deleteVote: async post => {
if(typeof post !== 'string')
throw 'name must be a string';
if(post.trim() === '')
throw 'name may not be empty';
const { status } = await $xhr.delete(`/comments/posts/${post}/vote`, { csrf: true });
if(status === 403)
throw 'you are not allowed to like or dislike comments';
if(status === 404)
throw 'that post does not exist';
if(status !== 204)
throw 'something went wrong';
},
};
})();