[PLUGIN][MediaFeed] Add Media plugin which filters a feed by notes containing media

This commit is contained in:
Phablulo Joel 2021-12-21 14:30:44 -03:00 committed by Diogo Peralta Cordeiro
parent fb64444325
commit d04b68a3ce
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
4 changed files with 108 additions and 6 deletions

View File

@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
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;
}
}

View File

@ -0,0 +1,5 @@
<nav class=media-feed-tabs role=navigation>
<a {{ main.active ? 'class="active"' : '' }} href="{{ main.url }}">{% trans %}All{% endtrans %}</a>
<div tabindex="0" class=separator>&nbsp;</div>
<a {{ media.active ? 'class="active"' : '' }} href="{{ media.url }}">{% trans %}Media{% endtrans %}</a>
</nav>

View File

@ -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);
}

View File

@ -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;