2019-02-13 06:43:01 +09:00
|
|
|
package pub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-14 06:37:29 +09:00
|
|
|
"encoding/json"
|
2019-02-13 06:43:01 +09:00
|
|
|
"fmt"
|
2019-02-14 06:37:29 +09:00
|
|
|
"github.com/go-fed/activity/streams"
|
2019-02-13 06:43:01 +09:00
|
|
|
"github.com/go-fed/activity/streams/vocab"
|
2019-02-14 06:37:29 +09:00
|
|
|
"net/url"
|
2019-02-13 06:43:01 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
// OnFollowBehavior enumerates the different default actions that the go-fed
|
|
|
|
// library can provide when receiving a Follow Activity from a peer.
|
|
|
|
type OnFollowBehavior int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// OnFollowDoNothing does not take any action when a Follow Activity
|
|
|
|
// is received.
|
|
|
|
OnFollowDoNothing OnFollowBehavior = iota
|
2019-02-14 06:37:29 +09:00
|
|
|
// OnFollowAutomaticallyAccept triggers the side effect of sending an
|
|
|
|
// Accept of this Follow request in response.
|
2019-02-13 06:43:01 +09:00
|
|
|
OnFollowAutomaticallyAccept
|
2019-02-14 06:37:29 +09:00
|
|
|
// OnFollowAutomaticallyAccept triggers the side effect of sending a
|
|
|
|
// Reject of this Follow request in response.
|
2019-02-13 06:43:01 +09:00
|
|
|
OnFollowAutomaticallyReject
|
|
|
|
)
|
|
|
|
|
|
|
|
// FederatingWrappedCallbacks lists the callback functions that already have
|
|
|
|
// some side effect behavior provided by the pub library.
|
|
|
|
//
|
|
|
|
// These functions are wrapped for the Federating Protocol.
|
|
|
|
type FederatingWrappedCallbacks struct {
|
|
|
|
// Create handles additional side effects for the Create ActivityStreams
|
|
|
|
// type, specific to the application using go-fed.
|
|
|
|
//
|
2019-02-14 06:37:29 +09:00
|
|
|
// The wrapping callback for the Federating Protocol ensures the
|
2019-02-13 06:43:01 +09:00
|
|
|
// 'object' property is created in the database.
|
|
|
|
//
|
2019-02-14 06:37:29 +09:00
|
|
|
// Create calls Create for each object in the federated Activity.
|
2019-02-13 06:43:01 +09:00
|
|
|
Create func(context.Context, vocab.ActivityStreamsCreate) error
|
|
|
|
// Update handles additional side effects for the Update ActivityStreams
|
|
|
|
// type, specific to the application using go-fed.
|
|
|
|
//
|
2019-02-14 06:37:29 +09:00
|
|
|
// The wrapping callback for the Federating Protocol ensures the
|
2019-02-13 06:43:01 +09:00
|
|
|
// 'object' property is updated in the database.
|
|
|
|
//
|
2019-02-14 06:37:29 +09:00
|
|
|
// Update calls Update on the federated entry from the database, with a
|
|
|
|
// new value.
|
2019-02-13 06:43:01 +09:00
|
|
|
Update func(context.Context, vocab.ActivityStreamsUpdate) error
|
|
|
|
// Delete handles additional side effects for the Delete ActivityStreams
|
|
|
|
// type, specific to the application using go-fed.
|
|
|
|
//
|
2019-02-14 06:37:29 +09:00
|
|
|
// Delete removes the federated entry from the database.
|
2019-02-13 06:43:01 +09:00
|
|
|
Delete func(context.Context, vocab.ActivityStreamsDelete) error
|
|
|
|
// Follow handles additional side effects for the Follow ActivityStreams
|
|
|
|
// type, specific to the application using go-fed.
|
|
|
|
//
|
2019-02-14 06:37:29 +09:00
|
|
|
// The wrapping function can have one of several default behaviors,
|
|
|
|
// depending on the value of the OnFollow setting.
|
2019-02-13 06:43:01 +09:00
|
|
|
Follow func(context.Context, vocab.ActivityStreamsFollow) error
|
|
|
|
// OnFollow determines what action to take for this particular callback
|
|
|
|
// if a Follow Activity is handled.
|
|
|
|
OnFollow OnFollowBehavior
|
|
|
|
// Accept handles additional side effects for the Accept ActivityStreams
|
|
|
|
// type, specific to the application using go-fed.
|
|
|
|
//
|
2019-02-14 06:37:29 +09:00
|
|
|
// The wrapping function determines if this 'Accept' is in response to a
|
|
|
|
// 'Follow'. If so, then the 'actor' is added to the original 'actor's
|
|
|
|
// 'following' collection.
|
2019-02-15 05:51:57 +09:00
|
|
|
//
|
|
|
|
// Otherwise, no side effects are done by go-fed.
|
2019-02-13 06:43:01 +09:00
|
|
|
Accept func(context.Context, vocab.ActivityStreamsAccept) error
|
|
|
|
// Reject handles additional side effects for the Reject ActivityStreams
|
|
|
|
// type, specific to the application using go-fed.
|
|
|
|
//
|
2019-02-15 05:51:57 +09:00
|
|
|
// The wrapping function has no default side effects. However, if this
|
|
|
|
// 'Reject' is in response to a 'Follow' then the client MUST NOT go
|
|
|
|
// forward with adding the 'actor' to the original 'actor's 'following'
|
|
|
|
// collection by the client application.
|
2019-02-13 06:43:01 +09:00
|
|
|
Reject func(context.Context, vocab.ActivityStreamsReject) error
|
|
|
|
// Add handles additional side effects for the Add ActivityStreams
|
|
|
|
// type, specific to the application using go-fed.
|
|
|
|
//
|
2019-02-14 06:37:29 +09:00
|
|
|
// The wrapping function will add the 'object' IRIs to a specific
|
|
|
|
// 'target' collection if the 'target' collection(s) live on this
|
|
|
|
// server.
|
2019-02-13 06:43:01 +09:00
|
|
|
Add func(context.Context, vocab.ActivityStreamsAdd) error
|
|
|
|
// Remove handles additional side effects for the Remove ActivityStreams
|
|
|
|
// type, specific to the application using go-fed.
|
|
|
|
//
|
2019-02-14 06:37:29 +09:00
|
|
|
// The wrapping function will remove all 'object' IRIs from a specific
|
|
|
|
// 'target' collection if the 'target' collection(s) live on this
|
|
|
|
// server.
|
2019-02-13 06:43:01 +09:00
|
|
|
Remove func(context.Context, vocab.ActivityStreamsRemove) error
|
|
|
|
// Like handles additional side effects for the Like ActivityStreams
|
|
|
|
// type, specific to the application using go-fed.
|
|
|
|
//
|
2019-02-14 06:37:29 +09:00
|
|
|
// The wrapping function will add the activity to the "likes" collection
|
|
|
|
// on all 'object' targets owned by this server.
|
2019-02-13 06:43:01 +09:00
|
|
|
Like func(context.Context, vocab.ActivityStreamsLike) error
|
|
|
|
// Announce handles additional side effects for the Announce
|
|
|
|
// ActivityStreams type, specific to the application using go-fed.
|
|
|
|
//
|
2019-02-14 06:37:29 +09:00
|
|
|
// The wrapping function will add the activity to the "shares"
|
|
|
|
// collection on all 'object' targets owned by this server.
|
2019-02-13 06:43:01 +09:00
|
|
|
Announce func(context.Context, vocab.ActivityStreamsAnnounce) error
|
|
|
|
// Undo handles additional side effects for the Undo ActivityStreams
|
|
|
|
// type, specific to the application using go-fed.
|
|
|
|
//
|
2019-02-14 06:37:29 +09:00
|
|
|
// The wrapping function ensures the 'actor' on the 'Undo'
|
|
|
|
// is be the same as the 'actor' on all Activities being undone.
|
|
|
|
// It enforces that the actors on the Undo must correspond to all of the
|
|
|
|
// 'object' actors in some manner.
|
2019-02-15 05:51:57 +09:00
|
|
|
//
|
|
|
|
// It is expected that the application will implement the proper
|
|
|
|
// reversal of activities that are being undone.
|
2019-02-13 06:43:01 +09:00
|
|
|
Undo func(context.Context, vocab.ActivityStreamsUndo) error
|
|
|
|
// Block handles additional side effects for the Block ActivityStreams
|
|
|
|
// type, specific to the application using go-fed.
|
|
|
|
//
|
2019-02-15 05:51:57 +09:00
|
|
|
// The wrapping function provides no default side effects. It simply
|
|
|
|
// calls the wrapped function. However, note that Blocks should not be
|
|
|
|
// received from a federated peer, as delivering Blocks explicitly
|
|
|
|
// deviates from the original ActivityPub specification.
|
2019-02-13 06:43:01 +09:00
|
|
|
Block func(context.Context, vocab.ActivityStreamsBlock) error
|
|
|
|
|
|
|
|
// Sidechannel data -- this is set at request handling time. These must
|
|
|
|
// be set before the callbacks are used.
|
|
|
|
|
|
|
|
// db is the Database the FederatingWrappedCallbacks should use.
|
|
|
|
db Database
|
2019-02-14 06:37:29 +09:00
|
|
|
// inboxIRI is the inboxIRI that is handling this callback.
|
|
|
|
inboxIRI *url.URL
|
|
|
|
// newTransport creates a new Transport.
|
|
|
|
newTransport func(c context.Context, actorBoxIRI *url.URL, gofedAgent string) (t Transport, err error)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
|
2019-02-16 05:03:43 +09:00
|
|
|
// callbacks returns the WrappedCallbacks members into a single interface slice
|
|
|
|
// for use in streams.Resolver callbacks.
|
|
|
|
//
|
|
|
|
// If the given functions have a type that collides with the default behavior,
|
|
|
|
// then disable our default behavior
|
|
|
|
func (w FederatingWrappedCallbacks) callbacks(fns []interface{}) []interface{} {
|
|
|
|
enableCreate := true
|
|
|
|
enableUpdate := true
|
|
|
|
enableDelete := true
|
|
|
|
enableFollow := true
|
|
|
|
enableAccept := true
|
|
|
|
enableReject := true
|
|
|
|
enableAdd := true
|
|
|
|
enableRemove := true
|
|
|
|
enableLike := true
|
|
|
|
enableAnnounce := true
|
|
|
|
enableUndo := true
|
|
|
|
enableBlock := true
|
2019-02-13 06:43:01 +09:00
|
|
|
for _, fn := range fns {
|
|
|
|
switch fn.(type) {
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
case func(context.Context, vocab.ActivityStreamsCreate) error:
|
2019-02-16 05:03:43 +09:00
|
|
|
enableCreate = false
|
2019-02-13 06:43:01 +09:00
|
|
|
case func(context.Context, vocab.ActivityStreamsUpdate) error:
|
2019-02-16 05:03:43 +09:00
|
|
|
enableUpdate = false
|
2019-02-13 06:43:01 +09:00
|
|
|
case func(context.Context, vocab.ActivityStreamsDelete) error:
|
2019-02-16 05:03:43 +09:00
|
|
|
enableDelete = false
|
2019-02-13 06:43:01 +09:00
|
|
|
case func(context.Context, vocab.ActivityStreamsFollow) error:
|
2019-02-16 05:03:43 +09:00
|
|
|
enableFollow = false
|
2019-02-13 06:43:01 +09:00
|
|
|
case func(context.Context, vocab.ActivityStreamsAccept) error:
|
2019-02-16 05:03:43 +09:00
|
|
|
enableAccept = false
|
2019-02-13 06:43:01 +09:00
|
|
|
case func(context.Context, vocab.ActivityStreamsReject) error:
|
2019-02-16 05:03:43 +09:00
|
|
|
enableReject = false
|
2019-02-13 06:43:01 +09:00
|
|
|
case func(context.Context, vocab.ActivityStreamsAdd) error:
|
2019-02-16 05:03:43 +09:00
|
|
|
enableAdd = false
|
2019-02-13 06:43:01 +09:00
|
|
|
case func(context.Context, vocab.ActivityStreamsRemove) error:
|
2019-02-16 05:03:43 +09:00
|
|
|
enableRemove = false
|
2019-02-13 06:43:01 +09:00
|
|
|
case func(context.Context, vocab.ActivityStreamsLike) error:
|
2019-02-16 05:03:43 +09:00
|
|
|
enableLike = false
|
2019-02-13 06:43:01 +09:00
|
|
|
case func(context.Context, vocab.ActivityStreamsAnnounce) error:
|
2019-02-16 05:03:43 +09:00
|
|
|
enableAnnounce = false
|
2019-02-13 06:43:01 +09:00
|
|
|
case func(context.Context, vocab.ActivityStreamsUndo) error:
|
2019-02-16 05:03:43 +09:00
|
|
|
enableUndo = false
|
2019-02-13 06:43:01 +09:00
|
|
|
case func(context.Context, vocab.ActivityStreamsBlock) error:
|
2019-02-16 05:03:43 +09:00
|
|
|
enableBlock = false
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
}
|
2019-02-16 05:03:43 +09:00
|
|
|
if enableCreate {
|
|
|
|
fns = append(fns, w.create)
|
|
|
|
}
|
|
|
|
if enableUpdate {
|
|
|
|
fns = append(fns, w.update)
|
|
|
|
}
|
|
|
|
if enableDelete {
|
|
|
|
fns = append(fns, w.deleteFn)
|
|
|
|
}
|
|
|
|
if enableFollow {
|
|
|
|
fns = append(fns, w.follow)
|
|
|
|
}
|
|
|
|
if enableAccept {
|
|
|
|
fns = append(fns, w.accept)
|
|
|
|
}
|
|
|
|
if enableReject {
|
|
|
|
fns = append(fns, w.reject)
|
|
|
|
}
|
|
|
|
if enableAdd {
|
|
|
|
fns = append(fns, w.add)
|
|
|
|
}
|
|
|
|
if enableRemove {
|
|
|
|
fns = append(fns, w.remove)
|
|
|
|
}
|
|
|
|
if enableLike {
|
|
|
|
fns = append(fns, w.like)
|
|
|
|
}
|
|
|
|
if enableAnnounce {
|
|
|
|
fns = append(fns, w.announce)
|
|
|
|
}
|
|
|
|
if enableUndo {
|
|
|
|
fns = append(fns, w.undo)
|
|
|
|
}
|
|
|
|
if enableBlock {
|
|
|
|
fns = append(fns, w.block)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-16 05:03:43 +09:00
|
|
|
return fns
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// create implements the federating Create activity side effects.
|
|
|
|
func (w FederatingWrappedCallbacks) create(c context.Context, a vocab.ActivityStreamsCreate) error {
|
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
// Create anonymous loop function to be able to properly scope the defer
|
|
|
|
// for the database lock at each iteration.
|
|
|
|
loopFn := func(iter vocab.ActivityStreamsObjectPropertyIterator) error {
|
2019-02-13 06:43:01 +09:00
|
|
|
t := iter.GetType()
|
2019-02-16 05:33:48 +09:00
|
|
|
if t == nil && iter.IsIRI() {
|
|
|
|
// Attempt to dereference the IRI instead
|
|
|
|
tport, err := w.newTransport(c, w.inboxIRI, goFedUserAgent())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b, err := tport.Dereference(c, iter.GetIRI())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err = json.Unmarshal(b, &m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-16 06:47:59 +09:00
|
|
|
t, err = streams.ToType(c, m)
|
2019-02-16 05:33:48 +09:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-21 05:31:11 +09:00
|
|
|
} else if t == nil {
|
2019-02-16 05:33:48 +09:00
|
|
|
return fmt.Errorf("cannot handle federated create: object is neither a value nor IRI")
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
id, err := GetId(t)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = w.db.Lock(c, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
defer w.db.Unlock(c, id)
|
2019-02-13 06:43:01 +09:00
|
|
|
if err := w.db.Create(c, t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for iter := op.Begin(); iter != op.End(); iter = iter.Next() {
|
|
|
|
if err := loopFn(iter); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
if w.Create != nil {
|
|
|
|
return w.Create(c, a)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// update implements the federating Update activity side effects.
|
|
|
|
func (w FederatingWrappedCallbacks) update(c context.Context, a vocab.ActivityStreamsUpdate) error {
|
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
|
|
|
}
|
|
|
|
if err := mustHaveActivityOriginMatchObjects(a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
// Create anonymous loop function to be able to properly scope the defer
|
|
|
|
// for the database lock at each iteration.
|
|
|
|
loopFn := func(iter vocab.ActivityStreamsObjectPropertyIterator) error {
|
2019-02-13 06:43:01 +09:00
|
|
|
t := iter.GetType()
|
|
|
|
if t == nil {
|
|
|
|
return fmt.Errorf("update requires an object to be wholly provided")
|
|
|
|
}
|
|
|
|
id, err := GetId(t)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = w.db.Lock(c, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
defer w.db.Unlock(c, id)
|
2019-02-13 06:43:01 +09:00
|
|
|
if err := w.db.Update(c, t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for iter := op.Begin(); iter != op.End(); iter = iter.Next() {
|
|
|
|
if err := loopFn(iter); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
if w.Update != nil {
|
|
|
|
return w.Update(c, a)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteFn implements the federating Delete activity side effects.
|
|
|
|
func (w FederatingWrappedCallbacks) deleteFn(c context.Context, a vocab.ActivityStreamsDelete) error {
|
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
|
|
|
}
|
|
|
|
if err := mustHaveActivityOriginMatchObjects(a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
// Create anonymous loop function to be able to properly scope the defer
|
|
|
|
// for the database lock at each iteration.
|
|
|
|
loopFn := func(iter vocab.ActivityStreamsObjectPropertyIterator) error {
|
2019-02-13 06:43:01 +09:00
|
|
|
id, err := ToId(iter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = w.db.Lock(c, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
defer w.db.Unlock(c, id)
|
2019-02-13 06:43:01 +09:00
|
|
|
if err := w.db.Delete(c, id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for iter := op.Begin(); iter != op.End(); iter = iter.Next() {
|
|
|
|
if err := loopFn(iter); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
if w.Delete != nil {
|
|
|
|
return w.Delete(c, a)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// follow implements the federating Follow activity side effects.
|
|
|
|
func (w FederatingWrappedCallbacks) follow(c context.Context, a vocab.ActivityStreamsFollow) error {
|
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
// Check that we own at least one of the 'object' properties, and ensure
|
|
|
|
// it is to the actor that owns this inbox.
|
|
|
|
//
|
|
|
|
// If not then don't send a response. It was federated to us as an FYI,
|
|
|
|
// by mistake, or some other reason.
|
|
|
|
if err := w.db.Lock(c, w.inboxIRI); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// WARNING: Unlock not deferred.
|
|
|
|
actorIRI, err := w.db.ActorForInbox(c, w.inboxIRI)
|
|
|
|
if err != nil {
|
|
|
|
w.db.Unlock(c, w.inboxIRI)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.db.Unlock(c, w.inboxIRI)
|
|
|
|
// Unlock must be called by now and every branch above.
|
|
|
|
isMe := false
|
2019-02-13 06:43:01 +09:00
|
|
|
if w.OnFollow != OnFollowDoNothing {
|
|
|
|
for iter := op.Begin(); iter != op.End(); iter = iter.Next() {
|
|
|
|
id, err := ToId(iter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
if id.String() == actorIRI.String() {
|
|
|
|
isMe = true
|
2019-02-13 06:43:01 +09:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
if isMe {
|
2019-02-13 06:43:01 +09:00
|
|
|
// Prepare the response.
|
|
|
|
var response Activity
|
|
|
|
if w.OnFollow == OnFollowAutomaticallyAccept {
|
|
|
|
response = streams.NewActivityStreamsAccept()
|
2019-02-14 06:37:29 +09:00
|
|
|
} else if w.OnFollow == OnFollowAutomaticallyReject {
|
2019-02-13 06:43:01 +09:00
|
|
|
response = streams.NewActivityStreamsReject()
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("unknown OnFollowBehavior: %d", w.OnFollow)
|
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
// Set us as the 'actor'.
|
|
|
|
me := streams.NewActivityStreamsActorProperty()
|
|
|
|
response.SetActivityStreamsActor(me)
|
|
|
|
me.AppendIRI(actorIRI)
|
2019-02-13 06:43:01 +09:00
|
|
|
// Set the Follow as the 'object' property.
|
|
|
|
op := streams.NewActivityStreamsObjectProperty()
|
|
|
|
response.SetActivityStreamsObject(op)
|
|
|
|
op.AppendActivityStreamsFollow(a)
|
|
|
|
// Add all actors on the original Follow to the 'to' property.
|
2019-02-14 06:37:29 +09:00
|
|
|
recipients := make([]*url.URL, 0)
|
2019-02-13 06:43:01 +09:00
|
|
|
to := streams.NewActivityStreamsToProperty()
|
|
|
|
response.SetActivityStreamsTo(to)
|
|
|
|
followActors := a.GetActivityStreamsActor()
|
|
|
|
for iter := followActors.Begin(); iter != followActors.End(); iter = iter.Next() {
|
|
|
|
id, err := ToId(iter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
to.AppendIRI(id)
|
2019-02-14 06:37:29 +09:00
|
|
|
recipients = append(recipients, id)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
if w.OnFollow == OnFollowAutomaticallyAccept {
|
2019-02-13 06:43:01 +09:00
|
|
|
// If automatically accepting, then also update our
|
2019-02-14 06:37:29 +09:00
|
|
|
// followers collection with the new actors.
|
|
|
|
//
|
|
|
|
// If automatically rejecting, do not update the
|
2019-02-13 06:43:01 +09:00
|
|
|
// followers collection.
|
2019-02-14 06:37:29 +09:00
|
|
|
if err := w.db.Lock(c, actorIRI); err != nil {
|
2019-02-13 06:43:01 +09:00
|
|
|
return err
|
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
// WARNING: Unlock not deferred.
|
|
|
|
followers, err := w.db.Followers(c, actorIRI)
|
2019-02-13 06:43:01 +09:00
|
|
|
if err != nil {
|
2019-02-14 06:37:29 +09:00
|
|
|
w.db.Unlock(c, actorIRI)
|
2019-02-13 06:43:01 +09:00
|
|
|
return err
|
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
items := followers.GetActivityStreamsItems()
|
|
|
|
for _, elem := range recipients {
|
|
|
|
items.PrependIRI(elem)
|
|
|
|
}
|
|
|
|
if err = w.db.Update(c, followers); err != nil {
|
|
|
|
w.db.Unlock(c, actorIRI)
|
2019-02-13 06:43:01 +09:00
|
|
|
return err
|
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
w.db.Unlock(c, actorIRI)
|
|
|
|
// Unlock must be called by now and every branch above.
|
|
|
|
}
|
|
|
|
m, err := serialize(response)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t, err := w.newTransport(c, w.inboxIRI, goFedUserAgent())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := t.BatchDeliver(c, b, recipients); err != nil {
|
|
|
|
return err
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
if w.Follow != nil {
|
|
|
|
return w.Follow(c, a)
|
|
|
|
}
|
|
|
|
return nil
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// accept implements the federating Accept activity side effects.
|
|
|
|
func (w FederatingWrappedCallbacks) accept(c context.Context, a vocab.ActivityStreamsAccept) error {
|
2019-02-14 06:37:29 +09:00
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op != nil && op.Len() > 0 {
|
|
|
|
// Get this actor's id.
|
|
|
|
if err := w.db.Lock(c, w.inboxIRI); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// WARNING: Unlock not deferred.
|
|
|
|
actorIRI, err := w.db.ActorForInbox(c, w.inboxIRI)
|
|
|
|
if err != nil {
|
|
|
|
w.db.Unlock(c, w.inboxIRI)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.db.Unlock(c, w.inboxIRI)
|
|
|
|
// Unlock must be called by now and every branch above.
|
|
|
|
//
|
|
|
|
// Determine if we are in a follow on the 'object' property.
|
2019-02-24 21:05:00 +09:00
|
|
|
//
|
|
|
|
// TODO: Handle Accept multiple Follow.
|
2019-02-16 05:33:48 +09:00
|
|
|
var maybeMyFollowIRI *url.URL
|
2019-02-14 06:37:29 +09:00
|
|
|
for iter := op.Begin(); iter != op.End(); iter = iter.Next() {
|
|
|
|
t := iter.GetType()
|
2019-02-16 05:33:48 +09:00
|
|
|
if t == nil && iter.IsIRI() {
|
|
|
|
// Attempt to dereference the IRI instead
|
|
|
|
tport, err := w.newTransport(c, w.inboxIRI, goFedUserAgent())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b, err := tport.Dereference(c, iter.GetIRI())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err = json.Unmarshal(b, &m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-16 06:47:59 +09:00
|
|
|
t, err = streams.ToType(c, m)
|
2019-02-16 05:33:48 +09:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-21 05:31:11 +09:00
|
|
|
} else if t == nil {
|
2019-02-16 05:33:48 +09:00
|
|
|
return fmt.Errorf("cannot handle federated create: object is neither a value nor IRI")
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
// Ensure it is a Follow.
|
|
|
|
if !streams.ActivityStreamsFollowIsExtendedBy(t) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
follow, ok := t.(Activity)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("a Follow in an Accept does not satisfy the Activity interface")
|
|
|
|
}
|
2019-02-16 05:33:48 +09:00
|
|
|
followId, err := GetId(follow)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
// Ensure that we are one of the actors on the Follow.
|
|
|
|
actors := follow.GetActivityStreamsActor()
|
|
|
|
for iter := actors.Begin(); iter != actors.End(); iter = iter.Next() {
|
|
|
|
id, err := ToId(iter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if id.String() == actorIRI.String() {
|
2019-02-16 05:33:48 +09:00
|
|
|
maybeMyFollowIRI = followId
|
2019-02-14 06:37:29 +09:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Continue breaking if we found ourselves
|
2019-02-16 05:33:48 +09:00
|
|
|
if maybeMyFollowIRI != nil {
|
2019-02-14 06:37:29 +09:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we received an Accept whose 'object' is a Follow with an
|
|
|
|
// Accept that we sent, add to the following collection.
|
2019-02-16 05:33:48 +09:00
|
|
|
if maybeMyFollowIRI != nil {
|
|
|
|
// Verify our Follow request exists and the peer didn't
|
|
|
|
// fabricate it.
|
2019-02-24 21:05:00 +09:00
|
|
|
actors := a.GetActivityStreamsActor()
|
|
|
|
if actors == nil || actors.Len() == 0 {
|
|
|
|
return fmt.Errorf("an Accept with a Follow has no actors")
|
|
|
|
}
|
|
|
|
// This may be a duplicate check if we dereferenced the
|
|
|
|
// Follow above. TODO: Separate this logic to avoid
|
|
|
|
// redundancy.
|
2019-02-16 05:33:48 +09:00
|
|
|
//
|
|
|
|
// Use an anonymous function to properly scope the
|
|
|
|
// database lock, immediately call it.
|
|
|
|
err = func() error {
|
|
|
|
if err := w.db.Lock(c, maybeMyFollowIRI); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer w.db.Unlock(c, maybeMyFollowIRI)
|
|
|
|
t, err := w.db.Get(c, maybeMyFollowIRI)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !streams.ActivityStreamsFollowIsExtendedBy(t) {
|
|
|
|
return fmt.Errorf("peer gave an Accept wrapping a Follow but provided a non-Follow id")
|
|
|
|
}
|
|
|
|
follow, ok := t.(Activity)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("a Follow in an Accept does not satisfy the Activity interface")
|
|
|
|
}
|
|
|
|
// Ensure that we are one of the actors on the Follow.
|
2019-02-24 21:05:00 +09:00
|
|
|
ok = false
|
2019-02-16 05:33:48 +09:00
|
|
|
actors := follow.GetActivityStreamsActor()
|
|
|
|
for iter := actors.Begin(); iter != actors.End(); iter = iter.Next() {
|
|
|
|
id, err := ToId(iter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if id.String() == actorIRI.String() {
|
2019-02-24 21:05:00 +09:00
|
|
|
ok = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("peer gave an Accept wrapping a Follow but we are not the actor on that Follow")
|
|
|
|
}
|
|
|
|
// Build map of original Accept actors
|
|
|
|
acceptActors := make(map[string]bool)
|
|
|
|
for iter := actors.Begin(); iter != actors.End(); iter = iter.Next() {
|
|
|
|
id, err := ToId(iter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-02-16 05:33:48 +09:00
|
|
|
}
|
2019-02-24 21:05:00 +09:00
|
|
|
acceptActors[id.String()] = false
|
2019-02-16 05:33:48 +09:00
|
|
|
}
|
2019-02-24 21:05:00 +09:00
|
|
|
// Verify all actor(s) were on the original Follow.
|
|
|
|
followObj := follow.GetActivityStreamsObject()
|
|
|
|
for iter := followObj.Begin(); iter != followObj.End(); iter = iter.Next() {
|
|
|
|
id, err := ToId(iter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, ok := acceptActors[id.String()]; ok {
|
|
|
|
acceptActors[id.String()] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, found := range acceptActors {
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("peer gave an Accept wrapping a Follow but was not an object in the original Follow")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2019-02-16 05:33:48 +09:00
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Add the peer to our following collection.
|
2019-02-14 06:37:29 +09:00
|
|
|
if err := w.db.Lock(c, actorIRI); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// WARNING: Unlock not deferred.
|
|
|
|
following, err := w.db.Following(c, actorIRI)
|
|
|
|
if err != nil {
|
|
|
|
w.db.Unlock(c, actorIRI)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
items := following.GetActivityStreamsItems()
|
|
|
|
for iter := actors.Begin(); iter != actors.End(); iter = iter.Next() {
|
|
|
|
id, err := ToId(iter)
|
|
|
|
if err != nil {
|
|
|
|
w.db.Unlock(c, actorIRI)
|
|
|
|
return err
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
items.PrependIRI(id)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
if err = w.db.Update(c, following); err != nil {
|
|
|
|
w.db.Unlock(c, actorIRI)
|
2019-02-13 06:43:01 +09:00
|
|
|
return err
|
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
w.db.Unlock(c, actorIRI)
|
|
|
|
// Unlock must be called by now and every branch above.
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
if w.Accept != nil {
|
|
|
|
return w.Accept(c, a)
|
|
|
|
}
|
|
|
|
return nil
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// reject implements the federating Reject activity side effects.
|
|
|
|
func (w FederatingWrappedCallbacks) reject(c context.Context, a vocab.ActivityStreamsReject) error {
|
2019-02-14 06:37:29 +09:00
|
|
|
if w.Reject != nil {
|
|
|
|
return w.Reject(c, a)
|
|
|
|
}
|
|
|
|
return nil
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// add implements the federating Add activity side effects.
|
|
|
|
func (w FederatingWrappedCallbacks) add(c context.Context, a vocab.ActivityStreamsAdd) error {
|
2019-02-14 06:37:29 +09:00
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
target := a.GetActivityStreamsTarget()
|
|
|
|
if target == nil || target.Len() == 0 {
|
|
|
|
return ErrTargetRequired
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
if err := add(c, op, target, w.db); err != nil {
|
|
|
|
return err
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
if w.Add != nil {
|
|
|
|
return w.Add(c, a)
|
|
|
|
}
|
|
|
|
return nil
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove implements the federating Remove activity side effects.
|
|
|
|
func (w FederatingWrappedCallbacks) remove(c context.Context, a vocab.ActivityStreamsRemove) error {
|
2019-02-14 06:37:29 +09:00
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
target := a.GetActivityStreamsTarget()
|
|
|
|
if target == nil || target.Len() == 0 {
|
|
|
|
return ErrTargetRequired
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
if err := remove(c, op, target, w.db); err != nil {
|
|
|
|
return err
|
2019-02-14 06:37:29 +09:00
|
|
|
}
|
|
|
|
if w.Remove != nil {
|
|
|
|
return w.Remove(c, a)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
return nil
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// like implements the federating Like activity side effects.
|
|
|
|
func (w FederatingWrappedCallbacks) like(c context.Context, a vocab.ActivityStreamsLike) error {
|
2019-02-14 06:37:29 +09:00
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
id, err := GetId(a)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
// Create anonymous loop function to be able to properly scope the defer
|
|
|
|
// for the database lock at each iteration.
|
|
|
|
loopFn := func(iter vocab.ActivityStreamsObjectPropertyIterator) error {
|
2019-02-14 06:37:29 +09:00
|
|
|
objId, err := ToId(iter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := w.db.Lock(c, objId); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
defer w.db.Unlock(c, objId)
|
2019-02-14 06:37:29 +09:00
|
|
|
if owns, err := w.db.Owns(c, objId); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !owns {
|
2019-02-15 05:51:57 +09:00
|
|
|
return nil
|
2019-02-14 06:37:29 +09:00
|
|
|
}
|
|
|
|
t, err := w.db.Get(c, objId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
l, ok := t.(likeser)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("cannot add Like to likes collection for type %T", t)
|
|
|
|
}
|
|
|
|
// Get 'likes' property on the object, creating default if
|
|
|
|
// necessary.
|
|
|
|
likes := l.GetActivityStreamsLikes()
|
|
|
|
if likes == nil {
|
|
|
|
likes = streams.NewActivityStreamsLikesProperty()
|
|
|
|
l.SetActivityStreamsLikes(likes)
|
|
|
|
}
|
|
|
|
// Get 'likes' value, defaulting to a collection.
|
|
|
|
likesT := likes.GetType()
|
|
|
|
if likesT == nil {
|
|
|
|
col := streams.NewActivityStreamsCollection()
|
|
|
|
likesT = col
|
|
|
|
likes.SetActivityStreamsCollection(col)
|
|
|
|
}
|
|
|
|
// Prepend the activity's 'id' on the 'likes' Collection or
|
|
|
|
// OrderedCollection.
|
|
|
|
if col, ok := likesT.(itemser); ok {
|
|
|
|
items := col.GetActivityStreamsItems()
|
|
|
|
if items == nil {
|
|
|
|
items = streams.NewActivityStreamsItemsProperty()
|
|
|
|
col.SetActivityStreamsItems(items)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
items.PrependIRI(id)
|
|
|
|
} else if oCol, ok := likesT.(orderedItemser); ok {
|
|
|
|
oItems := oCol.GetActivityStreamsOrderedItems()
|
|
|
|
if oItems == nil {
|
|
|
|
oItems = streams.NewActivityStreamsOrderedItemsProperty()
|
|
|
|
oCol.SetActivityStreamsOrderedItems(oItems)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
oItems.PrependIRI(id)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("likes type is neither a Collection nor an OrderedCollection: %T", likesT)
|
|
|
|
}
|
|
|
|
err = w.db.Update(c, t)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for iter := op.Begin(); iter != op.End(); iter = iter.Next() {
|
|
|
|
if err := loopFn(iter); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
if w.Like != nil {
|
|
|
|
return w.Like(c, a)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
return nil
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// announce implements the federating Announce activity side effects.
|
|
|
|
func (w FederatingWrappedCallbacks) announce(c context.Context, a vocab.ActivityStreamsAnnounce) error {
|
2019-02-14 06:37:29 +09:00
|
|
|
id, err := GetId(a)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
op := a.GetActivityStreamsObject()
|
2019-02-15 05:51:57 +09:00
|
|
|
// Create anonymous loop function to be able to properly scope the defer
|
|
|
|
// for the database lock at each iteration.
|
|
|
|
loopFn := func(iter vocab.ActivityStreamsObjectPropertyIterator) error {
|
2019-02-14 06:37:29 +09:00
|
|
|
objId, err := ToId(iter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := w.db.Lock(c, objId); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
defer w.db.Unlock(c, objId)
|
2019-02-14 06:37:29 +09:00
|
|
|
if owns, err := w.db.Owns(c, objId); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !owns {
|
2019-02-15 05:51:57 +09:00
|
|
|
return nil
|
2019-02-14 06:37:29 +09:00
|
|
|
}
|
|
|
|
t, err := w.db.Get(c, objId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s, ok := t.(shareser)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("cannot add Announce to Shares collection for type %T", t)
|
|
|
|
}
|
|
|
|
// Get 'shares' property on the object, creating default if
|
|
|
|
// necessary.
|
|
|
|
shares := s.GetActivityStreamsShares()
|
|
|
|
if shares == nil {
|
|
|
|
shares = streams.NewActivityStreamsSharesProperty()
|
|
|
|
s.SetActivityStreamsShares(shares)
|
|
|
|
}
|
|
|
|
// Get 'shares' value, defaulting to a collection.
|
|
|
|
sharesT := shares.GetType()
|
2019-02-14 07:56:36 +09:00
|
|
|
if sharesT == nil {
|
2019-02-14 06:37:29 +09:00
|
|
|
col := streams.NewActivityStreamsCollection()
|
|
|
|
sharesT = col
|
|
|
|
shares.SetActivityStreamsCollection(col)
|
|
|
|
}
|
|
|
|
// Prepend the activity's 'id' on the 'shares' Collection or
|
|
|
|
// OrderedCollection.
|
|
|
|
if col, ok := sharesT.(itemser); ok {
|
|
|
|
items := col.GetActivityStreamsItems()
|
|
|
|
if items == nil {
|
|
|
|
items = streams.NewActivityStreamsItemsProperty()
|
|
|
|
col.SetActivityStreamsItems(items)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
items.PrependIRI(id)
|
|
|
|
} else if oCol, ok := sharesT.(orderedItemser); ok {
|
|
|
|
oItems := oCol.GetActivityStreamsOrderedItems()
|
|
|
|
if oItems == nil {
|
|
|
|
oItems = streams.NewActivityStreamsOrderedItemsProperty()
|
|
|
|
oCol.SetActivityStreamsOrderedItems(oItems)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
oItems.PrependIRI(id)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("shares type is neither a Collection nor an OrderedCollection: %T", sharesT)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
err = w.db.Update(c, t)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for iter := op.Begin(); iter != op.End(); iter = iter.Next() {
|
|
|
|
if err := loopFn(iter); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 06:37:29 +09:00
|
|
|
if w.Announce != nil {
|
|
|
|
return w.Announce(c, a)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// undo implements the federating Undo activity side effects.
|
|
|
|
func (w FederatingWrappedCallbacks) undo(c context.Context, a vocab.ActivityStreamsUndo) error {
|
2019-02-14 06:37:29 +09:00
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
|
|
|
}
|
|
|
|
actors := a.GetActivityStreamsActor()
|
2019-02-16 05:42:56 +09:00
|
|
|
if err := mustHaveActivityActorsMatchObjectActors(c, actors, op, w.newTransport, w.inboxIRI); err != nil {
|
2019-02-15 05:51:57 +09:00
|
|
|
return err
|
2019-02-14 06:37:29 +09:00
|
|
|
}
|
|
|
|
if w.Undo != nil {
|
|
|
|
return w.Undo(c, a)
|
|
|
|
}
|
|
|
|
return nil
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// block implements the federating Block activity side effects.
|
|
|
|
func (w FederatingWrappedCallbacks) block(c context.Context, a vocab.ActivityStreamsBlock) error {
|
2019-02-14 06:37:29 +09:00
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
|
|
|
}
|
|
|
|
if w.Block != nil {
|
|
|
|
return w.Block(c, a)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|