[ENTITY] Rename 'getFrom' to 'getBy'

This commit is contained in:
Hugo Sales 2021-12-04 19:58:00 +00:00
parent c40866ecf6
commit e29e1cc87c
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0
15 changed files with 110 additions and 96 deletions

View File

@ -181,7 +181,7 @@ class Posting extends Component
'content' => $content, 'content' => $content,
'content_type' => $content_type, 'content_type' => $content_type,
'rendered' => $rendered, 'rendered' => $rendered,
'language_id' => Language::getFromLocale($language)->getId(), 'language_id' => Language::getByLocale($language)->getId(),
'is_local' => true, 'is_local' => true,
]); ]);

View File

@ -74,7 +74,7 @@ class Tag extends Component
preg_match_all(self::TAG_REGEX, $content, $matched_tags, \PREG_SET_ORDER); preg_match_all(self::TAG_REGEX, $content, $matched_tags, \PREG_SET_ORDER);
foreach ($matched_tags as $match) { foreach ($matched_tags as $match) {
$tag = self::ensureValid($match[2]); $tag = self::ensureValid($match[2]);
$canonical_tag = self::canonicalTag($tag, Language::getFromId($note->getLanguageId())->getLocale()); $canonical_tag = self::canonicalTag($tag, Language::getById($note->getLanguageId())->getLocale());
DB::persist(NoteTag::create([ DB::persist(NoteTag::create([
'tag' => $tag, 'tag' => $tag,
'canonical' => $canonical_tag, 'canonical' => $canonical_tag,

View File

@ -68,9 +68,9 @@ class Note extends Model
$source = $options['source']; $source = $options['source'];
$actor_uri = $options['actor_uri']; $actor_uri = $options['actor_uri'];
$actor_id = $options['actor_id']; $actor_id = $options['actor_id'];
$type_note = is_string($json) ? self::jsonToType($json) : $json; $type_note = \is_string($json) ? self::jsonToType($json) : $json;
if (is_null($actor_uri) || $actor_uri !== $type_note->get('attributedTo')) { if (\is_null($actor_uri) || $actor_uri !== $type_note->get('attributedTo')) {
$actor_id = ActivityPub::getActorByUri($type_note->get('attributedTo'))->getId(); $actor_id = ActivityPub::getActorByUri($type_note->get('attributedTo'))->getId();
} }
$map = [ $map = [
@ -98,8 +98,8 @@ class Note extends Model
$obj = new GSNote(); $obj = new GSNote();
if (!is_null($map['language_id'])) { if (!\is_null($map['language_id'])) {
$map['language_id'] = Language::getFromLocale($map['language_id'])->getId(); $map['language_id'] = Language::getByLocale($map['language_id'])->getId();
} else { } else {
$map['language_id'] = null; $map['language_id'] = null;
} }

View File

@ -91,7 +91,7 @@ class Repeat extends Controller
actor: Actor::getById($actor_id), actor: Actor::getById($actor_id),
content: $note->getContent(), content: $note->getContent(),
content_type: $note->getContentType(), content_type: $note->getContentType(),
language: Language::getFromId($note->getLanguageId())->getLocale(), language: Language::getById($note->getLanguageId())->getLocale(),
processed_attachments: $note->getAttachmentsWithTitle(), processed_attachments: $note->getAttachmentsWithTitle(),
); );

View File

@ -52,9 +52,9 @@ class TagBasedFiltering extends Controller
public function editBlockedNoteTags(Request $request, ?int $note_id) public function editBlockedNoteTags(Request $request, ?int $note_id)
{ {
$user = Common::ensureLoggedIn(); $user = Common::ensureLoggedIn();
$note = !\is_null($note_id) ? Note::getFromId($note_id) : null; $note = !\is_null($note_id) ? Note::getById($note_id) : null;
$note_tag_blocks = NoteTagBlock::getFromActorId($user->getId()); $note_tag_blocks = NoteTagBlock::getByActorId($user->getId());
$note_tags = !\is_null($note_id) ? NoteTag::getFromNoteId($note_id) : []; $note_tags = !\is_null($note_id) ? NoteTag::getByNoteId($note_id) : [];
$blockable_note_tags = F\reject( $blockable_note_tags = F\reject(
$note_tags, $note_tags,
fn (NoteTag $nt) => NoteTagBlock::checkBlocksNoteTag($nt, $note_tag_blocks), fn (NoteTag $nt) => NoteTagBlock::checkBlocksNoteTag($nt, $note_tag_blocks),
@ -92,7 +92,7 @@ class TagBasedFiltering extends Controller
Cache::delete(NoteTagBlock::cacheKey($user->getId())); Cache::delete(NoteTagBlock::cacheKey($user->getId()));
Cache::delete(TagFilerPlugin::cacheKeys($user->getId())['note']); Cache::delete(TagFilerPlugin::cacheKeys($user->getId())['note']);
$new_tag = Tag::ensureValid($data[$canon . ':new-tag']); $new_tag = Tag::ensureValid($data[$canon . ':new-tag']);
$canonical_tag = Tag::canonicalTag($new_tag, Language::getFromNote($note)->getLocale()); $canonical_tag = Tag::canonicalTag($new_tag, Language::getByNote($note)->getLocale());
DB::persist(NoteTagBlock::create([ DB::persist(NoteTagBlock::create([
'blocker' => $user->getId(), 'blocker' => $user->getId(),
'tag' => $new_tag, 'tag' => $new_tag,
@ -140,7 +140,7 @@ class TagBasedFiltering extends Controller
return [ return [
'_template' => 'tag-based-filtering/edit-tags.html.twig', '_template' => 'tag-based-filtering/edit-tags.html.twig',
'note' => !\is_null($note_id) ? Note::getFromId($note_id) : null, 'note' => !\is_null($note_id) ? Note::getById($note_id) : null,
'new_tags_form' => $new_tags_form?->createView(), 'new_tags_form' => $new_tags_form?->createView(),
'existing_tags_form' => $existing_tags_form?->createView(), 'existing_tags_form' => $existing_tags_form?->createView(),
]; ];

View File

@ -82,7 +82,7 @@ class TagBasedFiltering extends Plugin
$notes_out = F\reject( $notes_out = F\reject(
$notes, $notes,
fn (Note $n) => F\some( fn (Note $n) => F\some(
dump(NoteTag::getFromNoteId($n->getId())), dump(NoteTag::getByNoteId($n->getId())),
fn ($nt) => NoteTagBlock::checkBlocksNoteTag($nt, $blocked_note_tags), fn ($nt) => NoteTagBlock::checkBlocksNoteTag($nt, $blocked_note_tags),
), ),
); );

View File

@ -42,6 +42,7 @@ use App\Core\Controller;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m;
use App\Core\Log; use App\Core\Log;
use App\Entity\ActorLanguage; use App\Entity\ActorLanguage;
use App\Entity\Language; use App\Entity\Language;
@ -73,8 +74,6 @@ use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\SubmitButton; use Symfony\Component\Form\SubmitButton;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use function App\Core\I18n\_m;
use function is_null;
// }}} Imports // }}} Imports
@ -108,8 +107,6 @@ class UserPanel extends Controller
/** /**
* Change email settings form * Change email settings form
* *
* @param Request $request
* @return FormInterface
* @throws NoLoggedInUser * @throws NoLoggedInUser
* @throws ServerException * @throws ServerException
*/ */
@ -142,8 +139,6 @@ class UserPanel extends Controller
/** /**
* Change password form * Change password form
* *
* @param Request $request
* @return FormInterface
* @throws AuthenticationException * @throws AuthenticationException
* @throws NoLoggedInUser * @throws NoLoggedInUser
* @throws ServerException * @throws ServerException
@ -162,7 +157,7 @@ class UserPanel extends Controller
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData(); $data = $form->getData();
if (!is_null($data['old_password'])) { if (!\is_null($data['old_password'])) {
$data['password'] = $form->get('password')->getData(); $data['password'] = $form->get('password')->getData();
if (!($user->changePassword($data['old_password'], $data['password']))) { if (!($user->changePassword($data['old_password'], $data['password']))) {
throw new AuthenticationException(_m('The provided password is incorrect')); throw new AuthenticationException(_m('The provided password is incorrect'));
@ -182,9 +177,9 @@ class UserPanel extends Controller
} }
/** /**
* @throws NoLoggedInUser
* @throws RedirectException * @throws RedirectException
* @throws ServerException * @throws ServerException
* @throws NoLoggedInUser
*/ */
public function language(Request $request): FormInterface public function language(Request $request): FormInterface
{ {
@ -227,7 +222,6 @@ class UserPanel extends Controller
throw new RedirectException('settings_sort_languages', ['_fragment' => null]); // TODO doesn't clear fragment throw new RedirectException('settings_sort_languages', ['_fragment' => null]); // TODO doesn't clear fragment
} }
} }
return $form; return $form;
} }
@ -235,8 +229,6 @@ class UserPanel extends Controller
/** /**
* Local user personal information panel * Local user personal information panel
* *
* @param Request $request
* @return mixed
* @throws NicknameEmptyException * @throws NicknameEmptyException
* @throws NicknameInvalidException * @throws NicknameInvalidException
* @throws NicknameNotAllowedException * @throws NicknameNotAllowedException
@ -367,8 +359,6 @@ class UserPanel extends Controller
/** /**
* Controller for defining the ordering of a users' languages * Controller for defining the ordering of a users' languages
* *
* @param Request $request
* @return array
* @throws NoLoggedInUser * @throws NoLoggedInUser
* @throws RedirectException * @throws RedirectException
* @throws ServerException * @throws ServerException
@ -397,7 +387,7 @@ class UserPanel extends Controller
asort($data); // Sort by the order value asort($data); // Sort by the order value
$data = array_keys($data); // This keeps the order and gives us a unique number for each $data = array_keys($data); // This keeps the order and gives us a unique number for each
foreach ($data as $order => $locale) { foreach ($data as $order => $locale) {
$lang = Language::getFromLocale($locale); $lang = Language::getByLocale($locale);
$actor_lang = DB::getReference('actor_language', ['actor_id' => $user->getId(), 'language_id' => $lang->getId()]); $actor_lang = DB::getReference('actor_language', ['actor_id' => $user->getId(), 'language_id' => $lang->getId()]);
$actor_lang->setOrdering($order + 1); $actor_lang->setOrdering($order + 1);
} }

View File

@ -108,7 +108,7 @@ class ActorLanguage extends Entity
'select l from actor_language al join language l with al.language_id = l.id where al.actor_id = :id order by al.ordering ASC', 'select l from actor_language al join language l with al.language_id = l.id where al.actor_id = :id order by al.ordering ASC',
['id' => $id], ['id' => $id],
), ),
) ?: [Language::getFromLocale(Common::config('site', 'language'))]; ) ?: [Language::getByLocale(Common::config('site', 'language'))];
} }
public static function schemaDef(): array public static function schemaDef(): array

View File

@ -21,6 +21,8 @@ declare(strict_types = 1);
namespace App\Entity; namespace App\Entity;
use App\Core\Cache;
use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use Component\Tag\Tag; use Component\Tag\Tag;
use DateTimeInterface; use DateTimeInterface;
@ -91,6 +93,25 @@ class ActorTagBlock extends Entity
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
// }}} Autocode // }}} Autocode
public static function cacheKey(int $actor_id)
{
return "actor-tag-blocks-{$actor_id}";
}
public static function getByActorId(int $actor_id)
{
return Cache::getList(self::cacheKey($actor_id), fn () => DB::findBy('actor_tag_block', ['blocker' => $actor_id]));
}
/**
* Check whether $actor_tag is considered blocked by one of
* $actor_tag_blocks
*/
public static function checkBlocksActorTag(ActorTag $actor_tag, array $actor_tag_blocks): bool
{
return F\some($actor_tag_blocks, fn ($ntb) => ($ntb->getUseCanonical() && $actor_tag->getCanonical() === $ntb->getCanonical()) || $actor_tag->getTag() === $ntb->getTag());
}
public static function schemaDef(): array public static function schemaDef(): array
{ {
return [ return [

View File

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types = 1);
// {{{ License // {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social // This file is part of GNU social - https://www.gnu.org/software/social
// //
@ -62,8 +64,8 @@ class Group extends Entity
private ?string $mainpage; private ?string $mainpage;
private ?int $join_policy; private ?int $join_policy;
private ?int $force_scope; private ?int $force_scope;
private \DateTimeInterface $created; private DateTimeInterface $created;
private \DateTimeInterface $modified; private DateTimeInterface $modified;
public function setId(int $id): self public function setId(int $id): self
{ {
@ -271,7 +273,7 @@ class Group extends Entity
return Actor::getById($this->getId()); return Actor::getById($this->getId());
} }
public static function getFromNickname(string $nickname, ?Actor $actor = null): ?self public static function getByNickname(string $nickname, ?Actor $actor = null): ?self
{ {
$nickname = Nickname::normalize($nickname, check_already_used: false); $nickname = Nickname::normalize($nickname, check_already_used: false);
$group = null; $group = null;

View File

@ -108,7 +108,7 @@ class Language extends Entity
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
// }}} Autocode // }}} Autocode
public static function getFromId(int $id): self public static function getById(int $id): self
{ {
return Cache::getHashMapKey( return Cache::getHashMapKey(
map_key: 'languages-id', map_key: 'languages-id',
@ -117,7 +117,7 @@ class Language extends Entity
); );
} }
public static function getFromLocale(string $locale): self public static function getByLocale(string $locale): self
{ {
return Cache::getHashMapKey( return Cache::getHashMapKey(
'languages', 'languages',
@ -126,9 +126,9 @@ class Language extends Entity
); );
} }
public static function getFromNote(Note $note): self public static function getByNote(Note $note): self
{ {
return self::getFromId($note->getLanguageId()); return self::getById($note->getLanguageId());
} }
public static function getLanguageChoices(): array public static function getLanguageChoices(): array
@ -159,7 +159,7 @@ class Language extends Entity
$key = array_key_first($preferred_language_choices); $key = array_key_first($preferred_language_choices);
$locale = $preferred_language_choices[$key]; $locale = $preferred_language_choices[$key];
unset($preferred_language_choices[$key], $language_choices[$key]); unset($preferred_language_choices[$key], $language_choices[$key]);
$short_display = self::getFromLocale($locale)->getShortDisplay(); $short_display = self::getByLocale($locale)->getShortDisplay();
$preferred_language_choices[$short_display] = trim($locale); $preferred_language_choices[$short_display] = trim($locale);
$language_choices[$short_display] = trim($locale); $language_choices[$short_display] = trim($locale);
} }

View File

@ -216,19 +216,19 @@ class Note extends Entity
return Avatar::getUrl($this->getActorId(), $size); return Avatar::getUrl($this->getActorId(), $size);
} }
public static function getFromId(int $note_id): self public static function getById(int $note_id): self
{ {
return Cache::get("note-{$note_id}", fn () => DB::findOneBy('note', ['id' => $note_id])); return Cache::get("note-{$note_id}", fn () => DB::findOneBy('note', ['id' => $note_id]));
} }
public function getNoteLanguageShortDisplay(): string public function getNoteLanguageShortDisplay(): string
{ {
return Language::getFromId($this->language_id)->getShortDisplay(); return Language::getById($this->language_id)->getShortDisplay();
} }
public function getLanguageLocale(): string public function getLanguageLocale(): string
{ {
return Language::getFromId($this->language_id)->getLocale(); return Language::getById($this->language_id)->getLocale();
} }
public static function getAllNotesByActor(Actor $actor): array public static function getAllNotesByActor(Actor $actor): array
@ -330,7 +330,7 @@ class Note extends Entity
{ {
$rendered = null; $rendered = null;
$mentions = []; $mentions = [];
Event::handle('RenderNoteContent', [$this->getContent(), $this->getContentType(), &$rendered, &$mentions, $this->getActor(), Language::getFromId($this->getLanguageId())->getLocale()]); Event::handle('RenderNoteContent', [$this->getContent(), $this->getContentType(), &$rendered, &$mentions, $this->getActor(), Language::getById($this->getLanguageId())->getLocale()]);
$mentioned = []; $mentioned = [];
foreach ($mentions as $mention) { foreach ($mentions as $mention) {
foreach ($mention['mentioned'] as $m) { foreach ($mention['mentioned'] as $m) {

View File

@ -118,7 +118,7 @@ class NoteTag extends Entity
return "note-tags-{$note_id}"; return "note-tags-{$note_id}";
} }
public static function getFromNoteId(int $note_id): array public static function getByNoteId(int $note_id): array
{ {
return Cache::getList(self::cacheKey($note_id), fn () => DB::dql('select nt from note_tag nt join note n with n.id = nt.note_id where n.id = :id', ['id' => $note_id])); return Cache::getList(self::cacheKey($note_id), fn () => DB::dql('select nt from note_tag nt join note n with n.id = nt.note_id where n.id = :id', ['id' => $note_id]));
} }

View File

@ -111,13 +111,14 @@ class NoteTagBlock extends Entity
return "note-tag-blocks-{$actor_id}"; return "note-tag-blocks-{$actor_id}";
} }
public static function getFromActorId(int $actor_id) public static function getByActorId(int $actor_id)
{ {
return Cache::getList(self::cacheKey($actor_id), fn () => DB::findBy('note_tag_block', ['blocker' => $actor_id])); return Cache::getList(self::cacheKey($actor_id), fn () => DB::findBy('note_tag_block', ['blocker' => $actor_id]));
} }
/** /**
* Check whether $note_tag should * Check whether $note_tag is considered blocked by one of
* $note_tag_blocks
*/ */
public static function checkBlocksNoteTag(NoteTag $note_tag, array $note_tag_blocks): bool public static function checkBlocksNoteTag(NoteTag $note_tag, array $note_tag_blocks): bool
{ {

View File

@ -355,7 +355,7 @@ abstract class Formatting
$group_matches = self::findMentionsRaw($text, '!'); $group_matches = self::findMentionsRaw($text, '!');
foreach ($group_matches as $group_match) { foreach ($group_matches as $group_match) {
$nickname = Nickname::normalize($group_match[0], check_already_used: false, check_is_allowed: false); $nickname = Nickname::normalize($group_match[0], check_already_used: false, check_is_allowed: false);
$group = Group::getFromNickname($nickname, $actor); $group = Group::getByNickname($nickname, $actor);
if (!$group instanceof Group) { if (!$group instanceof Group) {
continue; continue;