Create Post/Tag/Remove module class and route

- Fix tag name parameter in single tag removal link in mod/photos
- Remove "/post" from backend routes as it was preventing sessions from working in this module. No existing module had a route starting with "/post".
This commit is contained in:
Hypolite Petovan 2022-11-01 23:42:20 -04:00
parent da1c13368b
commit e0dc7a471e
5 changed files with 172 additions and 7 deletions

View file

@ -1250,15 +1250,17 @@ function photos_content(App $a)
if (!empty($link_item['id'])) { if (!empty($link_item['id'])) {
// parse tags and add links // parse tags and add links
$tag_arr = []; $tag_arr = [];
foreach (Tag::getByURIId($link_item['uri-id']) as $tag) { foreach (explode(',', Tag::getCSVByURIId($link_item['uri-id'])) as $tag_name) {
$tag_arr[] = [ if ($tag_name) {
'name' => $tag['name'], $tag_arr[] = [
'removeurl' => '/tagrm/' . $link_item['id'] . '/' . bin2hex($tag['name']) 'name' => BBCode::toPlaintext($tag_name),
]; 'removeurl' => 'post/' . $link_item['id'] . '/tag/remove/' . bin2hex($tag_name) . '?return=' . urlencode(DI::args()->getCommand()),
];
}
} }
$tags = ['title' => DI::l10n()->t('Tags: '), 'tags' => $tag_arr]; $tags = ['title' => DI::l10n()->t('Tags: '), 'tags' => $tag_arr];
if ($cmd === 'edit') { if ($cmd === 'edit') {
$tags['removeanyurl'] = 'tagrm/' . $link_item['id']; $tags['removeanyurl'] = 'post/' . $link_item['id'] . '/tag/remove?return=' . urlencode(DI::args()->getCommand());
$tags['removetitle'] = DI::l10n()->t('[Select tags to remove]'); $tags['removetitle'] = DI::l10n()->t('[Select tags to remove]');
} }
} }

View file

@ -68,7 +68,6 @@ class Mode
'objects', 'objects',
'outbox', 'outbox',
'poco', 'poco',
'post',
'pubsub', 'pubsub',
'pubsubhubbub', 'pubsubhubbub',
'receive', 'receive',

View file

@ -0,0 +1,140 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module\Post\Tag;
use Friendica\App;
use Friendica\Content\Text\BBCode;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Model\Post;
use Friendica\Model\Tag;
use Friendica\Module\Response;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
class Remove extends \Friendica\BaseModule
{
/** @var IHandleUserSessions */
private $session;
public function __construct(IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
{
parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->session = $session;
}
protected function post(array $request = [])
{
if (!$this->session->getLocalUserId()) {
$this->baseUrl->redirect($request['return'] ?? '');
}
if (isset($request['cancel'])) {
$this->baseUrl->redirect($request['return'] ?? '');
}
$tags = [];
foreach ($request['tag'] ?? [] as $tag => $checked) {
if ($checked) {
$tags[] = hex2bin(trim($tag));
}
}
$this->removeTagsFromItem($this->parameters['item_id'], $tags);
$this->baseUrl->redirect($request['return'] ?? '');
}
protected function content(array $request = []): string
{
$returnUrl = $request['return'] ?? '';
if (!$this->session->getLocalUserId()) {
$this->baseUrl->redirect($returnUrl);
}
if (isset($this->parameters['tag_name'])) {
$this->removeTagsFromItem($this->parameters['item_id'], [trim(hex2bin($this->parameters['tag_name']))]);
$this->baseUrl->redirect($returnUrl);
}
$item_id = intval($this->parameters['item_id']);
if (!$item_id) {
$this->baseUrl->redirect($returnUrl);
}
$item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => $this->session->getLocalUserId()]);
if (!$item) {
$this->baseUrl->redirect($returnUrl);
}
$tag_text = Tag::getCSVByURIId($item['uri-id']);
$tags = explode(',', $tag_text);
if (empty($tags)) {
$this->baseUrl->redirect($returnUrl);
}
$tag_checkboxes = array_map(function ($tag_text) {
return ['tag[' . bin2hex($tag_text) . ']', BBCode::toPlaintext($tag_text)];
}, $tags);
$tpl = Renderer::getMarkupTemplate('post/tag/remove.tpl');
return Renderer::replaceMacros($tpl, [
'$l10n' => [
'header' => $this->t('Remove Item Tag'),
'desc' => $this->t('Select a tag to remove: '),
'remove' => $this->t('Remove'),
'cancel' => $this->t('Cancel'),
],
'$item_id' => $item_id,
'$return' => $returnUrl,
'$tag_checkboxes' => $tag_checkboxes,
]);
}
/**
* @param int $item_id
* @param array $tags
* @throws \Exception
*/
private function removeTagsFromItem(int $item_id, array $tags)
{
if (empty($item_id) || empty($tags)) {
return;
}
$item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => $this->session->getLocalUserId()]);
if (empty($item)) {
return;
}
foreach ($tags as $tag) {
if (preg_match('~([#@!])\[url=([^\[\]]*)]([^\[\]]*)\[/url]~im', $tag, $results)) {
Tag::removeByHash($item['uri-id'], $results[1], $results[3], $results[2]);
}
}
}
}

View file

@ -531,6 +531,11 @@ return [
], ],
'/ping' => [Module\Notifications\Ping::class, [R::GET]], '/ping' => [Module\Notifications\Ping::class, [R::GET]],
'/post' => [
'/{item_id}/tag/remove[/{tag_name}]' => [Module\Post\Tag\Remove::class, [R::GET, R::POST]],
],
'/pretheme' => [Module\ThemeDetails::class, [R::GET]], '/pretheme' => [Module\ThemeDetails::class, [R::GET]],
'/probe' => [Module\Debug\Probe::class, [R::GET]], '/probe' => [Module\Debug\Probe::class, [R::GET]],

View file

@ -0,0 +1,19 @@
<div class="generic-page-wrapper">
<h3>{{$l10n.header}}</h3>
<p id="tag-remove-desc">{{$l10n.desc}}</p>
<form id="tagrm" action="post/{{$item_id}}/tag/remove?return={{$return}}" method="post">
<ul>
{{foreach $tag_checkboxes as $tag_checkbox}}
<li>
{{include file="field_checkbox.tpl" field=$tag_checkbox}}
</li>
{{/foreach}}
</ul>
<p>
<button type="submit" id="tagrm-submit" class="btn btn-primary" name="submit">{{$l10n.remove}}</button>
<button type="submit" id="tagrm-submit" class="btn" name="cancel">{{$l10n.cancel}}</button>
</p>
</form>
</div>