Esempio n. 1
0
        public static async Task <Post> ReplyWallMessageAsync(WikiaSite site, object scopeInst, WikiPageStub owner,
                                                              int parentId, string messageBody, CancellationToken cancellationToken)
        {
            Debug.Assert(site != null);
            using (site.BeginActionScope(scopeInst, owner, parentId))
            {
                var tokenPurged = false;
BEGIN:
                var jresult = await site.InvokeNirvanaAsync(new WikiaQueryRequestMessage(new
                {
                    controller = "WallExternal",
                    method = "replyToMessage",
                    format = "json",
                    token = await site.GetTokenAsync("edit", cancellationToken),
                    pagenamespace = WikiaNamespaces.Thread,
                    pagetitle = parentId,
                    parent = parentId,
                    body = messageBody,
                    convertToFormat = "",
                }, true), WikiaJsonResonseParser.Default, cancellationToken);

                if (!string.Equals((string)jresult["status"], "True", StringComparison.OrdinalIgnoreCase))
                {
                    var errorMessage = (string)jresult["errormsg"];
                    if (errorMessage != null)
                    {
                        if (!tokenPurged)
                        {
                            if (errorMessage.IndexOf("There seems to be a problem with your login session", StringComparison.OrdinalIgnoreCase) >= 0)
                            {
                                await site.GetTokenAsync("edit", true, cancellationToken);

                                tokenPurged = true;
                                goto BEGIN;
                            }
                        }
                    }
                    errorMessage = "Status code indicates a failure: " + (string)jresult["status"];
                    throw new OperationFailedException(errorMessage);
                }
                var text = (string)jresult["message"];
                var doc  = new HtmlDocument();
                doc.LoadHtml(text);
                var node = doc.DocumentNode.SelectSingleNode("li");
                if (node == null)
                {
                    throw new UnexpectedDataException("Cannot locate the comment text node in the Wikia API response.");
                }
                return(Post.FromHtmlNode(site, owner, node));
            }
        }
Esempio n. 2
0
        public static async Task <Post> PostWallMessageAsync(WikiaSite site, object scopeInst, WikiPageStub owner,
                                                             string messageTitle, string messageBody, IEnumerable <string> relatedPages, CancellationToken cancellationToken)
        {
            Debug.Assert(site != null);
            Debug.Assert(owner.HasTitle);
            using (site.BeginActionScope(scopeInst, owner))
            {
                var tokenPurged     = false;
                var pageTitle       = owner.Title;
                var pageNamespaceId = owner.NamespaceId;
                if (pageTitle.StartsWith("Message Wall:", StringComparison.OrdinalIgnoreCase))
                {
                    pageTitle = pageTitle.Substring(13);
                    if (!owner.HasNamespaceId)
                    {
                        pageNamespaceId = WikiaNamespaces.MessageWall;
                    }
                }
                else if (pageTitle.StartsWith("Board:", StringComparison.OrdinalIgnoreCase))
                {
                    pageTitle = pageTitle.Substring(6);
                    if (!owner.HasNamespaceId)
                    {
                        pageNamespaceId = WikiaNamespaces.Thread;
                    }
                }
                else
                {
                    var link = WikiLink.Parse(site, owner.Title);
                    pageTitle       = link.Title;
                    pageNamespaceId = link.Namespace.Id;
                }
                var queryParams = new OrderedKeyValuePairs <string, object>
                {
                    { "token", null },
                    { "controller", "WallExternal" },
                    { "method", "postNewMessage" },
                    { "format", "json" },
                    { "pagenamespace", pageNamespaceId },
                    { "pagetitle", pageTitle },
                    { "messagetitle", messageTitle },
                    { "body", messageBody },
                    { "notifyeveryone", 0 },
                    { "convertToFormat", "" },
                };
                if (relatedPages != null)
                {
                    foreach (var title in relatedPages)
                    {
                        queryParams.Add("relatedTopics[]", title);
                    }
                }
BEGIN:
                queryParams["token"] = await site.GetTokenAsync("edit", cancellationToken);

                var jresult = await site.InvokeNirvanaAsync(new WikiaQueryRequestMessage(queryParams, true), WikiaJsonResonseParser.Default, cancellationToken);

                if (!string.Equals((string)jresult["status"], "True", StringComparison.OrdinalIgnoreCase))
                {
                    var errorMessage = (string)jresult["errormsg"];
                    if (errorMessage != null)
                    {
                        if (!tokenPurged)
                        {
                            if (errorMessage.IndexOf("There seems to be a problem with your login session", StringComparison.OrdinalIgnoreCase) >= 0)
                            {
                                await site.GetTokenAsync("edit", true, cancellationToken);

                                tokenPurged = true;
                                goto BEGIN;
                            }
                        }
                    }
                    errorMessage = "Status code indicates a failure: " + (string)jresult["status"];
                    throw new OperationFailedException(errorMessage);
                }
                var text = (string)jresult["message"];
                var doc  = new HtmlDocument();
                doc.LoadHtml(text);
                var node = doc.DocumentNode.SelectSingleNode("li");
                if (node == null)
                {
                    throw new UnexpectedDataException("Cannot locate the comment text node in the Wikia API response.");
                }
                return(Post.FromHtmlNode(site, owner, node));
            }
        }