]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
Merge pull request #10160 from annando/issue-10156
[friendica.git] / include / api.php
index 8543eef9e7d00928e226adf7ada02557f216e214..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
  *
@@ -43,7 +43,6 @@ use Friendica\Model\Notification;
 use Friendica\Model\Photo;
 use Friendica\Model\Post;
 use Friendica\Model\User;
-use Friendica\Model\UserItem;
 use Friendica\Model\Verb;
 use Friendica\Network\HTTPException;
 use Friendica\Network\HTTPException\BadRequestException;
@@ -1119,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);
@@ -1133,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);
@@ -1147,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);
@@ -1732,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 {
@@ -1811,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);
 
@@ -2170,16 +2169,15 @@ function api_statuses_mentions($type)
 
        $start = max(0, ($page - 1) * $count);
 
-       $query = "`gravity` IN (?, ?) AND `uri-id` IN (SELECT `uri-id` FROM `post-user`
-               WHERE (`hidden` IS NULL OR NOT `hidden`) AND
-                       `uid` = ? AND `notification-type` & ? != 0)
-                       AND `id` > ?";
+       $query = "`gravity` IN (?, ?) AND `uri-id` IN
+               (SELECT `uri-id` FROM `post-user-notification` WHERE `uid` = ? AND `notification-type` & ? != 0 ORDER BY `uri-id`)
+               AND (`uid` = 0 OR (`uid` = ? AND NOT `global`)) AND `id` > ?";
 
        $condition = [GRAVITY_PARENT, GRAVITY_COMMENT, api_user(),
-               UserItem::NOTIF_EXPLICIT_TAGGED | UserItem::NOTIF_IMPLICIT_TAGGED |
-               UserItem::NOTIF_THREAD_COMMENT | UserItem::NOTIF_DIRECT_COMMENT |
-               UserItem::NOTIF_DIRECT_THREAD_COMMENT,
-               $since_id];
+               Post\UserNotification::NOTIF_EXPLICIT_TAGGED | Post\UserNotification::NOTIF_IMPLICIT_TAGGED |
+               Post\UserNotification::NOTIF_THREAD_COMMENT | Post\UserNotification::NOTIF_DIRECT_COMMENT |
+               Post\UserNotification::NOTIF_DIRECT_THREAD_COMMENT,
+               api_user(), $since_id];
 
        if ($max_id > 0) {
                $query .= " AND `id` <= ?";
@@ -2264,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);
 
@@ -2933,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);
 
@@ -4871,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.');
@@ -4880,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);
@@ -6006,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);