Mention fixup (#167)

* rework mention creation a bit

* rework mention creation a bit

* tidy up status dereferencing

* start adding tests for dereferencing

* fixups

* fix

* review changes
This commit is contained in:
tobi 2021-08-29 12:03:08 +02:00 committed by GitHub
commit 53507ac2a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 680 additions and 192 deletions

View file

@ -181,44 +181,62 @@ func (c *converter) ASStatusToStatus(ctx context.Context, statusable ap.Statusab
}
status.URI = uriProp.GetIRI().String()
l := c.log.WithField("statusURI", status.URI)
// web url for viewing this status
if statusURL, err := ap.ExtractURL(statusable); err == nil {
if statusURL, err := ap.ExtractURL(statusable); err != nil {
l.Infof("ASStatusToStatus: error extracting status URL: %s", err)
} else {
status.URL = statusURL.String()
}
// the html-formatted content of this status
if content, err := ap.ExtractContent(statusable); err == nil {
if content, err := ap.ExtractContent(statusable); err != nil {
l.Infof("ASStatusToStatus: error extracting status content: %s", err)
} else {
status.Content = content
}
// attachments to dereference and fetch later on (we don't do that here)
if attachments, err := ap.ExtractAttachments(statusable); err == nil {
if attachments, err := ap.ExtractAttachments(statusable); err != nil {
l.Infof("ASStatusToStatus: error extracting status attachments: %s", err)
} else {
status.Attachments = attachments
}
// hashtags to dereference later on
if hashtags, err := ap.ExtractHashtags(statusable); err == nil {
if hashtags, err := ap.ExtractHashtags(statusable); err != nil {
l.Infof("ASStatusToStatus: error extracting status hashtags: %s", err)
} else {
status.Tags = hashtags
}
// emojis to dereference and fetch later on
if emojis, err := ap.ExtractEmojis(statusable); err == nil {
if emojis, err := ap.ExtractEmojis(statusable); err != nil {
l.Infof("ASStatusToStatus: error extracting status emojis: %s", err)
} else {
status.Emojis = emojis
}
// mentions to dereference later on
if mentions, err := ap.ExtractMentions(statusable); err == nil {
if mentions, err := ap.ExtractMentions(statusable); err != nil {
l.Infof("ASStatusToStatus: error extracting status mentions: %s", err)
} else {
status.Mentions = mentions
}
// cw string for this status
if cw, err := ap.ExtractSummary(statusable); err == nil {
if cw, err := ap.ExtractSummary(statusable); err != nil {
l.Infof("ASStatusToStatus: error extracting status summary: %s", err)
} else {
status.ContentWarning = cw
}
// when was this status created?
published, err := ap.ExtractPublished(statusable)
if err == nil {
if err != nil {
l.Infof("ASStatusToStatus: error extracting status published: %s", err)
} else {
status.CreatedAt = published
status.UpdatedAt = published
}

View file

@ -338,7 +338,7 @@ func (suite *ASToInternalTestSuite) SetupSuite() {
suite.log = testrig.NewTestLog()
suite.accounts = testrig.NewTestAccounts()
suite.people = testrig.NewTestFediPeople()
suite.typeconverter = typeutils.NewConverter(suite.config, suite.db)
suite.typeconverter = typeutils.NewConverter(suite.config, suite.db, suite.log)
}
func (suite *ASToInternalTestSuite) SetupTest() {
@ -346,7 +346,7 @@ func (suite *ASToInternalTestSuite) SetupTest() {
}
func (suite *ASToInternalTestSuite) TestParsePerson() {
testPerson := suite.people["new_person_1"]
testPerson := suite.people["https://unknown-instance.com/users/brand_new_person"]
acct, err := suite.typeconverter.ASRepresentationToAccount(context.Background(), testPerson, false)
assert.NoError(suite.T(), err)
@ -363,8 +363,6 @@ func (suite *ASToInternalTestSuite) TestParsePerson() {
suite.Equal("https://unknown-instance.com/@brand_new_person", acct.URL)
suite.True(acct.Discoverable)
suite.Equal("https://unknown-instance.com/users/brand_new_person#main-key", acct.PublicKeyURI)
suite.Equal("https://unknown-instance.com/media/some_avatar_filename.jpeg", acct.AvatarRemoteURL)
suite.Equal("https://unknown-instance.com/media/some_header_filename.jpeg", acct.HeaderRemoteURL)
suite.False(acct.Locked)
}

View file

@ -23,6 +23,7 @@ import (
"net/url"
"github.com/go-fed/activity/streams/vocab"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/ap"
"github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/cache"
@ -179,15 +180,17 @@ type TypeConverter interface {
type converter struct {
config *config.Config
db db.DB
log *logrus.Logger
frontendCache cache.Cache
asCache cache.Cache
}
// NewConverter returns a new Converter
func NewConverter(config *config.Config, db db.DB) TypeConverter {
func NewConverter(config *config.Config, db db.DB, log *logrus.Logger) TypeConverter {
return &converter{
config: config,
db: db,
log: log,
frontendCache: cache.New(),
asCache: cache.New(),
}

View file

@ -19,9 +19,9 @@
package typeutils_test
import (
"github.com/go-fed/activity/streams/vocab"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/ap"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@ -35,7 +35,7 @@ type ConverterStandardTestSuite struct {
db db.DB
log *logrus.Logger
accounts map[string]*gtsmodel.Account
people map[string]ap.Accountable
people map[string]vocab.ActivityStreamsPerson
typeconverter typeutils.TypeConverter
}

View file

@ -27,7 +27,6 @@ import (
"github.com/go-fed/activity/streams"
"github.com/go-fed/activity/streams/vocab"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
@ -600,12 +599,12 @@ func (c *converter) FollowToAS(ctx context.Context, f *gtsmodel.Follow, originAc
}
func (c *converter) MentionToAS(ctx context.Context, m *gtsmodel.Mention) (vocab.ActivityStreamsMention, error) {
if m.OriginAccount == nil {
a := &gtsmodel.Account{}
if err := c.db.GetWhere(ctx, []db.Where{{Key: "target_account_id", Value: m.TargetAccountID}}, a); err != nil {
if m.TargetAccount == nil {
a, err := c.db.GetAccountByID(ctx, m.TargetAccountID)
if err != nil {
return nil, fmt.Errorf("MentionToAS: error getting target account from db: %s", err)
}
m.OriginAccount = a
m.TargetAccount = a
}
// create the mention
@ -613,21 +612,21 @@ func (c *converter) MentionToAS(ctx context.Context, m *gtsmodel.Mention) (vocab
// href -- this should be the URI of the mentioned user
hrefProp := streams.NewActivityStreamsHrefProperty()
hrefURI, err := url.Parse(m.OriginAccount.URI)
hrefURI, err := url.Parse(m.TargetAccount.URI)
if err != nil {
return nil, fmt.Errorf("MentionToAS: error parsing uri %s: %s", m.OriginAccount.URI, err)
return nil, fmt.Errorf("MentionToAS: error parsing uri %s: %s", m.TargetAccount.URI, err)
}
hrefProp.SetIRI(hrefURI)
mention.SetActivityStreamsHref(hrefProp)
// name -- this should be the namestring of the mentioned user, something like @whatever@example.org
var domain string
if m.OriginAccount.Domain == "" {
if m.TargetAccount.Domain == "" {
domain = c.config.AccountDomain
} else {
domain = m.OriginAccount.Domain
domain = m.TargetAccount.Domain
}
username := m.OriginAccount.Username
username := m.TargetAccount.Username
nameString := fmt.Sprintf("@%s@%s", username, domain)
nameProp := streams.NewActivityStreamsNameProperty()
nameProp.AppendXMLSchemaString(nameString)
@ -684,8 +683,8 @@ func (c *converter) AttachmentToAS(ctx context.Context, a *gtsmodel.MediaAttachm
func (c *converter) FaveToAS(ctx context.Context, f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, error) {
// check if targetStatus is already pinned to this fave, and fetch it if not
if f.Status == nil {
s := &gtsmodel.Status{}
if err := c.db.GetByID(ctx, f.StatusID, s); err != nil {
s, err := c.db.GetStatusByID(ctx, f.StatusID)
if err != nil {
return nil, fmt.Errorf("FaveToAS: error fetching target status from database: %s", err)
}
f.Status = s
@ -693,8 +692,8 @@ func (c *converter) FaveToAS(ctx context.Context, f *gtsmodel.StatusFave) (vocab
// check if the targetAccount is already pinned to this fave, and fetch it if not
if f.TargetAccount == nil {
a := &gtsmodel.Account{}
if err := c.db.GetByID(ctx, f.TargetAccountID, a); err != nil {
a, err := c.db.GetAccountByID(ctx, f.TargetAccountID)
if err != nil {
return nil, fmt.Errorf("FaveToAS: error fetching target account from database: %s", err)
}
f.TargetAccount = a
@ -702,8 +701,8 @@ func (c *converter) FaveToAS(ctx context.Context, f *gtsmodel.StatusFave) (vocab
// check if the faving account is already pinned to this fave, and fetch it if not
if f.Account == nil {
a := &gtsmodel.Account{}
if err := c.db.GetByID(ctx, f.AccountID, a); err != nil {
a, err := c.db.GetAccountByID(ctx, f.AccountID)
if err != nil {
return nil, fmt.Errorf("FaveToAS: error fetching faving account from database: %s", err)
}
f.Account = a
@ -754,8 +753,8 @@ func (c *converter) FaveToAS(ctx context.Context, f *gtsmodel.StatusFave) (vocab
func (c *converter) BoostToAS(ctx context.Context, boostWrapperStatus *gtsmodel.Status, boostingAccount *gtsmodel.Account, boostedAccount *gtsmodel.Account) (vocab.ActivityStreamsAnnounce, error) {
// the boosted status is probably pinned to the boostWrapperStatus but double check to make sure
if boostWrapperStatus.BoostOf == nil {
b := &gtsmodel.Status{}
if err := c.db.GetByID(ctx, boostWrapperStatus.BoostOfID, b); err != nil {
b, err := c.db.GetStatusByID(ctx, boostWrapperStatus.BoostOfID)
if err != nil {
return nil, fmt.Errorf("BoostToAS: error getting status with ID %s from the db: %s", boostWrapperStatus.BoostOfID, err)
}
boostWrapperStatus.BoostOf = b
@ -837,16 +836,16 @@ func (c *converter) BoostToAS(ctx context.Context, boostWrapperStatus *gtsmodel.
*/
func (c *converter) BlockToAS(ctx context.Context, b *gtsmodel.Block) (vocab.ActivityStreamsBlock, error) {
if b.Account == nil {
a := &gtsmodel.Account{}
if err := c.db.GetByID(ctx, b.AccountID, a); err != nil {
return nil, fmt.Errorf("BlockToAS: error getting block account from database: %s", err)
a, err := c.db.GetAccountByID(ctx, b.AccountID)
if err != nil {
return nil, fmt.Errorf("BlockToAS: error getting block owner account from database: %s", err)
}
b.Account = a
}
if b.TargetAccount == nil {
a := &gtsmodel.Account{}
if err := c.db.GetByID(ctx, b.TargetAccountID, a); err != nil {
a, err := c.db.GetAccountByID(ctx, b.TargetAccountID)
if err != nil {
return nil, fmt.Errorf("BlockToAS: error getting block target account from database: %s", err)
}
b.TargetAccount = a

View file

@ -44,7 +44,7 @@ func (suite *InternalToASTestSuite) SetupSuite() {
suite.log = testrig.NewTestLog()
suite.accounts = testrig.NewTestAccounts()
suite.people = testrig.NewTestFediPeople()
suite.typeconverter = typeutils.NewConverter(suite.config, suite.db)
suite.typeconverter = typeutils.NewConverter(suite.config, suite.db, suite.log)
}
func (suite *InternalToASTestSuite) SetupTest() {

View file

@ -316,6 +316,8 @@ func (c *converter) TagToMasto(ctx context.Context, t *gtsmodel.Tag) (model.Tag,
}
func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) {
l := c.log
repliesCount, err := c.db.CountStatusReplies(ctx, s)
if err != nil {
return nil, fmt.Errorf("error counting replies: %s", err)
@ -392,7 +394,8 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque
for _, gtsAttachment := range s.Attachments {
mastoAttachment, err := c.AttachmentToMasto(ctx, gtsAttachment)
if err != nil {
return nil, fmt.Errorf("error converting attachment with id %s: %s", gtsAttachment.ID, err)
l.Errorf("error converting attachment with id %s: %s", gtsAttachment.ID, err)
continue
}
mastoAttachments = append(mastoAttachments, mastoAttachment)
}
@ -402,11 +405,13 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque
for _, aID := range s.AttachmentIDs {
gtsAttachment, err := c.db.GetAttachmentByID(ctx, aID)
if err != nil {
return nil, fmt.Errorf("error getting attachment with id %s: %s", aID, err)
l.Errorf("error getting attachment with id %s: %s", aID, err)
continue
}
mastoAttachment, err := c.AttachmentToMasto(ctx, gtsAttachment)
if err != nil {
return nil, fmt.Errorf("error converting attachment with id %s: %s", aID, err)
l.Errorf("error converting attachment with id %s: %s", aID, err)
continue
}
mastoAttachments = append(mastoAttachments, mastoAttachment)
}
@ -419,7 +424,8 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque
for _, gtsMention := range s.Mentions {
mastoMention, err := c.MentionToMasto(ctx, gtsMention)
if err != nil {
return nil, fmt.Errorf("error converting mention with id %s: %s", gtsMention.ID, err)
l.Errorf("error converting mention with id %s: %s", gtsMention.ID, err)
continue
}
mastoMentions = append(mastoMentions, mastoMention)
}
@ -429,11 +435,13 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque
for _, mID := range s.MentionIDs {
gtsMention, err := c.db.GetMention(ctx, mID)
if err != nil {
return nil, fmt.Errorf("error getting mention with id %s: %s", mID, err)
l.Errorf("error getting mention with id %s: %s", mID, err)
continue
}
mastoMention, err := c.MentionToMasto(ctx, gtsMention)
if err != nil {
return nil, fmt.Errorf("error converting mention with id %s: %s", gtsMention.ID, err)
l.Errorf("error converting mention with id %s: %s", gtsMention.ID, err)
continue
}
mastoMentions = append(mastoMentions, mastoMention)
}
@ -446,7 +454,8 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque
for _, gtsTag := range s.Tags {
mastoTag, err := c.TagToMasto(ctx, gtsTag)
if err != nil {
return nil, fmt.Errorf("error converting tag with id %s: %s", gtsTag.ID, err)
l.Errorf("error converting tag with id %s: %s", gtsTag.ID, err)
continue
}
mastoTags = append(mastoTags, mastoTag)
}
@ -456,11 +465,13 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque
for _, t := range s.TagIDs {
gtsTag := &gtsmodel.Tag{}
if err := c.db.GetByID(ctx, t, gtsTag); err != nil {
return nil, fmt.Errorf("error getting tag with id %s: %s", t, err)
l.Errorf("error getting tag with id %s: %s", t, err)
continue
}
mastoTag, err := c.TagToMasto(ctx, gtsTag)
if err != nil {
return nil, fmt.Errorf("error converting tag with id %s: %s", gtsTag.ID, err)
l.Errorf("error converting tag with id %s: %s", gtsTag.ID, err)
continue
}
mastoTags = append(mastoTags, mastoTag)
}
@ -473,7 +484,8 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque
for _, gtsEmoji := range s.Emojis {
mastoEmoji, err := c.EmojiToMasto(ctx, gtsEmoji)
if err != nil {
return nil, fmt.Errorf("error converting emoji with id %s: %s", gtsEmoji.ID, err)
l.Errorf("error converting emoji with id %s: %s", gtsEmoji.ID, err)
continue
}
mastoEmojis = append(mastoEmojis, mastoEmoji)
}
@ -483,11 +495,13 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque
for _, e := range s.EmojiIDs {
gtsEmoji := &gtsmodel.Emoji{}
if err := c.db.GetByID(ctx, e, gtsEmoji); err != nil {
return nil, fmt.Errorf("error getting emoji with id %s: %s", e, err)
l.Errorf("error getting emoji with id %s: %s", e, err)
continue
}
mastoEmoji, err := c.EmojiToMasto(ctx, gtsEmoji)
if err != nil {
return nil, fmt.Errorf("error converting emoji with id %s: %s", gtsEmoji.ID, err)
l.Errorf("error converting emoji with id %s: %s", gtsEmoji.ID, err)
continue
}
mastoEmojis = append(mastoEmojis, mastoEmoji)
}