Merge pull request #14101 from annando/self-this
[friendica.git/.git] / src / Module / Ping / Network.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Ping;
23
24 use Friendica\App;
25 use Friendica\App\Mode;
26 use Friendica\Content\Conversation;
27 use Friendica\Content\Conversation\Factory\Timeline as TimelineFactory;
28 use Friendica\Content\Conversation\Repository\UserDefinedChannel;
29 use Friendica\Content\Conversation\Factory\Channel as ChannelFactory;
30 use Friendica\Content\Conversation\Factory\UserDefinedChannel as UserDefinedChannelFactory;
31 use Friendica\Content\Conversation\Factory\Community as CommunityFactory;
32 use Friendica\Content\Conversation\Factory\Network as NetworkFactory;
33 use Friendica\Core\Cache\Capability\ICanCache;
34 use Friendica\Core\Config\Capability\IManageConfigValues;
35 use Friendica\Core\L10n;
36 use Friendica\Core\Lock\Capability\ICanLock;
37 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
38 use Friendica\Core\Session\Capability\IHandleUserSessions;
39 use Friendica\Core\System;
40 use Friendica\Database\Database;
41 use Friendica\Module\Conversation\Network as NetworkModule;
42 use Friendica\Module\Response;
43 use Friendica\Navigation\SystemMessages;
44 use Friendica\Util\Profiler;
45 use Psr\Log\LoggerInterface;
46
47 class Network extends NetworkModule
48 {
49         /**
50          * @var ICanLock
51          */
52         private $lock;
53
54         public function __construct(ICanLock $lock, UserDefinedChannelFactory $userDefinedChannel, NetworkFactory $network, CommunityFactory $community, ChannelFactory $channelFactory, UserDefinedChannel $channel, App $app, TimelineFactory $timeline, SystemMessages $systemMessages, Mode $mode, Conversation $conversation, App\Page $page, IHandleUserSessions $session, Database $database, IManagePersonalConfigValues $pConfig, IManageConfigValues $config, ICanCache $cache, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
55         {
56                 parent::__construct($userDefinedChannel, $network, $community, $channelFactory, $channel, $app, $timeline, $systemMessages, $mode, $conversation, $page, $session, $database, $pConfig, $config, $cache, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
57
58                 $this->lock = $lock;
59         }
60
61         protected function rawContent(array $request = [])
62         {
63                 if (!$this->session->getLocalUserId()) {
64                         System::exit();
65                 }
66
67                 if (!empty($request['ping'])) {
68                         $request = $this->getTimelineRequestBySession();
69                 }
70
71                 if (!isset($request['p']) || !isset($request['item'])) {
72                         System::exit();
73                 }
74
75                 $this->parseRequest($request);
76
77                 if ($this->force || !is_null($this->maxId)) {
78                         System::httpExit('');
79                 }
80
81                 $lockkey = 'network-ping-' . $this->session->getLocalUserId();
82                 if (!$this->lock->acquire($lockkey, 0)) {
83                         $this->logger->debug('Ping-1-lock', ['uid' => $this->session->getLocalUserId()]);
84                         System::httpExit('');
85                 }
86
87                 $this->setPing(true);
88                 $this->itemsPerPage = 100;
89
90                 if ($this->channel->isTimeline($this->selectedTab) || $this->userDefinedChannel->isTimeline($this->selectedTab, $this->session->getLocalUserId())) {
91                         $items = $this->getChannelItems($request);
92                 } elseif ($this->community->isTimeline($this->selectedTab)) {
93                         $items = $this->getCommunityItems();
94                 } else {
95                         $items = $this->getItems();
96                 }
97                 $this->lock->release($lockkey);
98                 $count = count($items);
99                 System::httpExit(($count < 100) ? $count : '99+');
100         }
101 }