🗑️ Clean up code

This commit is contained in:
2024-05-15 19:45:49 +08:00
parent da557fbe60
commit 4709760edc
25 changed files with 361 additions and 1349 deletions

View File

@@ -1,7 +1,5 @@
package models
import "time"
// Account profiles basically fetched from Hydrogen.Passport
// But cache at here for better usage
// At the same time this model can make relations between local models
@@ -15,19 +13,8 @@ type Account struct {
Description string `json:"description"`
EmailAddress string `json:"email_address"`
PowerLevel int `json:"power_level"`
Moments []Moment `json:"moments" gorm:"foreignKey:AuthorID"`
Articles []Article `json:"articles" gorm:"foreignKey:AuthorID"`
Posts []Post `json:"posts" gorm:"foreignKey:AuthorID"`
Attachments []Attachment `json:"attachments" gorm:"foreignKey:AuthorID"`
Reactions []Reaction `json:"reactions"`
ExternalID uint `json:"external_id"`
}
type AccountMembership struct {
ID uint `json:"id" gorm:"primaryKey"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Follower Account `json:"follower"`
Following Account `json:"following"`
FollowerID uint
FollowingID uint
}

View File

@@ -1,41 +0,0 @@
package models
type Article struct {
PostBase
Title string `json:"title"`
Hashtags []Tag `json:"tags" gorm:"many2many:article_tags"`
Categories []Category `json:"categories" gorm:"many2many:article_categories"`
Reactions []Reaction `json:"reactions"`
Attachments []Attachment `json:"attachments"`
Description string `json:"description"`
Content string `json:"content"`
RealmID *uint `json:"realm_id"`
Realm *Realm `json:"realm"`
Comments []Comment `json:"comments" gorm:"foreignKey:ArticleID"`
}
func (p *Article) GetReplyTo() PostInterface {
return nil
}
func (p *Article) GetRepostTo() PostInterface {
return nil
}
func (p *Article) GetHashtags() []Tag {
return p.Hashtags
}
func (p *Article) GetCategories() []Category {
return p.Categories
}
func (p *Article) SetHashtags(tags []Tag) {
p.Hashtags = tags
}
func (p *Article) SetCategories(categories []Category) {
p.Categories = categories
}

View File

@@ -19,18 +19,16 @@ const (
type Attachment struct {
BaseModel
FileID string `json:"file_id"`
Filesize int64 `json:"filesize"`
Filename string `json:"filename"`
Mimetype string `json:"mimetype"`
Hashcode string `json:"hashcode"`
Type AttachmentType `json:"type"`
ExternalUrl string `json:"external_url"`
Author Account `json:"author"`
ArticleID *uint `json:"article_id"`
MomentID *uint `json:"moment_id"`
CommentID *uint `json:"comment_id"`
AuthorID uint `json:"author_id"`
FileID string `json:"file_id"`
Filesize int64 `json:"filesize"`
Filename string `json:"filename"`
Mimetype string `json:"mimetype"`
Hashcode string `json:"hashcode"`
Type AttachmentType `json:"type"`
Author Account `json:"author"`
AuthorID uint `json:"author_id"`
PostID *uint `json:"post_id"`
}
func (v Attachment) GetStoragePath() string {

View File

@@ -3,21 +3,17 @@ package models
type Tag struct {
BaseModel
Alias string `json:"alias" gorm:"uniqueIndex" validate:"lowercase,alphanum,min=4,max=24"`
Name string `json:"name"`
Description string `json:"description"`
Articles []Article `json:"articles" gorm:"many2many:article_tags"`
Moments []Moment `json:"moments" gorm:"many2many:moment_tags"`
Comments []Comment `json:"comments" gorm:"many2many:comment_tags"`
Alias string `json:"alias" gorm:"uniqueIndex" validate:"lowercase,alphanum,min=4,max=24"`
Name string `json:"name"`
Description string `json:"description"`
Posts []Post `json:"posts" gorm:"many2many:post_tags"`
}
type Category struct {
BaseModel
Alias string `json:"alias" gorm:"uniqueIndex" validate:"lowercase,alphanum,min=4,max=24"`
Name string `json:"name"`
Description string `json:"description"`
Articles []Article `json:"articles" gorm:"many2many:article_categories"`
Moments []Moment `json:"moments" gorm:"many2many:moment_categories"`
Comments []Comment `json:"comments" gorm:"many2many:comment_categories"`
Alias string `json:"alias" gorm:"uniqueIndex" validate:"lowercase,alphanum,min=4,max=24"`
Name string `json:"name"`
Description string `json:"description"`
Posts []Post `json:"posts" gorm:"many2many:post_categories"`
}

View File

@@ -1,38 +0,0 @@
package models
type Comment struct {
PostBase
Content string `json:"content"`
Hashtags []Tag `json:"tags" gorm:"many2many:comment_tags"`
Categories []Category `json:"categories" gorm:"many2many:comment_categories"`
Reactions []Reaction `json:"reactions"`
Attachments []Attachment `json:"attachments"`
ReplyID *uint `json:"reply_id"`
ReplyTo *Comment `json:"reply_to" gorm:"foreignKey:ReplyID"`
ArticleID *uint `json:"article_id"`
MomentID *uint `json:"moment_id"`
Article *Article `json:"article"`
Moment *Moment `json:"moment"`
}
func (p *Comment) GetReplyTo() PostInterface {
return p.ReplyTo
}
func (p *Comment) GetHashtags() []Tag {
return p.Hashtags
}
func (p *Comment) GetCategories() []Category {
return p.Categories
}
func (p *Comment) SetHashtags(tags []Tag) {
p.Hashtags = tags
}
func (p *Comment) SetCategories(categories []Category) {
p.Categories = categories
}

View File

@@ -1,22 +0,0 @@
package models
type Feed struct {
BaseModel
Alias string `json:"alias"`
Title string `json:"title"`
Description string `json:"description"`
Content string `json:"content"`
ModelType string `json:"model_type"`
CommentCount int64 `json:"comment_count"`
ReactionCount int64 `json:"reaction_count"`
AuthorID uint `json:"author_id"`
RealmID *uint `json:"realm_id"`
Author Account `json:"author" gorm:"embedded"`
Attachments []Attachment `json:"attachments" gorm:"-"`
ReactionList map[string]int64 `json:"reaction_list" gorm:"-"`
}

View File

@@ -1,41 +0,0 @@
package models
type Moment struct {
PostBase
Content string `json:"content"`
Hashtags []Tag `json:"tags" gorm:"many2many:moment_tags"`
Categories []Category `json:"categories" gorm:"many2many:moment_categories"`
Reactions []Reaction `json:"reactions"`
Attachments []Attachment `json:"attachments"`
RealmID *uint `json:"realm_id"`
RepostID *uint `json:"repost_id"`
Realm *Realm `json:"realm"`
RepostTo *Moment `json:"repost_to" gorm:"foreignKey:RepostID"`
Comments []Comment `json:"comments" gorm:"foreignKey:MomentID"`
}
func (p *Moment) GetRepostTo() PostInterface {
return p.RepostTo
}
func (p *Moment) GetRealm() *Realm {
return p.Realm
}
func (p *Moment) GetHashtags() []Tag {
return p.Hashtags
}
func (p *Moment) GetCategories() []Category {
return p.Categories
}
func (p *Moment) SetHashtags(tags []Tag) {
p.Hashtags = tags
}
func (p *Moment) SetCategories(categories []Category) {
p.Categories = categories
}

View File

@@ -12,53 +12,30 @@ type PostReactInfo struct {
RepostCount int64 `json:"repost_count"`
}
type PostBase struct {
type Post struct {
BaseModel
Alias string `json:"alias" gorm:"uniqueIndex"`
Alias string `json:"alias" gorm:"uniqueIndex"`
Content string `json:"content"`
Tags []Tag `json:"tags" gorm:"many2many:post_tags"`
Categories []Category `json:"categories" gorm:"many2many:post_categories"`
Reactions []Reaction `json:"reactions"`
Attachments []Attachment `json:"attachments"`
Replies []Post `json:"replies" gorm:"foreignKey:ReplyID"`
ReplyID *uint `json:"reply_id"`
RepostID *uint `json:"repost_id"`
RealmID *uint `json:"realm_id"`
ReplyTo *Post `json:"reply_to" gorm:"foreignKey:ReplyID"`
RepostTo *Post `json:"repost_to" gorm:"foreignKey:RepostID"`
Realm *Realm `json:"realm"`
PublishedAt *time.Time `json:"published_at"`
AuthorID uint `json:"author_id"`
Author Account `json:"author"`
// Dynamic Calculated Values
ReactionList map[string]int64 `json:"reaction_list" gorm:"-"`
}
func (p *PostBase) GetID() uint {
return p.ID
}
func (p *PostBase) GetReplyTo() PostInterface {
return nil
}
func (p *PostBase) GetRepostTo() PostInterface {
return nil
}
func (p *PostBase) GetAuthor() Account {
return p.Author
}
func (p *PostBase) GetRealm() *Realm {
return nil
}
func (p *PostBase) SetReactionList(list map[string]int64) {
p.ReactionList = list
}
type PostInterface interface {
GetID() uint
GetHashtags() []Tag
GetCategories() []Category
GetReplyTo() PostInterface
GetRepostTo() PostInterface
GetAuthor() Account
GetRealm() *Realm
SetHashtags([]Tag)
SetCategories([]Category)
SetReactionList(map[string]int64)
ReplyCount int64 `json:"comment_count"`
ReactionCount int64 `json:"reaction_count"`
ReactionList map[string]int64 `json:"reaction_list" gorm:"-"`
}

View File

@@ -20,8 +20,6 @@ type Reaction struct {
Symbol string `json:"symbol"`
Attitude ReactionAttitude `json:"attitude"`
ArticleID *uint `json:"article_id"`
MomentID *uint `json:"moment_id"`
CommentID *uint `json:"comment_id"`
PostID *uint `json:"post_id"`
AccountID uint `json:"account_id"`
}

View File

@@ -5,12 +5,11 @@ package models
type Realm struct {
BaseModel
Alias string `json:"alias"`
Name string `json:"name"`
Description string `json:"description"`
Articles []Article `json:"article"`
Moments []Moment `json:"moments"`
IsPublic bool `json:"is_public"`
IsCommunity bool `json:"is_community"`
ExternalID uint `json:"external_id"`
Alias string `json:"alias"`
Name string `json:"name"`
Description string `json:"description"`
Posts []Post `json:"posts"`
IsPublic bool `json:"is_public"`
IsCommunity bool `json:"is_community"`
ExternalID uint `json:"external_id"`
}