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\Post;
21 use Friendica\Model\Profile;
22 use Friendica\Model\User;
23 use Friendica\Module\Contact as ModuleContact;
24 use Friendica\Module\Security\Login;
25 use Friendica\Util\DateTimeFormat;
27 class Network extends BaseModule
30 private static $groupId;
32 private static $forumContactId;
34 private static $selectedTab;
36 private static $min_id;
38 private static $max_id;
40 private static $accountTypeString;
42 private static $accountType;
44 private static $network;
46 private static $itemsPerPage;
48 private static $dateFrom;
50 private static $dateTo;
54 private static $mention;
56 protected static $order;
58 public static function content(array $parameters = [])
64 self::parseRequest($parameters, $_GET);
68 DI::page()['aside'] .= Widget::accounttypes($module, self::$accountTypeString);
69 DI::page()['aside'] .= Group::sidebarWidget($module, $module . '/group', 'standard', self::$groupId);
70 DI::page()['aside'] .= ForumManager::widget($module . '/forum', local_user(), self::$forumContactId);
71 DI::page()['aside'] .= Widget::postedByYear($module . '/archive', local_user(), false);
72 DI::page()['aside'] .= Widget::networks($module, !self::$forumContactId ? self::$network : '');
73 DI::page()['aside'] .= Widget\SavedSearches::getHTML(DI::args()->getQueryString());
74 DI::page()['aside'] .= Widget::fileAs('filed', null);
76 $arr = ['query' => DI::args()->getQueryString()];
77 Hook::callAll('network_content_init', $arr);
81 // Fetch a page full of parent items for this page
82 $params = ['limit' => self::$itemsPerPage];
83 $table = 'network-thread-view';
85 $items = self::getItems($table, $params);
87 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll') && ($_GET['mode'] ?? '') != 'minimal') {
88 $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
89 $o .= Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
92 if (!(isset($_GET['mode']) AND ($_GET['mode'] == 'raw'))) {
93 $o .= self::getTabsHTML(self::$selectedTab);
95 Nav::setSelected(DI::args()->get(0));
99 if (self::$forumContactId) {
100 // If self::$forumContactId belongs to a communitity forum or a privat goup,.add a mention to the status editor
101 $condition = ["`id` = ? AND (`forum` OR `prv`)", self::$forumContactId];
102 $contact = DBA::selectFirst('contact', ['addr'], $condition);
103 if (!empty($contact['addr'])) {
104 $content = '!' . $contact['addr'];
110 $default_permissions = [];
111 if (self::$groupId) {
112 $default_permissions['allow_gid'] = [self::$groupId];
116 if (self::$forumContactId) {
117 $allowedCids[] = (int) self::$forumContactId;
118 } elseif (self::$network) {
120 'uid' => local_user(),
121 'network' => self::$network,
126 'rel' => [Contact::SHARING, Contact::FRIEND],
128 $contactStmt = DBA::select('contact', ['id'], $condition);
129 while ($contact = DBA::fetch($contactStmt)) {
130 $allowedCids[] = (int) $contact['id'];
132 DBA::close($contactStmt);
135 if (count($allowedCids)) {
136 $default_permissions['allow_cid'] = $allowedCids;
141 'allow_location' => $a->user['allow_location'],
142 'default_location' => $a->user['default-location'],
143 'nickname' => $a->user['nickname'],
144 'lockstate' => (self::$groupId || self::$forumContactId || self::$network || (is_array($a->user) &&
145 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
146 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid']))) ? 'lock' : 'unlock'),
147 'default_perms' => ACL::getDefaultUserPermissions($a->user),
148 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true, $default_permissions),
149 'bang' => ((self::$groupId || self::$forumContactId || self::$network) ? '!' : ''),
150 'visitor' => 'block',
151 'profile_uid' => local_user(),
152 'content' => $content,
155 $o .= status_editor($a, $x);
158 if (self::$groupId) {
159 $group = DBA::selectFirst('group', ['name'], ['id' => self::$groupId, 'uid' => local_user()]);
160 if (!DBA::isResult($group)) {
161 notice(DI::l10n()->t('No such group'));
164 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
165 '$title' => DI::l10n()->t('Group: %s', $group['name'])
167 } elseif (self::$forumContactId) {
168 $contact = Contact::getById(self::$forumContactId);
169 if (DBA::isResult($contact)) {
170 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('viewcontact_template.tpl'), [
171 'contacts' => [ModuleContact::getContactTemplateVars($contact)],
172 'id' => DI::args()->get(0),
175 notice(DI::l10n()->t('Invalid contact.'));
177 } elseif (!DI::config()->get('theme', 'hide_eventlist')) {
178 $o .= Profile::getBirthdays();
179 $o .= Profile::getEventsReminderHTML();
182 if (self::$order === 'received') {
183 $ordering = '`received`';
185 $ordering = '`commented`';
188 $o .= conversation(DI::app(), $items, 'network', false, false, $ordering, local_user());
190 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
191 $o .= HTML::scrollLoader();
193 $pager = new BoundariesPager(
195 DI::args()->getQueryString(),
196 $items[0][self::$order] ?? null,
197 $items[count($items) - 1][self::$order] ?? null,
201 $o .= $pager->renderMinimal(count($items));
210 * @param array $condition The array with the SQL condition
211 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
213 private static function setItemsSeenByCondition(array $condition)
215 if (empty($condition)) {
219 $unseen = Post::exists($condition);
222 /// @todo handle huge "unseen" updates in the background to avoid timeout errors
223 Item::update(['unseen' => false], $condition);
228 * Get the network tabs menu
230 * @param string $selectedTab
231 * @return string Html of the network tabs
232 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
234 private static function getTabsHTML(string $selectedTab)
236 $cmd = DI::args()->getCommand();
241 'label' => DI::l10n()->t('Latest Activity'),
242 'url' => $cmd . '?' . http_build_query(['order' => 'commented']),
243 'sel' => !$selectedTab || $selectedTab == 'commented' ? 'active' : '',
244 'title' => DI::l10n()->t('Sort by latest activity'),
245 'id' => 'activity-order-tab',
249 'label' => DI::l10n()->t('Latest Posts'),
250 'url' => $cmd . '?' . http_build_query(['order' => 'received']),
251 'sel' => $selectedTab == 'received' ? 'active' : '',
252 'title' => DI::l10n()->t('Sort by post received date'),
253 'id' => 'post-order-tab',
257 'label' => DI::l10n()->t('Personal'),
258 'url' => $cmd . '?' . http_build_query(['mention' => true]),
259 'sel' => $selectedTab == 'mention' ? 'active' : '',
260 'title' => DI::l10n()->t('Posts that mention or involve you'),
261 'id' => 'personal-tab',
265 'label' => DI::l10n()->t('Starred'),
266 'url' => $cmd . '?' . http_build_query(['star' => true]),
267 'sel' => $selectedTab == 'star' ? 'active' : '',
268 'title' => DI::l10n()->t('Favourite Posts'),
269 'id' => 'starred-posts-tab',
274 $arr = ['tabs' => $tabs];
275 Hook::callAll('network_tabs', $arr);
277 $tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
279 return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs']]);
282 protected static function parseRequest(array $parameters, array $get)
284 self::$groupId = $parameters['group_id'] ?? 0;
286 self::$forumContactId = $parameters['contact_id'] ?? 0;
288 self::$selectedTab = Session::get('network-tab', DI::pConfig()->get(local_user(), 'network.view', 'selected_tab', ''));
290 self::$order = 'commented';
292 if (!empty($get['star'])) {
293 self::$selectedTab = 'star';
296 self::$star = self::$selectedTab == 'star';
299 if (!empty($get['mention'])) {
300 self::$selectedTab = 'mention';
301 self::$mention = true;
303 self::$mention = self::$selectedTab == 'mention';
306 if (!empty($get['order'])) {
307 self::$selectedTab = $get['order'];
308 self::$order = $get['order'];
309 } elseif (in_array(self::$selectedTab, ['received', 'star', 'mention'])) {
310 self::$order = 'received';
313 self::$selectedTab = self::$selectedTab ?? self::$order;
315 Session::set('network-tab', self::$selectedTab);
316 DI::pConfig()->set(local_user(), 'network.view', 'selected_tab', self::$selectedTab);
318 self::$accountTypeString = $get['accounttype'] ?? $parameters['accounttype'] ?? '';
319 self::$accountType = User::getAccountTypeByString(self::$accountTypeString);
321 self::$network = $get['nets'] ?? '';
323 self::$dateFrom = $parameters['from'] ?? '';
324 self::$dateTo = $parameters['to'] ?? '';
326 if (DI::mode()->isMobile()) {
327 self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
328 DI::config()->get('system', 'itemspage_network_mobile'));
330 self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
331 DI::config()->get('system', 'itemspage_network'));
334 self::$min_id = $get['min_id'] ?? null;
335 self::$max_id = $get['max_id'] ?? null;
337 switch (self::$order) {
339 self::$max_id = $get['last_received'] ?? self::$max_id;
342 self::$max_id = $get['last_created'] ?? self::$max_id;
345 self::$max_id = $get['last_uriid'] ?? self::$max_id;
348 self::$order = 'commented';
349 self::$max_id = $get['last_commented'] ?? self::$max_id;
353 protected static function getItems(string $table, array $params, array $conditionFields = [])
355 $conditionFields['uid'] = local_user();
356 $conditionStrings = [];
358 if (!is_null(self::$accountType)) {
359 $conditionFields['contact-type'] = self::$accountType;
363 $conditionFields['starred'] = true;
365 if (self::$mention) {
366 $conditionFields['mention'] = true;
368 if (self::$network) {
369 $conditionFields['network'] = self::$network;
372 if (self::$dateFrom) {
373 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` <= ? ", DateTimeFormat::convert(self::$dateFrom, 'UTC', date_default_timezone_get())]);
376 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` >= ? ", DateTimeFormat::convert(self::$dateTo, 'UTC', date_default_timezone_get())]);
379 if (self::$groupId) {
380 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`contact-id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = ?)", self::$groupId]);
381 } elseif (self::$forumContactId) {
382 $conditionFields['contact-id'] = self::$forumContactId;
385 // Currently only the order modes "received" and "commented" are in use
386 if (isset(self::$max_id)) {
387 switch (self::$order) {
389 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` < ?", self::$max_id]);
392 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`commented` < ?", self::$max_id]);
395 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`created` < ?", self::$max_id]);
398 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`uri-id` < ?", self::$max_id]);
403 if (isset(self::$min_id)) {
404 switch (self::$order) {
406 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` > ?", self::$min_id]);
409 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`commented` > ?", self::$min_id]);
412 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`created` > ?", self::$min_id]);
415 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`uri-id` > ?", self::$min_id]);
420 if (isset(self::$min_id) && !isset(self::$max_id)) {
421 // min_id quirk: querying in reverse order with min_id gets the most recent rows, regardless of how close
422 // they are to min_id. We change the query ordering to get the expected data, and we need to reverse the
423 // order of the results.
424 $params['order'] = [self::$order => false];
426 $params['order'] = [self::$order => true];
429 $items = DBA::selectToArray($table, [], DBA::mergeConditions($conditionFields, $conditionStrings), $params);
431 // min_id quirk, continued
432 if (isset(self::$min_id) && !isset(self::$max_id)) {
433 $items = array_reverse($items);
436 if (DBA::isResult($items)) {
437 $parents = array_column($items, 'parent');
442 // We aren't going to try and figure out at the item, group, and page
443 // level which items you've seen and which you haven't. If you're looking
444 // at the top level network page just mark everything seen.
445 if (!self::$groupId && !self::$forumContactId && !self::$star && !self::$mention) {
446 $condition = ['unseen' => true, 'uid' => local_user()];
447 self::setItemsSeenByCondition($condition);
448 } elseif (!empty($parents)) {
449 $condition = ['unseen' => true, 'uid' => local_user(), 'parent' => $parents];
450 self::setItemsSeenByCondition($condition);