From 43156f7fc5bdd103e8a2275f6e86ffd4946eaa44 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Sun, 12 Jun 2022 15:15:09 +0800 Subject: [PATCH] get_username() support in laravel --- app/Console/Commands/Test.php | 7 +++-- app/Models/Torrent.php | 2 +- app/Models/User.php | 29 ++++++++++++++--- app/Repositories/BaseRepository.php | 19 ++++++++++++ include/functions.php | 48 ++++++++++++++++++++--------- resources/lang/en/user.php | 9 ++++++ resources/lang/zh_CN/user.php | 9 ++++++ resources/lang/zh_TW/user.php | 9 ++++++ 8 files changed, 108 insertions(+), 24 deletions(-) diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index 73999038..6720fd7f 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -77,9 +77,10 @@ class Test extends Command */ public function handle() { - $torrent = \App\Models\Torrent::query()->find(3); - $promotionInfo = apply_filter('torrent_promotion', $torrent->toArray()); - dd($promotionInfo); + $end = Carbon::parse('2022-06-06 14:10'); + $begin = Carbon::parse('2022-06-06 03:10'); + $r = $end->diffInHours($begin); + dd($r); } diff --git a/app/Models/Torrent.php b/app/Models/Torrent.php index 30b072aa..74a3d99d 100644 --- a/app/Models/Torrent.php +++ b/app/Models/Torrent.php @@ -34,7 +34,7 @@ class Torrent extends NexusModel public static $commentFields = [ 'id', 'name', 'added', 'visible', 'banned', 'owner', 'sp_state', 'pos_state', 'hr', 'picktype', 'picktime', - 'last_action', 'leechers', 'seeders', 'times_completed', 'views', 'size', 'cover' + 'last_action', 'leechers', 'seeders', 'times_completed', 'views', 'size', 'cover', 'anonymous', ]; public static $basicRelations = [ diff --git a/app/Models/User.php b/app/Models/User.php index f5ebc7fb..2d53c5cc 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; +use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; use Laravel\Sanctum\HasApiTokens; use Nexus\Database\NexusDB; @@ -173,7 +174,7 @@ class User extends Authenticatable 'id', 'username', 'email', 'class', 'status', 'added', 'avatar', 'uploaded', 'downloaded', 'seedbonus', 'seedtime', 'leechtime', 'invited_by', 'enabled', 'seed_points', 'last_access', 'invites', - 'lang', 'attendance_card', + 'lang', 'attendance_card', 'privacy', 'noad', ]; public static function getDefaultUserAttributes(): array @@ -196,12 +197,28 @@ class User extends Authenticatable ]; } - public static function defaultUser() + public static function defaultUser(): static { return new static(self::getDefaultUserAttributes()); } - public function checkIsNormal(array $fields = ['status', 'enabled']) + public static function getClassName($class, $compact = false, $b_colored = false, $I18N = false) + { + $class_name = self::$classes[$class]['text']; + if ($class >= self::CLASS_VIP && $I18N) { + $class_name = nexus_trans("user.class_names.$class"); + } + $class_name_color = self::$classes[$class]['text']; + if ($compact) { + $class_name = str_replace(" ", "",$class_name); + } + if ($b_colored) { + return "" . $class_name . ""; + } + return $class_name; + } + + public function checkIsNormal(array $fields = ['status', 'enabled']): bool { if (in_array('status', $fields) && $this->getAttribute('status') != self::STATUS_CONFIRMED) { throw new \InvalidArgumentException(sprintf('User: %s is not confirmed.', $this->id)); @@ -416,7 +433,7 @@ class User extends Authenticatable return $this->update($update); } - public function canAccessAdmin() + public function canAccessAdmin(): bool { $targetClass = self::CLASS_SYSOP; if (!$this->class || $this->class < $targetClass) { @@ -426,7 +443,7 @@ class User extends Authenticatable return true; } - public function isDonating() + public function isDonating(): bool { $rawDonorUntil = $this->getRawOriginal('donoruntil'); if ( @@ -438,4 +455,6 @@ class User extends Authenticatable return false; } + + } diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 6ead548a..8491fa5e 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -2,6 +2,9 @@ namespace App\Repositories; +use App\Models\Setting; +use App\Models\Torrent; +use App\Models\User; use Illuminate\Encryption\Encrypter; use Illuminate\Support\Str; @@ -17,4 +20,20 @@ class BaseRepository return [$field, $type]; } + protected function handleAnonymous($username, User $user, User $authenticator, Torrent $torrent = null) + { + $canViewAnonymousClass = Setting::get('authority.viewanonymous'); + if($user->privacy == "strong" || ($torrent && $torrent->anonymous == 'yes' && $user->id == $torrent->owner)) { + //用户强私密,或者种子作者匿名而当前项作者刚好为种子作者 + if($authenticator->class >= $canViewAnonymousClass || $user->id == $authenticator->id) { + //但当前用户权限可以查看匿名者,或当前用户查看自己的数据,显示个匿名,后边加真实用户名 + return sprintf('匿名(%s)', $username); + } else { + return '匿名'; + } + } else { + return $username; + } + } + } diff --git a/include/functions.php b/include/functions.php index 625082aa..8f95547c 100644 --- a/include/functions.php +++ b/include/functions.php @@ -460,6 +460,9 @@ function get_user_class() function get_user_class_name($class, $compact = false, $b_colored = false, $I18N = false) { + if (!IN_NEXUS) { + return \App\Models\User::getClassName($class, $compact, $b_colored, $I18N); + } global $SITENAME; static $en_lang_functions; static $current_user_lang_functions; @@ -1896,20 +1899,29 @@ function get_user_row($id) global $Cache, $CURUSER; static $curuserRowUpdated = false; static $neededColumns = array('id', 'noad', 'class', 'enabled', 'privacy', 'avatar', 'signature', 'uploaded', 'downloaded', 'last_access', 'username', 'donor', 'donoruntil', 'leechwarn', 'warned', 'title'); - if ($CURUSER && $id == $CURUSER['id']) { - $row = array(); - foreach($neededColumns as $column) { - $row[$column] = $CURUSER[$column]; - } - if (!$curuserRowUpdated) { - $Cache->cache_value('user_'.$CURUSER['id'].'_content', $row, 900); - $curuserRowUpdated = true; - } - } elseif (!$row = $Cache->get_value('user_'.$id.'_content')){ - $res = sql_query("SELECT ".implode(',', $neededColumns)." FROM users WHERE id = ".sqlesc($id)) or sqlerr(__FILE__,__LINE__); - $row = mysql_fetch_array($res); - $Cache->cache_value('user_'.$id.'_content', $row, 900); - } + $cacheKey = 'user_'.$id.'_content'; + $row = \Nexus\Database\NexusDB::remember($cacheKey, 900, function () use ($id, $neededColumns) { + $user = \App\Models\User::query()->find($id, $neededColumns); + if ($user) { + return $user->toArray(); + } + return null; + }); + +// if ($CURUSER && $id == $CURUSER['id']) { +// $row = array(); +// foreach($neededColumns as $column) { +// $row[$column] = $CURUSER[$column]; +// } +// if (!$curuserRowUpdated) { +// $Cache->cache_value('user_'.$CURUSER['id'].'_content', $row, 900); +// $curuserRowUpdated = true; +// } +// } elseif (!$row = $Cache->get_value('user_'.$id.'_content')){ +// $res = sql_query("SELECT ".implode(',', $neededColumns)." FROM users WHERE id = ".sqlesc($id)) or sqlerr(__FILE__,__LINE__); +// $row = mysql_fetch_array($res); +// $Cache->cache_value('user_'.$id.'_content', $row, 900); +// } if (!$row) return false; @@ -3626,7 +3638,8 @@ function get_username($id, $big = false, $link = true, $bold = true, $target = f $username = ($underline == true ? "" . $arr['username'] . "" : $arr['username']); $username = ($bold == true ? "" . $username . "" : $username); - $username = ($link == true ? "" . $username . "" : $username) . $pics . ($withtitle == true ? " (" . ($arr['title'] == "" ? get_user_class_name($arr['class'],false,true,true) : "".htmlspecialchars($arr['title'])) . ")" : ""); + $href = getSchemeAndHttpHost() . "/userdetails.php?id=$id"; + $username = ($link == true ? "" . $username . "" : $username) . $pics . ($withtitle == true ? " (" . ($arr['title'] == "" ? get_user_class_name($arr['class'],false,true,true) : "".htmlspecialchars($arr['title'])) . ")" : ""); $username = "" . ( $bracket == true ? "(" . $username . ")" : $username) . ""; } @@ -5590,6 +5603,11 @@ function get_smile($num) return $all[$num] ?? null; } +function username_under_torrent() +{ + +} + /** * Calculate user seed bonus per hour * diff --git a/resources/lang/en/user.php b/resources/lang/en/user.php index 49d08ead..44015974 100644 --- a/resources/lang/en/user.php +++ b/resources/lang/en/user.php @@ -16,4 +16,13 @@ return [ 'invites' => 'Invites', 'attendance_card' => 'Attend card', ], + 'class_name' => [ + \App\Models\User::CLASS_VIP => 'Vip', + \App\Models\User::CLASS_RETIREE => 'Retiree', + \App\Models\User::CLASS_UPLOADER => 'Uploader', + \App\Models\User::CLASS_MODERATOR => 'Moderator', + \App\Models\User::CLASS_ADMINISTRATOR => 'Administrator', + \App\Models\User::CLASS_SYSOP => 'Sysop', + \App\Models\User::CLASS_STAFF_LEADER => 'Staff Leader', + ], ]; diff --git a/resources/lang/zh_CN/user.php b/resources/lang/zh_CN/user.php index 6285c9f7..1748a20c 100644 --- a/resources/lang/zh_CN/user.php +++ b/resources/lang/zh_CN/user.php @@ -16,4 +16,13 @@ return [ 'invites' => '邀请', 'attendance_card' => '补签卡', ], + 'class_names' => [ + \App\Models\User::CLASS_VIP => '贵宾', + \App\Models\User::CLASS_RETIREE => '养老族', + \App\Models\User::CLASS_UPLOADER => '发布员', + \App\Models\User::CLASS_MODERATOR => '总版主', + \App\Models\User::CLASS_ADMINISTRATOR => '管理员', + \App\Models\User::CLASS_SYSOP => '维护开发员', + \App\Models\User::CLASS_STAFF_LEADER => '主管', + ], ]; diff --git a/resources/lang/zh_TW/user.php b/resources/lang/zh_TW/user.php index f85ab03a..bda1fa34 100644 --- a/resources/lang/zh_TW/user.php +++ b/resources/lang/zh_TW/user.php @@ -16,4 +16,13 @@ return [ 'invites' => '邀請', 'attendance_card' => '補簽卡', ], + 'class_name' => [ + \App\Models\User::CLASS_VIP => '貴賓', + \App\Models\User::CLASS_RETIREE => '養老族', + \App\Models\User::CLASS_UPLOADER => '發布員', + \App\Models\User::CLASS_MODERATOR => '總版主', + \App\Models\User::CLASS_ADMINISTRATOR => '管理員', + \App\Models\User::CLASS_SYSOP => '維護開發員', + \App\Models\User::CLASS_STAFF_LEADER => '主管', + ], ];