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],
196 $items[count($items) - 1][self::$order],
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', '');
288 if (!empty($get['star'])) {
289 self::$selectedTab = 'star';
292 if (!empty($get['mention'])) {
293 self::$selectedTab = 'mention';
296 if (!empty($get['order'])) {
297 self::$selectedTab = $get['order'];
300 Session::set('network-tab', self::$selectedTab);
302 self::$star = intval($get['star'] ?? 0);
303 self::$mention = intval($get['mention'] ?? 0);
304 self::$order = $get['order'] ?? Session::get('network-order', 'commented');
306 self::$selectedTab = self::$selectedTab ?? self::$order;
308 Session::set('network-tab', self::$selectedTab);
309 Session::set('network-order', self::$order);
311 self::$accountTypeString = $get['accounttype'] ?? $parameters['accounttype'] ?? '';
312 self::$accountType = User::getAccountTypeByString(self::$accountTypeString);
314 self::$network = $get['nets'] ?? '';
316 self::$dateFrom = $parameters['from'] ?? '';
317 self::$dateTo = $parameters['to'] ?? '';
319 if (DI::mode()->isMobile()) {
320 self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
321 DI::config()->get('system', 'itemspage_network_mobile'));
323 self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
324 DI::config()->get('system', 'itemspage_network'));
327 self::$min_id = $get['min_id'] ?? null;
328 self::$max_id = $get['max_id'] ?? null;
330 switch (self::$order) {
332 self::$max_id = $get['last_received'] ?? self::$max_id;
335 self::$max_id = $_GET['last_commented'] ?? self::$max_id;
338 self::$max_id = $_GET['last_created'] ?? self::$max_id;
341 self::$max_id = $_GET['last_uriid'] ?? self::$max_id;
346 protected static function getItems(string $table, array $params, array $conditionFields = [])
348 $conditionFields['uid'] = local_user();
349 $conditionStrings = [];
351 if (!is_null(self::$accountType)) {
352 $conditionFields['contact-type'] = self::$accountType;
356 $conditionFields['starred'] = true;
358 if (self::$mention) {
359 $conditionFields['mention'] = true;
361 if (self::$network) {
362 $conditionFields['network'] = self::$network;
365 if (self::$dateFrom) {
366 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` <= ? ", DateTimeFormat::convert(self::$dateFrom, 'UTC', date_default_timezone_get())]);
369 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` >= ? ", DateTimeFormat::convert(self::$dateTo, 'UTC', date_default_timezone_get())]);
372 if (self::$groupId) {
373 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`contact-id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = ?)", self::$groupId]);
374 } elseif (self::$forumContactId) {
375 $conditionFields['contact-id'] = self::$forumContactId;
378 // Currently only the order modes "received" and "commented" are in use
379 if (isset(self::$max_id)) {
380 switch (self::$order) {
382 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` < ?", self::$max_id]);
385 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`commented` < ?", self::$max_id]);
388 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`created` < ?", self::$max_id]);
391 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`uri-id` < ?", self::$max_id]);
396 if (isset(self::$min_id)) {
397 switch (self::$order) {
399 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` > ?", self::$min_id]);
402 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`commented` > ?", self::$min_id]);
405 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`created` > ?", self::$min_id]);
408 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`uri-id` > ?", self::$min_id]);
413 if (isset(self::$min_id) && !isset(self::$max_id)) {
414 // min_id quirk: querying in reverse order with min_id gets the most recent rows, regardless of how close
415 // they are to min_id. We change the query ordering to get the expected data, and we need to reverse the
416 // order of the results.
417 $params['order'] = [self::$order => false];
419 $params['order'] = [self::$order => true];
422 $items = DBA::selectToArray($table, [], DBA::mergeConditions($conditionFields, $conditionStrings), $params);
424 // min_id quirk, continued
425 if (isset(self::$min_id) && !isset(self::$max_id)) {
426 $items = array_reverse($items);
429 if (DBA::isResult($items)) {
430 $parents = array_column($items, 'parent');
435 // We aren't going to try and figure out at the item, group, and page
436 // level which items you've seen and which you haven't. If you're looking
437 // at the top level network page just mark everything seen.
438 if (!self::$groupId && !self::$forumContactId && !self::$star && !self::$mention) {
439 $condition = ['unseen' => true, 'uid' => local_user()];
440 self::setItemsSeenByCondition($condition);
441 } elseif (!empty($parents)) {
442 $condition = ['unseen' => true, 'uid' => local_user(), 'parent' => $parents];
443 self::setItemsSeenByCondition($condition);