~mariusor/activitypub-client-example

c3d8c826ad8d08292ac5850f3b5eb569eefceb48 — Marius Orcsik 1 year, 9 months ago a8008b6
Simplify the example
1 files changed, 18 insertions(+), 28 deletions(-)

M main.go
M main.go => main.go +18 -28
@@ 2,7 2,6 @@ package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"time"


@@ 12,9 11,10 @@ import (
)

const (
	BaseURL = "https://fedbox.git/inbox?maxItems=20" //
	BaseURL = vocab.IRI("https://fedbox.git/inbox?maxItems=20")
	timeout = 10 * time.Second
)

// This can be called on any ActivityPub collection, it doesn't do anything except load it
// and print out the type of collection object, the number of items in the current response,
// and move on to the next page if it exists. If it does not, it exits.


@@ 26,22 26,24 @@ const (
func main() {
	url := BaseURL
	if len(os.Args) > 1 {
		url = os.Args[1]
		url = vocab.IRI(os.Args[1])
	}

	client := client.New(
		client.SkipTLSValidation(true),
		client.SetDefaultHTTPClient(),
	)
	client := client.New(client.SkipTLSValidation(true), client.SetDefaultHTTPClient())

	total := 0
	for {
		t := time.Now()
		ctx, cancel := context.WithTimeout(context.Background(), timeout)

		col, err := loadCollection(ctx, client, vocab.IRI(url))
		log.Printf("Loading %s", url)
		it, err := client.CtxLoadIRI(ctx, url)
		if err != nil {
			log.Fatalf("Error %s", err)
			log.Fatalf("Error: %s", err)
		}
		col, ok := it.(vocab.CollectionInterface)
		if !ok {
			log.Fatalf("Unable to convert ActivityPub object of type %s to a Collection type", it.GetType())
		}

		next := getNextCollectionPage(col)


@@ 60,48 62,36 @@ func main() {

		log.Printf("Loaded in %dms", time.Now().Sub(t).Milliseconds())
	}
	log.Printf("Loaded %d items", total)
	log.Printf("Total loaded items: %d", total)
}

func getNextCollectionPage(col vocab.CollectionInterface) string {
	var next string
func getNextCollectionPage(col vocab.CollectionInterface) vocab.IRI {
	var next vocab.IRI
	switch col.GetType() {
	case vocab.OrderedCollectionPageType:
		if c, ok := col.(*vocab.OrderedCollectionPage); ok {
			if c.Next != nil {
				next = c.Next.GetLink().String()
				next = c.Next.GetLink()
			}
		}
	case vocab.OrderedCollectionType:
		if c, ok := col.(*vocab.OrderedCollection); ok {
			if c.First != nil {
				next = c.First.GetLink().String()
				next = c.First.GetLink()
			}
		}
	case vocab.CollectionPageType:
		if c, ok := col.(*vocab.CollectionPage); ok {
			if c.Next != nil {
				next = c.Next.GetLink().String()
				next = c.Next.GetLink()
			}
		}
	case vocab.CollectionType:
		if c, ok := col.(*vocab.Collection); ok {
			if c.First != nil {
				next = c.First.GetLink().String()
				next = c.First.GetLink()
			}
		}
	}
	return next
}

func loadCollection(ctx context.Context, c *client.C, url vocab.IRI) (vocab.CollectionInterface, error) {
	log.Printf("Loading %s", url)
	it, err := c.CtxLoadIRI(ctx, url)
	if err != nil {
		return nil, err
	}
	if col, ok := it.(vocab.CollectionInterface); ok {
		return col, nil
	}
	return nil, fmt.Errorf("ActivityPub object of type %s could not be converted to Collection", it.GetType())
}