[CORE][ENTITY] Move preferred language setting from [LocalUser] to [Actor], make [Language] language unique and make [Note] content_type not null

This commit is contained in:
Hugo Sales 2021-10-28 17:34:01 +01:00
parent e6c5312025
commit 458c09485a
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
4 changed files with 140 additions and 122 deletions

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
@ -61,8 +63,9 @@ class Actor extends Entity
private ?float $lon; private ?float $lon;
private ?int $location_id; private ?int $location_id;
private ?int $location_service; private ?int $location_service;
private \DateTimeInterface $created; private ?int $preferred_lang_id;
private \DateTimeInterface $modified; private DateTimeInterface $created;
private DateTimeInterface $modified;
public function setId(int $id): self public function setId(int $id): self
{ {
@ -185,6 +188,17 @@ class Actor extends Entity
return $this->location_service; return $this->location_service;
} }
public function setPreferredLangId(?string $preferred_lang_id): self
{
$this->preferred_lang_id = $preferred_lang_id;
return $this;
}
public function getPreferredLangId(): ?int
{
return $this->preferred_lang_id;
}
public function setCreated(DateTimeInterface $created): self public function setCreated(DateTimeInterface $created): self
{ {
$this->created = $created; $this->created = $created;
@ -222,30 +236,26 @@ class Actor extends Entity
public static function getById(int $id): ?self public static function getById(int $id): ?self
{ {
return Cache::get('actor-id-' . $id, function () use ($id) { return Cache::get('actor-id-' . $id, fn () => DB::find('actor', ['id' => $id]));
return DB::find('actor', ['id' => $id]);
});
} }
public static function getNicknameById(int $id): string public static function getNicknameById(int $id): string
{ {
return Cache::get('actor-nickname-id-' . $id, function () use ($id) { return Cache::get('actor-nickname-id-' . $id, fn () => self::getById($id)->getNickname());
return self::getById($id)->getNickname();
});
} }
public static function getFullnameById(int $id): string public static function getFullnameById(int $id): string
{ {
return Cache::get('actor-fullname-id-' . $id, function () use ($id) { return Cache::get('actor-fullname-id-' . $id, fn () => self::getById($id)->getFullname());
return self::getById($id)->getFullname();
});
} }
public function getSelfTags(bool $_test_force_recompute = false): array public function getSelfTags(bool $_test_force_recompute = false): array
{ {
return Cache::get('selftags-' . $this->id, return Cache::get(
'selftags-' . $this->id,
fn () => DB::findBy('actor_tag', ['tagger' => $this->id, 'tagged' => $this->id]), fn () => DB::findBy('actor_tag', ['tagger' => $this->id, 'tagged' => $this->id]),
beta: $_test_force_recompute ? INF : 1.0); beta: $_test_force_recompute ? \INF : 1.0,
);
} }
public function setSelfTags(array $tags, array $existing): void public function setSelfTags(array $tags, array $existing): void
@ -253,7 +263,7 @@ class Actor extends Entity
$tag_existing = F\map($existing, fn ($pt) => $pt->getTag()); $tag_existing = F\map($existing, fn ($pt) => $pt->getTag());
$tag_to_add = array_diff($tags, $tag_existing); $tag_to_add = array_diff($tags, $tag_existing);
$tag_to_remove = array_diff($tag_existing, $tags); $tag_to_remove = array_diff($tag_existing, $tags);
$pt_to_remove = F\filter($existing, fn ($pt) => in_array($pt->getTag(), $tag_to_remove)); $pt_to_remove = F\filter($existing, fn ($pt) => \in_array($pt->getTag(), $tag_to_remove));
foreach ($tag_to_add as $tag) { foreach ($tag_to_add as $tag) {
$pt = ActorTag::create(['tagger' => $this->id, 'tagged' => $this->id, 'tag' => $tag]); $pt = ActorTag::create(['tagger' => $this->id, 'tagged' => $this->id, 'tag' => $tag]);
DB::persist($pt); DB::persist($pt);
@ -267,20 +277,28 @@ class Actor extends Entity
public function getSubscribersCount() public function getSubscribersCount()
{ {
return Cache::get('followers-' . $this->id, return Cache::get(
'followers-' . $this->id,
function () { function () {
return DB::dql('select count(f) from App\Entity\Follow f where f.followed = :followed', return DB::dql(
['followed' => $this->id])[0][1] - 1; // Remove self follow 'select count(f) from App\Entity\Follow f where f.followed = :followed',
}); ['followed' => $this->id],
)[0][1] - 1; // Remove self follow
},
);
} }
public function getSubscriptionsCount() public function getSubscriptionsCount()
{ {
return Cache::get('followed-' . $this->id, return Cache::get(
'followed-' . $this->id,
function () { function () {
return DB::dql('select count(f) from App\Entity\Follow f where f.follower = :follower', return DB::dql(
['follower' => $this->id])[0][1] - 1; // Remove self follow 'select count(f) from App\Entity\Follow f where f.follower = :follower',
}); ['follower' => $this->id],
)[0][1] - 1; // Remove self follow
},
);
} }
public function isPerson(): bool public function isPerson(): bool
@ -302,14 +320,17 @@ class Actor extends Entity
{ {
// Will throw exception on invalid input. // Will throw exception on invalid input.
$nickname = Nickname::normalize($nickname, check_already_used: false); $nickname = Nickname::normalize($nickname, check_already_used: false);
return Cache::get('relative-nickname-' . $nickname . '-' . $this->getId(), return Cache::get(
fn () => DB::dql('select a from actor a where ' . 'relative-nickname-' . $nickname . '-' . $this->getId(),
'a.id in (select followed from follow f join actor a on f.followed = a.id where and f.follower = :actor_id and a.nickname = :nickname) or' . fn () => DB::dql(
'a.id in (select follower from follow f join actor a on f.follower = a.id where and f.followed = :actor_id and a.nickname = :nickname) or' . 'select a from actor a where '
'a.nickname = :nickname' . . 'a.id in (select followed from follow f join actor a on f.followed = a.id where and f.follower = :actor_id and a.nickname = :nickname) or'
'limit 1', . 'a.id in (select follower from follow f join actor a on f.follower = a.id where and f.followed = :actor_id and a.nickname = :nickname) or'
['nickname' => $nickname, 'actor_id' => $this->getId()] . 'a.nickname = :nickname'
)); . 'limit 1',
['nickname' => $nickname, 'actor_id' => $this->getId()],
),
);
} }
public function getUri(int $type = Router::ABSOLUTE_PATH): string public function getUri(int $type = Router::ABSOLUTE_PATH): string
@ -337,9 +358,15 @@ class Actor extends Entity
return $aliases; return $aliases;
} }
public function getPreferredLanguageChoice()
{
$lang_id = $this->getPreferredLangId();
return Cache::get("language-{$lang_id}", fn () => (string) DB::findOneBy('language', ['id' => $lang_id]));
}
public static function schemaDef(): array public static function schemaDef(): array
{ {
$def = [ return [
'name' => 'actor', 'name' => 'actor',
'description' => 'local and remote users, groups and bots are actors, for instance', 'description' => 'local and remote users, groups and bots are actors, for instance',
'fields' => [ 'fields' => [
@ -354,6 +381,7 @@ class Actor extends Entity
'lon' => ['type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'longitude'], 'lon' => ['type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'longitude'],
'location_id' => ['type' => 'int', 'description' => 'location id if possible'], 'location_id' => ['type' => 'int', 'description' => 'location id if possible'],
'location_service' => ['type' => 'int', 'description' => 'service used to obtain location id'], 'location_service' => ['type' => 'int', 'description' => 'service used to obtain location id'],
'preferred_lang_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Language.id', 'multiplicity' => 'one to many', 'description' => 'preferred language'],
'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'], 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'], 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
], ],
@ -365,7 +393,5 @@ class Actor extends Entity
'actor_fulltext_idx' => ['nickname', 'fullname', 'location', 'bio', 'homepage'], 'actor_fulltext_idx' => ['nickname', 'fullname', 'location', 'bio', 'homepage'],
], ],
]; ];
return $def;
} }
} }

