[CORE][ENTITY] Rename 'Entity::getWithPK' to 'Entity::getByPK'

This commit is contained in:
Hugo Sales 2021-12-16 11:14:34 +00:00 committed by Diogo Peralta Cordeiro
parent 330b6b49a2
commit a81ac673ac
No known key found for this signature in database
GPG Key ID: 18D2D35001FBFAB0
16 changed files with 143 additions and 157 deletions

View File

@ -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

View File

@ -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)

View File

@ -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;

View File

@ -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 {

View File

@ -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]);
}

View File

@ -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()) {

View File

@ -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]);
}
/**

View File

@ -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;
}

View File

@ -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(),
];

View File

@ -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');
}

View File

@ -26,6 +26,7 @@ namespace Plugin\RepeatNote\Controller;
use App\Core\Controller;
use App\Core\DB\DB;
use App\Core\Form;
use function App\Core\I18n\_m;
use App\Core\Log;
use App\Core\Router\Router;
use App\Entity\Note;
@ -37,26 +38,24 @@ use App\Util\Exception\RedirectException;
use App\Util\Exception\ServerException;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use function App\Core\I18n\_m;
use function is_null;
class Repeat extends Controller
{
/**
* Controller for the note repeat non-JS page
*
* @throws ServerException
* @throws ClientException
* @throws NoLoggedInUser
* @throws NoSuchNoteException
* @throws RedirectException
* @throws ServerException
*/
public function repeatAddNote(Request $request, int $id): bool|array
{
$user = Common::ensureLoggedIn();
$actor_id = $user->getId();
$note = Note::getWithPK(['id' => $id]);
$note = Note::getByPK(['id' => $id]);
$form_add_to_repeat = Form::create([
['add_repeat', SubmitType::class,
@ -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)
@ -98,11 +97,11 @@ class Repeat extends Controller
}
/**
* @throws ServerException
* @throws ClientException
* @throws NoLoggedInUser
* @throws NoSuchNoteException
* @throws RedirectException
* @throws ServerException
*/
public function repeatRemoveNote(Request $request, int $id): array
{
@ -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)

View File

@ -23,6 +23,7 @@ namespace Plugin\RepeatNote;
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;
@ -36,12 +37,10 @@ use App\Util\Exception\NotFoundException;
use App\Util\Exception\ServerException;
use App\Util\Formatting;
use Component\Posting\Posting;
use DateTime;
use Plugin\RepeatNote\Entity\NoteRepeat;
use Symfony\Component\HttpFoundation\Request;
use function App\Core\I18n\_m;
use function count;
use function is_null;
use const SORT_REGULAR;
use Symfony\Component\HttpFoundation\Request;
class RepeatNote extends NoteHandlerPlugin
{
@ -52,12 +51,12 @@ class RepeatNote extends NoteHandlerPlugin
'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()
'object_id' => $note->getId(),
], order_by: ['created' => 'dsc'])[0];
}
@ -66,7 +65,7 @@ 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(),
);
@ -75,7 +74,7 @@ class RepeatNote extends NoteHandlerPlugin
$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,
@ -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',
'object_type' => 'note',
'object_id' => $already_repeated->getRepeatOf()
'object_id' => $already_repeated->getRepeatOf(),
])[0] ?? null;
// Remove the clone note
@ -132,7 +131,7 @@ 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',
@ -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;
}
@ -196,7 +192,7 @@ 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 = [
@ -219,7 +215,7 @@ 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'];
@ -229,9 +225,9 @@ class RepeatNote extends NoteHandlerPlugin
// 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;
}
@ -279,7 +275,7 @@ 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
@ -292,7 +288,7 @@ class RepeatNote extends NoteHandlerPlugin
}
} elseif ($type_object instanceof Note) {
$note = $type_object;
$note_id = $$note->getId();
$note_id = ${$note}->getId();
} else {
return Event::next;
}
@ -325,8 +321,8 @@ class RepeatNote extends NoteHandlerPlugin
$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(),
'created' => new DateTime($type_activity->get('published') ?? 'now'),
'modified' => new DateTime(),
]);
DB::persist($ap_act);
return Event::stop;

View File

@ -60,7 +60,7 @@ abstract class Entity
$class = static::class;
$date = new DateTime();
if (!is_null($obj)) { // Update modified
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

View File

@ -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([
DB::persist(
Activity::create([
'actor_id' => $actor_id ?? $this->getActorId(),
'verb' => 'delete',
'object_type' => 'note',
'object_id' => $this->getId(),
'source' => $source
])
'source' => $source,
]),
);
DB::remove($this);
return true;

View File

@ -1,8 +1,11 @@
<?php
declare(strict_types = 1);
namespace App\Entity;
use App\Core\Entity;
use DateTimeInterface;
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestInterface;
class ResetPasswordRequest extends Entity implements ResetPasswordRequestInterface
@ -13,8 +16,8 @@ class ResetPasswordRequest extends Entity implements ResetPasswordRequestInterfa
private int $user_id;
private ?string $selector;
private ?string $token;
private \DateTimeInterface $expires;
private \DateTimeInterface $created;
private DateTimeInterface $expires;
private DateTimeInterface $created;
public function setId(int $id): self
{
@ -60,24 +63,24 @@ class ResetPasswordRequest extends Entity implements ResetPasswordRequestInterfa
return $this->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;
}

View File

@ -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));
}
}