diff options
author | 2023-07-31 15:47:35 +0200 | |
---|---|---|
committer | 2023-07-31 15:47:35 +0200 | |
commit | 2796a2e82f16ade9872008878cf88299bd66b4e7 (patch) | |
tree | 76f7b69cc1da57ca10b71c57abf1892575bea100 /internal/federation/dereferencing/status.go | |
parent | [performance] cache follow, follow request and block ID lists (#2027) (diff) | |
download | gotosocial-2796a2e82f16ade9872008878cf88299bd66b4e7.tar.xz |
[feature] Hashtag federation (in/out), hashtag client API endpoints (#2032)
* update go-fed
* do the things
* remove unused columns from tags
* update to latest lingo from main
* further tag shenanigans
* serve stub page at tag endpoint
* we did it lads
* tests, oh tests, ohhh tests, oh tests (doo doo doo doo)
* swagger docs
* document hashtag usage + federation
* instanceGet
* don't bother parsing tag href
* rename whereStartsWith -> whereStartsLike
* remove GetOrCreateTag
* dont cache status tag timelineability
Diffstat (limited to 'internal/federation/dereferencing/status.go')
-rw-r--r-- | internal/federation/dereferencing/status.go | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/internal/federation/dereferencing/status.go b/internal/federation/dereferencing/status.go index 4525f64a9..8586884eb 100644 --- a/internal/federation/dereferencing/status.go +++ b/internal/federation/dereferencing/status.go @@ -288,7 +288,10 @@ func (d *deref) enrichStatus( return nil, nil, gtserror.Newf("error populating mentions for status %s: %w", uri, err) } - // TODO: populateStatusTags() + // Ensure the status' tags are populated. + if err := d.fetchStatusTags(ctx, requestUser, latestStatus); err != nil { + return nil, nil, gtserror.Newf("error populating tags for status %s: %w", uri, err) + } // Ensure the status' media attachments are populated, passing in existing to check for changes. if err := d.fetchStatusAttachments(ctx, tsport, status, latestStatus); err != nil { @@ -400,6 +403,55 @@ func (d *deref) fetchStatusMentions(ctx context.Context, requestUser string, exi return nil } +func (d *deref) fetchStatusTags(ctx context.Context, requestUser string, status *gtsmodel.Status) error { + // Allocate new slice to take the yet-to-be determined tag IDs. + status.TagIDs = make([]string, len(status.Tags)) + + for i := range status.Tags { + placeholder := status.Tags[i] + + // Look for existing tag with this name first. + tag, err := d.state.DB.GetTagByName(ctx, placeholder.Name) + if err != nil && !errors.Is(err, db.ErrNoEntries) { + log.Errorf(ctx, "db error getting tag %s: %v", tag.Name, err) + continue + } + + // No tag with this name yet, create it. + if tag == nil { + tag = >smodel.Tag{ + ID: id.NewULID(), + Name: placeholder.Name, + } + + if err := d.state.DB.PutTag(ctx, tag); err != nil { + log.Errorf(ctx, "db error putting tag %s: %v", tag.Name, err) + continue + } + } + + // Set the *new* tag and ID. + status.Tags[i] = tag + status.TagIDs[i] = tag.ID + } + + // Remove any tag we couldn't get or create. + for i := 0; i < len(status.TagIDs); { + if status.TagIDs[i] == "" { + // This is a failed tag population, likely due + // to some database peculiarity / race condition. + copy(status.Tags[i:], status.Tags[i+1:]) + copy(status.TagIDs[i:], status.TagIDs[i+1:]) + status.Tags = status.Tags[:len(status.Tags)-1] + status.TagIDs = status.TagIDs[:len(status.TagIDs)-1] + continue + } + i++ + } + + return nil +} + func (d *deref) fetchStatusAttachments(ctx context.Context, tsport transport.Transport, existing, status *gtsmodel.Status) error { // Allocate new slice to take the yet-to-be fetched attachment IDs. status.AttachmentIDs = make([]string, len(status.Attachments)) |