Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
perf(User): Add Entity\User\AbstractUserInterface
Browse files Browse the repository at this point in the history
1. Add `Entity\User\AbstractUserInterface`, `Entity\User\AbstractUser`
2. Make `AttributesImportUtils` trait function as part method for BaseObject
  • Loading branch information
Rhilip committed Jan 22, 2020
1 parent 547c772 commit 1d8e9e4
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 182 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- **README:** fix typo of start command (657c71a)
- **Redis:** Fix phpdoc for Redis Component (70ac399)
- **Sponsor:** Add Sponsor `MeiHeZi` (ae612e0)
- **phpstorm:** Add `.phpstorm.meat.php` for config() function (407bb66)

### Feat
- **Torrent:** Per-add torrent edit (f06b342)
Expand All @@ -18,6 +19,7 @@
- **Config:** Fix JSON type config return False (1129008)
- **Cron:** Fix components lost in CronTabProcess (1ced4bf)
- **Redis:** Fix wrong type of Redis call function hMset() to hMSet() (a163150)
- **Route:** Fix user can access maintenance page even not on maintenance status (b29c2f6)
- **Tracker:** Fix typo in TrackerController (323c8ec)

### Perf
Expand All @@ -29,6 +31,7 @@
- **Entity:** Move Class Torrent,User to namespace App\Entity (7814d88)

### Style
- **cs-fixer:** Update Composer config and php-cs-fixer whole project (e734812)
- **gitignore:** Add .php_cs.cache to .gitignore (15a2a15)


Expand Down
2 changes: 1 addition & 1 deletion application/Components/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function loadCurUser($grant = 'cookies')

if ($user_id !== false && is_int($user_id) && $user_id > 0) {
$user_id = intval($user_id);
$curuser = app()->site->getUser($user_id);
$curuser = app()->site->getUser($user_id, true);
if ($curuser->getStatus() !== UserStatus::DISABLED) { // user status shouldn't be disabled
return $curuser;
}
Expand Down
14 changes: 10 additions & 4 deletions application/Components/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,21 @@ public function getTorrent($tid)
}

/**
* @param $uid
* @return Entity\User|bool return False means this user is not exist
* @param int $uid
* @param bool $cur
* @return Entity\User\AbstractUserInterface|bool return False means this user is not exist
*/
public function getUser($uid)
public function getUser($uid, $cur = false)
{
if (array_key_exists($uid, $this->users)) {
$user = $this->users[$uid];
} else {
$user = new Entity\User($uid); // TODO Handing if this user id does not exist
if ($cur) {
$user = new Entity\User($uid);
} else {
$user = new Entity\User\AbstractUser($uid);
}
// TODO Handing if this user id does not exist
$this->users[$uid] = $user;
}
return $user;
Expand Down
2 changes: 2 additions & 0 deletions application/Controllers/Api/v1/TorrentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public function actionNfoFileContent()
$ret
);
}
} else {
return $this->buildMethodFailMsg('GET');
}
}
}
53 changes: 27 additions & 26 deletions application/Entity/Torrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,49 @@

namespace App\Entity;

use App\Entity\User\AbstractUserInterface;
use App\Libraries\Constant;

use Rid\Base\BaseObject;
use Rid\Utils;
use Rid\Exceptions\NotFoundException;

class Torrent
class Torrent extends BaseObject
{
use Utils\AttributesImportUtils;
use Utils\ClassValueCacheUtils;

private $id = null;
protected $id = null;

private $owner_id;
private $info_hash;
protected $owner_id;
protected $info_hash;

private $status;
protected $status;

private $added_at;
protected $added_at;

private $complete;
private $incomplete;
private $downloaded;
private $comments;
protected $complete;
protected $incomplete;
protected $downloaded;
protected $comments;

private $title;
private $subtitle;
private $category;
private $descr;
private $uplver;
private $hr;
protected $title;
protected $subtitle;
protected $category;
protected $descr;
protected $uplver;
protected $hr;

private $nfo;
protected $nfo;

private $tags;
private $pinned_tags;
protected $tags;
protected $pinned_tags;

private $torrent_name;
private $torrent_type;
private $torrent_size;
private $torrent_structure;
protected $torrent_name;
protected $torrent_type;
protected $torrent_size;
protected $torrent_structure;

private $team;
protected $team;

protected $comment_perpage = 10; // FIXME

Expand Down Expand Up @@ -89,7 +90,7 @@ public function getOwnerId(): int
return $this->owner_id;
}

