"item" is replaced by "post-view" / postupdate check added
[friendica.git/.git] / src / Module / Update / Profile.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\Update;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Session;
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\Network\HTTPException\ForbiddenException;
33 use Friendica\Util\DateTimeFormat;
34
35 class Profile extends BaseModule
36 {
37         public static function rawContent(array $parameters = [])
38         {
39                 $a = DI::app();
40
41                 if (DI::config()->get('system', 'block_public') && !local_user() && !Session::getRemoteContactID($a->profile['uid'])) {
42                         throw new ForbiddenException();
43                 }
44
45                 $profile_uid = intval($_GET['p'] ?? 0);
46
47                 // Ensure we've got a profile owner if updating.
48                 $a->profile['uid'] = $profile_uid;
49
50                 $remote_contact = Session::getRemoteContactID($a->profile['uid']);
51                 $is_owner = local_user() == $a->profile['uid'];
52                 $last_updated_key = "profile:" . $a->profile['uid'] . ":" . local_user() . ":" . $remote_contact;
53
54                 if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact) {
55                         throw new ForbiddenException(DI::l10n()->t('Access to this profile has been restricted.'));
56                 }
57
58                 $o = '';
59
60                 if (empty($_GET['force']) && DI::pConfig()->get(local_user(), 'system', 'no_auto_update')) {
61                         System::htmlUpdateExit($o);
62                 }
63
64                 // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups
65                 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['uid']);
66
67                 $last_updated_array = Session::get('last_updated', []);
68
69                 $last_updated = $last_updated_array[$last_updated_key] ?? 0;
70
71                 // If the page user is the owner of the page we should query for unseen
72                 // items. Otherwise use a timestamp of the last succesful update request.
73                 if ($is_owner || !$last_updated) {
74                         $sql_extra4 = " AND `unseen`";
75                 } else {
76                         $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated);
77                         $sql_extra4 = " AND `received` > '" . $gmupdate . "'";
78                 }
79
80                 $items_stmt = DBA::p(
81                         "SELECT DISTINCT(`parent-uri`) AS `uri`, `created` FROM `post-view`
82                                 WHERE `uid` = ? AND NOT `contact-blocked` AND NOT `contact-pending`
83                                 AND `visible` AND (NOT `deleted` OR `gravity` = ?)
84                                 AND NOT `moderated` AND `wall` $sql_extra4 $sql_extra
85                         ORDER BY `received` DESC",
86                         $a->profile['uid'],
87                         GRAVITY_ACTIVITY
88                 );
89
90                 if (!DBA::isResult($items_stmt)) {
91                         return '';
92                 }
93
94                 // Set a time stamp for this page. We will make use of it when we
95                 // search for new items (update routine)
96                 $last_updated_array[$last_updated_key] = time();
97                 Session::set('last_updated', $last_updated_array);
98
99                 if ($is_owner && !$profile_uid && !DI::config()->get('theme', 'hide_eventlist')) {
100                         $o .= ProfileModel::getBirthdays();
101                         $o .= ProfileModel::getEventsReminderHTML();
102                 }
103
104                 if ($is_owner) {
105                         $unseen = Post::exists(['wall' => true, 'unseen' => true, 'uid' => local_user()]);
106                         if ($unseen) {
107                                 Item::update(['unseen' => false], ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
108                         }
109                 }
110
111                 $items = DBA::toArray($items_stmt);
112
113                 $o .= conversation($a, $items, 'profile', $profile_uid, false, 'received', $a->profile['uid']);
114
115                 System::htmlUpdateExit($o);
116         }
117 }