]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
api_format_data can also return an array
[friendica.git] / include / api.php
index 2aa8fc645cf1816a24dcbd5d9ce36c1405e15dd1..b6181ea1a4d0c1252bf58d5e87dad42d8b3f68b5 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;
@@ -398,7 +399,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)
 {
@@ -779,6 +780,37 @@ function api_get_user(App $a, $contact_id = null)
                '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;
 }
 
@@ -914,12 +946,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":
@@ -1485,6 +1515,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
@@ -3055,12 +3175,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",
@@ -3123,6 +3251,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();
@@ -5308,6 +5484,37 @@ 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: