Example #1
0
        public override async Task <bool> Handle()
        {
            if (MainObject.Type != "Block")
            {
                return(true);
            }
            var activityData = MainObject.Data;

            var toBlock = (string)activityData["object"].First().Primitive;
            var entity  = await EntityStore.GetEntity(toBlock, true);

            if (entity == null)
            {
                throw new InvalidOperationException("Cannot block a non-existant object");
            }

            var blockscollection = await EntityStore.GetEntity((string)Actor.Data["blocks"].First().Primitive, false);

            await _collection.AddToCollection(blockscollection, MainObject);

            var blockedcollection = await EntityStore.GetEntity((string)blockscollection.Data["_blocked"].First().Primitive, false);

            await _collection.AddToCollection(blockedcollection, entity);

            return(true);
        }
Example #2
0
        public override async Task <bool> Handle()
        {
            if (MainObject.Type != "https://www.w3.org/ns/activitystreams#Block")
            {
                return(true);
            }
            var activityData = MainObject.Data;

            var toBlock = activityData["object"].First().Id;
            var entity  = await EntityStore.GetEntity(toBlock, true);

            if (entity == null)
            {
                throw new InvalidOperationException("Cannot block a non-existant object");
            }

            var blockscollection = await EntityStore.GetEntity(Actor.Data["blocks"].First().Id, false);

            await _collection.AddToCollection(blockscollection, MainObject);

            var blockedcollection = await EntityStore.GetEntity(blockscollection.Data["blocked"].First().Id, false);

            await _collection.AddToCollection(blockedcollection, entity);

            return(true);
        }
        public override async Task <bool> Handle()
        {
            if (MainObject.Type != "https://www.w3.org/ns/activitystreams#Accept" &&
                MainObject.Type != "https://www.w3.org/ns/activitystreams#Reject")
            {
                return(true);
            }

            var subObject = await EntityStore.GetEntity(MainObject.Data["object"].Single().Id, true);

            var requestedUser = await EntityStore.GetEntity(subObject.Data["actor"].First().Id, true);

            if (subObject.Type != "https://www.w3.org/ns/activitystreams#Follow")
            {
                return(true);
            }

            if (subObject.Data["object"].Single().Id != Actor.Id)
            {
                return(true);
            }

            bool isAccept  = MainObject.Type == "https://www.w3.org/ns/activitystreams#Accept";
            var  followers = await EntityStore.GetEntity(Actor.Data["followers"].Single().Id, false);

            if (isAccept && !await _collection.Contains(followers, requestedUser.Id))
            {
                await _collection.AddToCollection(followers, requestedUser);
            }
            if (!isAccept && await _collection.Contains(followers, requestedUser.Id))
            {
                await _collection.RemoveFromCollection(followers, requestedUser);
            }
            return(true);
        }
Example #4
0
        public override async Task <bool> Handle()
        {
            if (MainObject.Type != "https://www.w3.org/ns/activitystreams#Like")
            {
                return(true);
            }

            var    userData           = Actor.Data;
            string targetCollectionId = null;

            targetCollectionId = userData["liked"].SingleOrDefault()?.Id;

            if (targetCollectionId == null)
            {
                return(true);
            }

            var targetCollection = await EntityStore.GetEntity(targetCollectionId, false);

            var objectEntity = await EntityStore.GetEntity(MainObject.Data["object"].Single().Id, true);

            if (objectEntity == null)
            {
                throw new InvalidOperationException($"Cannot {MainObject.Type.ToLower()} a non-existant object!");
            }

            await _collection.AddToCollection(targetCollection, objectEntity);

            return(true);
        }
Example #5
0
        public override async Task <bool> Handle()
        {
            if (MainObject.Type != "Like" && MainObject.Type != "Follow")
            {
                return(true);
            }

            var    userData           = Actor.Data;
            string targetCollectionId = null;

            if (MainObject.Type == "Like")
            {
                targetCollectionId = (string)userData["likes"].Single().Primitive;
            }
            else if (MainObject.Type == "Follow")
            {
                targetCollectionId = (string)userData["following"].Single().Primitive;
            }

            var targetCollection = await EntityStore.GetEntity(targetCollectionId, false);

            var objectEntity = await EntityStore.GetEntity((string)MainObject.Data["object"].Single().Primitive, true);

            if (objectEntity == null)
            {
                throw new InvalidOperationException($"Cannot {MainObject.Type.ToLower()} a non-existant object!");
            }

            await _collection.AddToCollection(targetCollection, objectEntity);

            return(true);
        }
