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\Database\DBA;
16 use Friendica\Model\Contact;
17 use Friendica\Model\Group;
18 use Friendica\Model\Item;
19 use Friendica\Model\Profile;
20 use Friendica\Model\User;
21 use Friendica\Module\Contact as ModuleContact;
22 use Friendica\Module\Security\Login;
23 use Friendica\Util\DateTimeFormat;
25 class Network extends BaseModule
28 private static $groupId;
30 private static $forumContactId;
32 private static $selectedTab;
34 private static $min_id;
36 private static $max_id;
38 private static $accountTypeString;
40 private static $accountType;
42 private static $network;
44 private static $itemsPerPage;
46 private static $dateFrom;
48 private static $dateTo;
52 private static $mention;
54 protected static $order;
56 public static function content(array $parameters = [])
62 self::parseRequest($parameters, $_GET);
66 DI::page()['aside'] .= Widget::accounttypes($module, self::$accountTypeString);
67 DI::page()['aside'] .= Group::sidebarWidget($module, $module . '/group', 'standard', self::$groupId);
68 DI::page()['aside'] .= ForumManager::widget($module . '/forum', local_user(), self::$forumContactId);
69 DI::page()['aside'] .= Widget::postedByYear($module . '/archive', local_user(), false);
70 DI::page()['aside'] .= Widget::networks($module, !self::$forumContactId ? self::$network : '');
71 DI::page()['aside'] .= Widget\SavedSearches::getHTML(DI::args()->getQueryString());
72 DI::page()['aside'] .= Widget::fileAs('filed', null);
74 $arr = ['query' => DI::args()->getQueryString()];
75 Hook::callAll('network_content_init', $arr);
79 // Fetch a page full of parent items for this page
80 $params = ['limit' => self::$itemsPerPage];
81 $table = 'network-thread-view';
83 $items = self::getItems($table, $params);
85 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll') && ($_GET['mode'] ?? '') != 'minimal') {
86 $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
87 $o .= Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
90 if (!(isset($_GET['mode']) AND ($_GET['mode'] == 'raw'))) {
91 $o .= self::getTabsHTML(self::$selectedTab);
93 Nav::setSelected(DI::args()->get(0));
97 if (self::$forumContactId) {
98 // If self::$forumContactId belongs to a communitity forum or a privat goup,.add a mention to the status editor
99 $condition = ["`id` = ? AND (`forum` OR `prv`)", self::$forumContactId];
100 $contact = DBA::selectFirst('contact', ['addr'], $condition);
101 if (!empty($contact['addr'])) {
102 $content = '!' . $contact['addr'];
108 $default_permissions = [];
109 if (self::$groupId) {
110 $default_permissions['allow_gid'] = [self::$groupId];
114 if (self::$forumContactId) {
115 $allowedCids[] = (int) self::$forumContactId;
116 } elseif (self::$network) {
118 'uid' => local_user(),
119 'network' => self::$network,
124 'rel' => [Contact::SHARING, Contact::FRIEND],
126 $contactStmt = DBA::select('contact', ['id'], $condition);
127 while ($contact = DBA::fetch($contactStmt)) {
128 $allowedCids[] = (int) $contact['id'];
130 DBA::close($contactStmt);
133 if (count($allowedCids)) {
134 $default_permissions['allow_cid'] = $allowedCids;
139 'allow_location' => $a->user['allow_location'],
140 'default_location' => $a->user['default-location'],
141 'nickname' => $a->user['nickname'],
142 'lockstate' => (self::$groupId || self::$forumContactId || self::$network || (is_array($a->user) &&
143 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
144 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid']))) ? 'lock' : 'unlock'),
145 'default_perms' => ACL::getDefaultUserPermissions($a->user),
146 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true, $default_permissions),
147 'bang' => ((self::$groupId || self::$forumContactId || self::$network) ? '!' : ''),
148 'visitor' => 'block',
149 'profile_uid' => local_user(),
150 'content' => $content,
153 $o .= status_editor($a, $x);
156 if (self::$groupId) {
157 $group = DBA::selectFirst('group', ['name'], ['id' => self::$groupId, 'uid' => local_user()]);
158 if (!DBA::isResult($group)) {
159 notice(DI::l10n()->t('No such group'));
162 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
163 '$title' => DI::l10n()->t('Group: %s', $group['name'])
165 } elseif (self::$forumContactId) {
166 $contact = Contact::getById(self::$forumContactId);
167 if (DBA::isResult($contact)) {
168 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('viewcontact_template.tpl'), [
169 'contacts' => [ModuleContact::getContactTemplateVars($contact)],
170 'id' => DI::args()->get(0),
173 notice(DI::l10n()->t('Invalid contact.'));
175 } elseif (!DI::config()->get('theme', 'hide_eventlist')) {
176 $o .= Profile::getBirthdays();
177 $o .= Profile::getEventsReminderHTML();
180 if (self::$order === 'received') {
181 $ordering = '`received`';
183 $ordering = '`commented`';
186 $o .= conversation(DI::app(), $items, 'network', false, false, $ordering, local_user());
188 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
189 $o .= HTML::scrollLoader();
191 $pager = new BoundariesPager(
193 DI::args()->getQueryString(),
194 $items[0][self::$order],
195 $items[count($items) - 1][self::$order],
199 $o .= $pager->renderMinimal(count($items));
208 * @param array $condition The array with the SQL condition
209 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
211 private static function setItemsSeenByCondition(array $condition)
213 if (empty($condition)) {
217 $unseen = Item::exists($condition);
220 Item::update(['unseen' => false], $condition);
225 * Get the network tabs menu
227 * @param string $selectedTab
228 * @return string Html of the network tabs
229 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
231 private static function getTabsHTML(string $selectedTab)
233 $cmd = DI::args()->getCommand();
238 'label' => DI::l10n()->t('Latest Activity'),
239 'url' => $cmd . '?' . http_build_query(['order' => 'commented']),
240 'sel' => !$selectedTab || $selectedTab == 'commented' ? 'active' : '',
241 'title' => DI::l10n()->t('Sort by latest activity'),
242 'id' => 'activity-order-tab',
246 'label' => DI::l10n()->t('Latest Posts'),
247 'url' => $cmd . '?' . http_build_query(['order' => 'received']),
248 'sel' => $selectedTab == 'received' ? 'active' : '',
249 'title' => DI::l10n()->t('Sort by post received date'),
250 'id' => 'post-order-tab',
254 'label' => DI::l10n()->t('Personal'),
255 'url' => $cmd . '?' . http_build_query(['mention' => true]),
256 'sel' => $selectedTab == 'mention' ? 'active' : '',
257 'title' => DI::l10n()->t('Posts that mention or involve you'),
258 'id' => 'personal-tab',
262 'label' => DI::l10n()->t('Starred'),
263 'url' => $cmd . '?' . http_build_query(['star' => true]),
264 'sel' => $selectedTab == 'star' ? 'active' : '',
265 'title' => DI::l10n()->t('Favourite Posts'),
266 'id' => 'starred-posts-tab',
271 $arr = ['tabs' => $tabs];
272 Hook::callAll('network_tabs', $arr);
274 $tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
276 return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs']]);
279 protected static function parseRequest(array $parameters, array $get)
281 self::$groupId = $parameters['group_id'] ?? 0;
283 self::$forumContactId = $parameters['contact_id'] ?? 0;
285 self::$selectedTab = '';
287 if (!empty($get['star'])) {
288 self::$selectedTab = 'star';
291 if (!empty($get['mention'])) {
292 self::$selectedTab = 'mention';
295 if (!empty($get['order'])) {
296 self::$selectedTab = $get['order'];
299 self::$star = intval($get['star'] ?? 0);
300 self::$mention = intval($_GET['mention'] ?? 0);
301 self::$order = $get['order'] ?? 'commented';
303 self::$accountTypeString = $_GET['accounttype'] ?? $parameters['accounttype'] ?? '';
304 self::$accountType = User::getAccountTypeByString(self::$accountTypeString);
306 self::$network = $get['nets'] ?? '';
308 self::$dateFrom = $parameters['from'] ?? '';
309 self::$dateTo = $parameters['to'] ?? '';
311 if (DI::mode()->isMobile()) {
312 self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
313 DI::config()->get('system', 'itemspage_network_mobile'));
315 self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
316 DI::config()->get('system', 'itemspage_network'));
319 self::$min_id = $_GET['min_id'] ?? null;
320 self::$max_id = $_GET['max_id'] ?? null;
322 switch (self::$order) {
324 self::$max_id = $_GET['last_received'] ?? self::$max_id;
327 self::$max_id = $_GET['last_commented'] ?? self::$max_id;
330 self::$max_id = $_GET['last_created'] ?? self::$max_id;
333 self::$max_id = $_GET['last_uriid'] ?? self::$max_id;
338 protected static function getItems(string $table, array $params, array $conditionFields = [])
340 $conditionFields['uid'] = local_user();
341 $conditionStrings = [];
343 if (!is_null(self::$accountType)) {
344 $conditionFields['contact-type'] = self::$accountType;
348 $conditionFields['starred'] = true;
350 if (self::$mention) {
351 $conditionFields['mention'] = true;
353 if (self::$network) {
354 $conditionFields['network'] = self::$network;
357 if (self::$dateFrom) {
358 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` <= ? ", DateTimeFormat::convert(self::$dateFrom, 'UTC', date_default_timezone_get())]);
361 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` >= ? ", DateTimeFormat::convert(self::$dateTo, 'UTC', date_default_timezone_get())]);
364 if (self::$groupId) {
365 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`contact-id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = ?)", self::$groupId]);
366 } elseif (self::$forumContactId) {
367 $conditionFields['contact-id'] = self::$forumContactId;
370 // Currently only the order modes "received" and "commented" are in use
371 if (isset(self::$max_id)) {
372 switch (self::$order) {
374 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` < ?", self::$max_id]);
377 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`commented` < ?", self::$max_id]);
380 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`created` < ?", self::$max_id]);
383 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`uri-id` < ?", self::$max_id]);
388 if (isset(self::$min_id)) {
389 switch (self::$order) {
391 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` > ?", self::$min_id]);
394 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`commented` > ?", self::$min_id]);
397 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`created` > ?", self::$min_id]);
400 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`uri-id` > ?", self::$min_id]);
405 if (isset(self::$min_id) && !isset(self::$max_id)) {
406 // min_id quirk: querying in reverse order with min_id gets the most recent rows, regardless of how close
407 // they are to min_id. We change the query ordering to get the expected data, and we need to reverse the
408 // order of the results.
409 $params['order'] = [self::$order => false];
411 $params['order'] = [self::$order => true];
414 $items = DBA::selectToArray($table, [], DBA::mergeConditions($conditionFields, $conditionStrings), $params);
416 // min_id quirk, continued
417 if (isset(self::$min_id) && !isset(self::$max_id)) {
418 $items = array_reverse($items);
421 if (DBA::isResult($items)) {
422 $parents = array_column($items, 'parent');
427 // We aren't going to try and figure out at the item, group, and page
428 // level which items you've seen and which you haven't. If you're looking
429 // at the top level network page just mark everything seen.
430 if (!self::$groupId && !self::$forumContactId && !self::$star && !self::$mention) {
431 $condition = ['unseen' => true, 'uid' => local_user()];
432 self::setItemsSeenByCondition($condition);
433 } elseif (!empty($parents)) {
434 $condition = ['unseen' => true, 'uid' => local_user(), 'parent' => $parents];
435 self::setItemsSeenByCondition($condition);