]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
code style & use DBA::p
[friendica.git] / include / api.php
index 20b3844cd5cc93d7a96cf03f1b1f96a34a5920e7..c275dbf391bc65234629392fc125ef0e963d3368 100644 (file)
@@ -46,7 +46,6 @@ use Friendica\Util\Proxy as ProxyUtils;
 use Friendica\Util\Strings;
 use Friendica\Util\XML;
 
-require_once 'include/conversation.php';
 require_once 'mod/share.php';
 require_once 'mod/item.php';
 require_once 'mod/wall_upload.php';
@@ -431,7 +430,7 @@ function api_call(App $a)
  */
 function api_error($type, $e)
 {
-       $a = get_app();
+       $a = \get_app();
 
        $error = ($e->getMessage() !== "" ? $e->getMessage() : $e->httpdesc);
        /// @TODO:  https://dev.twitter.com/overview/api/response-codes
@@ -946,7 +945,6 @@ function api_format_data($root_element, $type, $data)
                        $ret = $data;
                        break;
        }
-
        return $ret;
 }
 
@@ -964,7 +962,7 @@ function api_format_data($root_element, $type, $data)
 function api_account_verify_credentials($type)
 {
 
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -1028,7 +1026,7 @@ function requestdata($k)
  */
 function api_statuses_mediap($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                Logger::log('api_statuses_update: no user');
@@ -1076,7 +1074,7 @@ api_register_func('api/statuses/mediap', 'api_statuses_mediap', true, API_METHOD
  */
 function api_statuses_update($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                Logger::log('api_statuses_update: no user');
@@ -1218,7 +1216,7 @@ api_register_func('api/statuses/update_with_media', 'api_statuses_update', true,
  */
 function api_media_upload()
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                Logger::log('no user');
@@ -1243,8 +1241,9 @@ function api_media_upload()
        $returndata["media_id_string"] = (string)$media["id"];
        $returndata["size"] = $media["size"];
        $returndata["image"] = ["w" => $media["width"],
-                                       "h" => $media["height"],
-                                       "image_type" => $media["type"]];
+                               "h" => $media["height"],
+                               "image_type" => $media["type"],
+                               "friendica_preview_url" => $media["preview"]];
 
        Logger::log("Media uploaded: " . print_r($returndata, true), Logger::DEBUG);
 
@@ -1262,7 +1261,7 @@ api_register_func('api/media/upload', 'api_media_upload', true, API_METHOD_POST)
  */
 function api_status_show($type, $item_id = 0)
 {
-       $a = get_app();
+       $a = \get_app();
 
        $user_info = api_get_user($a);
 
@@ -1361,7 +1360,7 @@ function api_status_show($type, $item_id = 0)
  */
 function api_users_show($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        $user_info = api_get_user($a);
 
@@ -1437,7 +1436,7 @@ api_register_func('api/externalprofile/show', 'api_users_show');
  */
 function api_users_search($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        $userlist = [];
 
@@ -1517,48 +1516,75 @@ api_register_func('api/users/lookup', 'api_users_lookup', true);
  */
 function api_search($type)
 {
-       $a = get_app();
+       $a = \get_app();
        $user_info = api_get_user($a);
 
-       if (api_user() === false || $user_info === false) {
-               throw new ForbiddenException();
-       }
-
-       $data = [];
+       if (api_user() === false || $user_info === false) { throw new ForbiddenException(); }
 
-       if (empty($_REQUEST['q'])) {
-               throw new BadRequestException("q parameter is required.");
-       }
+       if (empty($_REQUEST['q'])) { throw new BadRequestException("q parameter is required."); }
+       
+       $searchTerm = trim(rawurldecode($_REQUEST['q']));
 
+       $data = [];
+       $data['status'] = [];
+       $count = 15;
+       $exclude_replies = !empty($_REQUEST['exclude_replies']);
        if (!empty($_REQUEST['rpp'])) {
                $count = $_REQUEST['rpp'];
        } elseif (!empty($_REQUEST['count'])) {
                $count = $_REQUEST['count'];
-       } else {
-               $count = 15;
        }
-
+       
        $since_id = defaults($_REQUEST, 'since_id', 0);
        $max_id = defaults($_REQUEST, 'max_id', 0);
        $page = (!empty($_REQUEST['page']) ? $_REQUEST['page'] - 1 : 0);
-
        $start = $page * $count;
+       $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
+       if (preg_match('/^#(\w+)$/', $searchTerm, $matches) === 1 && isset($matches[1])) {
+               $searchTerm = $matches[1];
+               $condition = ["`oid` > ?
+                       AND (`uid` = 0 OR (`uid` = ? AND NOT `global`)) 
+                       AND `otype` = ? AND `type` = ? AND `term` = ?",
+                       $since_id, local_user(), TERM_OBJ_POST, TERM_HASHTAG, $searchTerm];
+               if ($max_id > 0) {
+                       $condition[0] .= " AND `oid` <= ?";
+                       $condition[] = $max_id;
+               }
+               $terms = DBA::select('term', ['oid'], $condition, []);
+               $itemIds = [];
+               while ( $term = DBA::fetch($terms) ) {
+                       $itemIds[] = $term['oid'];
+               }
+               DBA::close($terms);
 
-       $condition = ["`gravity` IN (?, ?) AND `item`.`id` > ?
-               AND (`item`.`uid` = 0 OR (`item`.`uid` = ? AND NOT `item`.`global`))
-               AND `item`.`body` LIKE CONCAT('%',?,'%')",
-               GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, api_user(), $_REQUEST['q']];
+               if (empty($itemIds)) {
+                       return api_format_data("statuses", $type, $data);
+               }
 
-       if ($max_id > 0) {
-               $condition[0] .= " AND `item`.`id` <= ?";
-               $condition[] = $max_id;
+               $tmpAr = ['`id` IN ('.implode(", ", $itemIds).')'];
+               if ($exclude_replies) {
+                       $tmpAr[] = "`id` = `parent`";
+               }
+
+               $condition = [ implode(" AND ", $tmpAr) ];
+       } else {
+               $condition = ["`id` > ? 
+                       " . ( $exclude_replies ? " AND `id` = `parent` " : ' ' ) . "
+                       AND (`uid` = 0 OR (`uid` = ? AND NOT `global`))
+                       AND `body` LIKE CONCAT('%',?,'%')",
+                       $since_id, api_user(), $_REQUEST['q']];
+               if ($max_id > 0) {
+                       $condition[0] .= " AND `id` <= ?";
+                       $condition[] = $max_id;
+               }
        }
 
-       $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
        $statuses = Item::selectForUser(api_user(), [], $condition, $params);
 
        $data['status'] = api_format_items(Item::inArray($statuses), $user_info);
 
+       bindComments($data['status']);
+
        return api_format_data("statuses", $type, $data);
 }
 
@@ -1578,7 +1604,7 @@ api_register_func('api/search', 'api_search', true);
  */
 function api_statuses_home_timeline($type)
 {
-       $a = get_app();
+       $a = \get_app();
        $user_info = api_get_user($a);
 
        if (api_user() === false || $user_info === false) {
@@ -1640,6 +1666,8 @@ function api_statuses_home_timeline($type)
                        Item::update(['unseen' => false], ['unseen' => true, 'id' => $idarray]);
                }
        }
+       
+       bindComments($ret);
 
        $data = ['status' => $ret];
        switch ($type) {
@@ -1653,6 +1681,7 @@ function api_statuses_home_timeline($type)
        return api_format_data("statuses", $type, $data);
 }
 
+
 /// @TODO move to top of file or somewhere better
 api_register_func('api/statuses/home_timeline', 'api_statuses_home_timeline', true);
 api_register_func('api/statuses/friends_timeline', 'api_statuses_home_timeline', true);
@@ -1666,7 +1695,7 @@ api_register_func('api/statuses/friends_timeline', 'api_statuses_home_timeline',
  */
 function api_statuses_public_timeline($type)
 {
-       $a = get_app();
+       $a = \get_app();
        $user_info = api_get_user($a);
 
        if (api_user() === false || $user_info === false) {
@@ -1722,6 +1751,8 @@ function api_statuses_public_timeline($type)
 
        $ret = api_format_items($r, $user_info, false, $type);
 
+       bindComments($ret);
+
        $data = ['status' => $ret];
        switch ($type) {
                case "atom":
@@ -1748,7 +1779,7 @@ api_register_func('api/statuses/public_timeline', 'api_statuses_public_timeline'
  */
 function api_statuses_networkpublic_timeline($type)
 {
-       $a = get_app();
+       $a = \get_app();
        $user_info = api_get_user($a);
 
        if (api_user() === false || $user_info === false) {
@@ -1779,6 +1810,8 @@ function api_statuses_networkpublic_timeline($type)
 
        $ret = api_format_items(Item::inArray($statuses), $user_info, false, $type);
 
+       bindComments($ret);
+
        $data = ['status' => $ret];
        switch ($type) {
                case "atom":
@@ -1803,7 +1836,7 @@ api_register_func('api/statuses/networkpublic_timeline', 'api_statuses_networkpu
  */
 function api_statuses_show($type)
 {
-       $a = get_app();
+       $a = \get_app();
        $user_info = api_get_user($a);
 
        if (api_user() === false || $user_info === false) {
@@ -1876,7 +1909,7 @@ api_register_func('api/statuses/show', 'api_statuses_show', true);
  */
 function api_conversation_show($type)
 {
-       $a = get_app();
+       $a = \get_app();
        $user_info = api_get_user($a);
 
        if (api_user() === false || $user_info === false) {
@@ -1955,7 +1988,7 @@ function api_statuses_repeat($type)
 {
        global $called_api;
 
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -2020,7 +2053,7 @@ api_register_func('api/statuses/retweet', 'api_statuses_repeat', true, API_METHO
  */
 function api_statuses_destroy($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -2061,7 +2094,7 @@ api_register_func('api/statuses/destroy', 'api_statuses_destroy', true, API_METH
  */
 function api_statuses_mentions($type)
 {
-       $a = get_app();
+       $a = \get_app();
        $user_info = api_get_user($a);
 
        if (api_user() === false || $user_info === false) {
@@ -2129,7 +2162,7 @@ api_register_func('api/statuses/replies', 'api_statuses_mentions', true);
  */
 function api_statuses_user_timeline($type)
 {
-       $a = get_app();
+       $a = \get_app();
        $user_info = api_get_user($a);
 
        if (api_user() === false || $user_info === false) {
@@ -2182,6 +2215,8 @@ function api_statuses_user_timeline($type)
 
        $ret = api_format_items(Item::inArray($statuses), $user_info, true, $type);
 
+       bindComments($ret);
+
        $data = ['status' => $ret];
        switch ($type) {
                case "atom":
@@ -2207,7 +2242,7 @@ api_register_func('api/statuses/user_timeline', 'api_statuses_user_timeline', tr
  */
 function api_favorites_create_destroy($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -2285,7 +2320,7 @@ function api_favorites($type)
 {
        global $called_api;
 
-       $a = get_app();
+       $a = \get_app();
        $user_info = api_get_user($a);
 
        if (api_user() === false || $user_info === false) {
@@ -2327,6 +2362,8 @@ function api_favorites($type)
                $ret = api_format_items(Item::inArray($statuses), $user_info, false, $type);
        }
 
+       bindComments($ret);
+
        $data = ['status' => $ret];
        switch ($type) {
                case "atom":
@@ -2738,7 +2775,7 @@ function api_contactlink_to_array($txt)
  */
 function api_format_items_activities($item, $type = "json")
 {
-       $a = get_app();
+       $a = \get_app();
 
        $activities = [
                'like' => [],
@@ -2860,11 +2897,11 @@ function api_format_items_profiles($profile_row)
  */
 function api_format_items($r, $user_info, $filter_user = false, $type = "json")
 {
-       $a = get_app();
+       $a = \get_app();
 
        $ret = [];
 
-       foreach ($r as $item) {
+       foreach ((array)$r as $item) {
                localize_item($item);
                list($status_user, $owner_user) = api_item_get_user($a, $item);
 
@@ -3050,7 +3087,7 @@ api_register_func('api/lists/subscriptions', 'api_lists_list', true);
  */
 function api_lists_ownerships($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -3094,7 +3131,7 @@ api_register_func('api/lists/ownerships', 'api_lists_ownerships', true);
  */
 function api_lists_statuses($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        $user_info = api_get_user($a);
        if (api_user() === false || $user_info === false) {
@@ -3171,7 +3208,7 @@ api_register_func('api/lists/statuses', 'api_lists_statuses', true);
  */
 function api_statuses_f($qtype)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -3347,7 +3384,7 @@ api_register_func('api/friendships/incoming', 'api_friendships_incoming', true);
  */
 function api_statusnet_config($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        $name      = Config::get('config', 'sitename');
        $server    = $a->getHostName();
@@ -3411,7 +3448,7 @@ function api_ff_ids($type)
                throw new ForbiddenException();
        }
 
-       $a = get_app();
+       $a = \get_app();
 
        api_get_user($a);
 
@@ -3479,7 +3516,7 @@ api_register_func('api/followers/ids', 'api_followers_ids', true);
  */
 function api_direct_messages_new($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -3567,7 +3604,7 @@ api_register_func('api/direct_messages/new', 'api_direct_messages_new', true, AP
  */
 function api_direct_messages_destroy($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -3725,7 +3762,7 @@ api_register_func('api/friendships/destroy', 'api_friendships_destroy', true, AP
  */
 function api_direct_messages_box($type, $box, $verbose)
 {
-       $a = get_app();
+       $a = \get_app();
        if (api_user() === false) {
                throw new ForbiddenException();
        }
@@ -3962,7 +3999,7 @@ function api_fr_photoalbum_delete($type)
        }
 
        // now let's delete all photos from the album
-       $result = DBA::delete('photo', ['uid' => api_user(), 'album' => $album]);
+       $result = Photo::delete(['uid' => api_user(), 'album' => $album]);
 
        // return success of deletion or error message
        if ($result) {
@@ -3996,11 +4033,11 @@ function api_fr_photoalbum_update($type)
                throw new BadRequestException("no new albumname specified");
        }
        // check if album is existing
-       if (!DBA::exists('photo', ['uid' => api_user(), 'album' => $album])) {
+       if (!Photo::exists(['uid' => api_user(), 'album' => $album])) {
                throw new BadRequestException("album not available");
        }
        // now let's update all photos to the albumname
-       $result = DBA::update('photo', ['album' => $album_new], ['uid' => api_user(), 'album' => $album]);
+       $result = Photo::update(['album' => $album_new], ['uid' => api_user(), 'album' => $album]);
 
        // return success of updating or error message
        if ($result) {
@@ -4100,14 +4137,8 @@ function api_fr_photo_create_update($type)
        } else {
                $mode = "update";
 
-               // check if photo is existing in database
-               $r = q(
-                       "SELECT `id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' AND `album` = '%s'",
-                       intval(api_user()),
-                       DBA::escape($photo_id),
-                       DBA::escape($album)
-               );
-               if (!DBA::isResult($r)) {
+               // check if photo is existing in databasei
+               if (!Photo::exists(['resource-id' => $photo_id, 'uid' => api_user(), 'album' => $album])) {
                        throw new BadRequestException("photo not available");
                }
        }
@@ -4136,47 +4167,40 @@ function api_fr_photo_create_update($type)
 
        // now let's do the changes in update-mode
        if ($mode == "update") {
-               $sql_extra = "";
+               $updated_fields = [];
 
                if (!is_null($desc)) {
-                       $sql_extra .= (($sql_extra != "") ? " ," : "") . "`desc` = '$desc'";
+                       $updated_fields['desc'] = $desc;
                }
 
                if (!is_null($album_new)) {
-                       $sql_extra .= (($sql_extra != "") ? " ," : "") . "`album` = '$album_new'";
+                       $updated_fields['album'] = $album_new;
                }
 
                if (!is_null($allow_cid)) {
                        $allow_cid = trim($allow_cid);
-                       $sql_extra .= (($sql_extra != "") ? " ," : "") . "`allow_cid` = '$allow_cid'";
+                       $updated_fields['allow_cid'] = $allow_cid;
                }
 
                if (!is_null($deny_cid)) {
                        $deny_cid = trim($deny_cid);
-                       $sql_extra .= (($sql_extra != "") ? " ," : "") . "`deny_cid` = '$deny_cid'";
+                       $updated_fields['deny_cid'] = $deny_cid;
                }
 
                if (!is_null($allow_gid)) {
                        $allow_gid = trim($allow_gid);
-                       $sql_extra .= (($sql_extra != "") ? " ," : "") . "`allow_gid` = '$allow_gid'";
+                       $updated_fields['allow_gid'] = $allow_gid;
                }
 
                if (!is_null($deny_gid)) {
                        $deny_gid = trim($deny_gid);
-                       $sql_extra .= (($sql_extra != "") ? " ," : "") . "`deny_gid` = '$deny_gid'";
+                       $updated_fields['deny_gid'] = $deny_gid;
                }
 
                $result = false;
-               if ($sql_extra != "") {
+               if (count($updated_fields) > 0) {
                        $nothingtodo = false;
-                       $result = q(
-                               "UPDATE `photo` SET %s, `edited`='%s' WHERE `uid` = %d AND `resource-id` = '%s' AND `album` = '%s'",
-                               $sql_extra,
-                               DateTimeFormat::utcNow(),   // update edited timestamp
-                               intval(api_user()),
-                               DBA::escape($photo_id),
-                               DBA::escape($album)
-                       );
+                       $result = Photo::update($updated_fields, ['uid' => api_user(), 'resource-id' => $photo_id, 'album' => $album]);
                } else {
                        $nothingtodo = true;
                }
@@ -4225,16 +4249,12 @@ function api_fr_photo_delete($type)
                throw new BadRequestException("no photo_id specified");
        }
        // check if photo is existing in database
-       $r = q(
-               "SELECT `id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'",
-               intval(api_user()),
-               DBA::escape($photo_id)
-       );
-       if (!DBA::isResult($r)) {
+       $r = Photo::exists(['resource-id' => $photo_id, 'uid' => api_user()]);
+       if (!$r) {
                throw new BadRequestException("photo not available");
        }
        // now we can perform on the deletion of the photo
-       $result = DBA::delete('photo', ['uid' => api_user(), 'resource-id' => $photo_id]);
+       $result = Photo::delete(['uid' => api_user(), 'resource-id' => $photo_id]);
 
        // return success of deletion or error message
        if ($result) {
@@ -4344,7 +4364,7 @@ function api_account_update_profile_image($type)
        // change specified profile or all profiles to the new resource-id
        if ($is_default_profile) {
                $condition = ["`profile` AND `resource-id` != ? AND `uid` = ?", $data['photo']['id'], api_user()];
-               DBA::update('photo', ['profile' => false], $condition);
+               Photo::update(['profile' => false], $condition);
        } else {
                $fields = ['photo' => System::baseUrl() . '/photo/' . $data['photo']['id'] . '-4.' . $filetype,
                        'thumb' => System::baseUrl() . '/photo/' . $data['photo']['id'] . '-5.' . $filetype];
@@ -4354,7 +4374,7 @@ function api_account_update_profile_image($type)
        Contact::updateSelfFromUserID(api_user(), true);
 
        // Update global directory in background
-       $url = System::baseUrl() . '/profile/' . get_app()->user['nickname'];
+       $url = System::baseUrl() . '/profile/' . \get_app()->user['nickname'];
        if ($url && strlen(Config::get('system', 'directory'))) {
                Worker::add(PRIORITY_LOW, "Directory", $url);
        }
@@ -4669,7 +4689,7 @@ function post_photo_item($hash, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $f
  */
 function prepare_photo_data($type, $scale, $photo_id)
 {
-       $a = get_app();
+       $a = \get_app();
        $user_info = api_get_user($a);
 
        if ($user_info === false) {
@@ -5210,7 +5230,7 @@ function api_best_nickname(&$contacts)
  */
 function api_friendica_group_show($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -5275,7 +5295,7 @@ api_register_func('api/friendica/group_show', 'api_friendica_group_show', true);
  */
 function api_friendica_group_delete($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -5337,7 +5357,7 @@ api_register_func('api/friendica/group_delete', 'api_friendica_group_delete', tr
  */
 function api_lists_destroy($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -5453,7 +5473,7 @@ function group_create($name, $uid, $users = [])
  */
 function api_friendica_group_create($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -5482,7 +5502,7 @@ api_register_func('api/friendica/group_create', 'api_friendica_group_create', tr
  */
 function api_lists_create($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -5516,7 +5536,7 @@ api_register_func('api/lists/create', 'api_lists_create', true, API_METHOD_POST)
  */
 function api_friendica_group_update($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -5590,7 +5610,7 @@ api_register_func('api/friendica/group_update', 'api_friendica_group_update', tr
  */
 function api_lists_update($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -5636,7 +5656,7 @@ api_register_func('api/lists/update', 'api_lists_update', true, API_METHOD_POST)
  */
 function api_friendica_activity($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -5680,7 +5700,7 @@ api_register_func('api/friendica/activity/unattendmaybe', 'api_friendica_activit
 */
 function api_friendica_notification($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -5702,7 +5722,6 @@ function api_friendica_notification($type)
 
                $notes = $xmlnotes;
        }
-
        return api_format_data("notes", $type, ['note' => $notes]);
 }
 
@@ -5716,7 +5735,7 @@ function api_friendica_notification($type)
  */
 function api_friendica_notification_seen($type)
 {
-       $a = get_app();
+       $a = \get_app();
        $user_info = api_get_user($a);
 
        if (api_user() === false || $user_info === false) {
@@ -5761,7 +5780,7 @@ api_register_func('api/friendica/notification', 'api_friendica_notification', tr
  */
 function api_friendica_direct_messages_setseen($type)
 {
-       $a = get_app();
+       $a = \get_app();
        if (api_user() === false) {
                throw new ForbiddenException();
        }
@@ -5810,7 +5829,7 @@ api_register_func('api/friendica/direct_messages_setseen', 'api_friendica_direct
  */
 function api_friendica_direct_messages_search($type, $box = "")
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -5873,7 +5892,7 @@ api_register_func('api/friendica/direct_messages_search', 'api_friendica_direct_
  */
 function api_friendica_profile_show($type)
 {
-       $a = get_app();
+       $a = \get_app();
 
        if (api_user() === false) {
                throw new ForbiddenException();
@@ -5976,6 +5995,42 @@ function api_saved_searches_list($type)
 /// @TODO move to top of file or somewhere better
 api_register_func('api/saved_searches/list', 'api_saved_searches_list', true);
 
+/*
+ * Bind comment numbers(friendica_comments: Int) on each statuses page of *_timeline / favorites / search
+ *
+ * @brief Number of comments
+ *
+ * @param object $data [Status, Status]
+ *
+ * @return void
+ */
+function bindComments(&$data) 
+{
+       if (count($data) == 0) {
+               return;
+       }
+       
+       $ids = [];
+       $comments = [];
+       foreach ($data as $item) {
+               $ids[] = $item['id'];
+       }
+
+       $idStr = DBA::escape(implode(", ", $ids));
+       $sql = "SELECT `parent`, COUNT(*) as comments FROM `item` WHERE `parent` IN ($idStr) AND `deleted` = ? AND `gravity`= ? GROUP BY `parent`";
+       $items = DBA::p($sql, 0, GRAVITY_COMMENT);
+       $itemsData = DBA::toArray($items);
+
+       foreach ($itemsData as $item) {
+               $comments[$item['parent']] = $item['comments'];
+       }
+
+       foreach ($data as $idx => $item) {
+               $id = $item['id'];
+               $data[$idx]['friendica_comments'] = isset($comments[$id]) ? $comments[$id] : 0;
+       }
+}
+
 /*
 @TODO Maybe open to implement?
 To.Do: