Compare commits
6 Commits
ba078b7b76
...
1f01923aa1
Author | SHA1 | Date | |
---|---|---|---|
1f01923aa1 | |||
1a99762699 | |||
f346cd8167 | |||
7aa90954eb | |||
0050371de7 | |||
b7872ba4ee |
@ -20,16 +20,23 @@ declare(strict_types = 1);
|
||||
|
||||
/**
|
||||
* @author Hugo Sales <hugo@hsal.es>
|
||||
* @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
|
||||
* @author Eliseu Amaro <mail@eliseuama.ro>
|
||||
* @copyright 2021-2022 Free Software Foundation, Inc http://www.fsf.org
|
||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||
*/
|
||||
|
||||
namespace Component\Conversation\Controller;
|
||||
|
||||
use App\Core\DB\DB;
|
||||
use App\Core\Form;
|
||||
use function App\Core\I18n\_m;
|
||||
use App\Util\Common;
|
||||
use App\Util\Exception\RedirectException;
|
||||
use Component\Conversation\Entity\ConversationBlock;
|
||||
use Component\Feed\Feed;
|
||||
use Component\Feed\Util\FeedController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use function App\Core\I18n\_m;
|
||||
|
||||
class Conversation extends FeedController
|
||||
{
|
||||
@ -49,4 +56,24 @@ class Conversation extends FeedController
|
||||
'page_title' => _m('Conversation'),
|
||||
];
|
||||
}
|
||||
|
||||
public function muteConversation(Request $request, int $conversation_id)
|
||||
{
|
||||
$user = Common::ensureLoggedIn();
|
||||
$form = Form::create([
|
||||
['mute_conversation', SubmitType::class, ['label' => _m('Mute conversation')]],
|
||||
]);
|
||||
|
||||
$form->handleRequest($request);
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
DB::persist(ConversationBlock::create(['conversation_id' => $conversation_id, 'actor_id' => $user->getId()]));
|
||||
DB::flush();
|
||||
throw new RedirectException();
|
||||
}
|
||||
|
||||
return [
|
||||
'_template' => 'conversation/mute.html.twig',
|
||||
'form' => $form->createView(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -26,16 +26,17 @@ namespace Component\Conversation;
|
||||
use App\Core\Cache;
|
||||
use App\Core\DB\DB;
|
||||
use App\Core\Event;
|
||||
use function App\Core\I18n\_m;
|
||||
use App\Core\Modules\Component;
|
||||
use App\Core\Router\RouteLoader;
|
||||
use App\Core\Router\Router;
|
||||
use App\Entity\Activity;
|
||||
use App\Entity\Actor;
|
||||
use App\Entity\Note;
|
||||
use App\Util\Common;
|
||||
use Component\Conversation\Controller\Reply as ReplyController;
|
||||
use Component\Conversation\Entity\Conversation as ConversationEntity;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use function App\Core\I18n\_m;
|
||||
|
||||
class Conversation extends Component
|
||||
{
|
||||
@ -138,7 +139,7 @@ class Conversation extends Component
|
||||
}
|
||||
|
||||
// Filter out multiple replies from the same actor
|
||||
$reply_actors = array_unique($reply_actors, SORT_REGULAR);
|
||||
$reply_actors = array_unique($reply_actors, \SORT_REGULAR);
|
||||
$result[] = ['actors' => $reply_actors, 'action' => 'replied to'];
|
||||
|
||||
return Event::next;
|
||||
@ -148,6 +149,7 @@ class Conversation extends Component
|
||||
{
|
||||
$r->connect('reply_add', '/object/note/new?to={actor_id<\d+>}&reply_to={note_id<\d+>}', [ReplyController::class, 'addReply']);
|
||||
$r->connect('conversation', '/conversation/{conversation_id<\d+>}', [Controller\Conversation::class, 'showConversation']);
|
||||
$r->connect('conversation_mute', '/conversation/{conversation_id<\d+>}/mute', [Controller\Conversation::class, 'muteConversation']);
|
||||
|
||||
return Event::next;
|
||||
}
|
||||
@ -174,4 +176,39 @@ class Conversation extends Component
|
||||
Cache::delete("note-replies-{$note->getId()}");
|
||||
return Event::next;
|
||||
}
|
||||
|
||||
public function onAddExtraNoteActions(Request $request, Note $note, array &$actions)
|
||||
{
|
||||
if (\is_null($actor = Common::actor())) {
|
||||
return Event::next;
|
||||
}
|
||||
|
||||
$actions[] = [
|
||||
'title' => _m('Mute conversation'),
|
||||
'classes' => '',
|
||||
'url' => Router::url('conversation_mute', ['conversation_id' => $note->getConversationId()]),
|
||||
];
|
||||
|
||||
return Event::next;
|
||||
}
|
||||
|
||||
public function onNewNotificationShould(Activity $activity, Actor $actor)
|
||||
{
|
||||
if ($activity->getObjectType() === 'note') {
|
||||
$is_blocked = !empty(DB::dql(
|
||||
<<<'EOQ'
|
||||
select 1
|
||||
from note n
|
||||
join conversation_block cb with n.conversation_id = cb.conversation_id
|
||||
where n.id = :object_id
|
||||
EOQ,
|
||||
['object_id' => $activity->getObjectId()],
|
||||
));
|
||||
if ($is_blocked) {
|
||||
return Event::stop;
|
||||
} else {
|
||||
return Event::next;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
95
components/Conversation/Entity/ConversationBlock.php
Normal file
95
components/Conversation/Entity/ConversationBlock.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?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 Component\Conversation\Entity;
|
||||
|
||||
use App\Core\Entity;
|
||||
use DateTimeInterface;
|
||||
|
||||
/**
|
||||
* Entity class for ConversationsBlocks
|
||||
*
|
||||
* @category DB
|
||||
* @package GNUsocial
|
||||
*
|
||||
* @author Hugo Sales <hugo@hsal.es>
|
||||
* @copyright 2022 Free Software Foundation, Inc http://www.fsf.org
|
||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||
*/
|
||||
class ConversationBlock extends Entity
|
||||
{
|
||||
// {{{ Autocode
|
||||
// @codeCoverageIgnoreStart
|
||||
private int $conversation_id;
|
||||
private int $actor_id;
|
||||
private DateTimeInterface $created;
|
||||
|
||||
public function setConversationId(int $conversation_id): self
|
||||
{
|
||||
$this->conversation_id = $conversation_id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getConversationId(): int
|
||||
{
|
||||
return $this->conversation_id;
|
||||
}
|
||||
|
||||
public function setActorId(int $actor_id): self
|
||||
{
|
||||
$this->actor_id = $actor_id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getActorId(): int
|
||||
{
|
||||
return $this->actor_id;
|
||||
}
|
||||
|
||||
public function setCreated(DateTimeInterface $created): self
|
||||
{
|
||||
$this->created = $created;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCreated(): DateTimeInterface
|
||||
{
|
||||
return $this->created;
|
||||
}
|
||||
|
||||
// @codeCoverageIgnoreEnd
|
||||
// }}} Autocode
|
||||
|
||||
public static function schemaDef(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'conversation_block',
|
||||
'fields' => [
|
||||
'conversation_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Conversation.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'The conversation being blocked'],
|
||||
'actor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'Who blocked the conversation'],
|
||||
'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
|
||||
],
|
||||
'primary key' => ['conversation_id', 'actor_id'],
|
||||
];
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block body %}
|
||||
{{ form(form) }}
|
||||
{% endblock body %}
|
@ -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));
|
||||
|
@ -90,13 +90,13 @@ class Language extends Component
|
||||
$note_expr = $temp_note_expr;
|
||||
$actor_expr = $temp_actor_expr;
|
||||
return Event::stop;
|
||||
} elseif (Formatting::startsWith($term, GSF::cartesianProduct(['-', '_'], ['note', 'post'], ['lang', 'language'], [':']))) {
|
||||
} elseif (Formatting::startsWith($term, GSF::cartesianProduct([['note', 'post'], ['lang', 'language'], [':']], separator: ['-', '_']))) {
|
||||
$note_expr = $temp_note_expr;
|
||||
return Event::stop;
|
||||
} elseif (Formatting::startsWith($term, GSF::cartesianProduct(['-', '_'], ['note', 'post'], ['author', 'actor', 'people', 'person'], ['lang', 'language'], [':']))) {
|
||||
} elseif (Formatting::startsWith($term, GSF::cartesianProduct([['note', 'post'], ['author', 'actor', 'people', 'person'], ['lang', 'language'], [':']], separator: ['-', '_']))) {
|
||||
$note_expr = $temp_note_actor_expr;
|
||||
return Event::stop;
|
||||
} elseif (Formatting::startsWith($term, GSF::cartesianProduct(['-', '_'], ['actor', 'people', 'person'], ['lang', 'language'], [':']))) {
|
||||
} elseif (Formatting::startsWith($term, GSF::cartesianProduct([['actor', 'people', 'person'], ['lang', 'language'], [':']], separator: ['-', '_']))) {
|
||||
$actor_expr = $temp_actor_expr;
|
||||
return Event::stop;
|
||||
}
|
||||
|
@ -32,6 +32,7 @@ use App\Entity\Activity;
|
||||
use App\Entity\Actor;
|
||||
use App\Entity\LocalUser;
|
||||
use Component\FreeNetwork\FreeNetwork;
|
||||
use Component\Group\Entity\GroupInbox;
|
||||
use Component\Notification\Controller\Feed;
|
||||
|
||||
class Notification extends Component
|
||||
@ -76,6 +77,10 @@ class Notification extends Component
|
||||
if ($target->getIsLocal()) {
|
||||
if ($target->isGroup()) {
|
||||
// FIXME: Make sure we check (for both local and remote) users are in the groups they send to!
|
||||
DB::persist(GroupInbox::create([
|
||||
'group_id' => $target->getId(),
|
||||
'activity_id' => $activity->getId(),
|
||||
]));
|
||||
} else {
|
||||
if ($target->hasBlocked($activity->getActor())) {
|
||||
Log::info("Not saving reply to actor {$target->getId()} from sender {$sender->getId()} because of a block.");
|
||||
|
@ -46,7 +46,6 @@ use App\Util\Formatting;
|
||||
use Component\Attachment\Entity\ActorToAttachment;
|
||||
use Component\Attachment\Entity\AttachmentToNote;
|
||||
use Component\Conversation\Conversation;
|
||||
use Component\Group\Entity\GroupInbox;
|
||||
use Component\Group\Entity\LocalGroup;
|
||||
use Component\Language\Entity\Language;
|
||||
use Functional as F;
|
||||
@ -267,24 +266,12 @@ class Posting extends Component
|
||||
}
|
||||
}
|
||||
|
||||
$mentioned = [];
|
||||
foreach (F\unique(F\flat_map($mentions, fn (array $m) => $m['mentioned'] ?? []), fn (Actor|null $a) => $a?->getId()) as $m) {
|
||||
if (!\is_null($m)) {
|
||||
$mentioned[] = $m->getId();
|
||||
|
||||
if ($m->isGroup()) {
|
||||
DB::persist(GroupInbox::create([
|
||||
'group_id' => $m->getId(),
|
||||
'activity_id' => $activity->getId(),
|
||||
]));
|
||||
}
|
||||
}
|
||||
}
|
||||
$mention_ids = F\unique(F\flat_map($mentions, fn (array $m) => F\map($m['mentioned'] ?? [], fn (Actor $a) => $a->getId())));
|
||||
|
||||
DB::flush();
|
||||
|
||||
if ($notify) {
|
||||
Event::handle('NewNotification', [$actor, $activity, ['object' => $mentioned], _m('{nickname} created a note {note_id}.', ['nickname' => $actor->getNickname(), 'note_id' => $activity->getObjectId()])]);
|
||||
Event::handle('NewNotification', [$actor, $activity, ['object' => $mention_ids], _m('{nickname} created a note {note_id}.', ['nickname' => $actor->getNickname(), 'note_id' => $activity->getObjectId()])]);
|
||||
}
|
||||
|
||||
return $note;
|
||||
|
@ -39,6 +39,7 @@ use App\Util\Exception\ClientException;
|
||||
use App\Util\Formatting;
|
||||
use App\Util\Functional as GSF;
|
||||
use App\Util\HTML;
|
||||
use App\Util\Nickname;
|
||||
use Component\Language\Entity\Language;
|
||||
use Component\Tag\Controller as C;
|
||||
use Doctrine\Common\Collections\ExpressionBuilder;
|
||||
@ -47,7 +48,6 @@ use Doctrine\ORM\QueryBuilder;
|
||||
use Functional as F;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use App\Util\Nickname;
|
||||
|
||||
/**
|
||||
* Component responsible for extracting tags from posted notes, as well as normalizing them
|
||||
@ -177,7 +177,7 @@ class Tag extends Component
|
||||
$note_expr = $temp_note_expr;
|
||||
} elseif (Formatting::startsWith($term, ['people:', 'actor:'])) {
|
||||
$actor_expr = $temp_actor_expr;
|
||||
} elseif (Formatting::startsWith($term, GSF::cartesianProduct('-', ['people', 'actor'], ['circle:', 'list:']))) {
|
||||
} elseif (Formatting::startsWith($term, GSF::cartesianProduct([['people', 'actor'], ['circle', 'list'], [':']], separator: ['-', '_']))) {
|
||||
$null_tagger_expr = $eb->isNull('actor_circle.tagger');
|
||||
$tagger_expr = \is_null($actor_expr) ? $null_tagger_expr : $eb->orX($null_tagger_expr, $eb->eq('actor_circle.tagger', $actor->getId()));
|
||||
$tags = array_unique([$search_term, $canon_search_term]);
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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>
|
172
plugins/NoteTypeFeedFilter/NoteTypeFeedFilter.php
Normal file
172
plugins/NoteTypeFeedFilter/NoteTypeFeedFilter.php
Normal 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;
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
{% for tab in tabs %}
|
||||
<a {{ tab.active ? 'class="active"' : '' }} href="{{ tab.url }}">{{ tab['icon'] }}</a>
|
||||
{% endfor %}
|
@ -136,8 +136,6 @@ abstract class Controller extends AbstractController implements EventSubscriberI
|
||||
if (\is_array($controller)) {
|
||||
$controller = $controller[0];
|
||||
}
|
||||
|
||||
// XXX: Could we do this differently?
|
||||
if (is_subclass_of($controller, FeedController::class)) {
|
||||
$this->vars = $controller->postProcess($this->vars);
|
||||
}
|
||||
|
@ -32,12 +32,16 @@ declare(strict_types = 1);
|
||||
|
||||
namespace App\Util;
|
||||
|
||||
use Stringable;
|
||||
|
||||
abstract class Functional
|
||||
{
|
||||
/**
|
||||
* TODO replace with \Functional\cartesian_product when it gets merged upstream
|
||||
*
|
||||
* @param array<array<string|Stringable>> $collections
|
||||
*/
|
||||
public static function cartesianProduct(string|array $separator, ...$collections)
|
||||
public static function cartesianProduct(array $collections, string|array $separator = ''): array
|
||||
{
|
||||
$aggregation = [];
|
||||
$left = array_shift($collections);
|
||||
|
Loading…
Reference in New Issue
Block a user