misuzu/assets/misuzu.js/comments/api.js

154 lines
5.1 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 => {
//
},
restorePost: 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 } = await $xhr.post(`/comments/posts/${post}/restore`, { csrf: true });
if(status === 400)
throw 'that post is not deleted';
if(status === 403)
throw 'you are not allowed to restore posts';
if(status === 404)
throw 'that post does not exist';
if(status !== 200)
throw 'something went wrong';
},
createVote: async (post, vote) => {
if(typeof post !== 'string')
throw 'post id must be a string';
if(post.trim() === '')
throw 'post id 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, body } = await $xhr.post(
`/comments/posts/${post}/vote`,
{ csrf: true, type: 'json' },
{ 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';
return body;
},
deleteVote: 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.delete(
`/comments/posts/${post}/vote`,
{ csrf: true, type: 'json' }
);
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';
return body;
},
};
})();