[PLUGIN][ActivityPub] Fix typo in getObjectByUri

This commit is contained in:
2021-12-24 01:58:41 +00:00
parent 7407028891
commit e3efd25b43
3 changed files with 97 additions and 138 deletions

View File

@@ -25,6 +25,7 @@ namespace Plugin\Favourite;
use App\Core\DB\DB;
use App\Core\Event;
use function App\Core\I18n\_m;
use App\Core\Modules\NoteHandlerPlugin;
use App\Core\Router\RouteLoader;
use App\Core\Router\Router;
@@ -34,28 +35,25 @@ use App\Entity\Feed;
use App\Entity\LocalUser;
use App\Entity\Note;
use App\Util\Common;
use App\Util\Exception\InvalidFormException;
use App\Util\Exception\NoSuchNoteException;
use App\Util\Exception\RedirectException;
use App\Util\Nickname;
use DateTime;
use Plugin\Favourite\Entity\Favourite as FavouriteEntity;
use Symfony\Component\HttpFoundation\Request;
use function App\Core\I18n\_m;
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];
$opts = ['note_id' => $note_id, 'actor_id' => $actor_id];
$note_already_favoured = DB::find('favourite', $opts);
if (is_null($note_already_favoured)) {
if (\is_null($note_already_favoured)) {
DB::persist(FavouriteEntity::create($opts));
$act = Activity::create([
'actor_id' => $actor_id,
'verb' => 'favourite',
'actor_id' => $actor_id,
'verb' => 'favourite',
'object_type' => 'note',
'object_id' => $note_id,
'source' => $source,
'object_id' => $note_id,
'source' => $source,
]);
DB::persist($act);
@@ -67,15 +65,15 @@ class Favourite extends NoteHandlerPlugin
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)) {
if (!\is_null($note_already_favoured)) {
DB::remove($note_already_favoured);
$favourite_activity = DB::findBy('activity', ['verb' => 'favourite', 'object_type' => 'note', 'object_id' => $note_id], order_by: ['created' => 'DESC'])[0];
$act = Activity::create([
'actor_id' => $actor_id,
'verb' => 'undo', // 'undo_favourite',
$act = Activity::create([
'actor_id' => $actor_id,
'verb' => 'undo', // 'undo_favourite',
'object_type' => 'activity', // 'note',
'object_id' => $favourite_activity->getId(), // $note_id,
'source' => $source,
'object_id' => $favourite_activity->getId(), // $note_id,
'source' => $source,
]);
DB::persist($act);
@@ -88,38 +86,35 @@ class Favourite extends NoteHandlerPlugin
* HTML rendering event that adds the favourite form as a note
* action, if a user is logged in
*
* @param Request $request
* @param Note $note
* @param array $actions
* @return bool Event hook
*/
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;
}
// If note is favourite, "is_favourite" is 1
$opts = ['note_id' => $note->getId(), 'actor_id' => $user->getId()];
$opts = ['note_id' => $note->getId(), 'actor_id' => $user->getId()];
$is_favourite = DB::find('favourite', $opts) !== null;
// Generating URL for favourite action route
$args = ['id' => $note->getId()];
$type = Router::ABSOLUTE_PATH;
$args = ['id' => $note->getId()];
$type = Router::ABSOLUTE_PATH;
$favourite_action_url = $is_favourite
? Router::url('favourite_remove', $args, $type)
: Router::url('favourite_add', $args, $type);
$query_string = $request->getQueryString();
// 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 = [
'url' => $favourite_action_url,
'title' => $is_favourite ? 'Remove this note from favourites' : 'Favourite this note!',
'url' => $favourite_action_url,
'title' => $is_favourite ? 'Remove this note from favourites' : 'Favourite this note!',
'classes' => "button-container favourite-button-container {$extra_classes}",
'id' => 'favourite-button-container-' . $note->getId(),
'id' => 'favourite-button-container-' . $note->getId(),
];
$actions[] = $favourite_action;
@@ -130,7 +125,7 @@ class Favourite extends NoteHandlerPlugin
{
// if note is the original, append on end "user favoured this"
$actor = $vars['actor'];
$note = $vars['note'];
$note = $vars['note'];
return Event::next;
}
@@ -155,16 +150,16 @@ class Favourite extends NoteHandlerPlugin
{
DB::persist(Feed::create([
'actor_id' => $actor_id,
'url' => Router::url($route = 'favourites_view_by_nickname', ['nickname' => $user->getNickname()]),
'route' => $route,
'title' => _m('Favourites'),
'url' => Router::url($route = 'favourites_view_by_nickname', ['nickname' => $user->getNickname()]),
'route' => $route,
'title' => _m('Favourites'),
'ordering' => $ordering++,
]));
DB::persist(Feed::create([
'actor_id' => $actor_id,
'url' => Router::url($route = 'favourites_reverse_view_by_nickname', ['nickname' => $user->getNickname()]),
'route' => $route,
'title' => _m('Reverse favourites'),
'url' => Router::url($route = 'favourites_reverse_view_by_nickname', ['nickname' => $user->getNickname()]),
'route' => $route,
'title' => _m('Reverse favourites'),
'ordering' => $ordering++,
]));
return Event::next;
@@ -174,7 +169,7 @@ class Favourite extends NoteHandlerPlugin
private function activitypub_handler(Actor $actor, \ActivityPhp\Type\AbstractObject $type_activity, mixed $type_object, ?\Plugin\ActivityPub\Entity\ActivitypubActivity &$ap_act): bool
{
if (!in_array($type_activity->get('type'), ['Like', 'Undo'])) {
if (!\in_array($type_activity->get('type'), ['Like', 'Undo'])) {
return Event::next;
}
if ($type_activity->get('type') === 'Like') { // Favourite
@@ -184,7 +179,7 @@ class Favourite extends NoteHandlerPlugin
} else {
return Event::next;
}
} else if ($type_object instanceof Note) {
} elseif ($type_object instanceof Note) {
$note_id = $type_object->getId();
} else {
return Event::next;
@@ -192,13 +187,13 @@ class Favourite extends NoteHandlerPlugin
} else { // Undo Favourite
if ($type_object instanceof \ActivityPhp\Type\AbstractObject) {
$ap_prev_favourite_act = \Plugin\ActivityPub\Util\Model\Activity::fromJson($type_object);
$prev_favourite_act = $ap_prev_favourite_act->getActivity();
$prev_favourite_act = $ap_prev_favourite_act->getActivity();
if ($prev_favourite_act->getVerb() === 'favourite' && $prev_favourite_act->getObjectType() === 'note') {
$note_id = $prev_favourite_act->getObjectId();
} else {
return Event::next;
}
} else if ($type_object instanceof Activity) {
} elseif ($type_object instanceof Activity) {
if ($type_object->getVerb() === 'favourite' && $type_object->getObjectType() === 'note') {
$note_id = $type_object->getObjectId();
} else {
@@ -214,14 +209,16 @@ class Favourite extends NoteHandlerPlugin
} else {
$act = self::unfavourNote($note_id, $actor->getId(), source: 'ActivityPub');
}
// Store ActivityPub Activity
$ap_act = \Plugin\ActivityPub\Entity\ActivitypubActivity::create([
'activity_id' => $act->getId(),
'activity_uri' => $type_activity->get('id'),
'created' => new \DateTime($type_activity->get('published') ?? 'now'),
'modified' => new \DateTime(),
]);
DB::persist($ap_act);
if (!\is_null($act)) {
// Store ActivityPub Activity
$ap_act = \Plugin\ActivityPub\Entity\ActivitypubActivity::create([
'activity_id' => $act->getId(),
'activity_uri' => $type_activity->get('id'),
'created' => new DateTime($type_activity->get('published') ?? 'now'),
'modified' => new DateTime(),
]);
DB::persist($ap_act);
}
return Event::stop;
}
@@ -234,13 +231,13 @@ class Favourite extends NoteHandlerPlugin
{
return $this->activitypub_handler($actor, $type_activity, $type_object, $ap_act);
}
public function onGSVerbToActivityStreamsTwoActivityType(string $verb, ?string &$gs_verb_to_activity_stream_two_verb): bool
{
if ($verb === 'favourite') {
$gs_verb_to_activity_stream_two_verb = 'Like';
return Event::stop;
}
return Event::next;
}
if ($verb === 'favourite') {
$gs_verb_to_activity_stream_two_verb = 'Like';
return Event::stop;
}
return Event::next;
}
}