Example #6
0
        public override async Task <bool> Handle()
        {
            if (MainObject.Type != "Accept")
            {
                return(true);
            }
            var followObject = await EntityStore.GetEntity((string)MainObject.Data["object"].Single().Primitive, false);

            if (followObject.Type != "Follow")
            {
                return(true);
            }
            if ((string)followObject.Data["object"].First().Primitive != (string)MainObject.Data["actor"].First().Primitive)
            {
                throw new InvalidOperationException("I won't let you do that, Starfox!");
            }
            var followUser = (string)followObject.Data["object"].First().Primitive;

            if (Actor.Id != (string)followObject.Data["object"].First().Primitive)
            {
                return(true);                                                                   // doesn't involve us, so meh
            }
            if (!followObject.IsOwner)
            {
                throw new InvalidOperationException("Follow isn't made on this server?");
            }

            var relevant = await _relevantEntities.FindRelevantObject(followUser, "Reject", followObject.Id);

            if (relevant != null)
            {
                throw new InvalidOperationException("Follow has already been Rejected before!");
            }

            if (MainObject.Type == "Accept")
            {
                relevant = await _relevantEntities.FindRelevantObject(followUser, "Accept", followObject.Id);

                if (relevant != null)
                {
                    throw new InvalidOperationException("Follow has already been Accepted before!");
                }
            }

            var following = await EntityStore.GetEntity((string)Actor.Data["following"].Single().Primitive, false);

            var user = await EntityStore.GetEntity((string)MainObject.Data["actor"].Single().Primitive, true);

            if (MainObject.Type == "Accept" && !await _collection.Contains(following.Id, user.Id))
            {
                await _collection.AddToCollection(following, user);
            }
            else if (MainObject.Type == "Reject")
            {
                await _collection.RemoveFromCollection(following, user);
            }

            return(true);
        }
        public override async Task <bool> Handle()
        {
            if (MainObject.Type != "Follow" && MainObject.Type != "Like" && MainObject.Type != "Announce")
            {
                return(true);
            }

            var toFollowOrLike = await EntityStore.GetEntity((string)MainObject.Data["object"].Single().Primitive, false);

            if (toFollowOrLike == null || !toFollowOrLike.IsOwner)
            {
                return(true);                                                   // not going to update side effects now.
            }
            // sent to not the owner, so not updating!
            if ((MainObject.Type == "Follow" && Actor.Id != toFollowOrLike.Id) || (MainObject.Type != "Follow" && (string)toFollowOrLike.Data["attributedTo"].Single().Primitive != Actor.Id))
            {
                return(true);
            }

            string collectionId = null, objectToAdd = null;

            switch (MainObject.Type)
            {
            case "Follow":
                collectionId = (string)toFollowOrLike.Data["followers"].SingleOrDefault()?.Primitive;
                objectToAdd  = (string)MainObject.Data["actor"].Single().Primitive;
                break;

            case "Like":
                collectionId = (string)toFollowOrLike.Data["likes"].SingleOrDefault()?.Primitive;
                objectToAdd  = MainObject.Id;
                break;

            case "Announce":
                collectionId = (string)toFollowOrLike.Data["shares"].SingleOrDefault()?.Primitive;
                objectToAdd  = MainObject.Id;
                break;
            }

            if (collectionId == null)
            {
                return(true);                      // no way to store followers/likse
            }
            var collection = await EntityStore.GetEntity(collectionId, false);

            var entityToAdd = await EntityStore.GetEntity(objectToAdd, true);

            if (entityToAdd == null)
            {
                throw new InvalidOperationException("Can't follow or like a null object!");
            }

            await _collection.AddToCollection(collection, entityToAdd);

            return(true);
        }
Example #8
0
        public override async Task <bool> Handle()
        {
            var addedTo = await _collection.AddToCollection(TargetBox, MainObject);

            if (MainObject.Type == "Block")
            {
                return(true);
            }

            await _deliveryService.QueueDeliveryForEntity(MainObject, addedTo.CollectionItemId, TargetBox.Type == "_inbox"?Actor.Id : null);

            return(true);
        }
