]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
Merge pull request #10179 from nupplaphil/fix/transifex_script
[friendica.git] / include / api.php
index d55f7ce7ac673ed7a5e1ddff7f650db9318eed50..c2a77bdcd8559bbece44a5a329951767978a3e10 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,18 +1731,18 @@ 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 `thread`.`iid` <= ?";
+                       $condition[0] .= " AND `id` <= ?";
                        $condition[] = $max_id;
                }
 
-               $params = ['order' => ['iid' => true], 'limit' => [$start, $count]];
-               $statuses = Item::selectThreadForUser(api_user(), Item::DISPLAY_FIELDLIST, $condition, $params);
+               $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
+               $statuses = Post::selectForUser(api_user(), [], $condition, $params);
 
-               $r = Item::toArray($statuses);
+               $r = Post::toArray($statuses);
        } else {
                $condition = ["`gravity` IN (?, ?) AND `id` > ? AND `private` = ? AND `wall` AND `origin` AND NOT `author-hidden`",
                        GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, Item::PUBLIC];
@@ -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 = Item::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(Item::toArray($statuses), $user_info, false, $type);
+       $ret = api_format_items($statuses, $user_info, false, $type);
 
        bindComments($ret);
 
@@ -1881,14 +1879,14 @@ function api_statuses_show($type)
        $conversation = !empty($_REQUEST['conversation']);
 
        // try to fetch the item for the local user - or the public item, if there is no local one
-       $uri_item = Post::selectFirst(['uri'], ['id' => $id]);
+       $uri_item = Post::selectFirst(['uri-id'], ['id' => $id]);
        if (!DBA::isResult($uri_item)) {
-               throw new BadRequestException("There is no status with this id.");
+               throw new BadRequestException(sprintf("There is no status with the id %d", $id));
        }
 
-       $item = Post::selectFirst(['id'], ['uri' => $uri_item['uri'], 'uid' => [0, api_user()]], ['order' => ['uid' => true]]);
+       $item = Post::selectFirst(['id'], ['uri-id' => $uri_item['uri-id'], 'uid' => [0, api_user()]], ['order' => ['uid' => true]]);
        if (!DBA::isResult($item)) {
-               throw new BadRequestException("There is no status with this id.");
+               throw new BadRequestException(sprintf("There is no status with the uri-id %d for the given user.", $uri_item['uri-id']));
        }
 
        $id = $item['id'];
@@ -1905,7 +1903,7 @@ function api_statuses_show($type)
 
        /// @TODO How about copying this to above methods which don't check $r ?
        if (!DBA::isResult($statuses)) {
-               throw new BadRequestException("There is no status with this id.");
+               throw new BadRequestException(sprintf("There is no status or conversation with the id %d.", $id));
        }
 
        $ret = api_format_items(Post::toArray($statuses), $user_info, false, $type);
@@ -1964,12 +1962,12 @@ function api_conversation_show($type)
        Logger::info(API_LOG_PREFIX . '{subaction}', ['module' => 'api', 'action' => 'conversation', 'subaction' => 'show', 'id' => $id]);
 
        // try to fetch the item for the local user - or the public item, if there is no local one
-       $item = Post::selectFirst(['parent-uri'], ['id' => $id]);
+       $item = Post::selectFirst(['parent-uri-id'], ['id' => $id]);
        if (!DBA::isResult($item)) {
                throw new BadRequestException("There is no status with this id.");
        }
 
-       $parent = Post::selectFirst(['id'], ['uri' => $item['parent-uri'], 'uid' => [0, api_user()]], ['order' => ['uid' => true]]);
+       $parent = Post::selectFirst(['id'], ['uri-id' => $item['parent-uri-id'], 'uid' => [0, api_user()]], ['order' => ['uid' => true]]);
        if (!DBA::isResult($parent)) {
                throw new BadRequestException("There is no status with this id.");
        }
@@ -2041,14 +2039,14 @@ function api_statuses_repeat($type)
        Logger::log('API: api_statuses_repeat: '.$id);
 
        $fields = ['uri-id', 'network', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink'];
-       $item = Item::selectFirst($fields, ['id' => $id, 'private' => [Item::PUBLIC, Item::UNLISTED]]);
+       $item = Post::selectFirst($fields, ['id' => $id, 'private' => [Item::PUBLIC, Item::UNLISTED]]);
 
-       if (DBA::isResult($item) && $item['body'] != "") {
+       if (DBA::isResult($item) && !empty($item['body'])) {
                if (in_array($item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::TWITTER])) {
                        if (!Item::performActivity($id, 'announce', local_user())) {
                                throw new InternalServerErrorException();
                        }
-               
+
                        $item_id = $id;
                } else {
                        if (strpos($item['body'], "[/share]") !== false) {
@@ -2171,35 +2169,25 @@ function api_statuses_mentions($type)
 
        $start = max(0, ($page - 1) * $count);
 
-       $query = "SELECT `item`.`id` FROM `user-item`
-               INNER JOIN `item` ON `item`.`id` = `user-item`.`iid` AND `item`.`gravity` IN (?, ?)
-               WHERE (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`) AND
-                       `user-item`.`uid` = ? AND `user-item`.`notification-type` & ? != 0
-                       AND `user-item`.`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 `item`.`id` <= ?";
+               $query .= " AND `id` <= ?";
                $condition[] = $max_id;
        }
 
-       $query .= " ORDER BY `user-item`.`iid` DESC LIMIT ?, ?";
-       $condition[] = $start;
-       $condition[] = $count;
-
-       $useritems = DBA::p($query, $condition);
-       $itemids = [];
-       while ($useritem = DBA::fetch($useritems)) {
-               $itemids[] = $useritem['id'];
-       }
-       DBA::close($useritems);
+       array_unshift($condition, $query);
 
        $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
-       $statuses = Post::selectForUser(api_user(), [], ['id' => $itemids], $params);
+       $statuses = Post::selectForUser(api_user(), [], $condition, $params);
 
        $ret = api_format_items(Post::toArray($statuses), $user_info, false, $type);
 
@@ -2274,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);
 
@@ -2521,7 +2508,8 @@ function api_format_messages($item, $recipient, $sender)
  */
 function api_convert_item($item)
 {
-       $body = $item['body'];
+       $body = api_add_attachments_to_body($item);
+
        $entities = api_get_entitities($statustext, $body);
 
        // Add pictures to the attachment array and remove them from the body
@@ -2589,6 +2577,52 @@ function api_convert_item($item)
        ];
 }
 
+/**
+ * Add media attachments to the body
+ *
+ * @param array $item
+ * @return string body with added media
+ */
+function api_add_attachments_to_body(array $item)
+{
+       $body = $item['body'];
+
+       foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::IMAGE, Post\Media::AUDIO, Post\Media::VIDEO]) as $media) {
+               if (Item::containsLink($item['body'], $media['url'])) {
+                       continue;
+               }
+
+               if ($media['type'] == Post\Media::IMAGE) {
+                       if (!empty($media['description'])) {
+                               $body .= "\n[img=" . $media['url'] . ']' . $media['description'] .'[/img]';
+                       } else {
+                               $body .= "\n[img]" . $media['url'] .'[/img]';
+                       }
+               } elseif ($media['type'] == Post\Media::AUDIO) {
+                       $body .= "\n[audio]" . $media['url'] . "[/audio]\n";
+               } elseif ($media['type'] == Post\Media::VIDEO) {
+                       $body .= "\n[video]" . $media['url'] . "[/video]\n";
+               }
+       }
+
+       if (strpos($body, '[/img]') !== false) {
+               return $body;
+       }
+
+       foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::HTML]) as $media) {
+               if (!empty($media['preview'])) {
+                       $description = $media['description'] ?: $media['name'];
+                       if (!empty($description)) {
+                               $body .= "\n[img=" . $media['preview'] . ']' . $description .'[/img]';
+                       } else {
+                               $body .= "\n[img]" . $media['preview'] .'[/img]';
+                       }
+               }
+       }
+
+       return $body;
+}
+
 /**
  *
  * @param string $body
@@ -2617,7 +2651,11 @@ function api_get_attachments(&$body)
                $imagedata = Images::getInfoFromURLCached($image);
 
                if ($imagedata) {
-                       $attachments[] = ["url" => $image, "mimetype" => $imagedata["mime"], "size" => $imagedata["size"]];
+                       if (DI::config()->get("system", "proxy_disabled")) {
+                               $attachments[] = ["url" => $image, "mimetype" => $imagedata["mime"], "size" => $imagedata["size"]];
+                       } else {
+                               $attachments[] = ["url" => ProxyUtils::proxifyUrl($image, false), "mimetype" => $imagedata["mime"], "size" => $imagedata["size"]];
+                       }
                }
        }
 
@@ -2641,7 +2679,7 @@ function api_get_entitities(&$text, $bbcode)
                preg_match_all("/\[img](.*?)\[\/img\]/ism", $bbcode, $images);
 
                foreach ($images[1] as $image) {
-                       $replace = ProxyUtils::proxifyUrl($image);
+                       $replace = ProxyUtils::proxifyUrl($image, false);
                        $text = str_replace($image, $replace, $text);
                }
                return [];
@@ -2756,7 +2794,7 @@ function api_get_entitities(&$text, $bbcode)
                                // If image cache is activated, then use the following sizes:
                                // thumb  (150), small (340), medium (600) and large (1024)
                                if (!DI::config()->get("system", "proxy_disabled")) {
-                                       $media_url = ProxyUtils::proxifyUrl($url);
+                                       $media_url = ProxyUtils::proxifyUrl($url, false);
 
                                        $sizes = [];
                                        $scale = Images::getScalingDimensions($image[0], $image[1], 150);
@@ -2875,7 +2913,7 @@ function api_format_items_activities($item, $type = "json")
        $condition = ['uid' => $item['uid'], 'thr-parent' => $item['uri'], 'gravity' => GRAVITY_ACTIVITY];
        $ret = Post::selectForUser($item['uid'], ['author-id', 'verb'], $condition);
 
-       while ($parent_item = Item::fetch($ret)) {
+       while ($parent_item = Post::fetch($ret)) {
                // not used as result should be structured like other user data
                //builtin_activity_puller($i, $activities);
 
@@ -2943,6 +2981,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);
 
@@ -3304,8 +3346,10 @@ function api_lists_statuses($type)
 
        $start = max(0, ($page - 1) * $count);
 
-       $condition = ["`uid` = ? AND `gravity` IN (?, ?) AND `id` > ? AND `group-id` = ?",
-               api_user(), GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, $_REQUEST['list_id']];
+       $groups = DBA::selectToArray('group_member', ['contact-id'], ['gid' => 1]);
+       $gids = array_column($groups, 'contact-id');
+       $condition = ['uid' => api_user(), 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'group-id' => $gids];
+       $condition = DBA::mergeConditions($condition, ["`id` > ?", $since_id]);
 
        if ($max_id > 0) {
                $condition[0] .= " AND `id` <= ?";
@@ -4831,7 +4875,7 @@ function prepare_photo_data($type, $scale, $photo_id)
                "SELECT %s `resource-id`, `created`, `edited`, `title`, `desc`, `album`, `filename`,
                                        `type`, `height`, `width`, `datasize`, `profile`, `allow_cid`, `deny_cid`, `allow_gid`, `deny_gid`,
                                        MIN(`scale`) AS `minscale`, MAX(`scale`) AS `maxscale`
-                       FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' %s GROUP BY 
+                       FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' %s GROUP BY
                               `resource-id`, `created`, `edited`, `title`, `desc`, `album`, `filename`,
                               `type`, `height`, `width`, `datasize`, `profile`, `allow_cid`, `deny_cid`, `allow_gid`, `deny_gid`",
                $data_sql,
@@ -4879,7 +4923,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.');
@@ -4888,7 +4932,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);
@@ -5814,7 +5858,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
@@ -6001,12 +6045,12 @@ api_register_func('api/saved_searches/list', 'api_saved_searches_list', true);
  *
  * @return void
  */
-function bindComments(&$data) 
+function bindComments(&$data)
 {
        if (count($data) == 0) {
                return;
        }
-       
+
        $ids = [];
        $comments = [];
        foreach ($data as $item) {
@@ -6014,7 +6058,7 @@ function bindComments(&$data)
        }
 
        $idStr = DBA::escape(implode(', ', $ids));
-       $sql = "SELECT `parent`, COUNT(*) as comments FROM `item` 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);