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\Enum\Duration;
29 use Friendica\Core\Hook;
30 use Friendica\Core\L10n;
31 use Friendica\Core\System;
32 use Friendica\Database\DBA;
34 use Friendica\Model\Group;
35 use Friendica\Model\Post;
36 use Friendica\Model\User;
37 use Friendica\Model\Verb;
38 use Friendica\Module\Register;
39 use Friendica\Module\Response;
40 use Friendica\Navigation\Notifications\Entity;
41 use Friendica\Navigation\Notifications\Exception\NoMessageException;
42 use Friendica\Navigation\Notifications\Factory;
43 use Friendica\Navigation\Notifications\Repository;
44 use Friendica\Navigation\Notifications\ValueObject;
45 use Friendica\Navigation\SystemMessages;
46 use Friendica\Protocol\Activity;
47 use Friendica\Util\DateTimeFormat;
48 use Friendica\Util\Profiler;
49 use GuzzleHttp\Psr7\Uri;
50 use Psr\Log\LoggerInterface;
52 class Ping extends BaseModule
54 /** @var SystemMessages */
55 private $systemMessages;
56 /** @var Repository\Notification */
57 private $notificationRepo;
58 /** @var Introduction */
59 private $introductionRepo;
60 /** @var Factory\FormattedNavNotification */
61 private $formattedNavNotification;
63 public function __construct(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 = [])
65 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
67 $this->systemMessages = $systemMessages;
68 $this->notificationRepo = $notificationRepo;
69 $this->introductionRepo = $introductionRepo;
70 $this->formattedNavNotification = $formattedNavNotification;
73 protected function rawContent(array $request = [])
76 $navNotifications = [];
88 $today_event_count = 0;
90 $today_birthday_count = 0;
93 if (DI::userSession()->getLocalUserId()) {
94 if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'detailed_notif')) {
95 $notifications = $this->notificationRepo->selectDetailedForUser(DI::userSession()->getLocalUserId());
97 $notifications = $this->notificationRepo->selectDigestForUser(DI::userSession()->getLocalUserId());
101 "`unseen` AND `uid` = ? AND NOT `origin` AND (`vid` != ? OR `vid` IS NULL)",
102 DI::userSession()->getLocalUserId(), Verb::getID(Activity::FOLLOW)
104 $items = Post::selectForUser(DI::userSession()->getLocalUserId(), ['wall', 'uid', 'uri-id'], $condition, ['limit' => 1000]);
105 if (DBA::isResult($items)) {
106 $items_unseen = Post::toArray($items, false);
107 $arr = ['items' => $items_unseen];
108 Hook::callAll('network_ping', $arr);
110 foreach ($items_unseen as $item) {
120 $compute_group_counts = DI::config()->get('system','compute_group_counts');
121 if ($network_count && $compute_group_counts) {
122 // Find out how unseen network posts are spread across groups
123 $group_counts = Group::countUnseen();
124 if (DBA::isResult($group_counts)) {
125 foreach ($group_counts as $group_count) {
126 if ($group_count['count'] > 0) {
127 $groups_unseen[] = $group_count;
132 $forum_counts = ForumManager::countUnseenItems();
133 if (DBA::isResult($forum_counts)) {
134 foreach ($forum_counts as $forum_count) {
135 if ($forum_count['count'] > 0) {
136 $forums_unseen[] = $forum_count;
142 $intros = $this->introductionRepo->selectForUser(DI::userSession()->getLocalUserId());
144 $intro_count = $intros->count();
146 $myurl = DI::baseUrl() . '/profile/' . DI::app()->getLoggedInUserNickname();
147 $mail_count = DBA::count('mail', ["`uid` = ? AND NOT `seen` AND `from-url` != ?", DI::userSession()->getLocalUserId(), $myurl]);
149 if (intval(DI::config()->get('config', 'register_policy')) === Register::APPROVE && DI::app()->isSiteAdmin()) {
150 $regs = \Friendica\Model\Register::getPending();
152 if (DBA::isResult($regs)) {
153 $register_count = count($regs);
157 $cachekey = 'ping:events:' . DI::userSession()->getLocalUserId();
158 $ev = DI::cache()->get($cachekey);
160 $ev = DBA::selectToArray('event', ['type', 'start'],
161 ["`uid` = ? AND `start` < ? AND `finish` > ? AND NOT `ignore`",
162 DI::userSession()->getLocalUserId(), DateTimeFormat::utc('now + 7 days'), DateTimeFormat::utcNow()]);
163 DI::cache()->set($cachekey, $ev, Duration::HOUR);
166 if (DBA::isResult($ev)) {
167 $all_events = count($ev);
170 $str_now = DateTimeFormat::localNow('Y-m-d');
171 foreach ($ev as $x) {
173 if ($x['type'] === 'birthday') {
179 if (DateTimeFormat::local($x['start'], 'Y-m-d') === $str_now) {
181 $today_birthday_count++;
183 $today_event_count++;
190 $owner = User::getOwnerDataById(DI::userSession()->getLocalUserId());
192 $navNotifications = array_map(function (Entity\Notification $notification) use ($owner) {
193 if (!DI::notify()->NotifyOnDesktop($notification)) {
196 if (($notification->type == Post\UserNotification::TYPE_NONE) && in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP])) {
200 return $this->formattedNavNotification->createFromNotification($notification);
201 } catch (NoMessageException $e) {
204 }, $notifications->getArrayCopy());
205 $navNotifications = array_filter($navNotifications);
207 $sysnotify_count = array_reduce($navNotifications, function (int $carry, ValueObject\FormattedNavNotification $navNotification) {
208 return $carry + ($navNotification->seen ? 0 : 1);
211 // merge all notification types in one array
212 foreach ($intros as $intro) {
213 $navNotifications[] = $this->formattedNavNotification->createFromIntro($intro);
216 if (DBA::isResult($regs)) {
217 if (count($regs) <= 1 || DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'detailed_notif')) {
218 foreach ($regs as $reg) {
219 $navNotifications[] = $this->formattedNavNotification->createFromParams(
221 'name' => $reg['name'],
222 'url' => $reg['url'],
224 DI::l10n()->t('{0} requested registration'),
225 new \DateTime($reg['created'], new \DateTimeZone('UTC')),
226 new Uri(DI::baseUrl()->get(true) . '/admin/users/pending')
230 $navNotifications[] = $this->formattedNavNotification->createFromParams(
232 'name' => $regs[0]['name'],
233 'url' => $regs[0]['url'],
235 DI::l10n()->t('{0} and %d others requested registration', count($regs) - 1),
236 new \DateTime($regs[0]['created'], new \DateTimeZone('UTC')),
237 new Uri(DI::baseUrl()->get(true) . '/admin/users/pending')
242 // sort notifications by $[]['date']
243 $sort_function = function (ValueObject\FormattedNavNotification $a, ValueObject\FormattedNavNotification $b) {
247 // Unseen messages are kept at the top
248 if ($a['seen'] == $b['seen']) {
249 if ($a['timestamp'] == $b['timestamp']) {
252 return $a['timestamp'] < $b['timestamp'] ? 1 : -1;
255 return $a['seen'] ? 1 : -1;
258 usort($navNotifications, $sort_function);
261 $notification_count = $sysnotify_count + $intro_count + $register_count;
264 $data['intro'] = $intro_count;
265 $data['mail'] = $mail_count;
266 $data['net'] = ($network_count < 1000) ? $network_count : '999+';
267 $data['home'] = ($home_count < 1000) ? $home_count : '999+';
268 $data['register'] = $register_count;
270 $data['events'] = $event_count;
271 $data['events-today'] = $today_event_count;
272 $data['birthdays'] = $birthday_count;
273 $data['birthdays-today'] = $today_birthday_count;
274 $data['groups'] = $groups_unseen;
275 $data['forums'] = $forums_unseen;
276 $data['notification'] = ($notification_count < 50) ? $notification_count : '49+';
278 $data['notifications'] = $navNotifications;
281 'notice' => $this->systemMessages->flushNotices(),
282 'info' => $this->systemMessages->flushInfos(),
285 if (isset($_GET['callback'])) {
287 System::httpExit($_GET['callback'] . '(' . json_encode(['result' => $data]) . ')', Response::TYPE_BLANK, 'application/javascript');
289 System::jsonExit(['result' => $data]);