]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Conversation/Network.php
Merge remote-tracking branch 'upstream/2021.12-rc' into user-banner
[friendica.git] / src / Module / Conversation / Network.php
index ab3e917ee5a8ec5dbe44d796df0e22fbd0df3602..908d0a63b4df251c7c67935d8ab75b7a7d8963ba 100644 (file)
@@ -1,4 +1,23 @@
 <?php
+/**
+ * @copyright Copyright (C) 2010-2022, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\Module\Conversation;
 
@@ -11,15 +30,19 @@ use Friendica\Content\Text\HTML;
 use Friendica\Core\ACL;
 use Friendica\Core\Hook;
 use Friendica\Core\Renderer;
+use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Group;
 use Friendica\Model\Item;
+use Friendica\Model\Post;
 use Friendica\Model\Profile;
 use Friendica\Model\User;
+use Friendica\Model\Verb;
 use Friendica\Module\Contact as ModuleContact;
 use Friendica\Module\Security\Login;
+use Friendica\Protocol\Activity;
 use Friendica\Util\DateTimeFormat;
 
 class Network extends BaseModule
@@ -34,6 +57,8 @@ class Network extends BaseModule
        private static $min_id;
        /** @var mixed */
        private static $max_id;
+       /** @var string */
+       private static $accountTypeString;
        /** @var int */
        private static $accountType;
        /** @var string */
@@ -51,17 +76,17 @@ class Network extends BaseModule
        /** @var string */
        protected static $order;
 
-       public static function content(array $parameters = [])
+       protected function content(array $request = []): string
        {
                if (!local_user()) {
                        return Login::form();
                }
 
-               self::parseRequest($parameters, $_GET);
+               $this->parseRequest($_GET);
 
                $module = 'network';
 
-               DI::page()['aside'] .= Widget::accounts($module . '/accounttype', self::$accountType);
+               DI::page()['aside'] .= Widget::accounttypes($module, self::$accountTypeString);
                DI::page()['aside'] .= Group::sidebarWidget($module, $module . '/group', 'standard', self::$groupId);
                DI::page()['aside'] .= ForumManager::widget($module . '/forum', local_user(), self::$forumContactId);
                DI::page()['aside'] .= Widget::postedByYear($module . '/archive', local_user(), false);
@@ -133,22 +158,13 @@ class Network extends BaseModule
                        }
 
                        $x = [
-                               'is_owner' => true,
-                               'allow_location' => $a->user['allow_location'],
-                               'default_location' => $a->user['default-location'],
-                               'nickname' => $a->user['nickname'],
-                               'lockstate' => (self::$groupId || self::$forumContactId || self::$network || (is_array($a->user) &&
-                                       (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
-                                               strlen($a->user['deny_cid']) || strlen($a->user['deny_gid']))) ? 'lock' : 'unlock'),
-                               'default_perms' => ACL::getDefaultUserPermissions($a->user),
-                               'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true, $default_permissions),
+                               'lockstate' => self::$groupId || self::$forumContactId || self::$network || ACL::getLockstateForUserId($a->getLoggedInUserId()) ? 'lock' : 'unlock',
+                               'acl' => ACL::getFullSelectorHTML(DI::page(), $a->getLoggedInUserId(), true, $default_permissions),
                                'bang' => ((self::$groupId || self::$forumContactId || self::$network) ? '!' : ''),
-                               'visitor' => 'block',
-                               'profile_uid' => local_user(),
                                'content' => $content,
                        ];
 
-                       $o .= status_editor($a, $x);
+                       $o .= DI::conversation()->statusEditor($x);
                }
 
                if (self::$groupId) {
@@ -181,7 +197,7 @@ class Network extends BaseModule
                        $ordering = '`commented`';
                }
 
-               $o .= conversation(DI::app(), $items, 'network', false, false, $ordering, local_user());
+               $o .= DI::conversation()->create($items, 'network', false, false, $ordering, local_user());
 
                if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
                        $o .= HTML::scrollLoader();
