2021-04-14 20:59:37 +01:00
|
|
|
<?php
|
|
|
|
|
2021-11-25 23:08:30 +00:00
|
|
|
declare(strict_types = 1);
|
2021-10-10 09:26:18 +01:00
|
|
|
|
2021-04-14 20:59:37 +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\Favourite\Controller;
|
|
|
|
|
|
|
|
use App\Core\DB\DB;
|
2022-02-18 17:48:06 +00:00
|
|
|
use App\Core\Event;
|
2021-10-21 20:14:22 +01:00
|
|
|
use App\Core\Form;
|
2021-12-23 13:27:31 +00:00
|
|
|
use function App\Core\I18n\_m;
|
2021-11-25 23:08:30 +00:00
|
|
|
use App\Core\Log;
|
2021-10-24 15:12:18 +01:00
|
|
|
use App\Core\Router\Router;
|
2022-03-28 20:52:12 +01:00
|
|
|
use App\Entity\Activity;
|
2022-02-26 14:45:38 +00:00
|
|
|
use App\Entity\Actor;
|
2022-03-13 18:23:19 +00:00
|
|
|
use App\Entity\LocalUser;
|
|
|
|
use App\Entity\Note;
|
2021-10-21 20:14:22 +01:00
|
|
|
use App\Util\Common;
|
2021-11-25 23:08:30 +00:00
|
|
|
use App\Util\Exception\ClientException;
|
2021-10-21 20:14:22 +01:00
|
|
|
use App\Util\Exception\InvalidFormException;
|
|
|
|
use App\Util\Exception\NoLoggedInUser;
|
|
|
|
use App\Util\Exception\NoSuchNoteException;
|
|
|
|
use App\Util\Exception\RedirectException;
|
2021-12-08 22:28:35 +00:00
|
|
|
use App\Util\Exception\ServerException;
|
2022-01-02 20:04:52 +00:00
|
|
|
use Component\Collection\Util\Controller\FeedController;
|
2022-03-28 20:52:12 +01:00
|
|
|
use Component\Notification\Entity\Attention;
|
2021-10-21 20:14:22 +01:00
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
2021-04-14 20:59:37 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
2021-12-07 23:31:20 +00:00
|
|
|
class Favourite extends FeedController
|
2021-04-14 20:59:37 +01:00
|
|
|
{
|
2021-10-21 20:14:22 +01:00
|
|
|
/**
|
2021-11-25 23:08:30 +00:00
|
|
|
* @throws InvalidFormException
|
2021-10-26 17:29:14 +01:00
|
|
|
* @throws NoLoggedInUser
|
2021-11-25 23:08:30 +00:00
|
|
|
* @throws NoSuchNoteException
|
|
|
|
* @throws RedirectException
|
2021-12-23 13:27:31 +00:00
|
|
|
* @throws ServerException
|
2021-10-21 20:14:22 +01:00
|
|
|
*/
|
2021-10-26 17:29:14 +01:00
|
|
|
public function favouriteAddNote(Request $request, int $id): bool|array
|
2021-10-21 20:14:22 +01:00
|
|
|
{
|
2021-12-23 13:27:31 +00:00
|
|
|
$user = Common::ensureLoggedIn();
|
|
|
|
$actor_id = $user->getId();
|
|
|
|
$opts = ['id' => $id];
|
2022-03-13 18:23:19 +00:00
|
|
|
$add_favourite_note = DB::findOneBy(Note::class, $opts);
|
2021-12-23 13:27:31 +00:00
|
|
|
if (\is_null($add_favourite_note)) {
|
2021-10-21 20:14:22 +01:00
|
|
|
throw new NoSuchNoteException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$form_add_to_favourite = Form::create([
|
|
|
|
['add_favourite', SubmitType::class,
|
|
|
|
[
|
|
|
|
'label' => _m('Favourite note!'),
|
2021-12-23 13:27:31 +00:00
|
|
|
'attr' => [
|
2021-11-25 23:08:30 +00:00
|
|
|
'title' => _m('Favourite this note!'),
|
2021-10-21 20:14:22 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$form_add_to_favourite->handleRequest($request);
|
|
|
|
|
|
|
|
if ($form_add_to_favourite->isSubmitted()) {
|
2022-02-18 17:48:06 +00:00
|
|
|
if (!\is_null($activity = \Plugin\Favourite\Favourite::favourNote(note_id: $id, actor_id: $actor_id))) {
|
2022-03-28 20:52:12 +01:00
|
|
|
$actor = Actor::getById($actor_id);
|
|
|
|
foreach ($actor->getSubscribers() as $subscriber) {
|
|
|
|
$target_id = $subscriber->getId();
|
|
|
|
DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $activity->getId(), 'target_id' => $target_id]));
|
|
|
|
$effective_attentions[$target_id] = $subscriber;
|
|
|
|
}
|
2021-12-10 02:42:36 +00:00
|
|
|
DB::flush();
|
2022-03-28 20:52:12 +01:00
|
|
|
Event::handle('NewNotification', [$actor, $activity, $activity->getAttentionTargets(), _m('{actor_id} favoured note {note_id}.', ['{actor_id}' => $actor->getId(), '{note_id}' => $activity->getObjectId()])]);
|
2021-12-10 02:42:36 +00:00
|
|
|
} else {
|
|
|
|
throw new ClientException(_m('Note already favoured!'));
|
|
|
|
}
|
2021-10-24 15:12:18 +01:00
|
|
|
|
2021-11-25 23:08:30 +00:00
|
|
|
// Redirect user to where they came from
|
|
|
|
// Prevent open redirect
|
2021-12-23 13:27:31 +00:00
|
|
|
if (!\is_null($from = $this->string('from'))) {
|
2021-11-25 23:08:30 +00:00
|
|
|
if (Router::isAbsolute($from)) {
|
2022-01-13 17:47:47 +00:00
|
|
|
Log::warning("Actor {$actor_id} attempted to favourite a note and then get redirected to another host, or the URL was invalid ({$from})");
|
2021-11-25 23:08:30 +00:00
|
|
|
throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
|
|
|
|
} else {
|
|
|
|
// TODO anchor on element id
|
2021-12-26 05:59:22 +00:00
|
|
|
throw new RedirectException(url: $from);
|
2021-11-25 23:08:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If we don't have a URL to return to, go to the instance root
|
|
|
|
throw new RedirectException('root');
|
2021-10-24 15:12:18 +01:00
|
|
|
}
|
2021-10-21 20:14:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2021-12-23 13:27:31 +00:00
|
|
|
'_template' => 'favourite/add_to_favourites.html.twig',
|
|
|
|
'note' => $add_favourite_note,
|
2021-11-25 23:08:30 +00:00
|
|
|
'add_favourite' => $form_add_to_favourite->createView(),
|
2021-10-21 20:14:22 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-25 23:08:30 +00:00
|
|
|
* @throws InvalidFormException
|
2021-10-21 20:14:22 +01:00
|
|
|
* @throws NoLoggedInUser
|
2021-11-25 23:08:30 +00:00
|
|
|
* @throws NoSuchNoteException
|
|
|
|
* @throws RedirectException
|
2021-12-23 13:27:31 +00:00
|
|
|
* @throws ServerException
|
2021-10-21 20:14:22 +01:00
|
|
|
*/
|
2021-10-26 17:29:14 +01:00
|
|
|
public function favouriteRemoveNote(Request $request, int $id): array
|
2021-10-21 20:14:22 +01:00
|
|
|
{
|
2021-12-23 13:27:31 +00:00
|
|
|
$user = Common::ensureLoggedIn();
|
|
|
|
$actor_id = $user->getId();
|
|
|
|
$opts = ['id' => $id];
|
2022-03-13 18:23:19 +00:00
|
|
|
$remove_favourite_note = DB::findOneBy(Note::class, $opts);
|
2021-12-23 13:27:31 +00:00
|
|
|
if (\is_null($remove_favourite_note)) {
|
2021-10-21 20:14:22 +01:00
|
|
|
throw new NoSuchNoteException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$form_remove_favourite = Form::create([
|
|
|
|
['remove_favourite', SubmitType::class,
|
|
|
|
[
|
|
|
|
'label' => _m('Remove favourite'),
|
2021-12-23 13:27:31 +00:00
|
|
|
'attr' => [
|
2021-11-25 23:08:30 +00:00
|
|
|
'title' => _m('Remove note from favourites.'),
|
2021-10-21 20:14:22 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$form_remove_favourite->handleRequest($request);
|
|
|
|
if ($form_remove_favourite->isSubmitted()) {
|
2022-02-18 17:48:06 +00:00
|
|
|
if (!\is_null($activity = \Plugin\Favourite\Favourite::unfavourNote(note_id: $id, actor_id: $actor_id))) {
|
2022-03-28 20:52:12 +01:00
|
|
|
$actor = Actor::getById($actor_id);
|
|
|
|
foreach ($actor->getSubscribers() as $subscriber) {
|
|
|
|
$target_id = $subscriber->getId();
|
|
|
|
DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $activity->getId(), 'target_id' => $target_id]));
|
|
|
|
$effective_attentions[$target_id] = $subscriber;
|
|
|
|
}
|
2021-12-10 02:42:36 +00:00
|
|
|
DB::flush();
|
2022-03-28 20:52:12 +01:00
|
|
|
Event::handle('NewNotification', [$actor, $activity, $activity->getAttentionTargets(), _m('{actor_id} unfavoured note {note_id}.', ['{actor_id}' => $actor->getId(), '{note_id}' => $activity->getObjectId()])]);
|
2021-12-10 02:42:36 +00:00
|
|
|
} else {
|
|
|
|
throw new ClientException(_m('Note already unfavoured!'));
|
|
|
|
}
|
2021-10-24 15:12:18 +01:00
|
|
|
|
2021-11-25 23:08:30 +00:00
|
|
|
// Redirect user to where they came from
|
|
|
|
// Prevent open redirect
|
2021-12-23 13:27:31 +00:00
|
|
|
if (!\is_null($from = $this->string('from'))) {
|
2021-11-25 23:08:30 +00: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})");
|
|
|
|
throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
|
|
|
|
} else {
|
|
|
|
// TODO anchor on element id
|
2021-12-26 05:59:22 +00:00
|
|
|
throw new RedirectException(url: $from);
|
2021-11-25 23:08:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If we don't have a URL to return to, go to the instance root
|
|
|
|
throw new RedirectException('root');
|
2021-10-24 15:12:18 +01:00
|
|
|
}
|
2021-10-21 20:14:22 +01:00
|
|
|
}
|
|
|
|
|
2022-03-13 18:23:19 +00:00
|
|
|
$note = DB::findOneBy(Note::class, ['id' => $id]);
|
2021-10-21 20:14:22 +01:00
|
|
|
return [
|
2021-12-23 13:27:31 +00:00
|
|
|
'_template' => 'favourite/remove_from_favourites.html.twig',
|
|
|
|
'note' => $note,
|
2021-11-25 23:08:30 +00:00
|
|
|
'remove_favourite' => $form_remove_favourite->createView(),
|
2021-10-21 20:14:22 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-01-04 22:36:16 +00:00
|
|
|
public function favouritesViewByActorId(Request $request, int $id)
|
2021-04-14 20:59:37 +01:00
|
|
|
{
|
2021-09-16 17:04:05 +01:00
|
|
|
$notes = DB::dql(
|
2021-12-07 23:31:20 +00:00
|
|
|
<<< 'EOF'
|
|
|
|
select n from note n
|
2022-01-04 22:36:16 +00:00
|
|
|
join note_favourite f with n.id = f.note_id
|
2021-12-07 23:31:20 +00:00
|
|
|
where f.actor_id = :id
|
|
|
|
order by f.created DESC
|
|
|
|
EOF,
|
2021-10-10 09:26:18 +01:00
|
|
|
['id' => $id],
|
2021-09-16 17:04:05 +01:00
|
|
|
);
|
2021-04-14 20:59:37 +01:00
|
|
|
|
2021-12-08 10:20:37 +00:00
|
|
|
return [
|
2022-01-02 20:04:52 +00:00
|
|
|
'_template' => 'collection/notes.html.twig',
|
2022-01-22 19:16:35 +00:00
|
|
|
'page_title' => 'Favourites',
|
2021-12-07 23:31:20 +00:00
|
|
|
'notes' => $notes,
|
2021-12-08 10:20:37 +00:00
|
|
|
];
|
2021-04-14 20:59:37 +01:00
|
|
|
}
|
2021-10-21 20:14:22 +01:00
|
|
|
|
2022-01-04 22:36:16 +00:00
|
|
|
public function favouritesViewByActorNickname(Request $request, string $nickname)
|
2021-09-16 17:04:05 +01:00
|
|
|
{
|
2022-03-13 18:23:19 +00:00
|
|
|
$user = DB::findOneBy(LocalUser::class, ['nickname' => $nickname]);
|
2022-01-04 22:36:16 +00:00
|
|
|
return self::favouritesViewByActorId($request, $user->getId());
|
2021-09-16 17:04:05 +01:00
|
|
|
}
|
2021-04-14 20:59:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverse favourites stream
|
|
|
|
*
|
2021-10-21 20:14:22 +01:00
|
|
|
* @throws NoLoggedInUser user not logged in
|
|
|
|
*
|
2021-12-23 13:27:31 +00:00
|
|
|
* @return array template
|
2021-04-14 20:59:37 +01:00
|
|
|
*/
|
2022-01-04 22:36:16 +00:00
|
|
|
public function reverseFavouritesViewByActorId(Request $request, int $id): array
|
2021-04-14 20:59:37 +01:00
|
|
|
{
|
2021-10-10 09:26:18 +01:00
|
|
|
$notes = DB::dql(
|
2021-12-07 23:31:20 +00:00
|
|
|
<<< 'EOF'
|
|
|
|
select n from note n
|
2022-01-04 22:36:16 +00:00
|
|
|
join note_favourite f with n.id = f.note_id
|
2021-12-07 23:31:20 +00:00
|
|
|
where f.actor_id != :id
|
|
|
|
and n.actor_id = :id
|
|
|
|
order by f.created DESC
|
|
|
|
EOF,
|
2021-10-10 09:26:18 +01:00
|
|
|
['id' => $id],
|
2021-09-16 17:04:05 +01:00
|
|
|
);
|
2021-04-14 20:59:37 +01:00
|
|
|
|
2021-12-08 10:20:37 +00:00
|
|
|
return [
|
2022-01-02 20:04:52 +00:00
|
|
|
'_template' => 'collection/notes.html.twig',
|
2021-11-26 13:05:23 +00:00
|
|
|
'page_title' => 'Reverse favourites feed.',
|
2021-12-07 23:31:20 +00:00
|
|
|
'notes' => $notes,
|
2021-12-08 10:20:37 +00:00
|
|
|
];
|
2021-04-14 20:59:37 +01:00
|
|
|
}
|
2021-10-21 20:14:22 +01:00
|
|
|
|
2022-01-04 22:36:16 +00:00
|
|
|
public function reverseFavouritesViewByActorNickname(Request $request, string $nickname)
|
2021-09-16 17:04:05 +01:00
|
|
|
{
|
2022-03-13 18:23:19 +00:00
|
|
|
$user = DB::findOneBy(LocalUser::class, ['nickname' => $nickname]);
|
2022-01-04 22:36:16 +00:00
|
|
|
return self::reverseFavouritesViewByActorId($request, $user->getId());
|
2021-09-16 17:04:05 +01:00
|
|
|
}
|
2021-04-14 20:59:37 +01:00
|
|
|
}
|