plugin management + user tables and torrents table text column migrate

This commit is contained in:
xiaomlove
2025-01-19 14:37:00 +08:00
parent 0f88ab8d82
commit 403a9447a9
66 changed files with 1432 additions and 786 deletions
+22
View File
@@ -3,6 +3,8 @@
namespace App\Models;
use Carbon\Carbon;
class BonusLogs extends NexusModel
{
protected $table = 'bonus_logs';
@@ -41,6 +43,7 @@ class BonusLogs extends NexusModel
const BUSINESS_TYPE_ROLE_WORK_SALARY = 1000;
const BUSINESS_TYPE_TORRENT_BE_DOWNLOADED = 1001;
const BUSINESS_TYPE_RECEIVE_REWARD = 1002;
public static array $businessTypes = [
self::BUSINESS_TYPE_CANCEL_HIT_AND_RUN => ['text' => 'Cancel H&R'],
@@ -65,6 +68,7 @@ class BonusLogs extends NexusModel
self::BUSINESS_TYPE_ROLE_WORK_SALARY => ['text' => 'Role work salary'],
self::BUSINESS_TYPE_TORRENT_BE_DOWNLOADED => ['text' => 'Torrent be downloaded'],
self::BUSINESS_TYPE_RECEIVE_REWARD => ['text' => 'Receive reward'],
];
public function getBusinessTypeTextAttribute()
@@ -102,5 +106,23 @@ class BonusLogs extends NexusModel
return $result ?? self::DEFAULT_BONUS_BUY_CHANGE_USERNAME_CARD;
}
public static function add(int $userId, float $old, float $delta, float $new, string $comment, int $businessType)
{
if (!isset(self::$businessTypes[$businessType])) {
throw new \InvalidArgumentException("Invalid business type: $businessType");
}
$nowStr = Carbon::now()->toDateTimeString();
return self::query()->create([
'business_type' => $businessType,
'uid' => $userId,
'old_total_value' => $old,
'value' => $delta,
'new_total_value' => $new,
'comment' => sprintf("[%s] %s", self::$businessTypes[$businessType]['text'], $comment),
'created_at' => $nowStr,
'updated_at' => $nowStr,
]);
}
}
+57
View File
@@ -0,0 +1,57 @@
<?php
namespace App\Models;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\HtmlString;
use Sushi\Sushi;
class PluginStore extends Model
{
use Sushi;
const PLUGIN_LIST_API = "https://nppl.nexusphp.workers.dev";
const BLOG_POST_INFO_API = "https://nexusphp.org/wp-json/wp/v2/posts/%d";
const BLOG_POST_URL = "https://nexusphp.org/?p=%d";
public function getRows()
{
return Http::get(self::PLUGIN_LIST_API)->json();
}
public function getBlogPostUrl(): string
{
return sprintf(self::BLOG_POST_URL, $this->post_id);
}
public function getFullDescription(): Htmlable
{
$url = $this->getBlogPostInfoUrl($this->post_id);
$logPrefix = sprintf("post_id: %s, url: %s", $this->post_id, $url);
$defaultContent = "无法获取详细信息 ...";
try {
$result = Http::get($url)->json();
do_log("$logPrefix, result: " . json_encode($result));
$content = $result['content']['rendered'] ?? $result['message'] ?? $defaultContent;
} catch (\Exception $e) {
do_log(sprintf(
"%s, error: %s",
$logPrefix, $e->getMessage() . $e->getTraceAsString()
), 'error');
$content = $defaultContent;
}
return new HtmlString($content);
}
private function getBlogPostInfoUrl(int $postId): string
{
return sprintf(self::BLOG_POST_INFO_API, $postId);
}
public static function getInfo(string $id)
{
return Http::get(self::PLUGIN_LIST_API . "/plugin/$id")->json();
}
}
+1 -1
View File
@@ -5,7 +5,7 @@ namespace App\Models;
class Post extends NexusModel
{
public function user()
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(User::class, 'userid');
}
+8 -4
View File
@@ -9,11 +9,11 @@ use Illuminate\Database\Eloquent\Casts\Attribute;
class Torrent extends NexusModel
{
protected $fillable = [
'name', 'filename', 'save_as', 'descr', 'small_descr', 'ori_descr',
'name', 'filename', 'save_as', 'small_descr',
'category', 'source', 'medium', 'codec', 'standard', 'processing', 'team', 'audiocodec',
'size', 'added', 'type', 'numfiles', 'owner', 'nfo', 'sp_state', 'promotion_time_type',
'promotion_until', 'anonymous', 'url', 'pos_state', 'cache_stamp', 'picktype', 'picktime',
'last_reseed', 'pt_gen', 'technical_info', 'leechers', 'seeders', 'cover', 'last_action',
'last_reseed', 'leechers', 'seeders', 'cover', 'last_action',
'times_completed', 'approval_status', 'banned', 'visible', 'pos_state_until', 'price',
];
@@ -25,7 +25,6 @@ class Torrent extends NexusModel
protected $casts = [
'added' => 'datetime',
'pt_gen' => 'array',
'promotion_until' => 'datetime',
'pos_state_until' => 'datetime',
];
@@ -241,7 +240,7 @@ class Torrent extends NexusModel
public static function getFieldsForList($appendTableName = false): array|bool
{
$fields = 'id, sp_state, promotion_time_type, promotion_until, banned, picktype, pos_state, category, source, medium, codec, standard, processing, team, audiocodec, leechers, seeders, name, small_descr, times_completed, size, added, comments,anonymous,owner,url,cache_stamp, pt_gen, hr, approval_status, cover, price';
$fields = 'id, sp_state, promotion_time_type, promotion_until, banned, picktype, pos_state, category, source, medium, codec, standard, processing, team, audiocodec, leechers, seeders, name, small_descr, times_completed, size, added, comments,anonymous,owner,url,cache_stamp, hr, approval_status, cover, price';
$fields = preg_split('/[,\s]+/', $fields);
if ($appendTableName) {
foreach ($fields as &$value) {
@@ -513,4 +512,9 @@ class Torrent extends NexusModel
{
return $this->hasMany(TorrentOperationLog::class, 'torrent_id');
}
public function extra(): \Illuminate\Database\Eloquent\Relations\HasOne
{
return $this->hasOne(TorrentExtra::class, 'torrent_id');
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace App\Models;
use Nexus\Database\NexusDB;
class TorrentExtra extends NexusModel
{
public $timestamps = true;
protected $fillable = ['torrent_id', 'descr', 'ori_descr', 'media_info'];
public function torrent()
{
return $this->belongsTo(Torrent::class, 'torrent_id');
}
}
+19 -7
View File
@@ -182,9 +182,9 @@ class User extends Authenticatable implements FilamentUser, HasName
* @var array
*/
protected $fillable = [
'username', 'email', 'passhash', 'secret', 'stylesheet', 'editsecret', 'added', 'modcomment', 'enabled', 'status',
'username', 'email', 'passhash', 'secret', 'stylesheet', 'editsecret', 'added', 'enabled', 'status',
'leechwarn', 'leechwarnuntil', 'page', 'class', 'uploaded', 'downloaded', 'clientselect', 'showclienterror', 'last_home',
'seedbonus', 'bonuscomment', 'downloadpos', 'vip_added', 'vip_until', 'title', 'invites', 'attendance_card',
'seedbonus', 'downloadpos', 'vip_added', 'vip_until', 'title', 'invites', 'attendance_card',
'seed_points_per_hour', 'passkey',
];
@@ -229,7 +229,7 @@ class User extends Authenticatable implements FilamentUser, HasName
'uploaded', 'downloaded', 'seedbonus', 'seedtime', 'leechtime',
'invited_by', 'enabled', 'seed_points', 'last_access', 'invites',
'lang', 'attendance_card', 'privacy', 'noad', 'downloadpos', 'donoruntil', 'donor',
'bonuscomment', 'downloadpos', 'vip_added', 'vip_until', 'title', 'invites', 'attendance_card',
'downloadpos', 'vip_added', 'vip_until', 'title', 'invites', 'attendance_card',
'seed_points_per_hour'
];
@@ -535,6 +535,11 @@ class User extends Authenticatable implements FilamentUser, HasName
return $this->examAndTasks()->wherePivot("status", ExamUser::STATUS_NORMAL);
}
public function modifyLogs(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(UserModifyLog::class, "user_id");
}
public function getAvatarAttribute($value)
{
if ($value) {
@@ -560,10 +565,17 @@ class User extends Authenticatable implements FilamentUser, HasName
throw new \RuntimeException('This method only works when user exists !');
}
//@todo how to do prepare bindings here ?
$comment = addslashes($comment);
do_log("update: " . json_encode($update) . ", $commentField: $comment", 'notice');
$update[$commentField] = NexusDB::raw("if($commentField = '', '$comment', concat_ws('\n', '$comment', $commentField))");
return $this->update($update);
// $comment = addslashes($comment);
// do_log("update: " . json_encode($update) . ", $commentField: $comment", 'notice');
// $update[$commentField] = NexusDB::raw("if($commentField = '', '$comment', concat_ws('\n', '$comment', $commentField))");
if ($commentField != "modcomment") {
throw new \RuntimeException("unsupported commentField: $commentField !");
}
return NexusDB::transaction(function () use ($update, $comment) {
$this->modifyLogs()->create(['content' => $comment]);
return $this->update($update);
});
}
public function canAccessAdmin(): bool
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace App\Models;
class UserModifyLog extends NexusModel
{
protected $fillable = ['user_id', 'content', ];
public $timestamps = true;
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(User::class, "user_id");
}
}