From a81ac673ac6d97ae144bafb442c2227668997204 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Thu, 16 Dec 2021 11:14:34 +0000 Subject: [PATCH] [CORE][ENTITY] Rename 'Entity::getWithPK' to 'Entity::getByPK' --- components/Conversation/Controller/Reply.php | 28 ++-- components/Conversation/Conversation.php | 16 +-- .../Entity/FreeNetworkActorProtocol.php | 6 +- components/FreeNetwork/FreeNetwork.php | 12 +- plugins/ActivityPub/ActivityPub.php | 10 +- plugins/ActivityPub/Entity/ActivitypubRsa.php | 2 +- plugins/ActivityPub/Util/Explorer.php | 4 +- plugins/ActivityPub/Util/Model/Activity.php | 2 +- plugins/ActivityPub/Util/Model/Note.php | 2 +- plugins/Poll/Controller/AnswerPoll.php | 2 +- plugins/RepeatNote/Controller/Repeat.php | 29 ++--- plugins/RepeatNote/RepeatNote.php | 120 +++++++++--------- src/Core/Entity.php | 13 +- src/Entity/Note.php | 23 ++-- src/Entity/ResetPasswordRequest.php | 23 ++-- tests/Core/EntityTest.php | 8 +- 16 files changed, 143 insertions(+), 157 deletions(-) diff --git a/components/Conversation/Controller/Reply.php b/components/Conversation/Controller/Reply.php index b38fb9bcc5..7d03aa0ba8 100644 --- a/components/Conversation/Controller/Reply.php +++ b/components/Conversation/Controller/Reply.php @@ -29,9 +29,6 @@ namespace Component\Conversation\Controller; use App\Core\Controller\FeedController; use App\Core\DB\DB; use App\Core\Form; -use App\Util\Exception\DuplicateFoundException; -use App\Util\Exception\NoLoggedInUser; -use App\Util\Exception\ServerException; use function App\Core\I18n\_m; use App\Core\Log; use App\Core\Router\Router; @@ -39,10 +36,13 @@ use App\Entity\Actor; use App\Entity\Note; use App\Util\Common; use App\Util\Exception\ClientException; +use App\Util\Exception\DuplicateFoundException; use App\Util\Exception\InvalidFormException; +use App\Util\Exception\NoLoggedInUser; use App\Util\Exception\NoSuchNoteException; use App\Util\Exception\NotImplementedException; use App\Util\Exception\RedirectException; +use App\Util\Exception\ServerException; use App\Util\Form\FormFields; use Component\Posting\Posting; use Symfony\Component\Form\Extension\Core\Type\FileType; @@ -52,27 +52,25 @@ use Symfony\Component\HttpFoundation\Request; class Reply extends FeedController { - /** * Controller for the note reply non-JS page * - * @param Request $request - * @param int $id - * @return array * @throws ClientException + * @throws DuplicateFoundException * @throws InvalidFormException + * @throws NoLoggedInUser * @throws NoSuchNoteException * @throws RedirectException - * @throws DuplicateFoundException - * @throws NoLoggedInUser * @throws ServerException + * + * @return array */ public function replyAddNote(Request $request, int $id) { $user = Common::ensureLoggedIn(); $actor_id = $user->getId(); - $note = Note::getWithPK($id); + $note = Note::getByPK($id); if (\is_null($note) || !$note->isVisibleTo($user)) { throw new NoSuchNoteException(); } @@ -80,9 +78,11 @@ class Reply extends FeedController // TODO shouldn't this be the posting form? $form = Form::create([ ['content', TextareaType::class, ['label' => _m('Reply'), 'label_attr' => ['class' => 'section-form-label'], 'help' => _m('Please input your reply.')]], - FormFields::language($user->getActor(), + FormFields::language( + $user->getActor(), context_actor: $note->getActor(), - label: _m('Note language')), + label: _m('Note language'), + ), ['attachments', FileType::class, ['label' => ' ', 'multiple' => true, 'required' => false]], ['replyform', SubmitType::class, ['label' => _m('Submit')]], ]); @@ -94,7 +94,7 @@ class Reply extends FeedController if ($form->isValid()) { // Create a new note with the same content as the original $reply = Posting::storeLocalNote( - actor: Actor::getWithPK($actor_id), + actor: Actor::getByPK($actor_id), content: $data['content'], content_type: 'text/plain', // TODO language: $data['language'], @@ -108,7 +108,7 @@ class Reply extends FeedController // Find the id of the note we just created $reply_id = $reply->getId(); $parent_id = $note->getId(); - $resulting_note = Note::getWithPK($reply_id); + $resulting_note = Note::getByPK($reply_id); $resulting_note->setReplyTo($parent_id); // Update DB one last time diff --git a/components/Conversation/Conversation.php b/components/Conversation/Conversation.php index aa8d9ab519..f161982c86 100644 --- a/components/Conversation/Conversation.php +++ b/components/Conversation/Conversation.php @@ -25,9 +25,9 @@ namespace Component\Conversation; use App\Core\DB\DB; use App\Core\Event; +use function App\Core\I18n\_m; use App\Core\Modules\Component; use App\Core\Router\RouteLoader; -use function App\Core\I18n\_m; use App\Core\Router\Router; use App\Entity\Actor; use App\Entity\Feed; @@ -42,15 +42,9 @@ use Symfony\Component\HttpFoundation\Request; class Conversation extends Component { - /** * HTML rendering event that adds the repeat form as a note * action, if a user is logged in - * - * @param Request $request - * @param Note $note - * @param array $actions - * @return bool */ public function onAddNoteActions(Request $request, Note $note, array &$actions): bool { @@ -78,13 +72,8 @@ class Conversation extends Component return Event::next; } - /** * Append on note information about user actions - * - * @param array $vars - * @param array $result - * @return bool */ public function onAppendCardNote(array $vars, array &$result): bool { @@ -99,7 +88,7 @@ class Conversation extends Component // Get actors who replied foreach ($note_replies as $reply) { - $reply_actor[] = Actor::getWithPK($reply->getActorId()); + $reply_actor[] = Actor::getByPK($reply->getActorId()); } if (\count($reply_actor) < 1) { return Event::next; @@ -137,7 +126,6 @@ class Conversation extends Component } /** - * @param RouteLoader $r * @return bool */ public function onAddRoute(RouteLoader $r) diff --git a/components/FreeNetwork/Entity/FreeNetworkActorProtocol.php b/components/FreeNetwork/Entity/FreeNetworkActorProtocol.php index 948d7febca..4980012438 100644 --- a/components/FreeNetwork/Entity/FreeNetworkActorProtocol.php +++ b/components/FreeNetwork/Entity/FreeNetworkActorProtocol.php @@ -120,7 +120,7 @@ class FreeNetworkActorProtocol extends Entity public static function protocolSucceeded(string $protocol, int|Actor $actor_id, string $addr): void { $actor_id = is_int($actor_id) ? $actor_id : $actor_id->getId(); - $attributed_protocol = self::getWithPK(['actor_id' => $actor_id]); + $attributed_protocol = self::getByPK(['actor_id' => $actor_id]); if (is_null($attributed_protocol)) { $attributed_protocol = self::create([ 'actor_id' => $actor_id, @@ -136,7 +136,7 @@ class FreeNetworkActorProtocol extends Entity public static function canIActor(string $protocol, int|Actor $actor_id): bool { $actor_id = is_int($actor_id) ? $actor_id : $actor_id->getId(); - $attributed_protocol = self::getWithPK(['actor_id' => $actor_id])?->getProtocol(); + $attributed_protocol = self::getByPK(['actor_id' => $actor_id])?->getProtocol(); if (is_null($attributed_protocol)) { // If it is not attributed, you can go ahead. return true; @@ -150,7 +150,7 @@ class FreeNetworkActorProtocol extends Entity { // Normalize $addr, i.e. add 'acct:' if missing $addr = Discovery::normalize($target); - $attributed_protocol = self::getWithPK(['addr' => $addr])?->getProtocol(); + $attributed_protocol = self::getByPK(['addr' => $addr])?->getProtocol(); if (is_null($attributed_protocol)) { // If it is not attributed, you can go ahead. return true; diff --git a/components/FreeNetwork/FreeNetwork.php b/components/FreeNetwork/FreeNetwork.php index c6a9867c1a..6ee07cbac6 100644 --- a/components/FreeNetwork/FreeNetwork.php +++ b/components/FreeNetwork/FreeNetwork.php @@ -147,7 +147,7 @@ class FreeNetwork extends Component } $nick = Nickname::normalize(nickname: $nick, check_already_used: false, check_is_allowed: false); - $freenetwork_actor = LocalUser::getWithPK(['nickname' => $nick]); + $freenetwork_actor = LocalUser::getByPK(['nickname' => $nick]); if (!($freenetwork_actor instanceof LocalUser)) { throw new NoSuchActorException($nick); } @@ -166,7 +166,7 @@ class FreeNetwork extends Component // actor_view_id $reuri = '/\/actor\/(\d+)\/?/m'; if (preg_match_all($renick, $str, $matches, PREG_SET_ORDER, 0) === 1) { - $profile = LocalUser::getWithPK(['nickname' => $matches[0][1]])->getActor(); + $profile = LocalUser::getByPK(['nickname' => $matches[0][1]])->getActor(); } elseif (preg_match_all($reuri, $str, $matches, PREG_SET_ORDER, 0) === 1) { $profile = Actor::getById((int) $matches[0][1]); } @@ -197,9 +197,9 @@ class FreeNetwork extends Component return Event::stop; // We got our target, stop handler execution } - $APNote = ActivitypubActivity::getWithPK(['object_uri' => $resource]); + $APNote = ActivitypubActivity::getByPK(['object_uri' => $resource]); if ($APNote instanceof ActivitypubActivity) { - $target = new WebfingerResourceNote(Note::getWithPK(['id' => $APNote->getObjectId()])); + $target = new WebfingerResourceNote(Note::getByPK(['id' => $APNote->getObjectId()])); return Event::stop; // We got our target, stop handler execution } @@ -364,7 +364,7 @@ class FreeNetwork extends Component $resource_parts = explode($preMention, $target); if ($resource_parts[1] === $_ENV['SOCIAL_DOMAIN']) { // XXX: Common::config('site', 'server')) { - $actor = LocalUser::getWithPK(['nickname' => $resource_parts[0]])->getActor(); + $actor = LocalUser::getByPK(['nickname' => $resource_parts[0]])->getActor(); } else { Event::handle('FreeNetworkFindMentions', [$target, &$actor]); if (is_null($actor)) { @@ -399,7 +399,7 @@ class FreeNetwork extends Component // actor_view_id $reuri = '/\/actor\/(\d+)\/?/m'; if (preg_match_all($renick, $str, $matches, PREG_SET_ORDER, 0) === 1) { - $actor = LocalUser::getWithPK(['nickname' => $matches[0][1]])->getActor(); + $actor = LocalUser::getByPK(['nickname' => $matches[0][1]])->getActor(); } elseif (preg_match_all($reuri, $str, $matches, PREG_SET_ORDER, 0) === 1) { $actor = Actor::getById((int) $matches[0][1]); } else { diff --git a/plugins/ActivityPub/ActivityPub.php b/plugins/ActivityPub/ActivityPub.php index 3d58dbc223..47131f1931 100644 --- a/plugins/ActivityPub/ActivityPub.php +++ b/plugins/ActivityPub/ActivityPub.php @@ -146,7 +146,7 @@ class ActivityPub extends Plugin // Is remote? !$actor->getIsLocal() // Is in ActivityPub? - && !is_null($ap_actor = ActivitypubActor::getWithPK(['actor_id' => $actor->getId()])) + && !is_null($ap_actor = ActivitypubActor::getByPK(['actor_id' => $actor->getId()])) // We can only provide a full URL (anything else wouldn't make sense) && $type === Router::ABSOLUTE_URL ) { @@ -236,7 +236,7 @@ class ActivityPub extends Plugin $to_addr = []; foreach ($targets as $actor) { if (FreeNetworkActorProtocol::canIActor('activitypub', $actor->getId())) { - if (is_null($ap_target = ActivitypubActor::getWithPK(['actor_id' => $actor->getId()]))) { + if (is_null($ap_target = ActivitypubActor::getByPK(['actor_id' => $actor->getId()]))) { continue; } $to_addr[$ap_target->getInboxSharedUri() ?? $ap_target->getInboxUri()][] = $actor; @@ -399,13 +399,13 @@ class ActivityPub extends Plugin public static function getObjectByUri(string $resource, bool $try_online = true) { // Try known objects - $known_object = ActivitypubObject::getWithPK(['object_uri' => $resource]); + $known_object = ActivitypubObject::getByPK(['object_uri' => $resource]); if ($known_object instanceof ActivitypubObject) { return $known_object->getObject(); } // Try known activities - $known_activity = ActivitypubActivity::getWithPK(['activity_uri' => $resource]); + $known_activity = ActivitypubActivity::getByPK(['activity_uri' => $resource]); if ($known_activity instanceof ActivitypubActivity) { return $known_activity->getActivity(); } @@ -462,7 +462,7 @@ class ActivityPub extends Plugin // actor_view_id $reuri = '/\/actor\/(\d+)\/?/m'; if (preg_match_all($renick, $str, $matches, PREG_SET_ORDER, 0) === 1) { - return LocalUser::getWithPK(['nickname' => $matches[0][1]])->getActor(); + return LocalUser::getByPK(['nickname' => $matches[0][1]])->getActor(); } elseif (preg_match_all($reuri, $str, $matches, PREG_SET_ORDER, 0) === 1) { return Actor::getById((int)$matches[0][1]); } diff --git a/plugins/ActivityPub/Entity/ActivitypubRsa.php b/plugins/ActivityPub/Entity/ActivitypubRsa.php index 96b0fc5dd9..0faafef15a 100644 --- a/plugins/ActivityPub/Entity/ActivitypubRsa.php +++ b/plugins/ActivityPub/Entity/ActivitypubRsa.php @@ -144,7 +144,7 @@ class ActivitypubRsa extends Entity */ public static function getByActor(Actor $gsactor, bool $fetch = true): self { - $apRSA = self::getWithPK(['actor_id' => ($actor_id = $gsactor->getId())]); + $apRSA = self::getByPK(['actor_id' => ($actor_id = $gsactor->getId())]); if (is_null($apRSA)) { // Nonexistent key pair for this profile if ($gsactor->getIsLocal()) { diff --git a/plugins/ActivityPub/Util/Explorer.php b/plugins/ActivityPub/Util/Explorer.php index 8b7bab20e0..b9a231883b 100644 --- a/plugins/ActivityPub/Util/Explorer.php +++ b/plugins/ActivityPub/Util/Explorer.php @@ -228,8 +228,8 @@ class Explorer */ public static function get_aprofile_by_url(string $v): ActivitypubActor|bool { - $aprofile = ActivitypubActor::getWithPK(['uri' => $v]); - return is_null($aprofile) ? false : ActivitypubActor::getWithPK(['uri' => $v]); + $aprofile = ActivitypubActor::getByPK(['uri' => $v]); + return is_null($aprofile) ? false : ActivitypubActor::getByPK(['uri' => $v]); } /** diff --git a/plugins/ActivityPub/Util/Model/Activity.php b/plugins/ActivityPub/Util/Model/Activity.php index c8de67feb3..90917e4577 100644 --- a/plugins/ActivityPub/Util/Model/Activity.php +++ b/plugins/ActivityPub/Util/Model/Activity.php @@ -77,7 +77,7 @@ class Activity extends Model $type_activity = is_string($json) ? self::jsonToType($json) : $json; // Ditch known activities - $ap_act = ActivitypubActivity::getWithPK(['activity_uri' => $type_activity->get('id')]); + $ap_act = ActivitypubActivity::getByPK(['activity_uri' => $type_activity->get('id')]); if (!is_null($ap_act)) { return $ap_act; } diff --git a/plugins/ActivityPub/Util/Model/Note.php b/plugins/ActivityPub/Util/Model/Note.php index 12b97cdfad..7ec706b4f6 100644 --- a/plugins/ActivityPub/Util/Model/Note.php +++ b/plugins/ActivityPub/Util/Model/Note.php @@ -240,7 +240,7 @@ class Note extends Model 'type' => 'Document', 'mediaType' => $attachment->getMimetype(), 'url' => $attachment->getUrl(Router::ABSOLUTE_URL), - 'name' => AttachmentToNote::getWithPK(['attachment_id' => $attachment->getId(), 'note_id' => $object->getId()])->getTitle(), + 'name' => AttachmentToNote::getByPK(['attachment_id' => $attachment->getId(), 'note_id' => $object->getId()])->getTitle(), 'width' => $attachment->getWidth(), 'height' => $attachment->getHeight(), ]; diff --git a/plugins/Poll/Controller/AnswerPoll.php b/plugins/Poll/Controller/AnswerPoll.php index faaab3b544..65562c3960 100644 --- a/plugins/Poll/Controller/AnswerPoll.php +++ b/plugins/Poll/Controller/AnswerPoll.php @@ -64,7 +64,7 @@ class AnswerPoll { $user = Common::ensureLoggedIn(); - $poll = Poll::getWithPk((int) $id); + $poll = Poll::getByPk((int) $id); if ($poll == null) { throw new NotFoundException('Poll does not exist'); } diff --git a/plugins/RepeatNote/Controller/Repeat.php b/plugins/RepeatNote/Controller/Repeat.php index a3e7dd5de6..ef7a840ec5 100644 --- a/plugins/RepeatNote/Controller/Repeat.php +++ b/plugins/RepeatNote/Controller/Repeat.php @@ -1,6 +1,6 @@ getId(); - $note = Note::getWithPK(['id' => $id]); + $note = Note::getByPK(['id' => $id]); $form_add_to_repeat = Form::create([ ['add_repeat', SubmitType::class, [ 'label' => _m('Repeat note!'), - 'attr' => [ + 'attr' => [ 'title' => _m('Repeat this note!'), ], ], @@ -76,7 +75,7 @@ class Repeat extends Controller // Redirect user to where they came from // Prevent open redirect - if (!is_null($from = $this->string('from'))) { + if (!\is_null($from = $this->string('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})"); throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive) @@ -91,18 +90,18 @@ class Repeat extends Controller } return [ - '_template' => 'repeat/add_to_repeats.html.twig', - 'note' => $note, + '_template' => 'repeat/add_to_repeats.html.twig', + 'note' => $note, 'add_repeat' => $form_add_to_repeat->createView(), ]; } /** - * @throws ServerException * @throws ClientException * @throws NoLoggedInUser * @throws NoSuchNoteException * @throws RedirectException + * @throws ServerException */ public function repeatRemoveNote(Request $request, int $id): array { @@ -114,7 +113,7 @@ class Repeat extends Controller ['remove_repeat', SubmitType::class, [ 'label' => _m('Remove repeat'), - 'attr' => [ + 'attr' => [ 'title' => _m('Remove note from repeats.'), ], ], @@ -123,7 +122,7 @@ class Repeat extends Controller $form_remove_repeat->handleRequest($request); if ($form_remove_repeat->isSubmitted()) { - if (!is_null(\Plugin\RepeatNote\RepeatNote::unrepeatNote(note_id: $id, actor_id: $actor_id))) { + if (!\is_null(\Plugin\RepeatNote\RepeatNote::unrepeatNote(note_id: $id, actor_id: $actor_id))) { DB::flush(); } else { throw new ClientException(_m('Note wasn\'t repeated!')); @@ -131,7 +130,7 @@ class Repeat extends Controller // Redirect user to where they came from // Prevent open redirect - if (!is_null($from = $this->string('from'))) { + if (!\is_null($from = $this->string('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})"); throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive) @@ -146,8 +145,8 @@ class Repeat extends Controller } return [ - '_template' => 'repeat/remove_from_repeats.html.twig', - 'note' => Note::getById($id), + '_template' => 'repeat/remove_from_repeats.html.twig', + 'note' => Note::getById($id), 'remove_repeat' => $form_remove_repeat->createView(), ]; } diff --git a/plugins/RepeatNote/RepeatNote.php b/plugins/RepeatNote/RepeatNote.php index 84d2c9b594..5ac63cfb6b 100644 --- a/plugins/RepeatNote/RepeatNote.php +++ b/plugins/RepeatNote/RepeatNote.php @@ -1,6 +1,6 @@ $actor_id, - 'note_id' => $note->getId(), + 'note_id' => $note->getId(), ])[0] ?? null; - if (!is_null($repeat_entity)) { + if (!\is_null($repeat_entity)) { return DB::findBy('activity', [ - 'actor_id' => $actor_id, - 'verb' => 'repeat', - 'object_type' => 'note', - 'object_id' => $note->getId() - ], order_by: ['created' => 'dsc'])[0]; + 'actor_id' => $actor_id, + 'verb' => 'repeat', + 'object_type' => 'note', + 'object_id' => $note->getId(), + ], order_by: ['created' => 'dsc'])[0]; } // Create a new note with the same content as the original @@ -66,30 +65,30 @@ class RepeatNote extends NoteHandlerPlugin actor: Actor::getById($actor_id), content: $note->getContent(), content_type: $note->getContentType(), - language: is_null($lang_id = $note->getLanguageId()) ? null : Language::getById($lang_id)->getLocale(), + language: \is_null($lang_id = $note->getLanguageId()) ? null : Language::getById($lang_id)->getLocale(), processed_attachments: $note->getAttachmentsWithTitle(), ); // Find the id of the note we just created $repeat_id = $repeat?->getId(); - $og_id = $note->getId(); + $og_id = $note->getId(); // Add it to note_repeat table - if (!is_null($repeat_id)) { + if (!\is_null($repeat_id)) { DB::persist(NoteRepeat::create([ - 'note_id' => $repeat_id, - 'actor_id' => $actor_id, + 'note_id' => $repeat_id, + 'actor_id' => $actor_id, 'repeat_of' => $og_id, ])); } // Log an activity $repeat_activity = Activity::create([ - 'actor_id' => $actor_id, - 'verb' => 'repeat', + 'actor_id' => $actor_id, + 'verb' => 'repeat', 'object_type' => 'note', - 'object_id' => $note->getId(), - 'source' => $source, + 'object_id' => $note->getId(), + 'source' => $source, ]); DB::persist($repeat_activity); @@ -102,13 +101,13 @@ class RepeatNote extends NoteHandlerPlugin { $already_repeated = DB::findBy('note_repeat', ['actor_id' => $actor_id, 'repeat_of' => $note_id])[0] ?? null; - if (!is_null($already_repeated)) { // If it was repeated, then we can undo it + if (!\is_null($already_repeated)) { // If it was repeated, then we can undo it // Find previous repeat activity $already_repeated_activity = DB::findBy('activity', [ - 'actor_id' => $actor_id, - 'verb' => 'repeat', + 'actor_id' => $actor_id, + 'verb' => 'repeat', 'object_type' => 'note', - 'object_id' => $already_repeated->getRepeatOf() + 'object_id' => $already_repeated->getRepeatOf(), ])[0] ?? null; // Remove the clone note @@ -119,11 +118,11 @@ class RepeatNote extends NoteHandlerPlugin // Log an activity $undo_repeat_activity = Activity::create([ - 'actor_id' => $actor_id, - 'verb' => 'undo', + 'actor_id' => $actor_id, + 'verb' => 'undo', 'object_type' => 'activity', - 'object_id' => $already_repeated_activity->getId(), - 'source' => $source, + 'object_id' => $already_repeated_activity->getId(), + 'source' => $source, ]); DB::persist($undo_repeat_activity); @@ -132,17 +131,17 @@ class RepeatNote extends NoteHandlerPlugin return $undo_repeat_activity; } else { // Either was undoed already - if (!is_null($already_repeated_activity = DB::findBy('activity', [ + if (!\is_null($already_repeated_activity = DB::findBy('activity', [ 'actor_id' => $actor_id, 'verb' => 'repeat', 'object_type' => 'note', 'object_id' => $note_id, ])[0] ?? null)) { return DB::findBy('activity', [ - 'actor_id' => $actor_id, - 'verb' => 'undo', + 'actor_id' => $actor_id, + 'verb' => 'undo', 'object_type' => 'activity', - 'object_id' => $already_repeated_activity->getId(), + 'object_id' => $already_repeated_activity->getId(), ])[0] ?? null; // null if not undoed } else { // or it's an attempt to undo something that wasn't repeated in the first place, @@ -155,29 +154,26 @@ class RepeatNote extends NoteHandlerPlugin * HTML rendering event that adds the repeat 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 { // Only logged users can repeat notes - if (is_null($user = Common::user())) { + if (\is_null($user = Common::user())) { return Event::next; } // If note is repeated, "is_repeated" is 1, 0 otherwise. $is_repeat = ($note_repeat = DB::findBy('note_repeat', [ 'actor_id' => $user->getId(), - 'note_id' => $note->getId() + 'note_id' => $note->getId(), ])) !== [] ? 1 : 0; // If note was already repeated, do not add the action try { if (DB::findOneBy('note_repeat', [ 'repeat_of' => $note->getId(), - 'actor_id' => $user->getId() + 'actor_id' => $user->getId(), ])) { return Event::next; } @@ -186,8 +182,8 @@ class RepeatNote extends NoteHandlerPlugin } // Generating URL for repeat action route - $args = ['id' => $is_repeat === 0 ? $note->getId() : $note_repeat[0]->getRepeatOf()]; - $type = Router::ABSOLUTE_PATH; + $args = ['id' => $is_repeat === 0 ? $note->getId() : $note_repeat[0]->getRepeatOf()]; + $type = Router::ABSOLUTE_PATH; $repeat_action_url = $is_repeat ? Router::url('repeat_remove', $args, $type) : Router::url('repeat_add', $args, $type); @@ -196,14 +192,14 @@ class RepeatNote extends NoteHandlerPlugin // SECURITY: open redirect? $query_string = $request->getQueryString(); // Concatenating get parameter to redirect the user to where he came from - $repeat_action_url .= !is_null($query_string) ? '?from=' . mb_substr($query_string, 2) : ''; + $repeat_action_url .= !\is_null($query_string) ? '?from=' . mb_substr($query_string, 2) : ''; $extra_classes = $is_repeat ? 'note-actions-set' : 'note-actions-unset'; $repeat_action = [ - 'url' => $repeat_action_url, - 'title' => $is_repeat ? 'Remove this repeat' : 'Repeat this note!', + 'url' => $repeat_action_url, + 'title' => $is_repeat ? 'Remove this repeat' : 'Repeat this note!', 'classes' => "button-container repeat-button-container {$extra_classes}", - 'id' => 'repeat-button-container-' . $note->getId(), + 'id' => 'repeat-button-container-' . $note->getId(), ]; $actions[] = $repeat_action; @@ -219,19 +215,19 @@ class RepeatNote extends NoteHandlerPlugin { // 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?" - $check_user = !is_null(Common::user()); + $check_user = !\is_null(Common::user()); $note = $vars['note']; $complementary_info = ''; - $repeat_actor = []; - $note_repeats = NoteRepeat::getNoteRepeats($note); + $repeat_actor = []; + $note_repeats = NoteRepeat::getNoteRepeats($note); // Get actors who replied foreach ($note_repeats as $reply) { - $repeat_actor[] = Actor::getWithPK($reply->getActorId()); + $repeat_actor[] = Actor::getByPK($reply->getActorId()); } - if (count($repeat_actor) < 1) { + if (\count($repeat_actor) < 1) { return Event::next; } @@ -240,7 +236,7 @@ class RepeatNote extends NoteHandlerPlugin // Add to complementary info foreach ($repeat_actor as $actor) { - $repeat_actor_url = $actor->getUrl(); + $repeat_actor_url = $actor->getUrl(); $repeat_actor_nickname = $actor->getNickname(); if ($check_user && $actor->getId() === (Common::actor())->getId()) { @@ -251,7 +247,7 @@ class RepeatNote extends NoteHandlerPlugin $you_translation = 'You'; } - $prepend = "{$you_translation}, " . ($prepend = &$complementary_info); + $prepend = "{$you_translation}, " . ($prepend = &$complementary_info); $complementary_info = $prepend; } else { // If the repeat is from someone else @@ -279,33 +275,33 @@ class RepeatNote 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'), ['Announce', 'Undo'])) { + if (!\in_array($type_activity->get('type'), ['Announce', 'Undo'])) { return Event::next; } if ($type_activity->get('type') === 'Announce') { // Repeat if ($type_object instanceof \ActivityPhp\Type\AbstractObject) { if ($type_object->get('type') === 'Note') { - $note = \Plugin\ActivityPub\Util\Model\Note::fromJson($type_object); + $note = \Plugin\ActivityPub\Util\Model\Note::fromJson($type_object); $note_id = $note->getId(); } else { return Event::next; } - } else if ($type_object instanceof Note) { - $note = $type_object; - $note_id = $$note->getId(); + } elseif ($type_object instanceof Note) { + $note = $type_object; + $note_id = ${$note}->getId(); } else { return Event::next; } } else { // Undo Repeat if ($type_object instanceof \ActivityPhp\Type\AbstractObject) { $ap_prev_repeat_act = \Plugin\ActivityPub\Util\Model\Activity::fromJson($type_object); - $prev_repeat_act = $ap_prev_repeat_act->getActivity(); + $prev_repeat_act = $ap_prev_repeat_act->getActivity(); if ($prev_repeat_act->getVerb() === 'repeat' && $prev_repeat_act->getObjectType() === 'note') { $note_id = $prev_repeat_act->getObjectId(); } else { return Event::next; } - } else if ($type_object instanceof Activity) { + } elseif ($type_object instanceof Activity) { if ($type_object->getVerb() === 'repeat' && $type_object->getObjectType() === 'note') { $note_id = $type_object->getObjectId(); } else { @@ -323,10 +319,10 @@ class RepeatNote extends NoteHandlerPlugin } // Store ActivityPub Activity $ap_act = \Plugin\ActivityPub\Entity\ActivitypubActivity::create([ - 'activity_id' => $act->getId(), + 'activity_id' => $act->getId(), 'activity_uri' => $type_activity->get('id'), - 'created' => new \DateTime($type_activity->get('published') ?? 'now'), - 'modified' => new \DateTime(), + 'created' => new DateTime($type_activity->get('published') ?? 'now'), + 'modified' => new DateTime(), ]); DB::persist($ap_act); return Event::stop; diff --git a/src/Core/Entity.php b/src/Core/Entity.php index 8464d4c764..c060c130a7 100644 --- a/src/Core/Entity.php +++ b/src/Core/Entity.php @@ -59,8 +59,8 @@ abstract class Entity { $class = static::class; - $date = new DateTime(); - if (!is_null($obj)) { // Update modified + $date = new DateTime(); + if (!\is_null($obj)) { // Update modified if (property_exists($class, 'modified')) { $args['modified'] = $date; } @@ -117,13 +117,13 @@ abstract class Entity * - array[string => mixed] $values - Perform a regular find * * Examples: - * Entity::getWithPK(42); - * Entity::getWithPK([42, 'foo']); - * Entity::getWithPK(['key1' => 42, 'key2' => 'foo']) + * Entity::getByPK(42); + * Entity::getByPK([42, 'foo']); + * Entity::getByPK(['key1' => 42, 'key2' => 'foo']) * * @return null|static */ - public static function getWithPK(mixed $values): ?self + public static function getByPK(mixed $values): ?self { $values = \is_array($values) ? $values : [$values]; $class = \get_called_class(); @@ -144,7 +144,6 @@ abstract class Entity } /** - * * @return array of Actors */ public function getNotificationTargets(array $ids_already_known = []): array diff --git a/src/Entity/Note.php b/src/Entity/Note.php index 9b24da8fc3..e288a14bb1 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -232,12 +232,12 @@ class Note extends Entity public function getNoteLanguageShortDisplay(): ?string { - return !is_null($this->language_id) ? Language::getById($this->language_id)->getShortDisplay() : null; + return !\is_null($this->language_id) ? Language::getById($this->language_id)->getShortDisplay() : null; } public function getLanguageLocale(): ?string { - return !is_null($this->language_id) ? Language::getById($this->language_id)->getLocale() : null; + return !\is_null($this->language_id) ? Language::getById($this->language_id)->getLocale() : null; } public static function getAllNotesByActor(Actor $actor): array @@ -312,9 +312,9 @@ class Note extends Entity }); } - public function getReplyToNote(): ?Note + public function getReplyToNote(): ?self { - return self::getWithPK($this->getReplyTo()); + return self::getByPK($this->getReplyTo()); } public function getReplies() @@ -363,13 +363,14 @@ class Note extends Entity public function delete(?int $actor_id = null, string $source = 'web'): bool { if (Event::handle('NoteDeleteRelated', [&$this]) === Event::next) { - DB::persist(Activity::create([ - 'actor_id' => $actor_id ?? $this->getActorId(), - 'verb' => 'delete', - 'object_type' => 'note', - 'object_id' => $this->getId(), - 'source' => $source - ]) + DB::persist( + Activity::create([ + 'actor_id' => $actor_id ?? $this->getActorId(), + 'verb' => 'delete', + 'object_type' => 'note', + 'object_id' => $this->getId(), + 'source' => $source, + ]), ); DB::remove($this); return true; diff --git a/src/Entity/ResetPasswordRequest.php b/src/Entity/ResetPasswordRequest.php index b2691d9a4c..5b631e5e8d 100644 --- a/src/Entity/ResetPasswordRequest.php +++ b/src/Entity/ResetPasswordRequest.php @@ -1,8 +1,11 @@ token; } - public function setExpires(\DateTimeInterface $expires): self + public function setExpires(DateTimeInterface $expires): self { $this->expires = $expires; return $this; } - public function getExpires(): \DateTimeInterface + public function getExpires(): DateTimeInterface { return $this->expires; } - public function setCreated(\DateTimeInterface $created): self + public function setCreated(DateTimeInterface $created): self { $this->created = $created; return $this; } - public function getCreated(): \DateTimeInterface + public function getCreated(): DateTimeInterface { return $this->created; } @@ -87,7 +90,7 @@ class ResetPasswordRequest extends Entity implements ResetPasswordRequestInterfa // {{{ Interface // @codeCoverageIgnoreStart - public function __construct(object $user, \DateTimeInterface $expiresAt, string $selector, string $hashedToken) + public function __construct(object $user, DateTimeInterface $expiresAt, string $selector, string $hashedToken) { $this->user_id = $user->getId(); $this->expires = $expiresAt; @@ -97,10 +100,10 @@ class ResetPasswordRequest extends Entity implements ResetPasswordRequestInterfa public function getUser(): object { - return LocalUser::getWithPK($this->user_id); + return LocalUser::getByPK($this->user_id); } - public function getRequestedAt(): \DateTimeInterface + public function getRequestedAt(): DateTimeInterface { return $this->created; } @@ -110,7 +113,7 @@ class ResetPasswordRequest extends Entity implements ResetPasswordRequestInterfa return $this->expires->getTimestamp() <= time(); } - public function getExpiresAt(): \DateTimeInterface + public function getExpiresAt(): DateTimeInterface { return $this->expires; } diff --git a/tests/Core/EntityTest.php b/tests/Core/EntityTest.php index 8fa6ae6fd1..5ddbbf0364 100644 --- a/tests/Core/EntityTest.php +++ b/tests/Core/EntityTest.php @@ -61,13 +61,13 @@ class EntityTest extends GNUsocialTestCase static::assertTrue($is_update); } - public function testGetWithPK() + public function testGetByPK() { $user = DB::findOneBy('local_user', ['nickname' => 'taken_user']); - $user_with_pk = LocalUser::getWithPK($user->getId()); + $user_with_pk = LocalUser::getByPK($user->getId()); static::assertSame($user, $user_with_pk); - $user_with_pk = LocalUser::getWithPK(['id' => $user->getId()]); + $user_with_pk = LocalUser::getByPK(['id' => $user->getId()]); static::assertSame($user, $user_with_pk); - static::assertNull(LocalUser::getWithPK(0)); + static::assertNull(LocalUser::getByPK(0)); } }