Get rid of the "item" table, enhanced "post" tables
[friendica.git/.git] / src / Module / Profile / Status.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Content\Nav;
25 use Friendica\Content\Pager;
26 use Friendica\Content\Widget;
27 use Friendica\Core\ACL;
28 use Friendica\Core\Protocol;
29 use Friendica\Core\Session;
30 use Friendica\Database\DBA;
31 use Friendica\DI;
32 use Friendica\Model\Item;
33 use Friendica\Model\Post;
34 use Friendica\Model\Post\Category;
35 use Friendica\Model\Profile as ProfileModel;
36 use Friendica\Model\User;
37 use Friendica\Model\Verb;
38 use Friendica\Module\BaseProfile;
39 use Friendica\Module\Security\Login;
40 use Friendica\Network\HTTPException;
41 use Friendica\Protocol\Activity;
42 use Friendica\Util\DateTimeFormat;
43 use Friendica\Security\Security;
44 use Friendica\Util\Strings;
45 use Friendica\Util\XML;
46
47 class Status extends BaseProfile
48 {
49         public static function content(array $parameters = [])
50         {
51                 $args = DI::args();
52
53                 $a = DI::app();
54
55                 ProfileModel::load($a, $parameters['nickname']);
56
57                 if (empty($a->profile)) {
58                         throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
59                 }
60
61                 if (!$a->profile['net-publish']) {
62                         DI::page()['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
63                 }
64
65                 DI::page()['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . DI::baseUrl() . '/dfrn_poll/' . $parameters['nickname'] . '" title="DFRN: ' . DI::l10n()->t('%s\'s timeline', $a->profile['name']) . '"/>' . "\n";
66                 DI::page()['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . DI::baseUrl() . '/feed/' . $parameters['nickname'] . '/" title="' . DI::l10n()->t('%s\'s posts', $a->profile['name']) . '"/>' . "\n";
67                 DI::page()['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . DI::baseUrl() . '/feed/' . $parameters['nickname'] . '/comments" title="' . DI::l10n()->t('%s\'s comments', $a->profile['name']) . '"/>' . "\n";
68                 DI::page()['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . DI::baseUrl() . '/feed/' . $parameters['nickname'] . '/activity" title="' . DI::l10n()->t('%s\'s timeline', $a->profile['name']) . '"/>' . "\n";
69
70                 $category = $datequery = $datequery2 = '';
71
72                 $dtFormat = DI::dtFormat();
73
74                 if ($args->getArgc() > 3) {
75                         for ($x = 3; $x < $args->getArgc(); $x++) {
76                                 if ($dtFormat->isYearMonthDay($args->get($x))) {
77                                         if ($datequery) {
78                                                 $datequery2 = Strings::escapeHtml($args->get($x));
79                                         } else {
80                                                 $datequery = Strings::escapeHtml($args->get($x));
81                                         }
82                                 } else {
83                                         $category = $args->get($x);
84                                 }
85                         }
86                 }
87
88                 if (empty($category)) {
89                         $category = $_GET['category'] ?? '';
90                 }
91
92                 $hashtags = $_GET['tag'] ?? '';
93
94                 if (DI::config()->get('system', 'block_public') && !local_user() && !Session::getRemoteContactID($a->profile['uid'])) {
95                         return Login::form();
96                 }
97
98                 $o = '';
99
100                 if ($a->profile['uid'] == local_user()) {
101                         Nav::setSelected('home');
102                 }
103
104                 $remote_contact = Session::getRemoteContactID($a->profile['uid']);
105                 $is_owner = local_user() == $a->profile['uid'];
106                 $last_updated_key = "profile:" . $a->profile['uid'] . ":" . local_user() . ":" . $remote_contact;
107
108                 if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact) {
109                         notice(DI::l10n()->t('Access to this profile has been restricted.'));
110                         return '';
111                 }
112
113                 $o .= self::getTabsHTML($a, 'status', $is_owner, $a->profile['nickname']);
114
115                 $o .= Widget::commonFriendsVisitor($a->profile['uid'], $a->profile['nickname']);
116
117                 $commpage = $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
118                 $commvisitor = $commpage && $remote_contact;
119
120                 DI::page()['aside'] .= Widget::postedByYear(DI::baseUrl() . '/profile/' . $a->profile['nickname'] . '/status', $a->profile['profile_uid'] ?? 0, true);
121                 DI::page()['aside'] .= Widget::categories(DI::baseUrl() . '/profile/' . $a->profile['nickname'] . '/status', XML::escape($category));
122                 DI::page()['aside'] .= Widget::tagCloud();
123
124                 if (Security::canWriteToUserWall($a->profile['uid'])) {
125                         $x = [
126                                 'is_owner' => $is_owner,
127                                 'allow_location' => ($is_owner || $commvisitor) && $a->profile['allow_location'],
128                                 'default_location' => $is_owner ? $a->user['default-location'] : '',
129                                 'nickname' => $a->profile['nickname'],
130                                 'lockstate' => is_array($a->user)
131                                 && (strlen($a->user['allow_cid'])
132                                         || strlen($a->user['allow_gid'])
133                                         || strlen($a->user['deny_cid'])
134                                         || strlen($a->user['deny_gid'])
135                                 ) ? 'lock' : 'unlock',
136                                 'acl' => $is_owner ? ACL::getFullSelectorHTML(DI::page(), $a->user, true) : '',
137                                 'bang' => '',
138                                 'visitor' => $is_owner || $commvisitor ? 'block' : 'none',
139                                 'profile_uid' => $a->profile['uid'],
140                         ];
141
142                         $o .= status_editor($a, $x);
143                 }
144
145                 // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups
146                 $condition = Item::getPermissionsConditionArrayByUserId($a->profile['uid']);
147
148                 $last_updated_array = Session::get('last_updated', []);
149
150                 if (!empty($category)) {
151                         $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `category-view` WHERE `name` = ? AND `type` = ? AND `uid` = ?)",
152                                 $category, Category::CATEGORY, $a->profile['uid']]);
153                 }
154
155                 if (!empty($hashtags)) {
156                         $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `tag-search-view` WHERE `name` = ? AND `uid` = ?)",
157                                 $hashtags, $a->profile['uid']]);
158                 }
159
160                 if (!empty($datequery)) {
161                         $condition = DBA::mergeConditions($condition, ["`received` <= ?", DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get())]);
162                 }
163                 if (!empty($datequery2)) {
164                         $condition = DBA::mergeConditions($condition, ["`received` >= ?", DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get())]);
165                 }
166
167                 // Does the profile page belong to a forum?
168                 // If not then we can improve the performance with an additional condition
169                 $condition2 = ['uid' => $a->profile['uid'], 'page-flags' => [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP]];
170                 if (!DBA::exists('user', $condition2)) {
171                         $condition = DBA::mergeConditions($condition, ['contact-id' => $a->profile['id']]);
172                 }
173
174                 if (DI::mode()->isMobile()) {
175                         $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
176                                 DI::config()->get('system', 'itemspage_network_mobile'));
177                 } else {
178                         $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
179                                 DI::config()->get('system', 'itemspage_network'));
180                 }
181
182                 $condition = DBA::mergeConditions($condition, ["((`gravity` = ? AND `wall`) OR
183                         (`gravity` = ? AND `vid` = ? AND `origin` AND `thr-parent-id` IN
184                                 (SELECT `uri-id` FROM `post-view` AS `i`
185                                         WHERE `gravity` = ? AND `network` IN (?, ?, ?, ?) AND `uid` IN (?, ?)
186                                                 AND `i`.`uri-id` = `thr-parent-id`)))",
187                         GRAVITY_PARENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), GRAVITY_PARENT,
188                         Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::DIASPORA, Protocol::OSTATUS,
189                         0, $a->profile['uid']]);
190
191                 $condition = DBA::mergeConditions($condition, ['uid' => $a->profile['uid'], 'network' => Protocol::FEDERATED,
192                         'visible' => true, 'deleted' => false]);
193
194                 $pager = new Pager(DI::l10n(), $args->getQueryString(), $itemspage_network);
195                 $params = ['limit' => [$pager->getStart(), $pager->getItemsPerPage()], 'order' => ['received' => true]];
196
197                 $items_stmt = Post::select(['uri-id', 'thr-parent-id', 'gravity', 'author-id', 'received'], $condition, $params);
198
199                 // Set a time stamp for this page. We will make use of it when we
200                 // search for new items (update routine)
201                 $last_updated_array[$last_updated_key] = time();
202                 Session::set('last_updated', $last_updated_array);
203
204                 if ($is_owner && !DI::config()->get('theme', 'hide_eventlist')) {
205                         $o .= ProfileModel::getBirthdays();
206                         $o .= ProfileModel::getEventsReminderHTML();
207                 }
208
209                 if ($is_owner) {
210                         $unseen = Post::exists(['wall' => true, 'unseen' => true, 'uid' => local_user()]);
211                         if ($unseen) {
212                                 Item::update(['unseen' => false], ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
213                         }
214                 }
215
216                 $items = Post::toArray($items_stmt);
217
218                 if ($pager->getStart() == 0 && !empty($a->profile['uid'])) {
219                         $condition = ['private' => [Item::PUBLIC, Item::UNLISTED]];
220                         $remote_user = Session::getRemoteContactID($a->profile['uid']);
221                         if (!empty($remote_user)) {
222                                 $permissionSets = DI::permissionSet()->selectByContactId($remote_user, $a->profile['uid']);
223                                 if (!empty($permissionSets)) {
224                                         $condition = ['psid' => array_merge($permissionSets->column('id'),
225                                                         [DI::permissionSet()->getIdFromACL($a->profile['uid'], '', '', '', '')])];
226                                 }
227                         } elseif ($a->profile['uid'] == local_user()) {
228                                 $condition = [];
229                         }
230         
231                         $pinned_items = Post::selectPinned($a->profile['uid'], ['uri-id', 'pinned'], $condition);
232                         $pinned = Post::toArray($pinned_items);
233                         $items = array_merge($items, $pinned);
234                 }
235
236                 $o .= conversation($a, $items, 'profile', false, false, 'pinned_received', $a->profile['uid']);
237
238                 $o .= $pager->renderMinimal(count($items));
239
240                 return $o;
241         }
242 }