linstrom/web/shared/Note.go
mstar b0f041e7b0
Some checks failed
/ docker (push) Has been cancelled
Add-ish support for tags and mentions in new messages
2025-06-05 17:07:04 +02:00

79 lines
1.8 KiB
Go

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"
)
type Note struct {
// ---- Section public data
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
CreatorId string `json:"creator_id"`
ServerId uint `json:"server_id"`
RawContent string `json:"raw_content"`
ContentWarning *string `json:"content_warning"`
RepliesToId *string `json:"replies_to_id"`
QuotesId *string `json:"quotes_id"`
AccessLevel uint8 `json:"access_level"`
}
// Compile time interface implementation enforcement
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() {
}
// No test, no data processing, only copy
func (note *Note) Clone() shared.Clonable {
tmp := *note
return &tmp
}
// No test, no data processing, only copy
func (n *Note) FromModel(m *models.Note) {
n.ID = m.ID
n.CreatedAt = m.CreatedAt
n.CreatorId = m.CreatorId
n.ServerId = m.OriginId
n.RawContent = m.RawContent
if m.ContentWarning.Valid {
n.ContentWarning = &m.ContentWarning.String
} else {
n.ContentWarning = nil
}
if m.RepliesTo.Valid {
n.RepliesToId = &m.RepliesTo.String
} else {
n.RepliesToId = nil
}
if m.Quotes.Valid {
n.QuotesId = &m.Quotes.String
} else {
n.QuotesId = nil
}
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]
}
})
}