]> git.mxchange.org Git - friendica.git/blob - src/Module/Conversation/Channel.php
3bb8b6039738e92554a8973f618e2bb2bc9fd76b
[friendica.git] / src / Module / Conversation / Channel.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  * See update_profile.php for documentation
21  */
22
23 namespace Friendica\Module\Conversation;
24
25 use Friendica\BaseModule;
26 use Friendica\Content\BoundariesPager;
27 use Friendica\Content\Conversation;
28 use Friendica\Content\Feature;
29 use Friendica\Content\Nav;
30 use Friendica\Content\Text\HTML;
31 use Friendica\Content\Widget;
32 use Friendica\Content\Widget\TrendingTags;
33 use Friendica\Core\Renderer;
34 use Friendica\Database\DBA;
35 use Friendica\DI;
36 use Friendica\Model\Contact;
37 use Friendica\Model\Post;
38 use Friendica\Model\User;
39 use Friendica\Module\Security\Login;
40 use Friendica\Network\HTTPException;
41
42 class Channel extends BaseModule
43 {
44         const WHATSHOT  = 'whatshot';
45         const FORYOU    = 'foryou';
46         const FOLLOWERS = 'followers';
47         /**
48          * @}
49          */
50
51         protected static $content;
52         protected static $accountTypeString;
53         protected static $accountType;
54         protected static $itemsPerPage;
55         protected static $min_id;
56         protected static $max_id;
57         protected static $item_id;
58
59         protected function content(array $request = []): string
60         {
61                 if (!DI::userSession()->getLocalUserId()) {
62                         return Login::form();
63                 }
64
65                 $this->parseRequest();
66
67                 $t = Renderer::getMarkupTemplate("community.tpl");
68                 $o = Renderer::replaceMacros($t, [
69                         '$content' => '',
70                         '$header'  => '',
71                 ]);
72
73                 if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) {
74                         $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
75                         $o .= Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
76                 }
77
78                 if (empty($_GET['mode']) || ($_GET['mode'] != 'raw')) {
79                         $tabs = [];
80
81                         $tabs[] = [
82                                 'label'     => DI::l10n()->t('For you'),
83                                 'url'       => 'channel/' . self::FORYOU,
84                                 'sel'       => self::$content == self::FORYOU ? 'active' : '',
85                                 'title'     => DI::l10n()->t('Posts from contacts you interact with and who interact with you'),
86                                 'id'        => 'channel-foryou-tab',
87                                 'accesskey' => 'y'
88                         ];
89
90                         $tabs[] = [
91                                 'label'     => DI::l10n()->t('Followers'),
92                                 'url'       => 'channel/' . self::FOLLOWERS,
93                                 'sel'       => self::$content == self::FOLLOWERS ? 'active' : '',
94                                 'title'     => DI::l10n()->t('Posts from your followers that you don\'t follow'),
95                                 'id'        => 'channel-followers-tab',
96                                 'accesskey' => 'f'
97                         ];
98
99                         $tabs[] = [
100                                 'label'     => DI::l10n()->t('Whats Hot'),
101                                 'url'       => 'channel/' . self::WHATSHOT,
102                                 'sel'       => self::$content == self::WHATSHOT ? 'active' : '',
103                                 'title'     => DI::l10n()->t('Posts with a lot of interactions'),
104                                 'id'        => 'channel-whatshot-tab',
105                                 'accesskey' => 'h'
106                         ];
107
108                         $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
109                         $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
110
111                         Nav::setSelected('channel');
112
113                         DI::page()['aside'] .= Widget::accountTypes('channel/' . self::$content, self::$accountTypeString);
114
115                         if ((self::$content != self::FOLLOWERS) && DI::config()->get('system', 'community_no_sharer')) {
116                                 $path = self::$content;
117                                 if (!empty($this->parameters['accounttype'])) {
118                                         $path .= '/' . $this->parameters['accounttype'];
119                                 }
120                                 $query_parameters = [];
121
122                                 if (!empty($_GET['min_id'])) {
123                                         $query_parameters['min_id'] = $_GET['min_id'];
124                                 }
125                                 if (!empty($_GET['max_id'])) {
126                                         $query_parameters['max_id'] = $_GET['max_id'];
127                                 }
128                                 if (!empty($_GET['last_created'])) {
129                                         $query_parameters['max_id'] = $_GET['last_created'];
130                                 }
131
132                                 $path_all       = $path . (!empty($query_parameters) ? '?' . http_build_query($query_parameters) : '');
133                                 $path_no_sharer = $path . '?' . http_build_query(array_merge($query_parameters, ['no_sharer' => true]));
134                                 DI::page()['aside'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/community_sharer.tpl'), [
135                                         '$title'           => DI::l10n()->t('Own Contacts'),
136                                         '$path_all'        => $path_all,
137                                         '$path_no_sharer'  => $path_no_sharer,
138                                         '$no_sharer'       => !empty($_REQUEST['no_sharer']),
139                                         '$all'             => DI::l10n()->t('Include'),
140                                         '$no_sharer_label' => DI::l10n()->t('Hide'),
141                                         '$base'            => 'channel',
142                                 ]);
143                         }
144
145                         if (Feature::isEnabled(DI::userSession()->getLocalUserId(), 'trending_tags')) {
146                                 DI::page()['aside'] .= TrendingTags::getHTML(self::$content);
147                         }
148
149                         // We need the editor here to be able to reshare an item.
150                         $o .= DI::conversation()->statusEditor([], 0, true);
151                 }
152
153                 $items = self::getItems();
154
155                 if (!DBA::isResult($items)) {
156                         DI::sysmsg()->addNotice(DI::l10n()->t('No results.'));
157                         return $o;
158                 }
159
160                 $o .= DI::conversation()->render($items, Conversation::MODE_CHANNEL, false, false, 'created', DI::userSession()->getLocalUserId());
161
162                 $pager = new BoundariesPager(
163                         DI::l10n(),
164                         DI::args()->getQueryString(),
165                         $items[0]['created'],
166                         $items[count($items) - 1]['created'],
167                         self::$itemsPerPage
168                 );
169
170                 if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) {
171                         $o .= HTML::scrollLoader();
172                 } else {
173                         $o .= $pager->renderMinimal(count($items));
174                 }
175
176                 return $o;
177         }
178
179         /**
180          * Computes module parameters from the request and local configuration
181          *
182          * @throws HTTPException\BadRequestException
183          * @throws HTTPException\ForbiddenException
184          */
185         protected function parseRequest()
186         {
187                 self::$accountTypeString = $_GET['accounttype'] ?? $this->parameters['accounttype'] ?? '';
188                 self::$accountType       = User::getAccountTypeByString(self::$accountTypeString);
189
190                 self::$content = $this->parameters['content'] ?? '';
191                 if (!self::$content) {
192                         self::$content = self::FORYOU;
193                 }
194
195                 if (!in_array(self::$content, [self::WHATSHOT, self::FORYOU, self::FOLLOWERS])) {
196                         throw new HTTPException\BadRequestException(DI::l10n()->t('Channel not available.'));
197                 }
198
199                 if (DI::mode()->isMobile()) {
200                         self::$itemsPerPage = DI::pConfig()->get(
201                                 DI::userSession()->getLocalUserId(),
202                                 'system',
203                                 'itemspage_mobile_network',
204                                 DI::config()->get('system', 'itemspage_network_mobile')
205                         );
206                 } else {
207                         self::$itemsPerPage = DI::pConfig()->get(
208                                 DI::userSession()->getLocalUserId(),
209                                 'system',
210                                 'itemspage_network',
211                                 DI::config()->get('system', 'itemspage_network')
212                         );
213                 }
214
215                 if (!empty($_GET['item'])) {
216                         $item          = Post::selectFirst(['parent-uri-id'], ['id' => $_GET['item']]);
217                         self::$item_id = $item['parent-uri-id'] ?? 0;
218                 } else {
219                         self::$item_id = 0;
220                 }
221
222                 self::$min_id = $_GET['min_id']       ?? null;
223                 self::$max_id = $_GET['max_id']       ?? null;
224                 self::$max_id = $_GET['last_created'] ?? self::$max_id;
225         }
226
227         /**
228          * Computes the displayed items.
229          *
230          * Community pages have a restriction on how many successive posts by the same author can show on any given page,
231          * so we may have to retrieve more content beyond the first query
232          *
233          * @return array
234          * @throws \Exception
235          */
236         protected static function getItems()
237         {
238                 if (self::$content == self::WHATSHOT) {
239                         $post     = DBA::selectToArray('post-engagement', ['comments'], [], ['order' => ['comments' => true], 'limit' => [DI::config()->get('channel', 'hot_posts_item_limit'), 1]]);
240                         $comments = $post[0]['comments'] ?? 0;
241                         if (!is_null(self::$accountType)) {
242                                 $condition = ["`comments` >= ? AND `contact-type` = ?", $comments, self::$accountType];
243                         } else {
244                                 $condition = ["`comments` >= ?", $comments];
245                         }
246                 } elseif (self::$content == self::FORYOU) {
247                         $cid = Contact::getPublicIdByUserId(DI::userSession()->getLocalUserId());
248                         if (!is_null(self::$accountType)) {
249                                 $condition = ["`author-id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `thread-score` > ? AND `relation-thread-score` > ?) AND `contact-type` = ?", $cid, 0, 0, self::$accountType];
250                         } else {
251                                 $condition = ["`author-id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `thread-score` > ? AND `relation-thread-score` > ?)", $cid, 0, 0];
252                         }
253                 } elseif (self::$content == self::FOLLOWERS) {
254                         if (!is_null(self::$accountType)) {
255                                 $condition = ["`author-id` IN (SELECT `pid` FROM `account-user-view` WHERE uid` = ? AND `rel` = ?) AND `contact-type` = ?", DI::userSession()->getLocalUserId(), Contact::FOLLOWER, self::$accountType];
256                         } else {
257                                 $condition = ["`author-id` IN (SELECT `pid` FROM `account-user-view` WHERE `uid` = ? AND `rel` = ?)", DI::userSession()->getLocalUserId(), Contact::FOLLOWER];
258                         }
259                 }
260
261                 $params = ['order' => ['created' => true], 'limit' => self::$itemsPerPage];
262
263                 if (!empty(self::$item_id)) {
264                         // @todo
265                         $condition[0] .= " AND `uri-id` = ?";
266                         $condition[] = self::$item_id;
267                 } else {
268                         if (!empty($_REQUEST['no_sharer'])) {
269                                 $condition[0] .= " AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uid` = ? AND `post-user`.`uri-id` = `post-engagement`.`uri-id`)";
270                                 $condition[] = DI::userSession()->getLocalUserId();
271                         }
272
273                         if (isset(self::$max_id)) {
274                                 $condition[0] .= " AND `created` < ?";
275                                 $condition[] = self::$max_id;
276                         }
277
278                         if (isset(self::$min_id)) {
279                                 $condition[0] .= " AND `created` > ?";
280                                 $condition[] = self::$min_id;
281
282                                 // Previous page case: we want the items closest to min_id but for that we need to reverse the query order
283                                 if (!isset(self::$max_id)) {
284                                         $params['order']['created'] = false;
285                                 }
286                         }
287                 }
288
289                 $items = DBA::selectToArray('post-engagement', ['uri-id', 'created'], $condition, $params);
290
291                 if (empty($items)) {
292                         return [];
293                 }
294
295                 // Previous page case: once we get the relevant items closest to min_id, we need to restore the expected display order
296                 if (empty(self::$item_id) && isset(self::$min_id) && !isset(self::$max_id)) {
297                         $items = array_reverse($items);
298                 }
299
300                 return $items;
301         }
302 }