コード例 #1
0
        private void handleReply(HttpContext ctx, Notification mirror, String requestPayload)
        {
            // create new string builder to hold the response
            StringBuilder response = new StringBuilder();

            // 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 the speakable text
            TimelineItem timelineItem = Mirror.getTimelineItem(Mirror.BuildService(credentials), mirror.ItemId);

            // get the new title and in reply to
            String newTitle  = timelineItem.Text;
            String inReplyTo = timelineItem.InReplyTo;

            // remove the extra timline card
            Mirror.DeleteTimelineItem(Mirror.BuildService(credentials), timelineItem.Id);

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

            // we need to determine if this came from our post toolbox or not
            PostToolbox postToolbox = DAL.GetPostToolbox(bl.userId);

            if (postToolbox.itemId == inReplyTo)
            // it's a new post request from the toolbox
            {
                // new posting
                String newPostText = newTitle;

                // post manager
                PostManager pm = new PostManager();

                // create blogger post instance
                Post postInsertRequest = new Post();

                try
                {
                    // append brand
                    newPostText += "<code><br />Posted From Glass</code>";

                    // use post insert request to handle items
                    postInsertRequest = Blogger.insertPost(Blogger.BuildService(credentials), bl, newPostText);

                    // create the response that will go to glass
                    ViewManager bloggerViewHandler = new ViewManager();
                    bloggerViewHandler.AddView("bloggerText", "~/views/bloggerTextPost.html");
                    bloggerViewHandler.Arguments = new Dictionary <String, String>()
                    {
                        { "BLOG_NAME", bl.blogName },
                        { "BLOG_TEXT", newPostText }
                    };
                    timelineItem = Mirror.insertTimelineItem(Mirror.BuildService(credentials), bloggerViewHandler.RenderView("bloggerText"));
                }
                catch (Exception ex)
                {
                    // notify user of exception
                    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 Insert Text Post</p>\n      <p>Problem with Inserting Text Post</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);
                }

                // submit to post_manager
                pm.postImageLocation = "TEXT_ONLY";
                pm.postImageWebURI   = "TEXT_ONLY";
                pm.postImageContent  = "TEXT_ONLY";
                pm.blogLinkId        = bl.blogId;
                pm.postTitle         = DAL.scrubApos(postInsertRequest.Title);
                pm.postId            = postInsertRequest.Id;
                pm.postContent       = DAL.scrubApos(newPostText);
                pm.userId            = mirror.UserToken;
                pm.eTagId            = timelineItem.ETag;
                pm.itemId            = timelineItem.Id;

                // insert post manager
                DAL.InsertPostManager(pm);
            }
            else
            // it's a title request
            {
                // create blogger post instance
                Post postPatchRequest = new Post();

                try
                {
                    // the inreply to is the specific post we are dealing with
                    PostManager pm = DAL.GetPostManager(mirror, inReplyTo);

                    if (pm.itemId != "-1")
                    {
                        // use post insert request to handle items
                        postPatchRequest = Blogger.updatePostTitle(Blogger.BuildService(credentials), bl, pm, newTitle);
                        // set post manager title to new title
                        pm.postTitle = DAL.scrubApos(newTitle);
                        pm.itemId    = inReplyTo;
                        // update the post manager database
                        DAL.UpdatePostManagerTitle(pm);
                    }
                }
                catch (Exception ex)
                {
                    // notify user of exception
                    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 Update</p>\n      <p>Problem with Updating Title Text</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;
        }