初始化

初始化项目
This commit is contained in:
truewhile
2026-08-23 22:12:32 +08:00
parent 0bcb1fec87
commit 71bf60c69c
631 changed files with 2121 additions and 76050 deletions
-64
View File
@@ -1,64 +0,0 @@
package repository
import (
"context"
"errors"
"gorm.io/gorm"
"github.com/ShukeBta/MediaStationGo/internal/model"
)
// AssistantRepository persists model.AssistantSession + AssistantMessage records.
type AssistantRepository struct{ db *gorm.DB }
// ─── Session ────────────────────────────────────────────────────────────
// CreateSession inserts a new chat session.
func (r *AssistantRepository) CreateSession(ctx context.Context, s *model.AssistantSession) error {
return r.db.WithContext(ctx).Create(s).Error
}
// FindSession returns a session by ID, or (nil, nil).
func (r *AssistantRepository) FindSession(ctx context.Context, id string) (*model.AssistantSession, error) {
var s model.AssistantSession
err := r.db.WithContext(ctx).Where("id = ?", id).First(&s).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &s, nil
}
// ListSessions returns sessions for a user, or all when userID is empty.
func (r *AssistantRepository) ListSessions(ctx context.Context, userID string) ([]model.AssistantSession, error) {
q := r.db.WithContext(ctx).Model(&model.AssistantSession{})
if userID != "" {
q = q.Where("user_id = ?", userID)
}
var rows []model.AssistantSession
err := q.Order("created_at desc").Find(&rows).Error
return rows, err
}
// DeleteSession soft-deletes a session (cascade handled by GORM hooks if set).
func (r *AssistantRepository) DeleteSession(ctx context.Context, id string) error {
return r.db.WithContext(ctx).Delete(&model.AssistantSession{}, "id = ?", id).Error
}
// ─── Message ────────────────────────────────────────────────────────────
// AppendMessage inserts a new message into a session.
func (r *AssistantRepository) AppendMessage(ctx context.Context, m *model.AssistantMessage) error {
return r.db.WithContext(ctx).Create(m).Error
}
// ListMessages returns all messages for a session in chronological order.
func (r *AssistantRepository) ListMessages(ctx context.Context, sessionID string) ([]model.AssistantMessage, error) {
var rows []model.AssistantMessage
err := r.db.WithContext(ctx).Where("session_id = ?", sessionID).
Order("created_at asc").Find(&rows).Error
return rows, err
}
-105
View File
@@ -1,105 +0,0 @@
// Package repository 实现下载客户端配置的数据访问层。
package repository
import (
"context"
"errors"
"time"
"gorm.io/gorm"
"github.com/ShukeBta/MediaStationGo/internal/model"
)
// DownloadClientRepository persists model.DownloadClient records.
type DownloadClientRepository struct{ db *gorm.DB }
// Create inserts a new download client.
func (r *DownloadClientRepository) Create(ctx context.Context, c *model.DownloadClient) error {
return r.db.WithContext(ctx).Create(c).Error
}
// FindByID returns the download client by ID, or (nil, nil) when absent.
func (r *DownloadClientRepository) FindByID(ctx context.Context, id string) (*model.DownloadClient, error) {
var c model.DownloadClient
err := r.db.WithContext(ctx).Where("id = ?", id).First(&c).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &c, nil
}
// FindDefault returns the default download client, or (nil, nil).
func (r *DownloadClientRepository) FindDefault(ctx context.Context) (*model.DownloadClient, error) {
var c model.DownloadClient
err := r.db.WithContext(ctx).
Where("is_default = ? AND enabled = ?", true, true).
Order("created_at asc").
First(&c).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &c, nil
}
// List returns all download clients ordered by creation time.
func (r *DownloadClientRepository) List(ctx context.Context) ([]model.DownloadClient, error) {
var rows []model.DownloadClient
err := r.db.WithContext(ctx).Order("created_at asc").Find(&rows).Error
return rows, err
}
// ListEnabled returns all enabled download clients.
func (r *DownloadClientRepository) ListEnabled(ctx context.Context) ([]model.DownloadClient, error) {
var rows []model.DownloadClient
err := r.db.WithContext(ctx).Where("enabled = ?", true).Order("created_at asc").Find(&rows).Error
return rows, err
}
// HasAnyIncludingDeleted reports whether the operator has ever configured a
// download client. This distinguishes legacy-only installations from systems
// where deleting/disabling all clients is an intentional "stop downloads"
// action, even though rows are soft-deleted.
func (r *DownloadClientRepository) HasAnyIncludingDeleted(ctx context.Context) (bool, error) {
var n int64
err := r.db.WithContext(ctx).Unscoped().Model(&model.DownloadClient{}).Count(&n).Error
return n > 0, err
}
// Update persists changes to a download client.
func (r *DownloadClientRepository) Update(ctx context.Context, c *model.DownloadClient) error {
return r.db.WithContext(ctx).Save(c).Error
}
// Delete removes a download client (soft-delete).
func (r *DownloadClientRepository) Delete(ctx context.Context, id string) error {
return r.db.WithContext(ctx).Delete(&model.DownloadClient{}, "id = ?", id).Error
}
// ClearDefault unsets the default flag for all clients.
func (r *DownloadClientRepository) ClearDefault(ctx context.Context) error {
return r.db.WithContext(ctx).Model(&model.DownloadClient{}).
Where("is_default = ?", true).Update("is_default", false).Error
}
// SetDefault sets a specific client as default and clears others.
func (r *DownloadClientRepository) SetDefault(ctx context.Context, id string) error {
now := time.Now()
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Model(&model.DownloadClient{}).
Where("is_default = ?", true).Update("is_default", false).Error; err != nil {
return err
}
return tx.Model(&model.DownloadClient{}).
Where("id = ?", id).Updates(map[string]any{
"is_default": true,
"updated_at": now,
}).Error
})
}
@@ -1,24 +0,0 @@
package repository
import (
"context"
"gorm.io/gorm"
"github.com/ShukeBta/MediaStationGo/internal/model"
)
// DownloadRepository persists model.DownloadTask records.
type DownloadRepository struct{ db *gorm.DB }
// Create inserts a new download task.
func (r *DownloadRepository) Create(ctx context.Context, t *model.DownloadTask) error {
return r.db.WithContext(ctx).Create(t).Error
}
// List returns all download tasks (admin view).
func (r *DownloadRepository) List(ctx context.Context) ([]model.DownloadTask, error) {
var rows []model.DownloadTask
err := r.db.WithContext(ctx).Order("created_at desc").Find(&rows).Error
return rows, err
}
@@ -1,74 +0,0 @@
// Package repository 实现通知渠道配置的数据访问层。
package repository
import (
"context"
"errors"
"gorm.io/gorm"
"github.com/ShukeBta/MediaStationGo/internal/model"
)
// NotifyChannelRepository persists model.NotifyChannel records.
type NotifyChannelRepository struct{ db *gorm.DB }
// Create inserts a new notification channel.
func (r *NotifyChannelRepository) Create(ctx context.Context, c *model.NotifyChannel) error {
return r.db.WithContext(ctx).Create(c).Error
}
// FindByID returns the notification channel by ID, or (nil, nil) when absent.
func (r *NotifyChannelRepository) FindByID(ctx context.Context, id string) (*model.NotifyChannel, error) {
var c model.NotifyChannel
err := r.db.WithContext(ctx).Where("id = ?", id).First(&c).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &c, nil
}
// List returns all notification channels ordered by creation time.
func (r *NotifyChannelRepository) List(ctx context.Context) ([]model.NotifyChannel, error) {
var rows []model.NotifyChannel
err := r.db.WithContext(ctx).Order("created_at asc").Find(&rows).Error
return rows, err
}
// ListEnabled returns all enabled notification channels.
func (r *NotifyChannelRepository) ListEnabled(ctx context.Context) ([]model.NotifyChannel, error) {
var rows []model.NotifyChannel
err := r.db.WithContext(ctx).Where("enabled = ?", true).Order("created_at asc").Find(&rows).Error
return rows, err
}
// ListByEvent returns all enabled channels that subscribe to the given event type.
func (r *NotifyChannelRepository) ListByEvent(ctx context.Context, eventType string) ([]model.NotifyChannel, error) {
var rows []model.NotifyChannel
// Events is a JSON array stored as text; use LIKE for simple matching.
// This works for exact event type matches within the JSON array.
err := r.db.WithContext(ctx).
Where("enabled = ? AND events LIKE ?", true, "%\""+eventType+"\"%").
Find(&rows).Error
return rows, err
}
// ListByType returns all channels of a given type (telegram/wechat/bark/webhook/email).
func (r *NotifyChannelRepository) ListByType(ctx context.Context, channelType string) ([]model.NotifyChannel, error) {
var rows []model.NotifyChannel
err := r.db.WithContext(ctx).Where("type = ?", channelType).Find(&rows).Error
return rows, err
}
// Update persists changes to a notification channel.
func (r *NotifyChannelRepository) Update(ctx context.Context, c *model.NotifyChannel) error {
return r.db.WithContext(ctx).Save(c).Error
}
// Delete removes a notification channel (soft-delete).
func (r *NotifyChannelRepository) Delete(ctx context.Context, id string) error {
return r.db.WithContext(ctx).Delete(&model.NotifyChannel{}, "id = ?", id).Error
}
-16
View File
@@ -17,20 +17,12 @@ type Container struct {
History *HistoryRepository
Favorite *FavoriteRepository
Playlist *PlaylistRepository
Download *DownloadRepository
Subscription *SubscriptionRepository
Setting *SettingRepository
Log *AccessLogRepository
Permission *PermissionRepository
RefreshToken *RefreshTokenRepository
ApiConfig *ApiConfigRepository
DownloadClient *DownloadClientRepository
NotifyChannel *NotifyChannelRepository
Site *SiteRepository
STRM *STRMRepository
PlayProfile *PlayProfileRepository
StorageConfig *StorageConfigRepository
Assistant *AssistantRepository
RegCode *RegistrationCodeRepository
SignIn *SignInRepository
UserDevice *UserDeviceRepository
@@ -47,20 +39,12 @@ func New(db *gorm.DB) *Container {
History: &HistoryRepository{db: db},
Favorite: &FavoriteRepository{db: db},
Playlist: &PlaylistRepository{db: db},
Download: &DownloadRepository{db: db},
Subscription: &SubscriptionRepository{db: db},
Setting: &SettingRepository{db: db},
Log: &AccessLogRepository{db: db},
Permission: &PermissionRepository{db: db},
RefreshToken: &RefreshTokenRepository{db: db},
ApiConfig: &ApiConfigRepository{db: db},
DownloadClient: &DownloadClientRepository{db: db},
NotifyChannel: &NotifyChannelRepository{db: db},
Site: &SiteRepository{db: db},
STRM: &STRMRepository{db: db},
PlayProfile: &PlayProfileRepository{db: db},
StorageConfig: &StorageConfigRepository{db: db},
Assistant: &AssistantRepository{db: db},
RegCode: &RegistrationCodeRepository{db: db},
SignIn: &SignInRepository{db: db},
UserDevice: &UserDeviceRepository{db: db},
-56
View File
@@ -1,56 +0,0 @@
// Package repository — PT 站点数据访问层。
package repository
import (
"context"
"errors"
"gorm.io/gorm"
"github.com/ShukeBta/MediaStationGo/internal/model"
)
// SiteRepository persists model.Site records.
type SiteRepository struct{ db *gorm.DB }
// Create inserts a new site.
func (r *SiteRepository) Create(ctx context.Context, s *model.Site) error {
return r.db.WithContext(ctx).Create(s).Error
}
// FindByID returns the site by ID, or (nil, nil) when absent.
func (r *SiteRepository) FindByID(ctx context.Context, id string) (*model.Site, error) {
var s model.Site
err := r.db.WithContext(ctx).Where("id = ?", id).First(&s).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &s, nil
}
// List returns all sites ordered by name.
func (r *SiteRepository) List(ctx context.Context) ([]model.Site, error) {
var rows []model.Site
err := r.db.WithContext(ctx).Order("name asc").Find(&rows).Error
return rows, err
}
// ListEnabled returns all enabled sites.
func (r *SiteRepository) ListEnabled(ctx context.Context) ([]model.Site, error) {
var rows []model.Site
err := r.db.WithContext(ctx).Where("enabled = ?", true).Order("name asc").Find(&rows).Error
return rows, err
}
// Update updates site fields.
func (r *SiteRepository) Update(ctx context.Context, s *model.Site) error {
return r.db.WithContext(ctx).Save(s).Error
}
// Delete removes a site (soft-delete).
func (r *SiteRepository) Delete(ctx context.Context, id string) error {
return r.db.WithContext(ctx).Delete(&model.Site{}, "id = ?", id).Error
}
-82
View File
@@ -1,82 +0,0 @@
// Package repository — STRM 文件记录数据访问层。
package repository
import (
"context"
"errors"
"gorm.io/gorm"
"github.com/ShukeBta/MediaStationGo/internal/model"
)
// STRMRepository persists model.STRMRecord records.
type STRMRepository struct{ db *gorm.DB }
// Create inserts a new STRM record.
func (r *STRMRepository) Create(ctx context.Context, s *model.STRMRecord) error {
return r.db.WithContext(ctx).Create(s).Error
}
// CreateBatch inserts multiple STRM records.
func (r *STRMRepository) CreateBatch(ctx context.Context, records []model.STRMRecord) error {
if len(records) == 0 {
return nil
}
return r.db.WithContext(ctx).CreateInBatches(records, 100).Error
}
// FindByID returns the STRM record by ID, or (nil, nil) when absent.
func (r *STRMRepository) FindByID(ctx context.Context, id string) (*model.STRMRecord, error) {
var s model.STRMRecord
err := r.db.WithContext(ctx).Where("id = ?", id).First(&s).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &s, nil
}
// List returns STRM records with optional filters. Supports pagination.
// Filters: media_id, media_type, protocol
func (r *STRMRepository) List(ctx context.Context, filters map[string]string, offset, limit int) ([]model.STRMRecord, int64, error) {
q := r.db.WithContext(ctx).Model(&model.STRMRecord{})
if mediaID, ok := filters["media_id"]; ok && mediaID != "" {
q = q.Where("media_id = ?", mediaID)
}
if mediaType, ok := filters["media_type"]; ok && mediaType != "" {
q = q.Where("media_type = ?", mediaType)
}
if protocol, ok := filters["protocol"]; ok && protocol != "" {
q = q.Where("protocol = ?", protocol)
}
var total int64
if err := q.Count(&total).Error; err != nil {
return nil, 0, err
}
var rows []model.STRMRecord
err := q.Order("created_at desc").Offset(offset).Limit(limit).Find(&rows).Error
return rows, total, err
}
// Update updates a STRM record.
func (r *STRMRepository) Update(ctx context.Context, s *model.STRMRecord) error {
return r.db.WithContext(ctx).Save(s).Error
}
// Delete removes a STRM record (soft-delete).
func (r *STRMRepository) Delete(ctx context.Context, id string) error {
return r.db.WithContext(ctx).Delete(&model.STRMRecord{}, "id = ?", id).Error
}
// FindByMediaID returns STRM records for a given media ID.
func (r *STRMRepository) FindByMediaID(ctx context.Context, mediaID string) ([]model.STRMRecord, error) {
var rows []model.STRMRecord
err := r.db.WithContext(ctx).Where("media_id = ?", mediaID).Find(&rows).Error
return rows, err
}
@@ -1,72 +0,0 @@
package repository
import (
"context"
"errors"
"time"
"gorm.io/gorm"
"github.com/ShukeBta/MediaStationGo/internal/model"
)
// SubscriptionRepository persists model.Subscription records.
type SubscriptionRepository struct{ db *gorm.DB }
// Create inserts a new subscription rule.
func (r *SubscriptionRepository) Create(ctx context.Context, s *model.Subscription) error {
return r.db.WithContext(ctx).Select("*").Omit("DeletedAt").Create(s).Error
}
// FindActiveByIdentity returns an unarchived, non-deleted rule with the same
// per-user functional identity. excludeID is used while editing/restoring.
func (r *SubscriptionRepository) FindActiveByIdentity(ctx context.Context, userID, identityKey, excludeID string) (*model.Subscription, error) {
if identityKey == "" {
return nil, nil
}
q := r.db.WithContext(ctx).
Where("user_id = ? AND identity_key = ? AND archived_at IS NULL", userID, identityKey)
if excludeID != "" {
q = q.Where("id <> ?", excludeID)
}
var sub model.Subscription
err := q.First(&sub).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &sub, nil
}
// List returns active subscription rules. Archived rows live in history and are
// intentionally excluded from scheduler polling and the active management list.
func (r *SubscriptionRepository) List(ctx context.Context) ([]model.Subscription, error) {
var rows []model.Subscription
err := r.db.WithContext(ctx).Unscoped().
Where("archived_at IS NULL").
Where("deleted_at IS NULL OR enabled = ?", true).
Order("created_at desc").
Find(&rows).Error
return rows, err
}
// History returns archived subscription rules.
func (r *SubscriptionRepository) History(ctx context.Context) ([]model.Subscription, error) {
var rows []model.Subscription
err := r.db.WithContext(ctx).Where("archived_at IS NOT NULL").Order("archived_at desc, updated_at desc").Find(&rows).Error
return rows, err
}
// Archive moves a completed subscription out of the active list without
// deleting its rule details, so users can audit completed subscriptions later.
func (r *SubscriptionRepository) Archive(ctx context.Context, id, reason string, archivedAt time.Time) error {
return r.db.WithContext(ctx).Model(&model.Subscription{}).
Where("id = ? AND archived_at IS NULL", id).
Updates(map[string]any{
"enabled": false,
"archived_at": &archivedAt,
"archive_reason": reason,
}).Error
}
-3
View File
@@ -141,9 +141,6 @@ func (r *UserRepository) Delete(ctx context.Context, id string) error {
if err := tx.Where("id = ?", id).First(&user).Error; err != nil {
return err
}
if err := tx.Unscoped().Where("user_id = ?", id).Delete(&model.TelegramBinding{}).Error; err != nil {
return err
}
released := user.Username + "__deleted__" + time.Now().Format("20060102150405.000000000")
if len(released) > 64 {
sum := sha256.Sum256([]byte(user.ID + user.Username))