]> git.mxchange.org Git - friendica.git/blob - src/Module/Update/Profile.php
Mode depending control for the behaviour with blocked contacts
[friendica.git] / src / Module / Update / Profile.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Update;
23
24 use Friendica\BaseModule;
25 use Friendica\Content\Conversation;
26 use Friendica\Core\System;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\Item;
30 use Friendica\Model\Post;
31 use Friendica\Model\Profile as ProfileModel;
32 use Friendica\Model\User;
33 use Friendica\Network\HTTPException\ForbiddenException;
34 use Friendica\Util\DateTimeFormat;
35
36 class Profile extends BaseModule
37 {
38         protected function rawContent(array $request = [])
39         {
40                 $a = DI::app();
41
42                 // Ensure we've got a profile owner if updating.
43                 $a->setProfileOwner((int)($request['p'] ?? 0));
44
45                 if (DI::config()->get('system', 'block_public') && !DI::userSession()->getLocalUserId() && !DI::userSession()->getRemoteContactID($a->getProfileOwner())) {
46                         throw new ForbiddenException();
47                 }
48
49                 $remote_contact = DI::userSession()->getRemoteContactID($a->getProfileOwner());
50                 $is_owner = DI::userSession()->getLocalUserId() == $a->getProfileOwner();
51                 $last_updated_key = "profile:" . $a->getProfileOwner() . ":" . DI::userSession()->getLocalUserId() . ":" . $remote_contact;
52
53                 if (!DI::userSession()->isAuthenticated()) {
54                         $user = User::getById($a->getProfileOwner(), ['hidewall']);
55                         if ($user['hidewall']) {
56                                 throw new ForbiddenException(DI::l10n()->t('Access to this profile has been restricted.'));
57                         }
58                 }
59
60                 $o = '';
61
62                 if (empty($request['force'])) {
63                         System::htmlUpdateExit($o);
64                 }
65
66                 // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups
67                 $sql_extra = Item::getPermissionsSQLByUserId($a->getProfileOwner());
68
69                 $last_updated_array = DI::session()->get('last_updated', []);
70
71                 $last_updated = $last_updated_array[$last_updated_key] ?? 0;
72
73                 $condition = ["`uid` = ? AND NOT `contact-blocked` AND NOT `contact-pending`
74                                 AND `visible` AND (NOT `deleted` OR `gravity` = ?)
75                                 AND `wall` " . $sql_extra, $a->getProfileOwner(), Item::GRAVITY_ACTIVITY];
76
77                 if ($request['force'] && !empty($request['item'])) {
78                         // When the parent is provided, we only fetch this
79                         $condition = DBA::mergeConditions($condition, ['parent' => $request['item']]);
80                 } elseif ($is_owner || !$last_updated) {
81                         // If the page user is the owner of the page we should query for unseen
82                         // items. Otherwise use a timestamp of the last succesful update request.
83                         $condition = DBA::mergeConditions($condition, ['unseen' => true]);
84                 } else {
85                         $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated);
86                         $condition = DBA::mergeConditions($condition, ["`received` > ?", $gmupdate]);
87                 }
88
89                 $items = Post::selectToArray(['parent-uri-id', 'created', 'received'], $condition, ['group_by' => ['parent-uri-id'], 'order' => ['received' => true]]);
90                 if (!DBA::isResult($items)) {
91                         return;
92                 }
93
94                 // @todo the DBA functions should handle "SELECT field AS alias" in the future,
95                 // so that this workaround here could be removed.
96                 $items = array_map(function ($item) {
97                         $item['uri-id'] = $item['parent-uri-id'];
98                         unset($item['parent-uri-id']);
99                         return $item;
100                 }, $items);
101
102                 // Set a time stamp for this page. We will make use of it when we
103                 // search for new items (update routine)
104                 $last_updated_array[$last_updated_key] = time();
105                 DI::session()->set('last_updated', $last_updated_array);
106
107                 if ($is_owner && !$a->getProfileOwner() && !DI::config()->get('theme', 'hide_eventlist')) {
108                         $o .= ProfileModel::getBirthdays();
109                         $o .= ProfileModel::getEventsReminderHTML();
110                 }
111
112                 if ($is_owner) {
113                         $unseen = Post::exists(['wall' => true, 'unseen' => true, 'uid' => DI::userSession()->getLocalUserId()]);
114                         if ($unseen) {
115                                 Item::update(['unseen' => false], ['wall' => true, 'unseen' => true, 'uid' => DI::userSession()->getLocalUserId()]);
116                         }
117                 }
118
119                 $o .= DI::conversation()->create($items, Conversation::MODE_PROFILE, $a->getProfileOwner(), false, 'received', $a->getProfileOwner());
120
121                 System::htmlUpdateExit($o);
122         }
123 }