[PLUGIN][NoteTypeFeedFilter][MediaFeed][COMPONENT][Feed] Rename MediaFeed to NoteTypeFeedFilter and add support for filtering by more types, moving functionality from Feed component

This commit is contained in:
Hugo Sales 2021-12-31 19:13:19 +00:00
parent b7872ba4ee
commit 0050371de7
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
6 changed files with 175 additions and 144 deletions

View File

@ -34,12 +34,9 @@ namespace Component\Feed\Util;
use App\Core\Controller;
use App\Core\Event;
use function App\Core\I18n\_m;
use App\Entity\Actor;
use App\Entity\Note;
use App\Util\Common;
use App\Util\Exception\ClientException;
use App\Util\Formatting;
use Functional as F;
abstract class FeedController extends Controller
@ -56,10 +53,6 @@ abstract class FeedController extends Controller
if (\array_key_exists('notes', $result)) {
$notes = $result['notes'];
self::enforceScope($notes, $actor);
$types = $this->string('types');
if (!\is_null($types)) {
self::filterNoteType($notes, $types);
}
Event::handle('FilterNoteList', [$actor, &$notes, $result['request']]);
Event::handle('FormatNoteList', [$notes, &$result['notes']]);
}
@ -67,43 +60,6 @@ abstract class FeedController extends Controller
return $result;
}
private static function filterNoteType(array &$notes, string $types): void
{
$types = explode(',', $types);
$types = [
...$types,
// Add any missing types as a negation
...array_diff(['!media', '!link', '!text', '!tags'], F\map($types, fn ($t) => $t[0] === '!' ? $t : '!' . $t)),
];
$notes = F\select($notes, function (Note $note) use ($types) {
$include = false;
foreach ($types as $type) {
$is_negate = $type[0] === '!';
$type = Formatting::removePrefix($type, '!');
$ret = (function () use ($note, $type) {
switch ($type) {
case 'media':
return !empty($note->getAttachments());
case 'link':
return !empty($note->getLinks());
case 'text':
return !\is_null($note->getContent());
case 'tags':
return !empty($note->getTags());
default:
throw new ClientException(_m('Unknown note type requested ({type})', ['{type}' => $type]));
}
})();
if ($is_negate && $ret) {
return false;
}
$include = $include || $ret;
}
return $include;
});
}
private static function enforceScope(array &$notes, ?Actor $actor): void
{
$notes = F\select($notes, fn (Note $n) => $n->isVisibleTo($actor));

View File

@ -1,98 +0,0 @@
<?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/>.
// }}}
/**
* Media Feed Plugin for GNU social
*
* @package GNUsocial
* @category Plugin
*
* @author Phablulo <phablulo@gmail.com>
* @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
namespace Plugin\MediaFeed;
use App\Core\Event;
use App\Core\Modules\Plugin;
use App\Entity\Actor;
use App\Entity\Note;
use App\Util\Formatting;
use Functional as F;
use Symfony\Component\HttpFoundation\Request;
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 onAddFeedActions(Request $request, &$res): bool
{
$isMediaActive = $request->get('filter-type') === 'media';
// we need two urls: one with filter-type=media and without it.
$query = mb_strpos($request->getRequestUri(), '?');
$mediaURL = $request->getRequestUri() . ($query !== false ? '&' : '?') . 'filter-type=media';
$allURL = $request->getPathInfo();
if ($query !== false) {
$params = explode('&', mb_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('mediaFeed/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

@ -1,2 +0,0 @@
<a {{ main.active ? 'class="active"' : '' }} href="{{ main.url }}">{{ icon('feed', 'icon icon-right') | raw }}</a>
<a {{ media.active ? 'class="active"' : '' }} href="{{ media.url }}">{{ icon('media', 'icon icon-right') | raw }}</a>

View File

@ -0,0 +1,172 @@
<?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/>.
// }}}
/**
* Media Feed Plugin for GNU social
*
* @package GNUsocial
* @category Plugin
*
* @author Phablulo <phablulo@gmail.com>
* @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
namespace Plugin\NoteTypeFeedFilter;
use App\Core\Event;
use function App\Core\I18n\_m;
use App\Core\Modules\Plugin;
use App\Entity\Actor;
use App\Entity\Note;
use App\Util\Exception\BugFoundException;
use App\Util\Exception\ClientException;
use App\Util\Formatting;
use App\Util\Functional as GSF;
use Functional as F;
use Symfony\Component\HttpFoundation\Request;
class NoteTypeFeedFilter extends Plugin
{
public const ALLOWED_TYPES = ['media', 'link', 'text', 'tag'];
private function unknownType(string $type): ClientException
{
return new ClientException(_m('Unknown note type requested ({type})', ['{type}' => $type]));
}
private function normalizeTypesList(array $types, bool $add_missing = true): array
{
if (empty($types)) {
return self::ALLOWED_TYPES;
} else {
$result = [];
foreach (self::ALLOWED_TYPES as $allowed_type) {
foreach ($types as $type) {
if ($type === 'all') {
return self::ALLOWED_TYPES;
} elseif (mb_detect_encoding($type, 'ASCII', strict: true) === false || empty($type)) {
throw $this->unknownType($type);
} elseif (\in_array(
$allowed_type,
GSF::cartesianProduct([
['', '!'],
[$type, mb_substr($type, 1), mb_substr($type, 0, -1)], // The original, without the first or without the last character
]),
)) {
$result[] = ($type[0] === '!' ? '!' : '') . $allowed_type;
continue 2;
}
} // else
if ($add_missing) {
$result[] = '!' . $allowed_type;
}
}
return $result;
}
}
public function onFilterNoteList(?Actor $actor, array &$notes, Request $request): bool
{
$types = $this->normalizeTypesList(\is_null($request->get('note-types')) ? [] : explode(',', $request->get('note-types')));
$notes = F\select(
$notes,
function (Note $note) use ($types) {
$include = false; // TODO Would like to express this as a reduce of some sort...
foreach ($types as $type) {
$is_negate = $type[0] === '!';
$type = Formatting::removePrefix($type, '!');
switch ($type) {
case 'text':
$ret = !\is_null($note->getContent());
break;
case 'media':
$ret = !empty($note->getAttachments());
break;
case 'link':
$ret = !empty($note->getLinks());
break;
case 'tag':
$ret = !empty($note->getTags());
break;
default:
throw new BugFoundException("Unkown note type requested {$type}", previous: $this->unknownType($type));
}
if ($is_negate && $ret) {
return false;
}
$include = $include || $ret;
}
return $include;
},
);
return Event::next;
}
/**
* Draw the media feed navigation.
*/
public function onAddFeedActions(Request $request, &$res): bool
{
$qs = [];
parse_str($request->getQueryString(), $qs);
if (\array_key_exists('p', $qs) && \is_string($qs['p'])) {
unset($qs['p']);
}
$types = $this->normalizeTypesList(\is_null($request->get('note-types')) ? [] : explode(',', $request->get('note-types')), add_missing: false);
$ftypes = array_flip($types);
$tabs = [
'all' => [
'active' => empty($types) || $types === self::ALLOWED_TYPES,
'url' => '?' . http_build_query(['note-types' => implode(',', self::ALLOWED_TYPES)], '', '&', \PHP_QUERY_RFC3986),
'icon' => 'All',
],
];
foreach (self::ALLOWED_TYPES as $allowed_type) {
$active = \array_key_exists($allowed_type, $ftypes);
$new_types = $this->normalizeTypesList([($active ? '!' : '') . $allowed_type, ...$types], add_missing: false);
$new_qs = $qs;
$new_qs['note-types'] = implode(',', $new_types);
$tabs[$allowed_type] = [
'active' => $active,
'url' => '?' . http_build_query($new_qs, '', '&', \PHP_QUERY_RFC3986),
'icon' => $allowed_type,
];
}
$res[] = Formatting::twigRenderFile('NoteTypeFeedFilter/tabs.html.twig', ['tabs' => $tabs]);
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/NoteTypeFeedFilter/assets/css/noteTypeFeedFilter.css';
return Event::next;
}
}

View File

@ -0,0 +1,3 @@
{% for tab in tabs %}
<a {{ tab.active ? 'class="active"' : '' }} href="{{ tab.url }}">{{ tab['icon'] }}</a>
{% endfor %}