Example #1
0
        public async Task Invoke(HttpContext context, IServiceProvider serviceProvider, IEntityStore entityStore, DbConnection connection, EntityData entityData)
        {
            if (!context.Request.Query["hub.mode"].Any())
            {
                await _next(context);

                return;
            }
            if (entityData.RewriteRequestScheme)
            {
                context.Request.Scheme = "https";
            }
            var fullpath = $"{context.Request.Scheme}://{context.Request.Host}{context.Request.Path}";

            fullpath = fullpath.Remove(fullpath.Length - 5); // remove .atom

            var box = await entityStore.GetEntity(fullpath, false);

            if (box == null || box.Type != "_inbox")
            {
                goto error;
            }
            var user = await entityStore.GetEntity(box.Data["attributedTo"].First().Id, false);

            if (user == null)
            {
                goto error;
            }

            var mode          = context.Request.Query["hub.mode"].First();
            var topic         = context.Request.Query["hub.topic"].First();
            var challenge     = context.Request.Query["hub.challenge"].First();
            var lease_seconds = context.Request.Query["hub.lease_seconds"].FirstOrDefault();

            var leaseSpan = lease_seconds == null ? null : (TimeSpan?)TimeSpan.FromSeconds(int.Parse(lease_seconds));

            var obj = await connection.QueryFirstOrDefaultAsync <WebSubClient>("select * from \"WebSubClients\" where \"Topic\" = @Topic and \"ForUserId\" = @ForUserId", new { Topic = topic, ForUserId = user.DbId });

            if (obj == null)
            {
                goto error;
            }

            if (mode == "subscribe")
            {
                obj.Expiry = DateTime.Now + leaseSpan.Value;
                await connection.ExecuteAsync("update \"WebSubClients\" set \"Expiry\" = @Expiry where \"WebSubClientId\" = @Id", new { Expiry = obj.Expiry, Id = obj.WebSubClientId });

                await WebSubBackgroundTask.Make(new WebSubBackgroundData { ActorID = user.Id, ToFollowID = obj.TargetUserId }, connection, obj.Expiry);

                context.Response.StatusCode = 200;
                await context.Response.WriteAsync(challenge);
            }

            return;

error:
            context.Response.StatusCode = 404;
            await context.Response.WriteAsync("nah");
        }
Example #2
0
        public override async Task <bool> Handle()
        {
            var activity = MainObject;

            if (MainObject.Type == "Undo")
            {
                var subObject = await EntityStore.GetEntity((string)activity.Data["object"].First().Primitive, false);

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

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

            var target = await EntityStore.GetEntity((string)activity.Data["object"].First().Primitive, true);

            if (target == null)
            {
                return(true);                // can't really fix subscriptions on a thing that doesn't exist
            }
            var hubUrl = (string)target.Data["_:hubUrl"].SingleOrDefault()?.Primitive;

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

            var taskEvent = WebSubBackgroundTask.Make(new WebSubBackgroundData {
                Unsubscribe = MainObject.Type == "Undo", ToFollowID = target.Id, ActorID = (string)MainObject.Data["actor"].Single().Primitive
            });

            _context.EventQueue.Add(taskEvent);
            await _context.SaveChangesAsync();

            return(true);
        }
Example #3
0
        public override async Task <bool> Handle()
        {
            var activity = MainObject;

            if (MainObject.Type == "https://www.w3.org/ns/activitystreams#Undo")
            {
                var subObject = await EntityStore.GetEntity(activity.Data["object"].First().Id, false);

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

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

            var target = await EntityStore.GetEntity(activity.Data["object"].First().Id, true);

            if (target == null)
            {
                return(true);                // can't really fix subscriptions on a thing that doesn't exist
            }
            var hubUrl = (string)target.Data["_:hubUrl"].SingleOrDefault()?.Primitive;

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

            await WebSubBackgroundTask.Make(new WebSubBackgroundData { Unsubscribe = MainObject.Type == "https://www.w3.org/ns/activitystreams#Undo", ToFollowID = target.DbId, ActorID = MainObject.Data["actor"].Single().Id }, _connection);

            return(true);
        }
Example #4
0
        public async Task Invoke(HttpContext context, IServiceProvider serviceProvider, IEntityStore entityStore, APContext db, EntityData entityData)
        {
            if (!context.Request.Query["hub.mode"].Any())
            {
                await _next(context);

                return;
            }
            if (entityData.RewriteRequestScheme)
            {
                context.Request.Scheme = "https";
            }
            var fullpath = $"{context.Request.Scheme}://{context.Request.Host}{context.Request.Path}";

            fullpath = fullpath.Remove(fullpath.Length - 5); // remove .atom

            var box = await entityStore.GetEntity(fullpath, false);

            if (box == null || box.Type != "_inbox")
            {
                goto error;
            }
            var user = await entityStore.GetEntity((string)box.Data["attributedTo"].First().Primitive, false);

            if (user == null)
            {
                goto error;
            }

            var mode          = context.Request.Query["hub.mode"].First();
            var topic         = context.Request.Query["hub.topic"].First();
            var challenge     = context.Request.Query["hub.challenge"].First();
            var lease_seconds = context.Request.Query["hub.lease_seconds"].FirstOrDefault();

            var leaseSpan = lease_seconds == null ? null : (TimeSpan?)TimeSpan.FromSeconds(int.Parse(lease_seconds));

            var obj = await db.WebSubClients.FirstOrDefaultAsync(a => a.Topic == topic && a.ForUserId == user.Id);

            if (obj == null)
            {
                goto error;
            }

            if (mode == "subscribe")
            {
                obj.Expiry = DateTime.Now + leaseSpan.Value;
                var task = WebSubBackgroundTask.Make(new WebSubBackgroundData {
                    ActorID = user.Id, ToFollowID = obj.TargetUserId
                });
                task.NextAttempt = obj.Expiry;
                db.EventQueue.Add(task);
                await db.SaveChangesAsync();

                context.Response.StatusCode = 200;
                await context.Response.WriteAsync(challenge);
            }

            return;

error:
            context.Response.StatusCode = 404;
            await context.Response.WriteAsync("nah");
        }