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\Protocol\Activity;
46 use Friendica\Util\DateTimeFormat;
47 use Friendica\Util\Profiler;
48 use GuzzleHttp\Psr7\Uri;
49 use Psr\Log\LoggerInterface;
51 class Ping extends BaseModule
53 /** @var Repository\Notification */
54 private $notificationRepo;
55 /** @var Introduction */
56 private $introductionRepo;
57 /** @var Factory\FormattedNavNotification */
58 private $formattedNavNotification;
60 public function __construct(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 = [])
62 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
64 $this->notificationRepo = $notificationRepo;
65 $this->introductionRepo = $introductionRepo;
66 $this->formattedNavNotification = $formattedNavNotification;
69 protected function rawContent(array $request = [])
72 $navNotifications = [];
84 $today_event_count = 0;
86 $today_birthday_count = 0;
90 if (DI::pConfig()->get(local_user(), 'system', 'detailed_notif')) {
91 $notifications = $this->notificationRepo->selectDetailedForUser(local_user());
93 $notifications = $this->notificationRepo->selectDigestForUser(local_user());
97 "`unseen` AND `uid` = ? AND NOT `origin` AND (`vid` != ? OR `vid` IS NULL)",
98 local_user(), Verb::getID(Activity::FOLLOW)
100 $items = Post::selectForUser(local_user(), ['wall', 'uid', 'uri-id'], $condition, ['limit' => 1000]);
101 if (DBA::isResult($items)) {
102 $items_unseen = Post::toArray($items, false);
103 $arr = ['items' => $items_unseen];
104 Hook::callAll('network_ping', $arr);
106 foreach ($items_unseen as $item) {
116 if ($network_count) {
117 // Find out how unseen network posts are spread across groups
118 $group_counts = Group::countUnseen();
119 if (DBA::isResult($group_counts)) {
120 foreach ($group_counts as $group_count) {
121 if ($group_count['count'] > 0) {
122 $groups_unseen[] = $group_count;
127 $forum_counts = ForumManager::countUnseenItems();
128 if (DBA::isResult($forum_counts)) {
129 foreach ($forum_counts as $forum_count) {
130 if ($forum_count['count'] > 0) {
131 $forums_unseen[] = $forum_count;
137 $intros = $this->introductionRepo->selectForUser(local_user());
139 $intro_count = $intros->count();
141 $myurl = DI::baseUrl() . '/profile/' . DI::app()->getLoggedInUserNickname();
142 $mail_count = DBA::count('mail', ["`uid` = ? AND NOT `seen` AND `from-url` != ?", local_user(), $myurl]);
144 if (intval(DI::config()->get('config', 'register_policy')) === Register::APPROVE && DI::app()->isSiteAdmin()) {
145 $regs = \Friendica\Model\Register::getPending();
147 if (DBA::isResult($regs)) {
148 $register_count = count($regs);
152 $cachekey = 'ping:events:' . local_user();
153 $ev = DI::cache()->get($cachekey);
155 $ev = DBA::selectToArray('event', ['type', 'start'],
156 ["`uid` = ? AND `start` < ? AND `finish` > ? AND NOT `ignore`",
157 local_user(), DateTimeFormat::utc('now + 7 days'), DateTimeFormat::utcNow()]);
158 if (DBA::isResult($ev)) {
159 DI::cache()->set($cachekey, $ev, Duration::HOUR);
163 if (DBA::isResult($ev)) {
164 $all_events = count($ev);
167 $str_now = DateTimeFormat::localNow('Y-m-d');
168 foreach ($ev as $x) {
170 if ($x['type'] === 'birthday') {
176 if (DateTimeFormat::local($x['start'], 'Y-m-d') === $str_now) {
178 $today_birthday_count++;
180 $today_event_count++;
187 $owner = User::getOwnerDataById(local_user());
189 $navNotifications = array_map(function (Entity\Notification $notification) use ($owner) {
190 if (($notification->type == Post\UserNotification::TYPE_NONE) && in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP])) {
194 return $this->formattedNavNotification->createFromNotification($notification);
195 } catch (NoMessageException $e) {
198 }, $notifications->getArrayCopy());
199 $navNotifications = array_filter($navNotifications);
201 $sysnotify_count = array_reduce($navNotifications, function (int $carry, ValueObject\FormattedNavNotification $navNotification) {
202 return $carry + ($navNotification->seen ? 0 : 1);
205 // merge all notification types in one array
206 foreach ($intros as $intro) {
207 $navNotifications[] = $this->formattedNavNotification->createFromIntro($intro);
210 if (DBA::isResult($regs)) {
211 if (count($regs) <= 1 || DI::pConfig()->get(local_user(), 'system', 'detailed_notif')) {
212 foreach ($regs as $reg) {
213 $navNotifications[] = $this->formattedNavNotification->createFromParams(
215 'name' => $reg['name'],
216 'url' => $reg['url'],
218 DI::l10n()->t('{0} requested registration'),
219 new \DateTime($reg['created'], new \DateTimeZone('UTC')),
220 new Uri(DI::baseUrl()->get(true) . '/admin/users/pending')
224 $navNotifications[] = $this->formattedNavNotification->createFromParams(
226 'name' => $regs[0]['name'],
227 'url' => $regs[0]['url'],
229 DI::l10n()->t('{0} and %d others requested registration', count($regs) - 1),
230 new \DateTime($regs[0]['created'], new \DateTimeZone('UTC')),
231 new Uri(DI::baseUrl()->get(true) . '/admin/users/pending')
236 // sort notifications by $[]['date']
237 $sort_function = function (ValueObject\FormattedNavNotification $a, ValueObject\FormattedNavNotification $b) {
241 // Unseen messages are kept at the top
242 if ($a['seen'] == $b['seen']) {
243 if ($a['timestamp'] == $b['timestamp']) {
246 return $a['timestamp'] < $b['timestamp'] ? 1 : -1;
249 return $a['seen'] ? 1 : -1;
252 usort($navNotifications, $sort_function);
258 if (!empty($_SESSION['sysmsg'])) {
259 $sysmsgs = $_SESSION['sysmsg'];
260 unset($_SESSION['sysmsg']);
263 if (!empty($_SESSION['sysmsg_info'])) {
264 $sysmsgs_info = $_SESSION['sysmsg_info'];
265 unset($_SESSION['sysmsg_info']);
268 $notification_count = $sysnotify_count + $intro_count + $register_count;
271 $data['intro'] = $intro_count;
272 $data['mail'] = $mail_count;
273 $data['net'] = ($network_count < 1000) ? $network_count : '999+';
274 $data['home'] = ($home_count < 1000) ? $home_count : '999+';
275 $data['register'] = $register_count;
277 $data['events'] = $event_count;
278 $data['events-today'] = $today_event_count;
279 $data['birthdays'] = $birthday_count;
280 $data['birthdays-today'] = $today_birthday_count;
281 $data['groups'] = $groups_unseen;
282 $data['forums'] = $forums_unseen;
283 $data['notification'] = ($notification_count < 50) ? $notification_count : '49+';
285 $data['notifications'] = $navNotifications;
288 'notice' => $sysmsgs,
289 'info' => $sysmsgs_info
292 if (isset($_GET['callback'])) {
294 header("Content-type: application/javascript");
295 echo $_GET['callback'] . '(' . json_encode(['result' => $data]) . ')';
298 System::jsonExit(['result' => $data]);