summaryrefslogtreecommitdiff
path: root/internal/ap/collections.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2024-02-23 15:24:40 +0000
committerLibravatar GitHub <noreply@github.com>2024-02-23 16:24:40 +0100
commit1d51e3c8d68fe79ee1103d3e412d4e823d99eaaa (patch)
tree122318b89d56ba9310bf65df5adf6c42f9611c31 /internal/ap/collections.go
parent[feature] Add "what is this" section to index template (#2680) (diff)
downloadgotosocial-1d51e3c8d68fe79ee1103d3e412d4e823d99eaaa.tar.xz
[bugfix] 2643 bug search for account url doesnt always work when redirected (#2673)
* update activity library so dereferencer returns full response and checks *final* link to allow for redirects * temporarily add bodged fixed library * remove unused code * update getAccountFeatured() to use dereferenceCollectionPage() * make sure to release map * perform a 2nd decode to ensure reader is empty after primary decode * add comment explaining choice of using Decode() instead of Unmarshal() * update embedded activity library to latest matching https://github.com/superseriousbusiness/activity/pull/21 * add checks to look for changed URI and re-check database if redirected * update max iteration count to 512, add checks during dereferenceAncestors() for indirect URLs * remove doubled-up code * fix use of status instead of current * use URIs for checking equality for security * use the latest known URI for boost_of_uri in case original was an indirect * add dereferenceCollection() function for dereferenceAccountFeatured() * pull in latest github.com/superseriousbusiness/activity version (and remove the bodge!!) * fix typo in code comments * update decodeType() to accept a readcloser and handle body closing * switch to checking using BoostOfID and add note why not using BoostOfURI * ensure InReplyTo gets unset when deleting status parent in case currently stubbed * add tests for Collection and CollectionPage iterators
Diffstat (limited to 'internal/ap/collections.go')
-rw-r--r--internal/ap/collections.go112
1 files changed, 112 insertions, 0 deletions
diff --git a/internal/ap/collections.go b/internal/ap/collections.go
index ba3887a5b..da789e179 100644
--- a/internal/ap/collections.go
+++ b/internal/ap/collections.go
@@ -26,6 +26,24 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/paging"
)
+// TODO: replace must of this logic with just
+// using extractIRIs() on the iterator types.
+
+// ToCollectionIterator attempts to resolve the given vocab type as a Collection
+// like object and wrap in a standardised interface in order to iterate its contents.
+func ToCollectionIterator(t vocab.Type) (CollectionIterator, error) {
+ switch name := t.GetTypeName(); name {
+ case ObjectCollection:
+ t := t.(vocab.ActivityStreamsCollection)
+ return WrapCollection(t), nil
+ case ObjectOrderedCollection:
+ t := t.(vocab.ActivityStreamsOrderedCollection)
+ return WrapOrderedCollection(t), nil
+ default:
+ return nil, fmt.Errorf("%T(%s) was not Collection-like", t, name)
+ }
+}
+
// ToCollectionPageIterator attempts to resolve the given vocab type as a CollectionPage
// like object and wrap in a standardised interface in order to iterate its contents.
func ToCollectionPageIterator(t vocab.Type) (CollectionPageIterator, error) {
@@ -41,6 +59,16 @@ func ToCollectionPageIterator(t vocab.Type) (CollectionPageIterator, error) {
}
}
+// WrapCollection wraps an ActivityStreamsCollection in a standardised collection interface.
+func WrapCollection(collection vocab.ActivityStreamsCollection) CollectionIterator {
+ return &regularCollectionIterator{ActivityStreamsCollection: collection}
+}
+
+// WrapOrderedCollection wraps an ActivityStreamsOrderedCollection in a standardised collection interface.
+func WrapOrderedCollection(collection vocab.ActivityStreamsOrderedCollection) CollectionIterator {
+ return &orderedCollectionIterator{ActivityStreamsOrderedCollection: collection}
+}
+
// WrapCollectionPage wraps an ActivityStreamsCollectionPage in a standardised collection page interface.
func WrapCollectionPage(page vocab.ActivityStreamsCollectionPage) CollectionPageIterator {
return &regularCollectionPageIterator{ActivityStreamsCollectionPage: page}
@@ -51,6 +79,90 @@ func WrapOrderedCollectionPage(page vocab.ActivityStreamsOrderedCollectionPage)
return &orderedCollectionPageIterator{ActivityStreamsOrderedCollectionPage: page}
}
+// regularCollectionIterator implements CollectionIterator
+// for the vocab.ActivitiyStreamsCollection type.
+type regularCollectionIterator struct {
+ vocab.ActivityStreamsCollection
+ items vocab.ActivityStreamsItemsPropertyIterator
+ once bool // only init items once
+}
+
+func (iter *regularCollectionIterator) NextItem() TypeOrIRI {
+ if !iter.initItems() {
+ return nil
+ }
+ cur := iter.items
+ iter.items = iter.items.Next()
+ return cur
+}
+
+func (iter *regularCollectionIterator) PrevItem() TypeOrIRI {
+ if !iter.initItems() {
+ return nil
+ }
+ cur := iter.items
+ iter.items = iter.items.Prev()
+ return cur
+}
+
+func (iter *regularCollectionIterator) initItems() bool {
+ if iter.once {
+ return (iter.items != nil)
+ }
+ iter.once = true
+ if iter.ActivityStreamsCollection == nil {
+ return false // no page set
+ }
+ items := iter.GetActivityStreamsItems()
+ if items == nil {
+ return false // no items found
+ }
+ iter.items = items.Begin()
+ return (iter.items != nil)
+}
+
+// orderedCollectionIterator implements CollectionIterator
+// for the vocab.ActivitiyStreamsOrderedCollection type.
+type orderedCollectionIterator struct {
+ vocab.ActivityStreamsOrderedCollection
+ items vocab.ActivityStreamsOrderedItemsPropertyIterator
+ once bool // only init items once
+}
+
+func (iter *orderedCollectionIterator) NextItem() TypeOrIRI {
+ if !iter.initItems() {
+ return nil
+ }
+ cur := iter.items
+ iter.items = iter.items.Next()
+ return cur
+}
+
+func (iter *orderedCollectionIterator) PrevItem() TypeOrIRI {
+ if !iter.initItems() {
+ return nil
+ }
+ cur := iter.items
+ iter.items = iter.items.Prev()
+ return cur
+}
+
+func (iter *orderedCollectionIterator) initItems() bool {
+ if iter.once {
+ return (iter.items != nil)
+ }
+ iter.once = true
+ if iter.ActivityStreamsOrderedCollection == nil {
+ return false // no page set
+ }
+ items := iter.GetActivityStreamsOrderedItems()
+ if items == nil {
+ return false // no items found
+ }
+ iter.items = items.Begin()
+ return (iter.items != nil)
+}
+
// regularCollectionPageIterator implements CollectionPageIterator
// for the vocab.ActivitiyStreamsCollectionPage type.
type regularCollectionPageIterator struct {