]> git.mxchange.org Git - friendica.git/blob - src/Content/Conversation/Factory/Timeline.php
Channels can now be created by users
[friendica.git] / src / Content / Conversation / Factory / Timeline.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Content\Conversation\Factory;
23
24 use Friendica\Capabilities\ICanCreateFromTableRow;
25 use Friendica\Content\Conversation\Collection\Timelines;
26 use Friendica\Model\User;
27 use Friendica\Content\Conversation\Entity\Timeline as TimelineEntity;
28 use Friendica\Content\Conversation\Repository\Channel;
29 use Friendica\Core\Config\Capability\IManageConfigValues;
30 use Friendica\Core\L10n;
31 use Friendica\Module\Conversation\Community;
32 use Psr\Log\LoggerInterface;
33
34 final class Timeline extends \Friendica\BaseFactory implements ICanCreateFromTableRow
35 {
36         /** @var L10n */
37         protected $l10n;
38         /** @var IManageConfigValues The config */
39         protected $config;
40         /** @var Channel */
41         protected $channel;
42
43         public function __construct(Channel $channel, L10n $l10n, LoggerInterface $logger, IManageConfigValues $config)
44         {
45                 parent::__construct($logger);
46
47                 $this->channel = $channel;
48                 $this->l10n    = $l10n;
49                 $this->config  = $config;
50         }
51
52         public function createFromTableRow(array $row): TimelineEntity
53         {
54                 return new TimelineEntity(
55                         $row['id'] ?? null,
56                         $row['label'],
57                         $row['description'] ?? null,
58                         $row['access-key'] ?? null,
59                         null,
60                         $row['uid'],
61                         $row['include-tags'] ?? null,
62                         $row['exclude-tags'] ?? null,
63                         $row['full-text-search'] ?? null,
64                         $row['media-type'] ?? null,
65                 );
66         }
67
68         /**
69          * List of available channels
70          *
71          * @param integer $uid
72          * @return Timelines
73          */
74         public function getChannelsForUser(int $uid): Timelines
75         {
76                 $language  = User::getLanguageCode($uid);
77                 $languages = $this->l10n->getAvailableLanguages(true);
78
79                 $tabs = [
80                         new TimelineEntity(TimelineEntity::FORYOU, $this->l10n->t('For you'), $this->l10n->t('Posts from contacts you interact with and who interact with you'), 'y'),
81                         new TimelineEntity(TimelineEntity::WHATSHOT, $this->l10n->t('What\'s Hot'), $this->l10n->t('Posts with a lot of interactions'), 'h'),
82                         new TimelineEntity(TimelineEntity::LANGUAGE, $languages[$language], $this->l10n->t('Posts in %s', $languages[$language]), 'g'),
83                         new TimelineEntity(TimelineEntity::FOLLOWERS, $this->l10n->t('Followers'), $this->l10n->t('Posts from your followers that you don\'t follow'), 'f'),
84                         new TimelineEntity(TimelineEntity::SHARERSOFSHARERS, $this->l10n->t('Sharers of sharers'), $this->l10n->t('Posts from accounts that are followed by accounts that you follow'), 'r'),
85                         new TimelineEntity(TimelineEntity::IMAGE, $this->l10n->t('Images'), $this->l10n->t('Posts with images'), 'i'),
86                         new TimelineEntity(TimelineEntity::AUDIO, $this->l10n->t('Audio'), $this->l10n->t('Posts with audio'), 'd'),
87                         new TimelineEntity(TimelineEntity::VIDEO, $this->l10n->t('Videos'), $this->l10n->t('Posts with videos'), 'v'),
88                 ];
89
90                 foreach ($this->channel->selectByUid($uid) as $channel) {
91                         $tabs[] = $channel;
92                 }
93
94                 return new Timelines($tabs);
95         }
96
97         /**
98          * List of available communities
99          *
100          * @param boolean $authenticated
101          * @return Timelines
102          */
103         public function getCommunities(bool $authenticated): Timelines
104         {
105                 $page_style = $this->config->get('system', 'community_page_style');
106
107                 $tabs = [];
108
109                 if (($authenticated || in_array($page_style, [Community::LOCAL_AND_GLOBAL, Community::LOCAL])) && empty($this->config->get('system', 'singleuser'))) {
110                         $tabs[] = new TimelineEntity(TimelineEntity::LOCAL, $this->l10n->t('Local Community'), $this->l10n->t('Posts from local users on this server'), 'l');
111                 }
112
113                 if ($authenticated || in_array($page_style, [Community::LOCAL_AND_GLOBAL, Community::GLOBAL])) {
114                         $tabs[] = new TimelineEntity(TimelineEntity::GLOBAL, $this->l10n->t('Global Community'), $this->l10n->t('Posts from users of the whole federated network'), 'g');
115                 }
116                 return new Timelines($tabs);
117         }
118
119         /**
120          * List of available network feeds
121          *
122          * @param string $command
123          * @return Timelines
124          */
125         public function getNetworkFeeds(string $command): Timelines
126         {
127                 $tabs = [
128                         new TimelineEntity(TimelineEntity::COMMENTED, $this->l10n->t('Latest Activity'), $this->l10n->t('Sort by latest activity'), 'e', $command . '?' . http_build_query(['order' => 'commented'])),
129                         new TimelineEntity(TimelineEntity::RECEIVED, $this->l10n->t('Latest Posts'), $this->l10n->t('Sort by post received date'), 't', $command . '?' . http_build_query(['order' => 'received'])),
130                         new TimelineEntity(TimelineEntity::CREATED, $this->l10n->t('Latest Creation'), $this->l10n->t('Sort by post creation date'), 'q', $command . '?' . http_build_query(['order' => 'created'])),
131                         new TimelineEntity(TimelineEntity::MENTION, $this->l10n->t('Personal'), $this->l10n->t('Posts that mention or involve you'), 'r', $command . '?' . http_build_query(['mention' => true])),
132                         new TimelineEntity(TimelineEntity::STAR, $this->l10n->t('Starred'), $this->l10n->t('Favourite Posts'), 'm', $command . '?' . http_build_query(['star' => true])),
133                 ];
134                 return new Timelines($tabs);
135         }
136
137         public function isCommunity(string $selectedTab): bool
138         {
139                 return in_array($selectedTab, [TimelineEntity::LOCAL, TimelineEntity::GLOBAL]);
140         }
141
142         public function isChannel(string $selectedTab, int $uid): bool
143         {
144                 if (is_numeric($selectedTab) && $uid && $this->channel->existsById($selectedTab, $uid)) {
145                         return true;
146                 }
147                 return in_array($selectedTab, [TimelineEntity::WHATSHOT, TimelineEntity::FORYOU, TimelineEntity::FOLLOWERS, TimelineEntity::SHARERSOFSHARERS, TimelineEntity::IMAGE, TimelineEntity::VIDEO, TimelineEntity::AUDIO, TimelineEntity::LANGUAGE]);
148         }
149 }