public async Task GetRelatedResult(bool read, RelatedPostsTabModel tab)
{
var targetPost = tab.TargetPost;
var relPosts = new Dictionary<Int64, PostClass>();
if (targetPost.TextFromApi.Contains("@") && targetPost.InReplyToStatusId == null)
{
//検索結果対応
var p = TabInformations.GetInstance()[targetPost.StatusId];
if (p != null && p.InReplyToStatusId != null)
{
targetPost = p;
}
else
{
p = await this.GetStatusApi(read, targetPost.StatusId)
.ConfigureAwait(false);
targetPost = p;
}
}
relPosts.Add(targetPost.StatusId, targetPost);
Exception lastException = null;
// in_reply_to_status_id を使用してリプライチェインを辿る
var nextPost = FindTopOfReplyChain(relPosts, targetPost.StatusId);
var loopCount = 1;
while (nextPost.InReplyToStatusId != null && loopCount++ <= 20)
{
var inReplyToId = nextPost.InReplyToStatusId.Value;
var inReplyToPost = TabInformations.GetInstance()[inReplyToId];
if (inReplyToPost == null)
{
try
{
inReplyToPost = await this.GetStatusApi(read, inReplyToId)
.ConfigureAwait(false);
}
catch (WebApiException ex)
{
lastException = ex;
break;
}
}
relPosts.Add(inReplyToPost.StatusId, inReplyToPost);
nextPost = FindTopOfReplyChain(relPosts, nextPost.StatusId);
}
//MRTとかに対応のためツイート内にあるツイートを指すURLを取り込む
var text = targetPost.Text;
var ma = Twitter.StatusUrlRegex.Matches(text).Cast<Match>()
.Concat(Twitter.ThirdPartyStatusUrlRegex.Matches(text).Cast<Match>());
foreach (var _match in ma)
{
Int64 _statusId;
if (Int64.TryParse(_match.Groups["StatusId"].Value, out _statusId))
{
if (relPosts.ContainsKey(_statusId))
continue;
var p = TabInformations.GetInstance()[_statusId];
if (p == null)
{
try
{
p = await this.GetStatusApi(read, _statusId)
.ConfigureAwait(false);
}
catch (WebApiException ex)
{
lastException = ex;
break;
}
}
if (p != null)
relPosts.Add(p.StatusId, p);
}
}
relPosts.Values.ToList().ForEach(p =>
{
if (p.IsMe && !read && this._readOwnPost)
p.IsRead = true;
else
p.IsRead = read;
tab.AddPostQueue(p);
});
if (lastException != null)
throw new WebApiException(lastException.Message, lastException);
}