]> 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 1332a535855c3ae5030c2acf0d0b53d0a39cd345..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
  *
@@ -39,13 +39,11 @@ use Friendica\Model\Contact;
 use Friendica\Model\Group;
 use Friendica\Model\Item;
 use Friendica\Model\Mail;
-use Friendica\Model\Notify;
+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\Security\FKOAuth1;
 use Friendica\Network\HTTPException;
 use Friendica\Network\HTTPException\BadRequestException;
 use Friendica\Network\HTTPException\ExpectationFailedException;
@@ -53,12 +51,12 @@ use Friendica\Network\HTTPException\ForbiddenException;
 use Friendica\Network\HTTPException\InternalServerErrorException;
 use Friendica\Network\HTTPException\MethodNotAllowedException;
 use Friendica\Network\HTTPException\NotFoundException;
-use Friendica\Network\HTTPException\NotImplementedException;
 use Friendica\Network\HTTPException\TooManyRequestsException;
 use Friendica\Network\HTTPException\UnauthorizedException;
 use Friendica\Object\Image;
 use Friendica\Protocol\Activity;
 use Friendica\Protocol\Diaspora;
+use Friendica\Security\FKOAuth1;
 use Friendica\Security\OAuth1\OAuthRequest;
 use Friendica\Security\OAuth1\OAuthUtil;
 use Friendica\Util\DateTimeFormat;
@@ -1120,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);
@@ -1134,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);
@@ -1148,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);
@@ -1733,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 {
@@ -1812,18 +1810,18 @@ function api_statuses_networkpublic_timeline($type)
 
        $start = max(0, ($page - 1) * $count);
 
-       $condition = ["`uid` = 0 AND `gravity` IN (?, ?) AND `thread`.`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 `thread`.`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);
 
@@ -2171,24 +2169,21 @@ function api_statuses_mentions($type)
 
        $start = max(0, ($page - 1) * $count);
 
-       $query = "`gravity` IN (?, ?) AND `id` IN (SELECT `iid` FROM `user-item`                
-               WHERE (`hidden` IS NULL OR NOT `hidden`) AND
-                       `uid` = ? AND `notification-type` & ? != 0
-                       AND `iid` > ?";
+       $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 `iid` <= ?";
+               $query .= " AND `id` <= ?";
                $condition[] = $max_id;
        }
 
-       $query .= ")";
-
        array_unshift($condition, $query);
 
        $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
@@ -2267,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);
 
@@ -2936,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);
 
@@ -4874,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.');
@@ -4883,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);
@@ -5809,7 +5807,7 @@ function api_friendica_notification_seen($type)
                $notify = DI::notify()->getByID($id, api_user());
                DI::notify()->setSeen(true, $notify);
 
-               if ($notify->otype === Notify\ObjectType::ITEM) {
+               if ($notify->otype === Notification\ObjectType::ITEM) {
                        $item = Post::selectFirstForUser(api_user(), [], ['id' => $notify->iid, 'uid' => api_user()]);
                        if (DBA::isResult($item)) {
                                // we found the item, return it to the user
@@ -6009,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);