Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
12 KiB

  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2020, Friendica
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. *
  20. * See update_profile.php for documentation
  21. */
  22. namespace Friendica\Module\Conversation;
  23. use Friendica\BaseModule;
  24. use Friendica\Content\BoundariesPager;
  25. use Friendica\Content\Feature;
  26. use Friendica\Content\Nav;
  27. use Friendica\Content\Text\HTML;
  28. use Friendica\Content\Widget;
  29. use Friendica\Content\Widget\TrendingTags;
  30. use Friendica\Core\ACL;
  31. use Friendica\Core\Renderer;
  32. use Friendica\Core\Session;
  33. use Friendica\Database\DBA;
  34. use Friendica\DI;
  35. use Friendica\Model\Item;
  36. use Friendica\Model\Post;
  37. use Friendica\Model\User;
  38. use Friendica\Network\HTTPException;
  39. class Community extends BaseModule
  40. {
  41. protected static $page_style;
  42. protected static $content;
  43. protected static $accountTypeString;
  44. protected static $accountType;
  45. protected static $itemsPerPage;
  46. protected static $min_id;
  47. protected static $max_id;
  48. protected static $item_id;
  49. public static function content(array $parameters = [])
  50. {
  51. self::parseRequest($parameters);
  52. if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
  53. $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
  54. $o = Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
  55. } else {
  56. $o = '';
  57. }
  58. if (empty($_GET['mode']) || ($_GET['mode'] != 'raw')) {
  59. $tabs = [];
  60. if ((Session::isAuthenticated() || in_array(self::$page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(DI::config()->get('system', 'singleuser'))) {
  61. $tabs[] = [
  62. 'label' => DI::l10n()->t('Local Community'),
  63. 'url' => 'community/local',
  64. 'sel' => self::$content == 'local' ? 'active' : '',
  65. 'title' => DI::l10n()->t('Posts from local users on this server'),
  66. 'id' => 'community-local-tab',
  67. 'accesskey' => 'l'
  68. ];
  69. }
  70. if (Session::isAuthenticated() || in_array(self::$page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
  71. $tabs[] = [
  72. 'label' => DI::l10n()->t('Global Community'),
  73. 'url' => 'community/global',
  74. 'sel' => self::$content == 'global' ? 'active' : '',
  75. 'title' => DI::l10n()->t('Posts from users of the whole federated network'),
  76. 'id' => 'community-global-tab',
  77. 'accesskey' => 'g'
  78. ];
  79. }
  80. $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
  81. $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
  82. Nav::setSelected('community');
  83. DI::page()['aside'] .= Widget::accounttypes('community/' . self::$content, self::$accountTypeString);
  84. if (local_user() && DI::config()->get('system', 'community_no_sharer')) {
  85. $path = self::$content;
  86. if (!empty($parameters['accounttype'])) {
  87. $path .= '/' . $parameters['accounttype'];
  88. }
  89. $query_parameters = [];
  90. if (!empty($_GET['min_id'])) {
  91. $query_parameters['min_id'] = $_GET['min_id'];
  92. }
  93. if (!empty($_GET['max_id'])) {
  94. $query_parameters['max_id'] = $_GET['max_id'];
  95. }
  96. if (!empty($_GET['last_commented'])) {
  97. $query_parameters['max_id'] = $_GET['last_commented'];
  98. }
  99. $path_all = $path . (!empty($query_parameters) ? '?' . http_build_query($query_parameters) : '');
  100. $path_no_sharer = $path . '?' . http_build_query(array_merge($query_parameters, ['no_sharer' => true]));
  101. DI::page()['aside'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/community_sharer.tpl'), [
  102. '$title' => DI::l10n()->t('Own Contacts'),
  103. '$path_all' => $path_all,
  104. '$path_no_sharer' => $path_no_sharer,
  105. '$no_sharer' => !empty($_REQUEST['no_sharer']),
  106. '$all' => DI::l10n()->t('Include'),
  107. '$no_sharer_label' => DI::l10n()->t('Hide'),
  108. ]);
  109. }
  110. if (Feature::isEnabled(local_user(), 'trending_tags')) {
  111. DI::page()['aside'] .= TrendingTags::getHTML(self::$content);
  112. }
  113. // We need the editor here to be able to reshare an item.
  114. if (Session::isAuthenticated()) {
  115. $x = [
  116. 'is_owner' => true,
  117. 'allow_location' => DI::app()->user['allow_location'],
  118. 'default_location' => DI::app()->user['default-location'],
  119. 'nickname' => DI::app()->user['nickname'],
  120. 'lockstate' => (is_array(DI::app()->user) && (strlen(DI::app()->user['allow_cid']) || strlen(DI::app()->user['allow_gid']) || strlen(DI::app()->user['deny_cid']) || strlen(DI::app()->user['deny_gid'])) ? 'lock' : 'unlock'),
  121. 'acl' => ACL::getFullSelectorHTML(DI::page(), DI::app()->user, true),
  122. 'bang' => '',
  123. 'visitor' => 'block',
  124. 'profile_uid' => local_user(),
  125. ];
  126. $o .= status_editor(DI::app(), $x, 0, true);
  127. }
  128. }
  129. $items = self::getItems();
  130. if (!DBA::isResult($items)) {
  131. notice(DI::l10n()->t('No results.'));
  132. return $o;
  133. }
  134. $o .= conversation(DI::app(), $items, 'community', false, false, 'commented', local_user());
  135. $pager = new BoundariesPager(
  136. DI::l10n(),
  137. DI::args()->getQueryString(),
  138. $items[0]['commented'],
  139. $items[count($items) - 1]['commented'],
  140. self::$itemsPerPage
  141. );
  142. if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
  143. $o .= HTML::scrollLoader();
  144. } else {
  145. $o .= $pager->renderMinimal(count($items));
  146. }
  147. $t = Renderer::getMarkupTemplate("community.tpl");
  148. return Renderer::replaceMacros($t, [
  149. '$content' => $o,
  150. '$header' => '',
  151. '$show_global_community_hint' => (self::$content == 'global') && DI::config()->get('system', 'show_global_community_hint'),
  152. '$global_community_hint' => DI::l10n()->t("This community stream shows all public posts received by this node. They may not reflect the opinions of this node’s users.")
  153. ]);
  154. }
  155. /**
  156. * Computes module parameters from the request and local configuration
  157. *
  158. * @param array $parameters
  159. * @throws HTTPException\BadRequestException
  160. * @throws HTTPException\ForbiddenException
  161. */
  162. protected static function parseRequest(array $parameters)
  163. {
  164. if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
  165. throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
  166. }
  167. self::$page_style = DI::config()->get('system', 'community_page_style');
  168. if (self::$page_style == CP_NO_INTERNAL_COMMUNITY) {
  169. throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
  170. }
  171. self::$accountTypeString = $_GET['accounttype'] ?? $parameters['accounttype'] ?? '';
  172. self::$accountType = User::getAccountTypeByString(self::$accountTypeString);
  173. self::$content = $parameters['content'] ?? '';
  174. if (!self::$content) {
  175. if (!empty(DI::config()->get('system', 'singleuser'))) {
  176. // On single user systems only the global page does make sense
  177. self::$content = 'global';
  178. } else {
  179. // When only the global community is allowed, we use this as default
  180. self::$content = self::$page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
  181. }
  182. }
  183. if (!in_array(self::$content, ['local', 'global'])) {
  184. throw new HTTPException\BadRequestException(DI::l10n()->t('Community option not available.'));
  185. }
  186. // Check if we are allowed to display the content to visitors
  187. if (!Session::isAuthenticated()) {
  188. $available = self::$page_style == CP_USERS_AND_GLOBAL;
  189. if (!$available) {
  190. $available = (self::$page_style == CP_USERS_ON_SERVER) && (self::$content == 'local');
  191. }
  192. if (!$available) {
  193. $available = (self::$page_style == CP_GLOBAL_COMMUNITY) && (self::$content == 'global');
  194. }
  195. if (!$available) {
  196. throw new HTTPException\ForbiddenException(DI::l10n()->t('Not available.'));
  197. }
  198. }
  199. if (DI::mode()->isMobile()) {
  200. self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
  201. DI::config()->get('system', 'itemspage_network_mobile'));
  202. } else {
  203. self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
  204. DI::config()->get('system', 'itemspage_network'));
  205. }
  206. if (!empty($_GET['item'])) {
  207. $item = Post::selectFirst(['parent'], ['id' => $_GET['item']]);
  208. self::$item_id = $item['parent'] ?? 0;
  209. } else {
  210. self::$item_id = 0;
  211. }
  212. self::$min_id = $_GET['min_id'] ?? null;
  213. self::$max_id = $_GET['max_id'] ?? null;
  214. self::$max_id = $_GET['last_commented'] ?? self::$max_id;
  215. }
  216. /**
  217. * Computes the displayed items.
  218. *
  219. * Community pages have a restriction on how many successive posts by the same author can show on any given page,
  220. * so we may have to retrieve more content beyond the first query
  221. *
  222. * @return array
  223. * @throws \Exception
  224. */
  225. protected static function getItems()
  226. {
  227. $items = self::selectItems(self::$min_id, self::$max_id, self::$item_id, self::$itemsPerPage);
  228. $maxpostperauthor = (int) DI::config()->get('system', 'max_author_posts_community_page');
  229. if ($maxpostperauthor != 0 && self::$content == 'local') {
  230. $count = 1;
  231. $previousauthor = '';
  232. $numposts = 0;
  233. $selected_items = [];
  234. while (count($selected_items) < self::$itemsPerPage && ++$count < 50 && count($items) > 0) {
  235. foreach ($items as $item) {
  236. if ($previousauthor == $item["author-link"]) {
  237. ++$numposts;
  238. } else {
  239. $numposts = 0;
  240. }
  241. $previousauthor = $item["author-link"];
  242. if (($numposts < $maxpostperauthor) && (count($selected_items) < self::$itemsPerPage)) {
  243. $selected_items[] = $item;
  244. }
  245. }
  246. // If we're looking at a "previous page", the lookup continues forward in time because the list is
  247. // sorted in chronologically decreasing order
  248. if (isset(self::$min_id)) {
  249. self::$min_id = $items[0]['commented'];
  250. } else {
  251. // In any other case, the lookup continues backwards in time
  252. self::$max_id = $items[count($items) - 1]['commented'];
  253. }
  254. $items = self::selectItems(self::$min_id, self::$max_id, self::$item_id, self::$itemsPerPage);
  255. }
  256. } else {
  257. $selected_items = $items;
  258. }
  259. return $selected_items;
  260. }
  261. /**
  262. * Database query for the comunity page
  263. *
  264. * @param $min_id
  265. * @param $max_id
  266. * @param $itemspage
  267. * @return array
  268. * @throws \Exception
  269. * @TODO Move to repository/factory
  270. */
  271. private static function selectItems($min_id, $max_id, $item_id, $itemspage)
  272. {
  273. if (self::$content == 'local') {
  274. if (!is_null(self::$accountType)) {
  275. $condition = ["`wall` AND `origin` AND `private` = ? AND `owner-contact-type` = ?", Item::PUBLIC, self::$accountType];
  276. } else {
  277. $condition = ["`wall` AND `origin` AND `private` = ?", Item::PUBLIC];
  278. }
  279. } elseif (self::$content == 'global') {
  280. if (!is_null(self::$accountType)) {
  281. $condition = ["`uid` = ? AND `private` = ? AND `owner-contact-type` = ?", 0, Item::PUBLIC, self::$accountType];
  282. } else {
  283. $condition = ["`uid` = ? AND `private` = ?", 0, Item::PUBLIC];
  284. }
  285. } else {
  286. return [];
  287. }
  288. $params = ['order' => ['commented' => true], 'limit' => $itemspage];
  289. if (!empty($item_id)) {
  290. $condition[0] .= " AND `id` = ?";
  291. $condition[] = $item_id;
  292. } else {
  293. if (local_user() && !empty($_REQUEST['no_sharer'])) {
  294. $condition[0] .= " AND NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-thread-user-view`.`uri-id` AND `post-user`.`uid` = ?)";
  295. $condition[] = local_user();
  296. }
  297. if (isset($max_id)) {
  298. $condition[0] .= " AND `commented` < ?";
  299. $condition[] = $max_id;
  300. }
  301. if (isset($min_id)) {
  302. $condition[0] .= " AND `commented` > ?";
  303. $condition[] = $min_id;
  304. // Previous page case: we want the items closest to min_id but for that we need to reverse the query order
  305. if (!isset($max_id)) {
  306. $params['order']['commented'] = false;
  307. }
  308. }
  309. }
  310. $r = Post::selectThreadForUser(0, ['uri-id', 'commented', 'author-link'], $condition, $params);
  311. $items = Post::toArray($r);
  312. // Previous page case: once we get the relevant items closest to min_id, we need to restore the expected display order
  313. if (empty($item_id) && isset($min_id) && !isset($max_id)) {
  314. $items = array_reverse($items);
  315. }
  316. return $items;
  317. }
  318. }