]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
API: Better use "about" instead of "pdesc"
[friendica.git] / include / api.php
index 52910d47273bba9c3e89aa70d84a58e7fa419702..84ad2a94b4d51a1fbf92bd5c35f5ade241c9cef5 100644 (file)
@@ -10,6 +10,7 @@ use Friendica\Content\Feature;
 use Friendica\Core\System;
 use Friendica\Core\Config;
 use Friendica\Core\NotificationsManager;
+use Friendica\Core\PConfig;
 use Friendica\Core\Worker;
 use Friendica\Database\DBM;
 use Friendica\Model\Contact;
@@ -185,7 +186,7 @@ function api_login(App $a)
        }
 
        if (!x($_SERVER, 'PHP_AUTH_USER')) {
-               logger('API_login: ' . print_r($_SERVER,true), LOGGER_DEBUG);
+               logger('API_login: ' . print_r($_SERVER, true), LOGGER_DEBUG);
                header('WWW-Authenticate: Basic realm="Friendica"');
                throw new UnauthorizedException("This API requires login");
        }
@@ -216,7 +217,7 @@ function api_login(App $a)
                */
        call_hooks('authenticate', $addon_auth);
 
-       if (($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
+       if ($addon_auth['authenticated'] && count($addon_auth['user_record'])) {
                $record = $addon_auth['user_record'];
        } else {
                $user_id = User::authenticate(trim($user), trim($password));
@@ -225,7 +226,7 @@ function api_login(App $a)
                }
        }
 
-       if ((! $record) || (! count($record))) {
+       if (!$record || !count($record)) {
                logger('API_login failure: ' . print_r($_SERVER, true), LOGGER_DEBUG);
                header('WWW-Authenticate: Basic realm="Friendica"');
                //header('HTTP/1.0 401 Unauthorized');
@@ -366,12 +367,13 @@ function api_call(App $a)
                                                break;
                                        case "json":
                                                header("Content-Type: application/json");
-                                               foreach ($r as $rr)
+                                               foreach ($r as $rr) {
                                                        $json = json_encode($rr);
-                                                       if (x($_GET, 'callback')) {
-                                                               $json = $_GET['callback'] . "(" . $json . ")";
-                                                       }
-                                                       return $json;
+                                               }
+                                               if (x($_GET, 'callback')) {
+                                                       $json = $_GET['callback'] . "(" . $json . ")";
+                                               }
+                                               return $json;
                                                break;
                                        case "rss":
                                                header("Content-Type: application/rss+xml");
@@ -398,7 +400,7 @@ function api_call(App $a)
  *
  * @param string $type Return type (xml, json, rss, as)
  * @param object $e    HTTPException Error object
- * @return strin error message formatted as $type
+ * @return string error message formatted as $type
  */
 function api_error($type, $e)
 {
@@ -470,12 +472,12 @@ function api_rss_extra(App $a, $arr, $user_info)
  * @return bool|string
  *             Contact url or False if contact id is unknown
  */
-function api_unique_id_to_url($id)
+function api_unique_id_to_nurl($id)
 {
-       $r = dba::select('contact', array('url'), array('uid' => 0, 'id' => $id), array('limit' => 1));
+       $r = dba::select('contact', array('nurl'), array('uid' => 0, 'id' => $id), array('limit' => 1));
 
        if (DBM::is_result($r)) {
-               return $r["url"];
+               return $r["nurl"];
        } else {
                return false;
        }
@@ -486,9 +488,8 @@ function api_unique_id_to_url($id)
  *
  * @param object     $a          App
  * @param int|string $contact_id Contact ID or URL
- * @param string     $type       Return type (for errors)
  */
-function api_get_user(App $a, $contact_id = null, $type = "json")
+function api_get_user(App $a, $contact_id = null)
 {
        global $called_api;
 
@@ -511,7 +512,7 @@ function api_get_user(App $a, $contact_id = null, $type = "json")
 
        // Searching for contact id with uid = 0
        if (!is_null($contact_id) && (intval($contact_id) != 0)) {
-               $user = dbesc(api_unique_id_to_url($contact_id));
+               $user = dbesc(api_unique_id_to_nurl($contact_id));
 
                if ($user == "") {
                        throw new BadRequestException("User not found.");
@@ -525,7 +526,7 @@ function api_get_user(App $a, $contact_id = null, $type = "json")
        }
 
        if (is_null($user) && x($_GET, 'user_id')) {
-               $user = dbesc(api_unique_id_to_url($_GET['user_id']));
+               $user = dbesc(api_unique_id_to_nurl($_GET['user_id']));
 
                if ($user == "") {
                        throw new BadRequestException("User not found.");
@@ -559,7 +560,7 @@ function api_get_user(App $a, $contact_id = null, $type = "json")
                $argid = count($called_api);
                list($user, $null) = explode(".", $a->argv[$argid]);
                if (is_numeric($user)) {
-                       $user = dbesc(api_unique_id_to_url($user));
+                       $user = dbesc(api_unique_id_to_nurl($user));
 
                        if ($user == "") {
                                return false;
@@ -742,13 +743,27 @@ function api_get_user(App $a, $contact_id = null, $type = "json")
 
        $pcontact_id  = Contact::getIdForURL($uinfo[0]['url'], 0, true);
 
+       if (!empty($profile[0]['about'])) {
+               $description = $profile[0]['about'];
+       } else {
+               $description = $uinfo[0]["about"];
+       }
+
+       if (!empty($usr[0]['default-location'])) {
+               $location = $usr[0]['default-location'];
+       } elseif (!empty($uinfo[0]["location"])) {
+               $location = $uinfo[0]["location"];
+       } else {
+               $location = $network_name;
+       }
+
        $ret = array(
                'id' => intval($pcontact_id),
                'id_str' => (string) intval($pcontact_id),
                'name' => (($uinfo[0]['name']) ? $uinfo[0]['name'] : $uinfo[0]['nick']),
                'screen_name' => (($uinfo[0]['nick']) ? $uinfo[0]['nick'] : $uinfo[0]['name']),
-               'location' => ($usr) ? $usr[0]['default-location'] : $network_name,
-               'description' => (($profile) ? $profile[0]['pdesc'] : null),
+               'location' => $location,
+               'description' => $description,
                'profile_image_url' => $uinfo[0]['micro'],
                'profile_image_url_https' => $uinfo[0]['micro'],
                'url' => $uinfo[0]['url'],
@@ -780,6 +795,37 @@ function api_get_user(App $a, $contact_id = null, $type = "json")
                'network' => $uinfo[0]['network'],
        );
 
+       // If this is a local user and it uses Frio, we can get its color preferences.
+       if ($ret['self']) {
+               $theme_info = dba::select('user', ['theme'], ['uid' => $ret['uid']], ['limit' => 1]);
+               if ($theme_info['theme'] === 'frio') {
+                       $schema = PConfig::get($ret['uid'], 'frio', 'schema');
+                       if ($schema && ($schema != '---')) {
+                               if (file_exists('view/theme/frio/schema/'.$schema.'.php')) {
+                                       $schemefile = 'view/theme/frio/schema/'.$schema.'.php';
+                                       require_once $schemefile;
+                               }
+                       } else {
+                               $nav_bg = PConfig::get($ret['uid'], 'frio', 'nav_bg');
+                               $link_color = PConfig::get($ret['uid'], 'frio', 'link_color');
+                               $bgcolor = PConfig::get($ret['uid'], 'frio', 'background_color');
+                       }
+                       if (!$nav_bg) {
+                               $nav_bg = "#708fa0";
+                       }
+                       if (!$link_color) {
+                               $link_color = "#6fdbe8";
+                       }
+                       if (!$bgcolor) {
+                               $bgcolor = "#ededed";
+                       }
+
+                       $ret['profile_sidebar_fill_color'] = str_replace('#', '', $nav_bg);
+                       $ret['profile_link_color'] = str_replace('#', '', $link_color);
+                       $ret['profile_background_color'] = str_replace('#', '', $bgcolor);
+               }
+       }
+
        return $ret;
 }
 
@@ -915,12 +961,10 @@ function api_create_xml($data, $root_element)
  * @param string $type         Return type (atom, rss, xml, json)
  * @param array  $data         JSON style array
  *
- * @return (string|object) XML data or JSON data
+ * @return (string|object|array) XML data or JSON data
  */
 function api_format_data($root_element, $type, $data)
 {
-       $a = get_app();
-
        switch ($type) {
                case "atom":
                case "rss":
@@ -1325,8 +1369,9 @@ function api_status_show($type)
                        'retweeted' => false,
                        'possibly_sensitive' => false,
                        'lang' => "",
-                       'statusnet_html'                => $converted["html"],
-                       'statusnet_conversation_id'     => $lastwall['parent'],
+                       'statusnet_html' => $converted["html"],
+                       'statusnet_conversation_id' => $lastwall['parent'],
+                       'external_url' => System::baseUrl() . "/display/" . $lastwall['guid'],
                );
 
                if (count($converted["attachments"]) > 0) {
@@ -1414,7 +1459,8 @@ function api_users_show($type)
                        $geo => null,
                        'favorited' => $lastwall['starred'] ? true : false,
                        'statusnet_html' => $converted["html"],
-                       'statusnet_conversation_id'     => $lastwall['parent'],
+                       'statusnet_conversation_id' => $lastwall['parent'],
+                       'external_url' => System::baseUrl() . "/display/" . $lastwall['guid'],
                );
 
                if (count($converted["attachments"]) > 0) {
@@ -1463,7 +1509,7 @@ function api_users_search($type)
                if (DBM::is_result($r)) {
                        $k = 0;
                        foreach ($r as $user) {
-                               $user_info = api_get_user($a, $user["id"], "json");
+                               $user_info = api_get_user($a, $user["id"]);
 
                                if ($type == "xml") {
                                        $userlist[$k++.":user"] = $user_info;
@@ -1484,6 +1530,96 @@ function api_users_search($type)
 /// @TODO move to top of file or somewhere better
 api_register_func('api/users/search', 'api_users_search');
 
+/**
+ * Return user objects
+ *
+ * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-users-lookup
+ *
+ * @param string $type Return format: json or xml
+ *
+ * @return array|string
+ * @throws NotFoundException if the results are empty.
+ */
+function api_users_lookup($type)
+{
+       $users = array();
+
+       if (x($_REQUEST['user_id'])) {
+               foreach (explode(',', $_REQUEST['user_id']) as $id) {
+                       if (!empty($id)) {
+                               $users[] = api_get_user(get_app(), $id);
+                       }
+               }
+       }
+
+       if (empty($users)) {
+               throw new NotFoundException;
+       }
+
+       return api_format_data("users", $type, array('users' => $users));
+}
+
+/// @TODO move to top of file or somewhere better
+api_register_func('api/users/lookup', 'api_users_lookup', true);
+
+/**
+ * Returns statuses that match a specified query.
+ *
+ * @see https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets
+ *
+ * @param string $type Return format: json, xml, atom, rss
+ *
+ * @return array|string
+ * @throws BadRequestException if the "q" parameter is missing.
+ */
+function api_search($type)
+{
+       $data = array();
+
+       if (!x($_REQUEST, 'q')) {
+               throw new BadRequestException("q parameter is required.");
+       }
+
+       if (x($_REQUEST, 'rpp')) {
+               $count = $_REQUEST['rpp'];
+       } elseif (x($_REQUEST, 'count')) {
+               $count = $_REQUEST['count'];
+       } else {
+               $count = 15;
+       }
+
+       $since_id = (x($_REQUEST, 'since_id') ? $_REQUEST['since_id'] : 0);
+       $max_id = (x($_REQUEST, 'max_id') ? $_REQUEST['max_id'] : 0);
+       $page = (x($_REQUEST, 'page') ? $_REQUEST['page'] - 1 : 0);
+
+       $start = $page * $count;
+
+       if ($max_id > 0) {
+               $sql_extra .= ' AND `item`.`id` <= ' . intval($max_id);
+       }
+
+       $r = dba::p(
+               "SELECT ".item_fieldlists()."
+               FROM `item` ".item_joins()."
+               WHERE ".item_condition()." AND (`item`.`uid` = 0 OR (`item`.`uid` = ? AND NOT `item`.`global`))
+               AND `item`.`body` LIKE CONCAT('%',?,'%')
+               $sql_extra
+               AND `item`.`id`>?
+               ORDER BY `item`.`id` DESC LIMIT ".intval($start)." ,".intval($count)." ",
+               api_user(),
+               $_REQUEST['q'],
+               $since_id
+       );
+
+       $data['status'] = api_format_items(dba::inArray($r), api_get_user(get_app()));
+
+       return api_format_data("statuses", $type, $data);
+}
+
+/// @TODO move to top of file or somewhere better
+api_register_func('api/search/tweets', 'api_search', true);
+api_register_func('api/search', 'api_search', true);
+
 /**
  *
  * http://developer.twitter.com/doc/get/statuses/home_timeline
@@ -1611,40 +1747,134 @@ function api_statuses_public_timeline($type)
 
        $start = $page * $count;
 
-       if ($max_id > 0) {
-               $sql_extra = 'AND `item`.`id` <= ' . intval($max_id);
+       if ($exclude_replies && !$conversation_id) {
+               if ($max_id > 0) {
+                       $sql_extra = 'AND `thread`.`iid` <= ' . intval($max_id);
+               }
+
+               $r = dba::p(
+                       "SELECT " . item_fieldlists() . "
+                       FROM `thread`
+                       STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
+                       " . item_joins() . "
+                       STRAIGHT_JOIN `user` ON `user`.`uid` = `thread`.`uid`
+                               AND NOT `user`.`hidewall`
+                       AND `verb` = ?
+                       AND NOT `thread`.`private`
+                       AND `thread`.`wall`
+                       AND `thread`.`visible`
+                       AND NOT `thread`.`deleted`
+                       AND NOT `thread`.`moderated`
+                       AND `thread`.`iid` > ?
+                       $sql_extra
+                       ORDER BY `thread`.`iid` DESC
+                       LIMIT " . intval($start) . ", " . intval($count),
+                       ACTIVITY_POST,
+                       $since_id
+               );
+
+               $r = dba::inArray($r);
+       } else {
+               if ($max_id > 0) {
+                       $sql_extra = 'AND `item`.`id` <= ' . intval($max_id);
+               }
+               if ($conversation_id > 0) {
+                       $sql_extra .= ' AND `item`.`parent` = ' . intval($conversation_id);
+               }
+
+               $r = dba::p(
+                       "SELECT " . item_fieldlists() . "
+                       FROM `item`
+                       " . item_joins() . "
+                       STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
+                               AND NOT `user`.`hidewall`
+                       AND `verb` = ?
+                       AND NOT `item`.`private`
+                       AND `item`.`wall`
+                       AND `item`.`visible`
+                       AND NOT `item`.`deleted`
+                       AND NOT `item`.`moderated`
+                       AND `item`.`id` > ?
+                       $sql_extra
+                       ORDER BY `item`.`id` DESC
+                       LIMIT " . intval($start) . ", " . intval($count),
+                       ACTIVITY_POST,
+                       $since_id
+               );
+
+               $r = dba::inArray($r);
        }
-       if ($exclude_replies > 0) {
-               $sql_extra .= ' AND `item`.`parent` = `item`.`id`';
+
+       $ret = api_format_items($r, $user_info, false, $type);
+
+       $data = array('status' => $ret);
+       switch ($type) {
+               case "atom":
+               case "rss":
+                       $data = api_rss_extra($a, $data, $user_info);
+                       break;
        }
-       if ($conversation_id > 0) {
-               $sql_extra .= ' AND `item`.`parent` = ' . intval($conversation_id);
+
+       return api_format_data("statuses", $type, $data);
+}
+
+/// @TODO move to top of file or somewhere better
+api_register_func('api/statuses/public_timeline', 'api_statuses_public_timeline', true);
+
+/**
+ * @brief Returns the list of public federated posts this node knows about
+ *
+ * @param string $type Return format: json, xml, atom, rss
+ * @return array|string
+ * @throws ForbiddenException
+ */
+function api_statuses_networkpublic_timeline($type)
+{
+       $a = get_app();
+
+       if (api_user() === false) {
+               throw new ForbiddenException();
        }
 
-       $r = q(
-               "SELECT `item`.*, `item`.`id` AS `item_id`, `item`.`network` AS `item_network`,
-               `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
-               `contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`,
-               `contact`.`id` AS `cid`,
-               `user`.`nickname`, `user`.`hidewall`
-               FROM `item`
-               STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` = `item`.`uid`
-                       AND (NOT `contact`.`blocked` OR `contact`.`pending`)
-               STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
-                       AND NOT `user`.`hidewall`
-               WHERE `verb` = '%s' AND `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
-               AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
-               AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
-               AND NOT `item`.`private` AND `item`.`wall`
+       $user_info = api_get_user($a);
+
+       $since_id        = x($_REQUEST, 'since_id')        ? $_REQUEST['since_id']        : 0;
+       $max_id          = x($_REQUEST, 'max_id')          ? $_REQUEST['max_id']          : 0;
+
+       // pagination
+       $count = x($_REQUEST, 'count') ? $_REQUEST['count']   : 20;
+       $page  = x($_REQUEST, 'page')  ? $_REQUEST['page']    : 1;
+       if ($page < 1) {
+               $page = 1;
+       }
+       $start = ($page - 1) * $count;
+
+       $sql_extra = '';
+       if ($max_id > 0) {
+               $sql_extra = 'AND `thread`.`iid` <= ' . intval($max_id);
+       }
+
+       $r = dba::p(
+               "SELECT " . item_fieldlists() . "
+               FROM `thread`
+               STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
+               " . item_joins() . "
+               WHERE `thread`.`uid` = 0
+               AND `verb` = ?
+               AND NOT `thread`.`private`
+               AND `thread`.`visible`
+               AND NOT `thread`.`deleted`
+               AND NOT `thread`.`moderated`
+               AND `thread`.`iid` > ?
                $sql_extra
-               AND `item`.`id`>%d
-               ORDER BY `item`.`id` DESC LIMIT %d, %d ",
-               dbesc(ACTIVITY_POST),
-               intval($since_id),
-               intval($start),
-               intval($count)
+               ORDER BY `thread`.`iid` DESC
+               LIMIT " . intval($start) . ", " . intval($count),
+               ACTIVITY_POST,
+               $since_id
        );
 
+       $r = dba::inArray($r);
+
        $ret = api_format_items($r, $user_info, false, $type);
 
        $data = array('status' => $ret);
@@ -1659,7 +1889,7 @@ function api_statuses_public_timeline($type)
 }
 
 /// @TODO move to top of file or somewhere better
-api_register_func('api/statuses/public_timeline', 'api_statuses_public_timeline', true);
+api_register_func('api/statuses/networkpublic_timeline', 'api_statuses_networkpublic_timeline', true);
 
 /**
  * @TODO nothing to say?
@@ -1797,10 +2027,12 @@ function api_conversation_show($type)
                AND `item`.`uid` = %d AND `item`.`verb` = '%s'
                AND `item`.`id`>%d $sql_extra
                ORDER BY `item`.`id` DESC LIMIT %d ,%d",
-               intval($id), intval(api_user()),
+               intval($id),
+               intval(api_user()),
                dbesc(ACTIVITY_POST),
                intval($since_id),
-               intval($start), intval($count)
+               intval($start),
+               intval($count)
        );
 
        if (!DBM::is_result($r)) {
@@ -2019,6 +2251,13 @@ function api_statuses_mentions($type)
 api_register_func('api/statuses/mentions', 'api_statuses_mentions', true);
 api_register_func('api/statuses/replies', 'api_statuses_mentions', true);
 
+/**
+ * @brief Returns a user's public timeline
+ *
+ * @param string $type Either "json" or "xml"
+ * @return string|array
+ * @throws ForbiddenException
+ */
 function api_statuses_user_timeline($type)
 {
        $a = get_app();
@@ -2028,7 +2267,6 @@ function api_statuses_user_timeline($type)
        }
 
        $user_info = api_get_user($a);
-       // get last network messages
 
        logger(
                "api_statuses_user_timeline: api_user: ". api_user() .
@@ -2037,18 +2275,18 @@ function api_statuses_user_timeline($type)
                LOGGER_DEBUG
        );
 
-       // params
-       $count = (x($_REQUEST, 'count') ? $_REQUEST['count'] : 20);
-       $page = (x($_REQUEST, 'page') ? $_REQUEST['page'] -1 : 0);
-       if ($page < 0) {
-               $page = 0;
-       }
-       $since_id = (x($_REQUEST, 'since_id') ? $_REQUEST['since_id'] : 0);
-       //$since_id = 0;//$since_id = (x($_REQUEST, 'since_id')?$_REQUEST['since_id'] : 0);
-       $exclude_replies = (x($_REQUEST, 'exclude_replies') ? 1 : 0);
-       $conversation_id = (x($_REQUEST, 'conversation_id') ? $_REQUEST['conversation_id'] : 0);
+       $since_id        = x($_REQUEST, 'since_id')        ? $_REQUEST['since_id']        : 0;
+       $max_id          = x($_REQUEST, 'max_id')          ? $_REQUEST['max_id']          : 0;
+       $exclude_replies = x($_REQUEST, 'exclude_replies') ? 1                            : 0;
+       $conversation_id = x($_REQUEST, 'conversation_id') ? $_REQUEST['conversation_id'] : 0;
 
-       $start = $page * $count;
+       // pagination
+       $count = x($_REQUEST, 'count') ? $_REQUEST['count'] : 20;
+       $page  = x($_REQUEST, 'page')  ? $_REQUEST['page']  : 1;
+       if ($page < 1) {
+               $page = 1;
+       }
+       $start = ($page - 1) * $count;
 
        $sql_extra = '';
        if ($user_info['self'] == 1) {
@@ -2058,10 +2296,15 @@ function api_statuses_user_timeline($type)
        if ($exclude_replies > 0) {
                $sql_extra .= ' AND `item`.`parent` = `item`.`id`';
        }
+
        if ($conversation_id > 0) {
                $sql_extra .= ' AND `item`.`parent` = ' . intval($conversation_id);
        }
 
+       if ($max_id > 0) {
+               $sql_extra .= ' AND `item`.`id` <= ' . intval($max_id);
+       }
+
        $r = q(
                "SELECT `item`.*, `item`.`id` AS `item_id`, `item`.`network` AS `item_network`,
                `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
@@ -2074,7 +2317,7 @@ function api_statuses_user_timeline($type)
                AND `item`.`contact-id` = %d
                AND `item`.`visible` AND NOT `item`.`moderated` AND NOT `item`.`deleted`
                $sql_extra
-               AND `item`.`id`>%d
+               AND `item`.`id` > %d
                ORDER BY `item`.`id` DESC LIMIT %d ,%d ",
                intval(api_user()),
                dbesc(ACTIVITY_POST),
@@ -2098,7 +2341,7 @@ function api_statuses_user_timeline($type)
 }
 
 /// @TODO move to top of file or somwhere better
-api_register_func('api/statuses/user_timeline','api_statuses_user_timeline', true);
+api_register_func('api/statuses/user_timeline', 'api_statuses_user_timeline', true);
 
 /**
  * Star/unstar an item
@@ -2149,7 +2392,7 @@ function api_favorites_create_destroy($type)
                        throw new BadRequestException("Invalid action ".$action);
        }
 
-       $r = q("UPDATE item SET starred=%d WHERE id=%d AND uid=%d",     $item[0]['starred'], $itemid, api_user());
+       $r = q("UPDATE item SET starred=%d WHERE id=%d AND uid=%d", $item[0]['starred'], $itemid, api_user());
 
        q("UPDATE thread SET starred=%d WHERE iid=%d AND uid=%d", $item[0]['starred'], $itemid, api_user());
 
@@ -2469,14 +2712,15 @@ function api_get_entitities(&$text, $bbcode)
        foreach ($ordered_urls as $url) {
                if ((substr($url["title"], 0, 7) != "http://") && (substr($url["title"], 0, 8) != "https://")
                        && !strpos($url["title"], "http://") && !strpos($url["title"], "https://")
-               )
+               ) {
                        $display_url = $url["title"];
-               else {
+               else {
                        $display_url = str_replace(array("http://www.", "https://www."), array("", ""), $url["url"]);
                        $display_url = str_replace(array("http://", "https://"), array("", ""), $display_url);
 
-                       if (strlen($display_url) > 26)
+                       if (strlen($display_url) > 26) {
                                $display_url = substr($display_url, 0, 25)."…";
+                       }
                }
 
                //$start = strpos($text, $url, $offset);
@@ -2495,8 +2739,9 @@ function api_get_entitities(&$text, $bbcode)
        foreach ($images[1] as $image) {
                //$start = strpos($text, $url, $offset);
                $start = iconv_strpos($text, $image, 0, "UTF-8");
-               if (!($start === false))
+               if (!($start === false)) {
                        $ordered_images[$start] = $image;
+               }
        }
        //$entities["media"] = array();
        $offset = 0;
@@ -2505,8 +2750,9 @@ function api_get_entitities(&$text, $bbcode)
                $display_url = str_replace(array("http://www.", "https://www."), array("", ""), $url);
                $display_url = str_replace(array("http://", "https://"), array("", ""), $display_url);
 
-               if (strlen($display_url) > 26)
+               if (strlen($display_url) > 26) {
                        $display_url = substr($display_url, 0, 25)."…";
+               }
 
                $start = iconv_strpos($text, $url, $offset, "UTF-8");
                if (!($start === false)) {
@@ -2657,8 +2903,9 @@ function api_format_items_activities(&$item, $type = "json")
                        $xml_activities["friendica:".$k] = $v;
                        // add user data into xml output
                        $k_user = 0;
-                       foreach ($v as $user)
+                       foreach ($v as $user) {
                                $xml_activities["friendica:".$k][$k_user++.":user"] = $user;
+                       }
                }
                $activities = $xml_activities;
        }
@@ -2769,8 +3016,9 @@ function api_format_items($r, $user_info, $filter_user = false, $type = "json")
                        'user' =>  $status_user ,
                        'friendica_owner' => $owner_user,
                        //'entities' => NULL,
-                       'statusnet_html'                => $converted["html"],
-                       'statusnet_conversation_id'     => $item['parent'],
+                       'statusnet_html' => $converted["html"],
+                       'statusnet_conversation_id' => $item['parent'],
+                       'external_url' => System::baseUrl() . "/display/" . $item['guid'],
                        'friendica_activities' => api_format_items_activities($item, $type),
                );
 
@@ -2825,12 +3073,13 @@ function api_format_items($r, $user_info, $filter_user = false, $type = "json")
                if ($item["coord"] != "") {
                        $coords = explode(' ', $item["coord"]);
                        if (count($coords) == 2) {
-                               if ($type == "json")
+                               if ($type == "json") {
                                        $status["geo"] = array('type' => 'Point',
                                                        'coordinates' => array((float) $coords[0],
                                                                                (float) $coords[1]));
-                               else // Not sure if this is the official format - if someone founds a documentation we can check
+                               } else {// Not sure if this is the official format - if someone founds a documentation we can check
                                        $status["georss:point"] = $item["coord"];
+                               }
                        }
                }
                $ret[] = $status;
@@ -2901,11 +3150,16 @@ function api_lists_list($type)
 api_register_func('api/lists/list', 'api_lists_list', true);
 
 /**
- * https://dev.twitter.com/docs/api/1/get/statuses/friends
- * This function is deprecated by Twitter
- * returns: json, xml
+ * @brief Returns either the friends of the follower list
+ *
+ * Note: Considers friends and followers lists to be private and won't return
+ * anything if any user_id parameter is passed.
+ *
+ * @param string $qtype Either "friends" or "followers"
+ * @return boolean|array
+ * @throws ForbiddenException
  */
-function api_statuses_f($type, $qtype)
+function api_statuses_f($qtype)
 {
        $a = get_app();
 
@@ -2913,9 +3167,17 @@ function api_statuses_f($type, $qtype)
                throw new ForbiddenException();
        }
 
+       // pagination
+       $count = x($_GET, 'count') ? $_GET['count'] : 20;
+       $page = x($_GET, 'page') ? $_GET['page'] : 1;
+       if ($page < 1) {
+               $page = 1;
+       }
+       $start = ($page - 1) * $count;
+
        $user_info = api_get_user($a);
 
-       if (x($_GET, 'cursor') && $_GET['cursor']=='undefined') {
+       if (x($_GET, 'cursor') && $_GET['cursor'] == 'undefined') {
                /* this is to stop Hotot to load friends multiple times
                *  I'm not sure if I'm missing return something or
                *  is a bug in hotot. Workaround, meantime
@@ -2926,10 +3188,10 @@ function api_statuses_f($type, $qtype)
                return false;
        }
 
+       $sql_extra = '';
        if ($qtype == 'friends') {
                $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_SHARING), intval(CONTACT_IS_FRIEND));
-       }
-       if ($qtype == 'followers') {
+       } elseif ($qtype == 'followers') {
                $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_FOLLOWER), intval(CONTACT_IS_FRIEND));
        }
 
@@ -2938,9 +3200,26 @@ function api_statuses_f($type, $qtype)
                $sql_extra = " AND false ";
        }
 
+       if ($qtype == 'blocks') {
+               $sql_filter = 'AND `blocked` AND NOT `pending`';
+       } elseif ($qtype == 'incoming') {
+               $sql_filter = 'AND `pending`';
+       } else {
+               $sql_filter = 'AND (NOT `blocked` OR `pending`)';
+       }
+
        $r = q(
-               "SELECT `nurl` FROM `contact` WHERE `uid` = %d AND NOT `self` AND (NOT `blocked` OR `pending`) $sql_extra ORDER BY `nick`",
-               intval(api_user())
+               "SELECT `nurl`
+               FROM `contact`
+               WHERE `uid` = %d
+               AND NOT `self`
+               $sql_filter
+               $sql_extra
+               ORDER BY `nick`
+               LIMIT %d, %d",
+               intval(api_user()),
+               intval($start),
+               intval($count)
        );
 
        $ret = array();
@@ -2956,21 +3235,37 @@ function api_statuses_f($type, $qtype)
        }
 
        return array('user' => $ret);
-
 }
 
+
+/**
+ * @brief Returns the list of friends of the provided user
+ *
+ * @deprecated By Twitter API in favor of friends/list
+ *
+ * @param string $type Either "json" or "xml"
+ * @return boolean|string|array
+ */
 function api_statuses_friends($type)
 {
-       $data =  api_statuses_f($type, "friends");
+       $data =  api_statuses_f("friends");
        if ($data === false) {
                return false;
        }
        return api_format_data("users", $type, $data);
 }
 
+/**
+ * @brief Returns the list of friends of the provided user
+ *
+ * @deprecated By Twitter API in favor of friends/list
+ *
+ * @param string $type Either "json" or "xml"
+ * @return boolean|string|array
+ */
 function api_statuses_followers($type)
 {
-       $data = api_statuses_f($type, "followers");
+       $data = api_statuses_f("followers");
        if ($data === false) {
                return false;
        }
@@ -2981,6 +3276,54 @@ function api_statuses_followers($type)
 api_register_func('api/statuses/friends', 'api_statuses_friends', true);
 api_register_func('api/statuses/followers', 'api_statuses_followers', true);
 
+/**
+ * Returns the list of blocked users
+ *
+ * @see https://developer.twitter.com/en/docs/accounts-and-users/mute-block-report-users/api-reference/get-blocks-list
+ *
+ * @param string $type Either "json" or "xml"
+ *
+ * @return boolean|string|array
+ */
+function api_blocks_list($type)
+{
+       $data =  api_statuses_f('blocks');
+       if ($data === false) {
+               return false;
+       }
+       return api_format_data("users", $type, $data);
+}
+
+/// @TODO move to top of file or somewhere better
+api_register_func('api/blocks/list', 'api_blocks_list', true);
+
+/**
+ * Returns the list of pending users IDs
+ *
+ * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-friendships-incoming
+ *
+ * @param string $type Either "json" or "xml"
+ *
+ * @return boolean|string|array
+ */
+function api_friendships_incoming($type)
+{
+       $data =  api_statuses_f('incoming');
+       if ($data === false) {
+               return false;
+       }
+
+       $ids = array();
+       foreach ($data['user'] as $user) {
+               $ids[] = $user['id'];
+       }
+
+       return api_format_data("ids", $type, array('id' => $ids));
+}
+
+/// @TODO move to top of file or somewhere better
+api_register_func('api/friendships/incoming', 'api_friendships_incoming', true);
+
 function api_statusnet_config($type)
 {
        $a = get_app();
@@ -2993,7 +3336,7 @@ function api_statusnet_config($type)
        $private = ((Config::get('system', 'block_public')) ? 'true' : 'false');
        $textlimit = (string) (($a->config['max_import_size']) ? $a->config['max_import_size'] : 200000);
        if ($a->config['api_import_size']) {
-               $texlimit = string($a->config['api_import_size']);
+               $textlimit = (string) $a->config['api_import_size'];
        }
        $ssl = ((Config::get('system', 'have_ssl')) ? 'true' : 'false');
        $sslserver = (($ssl === 'true') ? str_replace('http:', 'https:', System::baseUrl()) : '');
@@ -3035,7 +3378,7 @@ api_register_func('api/statusnet/version', 'api_statusnet_version', false);
 /**
  * @todo use api_format_data() to return data
  */
-function api_ff_ids($type,$qtype)
+function api_ff_ids($type, $qtype)
 {
        $a = get_app();
 
@@ -3045,17 +3388,6 @@ function api_ff_ids($type,$qtype)
 
        $user_info = api_get_user($a);
 
-       if ($qtype == 'friends') {
-               $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_SHARING), intval(CONTACT_IS_FRIEND));
-       }
-       if ($qtype == 'followers') {
-               $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_FOLLOWER), intval(CONTACT_IS_FRIEND));
-       }
-
-       if (!$user_info["self"]) {
-               $sql_extra = " AND false ";
-       }
-
        $stringify_ids = (x($_REQUEST, 'stringify_ids') ? $_REQUEST['stringify_ids'] : false);
 
        $r = q(
@@ -3100,9 +3432,13 @@ function api_direct_messages_new($type)
 
        $a = get_app();
 
-       if (api_user() === false) throw new ForbiddenException();
+       if (api_user() === false) {
+               throw new ForbiddenException();
+       }
 
-       if (!x($_POST, "text") || (!x($_POST, "screen_name") && !x($_POST, "user_id"))) return;
+       if (!x($_POST, "text") || (!x($_POST, "screen_name") && !x($_POST, "user_id"))) {
+               return;
+       }
 
        $sender = api_get_user($a);
 
@@ -3157,7 +3493,6 @@ function api_direct_messages_new($type)
        }
 
        return api_format_data("direct-messages", $type, $data);
-
 }
 
 /// @TODO move to top of file or somewhere better
@@ -3236,7 +3571,6 @@ function api_direct_messages_destroy($type)
                }
        }
        /// @todo return JSON data like Twitter API not yet implemented
-
 }
 
 /// @TODO move to top of file or somewhere better
@@ -3418,8 +3752,9 @@ function api_fr_photoalbum_delete($type)
                intval(api_user()),
                dbesc($album)
        );
-       if (!DBM::is_result($r))
+       if (!DBM::is_result($r)) {
                throw new BadRequestException("album not available");
+       }
 
        // function for setting the items to "deleted = 1" which ensures that comments, likes etc. are not shown anymore
        // to the user and the contacts of the users (drop_items() performs the federation of the deletion to other networks
@@ -3955,7 +4290,8 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
        }
        logger(
                "File upload src: " . $src . " - filename: " . $filename .
-               " - size: " . $filesize . " - type: " . $filetype, LOGGER_DEBUG
+               " - size: " . $filesize . " - type: " . $filetype,
+               LOGGER_DEBUG
        );
 
        // check if there was a php upload error
@@ -3964,7 +4300,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
        }
        // check against max upload size within Friendica instance
        $maximagesize = Config::get('system', 'maximagesize');
-       if (($maximagesize) && ($filesize > $maximagesize)) {
+       if ($maximagesize && ($filesize > $maximagesize)) {
                $formattedBytes = formatBytes($maximagesize);
                throw new InternalServerErrorException("image size exceeds Friendica config setting (uploaded size: $formattedBytes)");
        }
@@ -4227,8 +4563,8 @@ function prepare_photo_data($type, $scale, $photo_id)
  */
 function api_friendica_remoteauth()
 {
-       $url = ((x($_GET, 'url')) ? $_GET['url'] : '');
-       $c_url = ((x($_GET, 'c_url')) ? $_GET['c_url'] : '');
+       $url = (x($_GET, 'url') ? $_GET['url'] : '');
+       $c_url = (x($_GET, 'c_url') ? $_GET['c_url'] : '');
 
        if ($url === '' || $c_url === '') {
                throw new BadRequestException("Wrong parameters.");
@@ -4238,26 +4574,22 @@ function api_friendica_remoteauth()
 
        // traditional DFRN
 
-       $r = q(
-               "SELECT * FROM `contact` WHERE `id` = %d AND `nurl` = '%s' LIMIT 1",
-               dbesc($c_url),
-               intval(api_user())
-       );
+       $r = dba::select('contact', [], ['uid' => api_user(), 'nurl' => $c_url], ['limit' => 1]);
 
-       if ((! DBM::is_result($r)) || ($r[0]['network'] !== NETWORK_DFRN)) {
+       if (!DBM::is_result($r) || ($r['network'] !== NETWORK_DFRN)) {
                throw new BadRequestException("Unknown contact");
        }
 
-       $cid = $r[0]['id'];
+       $cid = $r['id'];
 
-       $dfrn_id = $orig_id = (($r[0]['issued-id']) ? $r[0]['issued-id'] : $r[0]['dfrn-id']);
+       $dfrn_id = $orig_id = (($r['issued-id']) ? $r['issued-id'] : $r['dfrn-id']);
 
-       if ($r[0]['duplex'] && $r[0]['issued-id']) {
-               $orig_id = $r[0]['issued-id'];
+       if ($r['duplex'] && $r['issued-id']) {
+               $orig_id = $r['issued-id'];
                $dfrn_id = '1:' . $orig_id;
        }
-       if ($r[0]['duplex'] && $r[0]['dfrn-id']) {
-               $orig_id = $r[0]['dfrn-id'];
+       if ($r['duplex'] && $r['dfrn-id']) {
+               $orig_id = $r['dfrn-id'];
                $dfrn_id = '0:' . $orig_id;
        }
 
@@ -4273,10 +4605,10 @@ function api_friendica_remoteauth()
                intval(time() + 45)
        );
 
-       logger($r[0]['name'] . ' ' . $sec, LOGGER_DEBUG);
-       $dest = (($url) ? '&destination_url=' . $url : '');
+       logger($r['name'] . ' ' . $sec, LOGGER_DEBUG);
+       $dest = ($url ? '&destination_url=' . $url : '');
        goaway(
-               $r[0]['poll'] . '?dfrn_id=' . $dfrn_id
+               $r['poll'] . '?dfrn_id=' . $dfrn_id
                . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
                . '&type=profile&sec=' . $sec . $dest . $quiet
        );
@@ -4287,7 +4619,7 @@ api_register_func('api/friendica/remoteauth', 'api_friendica_remoteauth', true);
  * @brief Return the item shared, if the item contains only the [share] tag
  *
  * @param array $item Sharer item
- * @return array Shared item or false if not a reshare
+ * @return array|false Shared item or false if not a reshare
  */
 function api_share_as_retweet(&$item)
 {
@@ -4358,8 +4690,9 @@ function api_share_as_retweet(&$item)
 
        $posted = "";
        preg_match("/posted='(.*?)'/ism", $attributes, $matches);
-       if ($matches[1] != "")
+       if ($matches[1] != "") {
                $posted = $matches[1];
+       }
 
        preg_match('/posted="(.*?)"/ism', $attributes, $matches);
        if ($matches[1] != "") {
@@ -4381,7 +4714,6 @@ function api_share_as_retweet(&$item)
        $reshared_item["edited"] = $posted;
 
        return $reshared_item;
-
 }
 
 function api_get_nick($profile)
@@ -4476,9 +4808,11 @@ function api_in_reply_to($item)
        $in_reply_to['screen_name'] = null;
 
        if (($item['thr-parent'] != $item['uri']) && (intval($item['parent']) != intval($item['id']))) {
-               $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
+               $r = q(
+                       "SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
                        intval($item['uid']),
-                       dbesc($item['thr-parent']));
+                       dbesc($item['thr-parent'])
+               );
 
                if (DBM::is_result($r)) {
                        $in_reply_to['status_id'] = intval($r[0]['id']);
@@ -4488,7 +4822,8 @@ function api_in_reply_to($item)
 
                $in_reply_to['status_id_str'] = (string) intval($in_reply_to['status_id']);
 
-               $r = q("SELECT `contact`.`nick`, `contact`.`name`, `contact`.`id`, `contact`.`url` FROM item
+               $r = q(
+                       "SELECT `contact`.`nick`, `contact`.`name`, `contact`.`id`, `contact`.`url` FROM item
                        STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`author-id`
                        WHERE `item`.`id` = %d LIMIT 1",
                        intval($in_reply_to['status_id'])
@@ -4550,20 +4885,20 @@ function api_clean_attachments($body)
 {
        $data = get_attachment_data($body);
 
-       if (!$data)
+       if (!$data) {
                return $body;
-
+       }
        $body = "";
 
-       if (isset($data["text"]))
+       if (isset($data["text"])) {
                $body = $data["text"];
-
-       if (($body == "") && (isset($data["title"])))
+       }
+       if (($body == "") && isset($data["title"])) {
                $body = $data["title"];
-
-       if (isset($data["url"]))
+       }
+       if (isset($data["url"])) {
                $body .= "\n".$data["url"];
-
+       }
        $body .= $data["after"];
 
        return $body;
@@ -4573,39 +4908,56 @@ function api_best_nickname(&$contacts)
 {
        $best_contact = array();
 
-       if (count($contact) == 0)
+       if (count($contact) == 0) {
                return;
+       }
 
-       foreach ($contacts as $contact)
+       foreach ($contacts as $contact) {
                if ($contact["network"] == "") {
                        $contact["network"] = "dfrn";
                        $best_contact = array($contact);
                }
+       }
 
-       if (sizeof($best_contact) == 0)
-               foreach ($contacts as $contact)
-                       if ($contact["network"] == "dfrn")
+       if (sizeof($best_contact) == 0) {
+               foreach ($contacts as $contact) {
+                       if ($contact["network"] == "dfrn") {
                                $best_contact = array($contact);
+                       }
+               }
+       }
 
-       if (sizeof($best_contact) == 0)
-               foreach ($contacts as $contact)
-                       if ($contact["network"] == "dspr")
+       if (sizeof($best_contact) == 0) {
+               foreach ($contacts as $contact) {
+                       if ($contact["network"] == "dspr") {
                                $best_contact = array($contact);
+                       }
+               }
+       }
 
-       if (sizeof($best_contact) == 0)
-               foreach ($contacts as $contact)
-                       if ($contact["network"] == "stat")
+       if (sizeof($best_contact) == 0) {
+               foreach ($contacts as $contact) {
+                       if ($contact["network"] == "stat") {
                                $best_contact = array($contact);
+                       }
+               }
+       }
 
-       if (sizeof($best_contact) == 0)
-               foreach ($contacts as $contact)
-                       if ($contact["network"] == "pump")
+       if (sizeof($best_contact) == 0) {
+               foreach ($contacts as $contact) {
+                       if ($contact["network"] == "pump") {
                                $best_contact = array($contact);
+                       }
+               }
+       }
 
-       if (sizeof($best_contact) == 0)
-               foreach ($contacts as $contact)
-                       if ($contact["network"] == "twit")
+       if (sizeof($best_contact) == 0) {
+               foreach ($contacts as $contact) {
+                       if ($contact["network"] == "twit") {
                                $best_contact = array($contact);
+                       }
+               }
+       }
 
        if (sizeof($best_contact) == 1) {
                $contacts = $best_contact;
@@ -4619,7 +4971,9 @@ function api_friendica_group_show($type)
 {
        $a = get_app();
 
-       if (api_user() === false) throw new ForbiddenException();
+       if (api_user() === false) {
+               throw new ForbiddenException();
+       }
 
        // params
        $user_info = api_get_user($a);
@@ -4634,8 +4988,9 @@ function api_friendica_group_show($type)
                        intval($gid)
                );
                // error message if specified gid is not in database
-               if (!DBM::is_result($r))
+               if (!DBM::is_result($r)) {
                        throw new BadRequestException("gid not available");
+               }
        } else {
                $r = q(
                        "SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d",
@@ -4730,7 +5085,9 @@ function api_friendica_group_create($type)
 {
        $a = get_app();
 
-       if (api_user() === false) throw new ForbiddenException();
+       if (api_user() === false) {
+               throw new ForbiddenException();
+       }
 
        // params
        $user_info = api_get_user($a);
@@ -4740,8 +5097,9 @@ function api_friendica_group_create($type)
        $users = $json['user'];
 
        // error if no name specified
-       if ($name == "")
+       if ($name == "") {
                throw new BadRequestException('group name not specified');
+       }
 
        // get data of the specified group name
        $rname = q(
@@ -4750,8 +5108,9 @@ function api_friendica_group_create($type)
                dbesc($name)
        );
        // error message if specified group name already exists
-       if (DBM::is_result($rname))
+       if (DBM::is_result($rname)) {
                throw new BadRequestException('group name already exists');
+       }
 
        // check if specified group name is a deleted group
        $rname = q(
@@ -4760,8 +5119,9 @@ function api_friendica_group_create($type)
                dbesc($name)
        );
        // error message if specified group name already exists
-       if (DBM::is_result($rname))
+       if (DBM::is_result($rname)) {
                $reactivate_group = true;
+       }
 
        // create group
        $ret = Group::create($uid, $name);
@@ -4782,9 +5142,9 @@ function api_friendica_group_create($type)
                        intval($cid),
                        intval($uid)
                );
-               if (count($contact))
+               if (count($contact)) {
                        $result = Group::addMember($gid, $cid);
-               else {
+               else {
                        $erroraddinguser = true;
                        $errorusers[] = $cid;
                }
@@ -4803,7 +5163,9 @@ function api_friendica_group_update($type)
 {
        $a = get_app();
 
-       if (api_user() === false) throw new ForbiddenException();
+       if (api_user() === false) {
+               throw new ForbiddenException();
+       }
 
        // params
        $user_info = api_get_user($a);
@@ -4814,12 +5176,14 @@ function api_friendica_group_update($type)
        $users = $json['user'];
 
        // error if no name specified
-       if ($name == "")
+       if ($name == "") {
                throw new BadRequestException('group name not specified');
+       }
 
        // error if no gid specified
-       if ($gid == "")
+       if ($gid == "") {
                throw new BadRequestException('gid not specified');
+       }
 
        // remove members
        $members = Contact::getByGroupId($gid);
@@ -4865,7 +5229,9 @@ function api_friendica_activity($type)
 {
        $a = get_app();
 
-       if (api_user() === false) throw new ForbiddenException();
+       if (api_user() === false) {
+               throw new ForbiddenException();
+       }
        $verb = strtolower($a->argv[3]);
        $verb = preg_replace("|\..*$|", "", $verb);
 
@@ -4907,16 +5273,21 @@ function api_friendica_notification($type)
 {
        $a = get_app();
 
-       if (api_user() === false) throw new ForbiddenException();
-       if ($a->argc!==3) throw new BadRequestException("Invalid argument count");
+       if (api_user() === false) {
+               throw new ForbiddenException();
+       }
+       if ($a->argc!==3) {
+               throw new BadRequestException("Invalid argument count");
+       }
        $nm = new NotificationsManager();
 
        $notes = $nm->getAll(array(), "+seen -date", 50);
 
        if ($type == "xml") {
                $xmlnotes = array();
-               foreach ($notes as $note)
+               foreach ($notes as $note) {
                        $xmlnotes[] = array("@attributes" => $note);
+               }
 
                $notes = $xmlnotes;
        }
@@ -4936,14 +5307,20 @@ function api_friendica_notification_seen($type)
 {
        $a = get_app();
 
-       if (api_user() === false) throw new ForbiddenException();
-       if ($a->argc!==4) throw new BadRequestException("Invalid argument count");
+       if (api_user() === false) {
+               throw new ForbiddenException();
+       }
+       if ($a->argc!==4) {
+               throw new BadRequestException("Invalid argument count");
+       }
 
        $id = (x($_REQUEST, 'id') ? intval($_REQUEST['id']) : 0);
 
        $nm = new NotificationsManager();
        $note = $nm->getByID($id);
-       if (is_null($note)) throw new BadRequestException("Invalid argument");
+       if (is_null($note)) {
+               throw new BadRequestException("Invalid argument");
+       }
 
        $nm->setSeen($note);
        if ($note['otype']=='item') {
@@ -5166,18 +5543,49 @@ function api_friendica_profile_show($type)
 }
 api_register_func('api/friendica/profile/show', 'api_friendica_profile_show', true, API_METHOD_GET);
 
+/**
+ * Returns a list of saved searches.
+ *
+ * @see https://developer.twitter.com/en/docs/accounts-and-users/manage-account-settings/api-reference/get-saved_searches-list
+ *
+ * @param  string $type Return format: json or xml
+ *
+ * @return string|array
+ */
+function api_saved_searches_list($type)
+{
+       $terms = dba::select('search', array('id', 'term'), array('uid' => local_user()));
+
+       $result = array();
+       while ($term = $terms->fetch()) {
+               $result[] = array(
+                       'name' => $term['term'],
+                       'query' => $term['term'],
+                       'id_str' => $term['id'],
+                       'id' => intval($term['id'])
+               );
+       }
+
+       dba::close($terms);
+
+       return api_format_data("terms", $type, array('terms' => $result));
+}
+
+/// @TODO move to top of file or somwhere better
+api_register_func('api/saved_searches/list', 'api_saved_searches_list', true);
+
 /*
 @TODO Maybe open to implement?
 To.Do:
-    [pagename] => api/1.1/statuses/lookup.json
-    [id] => 605138389168451584
-    [include_cards] => true
-    [cards_platform] => Android-12
-    [include_entities] => true
-    [include_my_retweet] => 1
-    [include_rts] => 1
-    [include_reply_count] => true
-    [include_descendent_reply_count] => true
+       [pagename] => api/1.1/statuses/lookup.json
+       [id] => 605138389168451584
+       [include_cards] => true
+       [cards_platform] => Android-12
+       [include_entities] => true
+       [include_my_retweet] => 1
+       [include_rts] => 1
+       [include_reply_count] => true
+       [include_descendent_reply_count] => true
 (?)