diff --git a/cmd/model-gen/main.go b/cmd/model-gen/main.go index adc420f..daefa96 100644 --- a/cmd/model-gen/main.go +++ b/cmd/model-gen/main.go @@ -18,14 +18,6 @@ import ( "git.mstar.dev/mstar/linstrom/storage-new/models" ) -// "git.mstar.dev/mstar/linstrom/config" - -// "context" -// "time" -// postgresContainer "github.com/testcontainers/testcontainers-go/modules/postgres" -// "github.com/testcontainers/testcontainers-go" -// "github.com/testcontainers/testcontainers-go/wait" - const ( dbName = "linstrom" dbUser = "linstrom" @@ -38,31 +30,6 @@ func main() { other.ConfigureLogging(nil) config.ReadAndWriteToGlobal(*shared.FlagConfigFile) - // Set up a temporary postgres container for gorm-gen to do its thing - // log.Info().Msg("Starting temporary postgres container") - // pgContainer, err := postgresContainer.Run( - // context.Background(), - // "postgres:16.4-alpine", - // postgresContainer.WithDatabase(dbName), - // postgresContainer.WithUsername(dbUser), - // postgresContainer.WithPassword(dbPass), - // testcontainers.WithWaitStrategyAndDeadline( - // time.Minute, - // wait.ForLog("database system is ready to accept connections"). - // WithOccurrence(2). - // WithStartupTimeout(time.Second*5), - // ), - // ) - // if err != nil { - // log.Fatal().Err(err).Msg("Failed to setup temporary postgres container") - // } - // log.Info().Msg("Temporary postgres container started") - // defer func() { - // if err := testcontainers.TerminateContainer(pgContainer); err != nil { - // log.Fatal().Err(err).Msg("Failed to terminate temporary postgres container") - // } - // log.Info().Msg("Temporary postgres container stopped") - // }() db, err := gorm.Open( postgres.Open(config.GlobalConfig.Storage.BuildPostgresDSN()), // postgres.Open(pgContainer.MustConnectionString(context.Background())), @@ -84,7 +51,10 @@ func main() { log.Info().Msg("Applying basic operations on all datatypes") g.ApplyBasic(models.AllTypes...) - log.Info().Msg("Starting generation") + log.Info().Msg("Basic operations applied, applying extra features") + g.ApplyInterface(func(models.INotification) {}, models.Notification{}) + + log.Info().Msg("Extra features applied, starting generation") g.Execute() log.Info().Msg("Code generation complete") } diff --git a/storage-new/models/0allTypes.go b/storage-new/models/0allTypes.go index 3f562fc..5c703a5 100644 --- a/storage-new/models/0allTypes.go +++ b/storage-new/models/0allTypes.go @@ -7,10 +7,12 @@ var AllTypes = []any{ &MediaMetadata{}, &Note{}, &NoteToAttachment{}, + &NoteToBoost{}, &NoteToEmote{}, &NoteToFeed{}, &NoteToPing{}, &NoteTag{}, + &Notification{}, &Reaction{}, &RemoteServer{}, &Role{}, diff --git a/storage-new/models/NoteToBoost.go b/storage-new/models/NoteToBoost.go new file mode 100644 index 0000000..5a21a03 --- /dev/null +++ b/storage-new/models/NoteToBoost.go @@ -0,0 +1,13 @@ +package models + +import "time" + +// Describes a user boosting one note +type NoteToBoost struct { + ID uint `gorm:"primarykey"` + CreatedAt time.Time + User User + UserId string + Note Note + NoteId string +} diff --git a/storage-new/models/Notification.go b/storage-new/models/Notification.go new file mode 100644 index 0000000..238a791 --- /dev/null +++ b/storage-new/models/Notification.go @@ -0,0 +1,41 @@ +package models + +import ( + "database/sql" + + "gorm.io/gen" + "gorm.io/gorm" +) + +// Notifications inform users of inbound events +// happening to their content +type Notification struct { + gorm.Model + ForUser User // The user the notification is for + ForUserId string + SourceNote *Note // The note where the notification originates from + SourceNoteId sql.NullString + SourceUser *User // The user causing the notification + SourceUserId sql.NullString + Cause NotificationCauseType // What the cause of the notification is + ViewedState NotificationViewedState // What the last known state of the notification is +} + +type INotification interface { + // Update a given set of notifications to a given viewed state + // + // UPDATE @@table SET viewed_state = @state WHERE id IN @id + SetState(state NotificationViewedState, ids ...uint) error + + // Get the lastest count amount of notifications with a given offset for a user + // + // SELECT * FROM @@table WHERE for_user_id = @id + // ORDER BY id DESC + // {{if count > 0 }} + // LIMIT @count + // {{else}} + // LIMIT 20 + // {{end}} + // OFFSET @offset + GetLatestWithOffset(userId string, count uint, offset uint) ([]gen.T, error) +} diff --git a/storage-new/models/NotificationType.go b/storage-new/models/NotificationType.go new file mode 100644 index 0000000..a4568f3 --- /dev/null +++ b/storage-new/models/NotificationType.go @@ -0,0 +1,22 @@ +package models + +import "database/sql/driver" + +// What a notification is caused by +type NotificationCauseType uint8 + +const ( + NotificationTypeReply NotificationCauseType = iota // A new reply + NotificationTypeReaction // A new reaction + NotificationTypeBoost // A note got boosted + NotificationTypeFollowRequest // Someone requested to follow +) + +func (r *NotificationCauseType) Value() (driver.Value, error) { + return r, nil +} + +func (ct *NotificationCauseType) Scan(value any) error { + *ct = NotificationCauseType(value.(uint8)) + return nil +} diff --git a/storage-new/models/NotificationViewedStateType.go b/storage-new/models/NotificationViewedStateType.go new file mode 100644 index 0000000..54054bd --- /dev/null +++ b/storage-new/models/NotificationViewedStateType.go @@ -0,0 +1,21 @@ +package models + +import "database/sql/driver" + +type NotificationViewedState uint8 + +const ( + NotViewed NotificationViewedState = iota + Viewed + Accepted + Denied +) + +func (r *NotificationViewedState) Value() (driver.Value, error) { + return r, nil +} + +func (ct *NotificationViewedState) Scan(value any) error { + *ct = NotificationViewedState(value.(uint8)) + return nil +}