2020-09-04 17:38:48 +01:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2020-09-04 17:38:48 +01:00
|
|
|
// {{{ 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\Repeat;
|
|
|
|
|
|
|
|
use App\Core\DB\DB;
|
|
|
|
use App\Core\Event;
|
2021-11-16 19:39:03 +00:00
|
|
|
use function App\Core\I18n\_m;
|
2021-11-11 12:24:45 +00:00
|
|
|
use App\Core\Modules\NoteHandlerPlugin;
|
2021-10-27 20:39:34 +01:00
|
|
|
use App\Core\Router\RouteLoader;
|
|
|
|
use App\Core\Router\Router;
|
2021-11-07 01:32:06 +00:00
|
|
|
use App\Entity\Actor;
|
2021-11-11 12:24:45 +00:00
|
|
|
use App\Entity\Note;
|
|
|
|
use App\Util\Common;
|
2021-11-16 19:39:03 +00:00
|
|
|
use App\Util\Exception\ClientException;
|
2021-11-01 21:19:20 +00:00
|
|
|
use App\Util\Exception\DuplicateFoundException;
|
2021-10-27 20:39:34 +01:00
|
|
|
use App\Util\Exception\InvalidFormException;
|
|
|
|
use App\Util\Exception\NoSuchNoteException;
|
2021-11-01 21:19:20 +00:00
|
|
|
use App\Util\Exception\NotFoundException;
|
2021-09-05 15:56:32 +01:00
|
|
|
use App\Util\Exception\RedirectException;
|
2021-11-16 19:39:03 +00:00
|
|
|
use App\Util\Exception\ServerException;
|
2021-11-15 17:11:27 +00:00
|
|
|
use App\Util\Formatting;
|
|
|
|
use Plugin\Repeat\Entity\NoteRepeat;
|
2020-09-04 17:38:48 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
2021-08-18 19:14:24 +01:00
|
|
|
class Repeat extends NoteHandlerPlugin
|
2020-09-04 17:38:48 +01:00
|
|
|
{
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* HTML rendering event that adds the repeat form as a note
|
|
|
|
* action, if a user is logged in
|
2021-09-05 17:56:03 +01:00
|
|
|
*
|
2021-10-27 20:39:34 +01:00
|
|
|
* @throws InvalidFormException
|
|
|
|
* @throws NoSuchNoteException
|
2021-11-16 19:39:03 +00:00
|
|
|
* @throws RedirectException*@throws ClientException*@throws DuplicateFoundException
|
2021-10-27 20:39:34 +01:00
|
|
|
*
|
|
|
|
* @return bool Event hook
|
2020-11-06 19:47:15 +00:00
|
|
|
*/
|
2021-10-27 20:39:34 +01:00
|
|
|
public function onAddNoteActions(Request $request, Note $note, array &$actions): bool
|
2020-09-04 17:38:48 +01:00
|
|
|
{
|
2021-11-11 12:24:45 +00:00
|
|
|
if (\is_null($user = Common::user())) {
|
2020-11-06 19:47:15 +00:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-11-01 21:19:20 +00:00
|
|
|
// If note is repeated, "is_repeated" is 1
|
2021-11-16 19:39:03 +00:00
|
|
|
$is_repeat = DB::count('note_repeat', ['note_id' => $note->getId()]) >= 1;
|
|
|
|
|
2021-11-01 21:19:20 +00:00
|
|
|
try {
|
2021-11-16 19:39:03 +00:00
|
|
|
if (DB::findOneBy('note_repeat', ['repeat_of' => $note->getId()])) {
|
2021-11-01 21:19:20 +00:00
|
|
|
return Event::next;
|
|
|
|
}
|
2021-11-16 19:39:03 +00:00
|
|
|
} catch (DuplicateFoundException|NotFoundException $e) {
|
2021-11-01 21:19:20 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 20:39:34 +01:00
|
|
|
// Generating URL for repeat action route
|
2021-11-11 12:24:45 +00:00
|
|
|
$args = ['id' => $note->getId()];
|
|
|
|
$type = Router::ABSOLUTE_PATH;
|
|
|
|
$repeat_action_url = $is_repeat
|
|
|
|
? Router::url('repeat_remove', $args, $type)
|
|
|
|
: Router::url('repeat_add', $args, $type);
|
|
|
|
|
|
|
|
// TODO clean this up
|
|
|
|
// SECURITY: open redirect?
|
|
|
|
$query_string = $request->getQueryString();
|
2021-10-27 20:39:34 +01:00
|
|
|
// Concatenating get parameter to redirect the user to where he came from
|
2021-11-11 12:24:45 +00:00
|
|
|
$repeat_action_url .= !\is_null($query_string) ? '?from=' . mb_substr($query_string, 2) : '';
|
2021-09-05 15:56:32 +01:00
|
|
|
|
2021-11-11 12:24:45 +00:00
|
|
|
$extra_classes = $is_repeat ? 'note-actions-set' : 'note-actions-unset';
|
2021-10-27 20:39:34 +01:00
|
|
|
$repeat_action = [
|
2021-11-11 12:24:45 +00:00
|
|
|
'url' => $repeat_action_url,
|
2021-12-02 15:05:23 +00:00
|
|
|
'title' => $is_repeat ? 'Remove this repeat' : 'Repeat this note!',
|
2021-11-11 12:24:45 +00:00
|
|
|
'classes' => "button-container repeat-button-container {$extra_classes}",
|
|
|
|
'id' => 'repeat-button-container-' . $note->getId(),
|
2021-10-27 20:39:34 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$actions[] = $repeat_action;
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:38:15 +00:00
|
|
|
/**
|
2021-11-16 19:39:03 +00:00
|
|
|
* Append on note information about user actions.
|
|
|
|
*
|
|
|
|
* @return array|bool
|
2021-11-15 17:38:15 +00:00
|
|
|
*/
|
2021-11-16 19:39:03 +00:00
|
|
|
public function onAppendCardNote(array $vars, array &$result)
|
|
|
|
{
|
2021-11-07 01:32:06 +00:00
|
|
|
// if note is the original and user isn't the one who repeated, append on end "user repeated this"
|
|
|
|
// if user is the one who repeated, append on end "you repeated this, remove repeat?"
|
2021-11-16 19:39:03 +00:00
|
|
|
$check_user = !\is_null(Common::user());
|
2021-11-15 17:38:15 +00:00
|
|
|
|
2021-11-07 01:32:06 +00:00
|
|
|
$note = $vars['note'];
|
2021-10-29 00:33:24 +01:00
|
|
|
|
2021-11-15 17:11:27 +00:00
|
|
|
$complementary_info = '';
|
2021-11-16 19:39:03 +00:00
|
|
|
$repeat_actor = [];
|
|
|
|
$note_repeats = NoteRepeat::getNoteRepeats($note);
|
2021-11-15 17:11:27 +00:00
|
|
|
|
|
|
|
// Get actors who replied
|
|
|
|
foreach ($note_repeats as $reply) {
|
|
|
|
$repeat_actor[] = Actor::getWithPK($reply->getActorId());
|
|
|
|
}
|
2021-11-16 19:39:03 +00:00
|
|
|
if (\count($repeat_actor) < 1) {
|
|
|
|
return Event::next;
|
2021-11-15 17:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out multiple replies from the same actor
|
2021-11-16 19:39:03 +00:00
|
|
|
$repeat_actor = array_unique($repeat_actor, \SORT_REGULAR);
|
2021-11-15 17:11:27 +00:00
|
|
|
|
|
|
|
// Add to complementary info
|
|
|
|
foreach ($repeat_actor as $actor) {
|
2021-11-16 19:39:03 +00:00
|
|
|
$repeat_actor_url = $actor->getUrl();
|
2021-11-15 17:11:27 +00:00
|
|
|
$repeat_actor_nickname = $actor->getNickname();
|
|
|
|
|
2021-11-15 17:38:15 +00:00
|
|
|
if ($check_user && $actor->getId() === (Common::actor())->getId()) {
|
2021-11-15 17:11:27 +00:00
|
|
|
// If the repeat is yours
|
2021-11-16 19:39:03 +00:00
|
|
|
try {
|
|
|
|
$you_translation = _m('You');
|
|
|
|
} catch (ServerException $e) {
|
|
|
|
$you_translation = 'You';
|
|
|
|
}
|
|
|
|
|
|
|
|
$prepend = "<a href={$repeat_actor_url}>{$you_translation}</a>, " . ($prepend = &$complementary_info);
|
2021-11-15 17:11:27 +00:00
|
|
|
$complementary_info = $prepend;
|
|
|
|
} else {
|
|
|
|
// If the repeat is from someone else
|
|
|
|
$complementary_info .= "<a href={$repeat_actor_url}>{$repeat_actor_nickname}</a>, ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$complementary_info = rtrim(trim($complementary_info), ',');
|
|
|
|
$complementary_info .= ' repeated this note.';
|
|
|
|
$result[] = Formatting::twigRenderString($complementary_info, []);
|
|
|
|
|
|
|
|
return $result;
|
2021-10-29 00:33:24 +01:00
|
|
|
}
|
|
|
|
|
2021-10-27 20:39:34 +01:00
|
|
|
public function onAddRoute(RouteLoader $r): bool
|
|
|
|
{
|
|
|
|
// Add/remove note to/from repeats
|
|
|
|
$r->connect(id: 'repeat_add', uri_path: '/object/note/{id<\d+>}/repeat', target: [Controller\Repeat::class, 'repeatAddNote']);
|
|
|
|
$r->connect(id: 'repeat_remove', uri_path: '/object/note/{id<\d+>}/unrepeat', target: [Controller\Repeat::class, 'repeatRemoveNote']);
|
2020-11-06 19:47:15 +00:00
|
|
|
|
2020-09-04 17:38:48 +01:00
|
|
|
return Event::next;
|
2021-10-27 20:39:34 +01:00
|
|
|
}
|
2020-09-04 20:36:37 +01:00
|
|
|
}
|