2021-02-11 06:12:41 +01:00
|
|
|
// @flow
|
|
|
|
import { COMMENT_SERVER_API } from 'config';
|
|
|
|
|
2022-01-17 07:34:41 +01:00
|
|
|
// prettier-ignore
|
2021-02-11 06:12:41 +01:00
|
|
|
const Comments = {
|
|
|
|
url: COMMENT_SERVER_API,
|
|
|
|
enabled: Boolean(COMMENT_SERVER_API),
|
2021-07-25 14:52:45 +02:00
|
|
|
isCustomServer: false,
|
|
|
|
|
|
|
|
setServerUrl: (customUrl: ?string) => {
|
|
|
|
Comments.url = customUrl === undefined ? COMMENT_SERVER_API : customUrl;
|
|
|
|
Comments.enabled = Boolean(Comments.url);
|
|
|
|
Comments.isCustomServer = Comments.url !== COMMENT_SERVER_API;
|
|
|
|
},
|
2021-02-11 06:12:41 +01:00
|
|
|
|
|
|
|
moderation_block: (params: ModerationBlockParams) => fetchCommentsApi('moderation.Block', params),
|
2021-03-03 19:50:16 +01:00
|
|
|
moderation_unblock: (params: ModerationBlockParams) => fetchCommentsApi('moderation.UnBlock', params),
|
2021-08-12 09:10:44 +02:00
|
|
|
moderation_block_list: (params: BlockedListArgs) => fetchCommentsApi('moderation.BlockedList', params),
|
2021-05-25 08:17:36 +02:00
|
|
|
moderation_add_delegate: (params: ModerationAddDelegateParams) => fetchCommentsApi('moderation.AddDelegate', params),
|
2022-01-17 07:34:41 +01:00
|
|
|
moderation_remove_delegate: (params: ModerationRemoveDelegateParams) => fetchCommentsApi('moderation.RemoveDelegate', params),
|
|
|
|
moderation_list_delegates: (params: ModerationListDelegatesParams) => fetchCommentsApi('moderation.ListDelegates', params),
|
2021-05-25 08:17:36 +02:00
|
|
|
moderation_am_i: (params: ModerationAmIParams) => fetchCommentsApi('moderation.AmI', params),
|
2021-02-11 06:12:41 +01:00
|
|
|
comment_list: (params: CommentListParams) => fetchCommentsApi('comment.List', params),
|
|
|
|
comment_abandon: (params: CommentAbandonParams) => fetchCommentsApi('comment.Abandon', params),
|
2021-04-23 21:59:48 +02:00
|
|
|
comment_create: (params: CommentCreateParams) => fetchCommentsApi('comment.Create', params),
|
2021-07-15 16:43:28 +02:00
|
|
|
comment_by_id: (params: CommentByIdParams) => fetchCommentsApi('comment.ByID', params),
|
2021-07-15 05:24:37 +02:00
|
|
|
comment_pin: (params: CommentPinParams) => fetchCommentsApi('comment.Pin', params),
|
|
|
|
comment_edit: (params: CommentEditParams) => fetchCommentsApi('comment.Edit', params),
|
|
|
|
reaction_list: (params: ReactionListParams) => fetchCommentsApi('reaction.List', params),
|
|
|
|
reaction_react: (params: ReactionReactParams) => fetchCommentsApi('reaction.React', params),
|
2021-04-20 10:40:53 +02:00
|
|
|
setting_list: (params: SettingsParams) => fetchCommentsApi('setting.List', params),
|
|
|
|
setting_block_word: (params: BlockWordParams) => fetchCommentsApi('setting.BlockWord', params),
|
|
|
|
setting_unblock_word: (params: BlockWordParams) => fetchCommentsApi('setting.UnBlockWord', params),
|
|
|
|
setting_list_blocked_words: (params: SettingsParams) => fetchCommentsApi('setting.ListBlockedWords', params),
|
|
|
|
setting_update: (params: UpdateSettingsParams) => fetchCommentsApi('setting.Update', params),
|
2021-07-29 16:53:36 +02:00
|
|
|
setting_get: (params: SettingsParams) => fetchCommentsApi('setting.Get', params),
|
2021-04-23 21:59:48 +02:00
|
|
|
super_list: (params: SuperListParams) => fetchCommentsApi('comment.SuperChatList', params),
|
2021-02-11 06:12:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
function fetchCommentsApi(method: string, params: {}) {
|
2021-07-25 14:52:45 +02:00
|
|
|
if (!Comments.url) {
|
|
|
|
return Promise.reject(new Error('Commenting server is not set.'));
|
|
|
|
} else if (!Comments.enabled) {
|
|
|
|
return Promise.reject('Comments are not currently enabled.'); // eslint-disable-line
|
2021-02-11 06:12:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const url = `${Comments.url}?m=${method}`;
|
|
|
|
const options = {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
id: 1,
|
|
|
|
method,
|
|
|
|
params,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
return fetch(url, options)
|
2021-03-03 19:50:16 +01:00
|
|
|
.then((res) => res.json())
|
2021-04-22 10:32:57 +02:00
|
|
|
.then((res) => {
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
return res.result;
|
|
|
|
});
|
2021-02-11 06:12:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Comments;
|