View File

@ -24,6 +24,7 @@ declare(strict_types = 1);
namespace App\Entity; namespace App\Entity;
use App\Core\Entity; use App\Core\Entity;
use DateTimeInterface;
/** /**
* Entity for languages * Entity for languages
@ -41,7 +42,7 @@ class Language extends Entity
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
private int $id; private int $id;
private string $language; private string $language;
private \DateTimeInterface $created; private DateTimeInterface $created;
public function setId(int $id): self public function setId(int $id): self
{ {
@ -78,6 +79,11 @@ class Language extends Entity
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
// }}} Autocode // }}} Autocode
public function __toString()
{
return $this->getLanguage();
}
public static function schemaDef(): array public static function schemaDef(): array
{ {
return [ return [
@ -89,6 +95,9 @@ class Language extends Entity
'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'], 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
], ],
'primary key' => ['id'], 'primary key' => ['id'],
'unique keys' => [
'language_language_uniq' => ['language'],
],
'indexes' => [ 'indexes' => [
'language_idx' => ['language'], 'language_idx' => ['language'],
], ],

View File

@ -57,7 +57,6 @@ class LocalUser extends Entity implements UserInterface
private ?string $outgoing_email; private ?string $outgoing_email;
private ?string $incoming_email; private ?string $incoming_email;
private ?bool $is_email_verified; private ?bool $is_email_verified;
private ?int $preferred_language;
private ?string $timezone; private ?string $timezone;
private ?PhoneNumber $phone_number; private ?PhoneNumber $phone_number;
private ?int $sms_carrier; private ?int $sms_carrier;
@ -135,17 +134,6 @@ class LocalUser extends Entity implements UserInterface
return $this->is_email_verified; return $this->is_email_verified;
} }
public function setPreferredLanguage(?string $preferred_language): self
{
$this->preferred_language = $preferred_language;
return $this;
}
public function getPreferredLanguage(): ?int
{
return $this->preferred_language;
}
public function setTimezone(?string $timezone): self public function setTimezone(?string $timezone): self
{ {
$this->timezone = $timezone; $this->timezone = $timezone;
@ -259,25 +247,7 @@ class LocalUser extends Entity implements UserInterface
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
// }}} Autocode // }}} Autocode
public function getActor() // {{{ Authentication
{
return DB::find('actor', ['id' => $this->id]);
}
/**
* Returns the roles granted to the user
*/
public function getRoles()
{
return UserRoles::toArray($this->getActor()->getRoles());
}
/**
* Returns the password used to authenticate the user.
*
* Implemented in the auto code
*/
/** /**
* Returns the salt that was originally used to encode the password. * Returns the salt that was originally used to encode the password.
* BCrypt and Argon2 generate their own salts * BCrypt and Argon2 generate their own salts
@ -287,14 +257,6 @@ class LocalUser extends Entity implements UserInterface
return null; return null;
} }
/**
* Returns the username used to authenticate the user.
*/
public function getUsername()
{
return $this->nickname;
}
/** /**
* Removes sensitive data from the user. * Removes sensitive data from the user.
* *
@ -305,19 +267,6 @@ class LocalUser extends Entity implements UserInterface
{ {
} }
public static function getByNickname(string $nickname): ?self
{
return Cache::get("user-nickname-{$nickname}", fn () => DB::findOneBy('local_user', ['nickname' => $nickname]));
}
/**
* @return self Returns self if email found
*/
public static function getByEmail(string $email): ?self
{
return Cache::get("user-email-{$email}", fn () => DB::findOneBy('local_user', ['or' => ['outgoing_email' => $email, 'incoming_email' => $email]]));
}
/** /**
* When authenticating, check a user's password in a timing safe * When authenticating, check a user's password in a timing safe
* way. Will update the password by rehashing if deemed necessary * way. Will update the password by rehashing if deemed necessary
@ -380,6 +329,41 @@ class LocalUser extends Entity implements UserInterface
throw new Exception('Unsupported or unsafe hashing algorithm requested'); throw new Exception('Unsupported or unsafe hashing algorithm requested');
} }
} }
// }}} Authentication
public function getActor()
{
return DB::find('actor', ['id' => $this->id]);
}
/**
* Returns the roles granted to the user
*/
public function getRoles()
{
return UserRoles::toArray($this->getActor()->getRoles());
}
/**
* Returns the username used to authenticate the user. Part of the Symfony UserInterface
*/
public function getUsername()
{
return $this->nickname;
}
public static function getByNickname(string $nickname): ?self
{
return Cache::get("user-nickname-{$nickname}", fn () => DB::findOneBy('local_user', ['nickname' => $nickname]));
}
/**
* @return self Returns self if email found
*/
public static function getByEmail(string $email): ?self
{
return Cache::get("user-email-{$email}", fn () => DB::findOneBy('local_user', ['or' => ['outgoing_email' => $email, 'incoming_email' => $email]]));
}
public static function schemaDef(): array public static function schemaDef(): array
{ {
@ -393,7 +377,6 @@ class LocalUser extends Entity implements UserInterface
'outgoing_email' => ['type' => 'varchar', 'length' => 191, 'description' => 'email address for password recovery, notifications, etc.'], 'outgoing_email' => ['type' => 'varchar', 'length' => 191, 'description' => 'email address for password recovery, notifications, etc.'],
'incoming_email' => ['type' => 'varchar', 'length' => 191, 'description' => 'email address for post-by-email'], 'incoming_email' => ['type' => 'varchar', 'length' => 191, 'description' => 'email address for post-by-email'],
'is_email_verified' => ['type' => 'bool', 'default' => false, 'description' => 'Whether the user opened the comfirmation email'], 'is_email_verified' => ['type' => 'bool', 'default' => false, 'description' => 'Whether the user opened the comfirmation email'],
'preferred_language' => ['type' => 'int', 'foreign key' => true, 'target' => 'Language.id', 'multiplicity' => 'one to many', 'description' => 'preferred language'],
'timezone' => ['type' => 'varchar', 'length' => 50, 'description' => 'timezone'], 'timezone' => ['type' => 'varchar', 'length' => 50, 'description' => 'timezone'],
'phone_number' => ['type' => 'phone_number', 'description' => 'phone number'], 'phone_number' => ['type' => 'phone_number', 'description' => 'phone number'],
'sms_carrier' => ['type' => 'int', 'foreign key' => true, 'target' => 'SmsCarrier.id', 'multiplicity' => 'one to one', 'description' => 'foreign key to sms_carrier'], 'sms_carrier' => ['type' => 'int', 'foreign key' => true, 'target' => 'SmsCarrier.id', 'multiplicity' => 'one to one', 'description' => 'foreign key to sms_carrier'],

View File

@ -288,7 +288,7 @@ class Note extends Entity
public function getReplies(): array public function getReplies(): array
{ {
return Cache::getList('note-replies-' . $this->id, fn () => DB::dql('select n from App\Entity\Note n where n.reply_to = :id', ['id' => $this->id])); return Cache::getList('note-replies-' . $this->id, fn () => DB::dql('select n from note n where n.reply_to = :id', ['id' => $this->id]));
} }
public function getReplyToNickname(): ?string public function getReplyToNickname(): ?string
@ -343,7 +343,7 @@ class Note extends Entity
'id' => ['type' => 'serial', 'not null' => true], 'id' => ['type' => 'serial', 'not null' => true],
'actor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'who made the note'], 'actor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'who made the note'],
'content' => ['type' => 'text', 'description' => 'note content'], 'content' => ['type' => 'text', 'description' => 'note content'],
'content_type' => ['type' => 'varchar', 'default' => 'text/plain', 'length' => 129, 'description' => 'A note can be written in a multitude of formats such as text/plain, text/markdown, application/x-latex, and text/html'], 'content_type' => ['type' => 'varchar', 'not null' => true, 'default' => 'text/plain', 'length' => 129, 'description' => 'A note can be written in a multitude of formats such as text/plain, text/markdown, application/x-latex, and text/html'],
'rendered' => ['type' => 'text', 'description' => 'rendered note content, so we can keep the microtags (if not local)'], 'rendered' => ['type' => 'text', 'description' => 'rendered note content, so we can keep the microtags (if not local)'],
'reply_to' => ['type' => 'int', 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'note replied to, null if root of a conversation'], 'reply_to' => ['type' => 'int', 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'note replied to, null if root of a conversation'],
'is_local' => ['type' => 'bool', 'description' => 'was this note generated by a local actor'], 'is_local' => ['type' => 'bool', 'description' => 'was this note generated by a local actor'],