]> git.mxchange.org Git - friendica.git/commitdiff
Central function to process request parameters
authorMichael <heluecht@pirati.ca>
Tue, 18 May 2021 06:31:22 +0000 (06:31 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 18 May 2021 06:31:22 +0000 (06:31 +0000)
src/Module/Api/Mastodon/Notifications.php
src/Module/Api/Mastodon/Search.php
src/Module/BaseApi.php

index 30e1060d820a38a83dad39c8dbb7b136360d7129..3dfcaa668917ff0c465fb48f99ed2cf59d2488ea 100644 (file)
@@ -50,23 +50,29 @@ class Notifications extends BaseApi
                        System::jsonExit(DI::mstdnNotification()->createFromNotifyId($id));
                }
 
+               $request = self::getRequest(['max_id' => 0, 'since_id' => 0, 'min_id' => 0, 'limit' => 20,
+                       'exclude_types' => [], 'account_id' => 0, 'with_muted' => false]);
+
                // Return results older than this ID
-               $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id'];
+               $max_id = $request['max_id'];
 
                // Return results newer than this ID
-               $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id'];
+               $since_id = $request['since_id'];
 
                // Return results immediately newer than this ID
-               $min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id'];
+               $min_id = $request['min_id'];
 
                // Maximum number of results to return (default 20)
-               $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit'];
+               $limit = $request['limit'];
 
                // Array of types to exclude (follow, favourite, reblog, mention, poll, follow_request)
-               $exclude_types = $_REQUEST['exclude_types'] ?? [];
+               $exclude_types = $request['exclude_types'];
 
                // Return only notifications received from this account
-               $account_id = (int)!isset($_REQUEST['account_id']) ? 0 : $_REQUEST['account_id'];
+               $account_id = $request['account_id'];
+
+               // Unknown parameter
+               $with_muted = $request['with_muted'];
 
                $params = ['order' => ['id' => true], 'limit' => $limit];
 
index 2da3f341c401a9b7e6afb1af29c48fdcec3db086..9d757584620917dd1022780f5cf26c194e17eaff 100644 (file)
@@ -46,26 +46,30 @@ class Search extends BaseApi
                self::login(self::SCOPE_READ);
                $uid = self::getCurrentUserID();
 
+               $request = self::getRequest(['account_id' => 0, 'max_id' => 0, 'min_id' => 0, 'type' => '',
+                       'exclude_unreviewed' => false, 'q' => '', 'resolve' => false, 'limit' => 20,
+                       'offset' => 0, 'following' => false]);
+
                // If provided, statuses returned will be authored only by this account
-               $account_id = $_REQUEST['account_id'] ?? '';
+               $account_id = $request['account_id'];
                // Return results older than this id
-               $max_id = (int)($_REQUEST['max_id'] ?? 0);
+               $max_id = $request['max_id'];
                // Return results immediately newer than this id
-               $min_id = (int)($_REQUEST['min_id'] ?? 0);
+               $min_id = $request['min_id'];
                // Enum(accounts, hashtags, statuses)
-               $type = $_REQUEST['type'] ?? '';
+               $type = $request['type'];
                // Filter out unreviewed tags? Defaults to false. Use true when trying to find trending tags.
-               $exclude_unreviewed = ($_REQUEST['exclude_unreviewed'] ?? '') == 'true';
+               $exclude_unreviewed = $request['exclude_unreviewed'];
                // The search query
-               $q = $_REQUEST['q'] ?? '';
+               $q = $request['q'];
                // Attempt WebFinger lookup. Defaults to false.
-               $resolve = ($_REQUEST['resolve'] ?? '') == 'true';
+               $resolve = $request['resolve'];
                // Maximum number of results to load, per type. Defaults to 20. Max 40.
-               $limit = (int)($_REQUEST['limit'] ?? 20);
+               $limit = $request['limit'];
                // Offset in search results. Used for pagination. Defaults to 0.
-               $offset = (int)($_REQUEST['offset'] ?? 0);
+               $offset = $request['offset'];
                // Only who the user is following. Defaults to false.
-               $following = ($_REQUEST['following'] ?? '') == 'true';
+               $following = $request['following'];
 
                $result = ['accounts' => [], 'statuses' => [], 'hashtags' => []];
 
index db4531d91c1351f0d139d232d6bd9c92d8fb12d5..c12872104b6498ba4c282c87ced9af83948083a5 100644 (file)
@@ -136,6 +136,43 @@ class BaseApi extends BaseModule
                System::jsonError(501, $errorobj->toArray());
        }
 
+       /**
+        * Processes data from GET requests and sets defaults
+        *
+        * @return array request data
+        */
+       public static function getRequest(array $defaults) {
+               $request = [];
+
+               foreach ($defaults as $parameter => $defaultvalue) {
+                       if (is_string($defaultvalue)) {
+                               $request[$parameter] = $_REQUEST[$parameter] ?? $defaultvalue;
+                       } elseif (is_int($defaultvalue)) {
+                               $request[$parameter] = (int)($_REQUEST[$parameter] ?? $defaultvalue);
+                       } elseif (is_float($defaultvalue)) {
+                               $request[$parameter] = (float)($_REQUEST[$parameter] ?? $defaultvalue);
+                       } elseif (is_array($defaultvalue)) {
+                               $request[$parameter] = $_REQUEST[$parameter] ?? [];
+                       } elseif (is_bool($defaultvalue)) {
+                               $request[$parameter] = in_array(strtolower($_REQUEST[$parameter] ?? ''), ['true', '1']);
+                       } else {
+                               Logger::notice('Unhandled default value type', ['parameter' => $parameter, 'type' => gettype($defaultvalue)]);
+                       }
+               }
+
+               foreach ($_REQUEST ?? [] as $parameter => $value) {
+                       if ($parameter == 'pagename') {
+                               continue;
+                       }
+                       if (!in_array($parameter, array_keys($defaults))) {
+                               Logger::notice('Unhandled request field', ['parameter' => $parameter, 'value' => $value, 'command' => DI::args()->getCommand()]);
+                       }
+               }
+
+               Logger::debug('Got request parameters', ['request' => $request, 'command' => DI::args()->getCommand()]);
+               return $request;
+       }
+
        /**
         * Get post data that is transmitted as JSON
         *