]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Conversation.php
Another incompatible method declaration fixed + type-hints added
[friendica.git] / src / Content / Conversation.php
index d8cbbf7cbb6dec2129ea5cdaab2c4bbbe5ad2a97..1088497983522f432dd008523efd3fede925ae27 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -47,7 +47,6 @@ use Friendica\Protocol\Activity;
 use Friendica\Util\Crypto;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Profiler;
-use Friendica\Util\Proxy;
 use Friendica\Util\Strings;
 use Friendica\Util\Temporal;
 use Psr\Log\LoggerInterface;
@@ -108,6 +107,8 @@ class Conversation
         */
        public function builtinActivityPuller(array $activity, array &$conv_responses)
        {
+               $thread_parent = $activity['thr-parent-row'] ?? [];
+
                foreach ($conv_responses as $mode => $v) {
                        $sparkle = '';
 
@@ -152,9 +153,8 @@ class Conversation
                                        $activity['thr-parent-id'] = $activity['parent-uri-id'];
                                }
 
-                               // Skip when the causer of the parent is the same than the author of the announce
-                               if (($verb == Activity::ANNOUNCE) && Post::exists(['uri-id' => $activity['thr-parent-id'],
-                                       'uid' => $activity['uid'], 'causer-id' => $activity['author-id'], 'gravity' => GRAVITY_PARENT])) {
+                               // Skip when the causer of the parent is the same as the author of the announce
+                               if (($verb == Activity::ANNOUNCE) && !empty($thread_parent['causer-id']) && ($thread_parent['causer-id'] == $activity['author-id'])) {
                                        continue;
                                }
 
@@ -189,7 +189,7 @@ class Conversation
         * @return string formatted text
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public function formatActivity(array $links, $verb, $id)
+       public function formatActivity(array $links, string $verb, int $id): string
        {
                $this->profiler->startRecording('rendering');
                $o        = '';
@@ -275,7 +275,7 @@ class Conversation
                return $o;
        }
 
-       public function statusEditor(array $x = [], $notes_cid = 0, $popup = false)
+       public function statusEditor(array $x = [], int $notes_cid = 0, bool $popup = false): string
        {
                $user = User::getById($this->app->getLoggedInUserId(), ['uid', 'nickname', 'allow_location', 'default-location']);
                if (empty($user['uid'])) {
@@ -316,6 +316,18 @@ class Conversation
                $jotplugins = '';
                Hook::callAll('jot_tool', $jotplugins);
 
+               if ($this->config->get('system', 'set_creation_date')) {
+                       $created_at = Temporal::getDateTimeField(
+                               new \DateTime(DBA::NULL_DATETIME),
+                               new \DateTime('now'),
+                               null,
+                               $this->l10n->t('Created at'),
+                               'created_at'
+                       );
+               } else {
+                       $created_at = '';
+               }
+
                $tpl = Renderer::getMarkupTemplate("jot.tpl");
 
                $o .= Renderer::replaceMacros($tpl, [
@@ -352,6 +364,7 @@ class Conversation
                                $this->l10n->t('Scheduled at'),
                                'scheduled_at'
                        ),
+                       '$created_at'   => $created_at,
                        '$wait'         => $this->l10n->t('Please wait'),
                        '$permset'      => $this->l10n->t('Permission settings'),
                        '$shortpermset' => $this->l10n->t('Permissions'),
@@ -401,8 +414,8 @@ class Conversation
         * figures out how to determine page owner and other contextual items
         * that are based on unique features of the calling module.
         * @param array  $items
-        * @param        $mode
-        * @param        $update
+        * @param string $mode
+        * @param        $update @TODO Which type?
         * @param bool   $preview
         * @param string $order
         * @param int    $uid
@@ -410,7 +423,7 @@ class Conversation
         * @throws ImagickException
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public function create(array $items, $mode, $update, $preview = false, $order = 'commented', $uid = 0)
+       public function create(array $items, string $mode, $update, bool $preview = false, string $order = 'commented', int $uid = 0): string
        {
                $this->profiler->startRecording('rendering');
 
@@ -428,7 +441,7 @@ class Conversation
                $previewing = (($preview) ? ' preview ' : '');
 
                if ($mode === 'network') {
-                       $items = $this->addChildren($items, false, $order, $uid);
+                       $items = $this->addChildren($items, false, $order, $uid, $mode);
                        if (!$update) {
                                /*
                                * The special div is needed for liveUpdate to kick in for this page.
@@ -454,7 +467,7 @@ class Conversation
                                        . "'; </script>\r\n";
                        }
                } elseif ($mode === 'profile') {
-                       $items = $this->addChildren($items, false, $order, local_user());
+                       $items = $this->addChildren($items, false, $order, $uid, $mode);
 
                        if (!$update) {
                                $tab = !empty($_GET['tab']) ? trim($_GET['tab']) : 'posts';
@@ -471,7 +484,7 @@ class Conversation
                                }
                        }
                } elseif ($mode === 'notes') {
-                       $items = $this->addChildren($items, false, $order, local_user());
+                       $items = $this->addChildren($items, false, $order, local_user(), $mode);
 
                        if (!$update) {
                                $live_update_div = '<div id="live-notes"></div>' . "\r\n"
@@ -479,7 +492,7 @@ class Conversation
                                        . "; var netargs = '/?f='; </script>\r\n";
                        }
                } elseif ($mode === 'display') {
-                       $items = $this->addChildren($items, false, $order, $uid);
+                       $items = $this->addChildren($items, false, $order, $uid, $mode);
 
                        if (!$update) {
                                $live_update_div = '<div id="live-display"></div>' . "\r\n"
@@ -487,17 +500,18 @@ class Conversation
                                        . "</script>";
                        }
                } elseif ($mode === 'community') {
-                       $items = $this->addChildren($items, true, $order, $uid);
+                       $items = $this->addChildren($items, true, $order, $uid, $mode);
 
                        if (!$update) {
                                $live_update_div = '<div id="live-community"></div>' . "\r\n"
                                        . "<script> var profile_uid = -1; var netargs = '" . substr($this->args->getCommand(), 10)
                                        . '?f='
                                        . (!empty($_GET['no_sharer']) ? '&no_sharer=' . rawurlencode($_GET['no_sharer']) : '')
+                                       . (!empty($_GET['accounttype']) ? '&accounttype=' . rawurlencode($_GET['accounttype']) : '')
                                        . "'; </script>\r\n";
                        }
                } elseif ($mode === 'contacts') {
-                       $items = $this->addChildren($items, false, $order, $uid);
+                       $items = $this->addChildren($items, false, $order, $uid, $mode);
 
                        if (!$update) {
                                $live_update_div = '<div id="live-contact"></div>' . "\r\n"
@@ -543,7 +557,7 @@ class Conversation
                        if (in_array($mode, ['community', 'contacts', 'profile'])) {
                                $writable = true;
                        } else {
-                               $writable = ($items[0]['uid'] == 0) && in_array($items[0]['network'], Protocol::FEDERATED);
+                               $writable = $items[0]['writable'] || ($items[0]['uid'] == 0) && in_array($items[0]['network'], Protocol::FEDERATED);
                        }
 
                        if (!local_user()) {
@@ -631,10 +645,18 @@ class Conversation
 
                                        [$categories, $folders] = $this->item->determineCategoriesTerms($item, local_user());
 
-                                       if (!empty($item['content-warning']) && $this->pConfig->get(local_user(), 'system', 'disable_cw', false)) {
+                                       if (!empty($item['title'])) {
+                                               $title = $item['title'];
+                                       } elseif (!empty($item['content-warning']) && $this->pConfig->get(local_user(), 'system', 'disable_cw', false)) {
                                                $title = ucfirst($item['content-warning']);
                                        } else {
-                                               $title = $item['title'];
+                                               $title = '';
+                                       }
+
+                                       if (!empty($item['featured'])) {
+                                               $pinned = $this->l10n->t('Pinned item');
+                                       } else {
+                                               $pinned = '';
                                        }
 
                                        $tmp_item = [
@@ -646,15 +668,15 @@ class Conversation
                                                'created_date'         => $item['created'],
                                                'uriid'                => $item['uri-id'],
                                                'network'              => $item['network'],
-                                               'network_name'         => ContactSelector::networkToName($item['author-network'], $item['author-link'], $item['network']),
-                                               'network_icon'         => ContactSelector::networkToIcon($item['network'], $item['author-link']),
+                                               'network_name'         => ContactSelector::networkToName($item['author-network'], $item['author-link'], $item['network'], $item['author-gsid']),
+                                               'network_icon'         => ContactSelector::networkToIcon($item['network'], $item['author-link'], $item['author-gsid']),
                                                'linktitle'            => $this->l10n->t('View %s\'s profile @ %s', $profile_name, $item['author-link']),
                                                'profile_url'          => $profile_link,
                                                'item_photo_menu_html' => $this->item->photoMenu($item, $formSecurityToken),
                                                'name'                 => $profile_name,
                                                'sparkle'              => $sparkle,
                                                'lock'                 => false,
-                                               'thumb'                => $this->baseURL->remove(Contact::getAvatarUrlForUrl($item['author-link'], $item['uid'], Proxy::SIZE_THUMB)),
+                                               'thumb'                => $this->baseURL->remove($this->item->getAuthorAvatar($item)),
                                                'title'                => $title,
                                                'body_html'            => $body_html,
                                                'tags'                 => $tags['tags'],
@@ -669,14 +691,16 @@ class Conversation
                                                'folders'              => $folders,
                                                'text'                 => strip_tags($body_html),
                                                'localtime'            => DateTimeFormat::local($item['created'], 'r'),
+                                               'utc'                  => DateTimeFormat::utc($item['created'], 'c'),
                                                'ago'                  => (($item['app']) ? $this->l10n->t('%s from %s', Temporal::getRelativeDate($item['created']), $item['app']) : Temporal::getRelativeDate($item['created'])),
                                                'location_html'        => $location_html,
                                                'indent'               => '',
                                                'owner_name'           => '',
                                                'owner_url'            => '',
-                                               'owner_photo'          => $this->baseURL->remove(Contact::getAvatarUrlForUrl($item['owner-link'], $item['uid'], Proxy::SIZE_THUMB)),
+                                               'owner_photo'          => $this->baseURL->remove($this->item->getOwnerAvatar($item)),
                                                'plink'                => ItemModel::getPlink($item),
                                                'edpost'               => false,
+                                               'pinned'               => $pinned,
                                                'isstarred'            => 'unstarred',
                                                'star'                 => false,
                                                'drop'                 => $drop,
@@ -760,7 +784,7 @@ class Conversation
                return $o;
        }
 
-       private function getBlocklist()
+       private function getBlocklist(): array
        {
                if (!local_user()) {
                        return [];
@@ -786,16 +810,17 @@ class Conversation
        /**
         * Adds some information (Causer, post reason, direction) to the fetched post row.
         *
-        * @param array   $row      Post row
-        * @param array   $activity Contact data of the resharer
+        * @param array   $row        Post row
+        * @param array   $activity   Contact data of the resharer
+        * @param array   $thr_parent Thread parent row
         *
         * @return array items with parents and comments
         */
-       private function addRowInformation(array $row, array $activity)
+       private function addRowInformation(array $row, array $activity, array $thr_parent): array
        {
                $this->profiler->startRecording('rendering');
 
-               if ($row['uid'] == 0) {
+               if (!$row['writable']) {
                        $row['writable'] = in_array($row['network'], Protocol::FEDERATED);
                }
 
@@ -842,7 +867,7 @@ class Conversation
                                        $row['owner-name']   = $row['causer-name'];
                                }
 
-                               if (($row['gravity'] == GRAVITY_PARENT) && !empty($row['causer-id'])) {
+                               if (in_array($row['gravity'], [GRAVITY_PARENT, GRAVITY_COMMENT]) && !empty($row['causer-id'])) {
                                        $causer = ['uid' => 0, 'id' => $row['causer-id'], 'network' => $row['causer-network'], 'url' => $row['causer-link']];
 
                                        $row['reshared'] = $this->l10n->t('%s reshared this.', '<a href="'. htmlentities(Contact::magicLinkByContact($causer)) .'">' . htmlentities($row['causer-name']) . '</a>');
@@ -866,6 +891,8 @@ class Conversation
                                break;
                }
 
+               $row['thr-parent-row'] = $thr_parent;
+
                $this->profiler->stopRecording();
                return $row;
        }
@@ -876,15 +903,15 @@ class Conversation
         * The system will fetch the comments for the local user whenever possible.
         * This behaviour is currently needed to allow commenting on Friendica posts.
         *
-        * @param array $parents Parent items
-        *
-        * @param       $block_authors
-        * @param       $order
-        * @param       $uid
+        * @param array  $parents       Parent items
+        * @param bool   $block_authors
+        * @param bool   $order
+        * @param int    $uid
+        * @param string $mode
         * @return array items with parents and comments
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private function addChildren(array $parents, $block_authors, $order, $uid)
+       private function addChildren(array $parents, bool $block_authors, string $order, int $uid, string $mode): array
        {
                $this->profiler->startRecording('rendering');
                if (count($parents) > 1) {
@@ -893,8 +920,6 @@ class Conversation
                        $max_comments = $this->config->get('system', 'max_display_comments', 1000);
                }
 
-               $params = ['order' => ['uri-id' => true, 'uid' => true]];
-
                $activities      = [];
                $uriids          = [];
                $commentcounter  = [];
@@ -928,7 +953,18 @@ class Conversation
                $condition = DBA::mergeConditions($condition,
                        ["`uid` IN (0, ?) AND (`vid` != ? OR `vid` IS NULL)", $uid, Verb::getID(Activity::FOLLOW)]);
 
-               $thread_items = Post::selectForUser(local_user(), array_merge(ItemModel::DISPLAY_FIELDLIST, ['pinned', 'contact-uid', 'gravity', 'post-type', 'post-reason']), $condition, $params);
+               $thread_parents = Post::select(['uri-id', 'causer-id'], $condition, ['order' => ['uri-id' => false, 'uid']]);
+
+               $thr_parent = [];
+
+               while ($row = Post::fetch($thread_parents)) {
+                       $thr_parent[$row['uri-id']] = $row;
+               }
+               DBA::close($thread_parents);
+
+               $params = ['order' => ['uri-id' => true, 'uid' => true]];
+
+               $thread_items = Post::selectForUser($uid, array_merge(ItemModel::DISPLAY_FIELDLIST, ['featured', 'contact-uid', 'gravity', 'post-type', 'post-reason']), $condition, $params);
 
                $items = [];
 
@@ -937,6 +973,10 @@ class Conversation
                                continue;
                        }
 
+                       if (($mode != 'contacts') && !$row['origin']) {
+                               $row['featured'] = false;
+                       }
+
                        if ($max_comments > 0) {
                                if (($row['gravity'] == GRAVITY_COMMENT) && (++$commentcounter[$row['parent-uri-id']] > $max_comments)) {
                                        continue;
@@ -945,7 +985,8 @@ class Conversation
                                        continue;
                                }
                        }
-                       $items[$row['uri-id']] = $this->addRowInformation($row, $activities[$row['uri-id']] ?? []);
+
+                       $items[$row['uri-id']] = $this->addRowInformation($row, $activities[$row['uri-id']] ?? [], $thr_parent[$row['thr-parent-id']] ?? []);
                }
 
                DBA::close($thread_items);
@@ -964,7 +1005,7 @@ class Conversation
         * @param bool  $recursive
         * @return array
         */
-       private function getItemChildren(array &$item_list, array $parent, $recursive = true)
+       private function getItemChildren(array &$item_list, array $parent, bool $recursive = true): array
        {
                $this->profiler->startRecording('rendering');
                $children = [];
@@ -999,7 +1040,7 @@ class Conversation
         * @param array $items
         * @return array
         */
-       private function sortItemChildren(array $items)
+       private function sortItemChildren(array $items): array
        {
                $this->profiler->startRecording('rendering');
                $result = $items;
@@ -1045,7 +1086,7 @@ class Conversation
         * @param array $parent A tree-like array of items
         * @return array
         */
-       private function smartFlattenConversation(array $parent)
+       private function smartFlattenConversation(array $parent): array
        {
                $this->profiler->startRecording('rendering');
                if (!isset($parent['children']) || count($parent['children']) == 0) {
@@ -1101,7 +1142,7 @@ class Conversation
         * @return array
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private function convSort(array $item_list, $order)
+       private function convSort(array $item_list, string $order): array
        {
                $this->profiler->startRecording('rendering');
                $parents = [];
@@ -1132,11 +1173,15 @@ class Conversation
                }
 
                if (stristr($order, 'pinned_received')) {
-                       usort($parents, [$this, 'sortThrPinnedReceived']);
+                       usort($parents, [$this, 'sortThrFeaturedReceived']);
+               } elseif (stristr($order, 'pinned_commented')) {
+                       usort($parents, [$this, 'sortThrFeaturedCommented']);
                } elseif (stristr($order, 'received')) {
                        usort($parents, [$this, 'sortThrReceived']);
                } elseif (stristr($order, 'commented')) {
                        usort($parents, [$this, 'sortThrCommented']);
+               } elseif (stristr($order, 'created')) {
+                       usort($parents, [$this, 'sortThrCreated']);
                }
 
                /*
@@ -1171,23 +1216,41 @@ class Conversation
        }
 
        /**
-        * usort() callback to sort item arrays by pinned and the received key
+        * usort() callback to sort item arrays by featured and the received key
         *
         * @param array $a
         * @param array $b
         * @return int
         */
-       private function sortThrPinnedReceived(array $a, array $b)
+       private function sortThrFeaturedReceived(array $a, array $b): int
        {
-               if ($b['pinned'] && !$a['pinned']) {
+               if ($b['featured'] && !$a['featured']) {
                        return 1;
-               } elseif (!$b['pinned'] && $a['pinned']) {
+               } elseif (!$b['featured'] && $a['featured']) {
                        return -1;
                }
 
                return strcmp($b['received'], $a['received']);
        }
 
+       /**
+        * usort() callback to sort item arrays by featured and the received key
+        *
+        * @param array $a
+        * @param array $b
+        * @return int
+        */
+       private function sortThrFeaturedCommented(array $a, array $b): int
+       {
+               if ($b['featured'] && !$a['featured']) {
+                       return 1;
+               } elseif (!$b['featured'] && $a['featured']) {
+                       return -1;
+               }
+
+               return strcmp($b['commented'], $a['commented']);
+       }
+
        /**
         * usort() callback to sort item arrays by the received key
         *
@@ -1195,7 +1258,7 @@ class Conversation
         * @param array $b
         * @return int
         */
-       private function sortThrReceived(array $a, array $b)
+       private function sortThrReceived(array $a, array $b): int
        {
                return strcmp($b['received'], $a['received']);
        }
@@ -1207,7 +1270,7 @@ class Conversation
         * @param array $b
         * @return int
         */
-       private function sortThrReceivedRev(array $a, array $b)
+       private function sortThrReceivedRev(array $a, array $b): int
        {
                return strcmp($a['received'], $b['received']);
        }
@@ -1219,8 +1282,20 @@ class Conversation
         * @param array $b
         * @return int
         */
-       private function sortThrCommented(array $a, array $b)
+       private function sortThrCommented(array $a, array $b): int
        {
                return strcmp($b['commented'], $a['commented']);
        }
+
+       /**
+        * usort() callback to sort item arrays by the created key
+        *
+        * @param array $a
+        * @param array $b
+        * @return int
+        */
+       private function sortThrCreated(array $a, array $b): int
+       {
+               return strcmp($b['created'], $a['created']);
+       }
 }