]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Notifications/Ping.php
Merge pull request #12051 from Quix0r/hotfix/one-dollar-to-much
[friendica.git] / src / Module / Notifications / Ping.php
index c2c0c2c67450b0d4f1a4bbd8dd41d11272cc91ef..a0fe8e9aee13a33c210ff8fe96420ef35211fcbe 100644 (file)
@@ -33,6 +33,7 @@ use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Group;
 use Friendica\Model\Post;
+use Friendica\Model\User;
 use Friendica\Model\Verb;
 use Friendica\Module\Register;
 use Friendica\Module\Response;
@@ -41,6 +42,7 @@ use Friendica\Navigation\Notifications\Exception\NoMessageException;
 use Friendica\Navigation\Notifications\Factory;
 use Friendica\Navigation\Notifications\Repository;
 use Friendica\Navigation\Notifications\ValueObject;
+use Friendica\Navigation\SystemMessages;
 use Friendica\Protocol\Activity;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Profiler;
@@ -49,6 +51,8 @@ use Psr\Log\LoggerInterface;
 
 class Ping extends BaseModule
 {
+       /** @var SystemMessages */
+       private $systemMessages;
        /** @var Repository\Notification */
        private $notificationRepo;
        /** @var Introduction */
@@ -56,10 +60,11 @@ class Ping extends BaseModule
        /** @var Factory\FormattedNavNotification */
        private $formattedNavNotification;
 
-       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 = [])
+       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 = [])
        {
                parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
 
+               $this->systemMessages           = $systemMessages;
                $this->notificationRepo         = $notificationRepo;
                $this->introductionRepo         = $introductionRepo;
                $this->formattedNavNotification = $formattedNavNotification;
@@ -85,18 +90,18 @@ class Ping extends BaseModule
                $today_birthday_count = 0;
 
 
-               if (local_user()) {
-                       if (DI::pConfig()->get(local_user(), 'system', 'detailed_notif')) {
-                               $notifications = $this->notificationRepo->selectForUser(local_user(), [], ['limit' => 50]);
+               if (DI::userSession()->getLocalUserId()) {
+                       if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'detailed_notif')) {
+                               $notifications = $this->notificationRepo->selectDetailedForUser(DI::userSession()->getLocalUserId());
                        } else {
-                               $notifications = $this->notificationRepo->selectDigestForUser(local_user());
+                               $notifications = $this->notificationRepo->selectDigestForUser(DI::userSession()->getLocalUserId());
                        }
 
                        $condition = [
                                "`unseen` AND `uid` = ? AND NOT `origin` AND (`vid` != ? OR `vid` IS NULL)",
-                               local_user(), Verb::getID(Activity::FOLLOW)
+                               DI::userSession()->getLocalUserId(), Verb::getID(Activity::FOLLOW)
                        ];
-                       $items = Post::selectForUser(local_user(), ['wall', 'uid', 'uri-id'], $condition, ['limit' => 1000]);
+                       $items = Post::selectForUser(DI::userSession()->getLocalUserId(), ['wall', 'uid', 'uri-id'], $condition, ['limit' => 1000]);
                        if (DBA::isResult($items)) {
                                $items_unseen = Post::toArray($items, false);
                                $arr          = ['items' => $items_unseen];
@@ -112,7 +117,8 @@ class Ping extends BaseModule
                        }
                        DBA::close($items);
 
-                       if ($network_count) {
+                       $compute_group_counts = DI::config()->get('system','compute_group_counts');
+                       if ($network_count && $compute_group_counts) {
                                // Find out how unseen network posts are spread across groups
                                $group_counts = Group::countUnseen();
                                if (DBA::isResult($group_counts)) {
@@ -133,12 +139,12 @@ class Ping extends BaseModule
                                }
                        }
 
-                       $intros = $this->introductionRepo->selectForUser(local_user());
+                       $intros = $this->introductionRepo->selectForUser(DI::userSession()->getLocalUserId());
 
                        $intro_count = $intros->count();
 
                        $myurl      = DI::baseUrl() . '/profile/' . DI::app()->getLoggedInUserNickname();
-                       $mail_count = DBA::count('mail', ["`uid` = ? AND NOT `seen` AND `from-url` != ?", local_user(), $myurl]);
+                       $mail_count = DBA::count('mail', ["`uid` = ? AND NOT `seen` AND `from-url` != ?", DI::userSession()->getLocalUserId(), $myurl]);
 
                        if (intval(DI::config()->get('config', 'register_policy')) === Register::APPROVE && DI::app()->isSiteAdmin()) {
                                $regs = \Friendica\Model\Register::getPending();
@@ -148,15 +154,13 @@ class Ping extends BaseModule
                                }
                        }
 
-                       $cachekey = 'ping:events:' . local_user();
+                       $cachekey = 'ping:events:' . DI::userSession()->getLocalUserId();
                        $ev       = DI::cache()->get($cachekey);
                        if (is_null($ev)) {
                                $ev = DBA::selectToArray('event', ['type', 'start'],
                                        ["`uid` = ? AND `start` < ? AND `finish` > ? AND NOT `ignore`",
-                                               local_user(), DateTimeFormat::utc('now + 7 days'), DateTimeFormat::utcNow()]);
-                               if (DBA::isResult($ev)) {
-                                       DI::cache()->set($cachekey, $ev, Duration::HOUR);
-                               }
+                                               DI::userSession()->getLocalUserId(), DateTimeFormat::utc('now + 7 days'), DateTimeFormat::utcNow()]);
+                               DI::cache()->set($cachekey, $ev, Duration::HOUR);
                        }
 
                        if (DBA::isResult($ev)) {
@@ -183,9 +187,15 @@ class Ping extends BaseModule
                                }
                        }
 
