Example #1
0
        public async Task <bool> SendRejectFollowAsync(ActivityFollow activity, string followerHost)
        {
            var acceptFollow = new ActivityRejectFollow()
            {
                context  = "https://www.w3.org/ns/activitystreams",
                id       = $"{activity.apObject}#rejects/follows/{Guid.NewGuid()}",
                type     = "Reject",
                actor    = activity.apObject,
                apObject = new ActivityFollow()
                {
                    id       = activity.id,
                    type     = activity.type,
                    actor    = activity.actor,
                    apObject = activity.apObject
                }
            };
            var result = await _activityPubService.PostDataAsync(acceptFollow, followerHost, activity.apObject);

            return(result == HttpStatusCode.Accepted ||
                   result == HttpStatusCode.OK); //TODO: revamp this for better error handling
        }
        public async Task <bool> FollowRequestedAsync(string signature, string method, string path, string queryString, Dictionary <string, string> requestHeaders, ActivityFollow activity, string body)
        {
            // Validate
            var sigValidation = await ValidateSignature(activity.actor, signature, method, path, queryString, requestHeaders, body);

            if (!sigValidation.SignatureIsValidated)
            {
                return(false);
            }

            // Save Follow in DB
            var followerUserName    = sigValidation.User.preferredUsername.ToLowerInvariant();
            var followerHost        = sigValidation.User.url.Replace("https://", string.Empty).Split('/').First();
            var followerInbox       = sigValidation.User.inbox;
            var followerSharedInbox = sigValidation.User?.endpoints?.sharedInbox;
            var twitterUser         = activity.apObject.Split('/').Last().Replace("@", string.Empty);

            // Make sure to only keep routes
            followerInbox       = OnlyKeepRoute(followerInbox, followerHost);
            followerSharedInbox = OnlyKeepRoute(followerSharedInbox, followerHost);

            var user = _twitterUserService.GetUser(twitterUser);

            if (!user.Protected)
            {
                // Execute
                await _processFollowUser.ExecuteAsync(followerUserName, followerHost, twitterUser, followerInbox, followerSharedInbox);

                // Send Accept Activity
                var acceptFollow = new ActivityAcceptFollow()
                {
                    context  = "https://www.w3.org/ns/activitystreams",
                    id       = $"{activity.apObject}#accepts/follows/{Guid.NewGuid()}",
                    type     = "Accept",
                    actor    = activity.apObject,
                    apObject = new ActivityFollow()
                    {
                        id       = activity.id,
                        type     = activity.type,
                        actor    = activity.actor,
                        apObject = activity.apObject
                    }
                };
                var result = await _activityPubService.PostDataAsync(acceptFollow, followerHost, activity.apObject);

                return(result == HttpStatusCode.Accepted || result == HttpStatusCode.OK); //TODO: revamp this for better error handling
            }
            else
            {
                // Send Reject Activity
                var acceptFollow = new ActivityRejectFollow()
                {
                    context  = "https://www.w3.org/ns/activitystreams",
                    id       = $"{activity.apObject}#rejects/follows/{Guid.NewGuid()}",
                    type     = "Reject",
                    actor    = activity.apObject,
                    apObject = new ActivityFollow()
                    {
                        id       = activity.id,
                        type     = activity.type,
                        actor    = activity.actor,
                        apObject = activity.apObject
                    }
                };
                var result = await _activityPubService.PostDataAsync(acceptFollow, followerHost, activity.apObject);

                return(result == HttpStatusCode.Accepted || result == HttpStatusCode.OK); //TODO: revamp this for better error handling
            }
        }