]> git.mxchange.org Git - friendica.git/blob - src/Module/Notifications/Ping.php
Merge pull request #12154 from MrPetovan/bug/12084-strip-RTL-override
[friendica.git] / src / Module / Notifications / Ping.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Notifications;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Contact\Introduction\Repository\Introduction;
27 use Friendica\Content\ForumManager;
28 use Friendica\Core\Cache\Capability\ICanCache;
29 use Friendica\Core\Cache\Enum\Duration;
30 use Friendica\Core\Config\Capability\IManageConfigValues;
31 use Friendica\Core\Hook;
32 use Friendica\Core\L10n;
33 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
34 use Friendica\Core\Session\Capability\IHandleUserSessions;
35 use Friendica\Core\System;
36 use Friendica\Database\Database;
37 use Friendica\Database\DBA;
38 use Friendica\Model\Group;
39 use Friendica\Model\Post;
40 use Friendica\Model\User;
41 use Friendica\Model\Verb;
42 use Friendica\Module\Conversation\Network;
43 use Friendica\Module\Register;
44 use Friendica\Module\Response;
45 use Friendica\Navigation\Notifications\Entity;
46 use Friendica\Navigation\Notifications\Exception\NoMessageException;
47 use Friendica\Navigation\Notifications\Factory;
48 use Friendica\Navigation\Notifications\Repository;
49 use Friendica\Navigation\Notifications\ValueObject;
50 use Friendica\Navigation\SystemMessages;
51 use Friendica\Protocol\Activity;
52 use Friendica\Util\DateTimeFormat;
53 use Friendica\Util\Profiler;
54 use GuzzleHttp\Psr7\Uri;
55 use Psr\Log\LoggerInterface;
56
57 class Ping extends BaseModule
58 {
59         /** @var SystemMessages */
60         private $systemMessages;
61         /** @var Repository\Notification */
62         private $notificationRepo;
63         /** @var Introduction */
64         private $introductionRepo;
65         /** @var Factory\FormattedNavNotification */
66         private $formattedNavNotification;
67         /** @var IHandleUserSessions */
68         private $session;
69         /** @var IManageConfigValues */
70         private $config;
71         /** @var IManagePersonalConfigValues */
72         private $pconfig;
73         /** @var Database */
74         private $database;
75         /** @var ICanCache */
76         private $cache;
77         /** @var Repository\Notify */
78         private $notify;
79         /** @var App */
80         private $app;
81
82         public function __construct(App $app, Repository\Notify $notify, ICanCache $cache, Database $database, IManagePersonalConfigValues $pconfig, IManageConfigValues $config, IHandleUserSessions $session, SystemMessages $systemMessages, Repository\Notification $notificationRepo, Introduction $introductionRepo, Factory\FormattedNavNotification $formattedNavNotification, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
83         {
84                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
85
86                 $this->systemMessages           = $systemMessages;
87                 $this->notificationRepo         = $notificationRepo;
88                 $this->introductionRepo         = $introductionRepo;
89                 $this->formattedNavNotification = $formattedNavNotification;
90                 $this->session                  = $session;
91                 $this->config                   = $config;
92                 $this->pconfig                  = $pconfig;
93                 $this->database                 = $database;
94                 $this->cache                    = $cache;
95                 $this->notify                   = $notify;
96                 $this->app                      = $app;
97         }
98
99         protected function rawContent(array $request = [])
100         {
101                 $registrations    = [];
102                 $navNotifications = [];
103
104                 $intro_count     = 0;
105                 $mail_count      = 0;
106                 $home_count      = 0;
107                 $network_count   = 0;
108                 $register_count  = 0;
109                 $sysnotify_count = 0;
110                 $groups_unseen   = [];
111                 $forums_unseen   = [];
112
113                 $event_count          = 0;
114                 $today_event_count    = 0;
115                 $birthday_count       = 0;
116                 $today_birthday_count = 0;
117
118
119                 if ($this->session->getLocalUserId()) {
120                         if ($this->pconfig->get($this->session->getLocalUserId(), 'system', 'detailed_notif')) {
121                                 $notifications = $this->notificationRepo->selectDetailedForUser($this->session->getLocalUserId());
122                         } else {
123                                 $notifications = $this->notificationRepo->selectDigestForUser($this->session->getLocalUserId());
124                         }
125
126                         $condition = [
127                                 "`unseen` AND `uid` = ? AND NOT `origin` AND (`vid` != ? OR `vid` IS NULL)",
128                                 $this->session->getLocalUserId(), Verb::getID(Activity::FOLLOW)
129                         ];
130
131                         // No point showing counts for non-top-level posts when the network page is ordered by received field
132                         if (Network::getTimelineOrderBySession($this->session, $this->pconfig) == 'received') {
133                                 $condition = DBA::mergeConditions($condition, ["`parent` = `id`"]);
134                         }
135
136                         $items_unseen = $this->database->toArray(Post::selectForUser(
137                                 $this->session->getLocalUserId(),
138                                 ['wall', 'uid', 'uri-id'],
139                                 $condition,
140                                 ['limit' => 1000],
141                         ));
142                         $arr = ['items' => $items_unseen];
143                         Hook::callAll('network_ping', $arr);
144
145                         foreach ($items_unseen as $item) {
146                                 if ($item['wall']) {
147                                         $home_count++;
148                                 } else {
149                                         $network_count++;
150                                 }
151                         }
152
153                         $compute_group_counts = $this->config->get('system','compute_group_counts');
154                         if ($network_count && $compute_group_counts) {
155                                 // Find out how unseen network posts are spread across groups
156                                 foreach (Group::countUnseen() as $group_count) {
157                                         if ($group_count['count'] > 0) {
158                                                 $groups_unseen[] = $group_count;
159                                         }
160                                 }
161
162                                 foreach (ForumManager::countUnseenItems() as $forum_count) {
163                                         if ($forum_count['count'] > 0) {
164                                                 $forums_unseen[] = $forum_count;
165                                         }
166                                 }
167                         }
168
169                         $intros = $this->introductionRepo->selectForUser($this->session->getLocalUserId());
170
171                         $intro_count = $intros->count();
172
173                         $myurl      = $this->session->getMyUrl();
174                         $mail_count = $this->database->count('mail', ["`uid` = ? AND NOT `seen` AND `from-url` != ?", $this->session->getLocalUserId(), $myurl]);
175
176                         if (intval($this->config->get('config', 'register_policy')) === Register::APPROVE && $this->app->isSiteAdmin()) {
177                                 $registrations = \Friendica\Model\Register::getPending();
178                                 $register_count = count($registrations);
179                         }
180
181                         $cachekey = 'ping:events:' . $this->session->getLocalUserId();
182                         $events   = $this->cache->get($cachekey);
183                         if (is_null($events)) {
184                                 $events = $this->database->selectToArray('event', ['type', 'start'],
185                                         ["`uid` = ? AND `start` < ? AND `finish` > ? AND NOT `ignore`",
186                                                 $this->session->getLocalUserId(), DateTimeFormat::utc('now + 7 days'), DateTimeFormat::utcNow()]);
187                                 $this->cache->set($cachekey, $events, Duration::HOUR);
188                         }
189
190                         $now_date = DateTimeFormat::localNow('Y-m-d');
191                         foreach ($events as $event) {
192                                 $is_birthday = false;
193                                 if ($event['type'] === 'birthday') {
194                                         $birthday_count++;
195                                         $is_birthday = true;
196                                 } else {
197                                         $event_count++;
198                                 }
199
200                                 if (DateTimeFormat::local($event['start'], 'Y-m-d') === $now_date) {
201                                         if ($is_birthday) {
202                                                 $today_birthday_count++;
203                                         } else {
204                                                 $today_event_count++;
205                                         }
206                                 }
207                         }
208
209                         $owner = User::getOwnerDataById($this->session->getLocalUserId());
210
211                         $navNotifications = array_map(function (Entity\Notification $notification) use ($owner) {
212                                 if (!$this->notify->shouldShowOnDesktop($notification)) {
213                                         return null;
214                                 }
215                                 if (($notification->type == Post\UserNotification::TYPE_NONE) && in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP])) {
216                                         return null;
217                                 }
218                                 try {
219                                         return $this->formattedNavNotification->createFromNotification($notification);
220                                 } catch (NoMessageException $e) {
221                                         return null;
222                                 }
223                         }, $notifications->getArrayCopy());
224                         $navNotifications = array_filter($navNotifications);
225
226                         $sysnotify_count = array_reduce($navNotifications, function (int $carry, ValueObject\FormattedNavNotification $navNotification) {
227                                 return $carry + ($navNotification->seen ? 0 : 1);
228                         }, 0);
229
230                         // merge all notification types in one array
231                         foreach ($intros as $intro) {
232                                 $navNotifications[] = $this->formattedNavNotification->createFromIntro($intro);
233                         }
234
235                         if (count($registrations) <= 1 || $this->pconfig->get($this->session->getLocalUserId(), 'system', 'detailed_notif')) {
236                                 foreach ($registrations as $registration) {
237                                         $navNotifications[] = $this->formattedNavNotification->createFromParams(
238                                                 $registration['name'],
239                                                 $registration['url'],
240                                                 $this->l10n->t('{0} requested registration'),
241                                                 new \DateTime($registration['created'], new \DateTimeZone('UTC')),
242                                                 new Uri($this->baseUrl->get(true) . '/moderation/users/pending')
243                                         );
244                                 }
245                         } elseif (count($registrations) > 1) {
246                                 $navNotifications[] = $this->formattedNavNotification->createFromParams(
247                                         $registrations[0]['name'],
248                                         $registrations[0]['url'],
249                                         $this->l10n->t('{0} and %d others requested registration', count($registrations) - 1),
250                                         new \DateTime($registrations[0]['created'], new \DateTimeZone('UTC')),
251                                         new Uri($this->baseUrl->get(true) . '/moderation/users/pending')
252                                 );
253                         }
254
255                         // sort notifications by $[]['date']
256                         $sort_function = function (ValueObject\FormattedNavNotification $a, ValueObject\FormattedNavNotification $b) {
257                                 $a = $a->toArray();
258                                 $b = $b->toArray();
259
260                                 // Unseen messages are kept at the top
261                                 if ($a['seen'] == $b['seen']) {
262                                         if ($a['timestamp'] == $b['timestamp']) {
263                                                 return 0;
264                                         } else {
265                                                 return $a['timestamp'] < $b['timestamp'] ? 1 : -1;
266                                         }
267                                 } else {
268                                         return $a['seen'] ? 1 : -1;
269                                 }
270                         };
271                         usort($navNotifications, $sort_function);
272                 }
273
274                 $notification_count = $sysnotify_count + $intro_count + $register_count;
275
276                 $data             = [];
277                 $data['intro']    = $intro_count;
278                 $data['mail']     = $mail_count;
279                 $data['net']      = ($network_count < 1000) ? $network_count : '999+';
280                 $data['home']     = ($home_count < 1000) ? $home_count : '999+';
281                 $data['register'] = $register_count;
282
283                 $data['events']          = $event_count;
284                 $data['events-today']    = $today_event_count;
285                 $data['birthdays']       = $birthday_count;
286                 $data['birthdays-today'] = $today_birthday_count;
287                 $data['groups']          = $groups_unseen;
288                 $data['forums']          = $forums_unseen;
289                 $data['notification']    = ($notification_count < 50) ? $notification_count : '49+';
290
291                 $data['notifications'] = $navNotifications;
292
293                 $data['sysmsgs'] = [
294                         'notice' => $this->systemMessages->flushNotices(),
295                         'info'   => $this->systemMessages->flushInfos(),
296                 ];
297
298                 if (isset($_GET['callback'])) {
299                         // JSONP support
300                         System::httpExit($_GET['callback'] . '(' . json_encode(['result' => $data]) . ')', Response::TYPE_BLANK, 'application/javascript');
301                 } else {
302                         System::jsonExit(['result' => $data]);
303                 }
304         }
305 }