Example #9
0
        public override async Task <bool> Handle()
        {
            if (MainObject.Type != "https://www.w3.org/ns/activitystreams#Remove" && MainObject.Type != "https://www.w3.org/ns/activitystreams#Add")
            {
                return(true);
            }
            var activityData = MainObject.Data;

            var targetEntity = await EntityStore.GetEntity(activityData["target"].Single().Id, false);

            if (targetEntity == null)
            {
                throw new InvalidOperationException("Cannot add or remove from a non-existant collection!");
            }

            if (!targetEntity.IsOwner)
            {
                return(true);
            }

            if (targetEntity.Type != "https://www.w3.org/ns/activitystreams#Collection" && targetEntity.Type != "https://www.w3.org/ns/activitystreams#OrderedCollection")
            {
                throw new InvalidOperationException("Cannot add or remove from something that isn't a collection!");
            }

            if (!targetEntity.Data["attributedTo"].Any(a => a.Id == Actor.Id))
            {
                throw new InvalidOperationException("You can't add/remove to this collection!");
            }

            var objectId = activityData["object"].Single().Id;

            if (MainObject.Type == "https://www.w3.org/ns/activitystreams#Add")
            {
                var objectEntity = await EntityStore.GetEntity(objectId, true);

                if (objectEntity == null)
                {
                    throw new InvalidOperationException("Cannot add a non-existant object!");
                }

                await _collection.AddToCollection(targetEntity, objectEntity);
            }
            else if (MainObject.Type == "https://www.w3.org/ns/activitystreams#Remove")
            {
                await _collection.RemoveFromCollection(targetEntity, objectId);
            }

            return(true);
        }
Example #10
0
        public override async Task <bool> Handle()
        {
            if (!await _collection.Contains(TargetBox, MainObject.Id))
            {
                var addedTo = await _collection.AddToCollection(TargetBox, MainObject);

                if (MainObject.Type == "https://www.w3.org/ns/activitystreams#Block")
                {
                    return(true);
                }
                await _deliveryService.QueueDeliveryForEntity(MainObject, addedTo.CollectionItemId, TargetBox.Type == "_inbox"?Actor.Id : null);
            }

            return(true);
        }
Example #11
0
        public override async Task <bool> Handle()
        {
            if (MainObject.Type != "Remove" && MainObject.Type != "Add")
            {
                return(true);
            }
            var activityData = MainObject.Data;

            var targetEntity = await EntityStore.GetEntity((string)activityData["target"].Single().Primitive, false);

            if (targetEntity == null)
            {
                throw new InvalidOperationException("Cannot add or remove from a non-existant collection!");
            }

            if (!targetEntity.IsOwner)
            {
                throw new InvalidOperationException("Cannot add or remove from a collection I'm not owner of!");
            }

            if (targetEntity.Type != "Collection" && targetEntity.Type != "OrderedCollection")
            {
                throw new InvalidOperationException("Cannot add or remove from something that isn't a collection!");
            }

            // XXX todo: add authorization on here

            var objectId = (string)activityData["object"].Single().Primitive;

            if (MainObject.Type == "Add")
            {
                var objectEntity = await EntityStore.GetEntity(objectId, true);

                if (objectEntity == null)
                {
                    throw new InvalidOperationException("Cannot add a non-existant object!");
                }

                await _collection.AddToCollection(targetEntity, objectEntity);
            }
            else if (MainObject.Type == "Remove")
            {
                await _collection.RemoveFromCollection(targetEntity, objectId);
            }

            return(true);
        }
Example #12
0
        public override async Task <bool> Handle()
        {
            if (MainObject.Type != "Accept" && MainObject.Type != "Reject")
            {
                return(true);
            }

            var subObject = await EntityStore.GetEntity((string)MainObject.Data["object"].Single().Primitive, true);

            var requestedUser = await EntityStore.GetEntity((string)subObject.Data["actor"].First().Primitive, true);

            if (subObject.Type != "Follow")
            {
                return(true);
            }

            if ((string)subObject.Data["object"].Single().Primitive != Actor.Id)
            {
                throw new InvalidOperationException("Cannot Accept or Reject a Follow from another actor!");
            }

            if (MainObject.Type != "Like" && MainObject.Type != "Follow")
            {
                return(true);
            }
            var audience = DeliveryService.GetAudienceIds(MainObject.Data);

            if (!audience.Contains(requestedUser.Id))
            {
                throw new InvalidOperationException("Accepts/Rejects of Follows should be sent to the actor of the follower!");
            }

            bool isAccept  = MainObject.Type == "Accept";
            var  followers = await EntityStore.GetEntity((string)Actor.Data["followers"].Single().Primitive, false);

            if (isAccept && !await _collection.Contains(followers.Id, requestedUser.Id))
            {
                await _collection.AddToCollection(followers, requestedUser);
            }
            if (!isAccept && await _collection.Contains(followers.Id, requestedUser.Id))
            {
                await _collection.RemoveFromCollection(followers, requestedUser);
            }
            return(true);
        }
