2022-03-04 14:01:56 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Module\Notifications;
|
|
|
|
|
2022-03-05 20:34:09 +00:00
|
|
|
use Friendica\App;
|
2022-03-04 14:01:56 +00:00
|
|
|
use Friendica\BaseModule;
|
2022-03-05 20:34:09 +00:00
|
|
|
use Friendica\Contact\Introduction\Repository\Introduction;
|
2022-03-04 14:01:56 +00:00
|
|
|
use Friendica\Content\ForumManager;
|
2022-11-03 00:44:35 +00:00
|
|
|
use Friendica\Core\Cache\Capability\ICanCache;
|
2022-03-04 14:01:56 +00:00
|
|
|
use Friendica\Core\Cache\Enum\Duration;
|
2022-11-03 00:44:35 +00:00
|
|
|
use Friendica\Core\Config\Capability\IManageConfigValues;
|
2022-03-04 14:01:56 +00:00
|
|
|
use Friendica\Core\Hook;
|
2022-03-05 20:34:09 +00:00
|
|
|
use Friendica\Core\L10n;
|
2022-11-03 00:44:35 +00:00
|
|
|
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
|
|
|
|
use Friendica\Core\Session\Capability\IHandleUserSessions;
|
2022-03-05 20:34:09 +00:00
|
|
|
use Friendica\Core\System;
|
2022-11-03 00:44:35 +00:00
|
|
|
use Friendica\Database\Database;
|
2022-03-04 14:01:56 +00:00
|
|
|
use Friendica\Database\DBA;
|
|
|
|
use Friendica\Model\Group;
|
|
|
|
use Friendica\Model\Post;
|
2022-03-19 18:38:59 +00:00
|
|
|
use Friendica\Model\User;
|
2022-03-04 14:01:56 +00:00
|
|
|
use Friendica\Model\Verb;
|
2022-11-03 00:44:35 +00:00
|
|
|
use Friendica\Module\Conversation\Network;
|
2022-03-04 14:01:56 +00:00
|
|
|
use Friendica\Module\Register;
|
2022-03-05 20:34:09 +00:00
|
|
|
use Friendica\Module\Response;
|
2022-03-04 14:01:56 +00:00
|
|
|
use Friendica\Navigation\Notifications\Entity;
|
2022-03-15 01:53:33 +00:00
|
|
|
use Friendica\Navigation\Notifications\Exception\NoMessageException;
|
2022-03-05 20:34:09 +00:00
|
|
|
use Friendica\Navigation\Notifications\Factory;
|
|
|
|
use Friendica\Navigation\Notifications\Repository;
|
|
|
|
use Friendica\Navigation\Notifications\ValueObject;
|
2022-07-27 15:54:50 +00:00
|
|
|
use Friendica\Navigation\SystemMessages;
|
2022-12-08 14:24:06 +00:00
|
|
|
use Friendica\Network\HTTPException;
|
2022-03-04 14:01:56 +00:00
|
|
|
use Friendica\Protocol\Activity;
|
|
|
|
use Friendica\Util\DateTimeFormat;
|
2022-03-05 20:34:09 +00:00
|
|
|
use Friendica\Util\Profiler;
|
|
|
|
use GuzzleHttp\Psr7\Uri;
|
|
|
|
use Psr\Log\LoggerInterface;
|
2022-03-04 14:01:56 +00:00
|
|
|
|
|
|
|
class Ping extends BaseModule
|
|
|
|
{
|
2022-07-27 15:54:50 +00:00
|
|
|
/** @var SystemMessages */
|
|
|
|
private $systemMessages;
|
2022-03-05 20:34:09 +00:00
|
|
|
/** @var Repository\Notification */
|
|
|
|
private $notificationRepo;
|
|
|
|
/** @var Introduction */
|
|
|
|
private $introductionRepo;
|
|
|
|
/** @var Factory\FormattedNavNotification */
|
|
|
|
private $formattedNavNotification;
|
2022-11-03 00:44:35 +00:00
|
|
|
/** @var IHandleUserSessions */
|
|
|
|
private $session;
|
|
|
|
/** @var IManageConfigValues */
|
|
|
|
private $config;
|
|
|
|
/** @var IManagePersonalConfigValues */
|
|
|
|
private $pconfig;
|
|
|
|
/** @var Database */
|
|
|
|
private $database;
|
|
|
|
/** @var ICanCache */
|
|
|
|
private $cache;
|
|
|
|
/** @var Repository\Notify */
|
|
|
|
private $notify;
|
|
|
|
/** @var App */
|
|
|
|
private $app;
|
2022-03-05 20:34:09 +00:00
|
|
|
|
2022-11-03 00:44:35 +00:00
|
|
|
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 = [])
|
2022-03-05 20:34:09 +00:00
|
|
|
{
|
|
|
|
parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
|
|
|
|
|
2022-07-27 15:54:50 +00:00
|
|
|
$this->systemMessages = $systemMessages;
|
2022-03-05 20:34:09 +00:00
|
|
|
$this->notificationRepo = $notificationRepo;
|
|
|
|
$this->introductionRepo = $introductionRepo;
|
|
|
|
$this->formattedNavNotification = $formattedNavNotification;
|
2022-11-03 00:44:35 +00:00
|
|
|
$this->session = $session;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->pconfig = $pconfig;
|
|
|
|
$this->database = $database;
|
|
|
|
$this->cache = $cache;
|
|
|
|
$this->notify = $notify;
|
|
|
|
$this->app = $app;
|
2022-03-05 20:34:09 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 14:01:56 +00:00
|
|
|
protected function rawContent(array $request = [])
|
|
|
|
{
|
2022-11-03 00:44:35 +00:00
|
|
|
$registrations = [];
|
2022-03-05 20:34:09 +00:00
|
|
|
$navNotifications = [];
|
|
|
|
|
|
|
|
$intro_count = 0;
|
|
|
|
$mail_count = 0;
|
|
|
|
$home_count = 0;
|
|
|
|
$network_count = 0;
|
|
|
|
$register_count = 0;
|
2022-03-04 14:01:56 +00:00
|
|
|
$sysnotify_count = 0;
|
2022-03-05 20:34:09 +00:00
|
|
|
$groups_unseen = [];
|
|
|
|
$forums_unseen = [];
|
2022-03-04 14:01:56 +00:00
|
|
|
|
2022-03-05 20:34:09 +00:00
|
|
|
$event_count = 0;
|
|
|
|
$today_event_count = 0;
|
|
|
|
$birthday_count = 0;
|
|
|
|
$today_birthday_count = 0;
|
2022-03-04 14:01:56 +00:00
|
|
|
|
2022-12-15 03:01:14 +00:00
|
|
|
// Suppress notification display for forum accounts
|
|
|
|
if ($this->session->getLocalUserId() && $this->session->get('page_flags', '') != User::PAGE_FLAGS_COMMUNITY) {
|
2022-11-03 00:44:35 +00:00
|
|
|
if ($this->pconfig->get($this->session->getLocalUserId(), 'system', 'detailed_notif')) {
|
|
|
|
$notifications = $this->notificationRepo->selectDetailedForUser($this->session->getLocalUserId());
|
2022-03-05 20:34:09 +00:00
|
|
|
} else {
|
2022-11-03 00:44:35 +00:00
|
|
|
$notifications = $this->notificationRepo->selectDigestForUser($this->session->getLocalUserId());
|
2022-03-05 20:34:09 +00:00
|
|
|
}
|
2022-03-04 14:01:56 +00:00
|
|
|
|
|
|
|
$condition = [
|
|
|
|
"`unseen` AND `uid` = ? AND NOT `origin` AND (`vid` != ? OR `vid` IS NULL)",
|
2022-11-03 00:44:35 +00:00
|
|
|
$this->session->getLocalUserId(), Verb::getID(Activity::FOLLOW)
|
2022-03-04 14:01:56 +00:00
|
|
|
];
|
|
|
|
|
2022-11-03 00:45:42 +00:00
|
|
|
// No point showing counts for non-top-level posts when the network page is ordered by received field
|
|
|
|
if (Network::getTimelineOrderBySession($this->session, $this->pconfig) == 'received') {
|
|
|
|
$condition = DBA::mergeConditions($condition, ["`parent` = `id`"]);
|
|
|
|
}
|
|
|
|
|
2022-11-03 00:44:35 +00:00
|
|
|
$items_unseen = $this->database->toArray(Post::selectForUser(
|
|
|
|
$this->session->getLocalUserId(),
|
|
|
|
['wall', 'uid', 'uri-id'],
|
|
|
|
$condition,
|
|
|
|
['limit' => 1000],
|
|
|
|
));
|
|
|
|
$arr = ['items' => $items_unseen];
|
|
|
|
Hook::callAll('network_ping', $arr);
|
|
|
|
|
|
|
|
foreach ($items_unseen as $item) {
|
|
|
|
if ($item['wall']) {
|
|
|
|
$home_count++;
|
|
|
|
} else {
|
|
|
|
$network_count++;
|
2022-03-04 14:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 00:44:35 +00:00
|
|
|
$compute_group_counts = $this->config->get('system','compute_group_counts');
|
2022-07-08 19:29:34 +00:00
|
|
|
if ($network_count && $compute_group_counts) {
|
2022-03-04 14:01:56 +00:00
|
|
|
// Find out how unseen network posts are spread across groups
|
2022-11-03 00:44:35 +00:00
|
|
|
foreach (Group::countUnseen() as $group_count) {
|
|
|
|
if ($group_count['count'] > 0) {
|
|
|
|
$groups_unseen[] = $group_count;
|
2022-03-04 14:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 00:44:35 +00:00
|
|
|
foreach (ForumManager::countUnseenItems() as $forum_count) {
|
|
|
|
if ($forum_count['count'] > 0) {
|
|
|
|
$forums_unseen[] = $forum_count;
|
2022-03-04 14:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 00:44:35 +00:00
|
|
|
$intros = $this->introductionRepo->selectForUser($this->session->getLocalUserId());
|
2022-03-05 20:34:09 +00:00
|
|
|
|
|
|
|
$intro_count = $intros->count();
|
|
|
|
|
2022-11-03 00:44:35 +00:00
|
|
|
$myurl = $this->session->getMyUrl();
|
|
|
|
$mail_count = $this->database->count('mail', ["`uid` = ? AND NOT `seen` AND `from-url` != ?", $this->session->getLocalUserId(), $myurl]);
|
2022-03-04 14:01:56 +00:00
|
|
|
|
2022-11-03 00:44:35 +00:00
|
|
|
if (intval($this->config->get('config', 'register_policy')) === Register::APPROVE && $this->app->isSiteAdmin()) {
|
|
|
|
$registrations = \Friendica\Model\Register::getPending();
|
|
|
|
$register_count = count($registrations);
|
2022-03-04 14:01:56 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 00:44:35 +00:00
|
|
|
$cachekey = 'ping:events:' . $this->session->getLocalUserId();
|
|
|
|
$events = $this->cache->get($cachekey);
|
|
|
|
if (is_null($events)) {
|
|
|
|
$events = $this->database->selectToArray('event', ['type', 'start'],
|
2022-03-04 14:01:56 +00:00
|
|
|
["`uid` = ? AND `start` < ? AND `finish` > ? AND NOT `ignore`",
|
2022-11-03 00:44:35 +00:00
|
|
|
$this->session->getLocalUserId(), DateTimeFormat::utc('now + 7 days'), DateTimeFormat::utcNow()]);
|
|
|
|
$this->cache->set($cachekey, $events, Duration::HOUR);
|
2022-03-04 14:01:56 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 00:44:35 +00:00
|
|
|
$now_date = DateTimeFormat::localNow('Y-m-d');
|
|
|
|
foreach ($events as $event) {
|
|
|
|
$is_birthday = false;
|
|
|
|
if ($event['type'] === 'birthday') {
|
|
|
|
$birthday_count++;
|
|
|
|
$is_birthday = true;
|
|
|
|
} else {
|
|
|
|
$event_count++;
|
|
|
|
}
|
2022-03-04 14:01:56 +00:00
|
|
|
|
2022-11-03 00:44:35 +00:00
|
|
|
if (DateTimeFormat::local($event['start'], 'Y-m-d') === $now_date) {
|
|
|
|
if ($is_birthday) {
|
|
|
|
$today_birthday_count++;
|
|
|
|
} else {
|
|
|
|
$today_event_count++;
|
2022-03-04 14:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 00:44:35 +00:00
|
|
|
$owner = User::getOwnerDataById($this->session->getLocalUserId());
|
2022-03-19 18:38:59 +00:00
|
|
|
|
|
|
|
$navNotifications = array_map(function (Entity\Notification $notification) use ($owner) {
|
2022-11-03 00:44:35 +00:00
|
|
|
if (!$this->notify->shouldShowOnDesktop($notification)) {
|
2022-05-29 09:20:06 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-03-19 18:38:59 +00:00
|
|
|
if (($notification->type == Post\UserNotification::TYPE_NONE) && in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP])) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-03-15 01:53:33 +00:00
|
|
|
try {
|
|
|
|
return $this->formattedNavNotification->createFromNotification($notification);
|
|
|
|
} catch (NoMessageException $e) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-03-05 20:34:09 +00:00
|
|
|
}, $notifications->getArrayCopy());
|
2022-03-15 01:53:33 +00:00
|
|
|
$navNotifications = array_filter($navNotifications);
|
|
|
|
|
|
|
|
$sysnotify_count = array_reduce($navNotifications, function (int $carry, ValueObject\FormattedNavNotification $navNotification) {
|
|
|
|
return $carry + ($navNotification->seen ? 0 : 1);
|
|
|
|
}, 0);
|
2022-03-04 14:01:56 +00:00
|
|
|
|
|
|
|
// merge all notification types in one array
|
2022-03-05 20:34:09 +00:00
|
|
|
foreach ($intros as $intro) {
|
2022-12-08 14:24:06 +00:00
|
|
|
try {
|
|
|
|
$navNotifications[] = $this->formattedNavNotification->createFromIntro($intro);
|
|
|
|
} catch (HTTPException\NotFoundException $e) {
|
|
|
|
$this->introductionRepo->delete($intro);
|
|
|
|
}
|
2022-03-04 14:01:56 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 00:44:35 +00:00
|
|
|
if (count($registrations) <= 1 || $this->pconfig->get($this->session->getLocalUserId(), 'system', 'detailed_notif')) {
|
2022-11-10 15:40:37 +00:00
|
|
|
foreach ($registrations as $registration) {
|
2022-03-05 20:34:09 +00:00
|
|
|
$navNotifications[] = $this->formattedNavNotification->createFromParams(
|
2022-11-10 15:40:37 +00:00
|
|
|
$registration['name'],
|
|
|
|
$registration['url'],
|
2022-11-03 00:44:35 +00:00
|
|
|
$this->l10n->t('{0} requested registration'),
|
2022-11-10 15:40:37 +00:00
|
|
|
new \DateTime($registration['created'], new \DateTimeZone('UTC')),
|
2022-11-08 09:26:11 +00:00
|
|
|
new Uri($this->baseUrl->get(true) . '/moderation/users/pending')
|
2022-03-05 20:34:09 +00:00
|
|
|
);
|
2022-03-04 14:01:56 +00:00
|
|
|
}
|
2022-12-08 14:24:06 +00:00
|
|
|
} else {
|
2022-11-03 00:44:35 +00:00
|
|
|
$navNotifications[] = $this->formattedNavNotification->createFromParams(
|
2022-11-10 15:40:37 +00:00
|
|
|
$registrations[0]['name'],
|
|
|
|
$registrations[0]['url'],
|
2022-11-03 00:44:35 +00:00
|
|
|
$this->l10n->t('{0} and %d others requested registration', count($registrations) - 1),
|
|
|
|
new \DateTime($registrations[0]['created'], new \DateTimeZone('UTC')),
|
2022-11-08 09:26:11 +00:00
|
|
|
new Uri($this->baseUrl->get(true) . '/moderation/users/pending')
|
2022-11-03 00:44:35 +00:00
|
|
|
);
|
2022-03-04 14:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sort notifications by $[]['date']
|
2022-03-05 20:34:09 +00:00
|
|
|
$sort_function = function (ValueObject\FormattedNavNotification $a, ValueObject\FormattedNavNotification $b) {
|
|
|
|
$a = $a->toArray();
|
|
|
|
$b = $b->toArray();
|
2022-03-04 14:01:56 +00:00
|
|
|
|
|
|
|
// Unseen messages are kept at the top
|
2022-03-05 20:34:09 +00:00
|
|
|
if ($a['seen'] == $b['seen']) {
|
|
|
|
if ($a['timestamp'] == $b['timestamp']) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return $a['timestamp'] < $b['timestamp'] ? 1 : -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $a['seen'] ? 1 : -1;
|
2022-03-04 14:01:56 +00:00
|
|
|
}
|
|
|
|
};
|
2022-03-05 20:34:09 +00:00
|
|
|
usort($navNotifications, $sort_function);
|
2022-03-04 14:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$notification_count = $sysnotify_count + $intro_count + $register_count;
|
|
|
|
|
2022-03-05 20:34:09 +00:00
|
|
|
$data = [];
|
2022-03-04 14:01:56 +00:00
|
|
|
$data['intro'] = $intro_count;
|
|
|
|
$data['mail'] = $mail_count;
|
|
|
|
$data['net'] = ($network_count < 1000) ? $network_count : '999+';
|
|
|
|
$data['home'] = ($home_count < 1000) ? $home_count : '999+';
|
|
|
|
$data['register'] = $register_count;
|
|
|
|
|
2022-03-05 20:34:09 +00:00
|
|
|
$data['events'] = $event_count;
|
|
|
|
$data['events-today'] = $today_event_count;
|
|
|
|
$data['birthdays'] = $birthday_count;
|
|
|
|
$data['birthdays-today'] = $today_birthday_count;
|
|
|
|
$data['groups'] = $groups_unseen;
|
|
|
|
$data['forums'] = $forums_unseen;
|
|
|
|
$data['notification'] = ($notification_count < 50) ? $notification_count : '49+';
|
|
|
|
|
|
|
|
$data['notifications'] = $navNotifications;
|
|
|
|
|
2022-03-04 14:01:56 +00:00
|
|
|
$data['sysmsgs'] = [
|
2022-07-27 15:54:50 +00:00
|
|
|
'notice' => $this->systemMessages->flushNotices(),
|
|
|
|
'info' => $this->systemMessages->flushInfos(),
|
2022-03-04 14:01:56 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if (isset($_GET['callback'])) {
|
|
|
|
// JSONP support
|
2022-04-10 08:31:55 +00:00
|
|
|
System::httpExit($_GET['callback'] . '(' . json_encode(['result' => $data]) . ')', Response::TYPE_BLANK, 'application/javascript');
|
2022-03-04 14:01:56 +00:00
|
|
|
} else {
|
2022-03-05 20:34:09 +00:00
|
|
|
System::jsonExit(['result' => $data]);
|
2022-03-04 14:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|