]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
Merge pull request #10134 from nupplaphil/feat/php-cs_fix
[friendica.git] / include / api.php
index 4c90ab80dc1083cf363c6ebefd1473f310f59462..115869fe27baeb96ba65138e868e62a23cb7433f 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -1118,8 +1118,8 @@ function api_statuses_update($type)
                if ($throttle_day > 0) {
                        $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60);
 
-                       $condition = ["`uid` = ? AND `wall` AND `received` > ?", api_user(), $datefrom];
-                       $posts_day = DBA::count('thread', $condition);
+                       $condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, api_user(), $datefrom];
+                       $posts_day = Post::count($condition);
 
                        if ($posts_day > $throttle_day) {
                                Logger::log('Daily posting limit reached for user '.api_user(), Logger::DEBUG);
@@ -1132,8 +1132,8 @@ function api_statuses_update($type)
                if ($throttle_week > 0) {
                        $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*7);
 
-                       $condition = ["`uid` = ? AND `wall` AND `received` > ?", api_user(), $datefrom];
-                       $posts_week = DBA::count('thread', $condition);
+                       $condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, api_user(), $datefrom];
+                       $posts_week = Post::count($condition);
 
                        if ($posts_week > $throttle_week) {
                                Logger::log('Weekly posting limit reached for user '.api_user(), Logger::DEBUG);
@@ -1146,8 +1146,8 @@ function api_statuses_update($type)
                if ($throttle_month > 0) {
                        $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*30);
 
-                       $condition = ["`uid` = ? AND `wall` AND `received` > ?", api_user(), $datefrom];
-                       $posts_month = DBA::count('thread', $condition);
+                       $condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, api_user(), $datefrom];
+                       $posts_month = Post::count($condition);
 
                        if ($posts_month > $throttle_month) {
                                Logger::log('Monthly posting limit reached for user '.api_user(), Logger::DEBUG);
@@ -1731,16 +1731,16 @@ function api_statuses_public_timeline($type)
        $start = max(0, ($page - 1) * $count);
 
        if ($exclude_replies && !$conversation_id) {
-               $condition = ["`gravity` IN (?, ?) AND `iid` > ? AND `private` = ? AND `wall` AND NOT `author-hidden`",
-                       GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, Item::PUBLIC];
+               $condition = ["`gravity` = ? AND `id` > ? AND `private` = ? AND `wall` AND NOT `author-hidden`",
+                       GRAVITY_PARENT, $since_id, Item::PUBLIC];
 
                if ($max_id > 0) {
-                       $condition[0] .= " AND `iid` <= ?";
+                       $condition[0] .= " AND `id` <= ?";
                        $condition[] = $max_id;
                }
 
-               $params = ['order' => ['iid' => true], 'limit' => [$start, $count]];
-               $statuses = Post::selectThreadForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params);
+               $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
+               $statuses = Post::selectForUser(api_user(), [], $condition, $params);
 
                $r = Post::toArray($statuses);
        } else {
@@ -1810,18 +1810,18 @@ function api_statuses_networkpublic_timeline($type)
 
        $start = max(0, ($page - 1) * $count);
 
-       $condition = ["`uid` = 0 AND `gravity` IN (?, ?) AND `iid` > ? AND `private` = ?",
+       $condition = ["`uid` = 0 AND `gravity` IN (?, ?) AND `id` > ? AND `private` = ?",
                GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, Item::PUBLIC];
 
        if ($max_id > 0) {
-               $condition[0] .= " AND `iid` <= ?";
+               $condition[0] .= " AND `id` <= ?";
                $condition[] = $max_id;
        }
 
-       $params = ['order' => ['iid' => true], 'limit' => [$start, $count]];
-       $statuses = Post::selectThreadForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params);
+       $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
+       $statuses = Post::toArray(Post::selectForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params));
 
-       $ret = api_format_items(Post::toArray($statuses), $user_info, false, $type);
+       $ret = api_format_items($statuses, $user_info, false, $type);
 
        bindComments($ret);
 
@@ -2262,7 +2262,6 @@ function api_statuses_user_timeline($type)
                $condition[0] .= " AND `id` <= ?";
                $condition[] = $max_id;
        }
-
        $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
        $statuses = Post::selectForUser(api_user(), [], $condition, $params);
 
@@ -2931,6 +2930,10 @@ function api_format_items($items, $user_info, $filter_user = false, $type = "jso
 
        $ret = [];
 
+       if (empty($items)) {
+               return $ret;
+       }
+
        foreach ((array)$items as $item) {
                list($status_user, $author_user, $owner_user) = api_item_get_user($a, $item);
 
@@ -4869,7 +4872,7 @@ function prepare_photo_data($type, $scale, $photo_id)
        }
 
        // retrieve item element for getting activities (like, dislike etc.) related to photo
-       $condition = ['uid' => api_user(), 'resource-id' => $photo_id, 'type' => 'photo'];
+       $condition = ['uid' => api_user(), 'resource-id' => $photo_id];
        $item = Post::selectFirst(['id', 'uid', 'uri', 'parent', 'allow_cid', 'deny_cid', 'allow_gid', 'deny_gid'], $condition);
        if (!DBA::isResult($item)) {
                throw new NotFoundException('Photo-related item not found.');
@@ -4878,7 +4881,7 @@ function prepare_photo_data($type, $scale, $photo_id)
        $data['photo']['friendica_activities'] = api_format_items_activities($item, $type);
 
        // retrieve comments on photo
-       $condition = ["`parent` = ? AND `uid` = ? AND (`gravity` IN (?, ?) OR `type`='photo')",
+       $condition = ["`parent` = ? AND `uid` = ? AND `gravity` IN (?, ?)",
                $item['parent'], api_user(), GRAVITY_PARENT, GRAVITY_COMMENT];
 
        $statuses = Post::selectForUser(api_user(), [], $condition);
@@ -6004,7 +6007,7 @@ function bindComments(&$data)
        }
 
        $idStr = DBA::escape(implode(', ', $ids));
-       $sql = "SELECT `parent`, COUNT(*) as comments FROM `post-view` WHERE `parent` IN ($idStr) AND `deleted` = ? AND `gravity`= ? GROUP BY `parent`";
+       $sql = "SELECT `parent`, COUNT(*) as comments FROM `post-user-view` WHERE `parent` IN ($idStr) AND `deleted` = ? AND `gravity`= ? GROUP BY `parent`";
        $items = DBA::p($sql, 0, GRAVITY_COMMENT);
        $itemsData = DBA::toArray($items);