Example #13
0
        public override async Task <bool> Handle()
        {
            if (MainObject.Type != "https://www.w3.org/ns/activitystreams#Create")
            {
                return(true);
            }
            var obj = await EntityStore.GetEntity(MainObject.Data["object"].Single().Id, false);

            if (obj == null)
            {
                return(true);
            }

            if (!obj.Data["inReplyTo"].Any())
            {
                return(true);
            }
            var inReply = await EntityStore.GetEntity(obj.Data["inReplyTo"].Single().Id, false);

            if (inReply == null || !inReply.IsOwner)
            {
                return(true);
            }
            if (inReply.Data["attributedTo"].Single().Id != Actor.Id)
            {
                return(true);
            }

            var collectionId = inReply.Data["replies"].SingleOrDefault()?.Id;

            if (collectionId == null)
            {
                return(true);                      // no way to store replies
            }
            var collection = await EntityStore.GetEntity(collectionId, false);

            await _collection.AddToCollection(collection, obj);

            return(true);
        }
Example #14
0
        public override async Task <bool> Handle()
        {
            if (MainObject.Type != "https://www.w3.org/ns/activitystreams#Accept")
            {
                return(true);
            }
            var followObject = await EntityStore.GetEntity(MainObject.Data["object"].Single().Id, false);

            if (followObject == null || !followObject.IsOwner)
            {
                followObject = (await _relevantEntities.FindRelevantObject(Actor.Id, "https://www.w3.org/ns/activitystreams#Follow", MainObject.Data["actor"].First().Id)).FirstOrDefault();
                if (followObject != null)
                {
                    MainObject.Data.Replace("object", ASTerm.MakeId(followObject.Id));
                    await EntityStore.StoreEntity(MainObject);
                }
            }

            if (followObject == null || followObject.Type != "https://www.w3.org/ns/activitystreams#Follow")
            {
                return(true);
            }

            if (followObject.Data["object"].First().Id != MainObject.Data["actor"].First().Id)
            {
                throw new InvalidOperationException("I won't let you do that, Starfox!");
            }
            var followUser = followObject.Data["object"].First().Id;

            if (Actor.Id != followObject.Data["actor"].First().Id)
            {
                return(true);                                                   // doesn't involve us, so meh
            }
            if (!followObject.IsOwner)
            {
                throw new InvalidOperationException("Follow isn't made on this server?");
            }

            var relevant = await _relevantEntities.FindRelevantObject(followUser, "Reject", followObject.Id);

            if (relevant.Count != 0)
            {
                throw new InvalidOperationException("Follow has already been Rejected before!");
            }

            if (MainObject.Type == "https://www.w3.org/ns/activitystreams#Accept")
            {
                relevant = await _relevantEntities.FindRelevantObject(followUser, "Accept", followObject.Id);

                if (relevant.Count > 0)
                {
                    throw new InvalidOperationException("Follow has already been Accepted before!");
                }
            }

            var following = await EntityStore.GetEntity(Actor.Data["following"].Single().Id, false);

            var user = await EntityStore.GetEntity(MainObject.Data["actor"].Single().Id, true);

            if (MainObject.Type == "https://www.w3.org/ns/activitystreams#Accept" && !await _collection.Contains(following, user.Id))
            {
                await _collection.AddToCollection(following, user);
            }
            else if (MainObject.Type == "https://www.w3.org/ns/activitystreams#Reject")
            {
                await _collection.RemoveFromCollection(following, user);
            }

            return(true);
        }
