🗃️ Update modeling

This commit is contained in:
LittleSheep 2024-08-24 15:17:26 +08:00
parent 5e76fa07b7
commit 516f5593de
7 changed files with 51 additions and 24 deletions

8
.idea/workspace.xml generated
View File

@ -6,8 +6,12 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":alien: Change avatar and banner id to string"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":alien: Change avatar and banner id to string">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/models/unified.go" beforeDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/internal/models/accounts.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/accounts.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/notifications.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/notifications.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/internal/models/auth.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/auth.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/models/events.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/events.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/models/notifications.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/notifications.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/userinfo_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/userinfo_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/cleaner.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/cleaner.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />

View File

@ -2,11 +2,11 @@ package models
import ( import (
"fmt" "fmt"
"gorm.io/datatypes"
"time" "time"
"github.com/samber/lo" "github.com/samber/lo"
"github.com/spf13/viper" "github.com/spf13/viper"
"gorm.io/datatypes"
) )
type Account struct { type Account struct {
@ -21,6 +21,12 @@ type Account struct {
SuspendedAt *time.Time `json:"suspended_at"` SuspendedAt *time.Time `json:"suspended_at"`
PermNodes datatypes.JSONMap `json:"perm_nodes"` PermNodes datatypes.JSONMap `json:"perm_nodes"`
AutomatedBy *Account `json:"automated_by" gorm:"foreignKey:AutomatedID"`
AutomatedID *uint `json:"automated_id"`
AffiliatedTo *Realm `json:"affiliated_to" gorm:"foreignKey:AffiliatedID"`
AffiliatedID *uint `json:"affiliated_id"`
Profile AccountProfile `json:"profile,omitempty"` Profile AccountProfile `json:"profile,omitempty"`
Contacts []AccountContact `json:"contacts,omitempty"` Contacts []AccountContact `json:"contacts,omitempty"`
Badges []Badge `json:"badges,omitempty"` Badges []Badge `json:"badges,omitempty"`
@ -28,11 +34,6 @@ type Account struct {
Tickets []AuthTicket `json:"tickets,omitempty"` Tickets []AuthTicket `json:"tickets,omitempty"`
Factors []AuthFactor `json:"factors,omitempty"` Factors []AuthFactor `json:"factors,omitempty"`
Events []ActionEvent `json:"events,omitempty"`
Notifications []Notification `json:"notifications,omitempty"`
NotifySubscribers []NotificationSubscriber `json:"notify_subscribers,omitempty"`
Relations []AccountRelationship `json:"relations,omitempty" gorm:"foreignKey:AccountID"` Relations []AccountRelationship `json:"relations,omitempty" gorm:"foreignKey:AccountID"`
} }

View File

@ -17,9 +17,11 @@ const (
type AuthFactor struct { type AuthFactor struct {
BaseModel BaseModel
Type int8 `json:"type"` Type int8 `json:"type"`
Secret string `json:"-"` Secret string `json:"-"`
Config JSONMap `json:"config"` Config JSONMap `json:"config"`
Account Account `json:"account"`
AccountID uint `json:"account_id"` AccountID uint `json:"account_id"`
} }
@ -41,7 +43,11 @@ type AuthTicket struct {
LastGrantAt *time.Time `json:"last_grant_at"` LastGrantAt *time.Time `json:"last_grant_at"`
Nonce *string `json:"nonce"` Nonce *string `json:"nonce"`
ClientID *uint `json:"client_id"` ClientID *uint `json:"client_id"`
AccountID uint `json:"account_id"`
Account Account `json:"account"`
AccountID uint `json:"account_id"`
IsApiKey bool `json:"is_api_key"`
} }
func (v AuthTicket) IsAvailable() error { func (v AuthTicket) IsAvailable() error {

View File

@ -8,5 +8,7 @@ type ActionEvent struct {
Location string `json:"location"` Location string `json:"location"`
IpAddress string `json:"ip_address"` IpAddress string `json:"ip_address"`
UserAgent string `json:"user_agent"` UserAgent string `json:"user_agent"`
AccountID uint `json:"account_id"`
Account Account `json:"account"`
AccountID uint `json:"account_id"`
} }

View File

@ -7,15 +7,17 @@ import (
type Notification struct { type Notification struct {
BaseModel BaseModel
Topic string `json:"topic"` Topic string `json:"topic"`
Title string `json:"title"` Title string `json:"title"`
Subtitle *string `json:"subtitle"` Subtitle *string `json:"subtitle"`
Body string `json:"body"` Body string `json:"body"`
Metadata datatypes.JSONMap `json:"metadata"` Metadata datatypes.JSONMap `json:"metadata"`
Avatar *string `json:"avatar"` Avatar *string `json:"avatar"`
Picture *string `json:"picture"` Picture *string `json:"picture"`
AccountID uint `json:"account_id"` SenderID *uint `json:"sender_id"`
SenderID *uint `json:"sender_id"`
Account Account `json:"account"`
AccountID uint `json:"account_id"`
IsRealtime bool `json:"is_realtime" gorm:"-"` IsRealtime bool `json:"is_realtime" gorm:"-"`
IsForcePush bool `json:"is_force_push" gorm:"-"` IsForcePush bool `json:"is_force_push" gorm:"-"`
@ -33,5 +35,7 @@ type NotificationSubscriber struct {
Provider string `json:"provider"` Provider string `json:"provider"`
DeviceID string `json:"device_id" gorm:"uniqueIndex"` DeviceID string `json:"device_id" gorm:"uniqueIndex"`
DeviceToken string `json:"device_token"` DeviceToken string `json:"device_token"`
AccountID uint `json:"account_id"`
Account Account `json:"account"`
AccountID uint `json:"account_id"`
} }

View File

@ -2,6 +2,8 @@ package api
import ( import (
"fmt" "fmt"
"github.com/spf13/viper"
"gorm.io/gorm"
"strings" "strings"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/database"
@ -17,7 +19,10 @@ func getOtherUserinfo(c *fiber.Ctx) error {
if err := database.C. if err := database.C.
Where(&models.Account{Name: alias}). Where(&models.Account{Name: alias}).
Preload("Profile"). Preload("Profile").
Preload("Badges"). Preload("Badges", func(db *gorm.DB) *gorm.DB {
prefix := viper.GetString("database.prefix")
return db.Order(fmt.Sprintf("%sbadges.type DESC", prefix))
}).
First(&account).Error; err != nil { First(&account).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} }

View File

@ -2,7 +2,9 @@ package services
import ( import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"time"
) )
func DoAutoDatabaseCleanup() { func DoAutoDatabaseCleanup() {
@ -17,5 +19,8 @@ func DoAutoDatabaseCleanup() {
count += tx.RowsAffected count += tx.RowsAffected
} }
deadline := time.Now().Add(-30 * 24 * time.Hour)
database.C.Unscoped().Where("created_at <= ?", deadline).Delete(&models.Notification{})
log.Debug().Int64("affected", count).Msg("Clean up entire database accomplished.") log.Debug().Int64("affected", count).Msg("Clean up entire database accomplished.")
} }