osu-api-extended
Version:
Advanced osu! api wrapper for v1 and v2, with extra stuff
157 lines (156 loc) • 6.38 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.forums_topics_actions = void 0;
const request_1 = require("../../utility/request");
const handleErrors_1 = require("../../utility/handleErrors");
const forums_topics_actions = (params, addons) => __awaiter(void 0, void 0, void 0, function* () {
let object = {};
const body = [];
let urls = [];
let methods = [];
const fields = [];
switch (params.type) {
case 'create':
if (params.forum_id == null) {
return (0, handleErrors_1.handleErrors)(`Specify forum id`);
}
;
if (params.title == null) {
return (0, handleErrors_1.handleErrors)(`Specify title`);
}
;
if (params.message == null) {
return (0, handleErrors_1.handleErrors)(`Specify message`);
}
;
urls.push(`https://osu.ppy.sh/api/v2/forums/topics`);
methods.push('POST');
object['forum_id'] = params.forum_id;
object['title'] = params.title;
object['body'] = params.message;
if (params.enable_poll != null)
object['with_poll'] = params.enable_poll;
if (params.enable_poll != true) {
body.push(object);
break;
}
;
object['forum_topic_poll[vote_change]'] = params.poll.allow_vote_change;
object['forum_topic_poll[hide_results]'] = params.poll.hide_results;
object['forum_topic_poll[title]'] = params.poll.title;
object['forum_topic_poll[options]'] = params.poll.options;
object['forum_topic_poll[max_options]'] = params.poll.max_votes_per_user;
object['forum_topic_poll[length_days]'] = params.poll.duration_days;
body.push(object);
break;
case 'reply':
if (params.post_id == null) {
return (0, handleErrors_1.handleErrors)(`Specify post id`);
}
;
if (params.message == null) {
return (0, handleErrors_1.handleErrors)(`Specify message`);
}
;
urls.push(`https://osu.ppy.sh/api/v2/forums/topics/${params.post_id}/reply`);
methods.push('POST');
object['body'] = params.message;
body.push(object);
break;
case 'edit_post':
if (params.post_id == null) {
return (0, handleErrors_1.handleErrors)(`Specify post id`);
}
;
if (params.message == null) {
return (0, handleErrors_1.handleErrors)(`Specify message`);
}
;
urls.push(`https://osu.ppy.sh/api/v2/forums/posts/${params.post_id}`);
methods.push('PATCH');
object['body'] = params.message;
body.push(object);
break;
case 'edit_topic':
if (params.topic_id && (params.title != null && params.title != '')) {
if (params.topic_id == null) {
return (0, handleErrors_1.handleErrors)(`Specify topic id`);
}
;
if (params.title == null) {
return (0, handleErrors_1.handleErrors)(`Specify title`);
}
;
urls.push(`https://osu.ppy.sh/api/v2/forums/topics/${params.topic_id}`);
methods.push('PUT');
object = {
forum_topic: {
'topic_title': params.title,
},
};
body.push(object);
fields.push('topic');
object = {};
}
;
if (params.post_id && (params.message != null && params.message != '')) {
if (params.post_id == null) {
return (0, handleErrors_1.handleErrors)(`Specify post id`);
}
;
if (params.message == null) {
return (0, handleErrors_1.handleErrors)(`Specify message`);
}
;
urls.push(`https://osu.ppy.sh/api/v2/forums/posts/${params.post_id}`);
methods.push('PUT');
object['body'] = params.message;
body.push(object);
fields.push('post');
object = {};
}
;
break;
default:
return (0, handleErrors_1.handleErrors)(`Unsupported type: ${params.type}`);
}
;
const results = [];
for (let i = 0; i < urls.length; i++) {
const url = urls[i];
const data = yield (0, request_1.request)(url, {
method: methods[i],
// params: object,
// params: body[i],
data: JSON.stringify(body[i]),
addons,
});
if (data.error) {
results.push((0, handleErrors_1.handleErrors)(data.error));
continue;
}
;
results.push(data);
}
;
if (params.type == 'edit_topic') {
let result = fields.map((r, index) => ({ [r]: results[index] }));
if (result[0].error)
return result[0].error;
return result[0].topic;
}
;
if (results[0].error)
return results[0].error;
return results[0];
});
exports.forums_topics_actions = forums_topics_actions;
;