]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact.php
Fix uddate issues and improve speed when displaying contact posts
[friendica.git] / src / Model / Contact.php
index b6a30915327846449827c3a44903bc66c5ba23e4..af9c631af9fe3f14d4c63dd28ad2dc58a057d4c2 100644 (file)
@@ -1081,7 +1081,7 @@ class Contact
                                return;
                        }
                } elseif (!isset($contact['url'])) {
-                       Logger::info('Empty contact', ['contact' => $contact, 'callstack' => System::callstack(20)]);
+                       Logger::info('Empty contact', ['contact' => $contact]);
                }
 
                Logger::info('Contact is marked for archival', ['id' => $contact['id'], 'term-date' => $contact['term-date']]);
@@ -1180,7 +1180,6 @@ class Contact
                $mention_url = '';
                $status_link = '';
                $photos_link = '';
-               $self        = false;
 
                if (($contact['network'] === Protocol::DFRN) && !$contact['self'] && empty($contact['pending'])) {
                        $profile_link = 'contact/redir/' . $contact['id'];
@@ -1199,21 +1198,20 @@ class Contact
                        $pm_url = 'message/new/' . $contact['id'];
                }
 
-               if (in_array($contact['rel'], [contact::SHARING])) {
+               $contact_url = 'contact/' . $contact['id'];
+
+               if ($contact['contact-type'] == Contact::TYPE_COMMUNITY) {
+                       $mention_label = DI::l10n()->t('Post to group');
                        $mention_url = 'compose/0?body=!' . $contact['addr'];
+                       $network_label = DI::l10n()->t('View group');
+                       $network_url = 'network/group/' . $contact['id'];
                } else {
+                       $mention_label = DI::l10n()->t('Mention');
                        $mention_url = 'compose/0?body=@' . $contact['addr'];
+                       $network_label = DI::l10n()->t('Network Posts');
+                       $network_url = 'contact/' . $contact['id'] . '/conversations';
                }
 
-
-               if (in_array($contact['rel'], [contact::SHARING])) {
-                       $self = true;
-               }
-
-               $contact_url = 'contact/' . $contact['id'];
-               $posts_link = 'contact/' . $contact['id'] . '/conversations';
-               $group_link = 'network/group/' . $contact['id'];
-
                $follow_link   = '';
                $unfollow_link = '';
                if (!$contact['self'] && Protocol::supportsFollow($contact['network'])) {
@@ -1228,29 +1226,28 @@ class Contact
                 * Menu array:
                 * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
                 */
+
+
                if (empty($contact['uid'])) {
                        $menu = [
                                'profile'  => [DI::l10n()->t('View Profile'), $profile_link, true],
-                               'network'  => [DI::l10n()->t('Network Posts'), $posts_link, false],
+                               'network'  => [$network_label, $network_url, false],
                                'edit'     => [DI::l10n()->t('View Contact'), $contact_url, false],
                                'follow'   => [DI::l10n()->t('Connect/Follow'), $follow_link, true],
                                'unfollow' => [DI::l10n()->t('Unfollow'), $unfollow_link, true],
-                               'mention'  => [DI::l10n()->t('Mention'), DI::l10n()->t('Post to group'), $mention_url, false],
-                               'group'    => [DI::l10n()->t('View group'), $group_link, $contact['forum'], true],
+                               'mention'  => [$mention_label, $mention_url, false],
                        ];
                } else {
                        $menu = [
                                'status'   => [DI::l10n()->t('View Status'), $status_link, true],
                                'profile'  => [DI::l10n()->t('View Profile'), $profile_link, true],
                                'photos'   => [DI::l10n()->t('View Photos'), $photos_link, true],
-                               'network'  => [DI::l10n()->t('Network Posts'), $posts_link, false],
+                               'network'  => [$network_label, $network_url, false],
                                'edit'     => [DI::l10n()->t('View Contact'), $contact_url, false],
                                'pm'       => [DI::l10n()->t('Send PM'), $pm_url, false],
                                'follow'   => [DI::l10n()->t('Connect/Follow'), $follow_link, true],
                                'unfollow' => [DI::l10n()->t('Unfollow'), $unfollow_link, true],
-                               'mention'  => [DI::l10n()->t('Mention'), DI::l10n()->t('Post to group'), $mention_url, false],
-                               'group'    => [DI::l10n()->t('View group'), $group_link, $contact['forum'], true],
-                               'self'     => [$self],
+                               'mention'  => [$mention_label, $mention_url, false],
                        ];
 
                        if (!empty($contact['pending'])) {
@@ -1369,7 +1366,7 @@ class Contact
                        }
 
                        if (DBA::isResult($personal_contact)) {
-                               Logger::info('Take contact data from personal contact', ['url' => $url, 'update' => $update, 'contact' => $personal_contact, 'callstack' => System::callstack(20)]);
+                               Logger::info('Take contact data from personal contact', ['url' => $url, 'update' => $update, 'contact' => $personal_contact]);
                                $data = $personal_contact;
                                $data['photo'] = $personal_contact['avatar'];
                                $data['account-type'] = $personal_contact['contact-type'];
@@ -1381,7 +1378,7 @@ class Contact
                }
 
                if (empty($data['network']) || ($data['network'] == Protocol::PHANTOM)) {
-                       Logger::notice('No valid network found', ['url' => $url, 'uid' => $uid, 'default' => $default, 'update' => $update, 'callstack' => System::callstack(20)]);
+                       Logger::notice('No valid network found', ['url' => $url, 'uid' => $uid, 'default' => $default, 'update' => $update]);
                        return 0;
                }
 
@@ -1569,23 +1566,20 @@ class Contact
         * @return string posts in HTML
         * @throws \Exception
         */
-       public static function getPostsFromUrl(string $contact_url, bool $thread_mode = false, int $update = 0, int $parent = 0, bool $only_media = false): string
+       public static function getPostsFromUrl(string $contact_url, int $uid, bool $only_media = false): string
        {
-               return self::getPostsFromId(self::getIdForURL($contact_url), $thread_mode, $update, $parent, $only_media);
+               return self::getPostsFromId(self::getIdForURL($contact_url), $uid, $only_media);
        }
 
        /**
         * Returns posts from a given contact id
         *
         * @param int  $cid         Contact ID
-        * @param bool $thread_mode
-        * @param int  $update      Update mode
-        * @param int  $parent      Item parent ID for the update mode
         * @param bool $only_media  Only display media content
         * @return string posts in HTML
         * @throws \Exception
         */
-       public static function getPostsFromId(int $cid, bool $thread_mode = false, int $update = 0, int $parent = 0, bool $only_media = false): string
+       public static function getPostsFromId(int $cid, int $uid, bool $only_media = false, string $last_received = null): string
        {
                $contact = DBA::selectFirst('contact', ['contact-type', 'network'], ['id' => $cid]);
                if (!DBA::isResult($contact)) {
@@ -1593,32 +1587,17 @@ class Contact
                }
 
                if (empty($contact["network"]) || in_array($contact["network"], Protocol::FEDERATED)) {
-                       $sql = "(`uid` = 0 OR (`uid` = ? AND NOT `global`))";
+                       $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))", $uid];
                } else {
-                       $sql = "`uid` = ?";
+                       $condition = ["`uid` = ?", $uid];
                }
 
                $contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id');
 
-               if ($thread_mode) {
-                       $condition = [
-                               "((`$contact_field` = ? AND `gravity` = ?) OR (`author-id` = ? AND `gravity` = ? AND `vid` = ? AND `protocol` != ? AND `thr-parent-id` = `parent-uri-id`)) AND " . $sql,
-                               $cid, Item::GRAVITY_PARENT, $cid, Item::GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), Conversation::PARCEL_DIASPORA, DI::userSession()->getLocalUserId()
-                       ];
-               } else {
-                       $condition = [
-                               "`$contact_field` = ? AND `gravity` IN (?, ?) AND " . $sql,
-                               $cid, Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT, DI::userSession()->getLocalUserId()
-                       ];
-               }
+               $condition = DBA::mergeConditions($condition, ["`$contact_field` = ? AND `gravity` IN (?, ?)", $cid, Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT]);
 
-               if (!empty($parent)) {
-                       $condition = DBA::mergeConditions($condition, ['parent' => $parent]);
-               } else {
-                       $last_received = isset($_GET['last_received']) ? DateTimeFormat::utc($_GET['last_received']) : '';
-                       if (!empty($last_received)) {
-                               $condition = DBA::mergeConditions($condition, ["`received` < ?", $last_received]);
-                       }
+               if (!empty($last_received)) {
+                       $condition = DBA::mergeConditions($condition, ["`received` < ?", $last_received]);
                }
 
                if ($only_media) {
@@ -1629,66 +1608,108 @@ class Contact
                }
 
                if (DI::mode()->isMobile()) {
-                       $itemsPerPage = DI::pConfig()->get(
-                               DI::userSession()->getLocalUserId(),
-                               'system',
-                               'itemspage_mobile_network',
-                               DI::config()->get('system', 'itemspage_network_mobile')
-                       );
+                       $itemsPerPage = DI::pConfig()->get($uid, 'system', 'itemspage_mobile_network', DI::config()->get('system', 'itemspage_network_mobile'));
                } else {
-                       $itemsPerPage = DI::pConfig()->get(
-                               DI::userSession()->getLocalUserId(),
-                               'system',
-                               'itemspage_network',
-                               DI::config()->get('system', 'itemspage_network')
-                       );
+                       $itemsPerPage = DI::pConfig()->get($uid, 'system', 'itemspage_network', DI::config()->get('system', 'itemspage_network'));
                }
 
                $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
 
                $params = ['order' => ['received' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
 
-               if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) {
+               if (DI::pConfig()->get($uid, 'system', 'infinite_scroll')) {
                        $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
                        $o = Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
                } else {
                        $o = '';
                }
 
-               if ($thread_mode) {
-                       $fields = ['uri-id', 'thr-parent-id', 'gravity', 'author-id', 'commented'];
-                       $items = Post::toArray(Post::selectForUser(DI::userSession()->getLocalUserId(), $fields, $condition, $params));
+               $fields = array_merge(Item::DISPLAY_FIELDLIST, ['featured']);
+               $items = Post::toArray(Post::selectForUser($uid, $fields, $condition, $params));
 
-                       if ($pager->getStart() == 0) {
-                               $cdata = self::getPublicAndUserContactID($cid, DI::userSession()->getLocalUserId());
-                               if (!empty($cdata['public'])) {
-                                       $pinned = Post\Collection::selectToArrayForContact($cdata['public'], Post\Collection::FEATURED, $fields);
-                                       $items = array_merge($items, $pinned);
-                               }
-                       }
+               $o .= DI::conversation()->render($items, ConversationContent::MODE_CONTACT_POSTS);
 
-                       $o .= DI::conversation()->render($items, ConversationContent::MODE_CONTACTS, $update, false, 'pinned_commented', DI::userSession()->getLocalUserId());
+               if (DI::pConfig()->get($uid, 'system', 'infinite_scroll')) {
+                       $o .= HTML::scrollLoader();
                } else {
-                       $fields = array_merge(Item::DISPLAY_FIELDLIST, ['featured']);
-                       $items = Post::toArray(Post::selectForUser(DI::userSession()->getLocalUserId(), $fields, $condition, $params));
-
-                       if ($pager->getStart() == 0) {
-                               $cdata = self::getPublicAndUserContactID($cid, DI::userSession()->getLocalUserId());
-                               if (!empty($cdata['public'])) {
-                                       $condition = [
-                                               "`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ? AND `type` = ?)",
-                                               $cdata['public'], Post\Collection::FEATURED
-                                       ];
-                                       $pinned = Post::toArray(Post::selectForUser(DI::userSession()->getLocalUserId(), $fields, $condition, $params));
-                                       $items = array_merge($pinned, $items);
-                               }
-                       }
+                       $o .= $pager->renderMinimal(count($items));
+               }
+
+               return $o;
+       }
 
-                       $o .= DI::conversation()->render($items, ConversationContent::MODE_CONTACT_POSTS, $update);
+       /**
+        * Returns threads from a given contact id
+        *
+        * @param int  $cid         Contact ID
+        * @param int  $update      Update mode
+        * @param int  $parent      Item parent ID for the update mode
+        * @return string posts in HTML
+        * @throws \Exception
+        */
+       public static function getThreadsFromId(int $cid, int $uid, int $update = 0, int $parent = 0, string $last_received = ''): string
+       {
+               $contact = DBA::selectFirst('contact', ['contact-type', 'network'], ['id' => $cid]);
+               if (!DBA::isResult($contact)) {
+                       return '';
+               }
+
+               if (empty($contact["network"]) || in_array($contact["network"], Protocol::FEDERATED)) {
+                       $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))", $uid];
+               } else {
+                       $condition = ["`uid` = ?", $uid];
+               }
+
+               if (!empty($parent)) {
+                       $condition = DBA::mergeConditions($condition, ['parent' => $parent]);
+               } elseif (!empty($last_received)) {
+                       $condition = DBA::mergeConditions($condition, ["`received` < ?", $last_received]);
+               }
+
+               $contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id');
+
+               if (DI::mode()->isMobile()) {
+                       $itemsPerPage = DI::pConfig()->get($uid, 'system', 'itemspage_mobile_network', DI::config()->get('system', 'itemspage_network_mobile'));
+               } else {
+                       $itemsPerPage = DI::pConfig()->get($uid, 'system', 'itemspage_network', DI::config()->get('system', 'itemspage_network'));
+               }
+
+               $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
+
+               if (DI::pConfig()->get($uid, 'system', 'infinite_scroll')) {
+                       $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
+                       $o = Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
+               } else {
+                       $o = '';
                }
 
+               $condition1 = DBA::mergeConditions($condition, ["`$contact_field` = ?", $cid]);
+
+               $condition2 = DBA::mergeConditions($condition, [
+                       "`author-id` = ? AND `gravity` = ? AND `vid` = ? AND `protocol` != ? AND `thr-parent-id` = `parent-uri-id`",
+                       $cid, Item::GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), Conversation::PARCEL_DIASPORA
+               ]);
+
+               $sql1 = "SELECT `uri-id`, `received` FROM `post-thread-user-view` WHERE " . array_shift($condition1);
+               $sql2 = "SELECT `thr-parent-id` AS `uri-id`, `received` FROM `post-user-view` WHERE " . array_shift($condition2);
+
+               $union = array_merge($condition1, $condition2);
+               $sql = $sql1 . " UNION " . $sql2;
+
+               $sql .= " ORDER BY `received` DESC LIMIT ?, ?";
+               $union = array_merge($union, [$pager->getStart(), $pager->getItemsPerPage()]);
+               $items = Post::toArray(DBA::p($sql, $union));
+
+               if ($pager->getStart() == 0) {
+                       $fields = ['uri-id', 'thr-parent-id', 'gravity', 'author-id', 'received'];
+                       $pinned = Post\Collection::selectToArrayForContact($cid, Post\Collection::FEATURED, $fields);
+                       $items = array_merge($items, $pinned);
+               }
+
+               $o .= DI::conversation()->render($items, ConversationContent::MODE_CONTACTS, $update, false, 'pinned_received', $uid);
+
                if (!$update) {
-                       if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) {
+                       if (DI::pConfig()->get($uid, 'system', 'infinite_scroll')) {
                                $o .= HTML::scrollLoader();
                        } else {
                                $o .= $pager->renderMinimal(count($items));
@@ -2579,7 +2600,7 @@ class Contact
                        Worker::add(Worker::PRIORITY_HIGH, 'MergeContact', $first, $duplicate['id'], $uid);
                }
                DBA::close($duplicates);
-               Logger::info('Duplicates handled', ['uid' => $uid, 'nurl' => $nurl, 'callstack' => System::callstack(20)]);
+               Logger::info('Duplicates handled', ['uid' => $uid, 'nurl' => $nurl]);
                return true;
        }
 
@@ -2602,7 +2623,7 @@ class Contact
 
                $stamp = (float)microtime(true);
                self::updateFromProbe($id);
-               Logger::debug('Contact data is updated.', ['duration' => round((float)microtime(true) - $stamp, 3), 'id' => $id, 'url' => $contact['url'], 'callstack' => System::callstack(20)]);
+               Logger::debug('Contact data is updated.', ['duration' => round((float)microtime(true) - $stamp, 3), 'id' => $id, 'url' => $contact['url']]);
                return true;
        }
 
@@ -2842,7 +2863,7 @@ class Contact
                        }
 
                        $ret['last-item'] = Probe::getLastUpdate($ret);
-                       Logger::info('Fetched last item', ['id' => $id, 'probed_url' => $ret['url'], 'last-item' => $ret['last-item'], 'callstack' => System::callstack(20)]);
+                       Logger::info('Fetched last item', ['id' => $id, 'probed_url' => $ret['url'], 'last-item' => $ret['last-item']]);
                }
 
                $update = false;
@@ -3373,7 +3394,7 @@ class Contact
                } elseif (!empty($contact['id'])) {
                        self::remove($contact['id']);
                } else {
-                       DI::logger()->info('Couldn\'t remove follower because of invalid contact array', ['contact' => $contact, 'callstack' => System::callstack()]);
+                       DI::logger()->info('Couldn\'t remove follower because of invalid contact array', ['contact' => $contact]);
                        return;
                }
 
@@ -3632,7 +3653,7 @@ class Contact
                ];
 
                if (!$show_blocked) {
-                       $condition['server-blocked'] = true;
+                       $condition['server-blocked'] = false;
                }
 
                if ($uid == 0) {