From 7400a3197fa89e6c32b52c009e87958ac9279c94 Mon Sep 17 00:00:00 2001 From: Cory Slep Date: Sun, 4 Feb 2018 18:32:01 +0100 Subject: [PATCH] Add support for wrapping objects in Create activity. --- pub/fed.go | 13 ++++++++++- pub/internal.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/pub/fed.go b/pub/fed.go index 7f68d52..75ad3be 100644 --- a/pub/fed.go +++ b/pub/fed.go @@ -145,6 +145,9 @@ type Application interface { // the provided object in the outbox of the user represented by the // client id token. AddToOutboxResolver(id string) (*streams.Resolver, error) + // ActorIRI returns the actor's IRI associated with the given client ID + // token. + ActorIRI(id string) (url.URL, error) } // Receiver is provided by users of this library and designed to handle @@ -329,7 +332,15 @@ func (f *federator) PostOutbox(id string) HandlerFunc { } newId := f.App.NewId(id, typer) if !isActivityType(typer) { - // TODO: Wrap in Create Activity + actorIri, err := f.App.ActorIRI(id) + if err != nil { + return true, err + } + obj, ok := typer.(vocab.ObjectType) + if !ok { + return true, fmt.Errorf("wrap in create: cannot convert to vocab.ObjectType: %T", typer) + } + typer = f.wrapInCreate(obj, actorIri) } typer.SetId(newId) if m, err = typer.Serialize(); err != nil { diff --git a/pub/internal.go b/pub/internal.go index da1c062..000aedf 100644 --- a/pub/internal.go +++ b/pub/internal.go @@ -89,6 +89,64 @@ func (f *federator) postToOutbox(b []byte, to url.URL) error { return nil } +// wrapInCreate will automatically wrap the provided object in a Create +// activity. This will copy over the 'to', 'bto', 'cc', 'bcc', and 'audience' +// properties. It will also copy over the published time if present. +func (f *federator) wrapInCreate(o vocab.ObjectType, actor url.URL) *vocab.Create { + c := &vocab.Create{} + c.AddObject(o) + c.AddActorIRI(actor) + if o.IsPublished() { + c.SetPublished(o.GetPublished()) + } + for i := 0; i < o.ToLen(); i++ { + if o.IsToObject(i) { + c.AddToObject(o.GetToObject(i)) + } else if o.IsToLink(i) { + c.AddToLink(o.GetToLink(i)) + } else if o.IsToIRI(i) { + c.AddToIRI(o.GetToIRI(i)) + } + } + for i := 0; i < o.BtoLen(); i++ { + if o.IsBtoObject(i) { + c.AddBtoObject(o.GetBtoObject(i)) + } else if o.IsBtoLink(i) { + c.AddBtoLink(o.GetBtoLink(i)) + } else if o.IsBtoIRI(i) { + c.AddBtoIRI(o.GetBtoIRI(i)) + } + } + for i := 0; i < o.CcLen(); i++ { + if o.IsCcObject(i) { + c.AddCcObject(o.GetCcObject(i)) + } else if o.IsCcLink(i) { + c.AddCcLink(o.GetCcLink(i)) + } else if o.IsCcIRI(i) { + c.AddCcIRI(o.GetCcIRI(i)) + } + } + for i := 0; i < o.BccLen(); i++ { + if o.IsBccObject(i) { + c.AddBccObject(o.GetBccObject(i)) + } else if o.IsBccLink(i) { + c.AddBccLink(o.GetBccLink(i)) + } else if o.IsBccIRI(i) { + c.AddBccIRI(o.GetBccIRI(i)) + } + } + for i := 0; i < o.AudienceLen(); i++ { + if o.IsAudienceObject(i) { + c.AddAudienceObject(o.GetAudienceObject(i)) + } else if o.IsAudienceLink(i) { + c.AddAudienceLink(o.GetAudienceLink(i)) + } else if o.IsAudienceIRI(i) { + c.AddAudienceIRI(o.GetAudienceIRI(i)) + } + } + return c +} + // TODO: (Section 7) HTTP caching mechanisms [RFC7234] SHOULD be respected when appropriate, both when receiving responses from other servers as well as sending responses to other servers. // prepare takes a DeliverableObject and returns a list of the proper recipient