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

Commit

Permalink
feat(Invite): Add base invite table
Browse files Browse the repository at this point in the history
1. Add Table `user_invitations` to store user temp invite, ALTER table `users`
with new column `invite` to store user unexpired invites count.
2. Table name changed from `users_{confirm,session_log}` to `user_{confirm,session_log}`
3. Fix user privilege check may throw exception, if config key is not exist,
The privilege check will compare with `class=1`.
4. Improve with the info get and cache of true_{uploaded,downloaded} in User info.
5. Add user invites (include temp invites) information in UserTrait.
6. Placeholder of new Route `/collections` in nav

BREAKING CHANGE: Some Table added or name changed
  • Loading branch information
Rhilip committed Jun 5, 2019
1 parent 8b887f8 commit c556644
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 57 deletions.
4 changes: 2 additions & 2 deletions apps/components/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function loadUserFromPasskey()
public function deleteUserThisSession()
{
$success = app()->redis->zRem($this->sessionSaveKey, $this->_userSessionId);
app()->pdo->createCommand('UPDATE `users_session_log` SET `expired` = 1 WHERE sid = :sid')->bindParams([
app()->pdo->createCommand('UPDATE `user_session_log` SET `expired` = 1 WHERE sid = :sid')->bindParams([
'sid' => $this->_userSessionId
])->execute();
app()->cookie->delete(Constant::cookie_name);
Expand Down Expand Up @@ -115,7 +115,7 @@ public function inBookmarkList($tid = null)
public function isPrivilege($require_class)
{
if (is_string($require_class)) {
$require_class = app()->config->get('authority.' . $require_class);
$require_class = app()->config->get('authority.' . $require_class, false) ?: 1;
}

return $this->class >= $require_class;
Expand Down
61 changes: 47 additions & 14 deletions apps/components/User/UserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ trait UserTrait
private $last_tracker_ip;

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

private $invites;
private $temp_invites;

protected $peer_status;
protected $infoCacheKey;

Expand Down Expand Up @@ -214,14 +219,15 @@ public function getLastTrackerIp($readable = true)
public function getUploaded($real = false)
{
if ($real) {
$upload = app()->redis->hGet($this->infoCacheKey, 'true_uploaded');
if (false === $upload) {
$upload = app()->pdo->createCommand('SELECT SUM(`true_uploaded`) FROM `snatched` WHERE `user_id` = :uid')->bindParams([
"uid" => $this->id
])->queryScalar() ?? 0;
app()->redis->hSet($this->infoCacheKey, 'true_uploaded', $upload);
if (is_null($this->true_uploaded)) {
$this->true_uploaded = app()->redis->hGet($this->infoCacheKey, 'true_uploaded');
if (false === $this->true_uploaded) {
$this->true_uploaded = app()->pdo->createCommand('SELECT SUM(`true_downloaded`) FROM `snatched` WHERE `user_id` = :uid')->bindParams([
"uid" => $this->id
])->queryScalar() ?? 0;
app()->redis->hSet($this->infoCacheKey, 'true_uploaded', $this->true_uploaded);
}
}
return $upload;
}
return $this->uploaded;
}
Expand All @@ -233,14 +239,16 @@ public function getUploaded($real = false)
public function getDownloaded($real = false)
{
if ($real) {
$download = app()->redis->hGet($this->infoCacheKey, 'true_downloaded');
if (false === $download) {
$download = app()->pdo->createCommand('SELECT SUM(`true_downloaded`) FROM `snatched` WHERE `user_id` = :uid')->bindParams([
"uid" => $this->id
])->queryScalar() ?? 0;
app()->redis->hSet($this->infoCacheKey, 'true_downloaded', $download);
if (is_null($this->true_downloaded)) {
$this->true_downloaded = app()->redis->hGet($this->infoCacheKey, 'true_downloaded');
if (false === $this->true_downloaded) {
$this->true_downloaded = app()->pdo->createCommand('SELECT SUM(`true_downloaded`) FROM `snatched` WHERE `user_id` = :uid')->bindParams([
"uid" => $this->id
])->queryScalar() ?? 0;
app()->redis->hSet($this->infoCacheKey, 'true_downloaded', $this->true_downloaded);
}
}
return $download;
return $this->true_downloaded;
}
return $this->downloaded;
}
Expand Down Expand Up @@ -306,4 +314,29 @@ public function getPasskey()
{
return $this->passkey;
}

/**
* @return mixed
*/
public function getInvites()
{
return $this->invites;
}

/**
* @return mixed
*/
public function getTempInvites()
{
if (is_null($this->temp_invites)) {
$this->temp_invites = app()->redis->hGet($this->infoCacheKey, 'temp_invite');
if (false === $this->temp_invites) {
$this->temp_invites = app()->pdo->createCommand('SELECT SUM(`qty`) FROM `user_invitations` WHERE `user_id` = :uid AND `qty` > 0 AND `expire_at` < NOW()')->bindParams([
"uid" => $this->id
])->queryScalar() ?? 0;
app()->redis->hSet($this->infoCacheKey, 'temp_invite', $this->temp_invites);
}
}
return $this->temp_invites;
}
}
4 changes: 2 additions & 2 deletions apps/controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function actionSessions()
$to_del_session = app()->request->post('session');

// expired it from Database first
app()->pdo->createCommand('UPDATE `users_session_log` SET `expired` = 1 WHERE uid = :uid AND sid = :sid')->bindParams([
app()->pdo->createCommand('UPDATE `user_session_log` SET `expired` = 1 WHERE uid = :uid AND sid = :sid')->bindParams([
'uid' => app()->user->getId(), 'sid' => $to_del_session
])->execute();
$success = app()->pdo->getRowCount();
Expand All @@ -56,7 +56,7 @@ public function actionSessions()
}
}

$sessions = app()->pdo->createCommand('SELECT sid,login_at,login_ip,user_agent,last_access_at FROM users_session_log WHERE uid = :uid and expired = 0')->bindParams([
$sessions = app()->pdo->createCommand('SELECT sid,login_at,login_ip,user_agent,last_access_at FROM user_session_log WHERE uid = :uid and expired = 0')->bindParams([
'uid' => app()->user->getId()
])->queryAll();
return $this->render('user/sessions', ['sessions' => $sessions]);
Expand Down
1 change: 1 addition & 0 deletions apps/lang/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class en
const nav_index = 'Index';
const nav_forums = 'Forums';
const nav_torrents = 'Torrents';
const nav_collections = 'Collections';
const nav_requests = 'Requests';
const nav_upload = 'Upload';
const nav_subtitles = 'Subtitles';
Expand Down
8 changes: 4 additions & 4 deletions apps/models/form/UserConfirmForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public static function callbackRules()
}

/**
* Verity The confirm secret and action exist in table `users_confirm` or not
* Verity The confirm secret and action exist in table `user_confirm` or not
*/
protected function validConfirmSecret()
{
$record = app()->pdo->createCommand(
'SELECT `users_confirm`.`id`,`users_confirm`.`uid`,`users`.`status` FROM `users_confirm`
LEFT JOIN `users` ON `users`.`id` = `users_confirm`.`uid`
'SELECT `user_confirm`.`id`,`user_confirm`.`uid`,`users`.`status` FROM `user_confirm`
LEFT JOIN `users` ON `users`.`id` = `user_confirm`.`uid`
WHERE `serect` = :serect AND `action` = :action AND used = 0 LIMIT 1;')->bindParams([
'serect' => $this->secret , 'action' => $this->action
])->queryOne();
Expand All @@ -64,7 +64,7 @@ protected function validConfirmSecret()
}

protected function update_confirm_status() {
app()->pdo->createCommand('UPDATE `users_confirm` SET `used` = 1 WHERE id = :id')->bindParams([
app()->pdo->createCommand('UPDATE `user_confirm` SET `used` = 1 WHERE id = :id')->bindParams([
'id' => $this->id
])->execute();
}
Expand Down
4 changes: 2 additions & 2 deletions apps/models/form/UserLoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ public function createUserSession()
$userSessionId = StringHelper::getRandomString($this->sessionLength - 1);
$userSessionId = ($this->securelogin === 'yes' ? '1' : '0') . $userSessionId;

$count = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `users_session_log` WHERE sid = :sid')->bindParams([
$count = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `user_session_log` WHERE sid = :sid')->bindParams([
'sid' => $userSessionId
])->queryScalar();
} while ($count != 0);

// store user login information , ( for example `login ip`,`user_agent`,`last activity at` )
app()->pdo->createCommand('INSERT INTO `users_session_log`(`uid`, `sid`, `login_ip`, `user_agent` , `last_access_at`) ' .
app()->pdo->createCommand('INSERT INTO `user_session_log`(`uid`, `sid`, `login_ip`, `user_agent` , `last_access_at`) ' .
'VALUES (:uid,:sid,INET6_ATON(:login_ip),:ua, NOW())')->bindParams([
'uid' => $userId, 'sid' => $userSessionId,
'login_ip' => app()->request->getClientIp(),
Expand Down
2 changes: 1 addition & 1 deletion apps/models/form/UserRecoverForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function flush() {

// Send user email to get comfirm link
$confirm_key = StringHelper::getRandomString(32);
app()->pdo->createCommand('INSERT INTO `users_confirm` (`uid`,`serect`,`action`) VALUES (:uid,:serect,:action)')->bindParams([
app()->pdo->createCommand('INSERT INTO `user_confirm` (`uid`,`serect`,`action`) VALUES (:uid,:serect,:action)')->bindParams([
'uid' => $user_info['id'], 'serect' => $confirm_key, 'action' => $this->_action
])->execute();
$confirm_url = app()->request->root() . '/auth/confirm?' . http_build_query([
Expand Down
2 changes: 1 addition & 1 deletion apps/models/form/UserRegisterForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public function flush()

if ($this->confirm_way == 'email') {
$confirm_key = StringHelper::getRandomString(32);
app()->pdo->createCommand('INSERT INTO `users_confirm` (`uid`,`serect`,`action`) VALUES (:uid,:serect,:action)')->bindParams([
app()->pdo->createCommand('INSERT INTO `user_confirm` (`uid`,`serect`,`action`) VALUES (:uid,:serect,:action)')->bindParams([
'uid' => $this->id, 'serect' => $confirm_key, 'action' => $this->_action
])->execute();
$confirm_url = app()->request->root() . '/auth/confirm?' . http_build_query([
Expand Down
2 changes: 1 addition & 1 deletion apps/task/CronTabTimer.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected function clean_expired_session() {

$expired_sessions = app()->redis->zRangeByScore('Site:Sessions:to_expire', 0, $timenow);
foreach ($expired_sessions as $session) {
app()->pdo->createCommand('UPDATE `users_session_log` SET `expired` = 1 WHERE sid = :sid')->createCommand([
app()->pdo->createCommand('UPDATE `user_session_log` SET `expired` = 1 WHERE sid = :sid')->bindParams([
'sid' => $session
])->execute();
}
Expand Down
25 changes: 15 additions & 10 deletions apps/views/layout/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,21 @@
<div class="collapse navbar-collapse navbar-collapse-custom">
<ul class="nav navbar-nav nav-justified">
<li<?= $this->uri('/index', ' class="active"') ?>><a href="/"><?= __('nav_index') ?></a></li>
<li<?= $this->uri('/forums', ' class="active"'); ?>><a href="/forums"><?= __('nav_forums') ?></a></li>
<li<?= $this->uri('/forums', ' class="active"'); ?>><a href="/forums"><?= __('nav_forums') ?></a></li> <!-- TODO -->
<li<?= $this->uri('/collections', ' class="active"'); ?>><a href="/collections"><?= __('nav_collections') ?></a></li> <!-- TODO -->
<li<?= $this->uri('/torrents', ' class="active"'); ?>><a href="/torrents"><?= __('nav_torrents') ?></a></li>
<li<?= $this->uri('/torrent/upload', ' class="active"'); ?>><a href="/torrent/upload"><?= __('nav_upload') ?></a></li>
<li<?= $this->uri('/torrents/request', ' class="active"'); ?>><a href="/torrents/request"><?= __('nav_requests') ?></a></li>
<li<?= $this->uri('/subtitles', ' class="active"'); ?>><a href="/subtitles"><?= __('nav_subtitles') ?></a></li>
<li<?= $this->uri('/site/rules', ' class="active"'); ?>><a href="/site/rules"><?= __('nav_rules') ?></a></li>
<li<?= $this->uri('/site/staff', ' class="active"'); ?>><a href="/site/staff"><?= __('nav_staff') ?></a></li>
<li<?= $this->uri('/torrents/request', ' class="active"'); ?>><a href="/torrents/request"><?= __('nav_requests') ?></a></li> <!-- TODO -->
<li<?= $this->uri('/subtitles', ' class="active"'); ?>><a href="/subtitles"><?= __('nav_subtitles') ?></a></li> <!-- TODO -->
<li<?= $this->uri('/site/rules', ' class="active"'); ?>><a href="/site/rules"><?= __('nav_rules') ?></a></li> <!-- TODO -->
<li<?= $this->uri('/site/staff', ' class="active"'); ?>><a href="/site/staff"><?= __('nav_staff') ?></a></li> <!-- TODO -->
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><?= __('nav_more') ?> <b class="caret"></b></a>
<ul class="dropdown-menu" role="menu">
<li<?= $this->uri('/site/topten', ' class="active"'); ?>><a href="/site/topten"><?= __('nav_topten') ?></a></li>
<ul class="dropdown-menu" role="menu"><!-- FIXME class="active" in dropdown-munu -->
<li<?= $this->uri('/site/topten', ' class="active"'); ?>><a href="/site/topten"><?= __('nav_topten') ?></a></li> <!-- TODO -->
<li class="divider"></li>
<li<?= $this->uri('/site/stats', ' class="active"'); ?>><a href="/site/stats"><?= __('nav_stats') ?></a></li>
<li<?= $this->uri('/site/log', ' class="active"'); ?>><a href="/site/log"><?= __('nav_log') ?></a></li>
<li<?= $this->uri('/site/stats', ' class="active"'); ?>><a href="/site/stats"><?= __('nav_stats') ?></a></li> <!-- TODO -->
<li<?= $this->uri('/site/log', ' class="active"'); ?>><a href="/site/log"><?= __('nav_log') ?></a></li> <!-- TODO -->
</ul>
</li>
</ul> <!-- END .navbar-nav -->
Expand All @@ -82,7 +83,11 @@
<?php endif; ?>
<span data-item="favour"><!--suppress HtmlUnknownTarget -->[<a href="/torrents/favour">Favour</a>]</span>
<span data-item="invite" data-invite="">
<span class="color-invite">Invite [<a href="/user/invite">Send</a>]: </span> 3 <!-- TODO -->
<span class="color-invite">Invite [<a href="/user/invite">Send</a>]: </span>
<?= app()->user->getInvites() ?>
<?php if (app()->user->getTempInvites() > 0): ?>
(<?= app()->user->getTempInvites() ?>)
<?php endif; ?>
</span>
<span data-item="bonus" data-bonus="">
<span class="color-bonus">Bonus: </span> <a href="#">2345234</a> <!-- TODO -->
Expand Down
Loading

0 comments on commit c556644

Please sign in to comment.