Exemplo n.º 1
0
        private void handleGetComments(HttpContext ctx, Notification mirror, String requestPayload)
        {
            // make new timeline item
            TimelineItem timelineItem = new TimelineItem();

            // access the credential state
            IAuthorizationState credentialState = DAL.RetrieveCredentialsByRequestIdAndUserToken(mirror.UserToken, mirror.VerifyToken);

            // get auth code from the verify token
            String authCode = DAL.RetrieveAuthCodeByRequestId(mirror.VerifyToken);

            // we can reuse the access token
            IAuthenticator credentials = Authorization.GetAuthenticatorFromState(credentialState);

            // get active blogger link
            BlogLink bl = DAL.GetActiveBlogLinkByUserId(mirror.UserToken);

            try
            {
                // set up the view manager
                ViewManager bloggerViewManager = new ViewManager();
                bloggerViewManager.AddView("bloggerPostCover", "~/views/bloggerPostingBundleCover.html");
                bloggerViewManager.AddView("bloggerComment", "~/views/bloggerComment.html");

                // take the top 3 recent posts
                PostList postList = Blogger.getPosts(Blogger.BuildService(credentials), bl);

                // list of html pages
                StringBuilder bundleCover = new StringBuilder();

                // html comment pages
                List <HtmlComment> htmlPages = new List <HtmlComment>();

                int totalCommentCount = 0;
                // build bundle cover - limit to constant recent_post_count init above
                for (int postIndex = 0; postIndex < RECENT_POST_COUNT; postIndex++)
                {
                    // if the index is more than the amount of posts, just break out.
                    if (postIndex >= postList.Items.Count)
                    {
                        break;
                    }

                    // get post
                    Post post = postList.Items[postIndex];
                    // get comments for post
                    CommentList commentList = Blogger.getComments(Blogger.BuildService(credentials), bl, post.Id);

                    // get the count
                    int commentCount = (commentList.Items == null) ? 0 : commentList.Items.Count;

                    // build individual comment view
                    if (commentCount > 0)
                    {
                        // go through each comment and make some pages
                        foreach (Comment comment in commentList.Items)
                        {
                            // new argument list
                            bloggerViewManager.Arguments = new Dictionary <String, String>()
                            {
                                { "PROFILE_URL", comment.Author.Image.Url },
                                { "POST_TITLE", post.Title },
                                { "COMMENT_CONTENT", comment.Content },
                                { "COMMENT_AUTHOR", comment.Author.DisplayName },
                                { "COMMENT_TIME", comment.Published },
                                { "BLOG_NAME", bl.blogName }
                            };

                            // render view and add it to the html page list
                            htmlPages.Add(new HtmlComment()
                            {
                                html = bloggerViewManager.RenderView("bloggerComment"), text = comment.Content, time = comment.Published
                            });
                        }
                        totalCommentCount += commentCount;
                    }
                }

                // make new argument dictionary for view before processing.
                bloggerViewManager.Arguments = new Dictionary <String, String>()
                {
                    { "POST_COUNT", totalCommentCount.ToString() }
                };


                timelineItem = Mirror.insertCommentBundleTimelineCard(Mirror.BuildService(credentials), bloggerViewManager.RenderView("bloggerPostCover"), htmlPages, mirror);
            }
            catch (Exception ex)
            {
                // notify user of exception
                StringBuilder response = new StringBuilder();
                DAL.InsertAccessLog(mirror.VerifyToken, mirror.UserToken, ex.ToString());
                response.Append("<article>\n  <section>\n    <div class=\"text-auto-size\" style=\"\">\n      <p class=\"red\">Unable to Retrieve Comments</p>\n      <p>Problem with Retrieving Comments</p>\n    </div>\n  </section>\n  <footer>\n    <div>please share again later</div>\n  </footer>\n</article>\n");
                timelineItem = Mirror.insertTimelineItem(Mirror.BuildService(credentials), response.ToString(), true);
                return;
            }
            return;
        }