Example #15
0
        public override async Task <bool> Handle()
        {
            if (MainObject.Type == "Follow")
            {
                if (Actor.Data["_:locked"].Any(a => !(bool)a.Primitive) || Actor.Data["locked"].Any(a => !(bool)a.Primitive))
                {
                    var accept = new ASObject();
                    accept.Replace("type", new ASTerm("Accept"));
                    accept.Replace("actor", new ASTerm(Actor.Id));
                    accept.Replace("object", new ASTerm(MainObject.Id));

                    var claims  = new ClaimsPrincipal();
                    var handler = ActivatorUtilities.CreateInstance <GetEntityMiddleware.GetEntityHandler>(_serviceProvider, claims);
                    var outbox  = await EntityStore.GetEntity((string)Actor.Data["outbox"].First().Primitive, false);

                    await handler.ClientToServer(outbox, accept);
                }

                return(true);
            }

            if (MainObject.Type != "Like" && MainObject.Type != "Announce")
            {
                return(true);
            }

            var toFollowOrLike = await EntityStore.GetEntity((string)MainObject.Data["object"].Single().Primitive, false);

            if (toFollowOrLike == null || !toFollowOrLike.IsOwner)
            {
                return(true);                                                   // not going to update side effects now.
            }
            // sent to not the owner, so not updating!
            if ((string)toFollowOrLike.Data["attributedTo"].Single().Primitive != Actor.Id)
            {
                return(true);
            }

            string collectionId = null, objectToAdd = null;

            switch (MainObject.Type)
            {
            case "Like":
                collectionId = (string)toFollowOrLike.Data["likes"].SingleOrDefault()?.Primitive;
                objectToAdd  = MainObject.Id;
                break;

            case "Announce":
                collectionId = (string)toFollowOrLike.Data["shares"].SingleOrDefault()?.Primitive;
                objectToAdd  = MainObject.Id;
                break;
            }

            if (collectionId == null)
            {
                return(true);                      // no way to store followers/likse
            }
            var collection = await EntityStore.GetEntity(collectionId, false);

            var entityToAdd = await EntityStore.GetEntity(objectToAdd, true);

            if (entityToAdd == null)
            {
                throw new InvalidOperationException("Can't like or announce a non-existant object!");
            }

            await _collection.AddToCollection(collection, entityToAdd);

            return(true);
        }
        public override async Task <bool> Handle()
        {
            if (MainObject.Type == "https://www.w3.org/ns/activitystreams#Follow")
            {
                if (Actor.Data["manuallyApprovesFollowers"].Any(a => !(bool)a.Primitive))
                {
                    var accept = new ASObject();
                    accept.Type.Add("https://www.w3.org/ns/activitystreams#Accept");
                    accept.Replace("actor", ASTerm.MakeId(Actor.Id));
                    accept.Replace("object", ASTerm.MakeId(MainObject.Id));
                    accept["to"].AddRange(MainObject.Data["actor"]);

                    var claims = new ClaimsPrincipal();
                    var id     = new ClaimsIdentity();
                    id.AddClaim(new Claim("actor", Actor.Id));
                    claims.AddIdentity(id);

                    var handler = ActivatorUtilities.CreateInstance <GetEntityHandler>(_serviceProvider, claims, EntityStore);
                    var outbox  = await EntityStore.GetEntity(Actor.Data["outbox"].First().Id, false);

                    await handler.ClientToServer(outbox, accept);
                }

                return(true);
            }

            if (MainObject.Type != "https://www.w3.org/ns/activitystreams#Like" && MainObject.Type != "https://www.w3.org/ns/activitystreams#Announce")
            {
                return(true);
            }

            var toFollowOrLike = await EntityStore.GetEntity(MainObject.Data["object"].Single().Id, false);

            if (toFollowOrLike == null)
            {
                await GetEntityTask.Make(MainObject.Data["object"].Single().Id, _connection);

                return(true);
            }

            // sent to not the owner, so not updating!
            if (toFollowOrLike.Data["attributedTo"].Single().Id != Actor.Id)
            {
                return(true);
            }

            string collectionId = null, objectToAdd = null;

            switch (MainObject.Type)
            {
            case "https://www.w3.org/ns/activitystreams#Like":
                collectionId = toFollowOrLike.Data["likes"].SingleOrDefault()?.Id;
                objectToAdd  = MainObject.Id;
                break;

            case "https://www.w3.org/ns/activitystreams#Announce":
                collectionId = toFollowOrLike.Data["shares"].SingleOrDefault()?.Id;
                objectToAdd  = MainObject.Id;
                break;
            }

            if (collectionId == null)
            {
                return(true);                      // no way to store followers/likse
            }
            var collection = await EntityStore.GetEntity(collectionId, false);

            var entityToAdd = await EntityStore.GetEntity(objectToAdd, true);

            if (entityToAdd == null)
            {
                throw new InvalidOperationException("Can't like or announce a non-existant object!");
            }

            await _collection.AddToCollection(collection, entityToAdd);

            return(true);
        }