~mariusor/activitypub-go

5 2

Two questions about extending go-ap

Anthony Wang <ta180m@proton.me>
Details
Message ID
<05vc_hBspKTDlG6J2jywvwbtB905Hqwc812QrjU2LztQIm-gg00Ax1vfhU0N9XOtGU8a6E6N6EPpMQXc7U7Nr3FagUF8N2V2fae7XRIN_30=@proton.me>
DKIM signature
pass
Download raw message
Hi Marius,

I have two questions about implementing ForgeFed over go-ap. I would like to add an alsoKnownAs property to the Person actor type, since we will be using this account migrations. What would be the best way to implement this in my ForgeFed package? Or should I fork go-ap/activitypub to make this change?

Also, I noticed that unmarshaling binary into an activity variable will not work correctly if the object is a ForgeFed type. For instance, the following code doesn't work:

var activity ap.Activity
// assume the binary variable contains a Create activity with a Repository object
activity.UnmarshalJSON(binary)
fmt.Println(activity.Object) // is nil!

Is there a way I can fix this, or will I have to fork go-ap? I'm already using a fork of go-ap/activitypub with all the read and write JSON functions exported, so I'm personally OK with maintaining a fork. However, it would be better if I could fix these two problems without making a fork.

-Anthony
Details
Message ID
<20220724100655.3ewvafndqk5nlath@gabriel.localdomain>
In-Reply-To
<05vc_hBspKTDlG6J2jywvwbtB905Hqwc812QrjU2LztQIm-gg00Ax1vfhU0N9XOtGU8a6E6N6EPpMQXc7U7Nr3FagUF8N2V2fae7XRIN_30=@proton.me> (view parent)
DKIM signature
pass
Download raw message
On 22-07-24 01:49:42, Anthony Wang wrote:
> Hi Marius,
> 
> I have two questions about implementing ForgeFed over go-ap. I would like to add an alsoKnownAs property to the Person actor type, since we will be using this account migrations. What would be the best way to implement this in my ForgeFed package? Or should I fork go-ap/activitypub to make this change?

I honestly don't know what to answer to this. It probably depends on how
it will be easier for you to maintain.

