ade67d1cf66c9372252f5afabd021e4060be614b
[friendica.git/.git] / src / Module / Profile / Conversations.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Profile;
23
24 use Friendica\App;
25 use Friendica\Content\Conversation;
26 use Friendica\Content\Nav;
27 use Friendica\Content\Pager;
28 use Friendica\Content\Widget;
29 use Friendica\Core\ACL;
30 use Friendica\Core\Config\Capability\IManageConfigValues;
31 use Friendica\Core\L10n;
32 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
33 use Friendica\Core\Protocol;
34 use Friendica\Core\Session\Capability\IHandleUserSessions;
35 use Friendica\Database\DBA;
36 use Friendica\Model\Contact;
37 use Friendica\Model\Item;
38 use Friendica\Model\Post;
39 use Friendica\Model\Post\Category;
40 use Friendica\Model\Profile as ProfileModel;
41 use Friendica\Model\User;
42 use Friendica\Model\Verb;
43 use Friendica\Module\BaseProfile;
44 use Friendica\Module\Response;
45 use Friendica\Module\Security\Login;
46 use Friendica\Network\HTTPException;
47 use Friendica\Protocol\Activity;
48 use Friendica\Security\Security;
49 use Friendica\Util\DateTimeFormat;
50 use Friendica\Util\Profiler;
51 use Friendica\Util\Strings;
52 use Psr\Log\LoggerInterface;
53
54 class Conversations extends BaseProfile
55 {
56         /** @var App */
57         private $app;
58         /** @var App\Page */
59         private $page;
60         /** @var DateTimeFormat */
61         private $dateTimeFormat;
62         /** @var IManageConfigValues */
63         private $config;
64         /** @var IHandleUserSessions */
65         private $session;
66         /** @var Conversation */
67         private $conversation;
68         /** @var IManagePersonalConfigValues */
69         private $pConfig;
70         /** @var App\Mode */
71         private $mode;
72
73         public function __construct(App\Mode $mode, IManagePersonalConfigValues $pConfig, Conversation $conversation, IHandleUserSessions $session, IManageConfigValues $config, DateTimeFormat $dateTimeFormat, App\Page $page, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
74         {
75                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
76
77                 $this->app            = $app;
78                 $this->page           = $page;
79                 $this->dateTimeFormat = $dateTimeFormat;
80                 $this->config         = $config;
81                 $this->session        = $session;
82                 $this->conversation   = $conversation;
83                 $this->pConfig        = $pConfig;
84                 $this->mode           = $mode;
85         }
86
87         protected function content(array $request = []): string
88         {
89                 $profile = ProfileModel::load($this->app, $this->parameters['nickname'] ?? '');
90                 if (empty($profile)) {
91                         throw new HTTPException\NotFoundException($this->t('User not found.'));
92                 }
93
94                 if ($this->config->get('system', 'block_public') && !$this->session->isAuthenticated()) {
95                         return Login::form();
96                 }
97
98                 if (!empty($profile['hidewall']) && !$this->session->isAuthenticated()) {
99                         $this->baseUrl->redirect('profile/' . $profile['nickname'] . '/restricted');
100                 }
101
102                 if (!$profile['net-publish']) {
103                         $this->page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
104                 }
105
106                 $this->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . $this->baseUrl . '/dfrn_poll/' . $this->parameters['nickname'] . '" title="DFRN: ' . $this->t('%s\'s timeline', Strings::escapeHtml($profile['name'])) . '"/>' . "\n";
107                 $this->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . $this->baseUrl . '/feed/' . $this->parameters['nickname'] . '/" title="' . $this->t('%s\'s posts', Strings::escapeHtml($profile['name'])) . '"/>' . "\n";
108                 $this->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . $this->baseUrl . '/feed/' . $this->parameters['nickname'] . '/comments" title="' . $this->t('%s\'s comments', Strings::escapeHtml($profile['name'])) . '"/>' . "\n";
109                 $this->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . $this->baseUrl . '/feed/' . $this->parameters['nickname'] . '/activity" title="' . $this->t('%s\'s timeline', Strings::escapeHtml($profile['name'])) . '"/>' . "\n";
110
111                 $category = $datequery = $datequery2 = '';
112
113                 if ($this->args->getArgc() > 3) {
114                         for ($x = 3; $x < $this->args->getArgc(); $x++) {
115                                 if ($this->dateTimeFormat->isYearMonthDay($this->args->get($x))) {
116                                         if ($datequery) {
117                                                 $datequery2 = $this->args->get($x);
118                                         } else {
119                                                 $datequery = $this->args->get($x);
120                                         }
121                                 } else {
122                                         $category = $this->args->get($x);
123                                 }
124                         }
125                 }
126
127                 if (empty($category)) {
128                         $category = $request['category'] ?? '';
129                 }
130
131                 $hashtags = $request['tag'] ?? '';
132
133                 $o = '';
134
135                 if ($profile['uid'] == $this->session->getLocalUserId()) {
136                         Nav::setSelected('home');
137                 }
138
139                 $remote_contact   = $this->session->getRemoteContactID($profile['uid']);
140                 $is_owner         = $this->session->getLocalUserId() == $profile['uid'];
141                 $last_updated_key = "profile:" . $profile['uid'] . ":" . $this->session->getLocalUserId() . ":" . $remote_contact;
142
143                 $o .= self::getTabsHTML('status', $is_owner, $profile['nickname'], $profile['hide-friends']);
144
145                 $o .= Widget::commonFriendsVisitor($profile['uid'], $profile['nickname']);
146
147                 $commpage    = in_array($profile['page-flags'], [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_COMM_MAN]);
148                 $commvisitor = $commpage && $remote_contact;
149
150                 $this->page['aside'] .= Widget::postedByYear($this->baseUrl . '/profile/' . $profile['nickname'] . '/conversations', $profile['profile_uid'] ?? 0, true);
151                 $this->page['aside'] .= Widget::categories($profile['uid'], $this->baseUrl . '/profile/' . $profile['nickname'] . '/conversations', $category);
152                 $this->page['aside'] .= Widget::tagCloud($profile['uid']);
153
154                 if (Security::canWriteToUserWall($profile['uid'])) {
155                         $x = [
156                                 'is_owner'         => $is_owner,
157                                 'allow_location'   => ($is_owner || $commvisitor) && $profile['allow_location'],
158                                 'default_location' => $is_owner ? $profile['default-location'] : '',
159                                 'nickname'         => $profile['nickname'],
160                                 'acl'              => $is_owner ? ACL::getFullSelectorHTML($this->page, $this->app->getLoggedInUserId(), true) : '',
161                                 'visitor'          => $is_owner || $commvisitor ? 'block' : 'none',
162                                 'profile_uid'      => $profile['uid'],
163                         ];
164
165                         $o .= $this->conversation->statusEditor($x);
166                 }
167
168                 // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched their circles
169                 $condition = Item::getPermissionsConditionArrayByUserId($profile['uid']);
170
171                 $last_updated_array = $this->session->get('last_updated', []);
172
173                 if (!empty($category)) {
174                         $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `category-view` WHERE `name` = ? AND `type` = ? AND `uid` = ?)",
175                                                                        $category, Category::CATEGORY, $profile['uid']]);
176                 }
177
178                 if (!empty($hashtags)) {
179                         $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `tag-search-view` WHERE `name` = ? AND `uid` = ?)",
180                                                                        $hashtags, $profile['uid']]);
181                 }
182
183                 if (!empty($datequery)) {
184                         $condition = DBA::mergeConditions($condition, ["`received` <= ?", DateTimeFormat::convert($datequery, 'UTC', $this->app->getTimeZone())]);
185                 }
186
187                 if (!empty($datequery2)) {
188                         $condition = DBA::mergeConditions($condition, ["`received` >= ?", DateTimeFormat::convert($datequery2, 'UTC', $this->app->getTimeZone())]);
189                 }
190
191                 // Does the profile page belong to a group?
192                 // If not then we can improve the performance with an additional condition
193                 if ($profile['account-type'] != User::ACCOUNT_TYPE_COMMUNITY) {
194                         $condition = DBA::mergeConditions($condition, ['contact-id' => $profile['id']]);
195                 }
196
197                 if ($this->mode->isMobile()) {
198                         $itemspage_network = $this->pConfig->get($this->session->getLocalUserId(), 'system', 'itemspage_mobile_network',
199                                 $this->config->get('system', 'itemspage_network_mobile'));
200                 } else {
201                         $itemspage_network = $this->pConfig->get($this->session->getLocalUserId(), 'system', 'itemspage_network',
202                                 $this->config->get('system', 'itemspage_network'));
203                 }
204
205                 $condition = DBA::mergeConditions($condition, ["((`gravity` = ? AND `wall`) OR
206                         (`gravity` = ? AND `vid` = ? AND `origin`
207                         AND EXISTS(SELECT `uri-id` FROM `post` WHERE `uri-id` = `post-user-view`.`thr-parent-id` AND `gravity` = ? AND `network` IN (?, ?))))",
208                                                                Item::GRAVITY_PARENT, Item::GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), Item::GRAVITY_PARENT, Protocol::ACTIVITYPUB, Protocol::DFRN]);
209
210                 $condition = DBA::mergeConditions($condition, ['uid'     => $profile['uid'], 'network' => Protocol::FEDERATED,
211                                                                'visible' => true, 'deleted' => false]);
212
213                 $pager  = new Pager($this->l10n, $this->args->getQueryString(), $itemspage_network);
214                 $params = ['limit' => [$pager->getStart(), $pager->getItemsPerPage()], 'order' => ['received' => true]];
215
216                 $items_stmt = Post::select(['uri-id', 'thr-parent-id', 'gravity', 'author-id', 'received'], $condition, $params);
217
218                 // Set a time stamp for this page. We will make use of it when we
219                 // search for new items (update routine)
220                 $last_updated_array[$last_updated_key] = time();
221                 $this->session->set('last_updated', $last_updated_array);
222
223                 if ($is_owner && ProfileModel::shouldDisplayEventList($this->session->getLocalUserId(), $this->mode)) {
224                         $o .= ProfileModel::getBirthdays($this->session->getLocalUserId());
225                         $o .= ProfileModel::getEventsReminderHTML($this->session->getLocalUserId(), $this->session->getPublicContactId());
226                 }
227
228                 if ($is_owner) {
229                         $unseen = Post::exists(['wall' => true, 'unseen' => true, 'uid' => $this->session->getLocalUserId()]);
230                         if ($unseen) {
231                                 Item::update(['unseen' => false], ['wall' => true, 'unseen' => true, 'uid' => $this->session->getLocalUserId()]);
232                         }
233                 }
234
235                 $items = Post::toArray($items_stmt);
236
237                 if ($pager->getStart() == 0 && !empty($profile['uid'])) {
238                         $pcid   = Contact::getPublicIdByUserId($profile['uid']);
239                         $pinned = Post\Collection::selectToArrayForContact($pcid, Post\Collection::FEATURED);
240                         $items  = array_merge($items, $pinned);
241                 }
242
243                 $o .= $this->conversation->render($items, Conversation::MODE_PROFILE, false, false, 'pinned_received', $profile['uid']);
244
245                 $o .= $pager->renderMinimal(count($items));
246
247                 return $o;
248         }
249 }