Example #1
0
 private async Task <TwitterStatus> SendInternal(
     [NotNull] TwitterAccount account, [NotNull] RequestBase <TwitterStatus> request)
 {
     if (account == null)
     {
         throw new ArgumentNullException("account");
     }
     if (request == null)
     {
         throw new ArgumentNullException("request");
     }
     return(await Task.Run(async() =>
     {
         var status = await RequestQueue.EnqueueAsync(account, request);
         StatusInbox.Enqueue(status);
         return status;
     }));
 }
Example #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, _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);
        }