3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Profile;
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;
54 class Conversations extends BaseProfile
60 /** @var DateTimeFormat */
61 private $dateTimeFormat;
62 /** @var IManageConfigValues */
64 /** @var IHandleUserSessions */
66 /** @var Conversation */
67 private $conversation;
68 /** @var IManagePersonalConfigValues */
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 = [])
75 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
79 $this->dateTimeFormat = $dateTimeFormat;
80 $this->config = $config;
81 $this->session = $session;
82 $this->conversation = $conversation;
83 $this->pConfig = $pConfig;
87 protected function content(array $request = []): string
89 $profile = ProfileModel::load($this->app, $this->parameters['nickname'] ?? '');
90 if (empty($profile)) {
91 throw new HTTPException\NotFoundException($this->t('User not found.'));
94 if ($this->config->get('system', 'block_public') && !$this->session->isAuthenticated()) {
98 if (!empty($profile['hidewall']) && !$this->session->isAuthenticated()) {
99 $this->baseUrl->redirect('profile/' . $profile['nickname'] . '/restricted');
102 if (!$profile['net-publish']) {
103 $this->page['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
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";
111 $category = $datequery = $datequery2 = '';
113 if ($this->args->getArgc() > 3) {
114 for ($x = 3; $x < $this->args->getArgc(); $x++) {
115 if ($this->dateTimeFormat->isYearMonthDay($this->args->get($x))) {
117 $datequery2 = $this->args->get($x);
119 $datequery = $this->args->get($x);
122 $category = $this->args->get($x);
127 if (empty($category)) {
128 $category = $request['category'] ?? '';
131 $hashtags = $request['tag'] ?? '';
135 if ($profile['uid'] == $this->session->getLocalUserId()) {
136 Nav::setSelected('home');
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;
143 $o .= self::getTabsHTML('status', $is_owner, $profile['nickname'], $profile['hide-friends']);
145 $o .= Widget::commonFriendsVisitor($profile['uid'], $profile['nickname']);
147 $commpage = $profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
148 $commvisitor = $commpage && $remote_contact;
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']);
154 if (Security::canWriteToUserWall($profile['uid'])) {
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'],
165 $o .= $this->conversation->statusEditor($x);
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']);
171 $last_updated_array = $this->session->get('last_updated', []);
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']]);
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']]);
183 if (!empty($datequery)) {
184 $condition = DBA::mergeConditions($condition, ["`received` <= ?", DateTimeFormat::convert($datequery, 'UTC', $this->app->getTimeZone())]);
187 if (!empty($datequery2)) {
188 $condition = DBA::mergeConditions($condition, ["`received` >= ?", DateTimeFormat::convert($datequery2, 'UTC', $this->app->getTimeZone())]);
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']]);
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'));
201 $itemspage_network = $this->pConfig->get($this->session->getLocalUserId(), 'system', 'itemspage_network',
202 $this->config->get('system', 'itemspage_network'));
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]);
210 $condition = DBA::mergeConditions($condition, ['uid' => $profile['uid'], 'network' => Protocol::FEDERATED,
211 'visible' => true, 'deleted' => false]);
213 $pager = new Pager($this->l10n, $this->args->getQueryString(), $itemspage_network);
214 $params = ['limit' => [$pager->getStart(), $pager->getItemsPerPage()], 'order' => ['received' => true]];
216 $items_stmt = Post::select(['uri-id', 'thr-parent-id', 'gravity', 'author-id', 'received'], $condition, $params);
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);
223 if ($is_owner && !$this->config->get('theme', 'hide_eventlist')) {
224 $o .= ProfileModel::getBirthdays();
225 $o .= ProfileModel::getEventsReminderHTML();
229 $unseen = Post::exists(['wall' => true, 'unseen' => true, 'uid' => $this->session->getLocalUserId()]);
231 Item::update(['unseen' => false], ['wall' => true, 'unseen' => true, 'uid' => $this->session->getLocalUserId()]);
235 $items = Post::toArray($items_stmt);
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);
243 $o .= $this->conversation->render($items, Conversation::MODE_PROFILE, false, false, 'pinned_received', $profile['uid']);
245 $o .= $pager->renderMinimal(count($items));