Add-ish support for tags and mentions in new messages
Some checks failed
/ docker (push) Has been cancelled
Some checks failed
/ docker (push) Has been cancelled
This commit is contained in:
parent
94106bb82f
commit
b0f041e7b0
14 changed files with 242 additions and 53 deletions
|
@ -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) {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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]
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue