NoteTypeFeedFilter.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. /**
  20. * Media Feed Plugin for GNU social
  21. *
  22. * @package GNUsocial
  23. * @category Plugin
  24. *
  25. * @author Phablulo <phablulo@gmail.com>
  26. * @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
  27. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  28. */
  29. namespace Plugin\NoteTypeFeedFilter;
  30. use App\Core\Event;
  31. use function App\Core\I18n\_m;
  32. use App\Core\Modules\Plugin;
  33. use App\Entity\Actor;
  34. use App\Entity\Note;
  35. use App\Util\Exception\BugFoundException;
  36. use App\Util\Exception\ClientException;
  37. use App\Util\Formatting;
  38. use App\Util\Functional as GSF;
  39. use Functional as F;
  40. use Symfony\Component\HttpFoundation\Request;
  41. // TODO: Migrate this to query filters
  42. class NoteTypeFeedFilter extends Plugin
  43. {
  44. public const ALLOWED_TYPES = ['media', 'link', 'text', 'tag'];
  45. private function unknownType(string $type): ClientException
  46. {
  47. return new ClientException(_m('Unknown note type requested ({type})', ['{type}' => $type]));
  48. }
  49. /**
  50. * Normalize the given $types so only those in self::ALLOWED_TYPES
  51. * are present, filling in the missing ones with the negated
  52. * version
  53. */
  54. private function normalizeTypesList(array $types, bool $add_missing = true): array
  55. {
  56. if (empty($types)) {
  57. return self::ALLOWED_TYPES;
  58. } else {
  59. $result = [];
  60. foreach (self::ALLOWED_TYPES as $allowed_type) {
  61. foreach ($types as $type) {
  62. if ($type === 'all') {
  63. return self::ALLOWED_TYPES;
  64. } elseif (mb_detect_encoding($type, 'ASCII', strict: true) === false || empty($type)) {
  65. throw $this->unknownType($type);
  66. } elseif (\in_array(
  67. $allowed_type,
  68. GSF::cartesianProduct([
  69. ['', '!'],
  70. [$type, mb_substr($type, 1), mb_substr($type, 0, -1)], // The original, without the first or without the last character
  71. ]),
  72. )) {
  73. $result[] = ($type[0] === '!' ? '!' : '') . $allowed_type;
  74. continue 2;
  75. }
  76. } // else
  77. if ($add_missing) {
  78. $result[] = '!' . $allowed_type;
  79. }
  80. }
  81. return $result;
  82. }
  83. }
  84. /**
  85. * Remove Notes from $notes if the GET parameter note-types requests they shoud
  86. *
  87. * Includes if any positive type matches, but removes if any negated matches
  88. */
  89. public function onFilterNoteList(?Actor $actor, array &$notes, Request $request): bool
  90. {
  91. $types = $this->normalizeTypesList(\is_null($request->get('note-types')) ? [] : explode(',', $request->get('note-types')));
  92. $notes = F\select(
  93. $notes,
  94. /**
  95. * Filter each note based on the requested $types
  96. *
  97. * @TODO Would like to express this as a reduce of some sort
  98. */
  99. function (Note $note) use ($types) {
  100. $include = false;
  101. foreach ($types as $type) {
  102. $is_negate = $type[0] === '!';
  103. $type = Formatting::removePrefix($type, '!');
  104. switch ($type) {
  105. case 'text':
  106. $ret = !\is_null($note->getContent());
  107. break;
  108. case 'media':
  109. $ret = !empty($note->getAttachments());
  110. break;
  111. case 'link':
  112. $ret = !empty($note->getLinks());
  113. break;
  114. case 'tag':
  115. $ret = !empty($note->getTags());
  116. break;
  117. default:
  118. throw new BugFoundException("Unkown note type requested {$type}", previous: $this->unknownType($type));
  119. }
  120. if ($is_negate && $ret) {
  121. return false;
  122. }
  123. $include = $include || $ret;
  124. }
  125. return $include;
  126. },
  127. );
  128. return Event::next;
  129. }
  130. /**
  131. * Draw the media feed navigation.
  132. */
  133. public function onAddFeedActions(Request $request, bool $is_not_empty, &$res): bool
  134. {
  135. $qs = [];
  136. parse_str($request->getQueryString(), $qs);
  137. if (\array_key_exists('p', $qs) && \is_string($qs['p'])) {
  138. unset($qs['p']);
  139. }
  140. $types = $this->normalizeTypesList(\is_null($request->get('note-types')) ? [] : explode(',', $request->get('note-types')), add_missing: false);
  141. $ftypes = array_flip($types);
  142. $tabs = [
  143. 'all' => [
  144. 'active' => empty($types) || $types === self::ALLOWED_TYPES,
  145. 'url' => '?' . http_build_query(['note-types' => implode(',', self::ALLOWED_TYPES)], '', '&', \PHP_QUERY_RFC3986),
  146. 'icon' => 'All',
  147. ],
  148. ];
  149. foreach (self::ALLOWED_TYPES as $allowed_type) {
  150. $active = \array_key_exists($allowed_type, $ftypes);
  151. $new_types = $this->normalizeTypesList([($active ? '!' : '') . $allowed_type, ...$types], add_missing: false);
  152. $new_qs = $qs;
  153. $new_qs['note-types'] = implode(',', $new_types);
  154. $tabs[$allowed_type] = [
  155. 'active' => $active,
  156. 'url' => '?' . http_build_query($new_qs, '', '&', \PHP_QUERY_RFC3986),
  157. 'icon' => $allowed_type,
  158. ];
  159. }
  160. $res[] = Formatting::twigRenderFile('NoteTypeFeedFilter/tabs.html.twig', ['tabs' => $tabs]);
  161. return Event::next;
  162. }
  163. /**
  164. * Output our dedicated stylesheet
  165. *
  166. * @param array $styles stylesheets path
  167. *
  168. * @return bool hook value; true means continue processing, false means stop
  169. */
  170. public function onEndShowStyles(array &$styles, string $route): bool
  171. {
  172. $styles[] = 'plugins/NoteTypeFeedFilter/assets/css/noteTypeFeedFilter.css';
  173. return Event::next;
  174. }
  175. }