3 namespace Friendica\Module\Conversation;
5 use Friendica\BaseModule;
6 use Friendica\Content\BoundariesPager;
7 use Friendica\Content\ForumManager;
8 use Friendica\Content\Nav;
9 use Friendica\Content\Widget;
10 use Friendica\Content\Text\HTML;
11 use Friendica\Core\ACL;
12 use Friendica\Core\Hook;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\Session;
15 use Friendica\Database\DBA;
17 use Friendica\Model\Contact;
18 use Friendica\Model\Group;
19 use Friendica\Model\Item;
20 use Friendica\Model\Profile;
21 use Friendica\Model\User;
22 use Friendica\Module\Contact as ModuleContact;
23 use Friendica\Module\Security\Login;
24 use Friendica\Util\DateTimeFormat;
26 class Network extends BaseModule
29 private static $groupId;
31 private static $forumContactId;
33 private static $selectedTab;
35 private static $min_id;
37 private static $max_id;
39 private static $accountTypeString;
41 private static $accountType;
43 private static $network;
45 private static $itemsPerPage;
47 private static $dateFrom;
49 private static $dateTo;
53 private static $mention;
55 protected static $order;
57 public static function content(array $parameters = [])
63 self::parseRequest($parameters, $_GET);
67 DI::page()['aside'] .= Widget::accounttypes($module, self::$accountTypeString);
68 DI::page()['aside'] .= Group::sidebarWidget($module, $module . '/group', 'standard', self::$groupId);
69 DI::page()['aside'] .= ForumManager::widget($module . '/forum', local_user(), self::$forumContactId);
70 DI::page()['aside'] .= Widget::postedByYear($module . '/archive', local_user(), false);
71 DI::page()['aside'] .= Widget::networks($module, !self::$forumContactId ? self::$network : '');
72 DI::page()['aside'] .= Widget\SavedSearches::getHTML(DI::args()->getQueryString());
73 DI::page()['aside'] .= Widget::fileAs('filed', null);
75 $arr = ['query' => DI::args()->getQueryString()];
76 Hook::callAll('network_content_init', $arr);
80 // Fetch a page full of parent items for this page
81 $params = ['limit' => self::$itemsPerPage];
82 $table = 'network-thread-view';
84 $items = self::getItems($table, $params);
86 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll') && ($_GET['mode'] ?? '') != 'minimal') {
87 $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
88 $o .= Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
91 if (!(isset($_GET['mode']) AND ($_GET['mode'] == 'raw'))) {
92 $o .= self::getTabsHTML(self::$selectedTab);
94 Nav::setSelected(DI::args()->get(0));
98 if (self::$forumContactId) {
99 // If self::$forumContactId belongs to a communitity forum or a privat goup,.add a mention to the status editor
100 $condition = ["`id` = ? AND (`forum` OR `prv`)", self::$forumContactId];
101 $contact = DBA::selectFirst('contact', ['addr'], $condition);
102 if (!empty($contact['addr'])) {
103 $content = '!' . $contact['addr'];
109 $default_permissions = [];
110 if (self::$groupId) {
111 $default_permissions['allow_gid'] = [self::$groupId];
115 if (self::$forumContactId) {
116 $allowedCids[] = (int) self::$forumContactId;
117 } elseif (self::$network) {
119 'uid' => local_user(),
120 'network' => self::$network,
125 'rel' => [Contact::SHARING, Contact::FRIEND],
127 $contactStmt = DBA::select('contact', ['id'], $condition);
128 while ($contact = DBA::fetch($contactStmt)) {
129 $allowedCids[] = (int) $contact['id'];
131 DBA::close($contactStmt);
134 if (count($allowedCids)) {
135 $default_permissions['allow_cid'] = $allowedCids;
140 'allow_location' => $a->user['allow_location'],
141 'default_location' => $a->user['default-location'],
142 'nickname' => $a->user['nickname'],
143 'lockstate' => (self::$groupId || self::$forumContactId || self::$network || (is_array($a->user) &&
144 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
145 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid']))) ? 'lock' : 'unlock'),
146 'default_perms' => ACL::getDefaultUserPermissions($a->user),
147 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true, $default_permissions),
148 'bang' => ((self::$groupId || self::$forumContactId || self::$network) ? '!' : ''),
149 'visitor' => 'block',
150 'profile_uid' => local_user(),
151 'content' => $content,
154 $o .= status_editor($a, $x);
157 if (self::$groupId) {
158 $group = DBA::selectFirst('group', ['name'], ['id' => self::$groupId, 'uid' => local_user()]);
159 if (!DBA::isResult($group)) {
160 notice(DI::l10n()->t('No such group'));
163 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
164 '$title' => DI::l10n()->t('Group: %s', $group['name'])
166 } elseif (self::$forumContactId) {
167 $contact = Contact::getById(self::$forumContactId);
168 if (DBA::isResult($contact)) {
169 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('viewcontact_template.tpl'), [
170 'contacts' => [ModuleContact::getContactTemplateVars($contact)],
171 'id' => DI::args()->get(0),
174 notice(DI::l10n()->t('Invalid contact.'));
176 } elseif (!DI::config()->get('theme', 'hide_eventlist')) {
177 $o .= Profile::getBirthdays();
178 $o .= Profile::getEventsReminderHTML();
181 if (self::$order === 'received') {
182 $ordering = '`received`';
184 $ordering = '`commented`';
187 $o .= conversation(DI::app(), $items, 'network', false, false, $ordering, local_user());
189 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
190 $o .= HTML::scrollLoader();
192 $pager = new BoundariesPager(
194 DI::args()->getQueryString(),
195 $items[0][self::$order] ?? null,
196 $items[count($items) - 1][self::$order] ?? null,
200 $o .= $pager->renderMinimal(count($items));
209 * @param array $condition The array with the SQL condition
210 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
212 private static function setItemsSeenByCondition(array $condition)
214 if (empty($condition)) {
218 $unseen = Item::exists($condition);
221 Item::update(['unseen' => false], $condition);
226 * Get the network tabs menu
228 * @param string $selectedTab
229 * @return string Html of the network tabs
230 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
232 private static function getTabsHTML(string $selectedTab)
234 $cmd = DI::args()->getCommand();
239 'label' => DI::l10n()->t('Latest Activity'),
240 'url' => $cmd . '?' . http_build_query(['order' => 'commented']),
241 'sel' => !$selectedTab || $selectedTab == 'commented' ? 'active' : '',
242 'title' => DI::l10n()->t('Sort by latest activity'),
243 'id' => 'activity-order-tab',
247 'label' => DI::l10n()->t('Latest Posts'),
248 'url' => $cmd . '?' . http_build_query(['order' => 'received']),
249 'sel' => $selectedTab == 'received' ? 'active' : '',
250 'title' => DI::l10n()->t('Sort by post received date'),
251 'id' => 'post-order-tab',
255 'label' => DI::l10n()->t('Personal'),
256 'url' => $cmd . '?' . http_build_query(['mention' => true]),
257 'sel' => $selectedTab == 'mention' ? 'active' : '',
258 'title' => DI::l10n()->t('Posts that mention or involve you'),
259 'id' => 'personal-tab',
263 'label' => DI::l10n()->t('Starred'),
264 'url' => $cmd . '?' . http_build_query(['star' => true]),
265 'sel' => $selectedTab == 'star' ? 'active' : '',
266 'title' => DI::l10n()->t('Favourite Posts'),
267 'id' => 'starred-posts-tab',
272 $arr = ['tabs' => $tabs];
273 Hook::callAll('network_tabs', $arr);
275 $tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
277 return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs']]);
280 protected static function parseRequest(array $parameters, array $get)
282 self::$groupId = $parameters['group_id'] ?? 0;
284 self::$forumContactId = $parameters['contact_id'] ?? 0;
286 self::$selectedTab = Session::get('network-tab', DI::pConfig()->get(local_user(), 'network.view', 'selected_tab', ''));
288 self::$order = 'commented';
290 if (!empty($get['star'])) {
291 self::$selectedTab = 'star';
294 self::$star = self::$selectedTab == 'star';
297 if (!empty($get['mention'])) {
298 self::$selectedTab = 'mention';
299 self::$mention = true;
301 self::$mention = self::$selectedTab == 'mention';
304 if (!empty($get['order'])) {
305 self::$selectedTab = $get['order'];
306 self::$order = $get['order'];
307 } elseif (in_array(self::$selectedTab, ['received', 'star', 'mention'])) {
308 self::$order = 'received';
311 self::$selectedTab = self::$selectedTab ?? self::$order;
313 Session::set('network-tab', self::$selectedTab);
314 DI::pConfig()->set(local_user(), 'network.view', 'selected_tab', self::$selectedTab);
316 self::$accountTypeString = $get['accounttype'] ?? $parameters['accounttype'] ?? '';
317 self::$accountType = User::getAccountTypeByString(self::$accountTypeString);
319 self::$network = $get['nets'] ?? '';
321 self::$dateFrom = $parameters['from'] ?? '';
322 self::$dateTo = $parameters['to'] ?? '';
324 if (DI::mode()->isMobile()) {
325 self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
326 DI::config()->get('system', 'itemspage_network_mobile'));
328 self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
329 DI::config()->get('system', 'itemspage_network'));
332 self::$min_id = $get['min_id'] ?? null;
333 self::$max_id = $get['max_id'] ?? null;
335 switch (self::$order) {
337 self::$max_id = $get['last_received'] ?? self::$max_id;
340 self::$max_id = $get['last_created'] ?? self::$max_id;
343 self::$max_id = $get['last_uriid'] ?? self::$max_id;
346 self::$order = 'commented';
347 self::$max_id = $get['last_commented'] ?? self::$max_id;
351 protected static function getItems(string $table, array $params, array $conditionFields = [])
353 $conditionFields['uid'] = local_user();
354 $conditionStrings = [];
356 if (!is_null(self::$accountType)) {
357 $conditionFields['contact-type'] = self::$accountType;
361 $conditionFields['starred'] = true;
363 if (self::$mention) {
364 $conditionFields['mention'] = true;
366 if (self::$network) {
367 $conditionFields['network'] = self::$network;
370 if (self::$dateFrom) {
371 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` <= ? ", DateTimeFormat::convert(self::$dateFrom, 'UTC', date_default_timezone_get())]);
374 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` >= ? ", DateTimeFormat::convert(self::$dateTo, 'UTC', date_default_timezone_get())]);
377 if (self::$groupId) {
378 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`contact-id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = ?)", self::$groupId]);
379 } elseif (self::$forumContactId) {
380 $conditionFields['contact-id'] = self::$forumContactId;
383 // Currently only the order modes "received" and "commented" are in use
384 if (isset(self::$max_id)) {
385 switch (self::$order) {
387 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` < ?", self::$max_id]);
390 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`commented` < ?", self::$max_id]);
393 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`created` < ?", self::$max_id]);
396 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`uri-id` < ?", self::$max_id]);
401 if (isset(self::$min_id)) {
402 switch (self::$order) {
404 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` > ?", self::$min_id]);
407 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`commented` > ?", self::$min_id]);
410 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`created` > ?", self::$min_id]);
413 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`uri-id` > ?", self::$min_id]);
418 if (isset(self::$min_id) && !isset(self::$max_id)) {
419 // min_id quirk: querying in reverse order with min_id gets the most recent rows, regardless of how close
420 // they are to min_id. We change the query ordering to get the expected data, and we need to reverse the
421 // order of the results.
422 $params['order'] = [self::$order => false];
424 $params['order'] = [self::$order => true];
427 $items = DBA::selectToArray($table, [], DBA::mergeConditions($conditionFields, $conditionStrings), $params);
429 // min_id quirk, continued
430 if (isset(self::$min_id) && !isset(self::$max_id)) {
431 $items = array_reverse($items);
434 if (DBA::isResult($items)) {
435 $parents = array_column($items, 'parent');
440 // We aren't going to try and figure out at the item, group, and page
441 // level which items you've seen and which you haven't. If you're looking
442 // at the top level network page just mark everything seen.
443 if (!self::$groupId && !self::$forumContactId && !self::$star && !self::$mention) {
444 $condition = ['unseen' => true, 'uid' => local_user()];
445 self::setItemsSeenByCondition($condition);
446 } elseif (!empty($parents)) {
447 $condition = ['unseen' => true, 'uid' => local_user(), 'parent' => $parents];
448 self::setItemsSeenByCondition($condition);