Compare commits

...

8 commits

Author SHA1 Message Date
349e78e433
Add two new webservers for new version
Some checks failed
/ test (push) Has been cancelled
One will be debug and localhost only, other one public stuff
2025-04-05 22:02:31 +02:00
0cd5031e4e
Update main file to have switch to new version 2025-04-05 22:01:59 +02:00
f453983f80
Add inserter for self stuff 2025-04-05 22:01:42 +02:00
427675f38e
chore(storage): run gorm gen 2025-04-05 22:01:22 +02:00
3f5df241db
Remove default stuff from models 2025-04-05 22:01:00 +02:00
a943f229e6
Add hardcoded IDs for default roles 2025-04-05 22:00:36 +02:00
9fdb57407b
Make icon optional
If not set, will be an identicon later
2025-04-05 22:00:17 +02:00
5a032d2007
Fix typo 2025-04-05 22:00:07 +02:00
32 changed files with 794 additions and 228 deletions

32
main.go
View file

@ -10,11 +10,15 @@ import (
"github.com/go-webauthn/webauthn/webauthn"
"github.com/mstarongithub/passkey"
"github.com/rs/zerolog/log"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"git.mstar.dev/mstar/linstrom/config"
"git.mstar.dev/mstar/linstrom/server"
"git.mstar.dev/mstar/linstrom/shared"
"git.mstar.dev/mstar/linstrom/storage"
storagenew "git.mstar.dev/mstar/linstrom/storage-new"
"git.mstar.dev/mstar/linstrom/storage-new/dbgen"
"git.mstar.dev/mstar/linstrom/storage/cache"
)
@ -45,7 +49,16 @@ func main() {
if *shared.FlagConfigOnly {
return
}
if *shared.FlagStartNew {
log.Info().Msg("Starting new system")
newServer()
} else {
log.Info().Msg("Starting old system")
oldServer()
}
}
func oldServer() {
storageCache, err := cache.NewCache(
config.GlobalConfig.Storage.MaxInMemoryCacheSize,
config.GlobalConfig.Storage.RedisUrl,
@ -93,3 +106,22 @@ func main() {
// TODO: Set up queues
// TODO: Set up plugins
}
func newServer() {
db, err := gorm.Open(
postgres.Open(config.GlobalConfig.Storage.BuildPostgresDSN()),
&gorm.Config{
Logger: shared.NewGormLogger(log.Logger),
},
)
if err != nil {
log.Fatal().Err(err).Msg("Failed to start db")
}
dbgen.SetDefault(db)
if err = storagenew.Migrate(db); err != nil {
log.Fatal().Err(err).Msg("Failed to automigrate structure")
}
if err = storagenew.InsertSelf(); err != nil {
log.Fatal().Err(err).Msg("Failed to insert self properly")
}
}

View file

@ -19,6 +19,7 @@ var (
false,
"If set, the server will only validate the config (or write the default one) and then quit",
)
FlagStartNew *bool = flag.Bool("new", false, "Start the new system")
)
func flagUsage() {

View file

@ -35,6 +35,19 @@ func newAccessToken(db *gorm.DB, opts ...gen.DOOption) accessToken {
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("User", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("User.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("User.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -245,6 +258,12 @@ type accessTokenBelongsToUser struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -440,15 +459,15 @@ type IAccessTokenDo interface {
GetTokenIfValid(token string) (result *models.AccessToken, err error)
}
// Get the data for a token if it hasn't expired yet
// Get the data for a token
//
// SELECT * FROM @@table WHERE token = @token AND expires_at < NOW() LIMIT 1
// SELECT * FROM @@table WHERE token = @token
func (a accessTokenDo) GetTokenIfValid(token string) (result *models.AccessToken, err error) {
var params []interface{}
var generateSQL strings.Builder
params = append(params, token)
generateSQL.WriteString("SELECT * FROM access_tokens WHERE token = ? AND expires_at < NOW() LIMIT 1 ")
generateSQL.WriteString("SELECT * FROM access_tokens WHERE token = ? ")
var executeSQL *gorm.DB
executeSQL = a.UnderlyingDB().Raw(generateSQL.String(), params...).Take(&result) // ignore_security_alert

View file

@ -38,6 +38,19 @@ func newFeed(db *gorm.DB, opts ...gen.DOOption) feed {
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("Owner", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Owner.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("Owner.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -260,6 +273,12 @@ type feedBelongsToOwner struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}

View file

@ -35,6 +35,19 @@ func newLoginProcessToken(db *gorm.DB, opts ...gen.DOOption) loginProcessToken {
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("User", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("User.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("User.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -248,6 +261,12 @@ type loginProcessTokenBelongsToUser struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}

View file

@ -30,7 +30,7 @@ func newMediaMetadata(db *gorm.DB, opts ...gen.DOOption) mediaMetadata {
_mediaMetadata.CreatedAt = field.NewTime(tableName, "created_at")
_mediaMetadata.UpdatedAt = field.NewTime(tableName, "updated_at")
_mediaMetadata.DeletedAt = field.NewField(tableName, "deleted_at")
_mediaMetadata.OwnedById = field.NewString(tableName, "owned_by_id")
_mediaMetadata.OwnedById = field.NewField(tableName, "owned_by_id")
_mediaMetadata.Remote = field.NewBool(tableName, "remote")
_mediaMetadata.Location = field.NewString(tableName, "location")
_mediaMetadata.Type = field.NewString(tableName, "type")
@ -51,7 +51,7 @@ type mediaMetadata struct {
CreatedAt field.Time
UpdatedAt field.Time
DeletedAt field.Field
OwnedById field.String
OwnedById field.Field
Remote field.Bool
Location field.String
Type field.String
@ -78,7 +78,7 @@ func (m *mediaMetadata) updateTableName(table string) *mediaMetadata {
m.CreatedAt = field.NewTime(table, "created_at")
m.UpdatedAt = field.NewTime(table, "updated_at")
m.DeletedAt = field.NewField(table, "deleted_at")
m.OwnedById = field.NewString(table, "owned_by_id")
m.OwnedById = field.NewField(table, "owned_by_id")
m.Remote = field.NewBool(table, "remote")
m.Location = field.NewString(table, "location")
m.Type = field.NewString(table, "type")

View file

@ -35,6 +35,12 @@ func newNoteTag(db *gorm.DB, opts ...gen.DOOption) noteTag {
RelationField: field.NewRelation("Note", "models.Note"),
Creator: struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -100,6 +106,19 @@ func newNoteTag(db *gorm.DB, opts ...gen.DOOption) noteTag {
}
}{
RelationField: field.NewRelation("Note.Creator", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Note.Creator.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("Note.Creator.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -269,9 +288,6 @@ func newNoteTag(db *gorm.DB, opts ...gen.DOOption) noteTag {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}{
@ -288,9 +304,6 @@ func newNoteTag(db *gorm.DB, opts ...gen.DOOption) noteTag {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote", "models.Emote"),
@ -301,16 +314,8 @@ func newNoteTag(db *gorm.DB, opts ...gen.DOOption) noteTag {
},
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote.Server.Icon", "models.MediaMetadata"),
},
},
},
},
@ -422,6 +427,12 @@ type noteTagBelongsToNote struct {
Creator struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -507,9 +518,6 @@ type noteTagBelongsToNote struct {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}

View file

@ -35,6 +35,12 @@ func newNoteToAttachment(db *gorm.DB, opts ...gen.DOOption) noteToAttachment {
RelationField: field.NewRelation("Note", "models.Note"),
Creator: struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -100,6 +106,19 @@ func newNoteToAttachment(db *gorm.DB, opts ...gen.DOOption) noteToAttachment {
}
}{
RelationField: field.NewRelation("Note.Creator", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Note.Creator.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("Note.Creator.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -269,9 +288,6 @@ func newNoteToAttachment(db *gorm.DB, opts ...gen.DOOption) noteToAttachment {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}{
@ -288,9 +304,6 @@ func newNoteToAttachment(db *gorm.DB, opts ...gen.DOOption) noteToAttachment {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote", "models.Emote"),
@ -301,16 +314,8 @@ func newNoteToAttachment(db *gorm.DB, opts ...gen.DOOption) noteToAttachment {
},
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote.Server.Icon", "models.MediaMetadata"),
},
},
},
},
@ -430,6 +435,12 @@ type noteToAttachmentBelongsToNote struct {
Creator struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -515,9 +526,6 @@ type noteToAttachmentBelongsToNote struct {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}

View file

@ -34,6 +34,19 @@ func newNoteToBoost(db *gorm.DB, opts ...gen.DOOption) noteToBoost {
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("User", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("User.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("User.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -213,9 +226,6 @@ func newNoteToBoost(db *gorm.DB, opts ...gen.DOOption) noteToBoost {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}{
@ -232,9 +242,6 @@ func newNoteToBoost(db *gorm.DB, opts ...gen.DOOption) noteToBoost {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote", "models.Emote"),
@ -245,16 +252,8 @@ func newNoteToBoost(db *gorm.DB, opts ...gen.DOOption) noteToBoost {
},
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote.Server.Icon", "models.MediaMetadata"),
},
},
},
},
@ -369,6 +368,12 @@ type noteToBoostBelongsToUser struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -528,9 +533,6 @@ type noteToBoostBelongsToNote struct {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}

View file

@ -35,6 +35,12 @@ func newNoteToEmote(db *gorm.DB, opts ...gen.DOOption) noteToEmote {
RelationField: field.NewRelation("Note", "models.Note"),
Creator: struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -100,6 +106,19 @@ func newNoteToEmote(db *gorm.DB, opts ...gen.DOOption) noteToEmote {
}
}{
RelationField: field.NewRelation("Note.Creator", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Note.Creator.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("Note.Creator.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -269,9 +288,6 @@ func newNoteToEmote(db *gorm.DB, opts ...gen.DOOption) noteToEmote {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}{
@ -288,9 +304,6 @@ func newNoteToEmote(db *gorm.DB, opts ...gen.DOOption) noteToEmote {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote", "models.Emote"),
@ -301,16 +314,8 @@ func newNoteToEmote(db *gorm.DB, opts ...gen.DOOption) noteToEmote {
},
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote.Server.Icon", "models.MediaMetadata"),
},
},
},
},
@ -430,6 +435,12 @@ type noteToEmoteBelongsToNote struct {
Creator struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -515,9 +526,6 @@ type noteToEmoteBelongsToNote struct {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}

View file

@ -35,6 +35,12 @@ func newNoteToFeed(db *gorm.DB, opts ...gen.DOOption) noteToFeed {
RelationField: field.NewRelation("Note", "models.Note"),
Creator: struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -100,6 +106,19 @@ func newNoteToFeed(db *gorm.DB, opts ...gen.DOOption) noteToFeed {
}
}{
RelationField: field.NewRelation("Note.Creator", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Note.Creator.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("Note.Creator.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -269,9 +288,6 @@ func newNoteToFeed(db *gorm.DB, opts ...gen.DOOption) noteToFeed {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}{
@ -288,9 +304,6 @@ func newNoteToFeed(db *gorm.DB, opts ...gen.DOOption) noteToFeed {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote", "models.Emote"),
@ -301,16 +314,8 @@ func newNoteToFeed(db *gorm.DB, opts ...gen.DOOption) noteToFeed {
},
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote.Server.Icon", "models.MediaMetadata"),
},
},
},
},
@ -422,6 +427,12 @@ type noteToFeedBelongsToNote struct {
Creator struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -507,9 +518,6 @@ type noteToFeedBelongsToNote struct {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}

View file

@ -35,6 +35,12 @@ func newNoteToPing(db *gorm.DB, opts ...gen.DOOption) noteToPing {
RelationField: field.NewRelation("Note", "models.Note"),
Creator: struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -100,6 +106,19 @@ func newNoteToPing(db *gorm.DB, opts ...gen.DOOption) noteToPing {
}
}{
RelationField: field.NewRelation("Note.Creator", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Note.Creator.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("Note.Creator.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -269,9 +288,6 @@ func newNoteToPing(db *gorm.DB, opts ...gen.DOOption) noteToPing {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}{
@ -288,9 +304,6 @@ func newNoteToPing(db *gorm.DB, opts ...gen.DOOption) noteToPing {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote", "models.Emote"),
@ -301,16 +314,8 @@ func newNoteToPing(db *gorm.DB, opts ...gen.DOOption) noteToPing {
},
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote.Server.Icon", "models.MediaMetadata"),
},
},
},
},
@ -430,6 +435,12 @@ type noteToPingBelongsToNote struct {
Creator struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -515,9 +526,6 @@ type noteToPingBelongsToNote struct {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}

View file

@ -46,6 +46,12 @@ func newNote(db *gorm.DB, opts ...gen.DOOption) note {
field.RelationField
Creator struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -125,9 +131,6 @@ func newNote(db *gorm.DB, opts ...gen.DOOption) note {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}
@ -150,6 +153,12 @@ func newNote(db *gorm.DB, opts ...gen.DOOption) note {
RelationField: field.NewRelation("AttachmentRelations.Note", "models.Note"),
Creator: struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -215,6 +224,19 @@ func newNote(db *gorm.DB, opts ...gen.DOOption) note {
}
}{
RelationField: field.NewRelation("AttachmentRelations.Note.Creator", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("AttachmentRelations.Note.Creator.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("AttachmentRelations.Note.Creator.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -368,9 +390,6 @@ func newNote(db *gorm.DB, opts ...gen.DOOption) note {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}{
@ -387,9 +406,6 @@ func newNote(db *gorm.DB, opts ...gen.DOOption) note {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}{
RelationField: field.NewRelation("AttachmentRelations.Note.EmoteRelations.Emote", "models.Emote"),
@ -400,16 +416,8 @@ func newNote(db *gorm.DB, opts ...gen.DOOption) note {
},
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("AttachmentRelations.Note.EmoteRelations.Emote.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("AttachmentRelations.Note.EmoteRelations.Emote.Server.Icon", "models.MediaMetadata"),
},
},
},
},
@ -588,6 +596,12 @@ type noteHasManyAttachmentRelations struct {
field.RelationField
Creator struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -667,9 +681,6 @@ type noteHasManyAttachmentRelations struct {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}

View file

@ -40,6 +40,19 @@ func newNotification(db *gorm.DB, opts ...gen.DOOption) notification {
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("ForUser", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("ForUser.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("ForUser.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -219,9 +232,6 @@ func newNotification(db *gorm.DB, opts ...gen.DOOption) notification {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}{
@ -238,9 +248,6 @@ func newNotification(db *gorm.DB, opts ...gen.DOOption) notification {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}{
RelationField: field.NewRelation("SourceNote.EmoteRelations.Emote", "models.Emote"),
@ -251,16 +258,8 @@ func newNotification(db *gorm.DB, opts ...gen.DOOption) notification {
},
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("SourceNote.EmoteRelations.Emote.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("SourceNote.EmoteRelations.Emote.Server.Icon", "models.MediaMetadata"),
},
},
},
},
@ -398,6 +397,12 @@ type notificationBelongsToForUser struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -557,9 +562,6 @@ type notificationBelongsToSourceNote struct {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}

View file

@ -39,6 +39,12 @@ func newReaction(db *gorm.DB, opts ...gen.DOOption) reaction {
RelationField: field.NewRelation("Note", "models.Note"),
Creator: struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -104,6 +110,19 @@ func newReaction(db *gorm.DB, opts ...gen.DOOption) reaction {
}
}{
RelationField: field.NewRelation("Note.Creator", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Note.Creator.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("Note.Creator.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -273,9 +292,6 @@ func newReaction(db *gorm.DB, opts ...gen.DOOption) reaction {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}{
@ -292,9 +308,6 @@ func newReaction(db *gorm.DB, opts ...gen.DOOption) reaction {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote", "models.Emote"),
@ -305,16 +318,8 @@ func newReaction(db *gorm.DB, opts ...gen.DOOption) reaction {
},
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("Note.EmoteRelations.Emote.Server.Icon", "models.MediaMetadata"),
},
},
},
},
@ -454,6 +459,12 @@ type reactionBelongsToNote struct {
Creator struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -539,9 +550,6 @@ type reactionBelongsToNote struct {
}
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
}
}

View file

@ -38,6 +38,19 @@ func newUserAuthMethod(db *gorm.DB, opts ...gen.DOOption) userAuthMethod {
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("User", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("User.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("User.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -260,6 +273,12 @@ type userAuthMethodBelongsToUser struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}

View file

@ -39,6 +39,19 @@ func newUserInfoField(db *gorm.DB, opts ...gen.DOOption) userInfoField {
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("User", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("User.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("User.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -264,6 +277,12 @@ type userInfoFieldBelongsToUser struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}

View file

@ -43,6 +43,19 @@ func newUserRemoteLinks(db *gorm.DB, opts ...gen.DOOption) userRemoteLinks {
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("User", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("User.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("User.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -280,6 +293,12 @@ type userRemoteLinksBelongsToUser struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}

View file

@ -33,6 +33,19 @@ func newUserToBeing(db *gorm.DB, opts ...gen.DOOption) userToBeing {
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("User", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("User.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("User.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -240,6 +253,12 @@ type userToBeingBelongsToUser struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}

View file

@ -28,11 +28,24 @@ func newUserToPronoun(db *gorm.DB, opts ...gen.DOOption) userToPronoun {
_userToPronoun.ALL = field.NewAsterisk(tableName)
_userToPronoun.ID = field.NewUint64(tableName, "id")
_userToPronoun.UserId = field.NewString(tableName, "user_id")
_userToPronoun.Pronoung = field.NewString(tableName, "pronoung")
_userToPronoun.Pronoun = field.NewString(tableName, "pronoun")
_userToPronoun.User = userToPronounBelongsToUser{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("User", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("User.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("User.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -178,11 +191,11 @@ func newUserToPronoun(db *gorm.DB, opts ...gen.DOOption) userToPronoun {
type userToPronoun struct {
userToPronounDo
ALL field.Asterisk
ID field.Uint64
UserId field.String
Pronoung field.String
User userToPronounBelongsToUser
ALL field.Asterisk
ID field.Uint64
UserId field.String
Pronoun field.String
User userToPronounBelongsToUser
fieldMap map[string]field.Expr
}
@ -201,7 +214,7 @@ func (u *userToPronoun) updateTableName(table string) *userToPronoun {
u.ALL = field.NewAsterisk(table)
u.ID = field.NewUint64(table, "id")
u.UserId = field.NewString(table, "user_id")
u.Pronoung = field.NewString(table, "pronoung")
u.Pronoun = field.NewString(table, "pronoun")
u.fillFieldMap()
@ -221,7 +234,7 @@ func (u *userToPronoun) fillFieldMap() {
u.fieldMap = make(map[string]field.Expr, 4)
u.fieldMap["id"] = u.ID
u.fieldMap["user_id"] = u.UserId
u.fieldMap["pronoung"] = u.Pronoung
u.fieldMap["pronoun"] = u.Pronoun
}
@ -240,6 +253,12 @@ type userToPronounBelongsToUser struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}

View file

@ -33,6 +33,19 @@ func newUserToRole(db *gorm.DB, opts ...gen.DOOption) userToRole {
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("User", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("User.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("User.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -248,6 +261,12 @@ type userToRoleBelongsToUser struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}

View file

@ -33,6 +33,19 @@ func newUserToTag(db *gorm.DB, opts ...gen.DOOption) userToTag {
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("User", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("User.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("User.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -240,6 +253,12 @@ type userToTagBelongsToUser struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}

View file

@ -34,6 +34,19 @@ func newUserToUserRelation(db *gorm.DB, opts ...gen.DOOption) userToUserRelation
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("User", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("User.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("User.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -252,6 +265,12 @@ type userToUserRelationBelongsToUser struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}

View file

@ -36,7 +36,7 @@ func newUser(db *gorm.DB, opts ...gen.DOOption) user {
_user.DisplayName = field.NewString(tableName, "display_name")
_user.Description = field.NewString(tableName, "description")
_user.IsBot = field.NewBool(tableName, "is_bot")
_user.IconId = field.NewString(tableName, "icon_id")
_user.IconId = field.NewField(tableName, "icon_id")
_user.BackgroundId = field.NewField(tableName, "background_id")
_user.BannerId = field.NewField(tableName, "banner_id")
_user.Indexable = field.NewBool(tableName, "indexable")
@ -47,12 +47,19 @@ func newUser(db *gorm.DB, opts ...gen.DOOption) user {
_user.Verified = field.NewBool(tableName, "verified")
_user.PasskeyId = field.NewBytes(tableName, "passkey_id")
_user.FinishedRegistration = field.NewBool(tableName, "finished_registration")
_user.PrivateKey = field.NewBytes(tableName, "private_key")
_user.RemoteInfo = userHasOneRemoteInfo{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("RemoteInfo", "models.UserRemoteLinks"),
User: struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -115,6 +122,19 @@ func newUser(db *gorm.DB, opts ...gen.DOOption) user {
}
}{
RelationField: field.NewRelation("RemoteInfo.User", "models.User"),
Server: struct {
field.RelationField
Icon struct {
field.RelationField
}
}{
RelationField: field.NewRelation("RemoteInfo.User.Server", "models.RemoteServer"),
Icon: struct {
field.RelationField
}{
RelationField: field.NewRelation("RemoteInfo.User.Server.Icon", "models.MediaMetadata"),
},
},
Icon: struct {
field.RelationField
}{
@ -287,6 +307,12 @@ func newUser(db *gorm.DB, opts ...gen.DOOption) user {
RelationField: field.NewRelation("AuthMethods", "models.UserAuthMethod"),
}
_user.Server = userBelongsToServer{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("Server", "models.RemoteServer"),
}
_user.Icon = userBelongsToIcon{
db: db.Session(&gorm.Session{}),
@ -323,7 +349,7 @@ type user struct {
DisplayName field.String
Description field.String
IsBot field.Bool
IconId field.String
IconId field.Field
BackgroundId field.Field
BannerId field.Field
Indexable field.Bool
@ -334,6 +360,7 @@ type user struct {
Verified field.Bool
PasskeyId field.Bytes
FinishedRegistration field.Bool
PrivateKey field.Bytes
RemoteInfo userHasOneRemoteInfo
InfoFields userHasManyInfoFields
@ -350,6 +377,8 @@ type user struct {
AuthMethods userHasManyAuthMethods
Server userBelongsToServer
Icon userBelongsToIcon
Background userBelongsToBackground
@ -380,7 +409,7 @@ func (u *user) updateTableName(table string) *user {
u.DisplayName = field.NewString(table, "display_name")
u.Description = field.NewString(table, "description")
u.IsBot = field.NewBool(table, "is_bot")
u.IconId = field.NewString(table, "icon_id")
u.IconId = field.NewField(table, "icon_id")
u.BackgroundId = field.NewField(table, "background_id")
u.BannerId = field.NewField(table, "banner_id")
u.Indexable = field.NewBool(table, "indexable")
@ -391,6 +420,7 @@ func (u *user) updateTableName(table string) *user {
u.Verified = field.NewBool(table, "verified")
u.PasskeyId = field.NewBytes(table, "passkey_id")
u.FinishedRegistration = field.NewBool(table, "finished_registration")
u.PrivateKey = field.NewBytes(table, "private_key")
u.fillFieldMap()
@ -407,7 +437,7 @@ func (u *user) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
}
func (u *user) fillFieldMap() {
u.fieldMap = make(map[string]field.Expr, 31)
u.fieldMap = make(map[string]field.Expr, 33)
u.fieldMap["id"] = u.ID
u.fieldMap["username"] = u.Username
u.fieldMap["created_at"] = u.CreatedAt
@ -428,6 +458,7 @@ func (u *user) fillFieldMap() {
u.fieldMap["verified"] = u.Verified
u.fieldMap["passkey_id"] = u.PasskeyId
u.fieldMap["finished_registration"] = u.FinishedRegistration
u.fieldMap["private_key"] = u.PrivateKey
}
@ -448,6 +479,12 @@ type userHasOneRemoteInfo struct {
User struct {
field.RelationField
Server struct {
field.RelationField
Icon struct {
field.RelationField
}
}
Icon struct {
field.RelationField
}
@ -1073,6 +1110,77 @@ func (a userHasManyAuthMethodsTx) Count() int64 {
return a.tx.Count()
}
type userBelongsToServer struct {
db *gorm.DB
field.RelationField
}
func (a userBelongsToServer) Where(conds ...field.Expr) *userBelongsToServer {
if len(conds) == 0 {
return &a
}
exprs := make([]clause.Expression, 0, len(conds))
for _, cond := range conds {
exprs = append(exprs, cond.BeCond().(clause.Expression))
}
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
return &a
}
func (a userBelongsToServer) WithContext(ctx context.Context) *userBelongsToServer {
a.db = a.db.WithContext(ctx)
return &a
}
func (a userBelongsToServer) Session(session *gorm.Session) *userBelongsToServer {
a.db = a.db.Session(session)
return &a
}
func (a userBelongsToServer) Model(m *models.User) *userBelongsToServerTx {
return &userBelongsToServerTx{a.db.Model(m).Association(a.Name())}
}
type userBelongsToServerTx struct{ tx *gorm.Association }
func (a userBelongsToServerTx) Find() (result *models.RemoteServer, err error) {
return result, a.tx.Find(&result)
}
func (a userBelongsToServerTx) Append(values ...*models.RemoteServer) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Append(targetValues...)
}
func (a userBelongsToServerTx) Replace(values ...*models.RemoteServer) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Replace(targetValues...)
}
func (a userBelongsToServerTx) Delete(values ...*models.RemoteServer) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Delete(targetValues...)
}
func (a userBelongsToServerTx) Clear() error {
return a.tx.Clear()
}
func (a userBelongsToServerTx) Count() int64 {
return a.tx.Count()
}
type userBelongsToIcon struct {
db *gorm.DB

View file

@ -1,6 +1,7 @@
package models
import (
"database/sql"
"time"
"gorm.io/gorm"
@ -18,7 +19,8 @@ type MediaMetadata struct {
// Soft delete means that this entry still exists in the db, but gorm won't include it anymore unless specifically told to
// If not null, this entry is marked as deleted
DeletedAt gorm.DeletedAt `gorm:"index"`
OwnedById string // Account id this media belongs to
// OwnedBy User
OwnedById sql.NullString // Account id this media belongs to
Remote bool // whether the attachment is a remote one
// Where the media is stored. Url
Location string
@ -37,14 +39,3 @@ type MediaMetadata struct {
// or embedded data, such as the default duck
// For external users, transform them into normal http URIs with a "special" path
const DefaultDuckLocationName = "linstrom://default-media"
var DefaultDuckMedia = MediaMetadata{
ID: "3b562a45-36b7-4c42-a944-3672f319ff7b",
OwnedById: "", // FIXME: Add default user ID here
Remote: false,
Location: DefaultDuckLocationName,
Type: "image/webp",
Name: "default-duck.webp",
AltText: "a greyscale image of a pidgeon on some smooth surface, likely a liquid. It is captioned with the text \"Duck\"",
Blurred: false,
}

View file

@ -3,7 +3,6 @@ package models
import (
"database/sql"
"git.mstar.dev/mstar/linstrom/config"
"gorm.io/gorm"
)
@ -18,17 +17,3 @@ type RemoteServer struct {
IconId sql.NullString // ID of a media file
IsSelf bool // Whether this server is yours truly
}
func BuildDefaultServer() *RemoteServer {
return &RemoteServer{
Model: gorm.Model{
ID: 1,
},
ServerType: ServerSoftwareLinstrom,
Domain: config.GlobalConfig.General.GetFullDomain(),
Name: config.GlobalConfig.Self.ServerDisplayName,
Icon: &DefaultDuckMedia,
IconId: sql.NullString{String: DefaultDuckMedia.ID, Valid: true},
IsSelf: true,
}
}

View file

@ -4,11 +4,13 @@ import (
"math"
"git.mstar.dev/mstar/goutils/other"
"gorm.io/gorm"
)
// Default role every user has. Defines sane defaults for a normal user
// Will get overwritten by just about every other role due to every other role having higher priority
var DefaultUserRole = Role{
Model: gorm.Model{ID: 1},
Name: "Default",
Priority: 0,
IsUserRole: false,
@ -67,6 +69,7 @@ var DefaultUserRole = Role{
// Role providing maximum permissions
var FullAdminRole = Role{
Model: gorm.Model{ID: 2},
Name: "fullAdmin",
Priority: math.MaxUint32,
IsUserRole: false,
@ -122,6 +125,7 @@ var FullAdminRole = Role{
// Role for totally freezing an account, blocking all activity from it
var AccountFreezeRole = Role{
Model: gorm.Model{ID: 3},
Name: "accountFreeze",
Priority: math.MaxUint32 - 1,
IsUserRole: false,
@ -179,6 +183,7 @@ var AccountFreezeRole = Role{
}
var ServerActorRole = Role{
Model: gorm.Model{ID: 4},
Name: "ServerActor",
Priority: math.MaxUint32,
IsUserRole: true,

View file

@ -6,8 +6,6 @@ import (
"gorm.io/gen"
"gorm.io/gorm"
"git.mstar.dev/mstar/linstrom/config"
)
// A user describes an account for creating content and events.
@ -44,8 +42,8 @@ type User struct {
DisplayName string // The display name of the user. Can be different from the handle
Description string // The description of a user account
IsBot bool // Whether to mark this account as a script controlled one
Icon MediaMetadata
IconId string // ID of a media file used as icon
Icon *MediaMetadata
IconId sql.NullString // ID of a media file used as icon
Background *MediaMetadata
BackgroundId sql.NullString // ID of a media file used as background image
Banner *MediaMetadata
@ -68,6 +66,7 @@ type User struct {
// saved space is worth
PasskeyId []byte
FinishedRegistration bool // Whether this account has completed registration yet
PrivateKey []byte
// ---- "Remote" linked values
InfoFields []UserInfoField
@ -80,25 +79,6 @@ type User struct {
AuthMethods []UserAuthMethod
}
func BuildLinstromUser() *User {
server := BuildDefaultServer()
return &User{
ID: "e3f4bb33-6d39-4349-b13e-f5c6fde56141",
Username: "linstrom",
Server: *server,
ServerId: server.ID,
DisplayName: config.GlobalConfig.Self.ServerActorDisplayName,
Description: "The default linstrom server user",
IsBot: true,
Icon: DefaultDuckMedia,
IconId: DefaultDuckMedia.ID,
Background: nil,
BackgroundId: sql.NullString{Valid: false},
Banner: nil,
BannerId: sql.NullString{Valid: false},
}
}
type IUser interface {
// Get a user by a username
//

View file

@ -4,8 +4,8 @@ package models
// Each user may have zero, one or more pronouns
// but each user to pronoun relation may appear at most once
type UserToPronoun struct {
ID uint64 `gorm:"primarykey"`
User User
UserId string
Pronoung string
ID uint64 `gorm:"primarykey"`
User User
UserId string
Pronoun string
}

154
storage-new/self.go Normal file
View file

@ -0,0 +1,154 @@
package storage
import (
"crypto/ed25519"
"crypto/rand"
"database/sql"
"git.mstar.dev/mstar/goutils/other"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
"git.mstar.dev/mstar/linstrom/config"
"git.mstar.dev/mstar/linstrom/storage-new/dbgen"
"git.mstar.dev/mstar/linstrom/storage-new/models"
)
func InsertSelf() error {
if err := insertRoles(); err != nil {
return other.Error("storage", "failed to save/update default roles", err)
}
duck, err := insertDefaultDuck()
if err != nil {
return other.Error("storage", "failed to save/update default duck", err)
}
server, err := insertServer(duck)
if err != nil {
return other.Error("storage", "failed to save/update self server", err)
}
log.Debug().Any("server", server).Send()
user, err := insertUser(server)
if err != nil {
return other.Error("storage", "failed to save/update self user", err)
}
if err = insertUserPronoun(user); err != nil {
return other.Error("storage", "failed to save/update self user pronoun", err)
}
return nil
}
func insertRoles() error {
return dbgen.Role.Save(models.AllDefaultRoles...)
}
func insertDefaultDuck() (*models.MediaMetadata, error) {
dbDuck, err := dbgen.MediaMetadata.Where(dbgen.MediaMetadata.Location.Eq(models.DefaultDuckLocationName)).
First()
if err == nil {
return dbDuck, nil
} else if err != gorm.ErrRecordNotFound {
return nil, err
}
duck := models.MediaMetadata{
ID: uuid.NewString(),
// NOTE: Default duck technically belongs to the server
// but to avoid cyclic dependency, declare it as unowned
OwnedById: sql.NullString{Valid: false},
Remote: false,
Location: models.DefaultDuckLocationName,
Type: "image/webp",
Name: "default-duck.webp",
AltText: "a greyscale image of a pidgeon on some smooth surface, likely a liquid. It is captioned with the text \"Duck\"",
Blurred: false,
}
if err := dbgen.MediaMetadata.Create(&duck); err != nil {
return nil, err
}
return &duck, nil
}
func insertServer(duck *models.MediaMetadata) (*models.RemoteServer, error) {
dbServer, err := dbgen.RemoteServer.Where(dbgen.RemoteServer.ID.Eq(1)).First()
if err == nil {
return dbServer, nil
} else if err != gorm.ErrRecordNotFound {
return nil, err
}
server := models.RemoteServer{
Model: gorm.Model{
ID: 1,
},
ServerType: models.ServerSoftwareLinstrom,
Domain: config.GlobalConfig.General.GetFullDomain(),
Name: config.GlobalConfig.Self.ServerDisplayName,
Icon: duck,
IconId: sql.NullString{String: duck.ID, Valid: true},
IsSelf: true,
}
err = dbgen.RemoteServer.Save(&server)
if err != nil {
return nil, err
}
return &server, nil
}
func insertUser(server *models.RemoteServer) (*models.User, error) {
dbUser, err := dbgen.User.GetByUsername("linstrom")
if err == nil {
return dbUser, nil
}
if err != gorm.ErrRecordNotFound {
return nil, err
}
publicKey, privateKey, err := ed25519.GenerateKey(nil)
pkeyId := make([]byte, 64)
_, err = rand.Read(pkeyId)
if err != nil {
return nil, other.Error(
"storage",
"failed to generate passkey ID for linstrom account",
err,
)
}
user := models.User{
Username: "linstrom",
Server: *server,
ServerId: server.ID,
DisplayName: config.GlobalConfig.Self.ServerActorDisplayName,
Description: "The default linstrom server user",
IsBot: true,
Icon: nil,
IconId: sql.NullString{Valid: false},
Background: nil,
BackgroundId: sql.NullString{Valid: false},
Banner: nil,
BannerId: sql.NullString{Valid: false},
Indexable: false,
PublicKey: publicKey,
PrivateKey: privateKey,
Verified: true,
FinishedRegistration: true,
PasskeyId: pkeyId,
}
err = dbgen.User.Save(&user)
if err != nil {
return nil, err
}
return &user, err
}
func insertUserPronoun(user *models.User) error {
_, err := dbgen.UserToPronoun.Where(dbgen.UserToPronoun.UserId.Eq(user.ID)).
Where(dbgen.UserToPronoun.Pronoun.Eq("Yes")).First()
switch err {
case nil:
return nil
case gorm.ErrRecordNotFound:
return dbgen.UserToPronoun.Create(
&models.UserToPronoun{UserId: user.ID, User: *user, Pronoun: "Yes"},
)
default:
return err
}
}

7
web-new/server.go Normal file
View file

@ -0,0 +1,7 @@
package web
import "net/http"
type Server struct {
server http.Server
}

31
webdebug/server.go Normal file
View file

@ -0,0 +1,31 @@
package webdebug
import (
"context"
"net/http"
)
const DebugAddr = "127.0.0.1:3305"
type Server struct {
server *http.Server
}
func New() *Server {
handler := http.NewServeMux()
web := http.Server{
Addr: DebugAddr,
Handler: handler,
}
return &Server{&web}
}
func (s *Server) Start() error {
if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
return err
}
return nil
}
func (s *Server) Stop() error {
return s.server.Shutdown(context.Background())
}