From 85fb5cd6c175fe0c15b57e743d538f71b571f574 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Mon, 23 Dec 2019 15:39:28 +0000 Subject: [PATCH] Finish implementing the watchlist backend. Now to test it! --- modules/feature-watchlist.php | 42 +++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/modules/feature-watchlist.php b/modules/feature-watchlist.php index 3b433d1..287b1f8 100644 --- a/modules/feature-watchlist.php +++ b/modules/feature-watchlist.php @@ -15,6 +15,7 @@ register_module([ * @apiParam {string} format The format to return the watchlist in. * * @apiError WatchlistsDisabled Watchlists are disabled because the watchlists_enable setting is set to false. + * @apiError NotLoggedIn You aren't logged in, so you can't edit your watchlist (only logged in users have a watchlist). * @apiError NoEmailAddress The currently logged in user doesn't have an email address specified in their account. */ @@ -96,16 +97,21 @@ register_module([ /** - * @api {get} ?action=watchlist-edit&do={do_verb} Edit your watchlist + * @api {get} ?action=watchlist-edit&do={do_verb}[&page={pagename}][&returnto=url] Edit your watchlist * @apiName WatchlistEdit * @apiGroup Settings * @apiPermission User * * TODO: Finish filling this out - * @apiParam {string} string The string to hash. - * @apiParam {boolean} raw Whether to return the hashed password as a raw string instead of as part of an HTML page. + * @apiParam {string} pagename The name of the page to operate on. + * @apiParam {string} do The thing to do. Supported verbs: add, remove, clear. The first 2 require the page GET parameter to be specified, but the clear verb doesn't (as it clears the entire list). + * @apiParam {string} returnto Optional. Specifies a URL to redirect to (with the http status code 302) upon success. * - * @apiError ParamNotFound The string parameter was not specified. + * @apiError WatchlistsDisabled Watchlists are disabled because the watchlists_enable setting is set to false. + * @apiError NotLoggedIn You aren't logged in, so you can't edit your watchlist (only logged in users have a watchlist). + * @apiError NoEmailAddress The currently logged in user doesn't have an email address specified in their account. + * @apiError DoVerbNotRecognised The specified do verb was not recognised. Supported verbs: add, remove, clear (a canonical list is returned with this error). + * @apiError PageNotFound The page name was not found in your watchlist. */ /* @@ -135,22 +141,50 @@ register_module([ header("x-problem: no-email-address-in-user-preferences"); exit(page_renderer::render_main("No email address specified -$settings->sitename", "

You are logged in, but have not specified an email address to send notifications to. Try specifying one in your user preferences and then coming back here.

")); } + // The thing we should do. $do = $_GET["do"] ?? "null"; + // The location we should redirect to after doing it successfully, if anywhere + $returnto = $_GET["returnto"] ?? null; + // If the watchlist doesn't exist, create it + // Note that saving this isn't essential - so we don't bother unless we perform some other action too. if(!is_array($env->user_data->watchlist)) $env->user_data->watchlist = []; switch($do) { case "add": + // Add the new page to the watchlist $env->user_data->watchlist[] = $env->page; + // Sort the list $collator = new Collator(); $collator->sort($env->user_data->watchlist, SORT_NATURAL | SORT_FLAG_CASE); + // Save back to disk + save_settings(); break; + case "remove": + $index = array_search($env->page, $env->user_data->watchlist); + if($index === false) { + http_response_code(400); + exit(page_renderer::render_main("Watchlist item not found - Error - $settings->sitename", "

Oops! The page with the name ".htmlentities($env->page)." isn't currently on your watchlist, so it couldn't be removed. Perhaps you already removed it?

+

Try going back to your watchlist.

")); + } + array_splice($env->user_data->watchlist, $index, 1); + save_settings(); + break; + case "clear": + $env->user_data->watchlist = []; + save_settings(); default: http_response_code(400); header("content-type: text/plain"); exit("Error: The do verb '$do' wasn't recognised. Current verbs supported: add, remove, clear"); } + + if($redirect) { + http_response_code(302); + header("location: $returnto"); + } + exit(page_renderer::render_main("Watchlist update successful", "

Your watchlist was updated successful. Click here to return to your previous page.

")); }); } ]);