torrent api + swip constants

This commit is contained in:
xiaomlove
2021-05-15 19:29:44 +08:00
parent 786095ca96
commit 33e99516b6
55 changed files with 1968 additions and 38 deletions

View File

@@ -0,0 +1,8 @@
<?php
namespace App\Models;
class Attachment extends NexusModel
{
}

View File

@@ -0,0 +1,9 @@
<?php
namespace App\Models;
class AudioCodec extends NexusModel
{
protected $table = 'audiocodecs';
}

9
app/Models/Category.php Normal file
View File

@@ -0,0 +1,9 @@
<?php
namespace App\Models;
class Category extends NexusModel
{
}

9
app/Models/Codec.php Normal file
View File

@@ -0,0 +1,9 @@
<?php
namespace App\Models;
class Codec extends NexusModel
{
protected $table = 'codecs';
}

27
app/Models/Comment.php Normal file
View File

@@ -0,0 +1,27 @@
<?php
namespace App\Models;
class Comment extends NexusModel
{
protected $casts = [
'added' => 'datetime',
'editdate' => 'datetime',
];
public function related_torrent()
{
return $this->belongsTo(Torrent::class, 'torrent');
}
public function create_user()
{
return $this->belongsTo(User::class, 'user');
}
public function update_user()
{
return $this->belongsTo(User::class, 'editedby');
}
}

9
app/Models/File.php Normal file
View File

@@ -0,0 +1,9 @@
<?php
namespace App\Models;
class File extends NexusModel
{
protected $table = 'files';
}

9
app/Models/Media.php Normal file
View File

@@ -0,0 +1,9 @@
<?php
namespace App\Models;
class Media extends NexusModel
{
protected $table = 'media';
}

View File

@@ -9,4 +9,19 @@ class Message extends NexusModel
protected $fillable = [
'sender', 'receiver', 'added', 'subject', 'msg', 'unread', 'location', 'saved'
];
protected $casts = [
'added' => 'datetime',
];
public function send_user()
{
return $this->belongsTo(User::class, 'sender')->withDefault(['id' => 0, 'username' => 'System']);
}
public function receive_user()
{
return $this->belongsTo(User::class, 'receiver');
}
}

73
app/Models/Peer.php Normal file
View File

@@ -0,0 +1,73 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
class Peer extends NexusModel
{
const CONNECTABLE_YES = 'yes';
const CONNECTABLE_NO = 'no';
protected $casts = [
'started' => 'datetime',
'last_action' => 'datetime',
'prev_action' => 'datetime',
];
public static $connectableText = [
self::CONNECTABLE_YES => '是',
self::CONNECTABLE_NO => '否',
];
const SEEDER_YES = 'yes';
const SEEDER_NO = 'no';
public static $cardTitles = [
'upload_text' => '上传',
'download_text' => '下载',
'share_ratio' => '分享率',
'agent_human' => '客户端',
'connect_time_total' => '连接时间',
'download_progress' => '完成进度',
];
public function getConnectableTextAttribute()
{
return self::$connectableText[$this->connectable] ?? '';
}
public function scopeIsSeeder(Builder $builder)
{
return $builder->where('seeder', self::SEEDER_YES);
}
public function scopeIsNotSeeder(Builder $builder)
{
return $builder->where('seeder', self::SEEDER_NO);
}
public function isSeeder()
{
return $this->seeder == self::SEEDER_YES;
}
public function isNotSeeder()
{
return $this->seeder == self::SEEDER_NO;
}
public function user()
{
return $this->belongsTo(User::class, 'userid');
}
public function relative_torrent()
{
return $this->belongsTo(Torrent::class, 'torrent');
}
}

9
app/Models/Post.php Normal file
View File

@@ -0,0 +1,9 @@
<?php
namespace App\Models;
class Post extends NexusModel
{
}

View File

@@ -0,0 +1,9 @@
<?php
namespace App\Models;
class Processing extends NexusModel
{
protected $table = 'processings';
}

50
app/Models/Snatch.php Normal file
View File

@@ -0,0 +1,50 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
class Snatch extends NexusModel
{
protected $table = 'snatched';
protected $casts = [
'last_action' => 'datetime',
'startdat' => 'datetime',
'completedat' => 'datetime',
];
public static $cardTitles = [
'upload_text' => '上传',
'download_text' => '下载',
'share_ratio' => '分享率',
'seed_time' => '做种时间',
'leech_time' => '下载时间',
'completed_at_human' => '完成',
];
const FINISHED_YES = 'yes';
const FINISHED_NO = 'no';
public function scopeIsFinished(Builder $builder)
{
return $builder->where('finished', self::FINISHED_YES);
}
public function scopeIsNotFinished(Builder $builder)
{
return $builder->where('finished', self::FINISHED_NO);
}
public function torrent()
{
return $this->belongsTo(Torrent::class, 'torrentid');
}
public function user()
{
return $this->belongsTo(User::class, 'userid');
}
}

