forked from GNUsocial/gnu-social
[PLUGIN][Favourite] Log changes into Activity
This commit is contained in:
parent
480a42cca5
commit
5025901c86
@ -26,7 +26,6 @@ namespace Plugin\Favourite\Controller;
|
|||||||
use App\Core\Controller\FeedController;
|
use App\Core\Controller\FeedController;
|
||||||
use App\Core\DB\DB;
|
use App\Core\DB\DB;
|
||||||
use App\Core\Form;
|
use App\Core\Form;
|
||||||
use function App\Core\I18n\_m;
|
|
||||||
use App\Core\Log;
|
use App\Core\Log;
|
||||||
use App\Core\Router\Router;
|
use App\Core\Router\Router;
|
||||||
use App\Util\Common;
|
use App\Util\Common;
|
||||||
@ -35,14 +34,16 @@ use App\Util\Exception\InvalidFormException;
|
|||||||
use App\Util\Exception\NoLoggedInUser;
|
use App\Util\Exception\NoLoggedInUser;
|
||||||
use App\Util\Exception\NoSuchNoteException;
|
use App\Util\Exception\NoSuchNoteException;
|
||||||
use App\Util\Exception\RedirectException;
|
use App\Util\Exception\RedirectException;
|
||||||
use Plugin\Favourite\Entity\Favourite as FavouriteEntity;
|
use App\Util\Exception\ServerException;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use function App\Core\I18n\_m;
|
||||||
|
use function is_null;
|
||||||
|
|
||||||
class Favourite extends FeedController
|
class Favourite extends FeedController
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @throws \App\Util\Exception\ServerException
|
* @throws ServerException
|
||||||
* @throws InvalidFormException
|
* @throws InvalidFormException
|
||||||
* @throws NoLoggedInUser
|
* @throws NoLoggedInUser
|
||||||
* @throws NoSuchNoteException
|
* @throws NoSuchNoteException
|
||||||
@ -54,7 +55,7 @@ class Favourite extends FeedController
|
|||||||
$actor_id = $user->getId();
|
$actor_id = $user->getId();
|
||||||
$opts = ['id' => $id];
|
$opts = ['id' => $id];
|
||||||
$add_favourite_note = DB::find('note', $opts);
|
$add_favourite_note = DB::find('note', $opts);
|
||||||
if (\is_null($add_favourite_note)) {
|
if (is_null($add_favourite_note)) {
|
||||||
throw new NoSuchNoteException();
|
throw new NoSuchNoteException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,18 +73,12 @@ class Favourite extends FeedController
|
|||||||
$form_add_to_favourite->handleRequest($request);
|
$form_add_to_favourite->handleRequest($request);
|
||||||
|
|
||||||
if ($form_add_to_favourite->isSubmitted()) {
|
if ($form_add_to_favourite->isSubmitted()) {
|
||||||
$opts = ['note_id' => $id, 'actor_id' => $user->getId()];
|
\Plugin\Favourite\Favourite::favourNote(note_id: $id, actor_id: $actor_id);
|
||||||
$note_already_favourited = DB::find('favourite', $opts);
|
|
||||||
|
|
||||||
if (\is_null($note_already_favourited)) {
|
|
||||||
$opts = ['note_id' => $id, 'actor_id' => $user->getId()];
|
|
||||||
DB::persist(FavouriteEntity::create($opts));
|
|
||||||
DB::flush();
|
DB::flush();
|
||||||
}
|
|
||||||
|
|
||||||
// Redirect user to where they came from
|
// Redirect user to where they came from
|
||||||
// Prevent open redirect
|
// Prevent open redirect
|
||||||
if (!\is_null($from = $this->string('from'))) {
|
if (!is_null($from = $this->string('from'))) {
|
||||||
if (Router::isAbsolute($from)) {
|
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})");
|
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)
|
throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
|
||||||
@ -105,7 +100,7 @@ class Favourite extends FeedController
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws \App\Util\Exception\ServerException
|
* @throws ServerException
|
||||||
* @throws InvalidFormException
|
* @throws InvalidFormException
|
||||||
* @throws NoLoggedInUser
|
* @throws NoLoggedInUser
|
||||||
* @throws NoSuchNoteException
|
* @throws NoSuchNoteException
|
||||||
@ -115,9 +110,9 @@ class Favourite extends FeedController
|
|||||||
{
|
{
|
||||||
$user = Common::ensureLoggedIn();
|
$user = Common::ensureLoggedIn();
|
||||||
$actor_id = $user->getId();
|
$actor_id = $user->getId();
|
||||||
$opts = ['note_id' => $id, 'actor_id' => $user->getId()];
|
$opts = ['id' => $id];
|
||||||
$remove_favourite_note = DB::find('favourite', $opts);
|
$remove_favourite_note = DB::find('note', $opts);
|
||||||
if (\is_null($remove_favourite_note)) {
|
if (is_null($remove_favourite_note)) {
|
||||||
throw new NoSuchNoteException();
|
throw new NoSuchNoteException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,14 +129,12 @@ class Favourite extends FeedController
|
|||||||
|
|
||||||
$form_remove_favourite->handleRequest($request);
|
$form_remove_favourite->handleRequest($request);
|
||||||
if ($form_remove_favourite->isSubmitted()) {
|
if ($form_remove_favourite->isSubmitted()) {
|
||||||
if ($remove_favourite_note) {
|
\Plugin\Favourite\Favourite::unfavourNote(note_id: $id, actor_id: $actor_id);
|
||||||
DB::remove($remove_favourite_note);
|
|
||||||
DB::flush();
|
DB::flush();
|
||||||
}
|
|
||||||
|
|
||||||
// Redirect user to where they came from
|
// Redirect user to where they came from
|
||||||
// Prevent open redirect
|
// Prevent open redirect
|
||||||
if (!\is_null($from = $this->string('from'))) {
|
if (!is_null($from = $this->string('from'))) {
|
||||||
if (Router::isAbsolute($from)) {
|
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})");
|
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)
|
throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
|
||||||
@ -191,9 +184,9 @@ class Favourite extends FeedController
|
|||||||
/**
|
/**
|
||||||
* Reverse favourites stream
|
* Reverse favourites stream
|
||||||
*
|
*
|
||||||
|
* @return array template
|
||||||
* @throws NoLoggedInUser user not logged in
|
* @throws NoLoggedInUser user not logged in
|
||||||
*
|
*
|
||||||
* @return array template
|
|
||||||
*/
|
*/
|
||||||
public function reverseFavouritesByActorId(Request $request, int $id): array
|
public function reverseFavouritesByActorId(Request $request, int $id): array
|
||||||
{
|
{
|
||||||
|
@ -25,10 +25,10 @@ namespace Plugin\Favourite;
|
|||||||
|
|
||||||
use App\Core\DB\DB;
|
use App\Core\DB\DB;
|
||||||
use App\Core\Event;
|
use App\Core\Event;
|
||||||
use function App\Core\I18n\_m;
|
|
||||||
use App\Core\Modules\NoteHandlerPlugin;
|
use App\Core\Modules\NoteHandlerPlugin;
|
||||||
use App\Core\Router\RouteLoader;
|
use App\Core\Router\RouteLoader;
|
||||||
use App\Core\Router\Router;
|
use App\Core\Router\Router;
|
||||||
|
use App\Entity\Activity;
|
||||||
use App\Entity\Actor;
|
use App\Entity\Actor;
|
||||||
use App\Entity\Feed;
|
use App\Entity\Feed;
|
||||||
use App\Entity\LocalUser;
|
use App\Entity\LocalUser;
|
||||||
@ -38,23 +38,61 @@ use App\Util\Exception\InvalidFormException;
|
|||||||
use App\Util\Exception\NoSuchNoteException;
|
use App\Util\Exception\NoSuchNoteException;
|
||||||
use App\Util\Exception\RedirectException;
|
use App\Util\Exception\RedirectException;
|
||||||
use App\Util\Nickname;
|
use App\Util\Nickname;
|
||||||
|
use Plugin\Favourite\Entity\Favourite as FavouriteEntity;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use function App\Core\I18n\_m;
|
||||||
|
|
||||||
class Favourite extends NoteHandlerPlugin
|
class Favourite extends NoteHandlerPlugin
|
||||||
{
|
{
|
||||||
|
public static function favourNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity
|
||||||
|
{
|
||||||
|
$opts = ['note_id' => $note_id, 'actor_id' => $actor_id];
|
||||||
|
$note_already_favoured = DB::find('favourite', $opts);
|
||||||
|
if (is_null($note_already_favoured)) {
|
||||||
|
DB::persist(FavouriteEntity::create($opts));
|
||||||
|
$act = Activity::create([
|
||||||
|
'actor_id' => $actor_id,
|
||||||
|
'verb' => 'favourite',
|
||||||
|
'object_type' => 'note',
|
||||||
|
'object_id' => $note_id,
|
||||||
|
'source' => $source,
|
||||||
|
]);
|
||||||
|
DB::persist($act);
|
||||||
|
}
|
||||||
|
return $act ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function unfavourNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity
|
||||||
|
{
|
||||||
|
$note_already_favoured = DB::find('favourite', ['note_id' => $note_id, 'actor_id' => $actor_id]);
|
||||||
|
if (!is_null($note_already_favoured)) {
|
||||||
|
DB::remove($note_already_favoured);
|
||||||
|
$favourite_activity = DB::findOneBy('activity', ['verb' => 'favourite', 'object_type' => 'note', 'object_id' => $note_id]);
|
||||||
|
$act = Activity::create([
|
||||||
|
'actor_id' => $actor_id,
|
||||||
|
'verb' => 'undo', // 'undo_favourite',
|
||||||
|
'object_type' => 'activity', // 'note',
|
||||||
|
'object_id' => $favourite_activity->getId(), // $note_id,
|
||||||
|
'source' => $source,
|
||||||
|
]);
|
||||||
|
DB::persist($act);
|
||||||
|
}
|
||||||
|
return $act ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HTML rendering event that adds the favourite form as a note
|
* HTML rendering event that adds the favourite form as a note
|
||||||
* action, if a user is logged in
|
* action, if a user is logged in
|
||||||
*
|
*
|
||||||
* @throws InvalidFormException
|
* @return bool Event hook
|
||||||
* @throws NoSuchNoteException
|
* @throws NoSuchNoteException
|
||||||
* @throws RedirectException
|
* @throws RedirectException
|
||||||
*
|
*
|
||||||
* @return bool Event hook
|
* @throws InvalidFormException
|
||||||
*/
|
*/
|
||||||
public function onAddNoteActions(Request $request, Note $note, array &$actions): bool
|
public function onAddNoteActions(Request $request, Note $note, array &$actions): bool
|
||||||
{
|
{
|
||||||
if (\is_null($user = Common::user())) {
|
if (is_null($user = Common::user())) {
|
||||||
return Event::next;
|
return Event::next;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +109,7 @@ class Favourite extends NoteHandlerPlugin
|
|||||||
|
|
||||||
$query_string = $request->getQueryString();
|
$query_string = $request->getQueryString();
|
||||||
// Concatenating get parameter to redirect the user to where he came from
|
// Concatenating get parameter to redirect the user to where he came from
|
||||||
$favourite_action_url .= !\is_null($query_string) ? '?from=' . mb_substr($query_string, 2) : '';
|
$favourite_action_url .= !is_null($query_string) ? '?from=' . mb_substr($query_string, 2) : '';
|
||||||
|
|
||||||
$extra_classes = $is_favourite ? 'note-actions-set' : 'note-actions-unset';
|
$extra_classes = $is_favourite ? 'note-actions-set' : 'note-actions-unset';
|
||||||
$favourite_action = [
|
$favourite_action = [
|
||||||
@ -87,7 +125,7 @@ class Favourite extends NoteHandlerPlugin
|
|||||||
|
|
||||||
public function onAppendCardNote(array $vars, array &$result)
|
public function onAppendCardNote(array $vars, array &$result)
|
||||||
{
|
{
|
||||||
// if note is the original, append on end "user favourited this"
|
// if note is the original, append on end "user favoured this"
|
||||||
$actor = $vars['actor'];
|
$actor = $vars['actor'];
|
||||||
$note = $vars['note'];
|
$note = $vars['note'];
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user