@@ -189,8 +205,8 @@ class Network extends BaseModule
                        $pager = new BoundariesPager(
                                DI::l10n(),
                                DI::args()->getQueryString(),
-                               $items[0][self::$order],
-                               $items[count($items) - 1][self::$order],
+                               $items[0][self::$order] ?? null,
+                               $items[count($items) - 1][self::$order] ?? null,
                                self::$itemsPerPage
                        );
 
@@ -212,9 +228,10 @@ class Network extends BaseModule
                        return;
                }
 
-               $unseen = Item::exists($condition);
+               $unseen = Post::exists($condition);
 
                if ($unseen) {
+                       /// @todo handle huge "unseen" updates in the background to avoid timeout errors
                        Item::update(['unseen' => false], $condition);
                }
        }
@@ -274,38 +291,58 @@ class Network extends BaseModule
                return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs']]);
        }
 
-       protected static function parseRequest(array $parameters, array $get)
+       protected function parseRequest(array $get)
        {
-               self::$groupId = $parameters['group_id'] ?? 0;
+               self::$groupId = $this->parameters['group_id'] ?? 0;
+
+               self::$forumContactId = $this->parameters['contact_id'] ?? 0;
 
-               self::$forumContactId = $parameters['contact_id'] ?? 0;
+               self::$selectedTab = Session::get('network-tab', DI::pConfig()->get(local_user(), 'network.view', 'selected_tab', ''));
 
-               self::$selectedTab = DI::pConfig()->get(local_user(), 'network.view', 'selected_tab', '');
+               self::$order = 'commented';
 
                if (!empty($get['star'])) {
                        self::$selectedTab = 'star';
+                       self::$star = true;
+               } else {
+                       self::$star = self::$selectedTab == 'star';
                }
 
                if (!empty($get['mention'])) {
                        self::$selectedTab = 'mention';
+                       self::$mention = true;
+               } else {
+                       self::$mention = self::$selectedTab == 'mention';
                }
 
                if (!empty($get['order'])) {
                        self::$selectedTab = $get['order'];
+                       self::$order = $get['order'];
+                       self::$star = false;
+                       self::$mention = false;
+               } elseif (in_array(self::$selectedTab, ['received', 'star', 'mention'])) {
+                       self::$order = 'received';
                }
 
-               DI::pConfig()->set(local_user(), 'network.view', 'selected_tab', self::$selectedTab);
+               self::$selectedTab = self::$selectedTab ?? self::$order;
 
-               self::$star    = intval($get['star']      ?? 0);
-               self::$mention = intval($_GET['mention']      ?? 0);
-               self::$order   = in_array(self::$selectedTab, ['received', 'commented', 'created', 'uriid']) ? self::$selectedTab : 'commented';
+               // Prohibit combined usage of "star" and "mention"
+               if (self::$selectedTab == 'star') {
+                       self::$mention = false;
+               } elseif (self::$selectedTab == 'mention') {
+                       self::$star = false;
+               }
 
-               self::$accountType = User::getAccountTypeByString($parameters['accounttype'] ?? '') ?? '';
+               Session::set('network-tab', self::$selectedTab);
+               DI::pConfig()->set(local_user(), 'network.view', 'selected_tab', self::$selectedTab);
+
+               self::$accountTypeString = $get['accounttype'] ?? $this->parameters['accounttype'] ?? '';
+               self::$accountType = User::getAccountTypeByString(self::$accountTypeString);
 
                self::$network = $get['nets'] ?? '';
 
-               self::$dateFrom = $parameters['from'] ?? '';
-               self::$dateTo = $parameters['to'] ?? '';
+               self::$dateFrom = $this->parameters['from'] ?? '';
+               self::$dateTo = $this->parameters['to'] ?? '';
 
                if (DI::mode()->isMobile()) {
                        self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
@@ -315,22 +352,22 @@ class Network extends BaseModule
                                DI::config()->get('system', 'itemspage_network'));
                }
 
-               self::$min_id = $_GET['min_id'] ?? null;
-               self::$max_id = $_GET['max_id'] ?? null;
+               self::$min_id = $get['min_id'] ?? null;
+               self::$max_id = $get['max_id'] ?? null;
 
