3 * @copyright Copyright (C) 2010-2022, 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\Notifications;
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;
57 class Ping extends BaseModule
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 */
69 /** @var IManageConfigValues */
71 /** @var IManagePersonalConfigValues */
77 /** @var Repository\Notify */
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 = [])
84 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
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;
99 protected function rawContent(array $request = [])
102 $navNotifications = [];
109 $sysnotify_count = 0;
114 $today_event_count = 0;
116 $today_birthday_count = 0;
119 if ($this->session->getLocalUserId()) {
120 if ($this->pconfig->get($this->session->getLocalUserId(), 'system', 'detailed_notif')) {
121 $notifications = $this->notificationRepo->selectDetailedForUser($this->session->getLocalUserId());
123 $notifications = $this->notificationRepo->selectDigestForUser($this->session->getLocalUserId());
127 "`unseen` AND `uid` = ? AND NOT `origin` AND (`vid` != ? OR `vid` IS NULL)",
128 $this->session->getLocalUserId(), Verb::getID(Activity::FOLLOW)
131 $items_unseen = $this->database->toArray(Post::selectForUser(
132 $this->session->getLocalUserId(),
133 ['wall', 'uid', 'uri-id'],
137 $arr = ['items' => $items_unseen];
138 Hook::callAll('network_ping', $arr);
140 foreach ($items_unseen as $item) {
148 $compute_group_counts = $this->config->get('system','compute_group_counts');
149 if ($network_count && $compute_group_counts) {
150 // Find out how unseen network posts are spread across groups
151 foreach (Group::countUnseen() as $group_count) {
152 if ($group_count['count'] > 0) {
153 $groups_unseen[] = $group_count;
157 foreach (ForumManager::countUnseenItems() as $forum_count) {
158 if ($forum_count['count'] > 0) {
159 $forums_unseen[] = $forum_count;
164 $intros = $this->introductionRepo->selectForUser($this->session->getLocalUserId());
166 $intro_count = $intros->count();
168 $myurl = $this->session->getMyUrl();
169 $mail_count = $this->database->count('mail', ["`uid` = ? AND NOT `seen` AND `from-url` != ?", $this->session->getLocalUserId(), $myurl]);
171 if (intval($this->config->get('config', 'register_policy')) === Register::APPROVE && $this->app->isSiteAdmin()) {
172 $registrations = \Friendica\Model\Register::getPending();
173 $register_count = count($registrations);
176 $cachekey = 'ping:events:' . $this->session->getLocalUserId();
177 $events = $this->cache->get($cachekey);
178 if (is_null($events)) {
179 $events = $this->database->selectToArray('event', ['type', 'start'],
180 ["`uid` = ? AND `start` < ? AND `finish` > ? AND NOT `ignore`",
181 $this->session->getLocalUserId(), DateTimeFormat::utc('now + 7 days'), DateTimeFormat::utcNow()]);
182 $this->cache->set($cachekey, $events, Duration::HOUR);
185 $now_date = DateTimeFormat::localNow('Y-m-d');
186 foreach ($events as $event) {
187 $is_birthday = false;
188 if ($event['type'] === 'birthday') {
195 if (DateTimeFormat::local($event['start'], 'Y-m-d') === $now_date) {
197 $today_birthday_count++;
199 $today_event_count++;
204 $owner = User::getOwnerDataById($this->session->getLocalUserId());
206 $navNotifications = array_map(function (Entity\Notification $notification) use ($owner) {
207 if (!$this->notify->shouldShowOnDesktop($notification)) {
210 if (($notification->type == Post\UserNotification::TYPE_NONE) && in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP])) {
214 return $this->formattedNavNotification->createFromNotification($notification);
215 } catch (NoMessageException $e) {
218 }, $notifications->getArrayCopy());
219 $navNotifications = array_filter($navNotifications);
221 $sysnotify_count = array_reduce($navNotifications, function (int $carry, ValueObject\FormattedNavNotification $navNotification) {
222 return $carry + ($navNotification->seen ? 0 : 1);
225 // merge all notification types in one array
226 foreach ($intros as $intro) {
227 $navNotifications[] = $this->formattedNavNotification->createFromIntro($intro);
230 if (count($registrations) <= 1 || $this->pconfig->get($this->session->getLocalUserId(), 'system', 'detailed_notif')) {
231 foreach ($registrations as $reg) {
232 $navNotifications[] = $this->formattedNavNotification->createFromParams(
234 'name' => $reg['name'],
235 'url' => $reg['url'],
237 $this->l10n->t('{0} requested registration'),
238 new \DateTime($reg['created'], new \DateTimeZone('UTC')),
239 new Uri($this->baseUrl->get(true) . '/admin/users/pending')
242 } elseif (count($registrations) > 1) {
243 $navNotifications[] = $this->formattedNavNotification->createFromParams(
245 'name' => $registrations[0]['name'],
246 'url' => $registrations[0]['url'],
248 $this->l10n->t('{0} and %d others requested registration', count($registrations) - 1),
249 new \DateTime($registrations[0]['created'], new \DateTimeZone('UTC')),
250 new Uri($this->baseUrl->get(true) . '/admin/users/pending')
254 // sort notifications by $[]['date']
255 $sort_function = function (ValueObject\FormattedNavNotification $a, ValueObject\FormattedNavNotification $b) {
259 // Unseen messages are kept at the top
260 if ($a['seen'] == $b['seen']) {
261 if ($a['timestamp'] == $b['timestamp']) {
264 return $a['timestamp'] < $b['timestamp'] ? 1 : -1;
267 return $a['seen'] ? 1 : -1;
270 usort($navNotifications, $sort_function);
273 $notification_count = $sysnotify_count + $intro_count + $register_count;
276 $data['intro'] = $intro_count;
277 $data['mail'] = $mail_count;
278 $data['net'] = ($network_count < 1000) ? $network_count : '999+';
279 $data['home'] = ($home_count < 1000) ? $home_count : '999+';
280 $data['register'] = $register_count;
282 $data['events'] = $event_count;
283 $data['events-today'] = $today_event_count;
284 $data['birthdays'] = $birthday_count;
285 $data['birthdays-today'] = $today_birthday_count;
286 $data['groups'] = $groups_unseen;
287 $data['forums'] = $forums_unseen;
288 $data['notification'] = ($notification_count < 50) ? $notification_count : '49+';
290 $data['notifications'] = $navNotifications;
293 'notice' => $this->systemMessages->flushNotices(),
294 'info' => $this->systemMessages->flushInfos(),
297 if (isset($_GET['callback'])) {
299 System::httpExit($_GET['callback'] . '(' . json_encode(['result' => $data]) . ')', Response::TYPE_BLANK, 'application/javascript');
301 System::jsonExit(['result' => $data]);