My preferred way (the way I imagined the library to be used by others)
is to create your own gofed.Person structure and use that. (see the
reply to your second question about how to interface these new types
with the library's plumbing).

> Also, I noticed that unmarshaling binary into an activity variable will not work correctly if the object is a ForgeFed type. For instance, the following code doesn't work:
> 
> var activity ap.Activity
> // assume the binary variable contains a Create activity with a Repository object
> activity.UnmarshalJSON(binary)
> fmt.Println(activity.Object) // is nil!

This is probably because you didn't "overload" the typer function[1] for
the package. This function tells the JSON/Gob decoder functions how to
handle specific activitypub types.

Here is some code that ideally would do what you need:



import vocab "github.com/go-ap/activitypub"

func GoFedItemTyper(typ vocab.ActivityVocabularyType) (vocab.Item, error) {
    switch typ {
    case vocab.PersonType:
        // For your custom Person that contains the alsoKnownAs property
        return new(gofed.Person), nil
    case gofed.RepositoryType:
        // For the Repository object
        return new(gofed.Repository), nil
    }
    return vocab.GetItemByType(typ)
}

// Then you need some code to initialize the go-ap/activitypub typer (which
// is a static package variable):

vocab.TyperFn = GoFedItemTyper

// assume the binary variable contains a Create activity with a Repository object
it, _ := vocab.UnmarshalJSON(binary)
vocab.OnActivity(it, func (activity *vocab.Activity) error {
    fmt.Println(activity.Object) // should not be nil!
    return nil
})


Let me know if this works for you, if it doesn't I can try to come up
with a better, more complete, example.

> 
> Is there a way I can fix this, or will I have to fork go-ap? I'm already using a fork of go-ap/activitypub with all the read and write JSON functions exported, so I'm personally OK with maintaining a fork. However, it would be better if I could fix these two problems without making a fork.
> 

You are striding new ground here, and I hope there's a way to move
forward without needing to fork the package (or it would mean I failed
in creating a proper library) so I don't have answers for you. It's up
to you to find some sort of solution for this. I hope that the code I
provided above would help a little.

(Any suggestions you might have about making it easier for you and
future developers will definitely be taken into account).


[1] https://pkg.go.dev/github.com/go-ap/activitypub#GetItemByType

Best,
/Marius Orcsik
Anthony Wang <ta180m@proton.me>
Details
Message ID
<GC96l7L35gtWjDIvRu1LG-Q7xn2_SmraMMLmfHPNQ9_yk3-igrIjR51SIp5VZI2y673TAOmj7n56InaJK75eRve4YoTeQEUEbPMipfAmORA=@proton.me>
In-Reply-To
<20220724100655.3ewvafndqk5nlath@gabriel.localdomain> (view parent)
DKIM signature
pass
Download raw message
------- Original Message -------
On Sunday, July 24th, 2022 at 05:06, Marius Orcsik <marius@federated.id> wrote:
> You are striding new ground here, and I hope there's a way to move
> forward without needing to fork the package (or it would mean I failed
> in creating a proper library) so I don't have answers for you. It's up
> to you to find some sort of solution for this. I hope that the code I
> provided above would help a little.
> 

> (Any suggestions you might have about making it easier for you and
> future developers will definitely be taken into account).
> 

> 

> [1] https://pkg.go.dev/github.com/go-ap/activitypub#GetItemByType
> 

> Best,
> /Marius Orcsik

Hello Marius,

Thanks for the help. I think you meant ItemTyperFunc instead of TyperFn (which doesn't compile), but when I tried it, it's still giving me nil. For instance:

// assume body is a Repository type object
ap.ItemTyperFunc = forgefed.GetItemByType
object, err := ap.UnmarshalJSON(body)
fmt.Println(object) // is still nil!

I think this is because the internal itemFn() function in decoding_json.go hard-codes all the types that it supports.

-Anthony
Details
Message ID
<20220726093040.m434bd2f42yy54uh@tp>
In-Reply-To
<GC96l7L35gtWjDIvRu1LG-Q7xn2_SmraMMLmfHPNQ9_yk3-igrIjR51SIp5VZI2y673TAOmj7n56InaJK75eRve4YoTeQEUEbPMipfAmORA=@proton.me> (view parent)
DKIM signature
pass
Download raw message
On 22-07-25 21:00:03, Anthony Wang wrote:
> 
> I think this is because the internal itemFn() function in decoding_json.go hard-codes all the types that it supports.

It seems possible, I will have to take a look at that.

Could you please extract a minimal example which reproduces the issue
for me to work from?

Cheers,
/Marius
Anthony Wang <ta180m@proton.me>
Details
Message ID
<j1iDcDLnx-goFpa1vgdu70rlXMKnbLYFE5rQPy91SWJIdL3FPAaUD258-b7XNeHTOYZpKFTHfB7Eg2SsgTKoO0Tc7wvIRI3oLz2GR9cTn80=@proton.me>
In-Reply-To
<20220726093040.m434bd2f42yy54uh@tp> (view parent)
DKIM signature
pass
Download raw message
------- Original Message -------
On Tuesday, July 26th, 2022 at 04:30, Marius Orcsik <marius@federated.id> wrote:
> It seems possible, I will have to take a look at that.
> 

> Could you please extract a minimal example which reproduces the issue
> for me to work from?
> 

> Cheers,
> /Marius

Here's the code that I'm using: https://gitea.com/Ta180m/gitea/src/branch/main/models/forgefed/forgefed.go has the GetItemByType() function (you can delete all the other cases except for the default and repository, https://gitea.com/Ta180m/gitea/src/branch/main/models/forgefed/repository.go has my implementation of the Repository type, and then the following code contains the issue:

// assume body is the binary of a Repository type object
ap.ItemTyperFunc = forgefed.GetItemByType
object, err := ap.UnmarshalJSON(body)
fmt.Println(object) // is nil!

As I mentioned in the last email, I suspect this is the function causing the issue: https://github.com/go-ap/activitypub/blob/master/decoding_json.go#L170

If you need more details or an actual minimal file that contains the issue, just let me know.

-Anthony
Details
Message ID
<20220801082119.xgivbormudxwfrag@tp>
In-Reply-To
<j1iDcDLnx-goFpa1vgdu70rlXMKnbLYFE5rQPy91SWJIdL3FPAaUD258-b7XNeHTOYZpKFTHfB7Eg2SsgTKoO0Tc7wvIRI3oLz2GR9cTn80=@proton.me> (view parent)
DKIM signature
pass
Download raw message
On 22-07-26 17:38:31, Anthony Wang wrote:
> ------- Original Message -------
> If you need more details or an actual minimal file that contains the issue, just let me know.

Hey Anthony, I managed to get an example running, but unfortunatelly I
didn't get too far into finding a solution.

I would say that probably your best bet to make it working as fast as
possible is to extract all the JSON functionality that you previously
updated in go-ap/activitypub (the functions that you needed to export to
make it work) and also improve the itemFn() function with the
functionality you want for the Repository type.

It's possible that the solution I come up with will also involve
extracting all the JSON functions to a subpackage (something like
go-ap/activitypub/jsonld) so you'll be a step ahead there.

I will let you know if I make any progress in any direction.

Cheers,
/Marius
Reply to thread Export thread (mbox)