Beispiel #1
0
        public async Task <PostResult> SendAsync()
        {
            var existedTags = TwitterRegexPatterns.ValidHashtag.Matches(Text)
                              .OfType <Match>()
                              .Select(_ => _.Groups[1].Value)
                              .Distinct()
                              .ToArray();
            var binds = !IsTagBindEnabled
                ? String.Empty
                : _boundTags.Guard().Except(existedTags)
                        .Distinct()
                        .Select(t => " #" + t)
                        .JoinString(String.Empty);
            RequestBase <TwitterStatus> request;

            if (IsDirectMessage)
            {
                request = new MessagePostingRequest(MessageRecipient, Text);
            }
            else
            {
                request = new TweetPostingRequest(Text + binds, InReplyTo,
                                                  AttachedGeoLocation, _attachedImages);
            }

            var posts = _accounts.Guard()
                        .Select(a => Tuple.Create(a, SendInternal(a, request)))
                        .ToArray();

            var amendTargets   = new Dictionary <TwitterAccount, TwitterStatus>();
            var failedAccounts = new List <TwitterAccount>();
            var exceptions     = new List <Exception>();

            foreach (var item in posts)
            {
                var account = item.Item1;
                try
                {
                    var result = await item.Item2.ConfigureAwait(false);

                    amendTargets.Add(account, result);
                }
                catch (Exception ex)
                {
                    failedAccounts.Add(account);
                    exceptions.Add(ex);
                }
            }
            InputData successData = null, failedData = null;

            if (amendTargets.Count > 0)
            {
                successData = Clone();
                successData.AmendTargetTweets = amendTargets.ToArray();
                successData.Accounts          = amendTargets.Select(p => p.Key);
            }

            if (failedAccounts.Count > 0)
            {
                failedData          = Clone();
                failedData.Accounts = failedAccounts.ToArray();
            }

            var pr = new PostResult(successData, failedData, exceptions.ToArray());


            if (IsAmend)
            {
                var amends = AmendTargetTweets
                             .Select(pair => RequestQueue.EnqueueAsync(pair.Key, new DeletionRequest(pair.Value)))
                             .OfType <Task>().ToArray();
                Task.WaitAll(amends);
            }
            return(pr);
        }
Beispiel #2
0
        public async Task<PostResult> SendAsync()
        {
            var existedTags = TwitterRegexPatterns.ValidHashtag.Matches(Text)
                                                  .OfType<Match>()
                                                  .Select(_ => _.Groups[1].Value)
                                                  .Distinct()
                                                  .ToArray();
            var binds = !IsTagBindEnabled
                ? String.Empty
                : _boundTags.Guard().Except(existedTags)
                            .Distinct()
                            .Select(t => " #" + t)
                            .JoinString(String.Empty);
            RequestBase<TwitterStatus> request;
            if (IsDirectMessage)
            {
                request = new MessagePostingRequest(MessageRecipient, Text);
            }
            else
            {
                request = new TweetPostingRequest(Text + binds, InReplyTo,
                    AttachedGeoLocation, _attachedImage);
            }

            var posts = _accounts.Guard()
                                 .Select(a => Tuple.Create(a, SendInternal(a, request)))
                                 .ToArray();
            var tasks = posts.Select(p => p.Item2).OfType<Task>().ToArray();
            // wait for completion
            Task.WaitAll(tasks);

            var amendTargets = new Dictionary<TwitterAccount, TwitterStatus>();
            var failedAccounts = new List<TwitterAccount>();
            var exceptions = new List<Exception>();

            foreach (var item in posts)
            {
                var account = item.Item1;
                try
                {
                    var result = await item.Item2;
                    amendTargets.Add(account, result);
                }
                catch (Exception ex)
                {
                    failedAccounts.Add(account);
                    exceptions.Add(ex);
                }
            }
            InputData successData = null, failedData = null;

            if (amendTargets.Count > 0)
            {
                successData = this.Clone();
                successData.AmendTargetTweets = amendTargets.ToArray();
                successData.Accounts = amendTargets.Select(p => p.Key);
            }

            if (failedAccounts.Count > 0)
            {
                failedData = this.Clone();
                failedData.Accounts = failedAccounts.ToArray();
            }

            var pr = new PostResult(successData, failedData, exceptions.ToArray());


            if (IsAmend)
            {
                var amends = AmendTargetTweets
                    .Select(pair => RequestQueue.EnqueueAsync(pair.Key, new DeletionRequest(pair.Value)))
                    .OfType<Task>().ToArray();
                Task.WaitAll(amends);
            }
            return pr;
        }