-               switch (self::$selectedTab) {
+               switch (self::$order) {
                        case 'received':
-                               self::$max_id = $_GET['last_received'] ?? self::$max_id;
-                               break;
-                       case 'commented':
-                               self::$max_id = $_GET['last_commented'] ?? self::$max_id;
+                               self::$max_id = $get['last_received'] ?? self::$max_id;
                                break;
                        case 'created':
-                               self::$max_id = $_GET['last_created'] ?? self::$max_id;
+                               self::$max_id = $get['last_created'] ?? self::$max_id;
                                break;
                        case 'uriid':
-                               self::$max_id = $_GET['last_uriid'] ?? self::$max_id;
+                               self::$max_id = $get['last_uriid'] ?? self::$max_id;
                                break;
+                       default:
+                               self::$order = 'commented';
+                               self::$max_id = $get['last_commented'] ?? self::$max_id;
                }
        }
 
@@ -339,7 +376,7 @@ class Network extends BaseModule
                $conditionFields['uid'] = local_user();
                $conditionStrings = [];
 
-               if (self::$accountType) {
+               if (!is_null(self::$accountType)) {
                        $conditionFields['contact-type'] = self::$accountType;
                }
 
@@ -354,16 +391,18 @@ class Network extends BaseModule
                }
 
                if (self::$dateFrom) {
-                       $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` <= ? ", DateTimeFormat::convert(self::$dateFrom, 'UTC', date_default_timezone_get())]);
+                       $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` <= ? ", DateTimeFormat::convert(self::$dateFrom, 'UTC', DI::app()->getTimeZone())]);
                }
                if (self::$dateTo) {
-                       $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` >= ? ", DateTimeFormat::convert(self::$dateTo, 'UTC', date_default_timezone_get())]);
+                       $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` >= ? ", DateTimeFormat::convert(self::$dateTo, 'UTC', DI::app()->getTimeZone())]);
                }
 
                if (self::$groupId) {
                        $conditionStrings = DBA::mergeConditions($conditionStrings, ["`contact-id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = ?)", self::$groupId]);
                } elseif (self::$forumContactId) {
-                       $conditionFields['contact-id'] = self::$forumContactId;
+                       $conditionStrings = DBA::mergeConditions($conditionStrings, 
+                               ["((`contact-id` = ?) OR EXISTS(SELECT `uri-id` FROM `post-user-view` WHERE `post-user-view`.`parent-uri-id` = " . DBA::quoteIdentifier($table) . ".`uri-id` AND (`contact-id` = ? AND `gravity` = ? AND `vid` = ? AND `uid` = ?)))",
+                               self::$forumContactId, self::$forumContactId, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), local_user()]);
                }
 
                // Currently only the order modes "received" and "commented" are in use
@@ -417,26 +456,20 @@ class Network extends BaseModule
                        $items = array_reverse($items);
                }
 
-               $parents_str = '';
                if (DBA::isResult($items)) {
-                       $parents_arr = [];
-
-                       foreach ($items as $item) {
-                               if (!in_array($item['parent'], $parents_arr) && ($item['parent'] > 0)) {
-                                       $parents_arr[] = $item['parent'];
-                               }
-                       }
-                       $parents_str = implode(', ', $parents_arr);
+                       $parents = array_column($items, 'parent-uri-id');
+               } else {
+                       $parents = [];
                }
 
                // We aren't going to try and figure out at the item, group, and page
                // level which items you've seen and which you haven't. If you're looking
                // at the top level network page just mark everything seen.
-               if (!self::$groupId && !self::$forumContactId && self::$selectedTab != 'star') {
+               if (!self::$groupId && !self::$forumContactId && !self::$star && !self::$mention) {
                        $condition = ['unseen' => true, 'uid' => local_user()];
                        self::setItemsSeenByCondition($condition);
-               } elseif ($parents_str) {
-                       $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . DBA::escape($parents_str) . ")", local_user()];
+               } elseif (!empty($parents)) {
+                       $condition = ['unseen' => true, 'uid' => local_user(), 'parent-uri-id' => $parents];
                        self::setItemsSeenByCondition($condition);
                }