2019-02-13 06:43:01 +09:00
|
|
|
package pub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-14 07:56:36 +09:00
|
|
|
"fmt"
|
|
|
|
"github.com/go-fed/activity/streams"
|
2019-02-13 06:43:01 +09:00
|
|
|
"github.com/go-fed/activity/streams/vocab"
|
2019-02-14 07:56:36 +09:00
|
|
|
"net/url"
|
2019-02-13 06:43:01 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
// SocialWrappedCallbacks lists the callback functions that already have some
|
|
|
|
// side effect behavior provided by the pub library.
|
|
|
|
//
|
|
|
|
// These functions are wrapped for the Social Protocol.
|
|
|
|
type SocialWrappedCallbacks struct {
|
|
|
|
// Create handles additional side effects for the Create ActivityStreams
|
|
|
|
// type.
|
|
|
|
//
|
2019-02-15 05:51:57 +09:00
|
|
|
// The wrapping callback copies the actor(s) to the 'attributedTo'
|
|
|
|
// property and copies recipients between the Create activity and all
|
|
|
|
// objects. It then saves the entry in the database.
|
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.
|
|
|
|
//
|
2019-02-15 05:51:57 +09:00
|
|
|
// The wrapping callback applies new top-level values on an object to
|
|
|
|
// the stored objects. Any top-level null literals will be deleted on
|
|
|
|
// the stored objects as well.
|
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.
|
|
|
|
//
|
2019-02-15 05:51:57 +09:00
|
|
|
// The wrapping callback replaces the object(s) with tombstones in 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.
|
|
|
|
//
|
2019-02-15 05:51:57 +09:00
|
|
|
// The wrapping callback only ensures the 'Follow' has at least one
|
|
|
|
// 'object' entry, but otherwise has no default side effect.
|
2019-02-13 06:43:01 +09:00
|
|
|
Follow func(context.Context, vocab.ActivityStreamsFollow) error
|
|
|
|
// Add handles additional side effects for the Add ActivityStreams
|
|
|
|
// type.
|
|
|
|
//
|
2019-02-16 05:03:43 +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.
|
|
|
|
//
|
2019-02-16 05:03:43 +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.
|
|
|
|
//
|
2019-02-16 05:03:43 +09:00
|
|
|
// The wrapping function will add the objects on the activity to the
|
|
|
|
// "liked" collection of this actor.
|
2019-02-13 06:43:01 +09:00
|
|
|
Like func(context.Context, vocab.ActivityStreamsLike) error
|
|
|
|
// Undo handles additional side effects for the Undo ActivityStreams
|
|
|
|
// type.
|
|
|
|
//
|
2019-02-16 05:03:43 +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.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2019-02-15 05:51:57 +09:00
|
|
|
// The wrapping callback only ensures the 'Block' has at least one
|
|
|
|
// 'object' entry, but otherwise has no default side effect. It is up
|
|
|
|
// to the wrapped application function to properly enforce the new
|
|
|
|
// blocking behavior.
|
|
|
|
//
|
|
|
|
// Note that go-fed does not federate 'Block' activities received in the
|
|
|
|
// Social Protocol.
|
2019-02-13 06:43:01 +09:00
|
|
|
Block func(context.Context, vocab.ActivityStreamsBlock) error
|
2019-02-14 07:56:36 +09:00
|
|
|
|
|
|
|
// Sidechannel data -- this is set at request handling time. These must
|
|
|
|
// be set before the callbacks are used.
|
|
|
|
|
2019-02-13 06:43:01 +09:00
|
|
|
// db is the Database the SocialWrappedCallbacks should use. It must be
|
|
|
|
// set before calling the callbacks.
|
|
|
|
db Database
|
2019-02-15 05:51:57 +09:00
|
|
|
// outboxIRI is the outboxIRI that is handling this callback.
|
|
|
|
outboxIRI *url.URL
|
|
|
|
// rawActivity is the JSON map literal received when deserializing the
|
|
|
|
// request body.
|
|
|
|
rawActivity map[string]interface{}
|
|
|
|
// clock is the server's clock.
|
|
|
|
clock Clock
|
2019-02-16 05:42:56 +09:00
|
|
|
// newTransport creates a new Transport.
|
|
|
|
newTransport func(c context.Context, actorBoxIRI *url.URL, gofedAgent string) (t Transport, err error)
|
2019-03-04 08:06:37 +09:00
|
|
|
// undeliverable is a sidechannel out, indicating if the handled activity
|
|
|
|
// should not be delivered to a peer.
|
|
|
|
//
|
|
|
|
// Its provided default value will always be used when a custom function
|
|
|
|
// is called.
|
|
|
|
undeliverable *bool
|
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 SocialWrappedCallbacks) callbacks(fns []interface{}) []interface{} {
|
|
|
|
enableCreate := true
|
|
|
|
enableUpdate := true
|
|
|
|
enableDelete := true
|
|
|
|
enableFollow := true
|
|
|
|
enableAdd := true
|
|
|
|
enableRemove := true
|
|
|
|
enableLike := 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.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.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 enableAdd {
|
|
|
|
fns = append(fns, w.add)
|
|
|
|
}
|
|
|
|
if enableRemove {
|
|
|
|
fns = append(fns, w.remove)
|
|
|
|
}
|
|
|
|
if enableLike {
|
|
|
|
fns = append(fns, w.like)
|
|
|
|
}
|
|
|
|
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 social Create activity side effects.
|
|
|
|
func (w SocialWrappedCallbacks) create(c context.Context, a vocab.ActivityStreamsCreate) error {
|
2019-03-04 08:06:37 +09:00
|
|
|
*w.undeliverable = false
|
2019-02-14 07:56:36 +09:00
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 07:56:36 +09:00
|
|
|
// Obtain all actor IRIs.
|
|
|
|
actors := a.GetActivityStreamsActor()
|
2019-03-04 08:06:37 +09:00
|
|
|
createActorIds := make(map[string]*url.URL, 0)
|
|
|
|
if actors != nil {
|
|
|
|
createActorIds = make(map[string]*url.URL, actors.Len())
|
|
|
|
for iter := actors.Begin(); iter != actors.End(); iter = iter.Next() {
|
|
|
|
id, err := ToId(iter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
createActorIds[id.String()] = id
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
}
|
2019-02-14 07:56:36 +09:00
|
|
|
// Obtain each object's 'attributedTo' IRIs.
|
|
|
|
objectAttributedToIds := make([]map[string]*url.URL, op.Len())
|
2019-02-13 06:43:01 +09:00
|
|
|
for i := range objectAttributedToIds {
|
2019-02-14 07:56:36 +09:00
|
|
|
objectAttributedToIds[i] = make(map[string]*url.URL)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 07:56:36 +09:00
|
|
|
for i := 0; i < op.Len(); i++ {
|
|
|
|
t := op.At(i).GetType()
|
|
|
|
attrToer, ok := t.(attributedToer)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
attr := attrToer.GetActivityStreamsAttributedTo()
|
|
|
|
if attr == nil {
|
|
|
|
attr = streams.NewActivityStreamsAttributedToProperty()
|
|
|
|
attrToer.SetActivityStreamsAttributedTo(attr)
|
|
|
|
}
|
|
|
|
for iter := attr.Begin(); iter != attr.End(); iter = iter.Next() {
|
|
|
|
id, err := ToId(iter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 07:56:36 +09:00
|
|
|
objectAttributedToIds[i][id.String()] = id
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
}
|
2019-02-14 07:56:36 +09:00
|
|
|
// Put all missing actor IRIs onto all object attributedTo properties.
|
2019-02-13 06:43:01 +09:00
|
|
|
for k, v := range createActorIds {
|
|
|
|
for i, attributedToMap := range objectAttributedToIds {
|
|
|
|
if _, ok := attributedToMap[k]; !ok {
|
2019-02-14 07:56:36 +09:00
|
|
|
t := op.At(i).GetType()
|
|
|
|
attrToer, ok := t.(attributedToer)
|
|
|
|
if !ok {
|
|
|
|
continue
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 07:56:36 +09:00
|
|
|
attr := attrToer.GetActivityStreamsAttributedTo()
|
|
|
|
attr.AppendIRI(v)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-14 07:56:36 +09:00
|
|
|
// Put all missing object attributedTo IRIs onto the actor property.
|
2019-02-13 06:43:01 +09:00
|
|
|
for _, attributedToMap := range objectAttributedToIds {
|
|
|
|
for k, v := range attributedToMap {
|
|
|
|
if _, ok := createActorIds[k]; !ok {
|
2019-02-14 07:56:36 +09:00
|
|
|
actors.AppendIRI(v)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-14 07:56:36 +09:00
|
|
|
// Copy over the 'to', 'bto', 'cc', 'bcc', and 'audience' recipients
|
|
|
|
// between the activity and all child objects and vice versa.
|
|
|
|
if err := normalizeRecipients(a); err != nil {
|
2019-02-13 06:43:01 +09:00
|
|
|
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(i int) error {
|
|
|
|
obj := op.At(i).GetType()
|
|
|
|
id, err := GetId(obj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = w.db.Lock(c, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer w.db.Unlock(c, id)
|
|
|
|
if err := w.db.Create(c, obj); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-02-14 07:56:36 +09:00
|
|
|
// Persist all objects we've created, which will include sensitive
|
|
|
|
// recipients such as 'bcc' and 'bto'.
|
|
|
|
for i := 0; i < op.Len(); i++ {
|
2019-02-15 05:51:57 +09:00
|
|
|
if err := loopFn(i); err != nil {
|
2019-02-13 06:43:01 +09:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-02-14 07:56:36 +09:00
|
|
|
if w.Create != nil {
|
|
|
|
return w.Create(c, a)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-14 07:56:36 +09:00
|
|
|
return nil
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// update implements the social Update activity side effects.
|
|
|
|
func (w SocialWrappedCallbacks) update(c context.Context, a vocab.ActivityStreamsUpdate) error {
|
2019-03-04 08:06:37 +09:00
|
|
|
*w.undeliverable = false
|
2019-02-15 05:51:57 +09:00
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
// Obtain all object ids, which should be owned by this server.
|
|
|
|
objIds := make([]*url.URL, 0, op.Len())
|
|
|
|
for iter := op.Begin(); iter != op.End(); iter = iter.Next() {
|
|
|
|
id, err := ToId(iter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
objIds = append(objIds, id)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
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(idx int, loopId *url.URL) error {
|
|
|
|
err := w.db.Lock(c, loopId)
|
2019-02-13 06:43:01 +09:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
defer w.db.Unlock(c, loopId)
|
|
|
|
t, err := w.db.Get(c, loopId)
|
2019-02-13 06:43:01 +09:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
m, err := t.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Copy over new top-level values.
|
|
|
|
objType := op.At(idx).GetType()
|
|
|
|
if objType == nil {
|
|
|
|
return fmt.Errorf("object at index %d is not a literal type value", idx)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
newM, err := objType.Serialize()
|
2019-02-13 06:43:01 +09:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
for k, v := range newM {
|
2019-02-13 06:43:01 +09:00
|
|
|
m[k] = v
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
// Delete top-level values where the raw Activity had nils.
|
|
|
|
for k, v := range w.rawActivity {
|
|
|
|
if _, ok := m[k]; v == nil && ok {
|
|
|
|
delete(m, k)
|
|
|
|
}
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-16 06:47:59 +09:00
|
|
|
newT, err := streams.ToType(c, m)
|
2019-02-13 06:43:01 +09:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
if err = w.db.Update(c, newT); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for i, id := range objIds {
|
|
|
|
if err := loopFn(i, id); err != nil {
|
|
|
|
return err
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
if w.Update != nil {
|
|
|
|
return w.Update(c, a)
|
|
|
|
}
|
|
|
|
return nil
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// deleteFn implements the social Delete activity side effects.
|
|
|
|
func (w SocialWrappedCallbacks) deleteFn(c context.Context, a vocab.ActivityStreamsDelete) error {
|
2019-03-04 08:06:37 +09:00
|
|
|
*w.undeliverable = false
|
2019-02-15 05:51:57 +09:00
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
// Obtain all object ids, which should be owned by this server.
|
|
|
|
objIds := make([]*url.URL, 0, op.Len())
|
|
|
|
for iter := op.Begin(); iter != op.End(); iter = iter.Next() {
|
|
|
|
id, err := ToId(iter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
objIds = append(objIds, id)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
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(idx int, loopId *url.URL) error {
|
|
|
|
err := w.db.Lock(c, loopId)
|
2019-02-13 06:43:01 +09:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
defer w.db.Unlock(c, loopId)
|
|
|
|
t, err := w.db.Get(c, loopId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tomb := toTombstone(t, loopId, w.clock.Now())
|
|
|
|
if err := w.db.Update(c, tomb); err != nil {
|
|
|
|
return err
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for i, id := range objIds {
|
|
|
|
if err := loopFn(i, id); err != nil {
|
2019-02-13 06:43:01 +09:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
if w.Delete != nil {
|
|
|
|
return w.Delete(c, a)
|
|
|
|
}
|
|
|
|
return nil
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// follow implements the social Follow activity side effects.
|
|
|
|
func (w SocialWrappedCallbacks) follow(c context.Context, a vocab.ActivityStreamsFollow) error {
|
2019-03-04 08:06:37 +09:00
|
|
|
*w.undeliverable = false
|
2019-02-15 05:51:57 +09:00
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
if w.Follow != nil {
|
|
|
|
return w.Follow(c, a)
|
|
|
|
}
|
|
|
|
return nil
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// add implements the social Add activity side effects.
|
|
|
|
func (w SocialWrappedCallbacks) add(c context.Context, a vocab.ActivityStreamsAdd) error {
|
2019-03-04 08:06:37 +09:00
|
|
|
*w.undeliverable = false
|
2019-02-15 05:51:57 +09:00
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +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-15 05:51:57 +09:00
|
|
|
if w.Add != nil {
|
|
|
|
return w.Add(c, a)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
return nil
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove implements the social Remove activity side effects.
|
|
|
|
func (w SocialWrappedCallbacks) remove(c context.Context, a vocab.ActivityStreamsRemove) error {
|
2019-03-04 08:06:37 +09:00
|
|
|
*w.undeliverable = false
|
2019-02-15 05:51:57 +09:00
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +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-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
if w.Remove != nil {
|
|
|
|
return w.Remove(c, a)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
return nil
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// like implements the social Like activity side effects.
|
|
|
|
func (w SocialWrappedCallbacks) like(c context.Context, a vocab.ActivityStreamsLike) error {
|
2019-03-04 08:06:37 +09:00
|
|
|
*w.undeliverable = false
|
2019-02-15 05:51:57 +09:00
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
// Get this actor's IRI.
|
|
|
|
if err := w.db.Lock(c, w.outboxIRI); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// WARNING: Unlock not deferred.
|
|
|
|
actorIRI, err := w.db.ActorForInbox(c, w.outboxIRI)
|
|
|
|
if err != nil {
|
|
|
|
w.db.Unlock(c, w.outboxIRI)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.db.Unlock(c, w.outboxIRI)
|
|
|
|
// Unlock must be called by now and every branch above.
|
|
|
|
//
|
|
|
|
// Now obtain this actor's 'liked' collection.
|
|
|
|
if err := w.db.Lock(c, actorIRI); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer w.db.Unlock(c, actorIRI)
|
|
|
|
liked, err := w.db.Liked(c, actorIRI)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
likedItems := liked.GetActivityStreamsItems()
|
|
|
|
if likedItems == nil {
|
|
|
|
likedItems = streams.NewActivityStreamsItemsProperty()
|
|
|
|
liked.SetActivityStreamsItems(likedItems)
|
|
|
|
}
|
|
|
|
for iter := op.Begin(); iter != op.End(); iter = iter.Next() {
|
|
|
|
objId, err := ToId(iter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
likedItems.PrependIRI(objId)
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
err = w.db.Update(c, liked)
|
|
|
|
if err != nil {
|
2019-02-13 06:43:01 +09:00
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
if w.Like != nil {
|
|
|
|
return w.Like(c, a)
|
|
|
|
}
|
|
|
|
return nil
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// undo implements the social Undo activity side effects.
|
|
|
|
func (w SocialWrappedCallbacks) undo(c context.Context, a vocab.ActivityStreamsUndo) error {
|
2019-03-04 08:06:37 +09:00
|
|
|
*w.undeliverable = false
|
2019-02-15 05:51:57 +09:00
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
actors := a.GetActivityStreamsActor()
|
2019-02-16 05:42:56 +09:00
|
|
|
if err := mustHaveActivityActorsMatchObjectActors(c, actors, op, w.newTransport, w.outboxIRI); err != nil {
|
2019-02-13 06:43:01 +09:00
|
|
|
return err
|
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
if w.Undo != nil {
|
|
|
|
return w.Undo(c, a)
|
|
|
|
}
|
|
|
|
return nil
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// block implements the social Block activity side effects.
|
|
|
|
func (w SocialWrappedCallbacks) block(c context.Context, a vocab.ActivityStreamsBlock) error {
|
2019-03-04 08:06:37 +09:00
|
|
|
*w.undeliverable = true
|
2019-02-15 05:51:57 +09:00
|
|
|
op := a.GetActivityStreamsObject()
|
|
|
|
if op == nil || op.Len() == 0 {
|
|
|
|
return ErrObjectRequired
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|
2019-02-15 05:51:57 +09:00
|
|
|
if w.Block != nil {
|
|
|
|
return w.Block(c, a)
|
|
|
|
}
|
|
|
|
return nil
|
2019-02-13 06:43:01 +09:00
|
|
|
}
|