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 $accountType;
40 private static $network;
42 private static $itemsPerPage;
44 private static $dateFrom;
46 private static $dateTo;
50 private static $mention;
52 protected static $order;
54 public static function content(array $parameters = [])
60 self::parseRequest($parameters, $_GET);
64 DI::page()['aside'] .= Widget::accounts($module . '/accounttype', self::$accountType);
65 DI::page()['aside'] .= Group::sidebarWidget($module, $module . '/group', 'standard', self::$groupId);
66 DI::page()['aside'] .= ForumManager::widget($module . '/forum', local_user(), self::$forumContactId);
67 DI::page()['aside'] .= Widget::postedByYear($module . '/archive', local_user(), false);
68 DI::page()['aside'] .= Widget::networks($module, !self::$forumContactId ? self::$network : '');
69 DI::page()['aside'] .= Widget\SavedSearches::getHTML(DI::args()->getQueryString());
70 DI::page()['aside'] .= Widget::fileAs('filed', null);
72 $arr = ['query' => DI::args()->getQueryString()];
73 Hook::callAll('network_content_init', $arr);
77 // Fetch a page full of parent items for this page
78 $params = ['limit' => self::$itemsPerPage];
79 $table = 'network-thread-view';
81 $items = self::getItems($table, $params);
83 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll') && ($_GET['mode'] ?? '') != 'minimal') {
84 $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
85 $o .= Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
88 if (!(isset($_GET['mode']) AND ($_GET['mode'] == 'raw'))) {
89 $o .= self::getTabsHTML(self::$selectedTab);
91 Nav::setSelected(DI::args()->get(0));
95 if (self::$forumContactId) {
96 // If self::$forumContactId belongs to a communitity forum or a privat goup,.add a mention to the status editor
97 $condition = ["`id` = ? AND (`forum` OR `prv`)", self::$forumContactId];
98 $contact = DBA::selectFirst('contact', ['addr'], $condition);
99 if (!empty($contact['addr'])) {
100 $content = '!' . $contact['addr'];
106 $default_permissions = [];
107 if (self::$groupId) {
108 $default_permissions['allow_gid'] = [self::$groupId];
112 if (self::$forumContactId) {
113 $allowedCids[] = (int) self::$forumContactId;
114 } elseif (self::$network) {
116 'uid' => local_user(),
117 'network' => self::$network,
122 'rel' => [Contact::SHARING, Contact::FRIEND],
124 $contactStmt = DBA::select('contact', ['id'], $condition);
125 while ($contact = DBA::fetch($contactStmt)) {
126 $allowedCids[] = (int) $contact['id'];
128 DBA::close($contactStmt);
131 if (count($allowedCids)) {
132 $default_permissions['allow_cid'] = $allowedCids;
137 'allow_location' => $a->user['allow_location'],
138 'default_location' => $a->user['default-location'],
139 'nickname' => $a->user['nickname'],
140 'lockstate' => (self::$groupId || self::$forumContactId || self::$network || (is_array($a->user) &&
141 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
142 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid']))) ? 'lock' : 'unlock'),
143 'default_perms' => ACL::getDefaultUserPermissions($a->user),
144 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true, $default_permissions),
145 'bang' => ((self::$groupId || self::$forumContactId || self::$network) ? '!' : ''),
146 'visitor' => 'block',
147 'profile_uid' => local_user(),
148 'content' => $content,
151 $o .= status_editor($a, $x);
154 if (self::$groupId) {
155 $group = DBA::selectFirst('group', ['name'], ['id' => self::$groupId, 'uid' => local_user()]);
156 if (!DBA::isResult($group)) {
157 notice(DI::l10n()->t('No such group'));
160 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
161 '$title' => DI::l10n()->t('Group: %s', $group['name'])
163 } elseif (self::$forumContactId) {
164 $contact = Contact::getById(self::$forumContactId);
165 if (DBA::isResult($contact)) {
166 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('viewcontact_template.tpl'), [
167 'contacts' => [ModuleContact::getContactTemplateVars($contact)],
168 'id' => DI::args()->get(0),
171 notice(DI::l10n()->t('Invalid contact.'));
173 } elseif (!DI::config()->get('theme', 'hide_eventlist')) {
174 $o .= Profile::getBirthdays();
175 $o .= Profile::getEventsReminderHTML();
178 if (self::$order === 'received') {
179 $ordering = '`received`';
181 $ordering = '`commented`';
184 $o .= conversation(DI::app(), $items, 'network', false, false, $ordering, local_user());
186 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
187 $o .= HTML::scrollLoader();
189 $pager = new BoundariesPager(
191 DI::args()->getQueryString(),
192 $items[0][self::$order],
193 $items[count($items) - 1][self::$order],
197 $o .= $pager->renderMinimal(count($items));
206 * @param array $condition The array with the SQL condition
207 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
209 private static function setItemsSeenByCondition(array $condition)
211 if (empty($condition)) {
215 $unseen = Item::exists($condition);
218 Item::update(['unseen' => false], $condition);
223 * Get the network tabs menu
225 * @param string $selectedTab
226 * @return string Html of the network tabs
227 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
229 private static function getTabsHTML(string $selectedTab)
231 $cmd = DI::args()->getCommand();
236 'label' => DI::l10n()->t('Latest Activity'),
237 'url' => $cmd . '?' . http_build_query(['order' => 'commented']),
238 'sel' => !$selectedTab || $selectedTab == 'commented' ? 'active' : '',
239 'title' => DI::l10n()->t('Sort by latest activity'),
240 'id' => 'activity-order-tab',
244 'label' => DI::l10n()->t('Latest Posts'),
245 'url' => $cmd . '?' . http_build_query(['order' => 'received']),
246 'sel' => $selectedTab == 'received' ? 'active' : '',
247 'title' => DI::l10n()->t('Sort by post received date'),
248 'id' => 'post-order-tab',
252 'label' => DI::l10n()->t('Personal'),
253 'url' => $cmd . '?' . http_build_query(['mention' => true]),
254 'sel' => $selectedTab == 'mention' ? 'active' : '',
255 'title' => DI::l10n()->t('Posts that mention or involve you'),
256 'id' => 'personal-tab',
260 'label' => DI::l10n()->t('Starred'),
261 'url' => $cmd . '?' . http_build_query(['star' => true]),
262 'sel' => $selectedTab == 'star' ? 'active' : '',
263 'title' => DI::l10n()->t('Favourite Posts'),
264 'id' => 'starred-posts-tab',
269 $arr = ['tabs' => $tabs];
270 Hook::callAll('network_tabs', $arr);
272 $tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
274 return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs']]);
277 protected static function parseRequest(array $parameters, array $get)
279 self::$groupId = $parameters['group_id'] ?? 0;
281 self::$forumContactId = $parameters['contact_id'] ?? 0;
283 self::$selectedTab = DI::pConfig()->get(local_user(), 'network.view', 'selected_tab', '');
285 if (!empty($get['star'])) {
286 self::$selectedTab = 'star';
289 if (!empty($get['mention'])) {
290 self::$selectedTab = 'mention';
293 if (!empty($get['order'])) {
294 self::$selectedTab = $get['order'];
297 DI::pConfig()->set(local_user(), 'network.view', 'selected_tab', self::$selectedTab);
299 self::$star = intval($get['star'] ?? 0);
300 self::$mention = intval($_GET['mention'] ?? 0);
301 self::$order = in_array(self::$selectedTab, ['received', 'commented', 'created', 'uriid']) ? self::$selectedTab : 'commented';
303 self::$accountType = User::getAccountTypeByString($parameters['accounttype'] ?? '') ?? '';
305 self::$network = $get['nets'] ?? '';
307 self::$dateFrom = $parameters['from'] ?? '';
308 self::$dateTo = $parameters['to'] ?? '';
310 if (DI::mode()->isMobile()) {
311 self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
312 DI::config()->get('system', 'itemspage_network_mobile'));
314 self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
315 DI::config()->get('system', 'itemspage_network'));
318 self::$min_id = $_GET['min_id'] ?? null;
319 self::$max_id = $_GET['max_id'] ?? null;
321 switch (self::$selectedTab) {
323 self::$max_id = $_GET['last_received'] ?? self::$max_id;
326 self::$max_id = $_GET['last_commented'] ?? self::$max_id;
329 self::$max_id = $_GET['last_created'] ?? self::$max_id;
332 self::$max_id = $_GET['last_uriid'] ?? self::$max_id;
337 protected static function getItems(string $table, array $params, array $conditionFields = [])
339 $conditionFields['uid'] = local_user();
340 $conditionStrings = [];
342 if (self::$accountType) {
343 $conditionFields['contact-type'] = self::$accountType;
347 $conditionFields['starred'] = true;
349 if (self::$mention) {
350 $conditionFields['mention'] = true;
352 if (self::$network) {
353 $conditionFields['network'] = self::$network;
356 if (self::$dateFrom) {
357 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` <= ? ", DateTimeFormat::convert(self::$dateFrom, 'UTC', date_default_timezone_get())]);
360 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` >= ? ", DateTimeFormat::convert(self::$dateTo, 'UTC', date_default_timezone_get())]);
363 if (self::$groupId) {
364 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`contact-id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = ?)", self::$groupId]);
365 } elseif (self::$forumContactId) {
366 $conditionFields['contact-id'] = self::$forumContactId;
369 // Currently only the order modes "received" and "commented" are in use
370 if (isset(self::$max_id)) {
371 switch (self::$order) {
373 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` < ?", self::$max_id]);
376 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`commented` < ?", self::$max_id]);
379 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`created` < ?", self::$max_id]);
382 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`uri-id` < ?", self::$max_id]);
387 if (isset(self::$min_id)) {
388 switch (self::$order) {
390 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` > ?", self::$min_id]);
393 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`commented` > ?", self::$min_id]);
396 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`created` > ?", self::$min_id]);
399 $conditionStrings = DBA::mergeConditions($conditionStrings, ["`uri-id` > ?", self::$min_id]);
404 if (isset(self::$min_id) && !isset(self::$max_id)) {
405 // min_id quirk: querying in reverse order with min_id gets the most recent rows, regardless of how close
406 // they are to min_id. We change the query ordering to get the expected data, and we need to reverse the
407 // order of the results.
408 $params['order'] = [self::$order => false];
410 $params['order'] = [self::$order => true];
413 $items = DBA::selectToArray($table, [], DBA::mergeConditions($conditionFields, $conditionStrings), $params);
415 // min_id quirk, continued
416 if (isset(self::$min_id) && !isset(self::$max_id)) {
417 $items = array_reverse($items);
421 if (DBA::isResult($items)) {
424 foreach ($items as $item) {
425 if (!in_array($item['parent'], $parents_arr) && ($item['parent'] > 0)) {
426 $parents_arr[] = $item['parent'];
429 $parents_str = implode(', ', $parents_arr);
432 // We aren't going to try and figure out at the item, group, and page
433 // level which items you've seen and which you haven't. If you're looking
434 // at the top level network page just mark everything seen.
435 if (!self::$groupId && !self::$forumContactId && self::$selectedTab != 'star') {
436 $condition = ['unseen' => true, 'uid' => local_user()];
437 self::setItemsSeenByCondition($condition);
438 } elseif ($parents_str) {
439 $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . DBA::escape($parents_str) . ")", local_user()];
440 self::setItemsSeenByCondition($condition);