-                       // Temporary workaround for notifications without messages like with the following verb:
-                       // - \Friendica\Protocol\Activity::ANNOUNCE
-                       $navNotifications = array_map(function (Entity\Notification $notification) {
+                       $owner = User::getOwnerDataById(DI::userSession()->getLocalUserId());
+
+                       $navNotifications = array_map(function (Entity\Notification $notification) use ($owner) {
+                               if (!DI::notify()->NotifyOnDesktop($notification)) {
+                                       return null;
+                               }
+                               if (($notification->type == Post\UserNotification::TYPE_NONE) && in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP])) {
+                                       return null;
+                               }
                                try {
                                        return $this->formattedNavNotification->createFromNotification($notification);
                                } catch (NoMessageException $e) {
@@ -204,7 +214,7 @@ class Ping extends BaseModule
                        }
 
                        if (DBA::isResult($regs)) {
-                               if (count($regs) <= 1 || DI::pConfig()->get(local_user(), 'system', 'detailed_notif')) {
+                               if (count($regs) <= 1 || DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'detailed_notif')) {
                                        foreach ($regs as $reg) {
                                                $navNotifications[] = $this->formattedNavNotification->createFromParams(
                                                        [
@@ -248,19 +258,6 @@ class Ping extends BaseModule
                        usort($navNotifications, $sort_function);
                }
 
-               $sysmsgs      = [];
-               $sysmsgs_info = [];
-
-               if (!empty($_SESSION['sysmsg'])) {
-                       $sysmsgs = $_SESSION['sysmsg'];
-                       unset($_SESSION['sysmsg']);
-               }
-
-               if (!empty($_SESSION['sysmsg_info'])) {
-                       $sysmsgs_info = $_SESSION['sysmsg_info'];
-                       unset($_SESSION['sysmsg_info']);
-               }
-
                $notification_count = $sysnotify_count + $intro_count + $register_count;
 
                $data             = [];
@@ -281,15 +278,13 @@ class Ping extends BaseModule
                $data['notifications'] = $navNotifications;
 
                $data['sysmsgs'] = [
-                       'notice' => $sysmsgs,
-                       'info'   => $sysmsgs_info
+                       'notice' => $this->systemMessages->flushNotices(),
+                       'info'   => $this->systemMessages->flushInfos(),
                ];
 
                if (isset($_GET['callback'])) {
                        // JSONP support
-                       header("Content-type: application/javascript");
-                       echo $_GET['callback'] . '(' . json_encode(['result' => $data]) . ')';
-                       exit;
+                       System::httpExit($_GET['callback'] . '(' . json_encode(['result' => $data]) . ')', Response::TYPE_BLANK, 'application/javascript');
                } else {
                        System::jsonExit(['result' => $data]);
                }