From d04b68a3ce40ea5ecf709ca9d02599a43321549e Mon Sep 17 00:00:00 2001 From: Phablulo Date: Tue, 21 Dec 2021 14:30:44 -0300 Subject: [PATCH] [PLUGIN][MediaFeed] Add Media plugin which filters a feed by notes containing media --- plugins/MediaFeed/MediaFeed.php | 86 +++++++++++++++++++ .../templates/mediaFeeed/tabs.html.twig | 5 ++ .../MediaFeed/assets/css/mediaFeed.css | 16 ++++ src/Core/Controller/FeedController.php | 7 +- 4 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 plugins/MediaFeed/MediaFeed.php create mode 100644 plugins/MediaFeed/templates/mediaFeeed/tabs.html.twig create mode 100644 public/plugins/MediaFeed/assets/css/mediaFeed.css diff --git a/plugins/MediaFeed/MediaFeed.php b/plugins/MediaFeed/MediaFeed.php new file mode 100644 index 0000000000..34a00f8bf5 --- /dev/null +++ b/plugins/MediaFeed/MediaFeed.php @@ -0,0 +1,86 @@ +. +// }}} + +namespace Plugin\MediaFeed; + +use App\Core\Event; +use App\Entity\Note; +use Functional as F; +use App\Entity\Actor; +use App\Util\Formatting; +use App\Core\Modules\Plugin; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Config\Definition\Exception\Exception; + +class MediaFeed extends Plugin +{ + public function onFilterNoteList(?Actor $actor, array &$notes, Request $request): bool + { + if ($request->get('filter-type') === 'media') { + $notes = F\select($notes, fn (Note $n) => \count($n->getAttachments()) > 0); + } + return Event::next; + } + /** + * Draw the media feed navigation. + * @return bool hook value; true means continue processing, false means stop. + */ + public function onBeforeFeed(Request $request, &$res): bool + { + $isMediaActive = $request->get('filter-type') === 'media'; + // we need two urls: one with filter-type=media and without it. + $query = strpos($request->getRequestUri(), '?'); + $mediaURL = $request->getRequestUri() . ($query !== false ? '&' : '?') . 'filter-type=media'; + $allURL = $request->getPathInfo(); + if ($query !== false) { + $params = explode('&', substr($request->getRequestUri(), $query + 1)); + $params = array_filter($params, fn ($s) => $s !== 'filter-type=media'); + $params = implode('&', $params); + if ($params) { + $allURL .= '?' . $params; + } + } + + $res[] = Formatting::twigRenderFile('mediaFeeed/tabs.html.twig', [ + 'main' => [ + 'active' => !$isMediaActive, + 'url' => $isMediaActive ? $allURL : '', + ], + 'media' => [ + 'active' => $isMediaActive, + 'url' => $isMediaActive ? '' : $mediaURL, + ] + ]); + return Event::next; + } + /** + * Output our dedicated stylesheet + * + * @param array $styles stylesheets path + * + * @return bool hook value; true means continue processing, false means stop + */ + public function onEndShowStyles(array &$styles, string $route): bool + { + $styles[] = 'plugins/MediaFeed/assets/css/mediaFeed.css'; + return Event::next; + } +} diff --git a/plugins/MediaFeed/templates/mediaFeeed/tabs.html.twig b/plugins/MediaFeed/templates/mediaFeeed/tabs.html.twig new file mode 100644 index 0000000000..f3ed230dce --- /dev/null +++ b/plugins/MediaFeed/templates/mediaFeeed/tabs.html.twig @@ -0,0 +1,5 @@ + diff --git a/public/plugins/MediaFeed/assets/css/mediaFeed.css b/public/plugins/MediaFeed/assets/css/mediaFeed.css new file mode 100644 index 0000000000..d8df165dfe --- /dev/null +++ b/public/plugins/MediaFeed/assets/css/mediaFeed.css @@ -0,0 +1,16 @@ +.media-feed-tabs { + border: 2px solid var(--border); + border-radius: var(--smaller) var(--smaller) 0 0; + border-bottom: none; + display: flex; +} +.media-feed-tabs > a { + flex: 0% 1 1; + padding: 10px 12px; + text-align: center; +} +.media-feed-tabs > .separator { + width: 2px; + flex: 0% 0 0; + background-color: var(--border); +} diff --git a/src/Core/Controller/FeedController.php b/src/Core/Controller/FeedController.php index 89d57a021d..b5b5ddd9a3 100644 --- a/src/Core/Controller/FeedController.php +++ b/src/Core/Controller/FeedController.php @@ -50,12 +50,7 @@ abstract class FeedController extends Controller if (\array_key_exists('notes', $result)) { $notes = $result['notes']; Event::handle('FilterNoteList', [$actor, &$notes, $result['request']]); - - if ($result['should_format'] ?? true) { - $notes_out = null; - Event::handle('FormatNoteList', [$notes, &$notes_out]); - $result['notes'] = $notes_out; - } + Event::handle('FormatNoteList', [$notes, &$result['notes']]); } return $result;