]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
refs #6292
[friendica.git] / include / api.php
index 8a89d00d773cd7ebc354bc5d2c1f44c30e9b4484..fe5cc9168aab6498e9718e0d5907ff7680f6e478 100644 (file)
@@ -945,7 +945,6 @@ function api_format_data($root_element, $type, $data)
                        $ret = $data;
                        break;
        }
-
        return $ret;
 }
 
@@ -1244,7 +1243,7 @@ function api_media_upload()
        $returndata["image"] = ["w" => $media["width"],
                                "h" => $media["height"],
                                "image_type" => $media["type"],
-                               "preview" => $media["preview"]];
+                               "friendica_preview_url" => $media["preview"]];
 
        Logger::log("Media uploaded: " . print_r($returndata, true), Logger::DEBUG);
 
@@ -1520,45 +1519,62 @@ function api_search($type)
        $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 = [];
+       $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 = [$exclude_replies ? "`id` = `parent` AND " : ''];
+               $condition[0] .= empty($itemIds) ? '' : ' `id` IN ('.implode(", ", $itemIds).')' ;
 
-       $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']];
+       } 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;
+               }
 
-       if ($max_id > 0) {
-               $condition[0] .= " AND `item`.`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);
 }
 
@@ -1640,6 +1656,8 @@ function api_statuses_home_timeline($type)
                        Item::update(['unseen' => false], ['unseen' => true, 'id' => $idarray]);
                }
        }
+       
+       bindComments($ret);
 
        $data = ['status' => $ret];
        switch ($type) {
@@ -1653,6 +1671,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);
@@ -1722,6 +1741,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":
@@ -1779,6 +1800,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":
@@ -2182,6 +2205,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":
@@ -2327,6 +2352,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":
@@ -2864,7 +2891,7 @@ function api_format_items($r, $user_info, $filter_user = false, $type = "json")
 
        $ret = [];
 
-       foreach ($r as $item) {
+       foreach ((array)$r as $item) {
                localize_item($item);
                list($status_user, $owner_user) = api_item_get_user($a, $item);
 
@@ -5685,7 +5712,6 @@ function api_friendica_notification($type)
 
                $notes = $xmlnotes;
        }
-
        return api_format_data("notes", $type, ['note' => $notes]);
 }
 
@@ -5959,6 +5985,36 @@ 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']; }
+
+       $sql = "SELECT `parent`,COUNT(*) as comments FROM `item` 
+               WHERE `parent` IN ( %s ) AND `deleted` = %d AND `gravity`= %d GROUP BY `parent`";
+       $result = q($sql, implode(",", $ids), 0, GRAVITY_COMMENT);
+
+       foreach($result as $records) {
+               $comments[$records['parent']] = $records['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: