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 // 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`"]);
136 $items_unseen = $this->database->toArray(Post::selectForUser(
137 $this->session->getLocalUserId(),
138 ['wall', 'uid', 'uri-id'],
142 $arr = ['items' => $items_unseen];
143 Hook::callAll('network_ping', $arr);
145 foreach ($items_unseen as $item) {
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;
162 foreach (ForumManager::countUnseenItems() as $forum_count) {
163 if ($forum_count['count'] > 0) {
164 $forums_unseen[] = $forum_count;
169 $intros = $this->introductionRepo->selectForUser($this->session->getLocalUserId());
171 $intro_count = $intros->count();
173 $myurl = $this->session->getMyUrl();
174 $mail_count = $this->database->count('mail', ["`uid` = ? AND NOT `seen` AND `from-url` != ?", $this->session->getLocalUserId(), $myurl]);
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);
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);
190 $now_date = DateTimeFormat::localNow('Y-m-d');
191 foreach ($events as $event) {
192 $is_birthday = false;
193 if ($event['type'] === 'birthday') {
200 if (DateTimeFormat::local($event['start'], 'Y-m-d') === $now_date) {
202 $today_birthday_count++;
204 $today_event_count++;
209 $owner = User::getOwnerDataById($this->session->getLocalUserId());
211 $navNotifications = array_map(function (Entity\Notification $notification) use ($owner) {
212 if (!$this->notify->shouldShowOnDesktop($notification)) {
215 if (($notification->type == Post\UserNotification::TYPE_NONE) && in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP])) {
219 return $this->formattedNavNotification->createFromNotification($notification);
220 } catch (NoMessageException $e) {
223 }, $notifications->getArrayCopy());
224 $navNotifications = array_filter($navNotifications);
226 $sysnotify_count = array_reduce($navNotifications, function (int $carry, ValueObject\FormattedNavNotification $navNotification) {
227 return $carry + ($navNotification->seen ? 0 : 1);
230 // merge all notification types in one array
231 foreach ($intros as $intro) {
232 $navNotifications[] = $this->formattedNavNotification->createFromIntro($intro);
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')
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')
255 // sort notifications by $[]['date']
256 $sort_function = function (ValueObject\FormattedNavNotification $a, ValueObject\FormattedNavNotification $b) {
260 // Unseen messages are kept at the top
261 if ($a['seen'] == $b['seen']) {
262 if ($a['timestamp'] == $b['timestamp']) {
265 return $a['timestamp'] < $b['timestamp'] ? 1 : -1;
268 return $a['seen'] ? 1 : -1;
271 usort($navNotifications, $sort_function);
274 $notification_count = $sysnotify_count + $intro_count + $register_count;
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;
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+';
291 $data['notifications'] = $navNotifications;
294 'notice' => $this->systemMessages->flushNotices(),
295 'info' => $this->systemMessages->flushInfos(),
298 if (isset($_GET['callback'])) {
300 System::httpExit($_GET['callback'] . '(' . json_encode(['result' => $data]) . ')', Response::TYPE_BLANK, 'application/javascript');
302 System::jsonExit(['result' => $data]);