Refactor/tidy (#261)

* tidy up streaming

* cut down code duplication

* test get followers/following

* test streaming processor

* fix some test models

* add TimeMustParse

* fix uri / url typo

* make trace logging less verbose

* make logging more consistent

* disable quote on logging

* remove context.Background

* remove many extraneous mastodon references

* regenerate swagger

* don't log query on no rows result

* log latency first for easier reading
This commit is contained in:
tobi
2021-10-04 15:24:19 +02:00
committed by GitHub
parent 9ce4234b9f
commit e04b187702
126 changed files with 1192 additions and 955 deletions

View File

@@ -29,9 +29,9 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
func (c *converter) AccountToMastoSensitive(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) {
func (c *converter) AccountToAPIAccountSensitive(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) {
// we can build this sensitive account easily by first getting the public account....
mastoAccount, err := c.AccountToMastoPublic(ctx, a)
apiAccount, err := c.AccountToAPIAccountPublic(ctx, a)
if err != nil {
return nil, err
}
@@ -50,19 +50,19 @@ func (c *converter) AccountToMastoSensitive(ctx context.Context, a *gtsmodel.Acc
frc = len(frs)
}
mastoAccount.Source = &model.Source{
Privacy: c.VisToMasto(ctx, a.Privacy),
apiAccount.Source = &model.Source{
Privacy: c.VisToAPIVis(ctx, a.Privacy),
Sensitive: a.Sensitive,
Language: a.Language,
Note: a.Note,
Fields: mastoAccount.Fields,
Fields: apiAccount.Fields,
FollowRequestsCount: frc,
}
return mastoAccount, nil
return apiAccount, nil
}
func (c *converter) AccountToMastoPublic(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) {
func (c *converter) AccountToAPIAccountPublic(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) {
if a == nil {
return nil, fmt.Errorf("given account was nil")
}
@@ -179,7 +179,7 @@ func (c *converter) AccountToMastoPublic(ctx context.Context, a *gtsmodel.Accoun
return accountFrontend, nil
}
func (c *converter) AccountToMastoBlocked(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) {
func (c *converter) AccountToAPIAccountBlocked(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) {
var acct string
if a.Domain != "" {
// this is a remote user
@@ -206,7 +206,7 @@ func (c *converter) AccountToMastoBlocked(ctx context.Context, a *gtsmodel.Accou
}, nil
}
func (c *converter) AppToMastoSensitive(ctx context.Context, a *gtsmodel.Application) (*model.Application, error) {
func (c *converter) AppToAPIAppSensitive(ctx context.Context, a *gtsmodel.Application) (*model.Application, error) {
return &model.Application{
ID: a.ID,
Name: a.Name,
@@ -217,14 +217,14 @@ func (c *converter) AppToMastoSensitive(ctx context.Context, a *gtsmodel.Applica
}, nil
}
func (c *converter) AppToMastoPublic(ctx context.Context, a *gtsmodel.Application) (*model.Application, error) {
func (c *converter) AppToAPIAppPublic(ctx context.Context, a *gtsmodel.Application) (*model.Application, error) {
return &model.Application{
Name: a.Name,
Website: a.Website,
}, nil
}
func (c *converter) AttachmentToMasto(ctx context.Context, a *gtsmodel.MediaAttachment) (model.Attachment, error) {
func (c *converter) AttachmentToAPIAttachment(ctx context.Context, a *gtsmodel.MediaAttachment) (model.Attachment, error) {
return model.Attachment{
ID: a.ID,
Type: strings.ToLower(string(a.Type)),
@@ -255,7 +255,7 @@ func (c *converter) AttachmentToMasto(ctx context.Context, a *gtsmodel.MediaAtta
}, nil
}
func (c *converter) MentionToMasto(ctx context.Context, m *gtsmodel.Mention) (model.Mention, error) {
func (c *converter) MentionToAPIMention(ctx context.Context, m *gtsmodel.Mention) (model.Mention, error) {
if m.TargetAccount == nil {
targetAccount, err := c.db.GetAccountByID(ctx, m.TargetAccountID)
if err != nil {
@@ -284,7 +284,7 @@ func (c *converter) MentionToMasto(ctx context.Context, m *gtsmodel.Mention) (mo
}, nil
}
func (c *converter) EmojiToMasto(ctx context.Context, e *gtsmodel.Emoji) (model.Emoji, error) {
func (c *converter) EmojiToAPIEmoji(ctx context.Context, e *gtsmodel.Emoji) (model.Emoji, error) {
return model.Emoji{
Shortcode: e.Shortcode,
URL: e.ImageURL,
@@ -294,14 +294,14 @@ func (c *converter) EmojiToMasto(ctx context.Context, e *gtsmodel.Emoji) (model.
}, nil
}
func (c *converter) TagToMasto(ctx context.Context, t *gtsmodel.Tag) (model.Tag, error) {
func (c *converter) TagToAPITag(ctx context.Context, t *gtsmodel.Tag) (model.Tag, error) {
return model.Tag{
Name: t.Name,
URL: t.URL,
}, nil
}
func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) {
func (c *converter) StatusToAPIStatus(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) {
l := c.log
repliesCount, err := c.db.CountStatusReplies(ctx, s)
@@ -319,7 +319,7 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque
return nil, fmt.Errorf("error counting faves: %s", err)
}
var mastoRebloggedStatus *model.Status
var apiRebloggedStatus *model.Status
if s.BoostOfID != "" {
// the boosted status might have been set on this struct already so check first before doing db calls
if s.BoostOf == nil {
@@ -342,19 +342,19 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque
s.BoostOf.Account = ba
}
mastoRebloggedStatus, err = c.StatusToMasto(ctx, s.BoostOf, requestingAccount)
apiRebloggedStatus, err = c.StatusToAPIStatus(ctx, s.BoostOf, requestingAccount)
if err != nil {
return nil, fmt.Errorf("error converting boosted status to mastotype: %s", err)
return nil, fmt.Errorf("error converting boosted status to apitype: %s", err)
}
}
var mastoApplication *model.Application
var apiApplication *model.Application
if s.CreatedWithApplicationID != "" {
gtsApplication := &gtsmodel.Application{}
if err := c.db.GetByID(ctx, s.CreatedWithApplicationID, gtsApplication); err != nil {
return nil, fmt.Errorf("error fetching application used to create status: %s", err)
}
mastoApplication, err = c.AppToMastoPublic(ctx, gtsApplication)
apiApplication, err = c.AppToAPIAppPublic(ctx, gtsApplication)
if err != nil {
return nil, fmt.Errorf("error parsing application used to create status: %s", err)
}
@@ -368,25 +368,25 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque
s.Account = a
}
mastoAuthorAccount, err := c.AccountToMastoPublic(ctx, s.Account)
apiAuthorAccount, err := c.AccountToAPIAccountPublic(ctx, s.Account)
if err != nil {
return nil, fmt.Errorf("error parsing account of status author: %s", err)
}
mastoAttachments := []model.Attachment{}
apiAttachments := []model.Attachment{}
// the status might already have some gts attachments on it if it's not been pulled directly from the database
// if so, we can directly convert the gts attachments into masto ones
// if so, we can directly convert the gts attachments into api ones
if s.Attachments != nil {
for _, gtsAttachment := range s.Attachments {
mastoAttachment, err := c.AttachmentToMasto(ctx, gtsAttachment)
apiAttachment, err := c.AttachmentToAPIAttachment(ctx, gtsAttachment)
if err != nil {
l.Errorf("error converting attachment with id %s: %s", gtsAttachment.ID, err)
continue
}
mastoAttachments = append(mastoAttachments, mastoAttachment)
apiAttachments = append(apiAttachments, apiAttachment)
}
// the status doesn't have gts attachments on it, but it does have attachment IDs
// in this case, we need to pull the gts attachments from the db to convert them into masto ones
// in this case, we need to pull the gts attachments from the db to convert them into api ones
} else {
for _, aID := range s.AttachmentIDs {
gtsAttachment, err := c.db.GetAttachmentByID(ctx, aID)
@@ -394,29 +394,29 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque
l.Errorf("error getting attachment with id %s: %s", aID, err)
continue
}
mastoAttachment, err := c.AttachmentToMasto(ctx, gtsAttachment)
apiAttachment, err := c.AttachmentToAPIAttachment(ctx, gtsAttachment)
if err != nil {
l.Errorf("error converting attachment with id %s: %s", aID, err)
continue
}
mastoAttachments = append(mastoAttachments, mastoAttachment)
apiAttachments = append(apiAttachments, apiAttachment)
}
}
mastoMentions := []model.Mention{}
apiMentions := []model.Mention{}
// the status might already have some gts mentions on it if it's not been pulled directly from the database
// if so, we can directly convert the gts mentions into masto ones
// if so, we can directly convert the gts mentions into api ones
if s.Mentions != nil {
for _, gtsMention := range s.Mentions {
mastoMention, err := c.MentionToMasto(ctx, gtsMention)
apiMention, err := c.MentionToAPIMention(ctx, gtsMention)
if err != nil {
l.Errorf("error converting mention with id %s: %s", gtsMention.ID, err)
continue
}
mastoMentions = append(mastoMentions, mastoMention)
apiMentions = append(apiMentions, apiMention)
}
// the status doesn't have gts mentions on it, but it does have mention IDs
// in this case, we need to pull the gts mentions from the db to convert them into masto ones
// in this case, we need to pull the gts mentions from the db to convert them into api ones
} else {
for _, mID := range s.MentionIDs {
gtsMention, err := c.db.GetMention(ctx, mID)
@@ -424,29 +424,29 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque
l.Errorf("error getting mention with id %s: %s", mID, err)
continue
}
mastoMention, err := c.MentionToMasto(ctx, gtsMention)
apiMention, err := c.MentionToAPIMention(ctx, gtsMention)
if err != nil {
l.Errorf("error converting mention with id %s: %s", gtsMention.ID, err)
continue
}
mastoMentions = append(mastoMentions, mastoMention)
apiMentions = append(apiMentions, apiMention)
}
}
mastoTags := []model.Tag{}
apiTags := []model.Tag{}
// the status might already have some gts tags on it if it's not been pulled directly from the database
// if so, we can directly convert the gts tags into masto ones
// if so, we can directly convert the gts tags into api ones
if s.Tags != nil {
for _, gtsTag := range s.Tags {
mastoTag, err := c.TagToMasto(ctx, gtsTag)
apiTag, err := c.TagToAPITag(ctx, gtsTag)
if err != nil {
l.Errorf("error converting tag with id %s: %s", gtsTag.ID, err)
continue
}
mastoTags = append(mastoTags, mastoTag)
apiTags = append(apiTags, apiTag)
}
// the status doesn't have gts tags on it, but it does have tag IDs
// in this case, we need to pull the gts tags from the db to convert them into masto ones
// in this case, we need to pull the gts tags from the db to convert them into api ones
} else {
for _, t := range s.TagIDs {
gtsTag := &gtsmodel.Tag{}
@@ -454,29 +454,29 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque
l.Errorf("error getting tag with id %s: %s", t, err)
continue
}
mastoTag, err := c.TagToMasto(ctx, gtsTag)
apiTag, err := c.TagToAPITag(ctx, gtsTag)
if err != nil {
l.Errorf("error converting tag with id %s: %s", gtsTag.ID, err)
continue
}
mastoTags = append(mastoTags, mastoTag)
apiTags = append(apiTags, apiTag)
}
}
mastoEmojis := []model.Emoji{}
apiEmojis := []model.Emoji{}
// the status might already have some gts emojis on it if it's not been pulled directly from the database
// if so, we can directly convert the gts emojis into masto ones
// if so, we can directly convert the gts emojis into api ones
if s.Emojis != nil {
for _, gtsEmoji := range s.Emojis {
mastoEmoji, err := c.EmojiToMasto(ctx, gtsEmoji)
apiEmoji, err := c.EmojiToAPIEmoji(ctx, gtsEmoji)
if err != nil {
l.Errorf("error converting emoji with id %s: %s", gtsEmoji.ID, err)
continue
}
mastoEmojis = append(mastoEmojis, mastoEmoji)
apiEmojis = append(apiEmojis, apiEmoji)
}
// the status doesn't have gts emojis on it, but it does have emoji IDs
// in this case, we need to pull the gts emojis from the db to convert them into masto ones
// in this case, we need to pull the gts emojis from the db to convert them into api ones
} else {
for _, e := range s.EmojiIDs {
gtsEmoji := &gtsmodel.Emoji{}
@@ -484,17 +484,17 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque
l.Errorf("error getting emoji with id %s: %s", e, err)
continue
}
mastoEmoji, err := c.EmojiToMasto(ctx, gtsEmoji)
apiEmoji, err := c.EmojiToAPIEmoji(ctx, gtsEmoji)
if err != nil {
l.Errorf("error converting emoji with id %s: %s", gtsEmoji.ID, err)
continue
}
mastoEmojis = append(mastoEmojis, mastoEmoji)
apiEmojis = append(apiEmojis, apiEmoji)
}
}
var mastoCard *model.Card
var mastoPoll *model.Poll
var apiCard *model.Card
var apiPoll *model.Poll
statusInteractions := &statusInteractions{}
si, err := c.interactionsWithStatusForAccount(ctx, s, requestingAccount)
@@ -509,7 +509,7 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque
InReplyToAccountID: s.InReplyToAccountID,
Sensitive: s.Sensitive,
SpoilerText: s.ContentWarning,
Visibility: c.VisToMasto(ctx, s.Visibility),
Visibility: c.VisToAPIVis(ctx, s.Visibility),
Language: s.Language,
URI: s.URI,
URL: s.URL,
@@ -522,26 +522,26 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque
Reblogged: statusInteractions.Reblogged,
Pinned: s.Pinned,
Content: s.Content,
Application: mastoApplication,
Account: mastoAuthorAccount,
MediaAttachments: mastoAttachments,
Mentions: mastoMentions,
Tags: mastoTags,
Emojis: mastoEmojis,
Card: mastoCard, // TODO: implement cards
Poll: mastoPoll, // TODO: implement polls
Application: apiApplication,
Account: apiAuthorAccount,
MediaAttachments: apiAttachments,
Mentions: apiMentions,
Tags: apiTags,
Emojis: apiEmojis,
Card: apiCard, // TODO: implement cards
Poll: apiPoll, // TODO: implement polls
Text: s.Text,
}
if mastoRebloggedStatus != nil {
apiStatus.Reblog = &model.StatusReblogged{Status: mastoRebloggedStatus}
if apiRebloggedStatus != nil {
apiStatus.Reblog = &model.StatusReblogged{Status: apiRebloggedStatus}
}
return apiStatus, nil
}
// VisToMasto converts a gts visibility into its mastodon equivalent
func (c *converter) VisToMasto(ctx context.Context, m gtsmodel.Visibility) model.Visibility {
// VisToapi converts a gts visibility into its api equivalent
func (c *converter) VisToAPIVis(ctx context.Context, m gtsmodel.Visibility) model.Visibility {
switch m {
case gtsmodel.VisibilityPublic:
return model.VisibilityPublic
@@ -555,7 +555,7 @@ func (c *converter) VisToMasto(ctx context.Context, m gtsmodel.Visibility) model
return ""
}
func (c *converter) InstanceToMasto(ctx context.Context, i *gtsmodel.Instance) (*model.Instance, error) {
func (c *converter) InstanceToAPIInstance(ctx context.Context, i *gtsmodel.Instance) (*model.Instance, error) {
mi := &model.Instance{
URI: i.URI,
Title: i.Title,
@@ -614,7 +614,7 @@ func (c *converter) InstanceToMasto(ctx context.Context, i *gtsmodel.Instance) (
i.ContactAccount = contactAccount
}
}
ma, err := c.AccountToMastoPublic(ctx, i.ContactAccount)
ma, err := c.AccountToAPIAccountPublic(ctx, i.ContactAccount)
if err == nil {
mi.ContactAccount = ma
}
@@ -623,7 +623,7 @@ func (c *converter) InstanceToMasto(ctx context.Context, i *gtsmodel.Instance) (
return mi, nil
}
func (c *converter) RelationshipToMasto(ctx context.Context, r *gtsmodel.Relationship) (*model.Relationship, error) {
func (c *converter) RelationshipToAPIRelationship(ctx context.Context, r *gtsmodel.Relationship) (*model.Relationship, error) {
return &model.Relationship{
ID: r.ID,
Following: r.Following,
@@ -641,11 +641,11 @@ func (c *converter) RelationshipToMasto(ctx context.Context, r *gtsmodel.Relatio
}, nil
}
func (c *converter) NotificationToMasto(ctx context.Context, n *gtsmodel.Notification) (*model.Notification, error) {
func (c *converter) NotificationToAPINotification(ctx context.Context, n *gtsmodel.Notification) (*model.Notification, error) {
if n.TargetAccount == nil {
tAccount, err := c.db.GetAccountByID(ctx, n.TargetAccountID)
if err != nil {
return nil, fmt.Errorf("NotificationToMasto: error getting target account with id %s from the db: %s", n.TargetAccountID, err)
return nil, fmt.Errorf("NotificationToapi: error getting target account with id %s from the db: %s", n.TargetAccountID, err)
}
n.TargetAccount = tAccount
}
@@ -653,22 +653,22 @@ func (c *converter) NotificationToMasto(ctx context.Context, n *gtsmodel.Notific
if n.OriginAccount == nil {
ogAccount, err := c.db.GetAccountByID(ctx, n.OriginAccountID)
if err != nil {
return nil, fmt.Errorf("NotificationToMasto: error getting origin account with id %s from the db: %s", n.OriginAccountID, err)
return nil, fmt.Errorf("NotificationToapi: error getting origin account with id %s from the db: %s", n.OriginAccountID, err)
}
n.OriginAccount = ogAccount
}
mastoAccount, err := c.AccountToMastoPublic(ctx, n.OriginAccount)
apiAccount, err := c.AccountToAPIAccountPublic(ctx, n.OriginAccount)
if err != nil {
return nil, fmt.Errorf("NotificationToMasto: error converting account to masto: %s", err)
return nil, fmt.Errorf("NotificationToapi: error converting account to api: %s", err)
}
var mastoStatus *model.Status
var apiStatus *model.Status
if n.StatusID != "" {
if n.Status == nil {
status, err := c.db.GetStatusByID(ctx, n.StatusID)
if err != nil {
return nil, fmt.Errorf("NotificationToMasto: error getting status with id %s from the db: %s", n.StatusID, err)
return nil, fmt.Errorf("NotificationToapi: error getting status with id %s from the db: %s", n.StatusID, err)
}
n.Status = status
}
@@ -682,9 +682,9 @@ func (c *converter) NotificationToMasto(ctx context.Context, n *gtsmodel.Notific
}
var err error
mastoStatus, err = c.StatusToMasto(ctx, n.Status, nil)
apiStatus, err = c.StatusToAPIStatus(ctx, n.Status, nil)
if err != nil {
return nil, fmt.Errorf("NotificationToMasto: error converting status to masto: %s", err)
return nil, fmt.Errorf("NotificationToapi: error converting status to api: %s", err)
}
}
@@ -692,12 +692,12 @@ func (c *converter) NotificationToMasto(ctx context.Context, n *gtsmodel.Notific
ID: n.ID,
Type: string(n.NotificationType),
CreatedAt: n.CreatedAt.Format(time.RFC3339),
Account: mastoAccount,
Status: mastoStatus,
Account: apiAccount,
Status: apiStatus,
}, nil
}
func (c *converter) DomainBlockToMasto(ctx context.Context, b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error) {
func (c *converter) DomainBlockToAPIDomainBlock(ctx context.Context, b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error) {
domainBlock := &model.DomainBlock{
Domain: b.Domain,