diff --git a/Web/Models/Entities/Alias.php b/Web/Models/Entities/Alias.php new file mode 100644 index 00000000..99f7baae --- /dev/null +++ b/Web/Models/Entities/Alias.php @@ -0,0 +1,34 @@ +getRecord()->owner_id; + } + + function getType(): string + { + if ($this->getOwnerId() < 0) + return "club"; + + return "user"; + } + + function getUser(): ?User + { + return (new Users)->get($this->getOwnerId()); + } + + function getClub(): ?Club + { + return (new Clubs)->get($this->getOwnerId() * -1); + } +} diff --git a/Web/Models/Entities/User.php b/Web/Models/Entities/User.php index 115bf8c2..f1045f62 100644 --- a/Web/Models/Entities/User.php +++ b/Web/Models/Entities/User.php @@ -908,6 +908,10 @@ class User extends RowModel $pClub = DatabaseConnection::i()->getContext()->table("groups")->where("shortcode", $code)->fetch(); if(!is_null($pClub)) return false; + + $pAlias = DatabaseConnection::i()->getContext()->table("aliases")->where("shortcode", $code)->fetch(); + if(!is_null($pAlias)) + return false; } $this->stateChanges("shortcode", $code); diff --git a/Web/Models/Repositories/Aliases.php b/Web/Models/Repositories/Aliases.php new file mode 100644 index 00000000..e74532a1 --- /dev/null +++ b/Web/Models/Repositories/Aliases.php @@ -0,0 +1,35 @@ +context = DB::i()->getContext(); + $this->aliases = $this->context->table("aliases"); + } + + private function toAlias(?ActiveRow $ar): ?Alias + { + return is_null($ar) ? NULL : new Alias($ar); + } + + function get(int $id): ?Alias + { + return $this->toAlias($this->aliases->get($id)); + } + + function getByShortcode(string $shortcode): ?Alias + { + return $this->toAlias($this->aliases->where("shortcode", $shortcode)->fetch()); + } +} diff --git a/Web/Models/Repositories/Clubs.php b/Web/Models/Repositories/Clubs.php index 4a1b28fe..edbe75c6 100644 --- a/Web/Models/Repositories/Clubs.php +++ b/Web/Models/Repositories/Clubs.php @@ -1,6 +1,7 @@ toClub($this->clubs->where("shortcode", $url)->fetch()); + $shortcode = $this->toClub($this->clubs->where("shortcode", $url)->fetch()); + + if ($shortcode) + return $shortcode; + + $alias = (new Aliases)->getByShortcode($url); + + if (!$alias) return NULL; + if ($alias->getType() !== "club") return NULL; + + return $alias->getClub(); } function get(int $id): ?Club diff --git a/Web/Models/Repositories/Users.php b/Web/Models/Repositories/Users.php index 9cd7d001..63e77df0 100644 --- a/Web/Models/Repositories/Users.php +++ b/Web/Models/Repositories/Users.php @@ -1,6 +1,7 @@ context = DatabaseConnection::i()->getContext(); $this->users = $this->context->table("profiles"); + $this->aliases = $this->context->table("aliases"); } private function toUser(?ActiveRow $ar): ?User @@ -28,7 +31,17 @@ class Users function getByShortURL(string $url): ?User { - return $this->toUser($this->users->where("shortcode", $url)->fetch()); + $shortcode = $this->toUser($this->users->where("shortcode", $url)->fetch()); + + if ($shortcode) + return $shortcode; + + $alias = (new Aliases)->getByShortcode($url); + + if (!$alias) return NULL; + if ($alias->getType() !== "user") return NULL; + + return $alias->getUser(); } function getByChandlerUser(ChandlerUser $user): ?User diff --git a/Web/di.yml b/Web/di.yml index f4b04650..ec867809 100644 --- a/Web/di.yml +++ b/Web/di.yml @@ -43,5 +43,6 @@ services: - openvk\Web\Models\Repositories\Topics - openvk\Web\Models\Repositories\Applications - openvk\Web\Models\Repositories\ContentSearchRepository + - openvk\Web\Models\Repositories\Aliases - openvk\Web\Models\Repositories\BannedLinks - openvk\Web\Presenters\MaintenancePresenter diff --git a/install/sqls/00031-banned-urls.sql b/install/sqls/00032-banned-urls.sql similarity index 100% rename from install/sqls/00031-banned-urls.sql rename to install/sqls/00032-banned-urls.sql diff --git a/install/sqls/00033-shortcode-aliases.sql b/install/sqls/00033-shortcode-aliases.sql new file mode 100644 index 00000000..520f0502 --- /dev/null +++ b/install/sqls/00033-shortcode-aliases.sql @@ -0,0 +1,12 @@ +CREATE TABLE `aliases` ( + `id` bigint UNSIGNED NOT NULL, + `owner_id` bigint NOT NULL, + `shortcode` varchar(36) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; + +ALTER TABLE `aliases` + ADD PRIMARY KEY (`id`); + +ALTER TABLE `aliases` + MODIFY `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT; +COMMIT;