Add-ish support for tags and mentions in new messages
Some checks failed
/ docker (push) Has been cancelled

This commit is contained in:
Melody Becker 2025-06-05 17:07:04 +02:00
parent 94106bb82f
commit b0f041e7b0
Signed by: mstar
SSH key fingerprint: SHA256:9VAo09aaVNTWKzPW7Hq2LW+ox9OdwmTSHRoD4mlz1yI
14 changed files with 242 additions and 53 deletions

View file

@ -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,

View file

@ -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 = &note.RepliesTo.String
@ -98,5 +127,23 @@ func NoteFromStorage(ctx context.Context, id string) (*ObjectNote, error) {
data.Summary = &note.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
}