Skip to content

Commit

Permalink
API spec, error message for assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Sep 13, 2024
1 parent b811d9a commit de7264e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 60 deletions.
26 changes: 10 additions & 16 deletions batch/apispec.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ static private function parse_parameters($j) {
return $known;
}

/** @param string $fn
* @return mixed */
/** @param string $fn */
private function expand_paths($fn) {
foreach (["GET", "POST"] as $method) {
if (!($j = $this->conf->api($fn, null, $method))) {
Expand All @@ -124,19 +123,16 @@ private function expand_paths($fn) {
/** @param string $path
* @param 'GET'|'POST' $method
* @param array<string,int> $known
* @param object $j
* @param array &$x
* @return object */
* @param object $j */
private function expand_path_method($path, $method, $known, $j) {
$this->paths[$path] = $this->paths[$path] ?? [];
$this->paths[$path][strtolower($method)] = $x = (object) [];
$this->expand_request($x, $known, $j);
$this->expand_response($x, $j);
return $x;
}

/** @param string $name
* @return array */
* @return object */
private function resolve_common_schema($name) {
if (!isset($this->schemas[$name])) {
if ($name === "pid") {
Expand Down Expand Up @@ -168,11 +164,11 @@ private function resolve_common_schema($name) {
assert(false);
}
}
return ["\$ref" => "#/components/schemas/{$name}"];
return (object) ["\$ref" => "#/components/schemas/{$name}"];
}

/** @param string $name
* @return array */
* @return object */
private function resolve_common_param($name) {
if (!isset($this->parameters[$name])) {
if ($name === "p") {
Expand All @@ -193,13 +189,12 @@ private function resolve_common_param($name) {
assert(false);
}
}
return ["\$ref" => "#/components/parameters/{$name}"];
return (object) ["\$ref" => "#/components/parameters/{$name}"];
}

/** @param object $x
* @param array<string,int> $known
* @param object $j
* @return object */
* @param object $j */
private function expand_request($x, $known, $j) {
$params = $body_properties = $body_required = [];
$has_file = false;
Expand All @@ -211,14 +206,14 @@ private function expand_request($x, $known, $j) {
} else if ($name === "redirect" && $f === 0) {
$params[] = $this->resolve_common_param("redirect");
} else if (($f & (self::F_BODY | self::F_FILE)) === 0) {
$params[] = [
$params[] = (object) [
"name" => $name,
"in" => "query",
"required" => ($f & self::F_REQUIRED) !== 0,
"schema" => (object) []
];
} else {
$body_properties[$name] = [
$body_properties[$name] = (object) [
"schema" => (object) []
];
if (($f & self::F_REQUIRED) !== 0) {
Expand Down Expand Up @@ -253,8 +248,7 @@ private function expand_request($x, $known, $j) {
}

/** @param object $x
* @param object $j
* @return object */
* @param object $j */
private function expand_response($x, $j) {
$body_properties = $body_required = [];
$response = $j->response ?? [];
Expand Down
20 changes: 12 additions & 8 deletions etc/apifunctions.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
{
"name": "account", "get": true,
"function": "User_API::account",
"parameters": "email"
"parameters": "email",
"response": "email disabled placeholder"
},
{
"name": "account", "post": true,
"function": "User_API::account",
"parameters": "email ?disable ?enable ?sendinfo"
"parameters": "email ?disable ?enable ?sendinfo",
"response": "email disabled placeholder"
},
{
"name": "alltags", "get": true,
Expand All @@ -28,7 +30,8 @@
{
"name": "claimreview", "paper": true, "post": true, "redirect": true,
"function": "RequestReview_API::claimreview",
"parameters": "r email"
"parameters": "r email",
"response": "action review_site_relative"
},
{
"name": "clickthrough", "post": true,
Expand All @@ -39,20 +42,21 @@
"name": "comment", "paper": true, "get": true,
"function": "Comment_API::run",
"parameters": "c",
"response": "comment",
"response": "?comment",
"response_deprecated": "cmt"
},
{
"name": "comment", "paper": true, "post": true,
"function": "Comment_API::run",
"parameters": "c ?override ?=text ?=response ?=ready ?=topic ?=draft ?=blind ?=tags ?=visibility ?=:attachment ?=by_author ?=review_token ?delete",
"response": "comment",
"response": "?comment ?conflict",
"response_deprecated": "cmt"
},
{
"name": "declinereview", "post": true, "redirect": true,
"function": "RequestReview_API::declinereview",
"parameters": "p r ?=reason"
"parameters": "p r ?=reason",
"response": "action ?reason review_site_relative"
},
{
"name": "decision", "paper": true, "get": true,
Expand Down Expand Up @@ -108,7 +112,7 @@
{
"name": "jserror", "get": true,
"function": "Error_API::jserror",
"parameters": "=error"
"parameters": "=error ?=url ?=lineno ?=colno ?=stack"
},
{
"name": "lead", "paper": true, "get": true,
Expand Down Expand Up @@ -276,7 +280,7 @@
{
"name": "tags", "post": true,
"function": "Tags_API::run",
"parameters": "?p ?forceShow ?=tags ?=addtags ?=deltags ?=tagassignment ?=search",
"parameters": "?p ?=tags ?=addtags ?=deltags ?=tagassignment ?=search",
"response": "?pid ?tags ?tags_edit_text ?tags_view_html ?tag_decoration_html ?color_classes ?p"
},
{
Expand Down
8 changes: 4 additions & 4 deletions scripts/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -12991,8 +12991,9 @@ handle_ui.on("js-delete-user", function () {
handle_ui.on("js-disable-user", function () {
var disabled = hasClass(this, "btn-success"), self = this;
self.disabled = true;
$.post(hoturl("=api/account", {u: this.getAttribute("data-user") || this.form.getAttribute("data-user")}),
disabled ? {enable: 1} : {disable: 1},
const param = {email: this.getAttribute("data-user") || this.form.getAttribute("data-user")};
param[disabled ? "enable" : "disable"] = 1;
$.post(hoturl("=api/account", param), {},
function (data) {
self.disabled = false;
if (data.ok) {
Expand Down Expand Up @@ -13023,8 +13024,7 @@ handle_ui.on("js-disable-user", function () {
handle_ui.on("js-send-user-accountinfo", function () {
var self = this;
self.disabled = true;
$.post(hoturl("=api/account", {u: this.getAttribute("data-user") || this.form.getAttribute("data-user")}),
{sendinfo: 1},
$.post(hoturl("=api/account", {email: this.getAttribute("data-user") || this.form.getAttribute("data-user"), sendinfo: 1}), {},
function (data) {
minifeedback(self, data);
});
Expand Down
46 changes: 21 additions & 25 deletions src/api/api_user.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,13 @@ static function account_disable(Contact $user, Contact $viewer, $disabled) {
return JsonResult::make_permission_error();
} else if ($viewer->contactId === $user->contactId) {
return JsonResult::make_error(400, "<0>You cannot disable your own account");
} else {
$ustatus = new UserStatus($viewer);
$ustatus->set_user($user);
if ($ustatus->save_user((object) ["disabled" => $disabled], $user)) {
return new JsonResult([
"ok" => true,
"u" => $user->email,
"disabled" => $user->is_disabled(),
"placeholder" => $user->is_placeholder()
]);
} else {
return new JsonResult(["ok" => false, "u" => $user->email]);
}
}
$ustatus = new UserStatus($viewer);
$ustatus->set_user($user);
if (!$ustatus->save_user((object) ["disabled" => $disabled], $user)) {
return new JsonResult(["ok" => false]);
}
return new JsonResult(["ok" => true]);
}

/** @return JsonResult */
Expand All @@ -151,34 +144,37 @@ static function account_sendinfo(Contact $user, Contact $viewer) {
}
$user->activate_placeholder(false);
$prep = $user->prepare_mail("@accountinfo");
if ($prep->send()) {
$jr = new JsonResult(200);
} else {
$jr = new JsonResult(400);
$jr->content["message_list"] = $prep->message_list();
if (!$prep->send()) {
return new JsonResult(["ok" => false, "message_list" => $prep->message_list()]);
}
$jr->content["u"] = $user->email;
return $jr;
return new JsonResult(["ok" => true]);
}

/** @return JsonResult */
static function account(Contact $viewer, Qrequest $qreq) {
if (!isset($qreq->u) || $qreq->u === "me" || strcasecmp($qreq->u, $viewer->email) === 0) {
$u = $qreq->email ?? $qreq->u;
if ($u === null || $u === "me" || strcasecmp($u, $viewer->email) === 0) {
$user = $viewer;
} else if ($viewer->isPC) {
$user = $viewer->conf->user_by_email($qreq->u);
$user = $viewer->conf->user_by_email($u);
} else {
return JsonResult::make_permission_error();
}
if (!$user) {
return JsonResult::make_error(404, "<0>User not found");
}
if ($qreq->valid_post() && ($qreq->disable || $qreq->enable)) {
return self::account_disable($user, $viewer, !!$qreq->disable);
$jr = self::account_disable($user, $viewer, !!$qreq->disable);
} else if ($qreq->valid_post() && $qreq->sendinfo) {
return self::account_sendinfo($user, $viewer);
$jr = self::account_sendinfo($user, $viewer);
} else {
return new JsonResult(["ok" => true]);
$jr = new JsonResult(["ok" => true]);
}
if ($jr->content["ok"]) {
$jr->content["email"] = $user->email;
$jr->content["disabled"] = $user->is_disabled();
$jr->content["placeholder"] = $user->is_placeholder();
}
return $jr;
}
}
8 changes: 4 additions & 4 deletions src/assignmentset.php
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ function json_result() {
}

private static function req_user_text($req) {
return Text::name($req["firstName"], $req["lastName"], $req["email"], NAME_E);
return Text::name($req["firstName"] ?? "", $req["lastName"] ?? "", $req["email"] ?? "", NAME_E);
}

private static function apply_user_parts($req, $a) {
Expand Down Expand Up @@ -1368,12 +1368,12 @@ private function lookup_users($req, AssignmentParser $aparser) {
if ($cset) {
$text = "";
if ($first && $last) {
$text = "$last, $first";
$text = "{$last}, {$first}";
} else if ($first || $last) {
$text = "$last$first";
$text = $first . $last;
}
if ($email) {
$text .= " <$email>";
$text = $text ? "{$text} <{$email}>" : "<{$email}>";
}
$ret = ContactSearch::make_cset($text, $this->astate->user, $cset);
if (count($ret->user_ids()) === 1) {
Expand Down
3 changes: 0 additions & 3 deletions src/papertable.php
Original file line number Diff line number Diff line change
Expand Up @@ -1653,9 +1653,6 @@ function papstripOutcomeSelector() {
echo $this->papt("decision", Ht::label("Decision", $id),
["type" => "ps", "fold" => "decision"]),
'<form class="ui-submit uin fx">';
if (isset($this->qreq->forceShow)) {
echo Ht::hidden("forceShow", $this->qreq->forceShow ? 1 : 0);
}
$opts = [];
foreach ($this->conf->decision_set() as $dec) {
$opts[$dec->id] = $dec->name_as(5);
Expand Down

0 comments on commit de7264e

Please sign in to comment.