8
app/Models/Source.php Normal file
View File

@@ -0,0 +1,8 @@
<?php
namespace App\Models;
class Source extends NexusModel
{
}

9
app/Models/Standard.php Normal file
View File

@@ -0,0 +1,9 @@
<?php
namespace App\Models;
class Standard extends NexusModel
{
}

9
app/Models/Team.php Normal file
View File

@@ -0,0 +1,9 @@
<?php
namespace App\Models;
class Team extends NexusModel
{
}

17
app/Models/Thank.php Normal file
View File

@@ -0,0 +1,17 @@
<?php
namespace App\Models;
class Thank extends NexusModel
{
public function user()
{
return $this->belongsTo(User::class, 'userid');
}
public function torrent()
{
return $this->belongsTo(Torrent::class. 'torrentid');
}
}

View File

@@ -18,7 +18,9 @@ class Torrent extends NexusModel
const BANNED_YES = 'yes';
const BANNED_NO = 'no';
public $timestamps = true;
protected $casts = [
'added' => 'datetime'
];
public function checkIsNormal(array $fields = ['visible', 'banned'])
{
@@ -31,4 +33,104 @@ class Torrent extends NexusModel
return true;
}
public function user()
{
return $this->belongsTo(User::class, 'owner');
}
public function thanks()
{
return $this->hasMany(Thank::class, 'torrentid');
}
public function thank_users()
{
return $this->belongsToMany(User::class, 'thanks', 'torrentid', 'userid');
}
/**
* 同伴
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function peers()
{
return $this->hasMany(Peer::class, 'torrent');
}
/**
* 完成情况
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function snatches()
{
return $this->hasMany(Snatch::class, 'torrentid');
}
public function upload_peers()
{
return $this->peers()->where('seeder', Peer::SEEDER_YES);
}
public function download_peers()
{
return $this->peers()->where('seeder', Peer::SEEDER_NO);
}
public function finish_peers()
{
return $this->peers()->where('finishedat', '>', 0);
}
public function files()
{
return $this->hasMany(File::class, 'torrent');
}
public function basic_category()
{
return $this->belongsTo(Category::class, 'category');
}
public function basic_source()
{
return $this->belongsTo(Source::class, 'source');
}
public function basic_media()
{
return $this->belongsTo(Media::class, 'medium');
}
public function basic_codec()
{
return $this->belongsTo(Codec::class, 'codec');
}
public function basic_standard()
{
return $this->belongsTo(Standard::class, 'standard');
}
public function basic_processing()
{
return $this->belongsTo(Processing::class, 'processing');
}
public function basic_team()
{
return $this->belongsTo(Team::class, 'team');
}
public function basic_audiocodec()
{
return $this->belongsTo(AudioCodec::class, 'audiocodec');
}
public function scopeVisible($query, $visible = self::VISIBLE_YES)
{
$query->where('visible', $visible);
}
}

View File

@@ -59,6 +59,15 @@ class User extends Authenticatable
self::CLASS_STAFF_LEADER => ['text' => 'Staff Leader'],
];
public static $cardTitles = [
'uploaded_human' => '上传',
'downloaded_human' => '下载',
'share_ratio' => '分享率',
'seed_time' => '做种时间',
'seed_bonus' => '魔力值',
'invites' => '邀请',
];
public function getClassTextAttribute(): string
{
return self::$classes[$this->class]['text'] ?? '';
@@ -157,6 +166,68 @@ class User extends Authenticatable
return $this->belongsTo(User::class, 'invited_by');
}
public function send_messages()
{
return $this->hasMany(Message::class, 'sender');
}
public function receive_messages()
{
return $this->hasMany(Message::class, 'receiver');
}
public function comments()
{
return $this->hasMany(Comment::class, 'user');
}
public function posts()
{
return $this->hasMany(Post::class, 'userid');
}
public function torrents()
{
return $this->hasMany(Torrent::class, 'owner');
}
public function peers_torrents()
{
return $this->hasManyThrough(
Torrent::class,
Peer::class,
'userid',
'id',
'id',
'torrent');
}
public function snatched_torrents()
{
return $this->hasManyThrough(
Torrent::class,
Snatch::class,
'userid',
'id',
'id',
'torrentid');
}
public function getAvatarAttribute($value)
{
if ($value) {
if (substr($value, 0, 4) == 'http') {
return $value;
} else {
do_log("用户头像: $value 不是 http 地址");
}
}
return getSchemeAndHttpHost() . '/pic/default_avatar.png';
}
public function updateWithModComment(array $update, $modComment)
{
if (!$this->exists) {