private void TextBoxSubscriptionUrl_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Enter)
            {
                return;
            }

            try
            {
                Uri url;
                if (Uri.TryCreate(TextBoxSubscriptionUrl.Text, UriKind.Absolute, out url))
                {
                    _currSubscription             = WebSubClient.Subscribe(url);
                    _currSubscription.OnClosed   += _currSubscription_OnClosed;
                    _currSubscription.OnReceived += _currSubscription_OnReceived;
                    _currSubscription.Start();
                    AddText("Subscribed");
                }
                else
                {
                    throw new Exception("Invalid URL");
                }
            }
            catch
            {
                MessageBox.Show("Please enter a valid URL");
            }
        }
Esempio n. 2
0
        public override async Task Go()
        {
            var targetActor = await _entityStore.GetEntity(Data.ToFollowID, true);

            var actor = await _entityStore.GetEntity(Data.ActorID, true);

            if (targetActor == null || actor == null)
            {
                return;
            }

            var hubUrl   = (string)targetActor.Data["_:hubUrl"].FirstOrDefault()?.Primitive;
            var topicUrl = (string)targetActor.Data["_:atomRetrieveUrl"].FirstOrDefault()?.Primitive;

            if (hubUrl == null || topicUrl == null)
            {
                return;
            }

            var clientObject = await _context.WebSubClients.FirstOrDefaultAsync(a => a.ForUserId == Data.ActorID && a.TargetUserId == Data.ToFollowID);

            var hc = new HttpClient();

            if (Data.Unsubscribe)
            {
                if (clientObject.Expiry > DateTime.Now.AddMinutes(1))
                {
                    // send request
                    var content = new FormUrlEncodedContent(new Dictionary <string, string>
                    {
                        ["hub.mode"]   = "unsubscribe",
                        ["hub.topic"]  = topicUrl,
                        ["hub.secret"] = clientObject.Secret
                    });

                    await hc.PostAsync(hubUrl, content);
                }

                _context.WebSubClients.Remove(clientObject);
                return;
            }

            if (clientObject == null)
            {
                clientObject = new WebSubClient
                {
                    ForUserId    = Data.ActorID,
                    TargetUserId = Data.ToFollowID,
                    Secret       = Guid.NewGuid().ToString()
                };

                _context.WebSubClients.Add(clientObject);
            }

            clientObject.Topic = topicUrl;
            await _context.SaveChangesAsync();

            var subscribeContent = new FormUrlEncodedContent(new Dictionary <string, string>
            {
                ["hub.mode"]          = "subscribe",
                ["hub.topic"]         = topicUrl,
                ["hub.secret"]        = clientObject.Secret,
                ["hub.callback"]      = ((string)actor.Data["inbox"].First().Primitive) + ".atom",
                ["hub.lease_seconds"] = TimeSpan.FromDays(1).TotalSeconds.ToString()
            });

            var response = await hc.PostAsync(hubUrl, subscribeContent);

            var respText = await response.Content.ReadAsStringAsync();

            if (((int)response.StatusCode) / 100 != 2)
            {
                response.EnsureSuccessStatusCode();
            }
        }
Esempio n. 3
0
        public override async Task Go()
        {
            var targetActor = await _entityStore.GetDBEntity(Data.ToFollowID);

            var actor = await _entityStore.GetEntity(Data.ActorID, true);

            if (targetActor == null || actor == null)
            {
                return;
            }

            var hubUrl   = (string)targetActor.Data["_:hubUrl"].FirstOrDefault()?.Primitive;
            var topicUrl = (string)targetActor.Data["atomUri"].FirstOrDefault()?.Primitive;

            if (hubUrl == null || topicUrl == null)
            {
                return;
            }

            var clientObject = await _connection.QuerySingleOrDefaultAsync <WebSubClient>("select * from WebSubClients where ForUserId = @ForUserId and TargetUserId = @TargetUserId", new { ForUserID = actor.DbId, TargetUserId = targetActor.DbId });

            var hc = new HttpClient();

            if (Data.Unsubscribe)
            {
                if (clientObject.Expiry > DateTime.Now.AddMinutes(1))
                {
                    // send request
                    var content = new FormUrlEncodedContent(new Dictionary <string, string>
                    {
                        ["hub.mode"]   = "unsubscribe",
                        ["hub.topic"]  = topicUrl,
                        ["hub.secret"] = clientObject.Secret
                    });

                    await hc.PostAsync(hubUrl, content);
                }

                await _connection.ExecuteAsync("delete from \"WebSubClients\" where \"WebSubClientId\" = @Id", new { Id = clientObject.WebSubClientId });

                return;
            }

            if (clientObject == null)
            {
                clientObject = new WebSubClient
                {
                    ForUserId    = actor.DbId,
                    TargetUserId = targetActor.DbId,
                    Secret       = Guid.NewGuid().ToString(),
                    Topic        = clientObject.Topic
                };

                await _connection.ExecuteAsync("insert into \"WebSubClients\" (\"ForUserId\", \"TargetUserId\", \"Secret\", \"Topic\") VALUES (@ForUserId, @TargetUserId, @Secret, @Topic", clientObject);
            }

            var subscribeContent = new FormUrlEncodedContent(new Dictionary <string, string>
            {
                ["hub.mode"]          = "subscribe",
                ["hub.topic"]         = topicUrl,
                ["hub.secret"]        = clientObject.Secret,
                ["hub.callback"]      = (actor.Data["inbox"].First().Id) + ".atom",
                ["hub.lease_seconds"] = TimeSpan.FromDays(1).TotalSeconds.ToString()
            });

            var response = await hc.PostAsync(hubUrl, subscribeContent);

            var respText = await response.Content.ReadAsStringAsync();

            if (((int)response.StatusCode) / 100 != 2)
            {
                response.EnsureSuccessStatusCode();
            }
        }