]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
Throw NotFoundException if results are empty in api_users_lookup
[friendica.git] / include / api.php
index 2cf79fa5aa4a066a5467ea6bcb07d0c3df8ad355..6201b460a3c162e6ce96d68ea9660740568a35ca 100644 (file)
@@ -1485,6 +1485,39 @@ 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 UnauthorizedException
+ * @throws NotFoundException
+ */
+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.
  *
@@ -1526,21 +1559,16 @@ function api_search($type)
                "SELECT ".item_fieldlists()."
                FROM `item` ".item_joins()."
                WHERE ".item_condition()." AND (`item`.`uid` = 0 OR (`item`.`uid` = ? AND NOT `item`.`global`))
-               AND `item`.`body` REGEXP ?
+               AND `item`.`body` LIKE CONCAT('%',?,'%')
                $sql_extra
                AND `item`.`id`>?
                ORDER BY `item`.`id` DESC LIMIT ".intval($start)." ,".intval($count)." ",
-               intval(api_user()),
+               api_user(),
                $_REQUEST['q'],
-               intval($since_id)
+               $since_id
        );
 
-       $statuses = array();
-       while ($row = dba::fetch($r)) {
-               $statuses[] = $row;
-       }
-
-       $data['status'] = api_format_items($statuses, api_get_user(get_app()));
+       $data['status'] = api_format_items(dba::inArray($r), api_get_user(get_app()));
 
        return api_format_data("statuses", $type, $data);
 }
@@ -3119,12 +3147,20 @@ function api_statuses_f($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_filter
                $sql_extra
                ORDER BY `nick`
                LIMIT %d, %d",
@@ -3187,6 +3223,56 @@ 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
+ * @throws UnauthorizedException
+ */
+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
+ * @throws UnauthorizedException
+ */
+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();