From 5546d31e5d581960e807b6e550c64e9dd0417a5f Mon Sep 17 00:00:00 2001 From: mStar Date: Mon, 11 Nov 2024 09:04:42 +0100 Subject: [PATCH] Move account relations into separate table --- server/apiLinstromTypeHelpers.go | 3 +-- server/apiLinstromTypes.go | 13 +++++++++++-- storage/accountRelations.go | 12 ++++++++++++ storage/followRequests.go | 10 ---------- storage/storage.go | 2 +- storage/user.go | 8 +++----- 6 files changed, 28 insertions(+), 20 deletions(-) create mode 100644 storage/accountRelations.go delete mode 100644 storage/followRequests.go diff --git a/server/apiLinstromTypeHelpers.go b/server/apiLinstromTypeHelpers.go index 141a904..3ca673b 100644 --- a/server/apiLinstromTypeHelpers.go +++ b/server/apiLinstromTypeHelpers.go @@ -68,8 +68,7 @@ func convertAccountStorageToLinstrom( BannerId: acc.Banner, Background: apiBackground, BackgroundId: acc.Background, - FollowerIds: acc.Followers, - FollowingIds: acc.Follows, + RelationIds: acc.Relations, Indexable: acc.Indexable, RestrictedFollow: acc.RestrictedFollow, IdentifiesAs: sliceutils.Map( diff --git a/server/apiLinstromTypes.go b/server/apiLinstromTypes.go index 22bbaf6..906405f 100644 --- a/server/apiLinstromTypes.go +++ b/server/apiLinstromTypes.go @@ -7,6 +7,7 @@ import "time" var ( _ = linstromNote{} _ = linstromRole{} + _ = linstromRelation{} ) type linstromNote struct { @@ -71,8 +72,7 @@ type linstromAccount struct { BannerId *string `jsonapi:"attr,banner-id"` Background *linstromMediaMetadata `jsonapi:"relation,background"` BackgroundId *string `jsonapi:"attr,background-id"` - FollowerIds []string `jsonapi:"attr,follows-ids"` - FollowingIds []string `jsonapi:"attr,following-ids"` + RelationIds []uint `jsonapi:"attr,follows-ids"` Indexable bool `jsonapi:"attr,indexable"` RestrictedFollow bool `jsonapi:"attr,restricted-follow"` IdentifiesAs []string `jsonapi:"attr,identifies-as"` @@ -90,6 +90,15 @@ type linstromCustomAccountField struct { BelongsToId string `jsonapi:"attr,belongs-to-id"` } +type linstromRelation struct { + Id uint `jsonapi:"primary,relations"` + CreatedAt time.Time `jsonapi:"attr,created-at"` + UpdatedAt time.Time `jsonapi:"attr,updated-at"` + FromId string `jsonapi:"attr,from-id"` + ToId string `jsonapi:"attr,to-id"` + Accepted bool `jsonapi:"attr,accepted"` +} + // Role is essentially just a carbon copy of storage/roles.go type linstromRole struct { Id uint `jsonapi:"primary,roles"` diff --git a/storage/accountRelations.go b/storage/accountRelations.go new file mode 100644 index 0000000..644f44f --- /dev/null +++ b/storage/accountRelations.go @@ -0,0 +1,12 @@ +package storage + +import ( + "gorm.io/gorm" +) + +type AccountRelation struct { + gorm.Model + FromId string + ToId string + Accepted bool +} diff --git a/storage/followRequests.go b/storage/followRequests.go deleted file mode 100644 index 71add15..0000000 --- a/storage/followRequests.go +++ /dev/null @@ -1,10 +0,0 @@ -package storage - -import "time" - -type FollowRequest struct { - ID uint `gorm:"primarykey"` - CreatedAt time.Time - FromId string - ToId string -} diff --git a/storage/storage.go b/storage/storage.go index 5584782..f270c21 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -49,7 +49,7 @@ func NewStorage(dbUrl string, cache *cache.Cache) (*Storage, error) { AccessToken{}, Emote{}, UserInfoField{}, - FollowRequest{}, + AccountRelation{}, ) if err != nil { return nil, fmt.Errorf("failed to apply migrations: %w", err) diff --git a/storage/user.go b/storage/user.go index 0da6efc..b11dfa2 100644 --- a/storage/user.go +++ b/storage/user.go @@ -25,7 +25,7 @@ type Account struct { // Username of the user (eg "max" if the full username is @max@example.com) // Assume unchangable (once set by a user) to be kind to other implementations // Would be an easy avenue to fuck with them though - Username string + Username string `gorm:"unique"` CreatedAt time.Time // When this entry was created. Automatically set by gorm // When this account was last updated. Will also be used for refreshing remote accounts. Automatically set by gorm UpdatedAt time.Time @@ -40,8 +40,7 @@ type Account struct { Description string // The description of a user account Tags []string `gorm:"serializer:json"` // Hashtags IsBot bool // Whether to mark this account as a script controlled one - Follows []string `gorm:"serializer:json"` // List of account ids this account follows - Followers []string `gorm:"serializer:json"` // List of account ids that follow this account + Relations []uint `gorm:"serializer:json"` // List of ids of all relations this account has. Both follows and followers Icon string // ID of a media file used as icon Background *string // ID of a media file used as background image Banner *string // ID of a media file used as banner @@ -332,9 +331,8 @@ func (s *Storage) NewEmptyAccount() (*Account, error) { } acc.WebAuthnId = data - acc.Followers = []string{} + acc.Relations = []uint{} acc.Tags = []string{} - acc.Follows = []string{} acc.Gender = []string{} acc.CustomFields = []uint{} acc.IdentifiesAs = []Being{}