Revert "♻️ Move models.Account to sec.UserInfo" for a better solution

This reverts commit 8fbb7960
This commit is contained in:
2024-10-31 00:17:53 +08:00
parent 69c6ac6581
commit 2fcc784bc4
35 changed files with 192 additions and 286 deletions

View File

@ -5,9 +5,9 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
)
func GetBotCount(user uint) (int64, error) {
func GetBotCount(user models.Account) (int64, error) {
var count int64
if err := database.C.Where("automated_id = ?", user).Count(&count).Error; err != nil {
if err := database.C.Where("automated_id = ?", user.ID).Count(&count).Error; err != nil {
return 0, err
}
return count, nil

View File

@ -21,13 +21,13 @@ func AddEvent(user uint, event, target, ip, ua string) {
}
// AddAuditRecord to keep logs to make administrators' operations clear to query
func AddAuditRecord(operator uint, act, ip, ua string, metadata map[string]any) {
func AddAuditRecord(operator models.Account, act, ip, ua string, metadata map[string]any) {
writeAuditQueue = append(writeAuditQueue, models.AuditRecord{
Action: act,
Metadata: metadata,
IpAddress: ip,
UserAgent: ua,
AccountID: operator,
AccountID: operator.ID,
})
}

View File

@ -15,21 +15,21 @@ import (
"gorm.io/datatypes"
)
func GetAuthPreference(account uint) (models.PreferenceAuth, error) {
func GetAuthPreference(account models.Account) (models.PreferenceAuth, error) {
var auth models.PreferenceAuth
if err := database.C.Where("account_id = ?", account).First(&auth).Error; err != nil {
if err := database.C.Where("account_id = ?", account.ID).First(&auth).Error; err != nil {
return auth, err
}
return auth, nil
}
func UpdateAuthPreference(account uint, config models.AuthConfig) (models.PreferenceAuth, error) {
func UpdateAuthPreference(account models.Account, config models.AuthConfig) (models.PreferenceAuth, error) {
var auth models.PreferenceAuth
var err error
if auth, err = GetAuthPreference(account); err != nil {
auth = models.PreferenceAuth{
AccountID: account,
AccountID: account.ID,
Config: datatypes.NewJSONType(config),
}
} else {
@ -44,16 +44,16 @@ func GetNotificationPreferenceCacheKey(accountId uint) string {
return fmt.Sprintf("notification-preference#%d", accountId)
}
func GetNotificationPreference(account uint) (models.PreferenceNotification, error) {
func GetNotificationPreference(account models.Account) (models.PreferenceNotification, error) {
var notification models.PreferenceNotification
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
if val, err := marshal.Get(contx, GetNotificationPreferenceCacheKey(account), new(models.PreferenceNotification)); err == nil {
if val, err := marshal.Get(contx, GetNotificationPreferenceCacheKey(account.ID), new(models.PreferenceNotification)); err == nil {
notification = val.(models.PreferenceNotification)
} else {
if err := database.C.Where("account_id = ?", account).First(&notification).Error; err != nil {
if err := database.C.Where("account_id = ?", account.ID).First(&notification).Error; err != nil {
return notification, err
}
CacheNotificationPreference(notification)
@ -76,12 +76,12 @@ func CacheNotificationPreference(prefs models.PreferenceNotification) {
)
}
func UpdateNotificationPreference(account uint, config map[string]bool) (models.PreferenceNotification, error) {
func UpdateNotificationPreference(account models.Account, config map[string]bool) (models.PreferenceNotification, error) {
var notification models.PreferenceNotification
var err error
if notification, err = GetNotificationPreference(account); err != nil {
notification = models.PreferenceNotification{
AccountID: account,
AccountID: account.ID,
Config: lo.MapValues(config, func(v bool, k string) any { return v }),
}
} else {

View File

@ -18,20 +18,20 @@ func ListCommunityRealm() ([]models.Realm, error) {
return realms, nil
}
func ListOwnedRealm(user uint) ([]models.Realm, error) {
func ListOwnedRealm(user models.Account) ([]models.Realm, error) {
var realms []models.Realm
if err := database.C.Where(&models.Realm{AccountID: user}).Find(&realms).Error; err != nil {
if err := database.C.Where(&models.Realm{AccountID: user.ID}).Find(&realms).Error; err != nil {
return realms, err
}
return realms, nil
}
func ListAvailableRealm(user uint) ([]models.Realm, error) {
func ListAvailableRealm(user models.Account) ([]models.Realm, error) {
var realms []models.Realm
var members []models.RealmMember
if err := database.C.Where(&models.RealmMember{
AccountID: user,
AccountID: user.ID,
}).Find(&members).Error; err != nil {
return realms, err
}
@ -57,9 +57,9 @@ func GetRealmWithAlias(alias string) (models.Realm, error) {
return realm, nil
}
func NewRealm(realm models.Realm, user uint) (models.Realm, error) {
func NewRealm(realm models.Realm, user models.Account) (models.Realm, error) {
realm.Members = []models.RealmMember{
{AccountID: user, PowerLevel: 100},
{AccountID: user.ID, PowerLevel: 100},
}
err := database.C.Save(&realm).Error
@ -90,14 +90,14 @@ func GetRealmMember(userId uint, realmId uint) (models.RealmMember, error) {
return member, nil
}
func AddRealmMember(user uint, affected models.Account, target models.Realm) error {
func AddRealmMember(user models.Account, affected models.Account, target models.Realm) error {
if !target.IsPublic && !target.IsCommunity {
if member, err := GetRealmMember(user, target.ID); err != nil {
if member, err := GetRealmMember(user.ID, target.ID); err != nil {
return fmt.Errorf("only realm member can add people: %v", err)
} else if member.PowerLevel < 50 {
return fmt.Errorf("only realm moderator can add people")
}
rel, err := GetRelationWithTwoNode(affected.ID, user)
rel, err := GetRelationWithTwoNode(affected.ID, user.ID)
if err != nil || HasPermNodeWithDefault(
rel.PermNodes,
"RealmAdd",
@ -116,9 +116,9 @@ func AddRealmMember(user uint, affected models.Account, target models.Realm) err
return err
}
func RemoveRealmMember(user uint, affected models.Account, target models.Realm) error {
if user != affected.ID {
if member, err := GetRealmMember(user, target.ID); err != nil {
func RemoveRealmMember(user models.Account, affected models.Account, target models.Realm) error {
if user.ID != affected.ID {
if member, err := GetRealmMember(user.ID, target.ID); err != nil {
return fmt.Errorf("only realm member can remove other member: %v", err)
} else if member.PowerLevel < 50 {
return fmt.Errorf("only realm moderator can invite people")

View File

@ -9,10 +9,10 @@ import (
"gorm.io/gorm"
)
func ListAllRelationship(user uint) ([]models.AccountRelationship, error) {
func ListAllRelationship(user models.Account) ([]models.AccountRelationship, error) {
var relationships []models.AccountRelationship
if err := database.C.
Where("account_id = ?", user).
Where("account_id = ?", user.ID).
Preload("Account").
Preload("Related").
Find(&relationships).Error; err != nil {
@ -22,10 +22,10 @@ func ListAllRelationship(user uint) ([]models.AccountRelationship, error) {
return relationships, nil
}
func ListRelationshipWithFilter(user uint, status models.RelationshipStatus) ([]models.AccountRelationship, error) {
func ListRelationshipWithFilter(user models.Account, status models.RelationshipStatus) ([]models.AccountRelationship, error) {
var relationships []models.AccountRelationship
if err := database.C.
Where("account_id = ? AND status = ?", user, status).
Where("account_id = ? AND status = ?", user.ID, status).
Preload("Account").
Preload("Related").
Find(&relationships).Error; err != nil {

View File

@ -5,10 +5,10 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
)
func ListAbuseReport(account uint) ([]models.AbuseReport, error) {
func ListAbuseReport(account models.Account) ([]models.AbuseReport, error) {
var reports []models.AbuseReport
err := database.C.
Where("account_id = ?", account).
Where("account_id = ?", account.ID).
Find(&reports).Error
return reports, err
}
@ -51,13 +51,13 @@ func UpdateAbuseReportStatus(id uint, status, message string) error {
return nil
}
func NewAbuseReport(resource string, reason string, account uint) (models.AbuseReport, error) {
func NewAbuseReport(resource string, reason string, account models.Account) (models.AbuseReport, error) {
var report models.AbuseReport
if err := database.C.
Where(
"resource = ? AND account_id = ? AND status IN ?",
resource,
account,
account.ID,
[]string{models.ReportStatusPending, models.ReportStatusReviewing},
).First(&report).Error; err == nil {
return report, fmt.Errorf("you already reported this resource and it still in process")
@ -66,7 +66,7 @@ func NewAbuseReport(resource string, reason string, account uint) (models.AbuseR
report = models.AbuseReport{
Resource: resource,
Reason: reason,
AccountID: account,
AccountID: account.ID,
}
err := database.C.Create(&report).Error

View File

@ -11,11 +11,11 @@ import (
"time"
)
func CheckDailyCanSign(user uint) error {
func CheckDailyCanSign(user models.Account) error {
probe := time.Now().Format("2006-01-02")
var record models.SignRecord
if err := database.C.Where("account_id = ? AND created_at::date = ?", user, probe).First(&record).Error; err != nil {
if err := database.C.Where("account_id = ? AND created_at::date = ?", user.ID, probe).First(&record).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil
}
@ -24,22 +24,22 @@ func CheckDailyCanSign(user uint) error {
return fmt.Errorf("daliy sign record exists")
}
func GetTodayDailySign(user uint) (models.SignRecord, error) {
func GetTodayDailySign(user models.Account) (models.SignRecord, error) {
probe := time.Now().Format("2006-01-02")
var record models.SignRecord
if err := database.C.Where("account_id = ? AND created_at::date = ?", user, probe).First(&record).Error; err != nil {
if err := database.C.Where("account_id = ? AND created_at::date = ?", user.ID, probe).First(&record).Error; err != nil {
return record, fmt.Errorf("unable get daliy sign record: %v", err)
}
return record, nil
}
func DailySign(user uint) (models.SignRecord, error) {
func DailySign(user models.Account) (models.SignRecord, error) {
tier := rand.Intn(5)
record := models.SignRecord{
ResultTier: tier,
ResultExperience: rand.Intn(int(math.Max(float64(tier), 1)*100)+1-100) + 100,
AccountID: user,
AccountID: user.ID,
}
if err := CheckDailyCanSign(user); err != nil {
@ -47,7 +47,7 @@ func DailySign(user uint) (models.SignRecord, error) {
}
var profile models.AccountProfile
if err := database.C.Where("account_id = ?", user).First(&profile).Error; err != nil {
if err := database.C.Where("account_id = ?", user.ID).First(&profile).Error; err != nil {
return record, fmt.Errorf("unable get account profile: %v", err)
} else {
profile.Experience += uint64(record.ResultExperience)

View File

@ -98,27 +98,27 @@ func GetStatusOnline(uid uint) error {
}
}
func NewStatus(user uint, status models.Status) (models.Status, error) {
func NewStatus(user models.Account, status models.Status) (models.Status, error) {
if err := database.C.Save(&status).Error; err != nil {
return status, err
} else {
CacheUserStatus(user, status)
CacheUserStatus(user.ID, status)
}
return status, nil
}
func EditStatus(user uint, status models.Status) (models.Status, error) {
func EditStatus(user models.Account, status models.Status) (models.Status, error) {
if err := database.C.Save(&status).Error; err != nil {
return status, err
} else {
CacheUserStatus(user, status)
CacheUserStatus(user.ID, status)
}
return status, nil
}
func ClearStatus(user uint) error {
func ClearStatus(user models.Account) error {
if err := database.C.
Where("account_id = ?", user).
Where("account_id = ?", user.ID).
Where("clear_at > ?", time.Now()).
Updates(models.Status{ClearAt: lo.ToPtr(time.Now())}).Error; err != nil {
return err
@ -127,7 +127,7 @@ func ClearStatus(user uint) error {
marshal := marshaler.New(cacheManager)
contx := context.Background()
marshal.Delete(contx, GetStatusCacheKey(user))
marshal.Delete(contx, GetStatusCacheKey(user.ID))
}
return nil

View File

@ -57,7 +57,7 @@ func NewTicket(user models.Account, ip, ua string) (models.AuthTicket, error) {
} else {
steps = min(steps, int(count))
cfg, err := GetAuthPreference(user.ID)
cfg, err := GetAuthPreference(user)
if err == nil && cfg.Config.Data().MaximumAuthSteps >= 1 {
steps = min(steps, cfg.Config.Data().MaximumAuthSteps)
}