Add ids to Accept/Rejects to Follows; clean out nested @context in serialization

This commit is contained in:
Cory Slep 2019-05-03 23:39:40 +02:00
parent 3bc578e7dd
commit 8dd3c056b3
3 changed files with 20 additions and 5 deletions

View file

@ -131,6 +131,9 @@ type FederatingWrappedCallbacks struct {
db Database
// inboxIRI is the inboxIRI that is handling this callback.
inboxIRI *url.URL
// addNewIds creates new 'id' entries on an activity and its objects if
// it is a Create activity.
addNewIds func(c context.Context, activity Activity) error
// deliver delivers an outgoing message.
deliver func(c context.Context, outboxIRI *url.URL, activity Activity) error
// newTransport creates a new Transport.
@ -424,10 +427,6 @@ func (w FederatingWrappedCallbacks) follow(c context.Context, a vocab.ActivitySt
to.AppendIRI(id)
recipients = append(recipients, id)
}
// Set the 'attributedTo' property on the activity.
attrTo := streams.NewActivityStreamsAttributedToProperty()
attrTo.AppendIRI(actorIRI)
response.SetActivityStreamsAttributedTo(attrTo)
if w.OnFollow == OnFollowAutomaticallyAccept {
// If automatically accepting, then also update our
// followers collection with the new actors.
@ -463,7 +462,9 @@ func (w FederatingWrappedCallbacks) follow(c context.Context, a vocab.ActivitySt
}
w.db.Unlock(c, w.inboxIRI)
// Everything must be unlocked by now.
if err := w.deliver(c, outboxIRI, response); err != nil {
if err := w.addNewIds(c, response); err != nil {
return err
} else if err := w.deliver(c, outboxIRI, response); err != nil {
return err
}
}

View file

@ -106,6 +106,7 @@ func (a *sideEffectActor) PostInbox(c context.Context, inboxIRI *url.URL, activi
wrapped.inboxIRI = inboxIRI
wrapped.newTransport = a.common.NewTransport
wrapped.deliver = a.Deliver
wrapped.addNewIds = a.AddNewIds
res, err := streams.NewTypeResolver(wrapped.callbacks(other)...)
if err != nil {
return err

View file

@ -126,6 +126,8 @@ const (
// addJSONLDContext adds the context vocabularies contained within the type
// into the JSON-LD @context field, and aliases them appropriately.
//
// TODO: This probably belongs in the streams package as a public method.
func serialize(a vocab.Type) (m map[string]interface{}, e error) {
m, e = a.Serialize()
if e != nil {
@ -159,6 +161,17 @@ func serialize(a vocab.Type) (m map[string]interface{}, e error) {
}
// TODO: Update the context instead if it already exists
m[jsonLDContext] = contextValue
// Delete any existing `@context` in child maps.
var cleanFnRecur func(map[string]interface{})
cleanFnRecur = func(r map[string]interface{}) {
for _, v := range r {
if n, ok := v.(map[string]interface{}); ok {
delete(n, jsonLDContext)
cleanFnRecur(n)
}
}
}
cleanFnRecur(m)
return
}