[chore] Refactor thread dereference a bit for clarity (#647)

* refactor thread dereference a bit for clarity

* lint for the lint gods
This commit is contained in:
tobi 2022-06-11 16:25:41 +02:00 committed by GitHub
parent cf5c6d724d
commit 7f9925afe5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -41,11 +41,11 @@ func (d *deref) DereferenceThread(ctx context.Context, username string, statusIR
"username": username, "username": username,
"statusIRI": statusIRI.String(), "statusIRI": statusIRI.String(),
}) })
l.Debug("entering DereferenceThread") l.Trace("entering DereferenceThread")
// if it's our status we already have everything stashed so we can bail early // if it's our status we already have everything stashed so we can bail early
if statusIRI.Host == config.GetHost() { if statusIRI.Host == config.GetHost() {
l.Debug("iri belongs to us, bailing") l.Trace("iri belongs to us, bailing")
return nil return nil
} }
@ -75,11 +75,11 @@ func (d *deref) iterateAncestors(ctx context.Context, username string, statusIRI
"username": username, "username": username,
"statusIRI": statusIRI.String(), "statusIRI": statusIRI.String(),
}) })
l.Debug("entering iterateAncestors") l.Trace("entering iterateAncestors")
// if it's our status we don't need to dereference anything so we can immediately move up the chain // if it's our status we don't need to dereference anything so we can immediately move up the chain
if statusIRI.Host == config.GetHost() { if statusIRI.Host == config.GetHost() {
l.Debug("iri belongs to us, moving up to next ancestor") l.Trace("iri belongs to us, moving up to next ancestor")
// since this is our status, we know we can extract the id from the status path // since this is our status, we know we can extract the id from the status path
_, id, err := uris.ParseStatusesPath(&statusIRI) _, id, err := uris.ParseStatusesPath(&statusIRI)
@ -96,17 +96,19 @@ func (d *deref) iterateAncestors(ctx context.Context, username string, statusIRI
// status doesn't reply to anything // status doesn't reply to anything
return nil return nil
} }
nextIRI, err := url.Parse(status.URI) nextIRI, err := url.Parse(status.URI)
if err != nil { if err != nil {
return err return err
} }
return d.iterateAncestors(ctx, username, *nextIRI) return d.iterateAncestors(ctx, username, *nextIRI)
} }
// If we reach here, we're looking at a remote status // If we reach here, we're looking at a remote status
_, statusable, err := d.GetRemoteStatus(ctx, username, &statusIRI, true, false) _, statusable, err := d.GetRemoteStatus(ctx, username, &statusIRI, true, false)
if err != nil { if err != nil {
l.Debugf("error getting remote status: %s", err) l.Debugf("couldn't get remote status %s: %s; can't iterate any more ancestors", statusIRI.String(), err)
return nil return nil
} }
@ -126,41 +128,41 @@ func (d *deref) iterateDescendants(ctx context.Context, username string, statusI
"username": username, "username": username,
"statusIRI": statusIRI.String(), "statusIRI": statusIRI.String(),
}) })
l.Debug("entering iterateDescendants") l.Trace("entering iterateDescendants")
// if it's our status we already have descendants stashed so we can bail early // if it's our status we already have descendants stashed so we can bail early
if statusIRI.Host == config.GetHost() { if statusIRI.Host == config.GetHost() {
l.Debug("iri belongs to us, bailing") l.Trace("iri belongs to us, bailing")
return nil return nil
} }
replies := statusable.GetActivityStreamsReplies() replies := statusable.GetActivityStreamsReplies()
if replies == nil || !replies.IsActivityStreamsCollection() { if replies == nil || !replies.IsActivityStreamsCollection() {
l.Debug("no replies, bailing") l.Trace("no replies, bailing")
return nil return nil
} }
repliesCollection := replies.GetActivityStreamsCollection() repliesCollection := replies.GetActivityStreamsCollection()
if repliesCollection == nil { if repliesCollection == nil {
l.Debug("replies collection is nil, bailing") l.Trace("replies collection is nil, bailing")
return nil return nil
} }
first := repliesCollection.GetActivityStreamsFirst() first := repliesCollection.GetActivityStreamsFirst()
if first == nil { if first == nil {
l.Debug("replies collection has no first, bailing") l.Trace("replies collection has no first, bailing")
return nil return nil
} }
firstPage := first.GetActivityStreamsCollectionPage() firstPage := first.GetActivityStreamsCollectionPage()
if firstPage == nil { if firstPage == nil {
l.Debug("first has no collection page, bailing") l.Trace("first has no collection page, bailing")
return nil return nil
} }
firstPageNext := firstPage.GetActivityStreamsNext() firstPageNext := firstPage.GetActivityStreamsNext()
if firstPageNext == nil || !firstPageNext.IsIRI() { if firstPageNext == nil || !firstPageNext.IsIRI() {
l.Debug("next is not an iri, bailing") l.Trace("next is not an iri, bailing")
return nil return nil
} }
@ -169,24 +171,23 @@ func (d *deref) iterateDescendants(ctx context.Context, username string, statusI
pageLoop: pageLoop:
for { for {
l.Debugf("dereferencing page %s", currentPageIRI) l.Tracef("dereferencing page %s", currentPageIRI)
nextPage, err := d.DereferenceCollectionPage(ctx, username, currentPageIRI) collectionPage, err := d.DereferenceCollectionPage(ctx, username, currentPageIRI)
if err != nil { if err != nil {
return err l.Debugf("couldn't get remote collection page %s: %s; breaking pageLoop", currentPageIRI, err)
break pageLoop
} }
// next items could be either a list of URLs or a list of statuses pageItems := collectionPage.GetActivityStreamsItems()
if pageItems.Len() == 0 {
nextItems := nextPage.GetActivityStreamsItems()
if nextItems.Len() == 0 {
// no items on this page, which means we're done // no items on this page, which means we're done
break pageLoop break pageLoop
} }
// have a look through items and see what we can find // have a look through items and see what we can find
for iter := nextItems.Begin(); iter != nextItems.End(); iter = iter.Next() { for iter := pageItems.Begin(); iter != pageItems.End(); iter = iter.Next() {
// We're looking for a url to feed to GetRemoteStatus. // We're looking for a url to feed to GetRemoteStatus.
// Items can be either an IRI, or a Note. // Each item can be either an IRI, or a Note.
// If a note, we grab the ID from it and call it, rather than parsing the note. // If a note, we grab the ID from it and call it, rather than parsing the note.
var itemURI *url.URL var itemURI *url.URL
switch { switch {
@ -195,10 +196,10 @@ func (d *deref) iterateDescendants(ctx context.Context, username string, statusI
itemURI = iter.GetIRI() itemURI = iter.GetIRI()
case iter.IsActivityStreamsNote(): case iter.IsActivityStreamsNote():
// note, get the id from it to use as iri // note, get the id from it to use as iri
n := iter.GetActivityStreamsNote() note := iter.GetActivityStreamsNote()
id := n.GetJSONLDId() noteID := note.GetJSONLDId()
if id != nil && id.IsIRI() { if noteID != nil && noteID.IsIRI() {
itemURI = id.GetIRI() itemURI = noteID.GetIRI()
} }
default: default:
// if it's not an iri or a note, we don't know how to process it // if it's not an iri or a note, we don't know how to process it
@ -223,12 +224,13 @@ func (d *deref) iterateDescendants(ctx context.Context, username string, statusI
} }
} }
next := nextPage.GetActivityStreamsNext() nextPage := collectionPage.GetActivityStreamsNext()
if next != nil && next.IsIRI() { if nextPage != nil && nextPage.IsIRI() {
l.Debug("setting next page") nextPageIRI := nextPage.GetIRI()
currentPageIRI = next.GetIRI() l.Tracef("moving on to next page %s", nextPageIRI)
currentPageIRI = nextPageIRI
} else { } else {
l.Debug("no next page, bailing") l.Trace("no next page, bailing")
break pageLoop break pageLoop
} }
} }