3 * @copyright Copyright (C) 2010-2023, 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\GroupManager;
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\Circle;
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\Network\HTTPException;
52 use Friendica\Protocol\Activity;
53 use Friendica\Util\DateTimeFormat;
54 use Friendica\Util\Profiler;
55 use GuzzleHttp\Psr7\Uri;
56 use Psr\Log\LoggerInterface;
58 class Ping extends BaseModule
60 /** @var SystemMessages */
61 private $systemMessages;
62 /** @var Repository\Notification */
63 private $notificationRepo;
64 /** @var Introduction */
65 private $introductionRepo;
66 /** @var Factory\FormattedNavNotification */
67 private $formattedNavNotification;
68 /** @var IHandleUserSessions */
70 /** @var IManageConfigValues */
72 /** @var IManagePersonalConfigValues */
78 /** @var Repository\Notify */
83 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 = [])
85 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
87 $this->systemMessages = $systemMessages;
88 $this->notificationRepo = $notificationRepo;
89 $this->introductionRepo = $introductionRepo;
90 $this->formattedNavNotification = $formattedNavNotification;
91 $this->session = $session;
92 $this->config = $config;
93 $this->pconfig = $pconfig;
94 $this->database = $database;
95 $this->cache = $cache;
96 $this->notify = $notify;
100 protected function rawContent(array $request = [])
103 $navNotifications = [];
110 $sysnotify_count = 0;
111 $circles_unseen = [];
115 $today_event_count = 0;
117 $today_birthday_count = 0;
119 // Suppress notification display for group accounts
120 if ($this->session->getLocalUserId() && $this->session->get('page_flags', '') != User::PAGE_FLAGS_COMMUNITY) {
121 if ($this->pconfig->get($this->session->getLocalUserId(), 'system', 'detailed_notif')) {
122 $notifications = $this->notificationRepo->selectDetailedForUser($this->session->getLocalUserId());
124 $notifications = $this->notificationRepo->selectDigestForUser($this->session->getLocalUserId());
128 "`unseen` AND `uid` = ? AND NOT `origin` AND (`vid` != ? OR `vid` IS NULL)",
129 $this->session->getLocalUserId(), Verb::getID(Activity::FOLLOW)
132 // No point showing counts for non-top-level posts when the network page is ordered by received field
133 if (Network::getTimelineOrderBySession($this->session, $this->pconfig) == 'received') {
134 $condition = DBA::mergeConditions($condition, ["`parent` = `id`"]);
137 $items_unseen = $this->database->toArray(Post::selectForUser(
138 $this->session->getLocalUserId(),
139 ['wall', 'uid', 'uri-id'],
143 $arr = ['items' => $items_unseen];
144 Hook::callAll('network_ping', $arr);
146 foreach ($items_unseen as $item) {
154 $compute_circle_counts = $this->config->get('system','compute_group_counts') ?? $this->config->get('system','compute_circle_counts');
155 if ($network_count && $compute_circle_counts) {
156 // Find out how unseen network posts are spread across circles
157 foreach (Circle::countUnseen() as $circle_count) {
158 if ($circle_count['count'] > 0) {
159 $circles_unseen[] = $circle_count;
163 foreach (GroupManager::countUnseenItems() as $group_count) {
164 if ($group_count['count'] > 0) {
165 $groups_unseen[] = $group_count;
170 $intros = $this->introductionRepo->selectForUser($this->session->getLocalUserId());
172 $intro_count = $intros->count();
174 $myurl = $this->session->getMyUrl();
175 $mail_count = $this->database->count('mail', ["`uid` = ? AND NOT `seen` AND `from-url` != ?", $this->session->getLocalUserId(), $myurl]);
177 if (intval($this->config->get('config', 'register_policy')) === Register::APPROVE && $this->app->isSiteAdmin()) {
178 $registrations = \Friendica\Model\Register::getPending();
179 $register_count = count($registrations);
182 $cachekey = 'ping:events:' . $this->session->getLocalUserId();
183 $events = $this->cache->get($cachekey);
184 if (is_null($events)) {
185 $events = $this->database->selectToArray('event', ['type', 'start'],
186 ["`uid` = ? AND `start` < ? AND `finish` > ? AND NOT `ignore`",
187 $this->session->getLocalUserId(), DateTimeFormat::utc('now + 7 days'), DateTimeFormat::utcNow()]);
188 $this->cache->set($cachekey, $events, Duration::HOUR);
191 $now_date = DateTimeFormat::localNow('Y-m-d');
192 foreach ($events as $event) {
193 $is_birthday = false;
194 if ($event['type'] === 'birthday') {
201 if (DateTimeFormat::local($event['start'], 'Y-m-d') === $now_date) {
203 $today_birthday_count++;
205 $today_event_count++;
210 $owner = User::getOwnerDataById($this->session->getLocalUserId());
212 $navNotifications = array_map(function (Entity\Notification $notification) use ($owner) {
213 if (!$this->notify->shouldShowOnDesktop($notification)) {
216 if (($notification->type == Post\UserNotification::TYPE_NONE) && in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP])) {
220 return $this->formattedNavNotification->createFromNotification($notification);
221 } catch (NoMessageException $e) {
224 }, $notifications->getArrayCopy());
225 $navNotifications = array_filter($navNotifications);
227 $sysnotify_count = array_reduce($navNotifications, function (int $carry, ValueObject\FormattedNavNotification $navNotification) {
228 return $carry + ($navNotification->seen ? 0 : 1);
231 // merge all notification types in one array
232 foreach ($intros as $intro) {
234 $navNotifications[] = $this->formattedNavNotification->createFromIntro($intro);
235 } catch (HTTPException\NotFoundException $e) {
236 $this->introductionRepo->delete($intro);
240 if (count($registrations) <= 1 || $this->pconfig->get($this->session->getLocalUserId(), 'system', 'detailed_notif')) {
241 foreach ($registrations as $registration) {
242 $navNotifications[] = $this->formattedNavNotification->createFromParams(
243 $registration['name'],
244 $registration['url'],
245 $this->l10n->t('{0} requested registration'),
246 new \DateTime($registration['created'], new \DateTimeZone('UTC')),
247 new Uri($this->baseUrl . '/moderation/users/pending')
251 $navNotifications[] = $this->formattedNavNotification->createFromParams(
252 $registrations[0]['name'],
253 $registrations[0]['url'],
254 $this->l10n->t('{0} and %d others requested registration', count($registrations) - 1),
255 new \DateTime($registrations[0]['created'], new \DateTimeZone('UTC')),
256 new Uri($this->baseUrl . '/moderation/users/pending')
260 // sort notifications by $[]['date']
261 $sort_function = function (ValueObject\FormattedNavNotification $a, ValueObject\FormattedNavNotification $b) {
265 // Unseen messages are kept at the top
266 if ($a['seen'] == $b['seen']) {
267 if ($a['timestamp'] == $b['timestamp']) {
270 return $a['timestamp'] < $b['timestamp'] ? 1 : -1;
273 return $a['seen'] ? 1 : -1;
276 usort($navNotifications, $sort_function);
279 $notification_count = $sysnotify_count + $intro_count + $register_count;
282 $data['intro'] = $intro_count;
283 $data['mail'] = $mail_count;
284 $data['net'] = ($network_count < 1000) ? $network_count : '999+';
285 $data['home'] = ($home_count < 1000) ? $home_count : '999+';
286 $data['register'] = $register_count;
288 $data['events'] = $event_count;
289 $data['events-today'] = $today_event_count;
290 $data['birthdays'] = $birthday_count;
291 $data['birthdays-today'] = $today_birthday_count;
292 $data['circles'] = $circles_unseen;
293 $data['groups'] = $groups_unseen;
294 $data['notification'] = ($notification_count < 50) ? $notification_count : '49+';
296 $data['notifications'] = $navNotifications;
299 'notice' => $this->systemMessages->flushNotices(),
300 'info' => $this->systemMessages->flushInfos(),
303 if (isset($_GET['callback'])) {
305 System::httpExit($_GET['callback'] . '(' . json_encode(['result' => $data]) . ')', Response::TYPE_BLANK, 'application/javascript');
307 System::jsonExit(['result' => $data]);