public function getOwner(): User
public function getOwner(): AbstractUserInterface
{
return app()->site->getUser($this->owner_id);
}
Expand Down
132 changes: 17 additions & 115 deletions application/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,97 +2,31 @@

namespace App\Entity;

use Rid\Utils\AttributesImportUtils;
use Rid\Utils\ClassValueCacheUtils;
use App\Entity\User\AbstractUser;

use App\Libraries\Constant;

class User
class User extends AbstractUser
{
use AttributesImportUtils;
use ClassValueCacheUtils;

private $id;
private $username;
private $email;
private $status;
private $class = 0;
private $passkey;
private $avatar;
private $lang;

private $uploadpos;
private $downloadpos;

private $uploaded;
private $true_uploaded;
private $downloaded;
private $true_downloaded;
private $seedtime;
private $leechtime;

private $bonus_seeding;
private $bonus_other;
protected $lang;
protected $passkey;

private $invites;
protected $uploadpos;
protected $downloadpos;

protected $infoCacheKey;
protected $uploaded;
protected $downloaded;
protected $true_uploaded;
protected $true_downloaded;
protected $seedtime;
protected $leechtime;

protected function getCacheNameSpace(): string
{
return Constant::userContent($this->id);
}

public function __construct($id = null)
{
$this->infoCacheKey = Constant::userContent($id);
$self = app()->redis->hGetAll($this->infoCacheKey);
if (empty($self) || !isset($self['id'])) {
if (app()->redis->zScore(Constant::invalidUserIdZset, $id) === false) {
$self = app()->pdo->createCommand('SELECT id, username, email, status, class, passkey, uploadpos, downloadpos, uploaded, downloaded, seedtime, leechtime, avatar, bonus_seeding, bonus_other, lang, invites FROM `users` WHERE id = :id LIMIT 1;')->bindParams([
'id' => $id
])->queryOne();
if (false === $self) {
app()->redis->zAdd(Constant::invalidUserIdZset, time() + 3600, $id);
} else {
app()->redis->hMSet($this->infoCacheKey, $self);
app()->redis->expire($this->infoCacheKey, 15 * 60); // Cache This User Detail for 15 minutes
}
} else {
$self = false; // It means this user id is invalid ( And already checked in last 60 minutes )
}
}
protected $bonus_seeding;
protected $bonus_other;

if ($self === false) {
return false;
}

$this->importAttributes($self);
}

public function getId(): int
{
return $this->id;
}

public function getUsername(): string
{
return $this->username;
}
protected $invites;

public function getEmail(): string
{
return $this->email;
}

public function getStatus(): string
{
return $this->status;
}

public function getClass(): int
public function getLang(): string
{
return $this->class;
return $this->lang;
}

public function getPasskey(): string
Expand All @@ -110,33 +44,6 @@ public function getDownloadpos(): bool
return (bool)$this->downloadpos;
}

public function getAvatar(array $opts = []): string
{
if (config('user.avatar_provider') === 'gravatar') {
/** Get a Gravatar URL for a specified email address.
*
* @param int|string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
* @param string $d Default imageset to use [ 404 | mp | identicon | monsterid | wavatar ]
* @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
*/
$url = config('gravatar.base_url') . md5(strtolower(trim($this->email)));
$url .= '?' . http_build_query([
's' => $opts['s'] ?? 80,
'd' => $opts['d'] ?? config('gravatar.default_fallback') ?? 'identicon',
'r' => $opts['r'] ?? config('gravatar.maximum_rating') ?? 'g'
], null, '&', PHP_QUERY_RFC3986);
return $url;
}/* elseif (config('user.avatar_provider') === 'remote') {
// For example : another Image Hosting
}*/ else { // config('user.avatar_provider') === 'local'
if ($this->avatar == '') {
$this->avatar = '/static/avatar/default_avatar.jpg';
}
}

return $this->avatar;
}

public function getUploaded(): int
{
return $this->uploaded;
Expand Down Expand Up @@ -242,11 +149,6 @@ public function getActivePartial()
return $this->getPeerStatus('partial');
}

public function getLang(): string
{
return $this->lang;
}

public function getBonus(): float
{
return $this->bonus_seeding + $this->bonus_other;
Expand Down
Loading

0 comments on commit 1d8e9e4

Please sign in to comment.