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

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