diff --git a/activitypub/importNote.go b/activitypub/importNote.go index 245ca12..27e363a 100644 --- a/activitypub/importNote.go +++ b/activitypub/importNote.go @@ -6,12 +6,14 @@ import ( "errors" "fmt" "io" + "strings" "time" "git.mstar.dev/mstar/goutils/other" "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" webshared "git.mstar.dev/mstar/linstrom/web/shared" @@ -30,6 +32,11 @@ func importRemoteNoteRecursive( requester *models.User, recursionDepth uint, ) (string, error) { + type NoteTag struct { + Type string `json:"type"` + Href string `json:"href"` + Name string `json:"name"` + } type Note struct { Type string `json:"type"` Id string `json:"id"` @@ -42,11 +49,29 @@ func importRemoteNoteRecursive( InReplyTo *string `json:"inReplyTo"` Sensitive bool `json:"sensitive"` AttributedTo string `json:"attributedTo"` + Tags []NoteTag `json:"tag"` } // TODO: Decide whether the max recursion depth can be configured via config file if recursionDepth > DefaultMaxImportRecursion { return "", ErrMaxImportRecursionReached } + // No need to import local notes. Ids of local notes either have the full public url as prefix + // or no http prefix at all (internal id only) + if strings.HasPrefix(noteId, config.GlobalConfig.General.GetFullPublicUrl()) || + !strings.HasPrefix(noteId, "http") { + switch _, err := dbgen.Note.Where(dbgen.Note.ID.Eq(noteId)).First(); err { + case nil: + return noteId, nil + case gorm.ErrRecordNotFound: + return "", other.Error("activitypub", "local note doesn't exist", err) + default: + return "", other.Error( + "activitypub", + "failed to check for existence of local note", + err, + ) + } + } res, _, err := webshared.RequestSigned("GET", noteId, nil, requester) if err != nil { return "", other.Error("activitypub", "failed to request object", err) @@ -69,10 +94,12 @@ func importRemoteNoteRecursive( if err != nil { return "", other.Error("activitypub", "failed to import note author", err) } + // If the note already exists in storage, update that dbNote, err := dbgen.Note.Where(dbgen.Note.ID.Eq(data.Id)).First() switch err { case nil: case gorm.ErrRecordNotFound: + // Otherwise create a new one dbNote = &models.Note{ ID: data.Id, CreatorId: data.AttributedTo, @@ -120,5 +147,35 @@ func importRemoteNoteRecursive( if err != nil { return "", err } + // Handle tags after the initial note since stored in separate tables and pings require more imports + hashtags := []*models.NoteTag{} + pings := []*models.NoteToPing{} + for _, tag := range data.Tags { + switch tag.Type { + case "Mention": + _, err := ImportRemoteAccountByAPUrl(tag.Href) + if err != nil { + return "", err + } + pings = append(pings, &models.NoteToPing{ + NoteId: dbNote.ID, + PingTargetId: tag.Href, + }) + case "Hashtag": + hashtags = append(hashtags, &models.NoteTag{ + NoteId: dbNote.ID, + Tag: tag.Name, + TagUrl: tag.Href, + }) + default: + log.Warn().Str("tag-type", tag.Type).Msg("Unknown tag type") + } + } + // FIXME: This is bad, what if it's a note update and not a new one? + // For new notes this is fine, but existing ones might already have attachments. + // In which case, you need to remove tags that don't exist anymore + // and only create the ones not yet stored + err = dbgen.NoteToPing.Save(pings...) + err = dbgen.NoteTag.Save(hashtags...) return dbNote.ID, nil } diff --git a/auth-new/passkey.go b/auth-new/passkey.go index fe3e520..28b4fbc 100644 --- a/auth-new/passkey.go +++ b/auth-new/passkey.go @@ -146,6 +146,9 @@ func (a *Authenticator) StartPasskeyRegistration( } wrappedAcc := fakeUser{acc} options, session, err := a.webauthn.BeginRegistration(&wrappedAcc) + if err != nil { + return nil, "", err + } jsonSession, err := json.Marshal(session) if err != nil { return nil, "", other.Error("auth", "failed to marshal session to json", err) diff --git a/auth-new/password.go b/auth-new/password.go index 8994fbc..373509d 100644 --- a/auth-new/password.go +++ b/auth-new/password.go @@ -8,6 +8,7 @@ import ( "gorm.io/gorm" "gorm.io/gorm/clause" + "git.mstar.dev/mstar/linstrom/config" "git.mstar.dev/mstar/linstrom/shared" "git.mstar.dev/mstar/linstrom/storage-new/dbgen" "git.mstar.dev/mstar/linstrom/storage-new/models" @@ -42,7 +43,11 @@ func (a *Authenticator) PerformPasswordLogin( if method == nil { return LoginNextFailure, "", ErrUnsupportedAuthMethod } - if !comparePassword(password, method.Token) { + decrypted, err := Decrypt([]byte(config.GlobalConfig.Storage.EncryptionKey), method.Token) + if err != nil { + return 0, "", other.Error("auth", "failed to decrypt password hash", err) + } + if !comparePassword(password, decrypted) { return LoginNextFailure, "", ErrInvalidCombination } nextStates := ConvertNewStorageAuthMethodsToLoginState( @@ -118,6 +123,10 @@ func (a *Authenticator) PerformPasswordRegister(username, password string) error if err != nil { return other.Error("auth", "failed to hash password", err) } + encryptedPass, err := Encrypt([]byte(config.GlobalConfig.Storage.EncryptionKey), passwordHash) + if err != nil { + return other.Error("auth", "failed to encrypt password hash", err) + } passwordMethods := sliceutils.Filter( acc.AuthMethods, func(t models.UserAuthMethod) bool { return t.AuthMethod == models.AuthMethodPassword }, @@ -129,13 +138,13 @@ func (a *Authenticator) PerformPasswordRegister(username, password string) error // For now, do perform an update dbPass := passwordMethods[0] _, err = dbgen.UserAuthMethod.Where(dbgen.UserAuthMethod.ID.Eq(dbPass.ID)). - Update(dbgen.UserAuthMethod.Token, passwordHash) + Update(dbgen.UserAuthMethod.Token, encryptedPass) if err != nil { return other.Error("auth", "failed to update password", err) } } else { dbPass := models.UserAuthMethod{ - Token: passwordHash, + Token: encryptedPass, AuthMethod: models.AuthMethodPassword, User: *acc, UserId: acc.ID, diff --git a/go.mod b/go.mod index 9ff83e5..4fd25c6 100644 --- a/go.mod +++ b/go.mod @@ -53,7 +53,6 @@ require ( github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect - github.com/klauspost/cpuid/v2 v2.0.9 // indirect github.com/lestrrat-go/blackmagic v1.0.3 // indirect github.com/lestrrat-go/httpcc v1.0.1 // indirect github.com/lestrrat-go/httprc v1.0.6 // indirect @@ -63,14 +62,9 @@ require ( github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/miekg/dns v1.1.64 // indirect - github.com/minio/sha256-simd v1.0.0 // indirect - github.com/mr-tron/base58 v1.2.0 // indirect - github.com/multiformats/go-multihash v0.2.3 // indirect - github.com/multiformats/go-varint v0.0.6 // indirect github.com/onsi/ginkgo/v2 v2.9.5 // indirect github.com/quic-go/qpack v0.5.1 // indirect github.com/segmentio/asm v1.2.0 // indirect - github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/x448/float16 v0.8.4 // indirect github.com/yuin/gluamapper v0.0.0-20150323120927-d836955830e7 // indirect go.uber.org/mock v0.5.0 // indirect @@ -82,5 +76,4 @@ require ( gorm.io/datatypes v1.2.5 // indirect gorm.io/driver/mysql v1.5.7 // indirect gorm.io/hints v1.1.2 // indirect - lukechampine.com/blake3 v1.1.6 // indirect ) diff --git a/go.sum b/go.sum index 990926b..75b03db 100644 --- a/go.sum +++ b/go.sum @@ -77,9 +77,6 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= -github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4= -github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/kohkimakimoto/gluatemplate v0.0.0-20160815033744-d9e2c9d6b00f h1:CXJzfe/zhkWjXLAZItKA4BPHo4d8Fh7Hc4gqcLVyrWQ= github.com/kohkimakimoto/gluatemplate v0.0.0-20160815033744-d9e2c9d6b00f/go.mod h1:mepZlGlueX0FYzgC3KQMEuBBQuaAvdp8RUY+ZEe2fbI= github.com/layeh/gopher-json v0.0.0-20201124131017-552bb3c4c3bf h1:bg6J/5S/AeTz7K9i/luJRj31BJ8f+LgYwKQBSOZxSEM= @@ -110,16 +107,8 @@ github.com/microsoft/go-mssqldb v1.7.2 h1:CHkFJiObW7ItKTJfHo1QX7QBBD1iV+mn1eOyRP github.com/microsoft/go-mssqldb v1.7.2/go.mod h1:kOvZKUdrhhFQmxLZqbwUV0rHkNkZpthMITIb2Ko1IoA= github.com/miekg/dns v1.1.64 h1:wuZgD9wwCE6XMT05UU/mlSko71eRSXEAm2EbjQXLKnQ= github.com/miekg/dns v1.1.64/go.mod h1:Dzw9769uoKVaLuODMDZz9M6ynFU6Em65csPuoi8G0ck= -github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g= -github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= -github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o= -github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= -github.com/multiformats/go-multihash v0.2.3 h1:7Lyc8XfX/IY2jWb/gI7JP+o7JEq9hOa7BFvVU9RSh+U= -github.com/multiformats/go-multihash v0.2.3/go.mod h1:dXgKXCXjBzdscBLk9JkjINiEsCKRVch90MdaGiKsvSM= -github.com/multiformats/go-varint v0.0.6 h1:gk85QWKxh3TazbLxED/NlDVv8+q+ReFJk7Y2W/KhfNY= -github.com/multiformats/go-varint v0.0.6/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/nrednav/cuid2 v1.0.1 h1:aYLDCmGxEij7xCdiV6GVSPSlqFOS6sqHKKvBeKjddVY= github.com/nrednav/cuid2 v1.0.1/go.mod h1:nH9lUYqbtoVsnpy20etw5q1guTjE99Xy4EpmnK5nKm0= github.com/onsi/ginkgo/v2 v2.9.5 h1:+6Hr4uxzP4XIUyAkg61dWBw8lb/gc4/X5luuxN/EC+Q= @@ -144,8 +133,6 @@ github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys= github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs= github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= -github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= -github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -243,5 +230,3 @@ gorm.io/hints v1.1.2 h1:b5j0kwk5p4+3BtDtYqqfY+ATSxjj+6ptPgVveuynn9o= gorm.io/hints v1.1.2/go.mod h1:/ARdpUHAtyEMCh5NNi3tI7FsGh+Cj/MIUlvNxCNCFWg= gorm.io/plugin/dbresolver v1.5.3 h1:wFwINGZZmttuu9h7XpvbDHd8Lf9bb8GNzp/NpAMV2wU= gorm.io/plugin/dbresolver v1.5.3/go.mod h1:TSrVhaUg2DZAWP3PrHlDlITEJmNOkL0tFTjvTEsQ4XE= -lukechampine.com/blake3 v1.1.6 h1:H3cROdztr7RCfoaTpGZFQsrqvweFLrqS73j7L7cmR5c= -lukechampine.com/blake3 v1.1.6/go.mod h1:tkKEOtDkNtklkXtLNEOGNq5tcV90tJiA1vAA12R78LA= diff --git a/shared/signing.go b/shared/signing.go index 2fa6ee5..6ebb9c8 100644 --- a/shared/signing.go +++ b/shared/signing.go @@ -21,6 +21,9 @@ func GenerateKeypair(useEd bool) (publicKey []byte, privateKey []byte, err error return nil, nil, err } publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey) + if err != nil { + return nil, nil, err + } return publicKeyBytes, privateKey, nil } else { diff --git a/shared/signing_test.go b/shared/signing_test.go index 1b24585..cf65b31 100644 --- a/shared/signing_test.go +++ b/shared/signing_test.go @@ -33,7 +33,7 @@ func TestGenerateKeypairRSA(t *testing.T) { t.Fatalf("validation of private key failed: %v", err) } genPublicRaw := private.Public() - genPublic, ok := genPublicRaw.(*rsa.PublicKey) + genPublic, _ := genPublicRaw.(*rsa.PublicKey) if !reflect.DeepEqual(*public, *genPublic) { t.Fatal("public from generator and from private are different") } diff --git a/storage-new/models/NoteToTag.go b/storage-new/models/NoteToTag.go index f0b419c..a258734 100644 --- a/storage-new/models/NoteToTag.go +++ b/storage-new/models/NoteToTag.go @@ -8,4 +8,5 @@ type NoteTag struct { Note Note // The note containing a tag NoteId string Tag string // The tag contained + TagUrl string // Url to the tag "collection" } diff --git a/storage-new/models/RemoteServer.go b/storage-new/models/RemoteServer.go index 6f8df6a..659778a 100644 --- a/storage-new/models/RemoteServer.go +++ b/storage-new/models/RemoteServer.go @@ -10,10 +10,9 @@ import ( // This includes self too type RemoteServer struct { gorm.Model - // What software type the server is running. Useful for formatting. - // Groups various types together (ex. firefish, iceshrimp, sharkey, misskey => misskey) + // The software the server is based on (Mastodon+Glitch => Mastodon, Sharkey => Misskey, Akoma => Plemora, etc) ServerType ServerSoftwareType - SpecificType string // Specific type + SpecificType string // Specific software name (Sharkey, Iceshrimp, Akoma, etc) Version string Domain string // `gorm:"primaryKey"` // Domain the server exists under. Additional primary key Name string // What the server wants to be known as (usually same as url) diff --git a/web/debug/posts.go b/web/debug/posts.go index fe34feb..2aacdc7 100644 --- a/web/debug/posts.go +++ b/web/debug/posts.go @@ -23,8 +23,10 @@ import ( func postAs(w http.ResponseWriter, r *http.Request) { type Inbound struct { - Username string `json:"username"` - Content string `json:"content"` + Username string `json:"username"` + Content string `json:"content"` + ContentWarning *string `json:"content_warning"` + ReplyTo *string `json:"reply_to"` } log := hlog.FromRequest(r) dec := json.NewDecoder(r.Body) @@ -69,6 +71,34 @@ func postAs(w http.ResponseWriter, r *http.Request) { AccessLevel: models.NOTE_TARGET_PUBLIC, OriginId: 1, } + if data.ContentWarning != nil { + note.ContentWarning = sql.NullString{Valid: true, String: *data.ContentWarning} + } + if data.ReplyTo != nil { + note.RepliesTo = sql.NullString{Valid: true, String: *data.ReplyTo} + _, err = activitypub.ImportRemoteNote(*data.ReplyTo, user) + if err != nil { + log.Error(). + Err(err). + Str("remote-note-id", *data.ReplyTo). + Msg("Failed to import remote note that's being replied to") + _ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError) + return + } + } + mentions := webshared.MentionsFromContent(data.Content) + dbPings := []*models.NoteToPing{} + for _, mention := range mentions { + accId, err := activitypub.ImportRemoteAccountByHandle(mention) + if err != nil { + log.Warn().Err(err).Str("handle", mention).Msg("Failed to import pinged target") + continue + } + dbPings = append(dbPings, &models.NoteToPing{ + NoteId: note.ID, + PingTargetId: accId, + }) + } tx := dbgen.Q.Begin() err = tx.Note.Select( n.ID, @@ -91,6 +121,7 @@ func postAs(w http.ResponseWriter, r *http.Request) { _ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError) return } + err = tx.NoteToPing.Create(dbPings...) activity := models.Activity{ Id: shared.NewId(), Type: string(models.ActivityCreate), @@ -108,6 +139,7 @@ func postAs(w http.ResponseWriter, r *http.Request) { _ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError) return } + // TODO: Move everything past this into separate functions u2u := dbgen.UserToUserRelation links, err := u2u.GetFollowerInboxesForId(user.ID) if err != nil { @@ -132,9 +164,10 @@ func postAs(w http.ResponseWriter, r *http.Request) { for _, link := range links { log.Debug().Str("target-inbox", link).Msg("Sending message to") go func() { - res, err := webshared.RequestSignedCavage("POST", link, outData, user) + res, _, err := webshared.RequestSigned("POST", link, outData, user) if err != nil { log.Warn().Err(err).Str("link", link).Msg("Failed to send create to target inbox") + return } if res.StatusCode >= 400 { body, _ := io.ReadAll(res.Body) @@ -142,6 +175,36 @@ func postAs(w http.ResponseWriter, r *http.Request) { } }() } + go func() { + for _, ping := range dbPings { + go func() { + links, err := dbgen.UserRemoteLinks.Where(dbgen.UserRemoteLinks.UserId.Eq(ping.PingTargetId)). + First() + if err != nil { + log.Warn(). + Err(err). + Str("tagged-id", ping.PingTargetId). + Msg("Failed to get link for tagged account") + return + } + res, _, err := webshared.RequestSigned("POST", links.InboxLink, outData, user) + if err != nil { + log.Warn(). + Err(err). + Str("link", links.InboxLink). + Msg("Failed to send create to ping target inbox") + return + } + if res.StatusCode >= 400 { + body, _ := io.ReadAll(res.Body) + log.Warn(). + Int("status-code", res.StatusCode). + Bytes("body", body). + Msg("Bad reply") + } + }() + } + }() } func notesFrom(w http.ResponseWriter, r *http.Request) { diff --git a/web/public/api/activitypub/inbox.go b/web/public/api/activitypub/inbox.go index 280335b..f276e65 100644 --- a/web/public/api/activitypub/inbox.go +++ b/web/public/api/activitypub/inbox.go @@ -389,7 +389,7 @@ func handleFollow(w http.ResponseWriter, r *http.Request, object map[string]any) log.Error().Err(err).Msg("Failed to marshal accept") return } - res, err := webshared.RequestSignedCavage( + res, _, err := webshared.RequestSigned( "POST", follower.RemoteInfo.InboxLink, body, diff --git a/web/public/api/activitypub/note.go b/web/public/api/activitypub/note.go index 92c29f8..29b9c7a 100644 --- a/web/public/api/activitypub/note.go +++ b/web/public/api/activitypub/note.go @@ -15,31 +15,39 @@ import ( "git.mstar.dev/mstar/linstrom/config" "git.mstar.dev/mstar/linstrom/storage-new" "git.mstar.dev/mstar/linstrom/storage-new/dbgen" + "git.mstar.dev/mstar/linstrom/storage-new/models" + webshared "git.mstar.dev/mstar/linstrom/web/shared" ) +type Tag struct { + Type string `json:"type"` + Href string `json:"href"` + Name string `json:"name"` +} + type ObjectNote struct { // Context should be set, if needed, by the endpoint handler Context any `json:"@context,omitempty"` // Attributes below set from storage - Id string `json:"id"` - Type string `json:"type"` - Summary *string `json:"summary"` - InReplyTo *string `json:"inReplyTo"` - Published time.Time `json:"published"` - Url string `json:"url"` - AttributedTo string `json:"attributedTo"` - To []string `json:"to"` - // CC []string `json:"cc"` // FIXME: Uncomment once followers collection implemented - Sensitive bool `json:"sensitive"` - AtomUri string `json:"atomUri"` - InReplyToAtomUri *string `json:"inReplyToAtomUri"` + Id string `json:"id"` + Type string `json:"type"` + Summary *string `json:"summary"` + InReplyTo *string `json:"inReplyTo"` + Published time.Time `json:"published"` + Url string `json:"url"` + AttributedTo string `json:"attributedTo"` + To []string `json:"to"` + CC []string `json:"cc"` + Sensitive bool `json:"sensitive"` + AtomUri string `json:"atomUri"` + InReplyToAtomUri *string `json:"inReplyToAtomUri"` // Conversation string `json:"conversation"` // FIXME: Uncomment once understood what this field wants Content string `json:"content"` // ContentMap map[string]string `json:"content_map"` // TODO: Uncomment once/if support for multiple languages available // Attachments []string `json:"attachments"` // FIXME: Change this to document type - // Tags []string `json:"tags"` // FIXME: Change this to hashtag type + Tags []Tag `json:"tag"` // Replies any `json:"replies"` // FIXME: Change this to collection type embedding first page // Likes any `json:"likes"` // FIXME: Change this to collection // Shares any `json:"shares"` // FIXME: Change this to collection, is boosts @@ -74,21 +82,42 @@ func objectNote(w http.ResponseWriter, r *http.Request) { } func NoteFromStorage(ctx context.Context, id string) (*ObjectNote, error) { - note, err := dbgen.Note.Where(dbgen.Note.ID.Eq(id)).Preload(dbgen.Note.Creator).First() + note, err := dbgen.Note.Where(dbgen.Note.ID.Eq(id)). + Preload(dbgen.Note.Creator). + Preload(dbgen.Note.PingRelations). + Preload(dbgen.Note.Tags). + First() if err != nil { return nil, err } + // TODO: Check access level, requires acting user to be included in function signature + publicUrlPrefix := config.GlobalConfig.General.GetFullPublicUrl() data := &ObjectNote{ - Id: config.GlobalConfig.General.GetFullPublicUrl() + "/api/activitypub/note/" + id, + Id: publicUrlPrefix + "/api/activitypub/note/" + id, Type: "Note", Published: note.CreatedAt, - AttributedTo: config.GlobalConfig.General.GetFullPublicUrl() + "/api/activitypub/user/" + note.CreatorId, + AttributedTo: publicUrlPrefix + "/api/activitypub/user/" + note.CreatorId, Content: note.RawContent, // FIXME: Escape content - Url: config.GlobalConfig.General.GetFullPublicUrl() + "/@" + note.Creator.Username + "/" + id, - To: []string{ + Url: publicUrlPrefix + "/@" + note.Creator.Username + "/" + id, + AtomUri: publicUrlPrefix + "/api/activitypub/object/" + id, + Tags: []Tag{}, + } + switch note.AccessLevel { + case models.NOTE_TARGET_PUBLIC: + data.To = []string{ "https://www.w3.org/ns/activitystreams#Public", - }, // FIXME: Replace with proper targets, not always public - AtomUri: config.GlobalConfig.General.GetFullPublicUrl() + "/api/activitypub/object/" + id, + } + data.CC = []string{ + fmt.Sprintf("%s/api/activitypub/user/%s/followers", publicUrlPrefix, note.CreatorId), + } + case models.NOTE_TARGET_HOME: + return nil, fmt.Errorf("home access level not implemented") + case models.NOTE_TARGET_FOLLOWERS: + return nil, fmt.Errorf("followers access level not implemented") + case models.NOTE_TARGET_DM: + return nil, fmt.Errorf("dm access level not implemented") + default: + return nil, fmt.Errorf("unknown access level %v", note.AccessLevel) } if note.RepliesTo.Valid { data.InReplyTo = ¬e.RepliesTo.String @@ -98,5 +127,23 @@ func NoteFromStorage(ctx context.Context, id string) (*ObjectNote, error) { data.Summary = ¬e.ContentWarning.String data.Sensitive = true } + for _, ping := range note.PingRelations { + target, err := dbgen.User.GetById(ping.PingTargetId) + if err != nil { + return nil, err + } + data.Tags = append(data.Tags, Tag{ + Type: "Mention", + Href: webshared.UserPublicUrl(target.ID), + Name: target.Username, + }) + } + for _, tag := range note.Tags { + data.Tags = append(data.Tags, Tag{ + Type: "Hashtag", + Href: tag.TagUrl, + Name: tag.Tag, + }) + } return data, nil } diff --git a/web/shared/Note.go b/web/shared/Note.go index 208f814..5ed9556 100644 --- a/web/shared/Note.go +++ b/web/shared/Note.go @@ -1,8 +1,11 @@ package webshared import ( + "regexp" "time" + "git.mstar.dev/mstar/goutils/sliceutils" + "git.mstar.dev/mstar/linstrom/shared" "git.mstar.dev/mstar/linstrom/storage-new/models" ) @@ -24,6 +27,8 @@ type Note struct { var _ shared.Clonable = &Note{} var _ shared.Sanitisable = &Note{} +var mentionsRegex = regexp.MustCompile(`@([a-zA-Z@\._0-9]+)`) + // No test, does nothing currently func (note *Note) Sanitize() { } @@ -58,3 +63,17 @@ func (n *Note) FromModel(m *models.Note) { } n.AccessLevel = uint8(m.AccessLevel) } + +func MentionsFromContent(content string) []string { + matches := mentionsRegex.FindAllStringSubmatch(content, -1) + if len(matches) == 0 { + return []string{} + } + return sliceutils.Map(matches, func(t []string) string { + if len(t) != 2 { + return "" + } else { + return t[1] + } + }) +} diff --git a/web/shared/User.go b/web/shared/User.go index 4cb8cbc..e3bfc59 100644 --- a/web/shared/User.go +++ b/web/shared/User.go @@ -1,7 +1,9 @@ package webshared import ( + "fmt" "slices" + "strings" "time" "git.mstar.dev/mstar/linstrom/config" @@ -118,3 +120,11 @@ func (u *User) FromModel(m *models.User) { u.Verified = &m.Verified u.FinishedRegistration = &m.FinishedRegistration } + +func UserPublicUrl(id string) string { + if strings.HasPrefix(id, "http") { + return id + } else { + return fmt.Sprintf("%s/api/activitypub/user/%s", config.GlobalConfig.General.GetFullPublicUrl(), id) + } +}