2021-10-28 04:39:34 +09:00
|
|
|
<?php
|
|
|
|
|
2021-12-16 20:14:34 +09:00
|
|
|
declare(strict_types = 1);
|
2021-10-28 04:39:34 +09: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/>.
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
2021-12-10 11:45:31 +09:00
|
|
|
namespace Plugin\RepeatNote\Controller;
|
2021-10-28 04:39:34 +09:00
|
|
|
|
|
|
|
use App\Core\Controller;
|
|
|
|
use App\Core\DB\DB;
|
2022-02-19 02:48:06 +09:00
|
|
|
use App\Core\Event;
|
2021-10-28 04:39:34 +09:00
|
|
|
use App\Core\Form;
|
2021-12-16 20:14:34 +09:00
|
|
|
use function App\Core\I18n\_m;
|
2021-11-17 04:39:03 +09:00
|
|
|
use App\Core\Log;
|
2021-10-28 04:39:34 +09:00
|
|
|
use App\Core\Router\Router;
|
2022-02-26 23:45:38 +09:00
|
|
|
use App\Entity\Actor;
|
2021-10-28 04:39:34 +09:00
|
|
|
use App\Entity\Note;
|
|
|
|
use App\Util\Common;
|
2021-11-17 04:39:03 +09:00
|
|
|
use App\Util\Exception\ClientException;
|
2021-10-28 04:39:34 +09:00
|
|
|
use App\Util\Exception\NoLoggedInUser;
|
|
|
|
use App\Util\Exception\RedirectException;
|
2021-12-10 11:45:31 +09:00
|
|
|
use App\Util\Exception\ServerException;
|
2021-10-28 04:39:34 +09:00
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
|
|
|
class Repeat extends Controller
|
|
|
|
{
|
|
|
|
/**
|
2021-11-17 04:39:03 +09:00
|
|
|
* Controller for the note repeat non-JS page
|
|
|
|
*
|
2022-01-27 09:54:27 +09:00
|
|
|
* @param int $note_id Note being repeated
|
|
|
|
*
|
|
|
|
* @throws \App\Util\Exception\DuplicateFoundException
|
2021-11-17 04:39:03 +09:00
|
|
|
* @throws ClientException
|
2021-10-28 04:39:34 +09:00
|
|
|
* @throws NoLoggedInUser
|
2021-11-17 04:39:03 +09:00
|
|
|
* @throws RedirectException
|
2021-12-16 20:14:34 +09:00
|
|
|
* @throws ServerException
|
2021-10-28 04:39:34 +09:00
|
|
|
*/
|
2021-12-27 03:40:54 +09:00
|
|
|
public function repeatAddNote(Request $request, int $note_id): bool|array
|
2021-10-28 04:39:34 +09:00
|
|
|
{
|
2021-11-26 08:08:30 +09:00
|
|
|
$user = Common::ensureLoggedIn();
|
2021-11-18 02:14:15 +09:00
|
|
|
|
2021-12-10 11:45:31 +09:00
|
|
|
$actor_id = $user->getId();
|
2021-12-27 03:40:54 +09:00
|
|
|
$note = Note::getByPK(['id' => $note_id]);
|
2021-10-28 04:39:34 +09:00
|
|
|
|
|
|
|
$form_add_to_repeat = Form::create([
|
|
|
|
['add_repeat', SubmitType::class,
|
|
|
|
[
|
|
|
|
'label' => _m('Repeat note!'),
|
2021-12-16 20:14:34 +09:00
|
|
|
'attr' => [
|
2021-11-17 04:39:03 +09:00
|
|
|
'title' => _m('Repeat this note!'),
|
2021-10-28 04:39:34 +09:00
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$form_add_to_repeat->handleRequest($request);
|
|
|
|
if ($form_add_to_repeat->isSubmitted()) {
|
2022-02-19 02:48:06 +09:00
|
|
|
$repeat_activity = \Plugin\RepeatNote\RepeatNote::repeatNote(note: $note, actor_id: $actor_id);
|
2021-12-10 11:45:31 +09:00
|
|
|
DB::flush();
|
2022-03-14 03:23:19 +09:00
|
|
|
Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $repeat_activity, [], _m('{actor_id} repeated note {note_id}.', ['{actor_id}' => $actor->getId(), '{note_id}' => $repeat_activity->getObjectId()])]);
|
2022-02-19 02:48:06 +09:00
|
|
|
|
2021-11-17 04:39:03 +09:00
|
|
|
// Redirect user to where they came from
|
|
|
|
// Prevent open redirect
|
2021-12-16 20:14:34 +09:00
|
|
|
if (!\is_null($from = $this->string('from'))) {
|
2021-11-26 08:08:30 +09:00
|
|
|
if (Router::isAbsolute($from)) {
|
|
|
|
Log::warning("Actor {$actor_id} attempted to reply to a note and then get redirected to another host, or the URL was invalid ({$from})");
|
2021-11-17 04:39:03 +09:00
|
|
|
throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
|
|
|
|
}
|
2022-01-27 09:54:27 +09:00
|
|
|
// TODO anchor on element id
|
|
|
|
throw new RedirectException(url: $from);
|
2021-10-28 04:39:34 +09:00
|
|
|
}
|
2022-01-27 09:54:27 +09:00
|
|
|
|
|
|
|
// If we don't have a URL to return to, go to the instance root
|
|
|
|
throw new RedirectException('root');
|
2021-10-28 04:39:34 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2021-12-16 20:14:34 +09:00
|
|
|
'_template' => 'repeat/add_to_repeats.html.twig',
|
|
|
|
'note' => $note,
|
2021-10-28 04:39:34 +09:00
|
|
|
'add_repeat' => $form_add_to_repeat->createView(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-27 09:54:27 +09:00
|
|
|
* Controller for the note unrepeat non-JS page
|
|
|
|
*
|
|
|
|
* @param int $note_id Note being unrepeated
|
|
|
|
*
|
|
|
|
* @throws \App\Util\Exception\DuplicateFoundException
|
|
|
|
* @throws \App\Util\Exception\NotFoundException
|
2021-11-17 04:39:03 +09:00
|
|
|
* @throws ClientException
|
2021-10-28 04:39:34 +09:00
|
|
|
* @throws NoLoggedInUser
|
2021-11-17 04:39:03 +09:00
|
|
|
* @throws RedirectException
|
2021-12-16 20:14:34 +09:00
|
|
|
* @throws ServerException
|
2021-10-28 04:39:34 +09:00
|
|
|
*/
|
2021-12-27 03:40:54 +09:00
|
|
|
public function repeatRemoveNote(Request $request, int $note_id): array
|
2021-10-28 04:39:34 +09:00
|
|
|
{
|
2021-12-10 11:45:31 +09:00
|
|
|
$user = Common::ensureLoggedIn();
|
|
|
|
|
|
|
|
$actor_id = $user->getId();
|
2021-10-28 04:39:34 +09:00
|
|
|
|
|
|
|
$form_remove_repeat = Form::create([
|
|
|
|
['remove_repeat', SubmitType::class,
|
|
|
|
[
|
|
|
|
'label' => _m('Remove repeat'),
|
2021-12-16 20:14:34 +09:00
|
|
|
'attr' => [
|
2021-11-17 04:39:03 +09:00
|
|
|
'title' => _m('Remove note from repeats.'),
|
2021-10-28 04:39:34 +09:00
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$form_remove_repeat->handleRequest($request);
|
|
|
|
if ($form_remove_repeat->isSubmitted()) {
|
2022-02-19 02:48:06 +09:00
|
|
|
if (!\is_null($undo_repeat_activity = \Plugin\RepeatNote\RepeatNote::unrepeatNote(note_id: $note_id, actor_id: $actor_id))) {
|
2021-11-16 02:19:20 +09:00
|
|
|
DB::flush();
|
2022-03-14 03:23:19 +09:00
|
|
|
Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $undo_repeat_activity, [], _m('{actor_id} unrepeated note {note_id}.', ['{actor_id}' => $actor->getId(), '{note_id}' => $note_id])]);
|
2021-12-10 11:45:31 +09:00
|
|
|
} else {
|
|
|
|
throw new ClientException(_m('Note wasn\'t repeated!'));
|
2021-10-28 04:39:34 +09:00
|
|
|
}
|
|
|
|
|
2021-11-17 04:39:03 +09:00
|
|
|
// Redirect user to where they came from
|
|
|
|
// Prevent open redirect
|
2021-12-16 20:14:34 +09:00
|
|
|
if (!\is_null($from = $this->string('from'))) {
|
2021-11-26 08:08:30 +09:00
|
|
|
if (Router::isAbsolute($from)) {
|
|
|
|
Log::warning("Actor {$actor_id} attempted to reply to a note and then get redirected to another host, or the URL was invalid ({$from})");
|
2021-11-17 04:39:03 +09:00
|
|
|
throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
|
|
|
|
}
|
2022-01-27 09:54:27 +09:00
|
|
|
// TODO anchor on element id
|
|
|
|
throw new RedirectException(url: $from);
|
2021-10-28 04:39:34 +09:00
|
|
|
}
|
2022-01-27 09:54:27 +09:00
|
|
|
|
|
|
|
// If we don't have a URL to return to, go to the instance root
|
|
|
|
throw new RedirectException('root');
|
2021-10-28 04:39:34 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2021-12-16 20:14:34 +09:00
|
|
|
'_template' => 'repeat/remove_from_repeats.html.twig',
|
2021-12-27 03:40:54 +09:00
|
|
|
'note' => Note::getById($note_id),
|
2021-11-17 04:39:03 +09:00
|
|
|
'remove_repeat' => $form_remove_repeat->createView(),
|
2021-10-28 04:39:34 +09:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|