示例#1
0
        public async Task <Post> CreateNewThreadPreviewAsync(NewThread newThreadEntity)
        {
            if (!_webManager.IsAuthenticated)
            {
                throw new Exception("User must be authenticated before using this method.");
            }
            var result = new Result();

            // We post to SA the same way we would for a normal reply, but instead of getting a redirect back to the
            // thread, we'll get redirected to back to the reply screen with the preview message on it.
            // From here we can parse that preview and return it to the user.
            var form = new MultipartFormDataContent
            {
                { new StringContent("postthread"), "action" },
                { new StringContent(newThreadEntity.ForumId.ToString(CultureInfo.InvariantCulture)), "forumid" },
                { new StringContent(newThreadEntity.FormKey), "formkey" },
                { new StringContent(newThreadEntity.FormCookie), "form_cookie" },
                { new StringContent(newThreadEntity.PostIcon.Id.ToString(CultureInfo.InvariantCulture)), "iconid" },
                { new StringContent(HtmlHelpers.HtmlEncode(newThreadEntity.Subject)), "subject" },
                { new StringContent(HtmlHelpers.HtmlEncode(newThreadEntity.Content)), "message" },
                { new StringContent(newThreadEntity.ParseUrl.ToString()), "parseurl" },
                { new StringContent("Submit Post"), "submit" },
                { new StringContent("Preview Post"), "preview" }
            };

            result = await _webManager.PostFormDataAsync(EndPoints.NewThreadBase, form);

            return(PostHandler.ParsePostPreview(_webManager.Parser.Parse(result.ResultHtml)));
        }
        public async Task <Post> CreateNewThreadPreviewAsync(NewThread newThreadEntity, CancellationToken token = default)
        {
            if (newThreadEntity == null)
            {
                throw new ArgumentNullException(nameof(newThreadEntity));
            }

            if (!this.webManager.IsAuthenticated)
            {
                throw new UserAuthenticationException(Awful.Core.Resources.ExceptionMessages.UserAuthenticationError);
            }

            // We post to SA the same way we would for a normal reply, but instead of getting a redirect back to the
            // thread, we'll get redirected to back to the reply screen with the preview message on it.
            // From here we can parse that preview and return it to the user.
            using var form = new MultipartFormDataContent
                  {
                      { new StringContent("postthread"), "action" },
                      { new StringContent(newThreadEntity.ForumId.ToString(CultureInfo.InvariantCulture)), "forumid" },
                      { new StringContent(newThreadEntity.FormKey), "formkey" },
                      { new StringContent(newThreadEntity.FormCookie), "form_cookie" },
                      { new StringContent(newThreadEntity.PostIcon.Id.ToString(CultureInfo.InvariantCulture)), "iconid" },
                      { new StringContent(HtmlHelpers.HtmlEncode(newThreadEntity.Subject)), "subject" },
                      { new StringContent(HtmlHelpers.HtmlEncode(newThreadEntity.Content)), "message" },
                      { new StringContent(newThreadEntity.ParseUrl.ToString()), "parseurl" },
                      { new StringContent("Submit Post"), "submit" },
                      { new StringContent("Preview Post"), "preview" },
                  };

            var result = await this.webManager.PostFormDataAsync(EndPoints.NewThreadBase, form, token).ConfigureAwait(false);

            return(PostHandler.ParsePostPreview(await this.webManager.Parser.ParseDocumentAsync(result.ResultHtml, token).ConfigureAwait(false)));
        }
        public async Task <Result> CreateNewThreadAsync(NewThread newThreadEntity, CancellationToken token = default)
        {
            if (newThreadEntity == null)
            {
                throw new ArgumentNullException(nameof(newThreadEntity));
            }

            if (!this.webManager.IsAuthenticated)
            {
                throw new UserAuthenticationException(Awful.Core.Resources.ExceptionMessages.UserAuthenticationError);
            }

            using var form = new MultipartFormDataContent
                  {
                      { new StringContent("postthread"), "action" },
                      { new StringContent(newThreadEntity.ForumId.ToString(CultureInfo.InvariantCulture)), "forumid" },
                      { new StringContent(newThreadEntity.FormKey), "formkey" },
                      { new StringContent(newThreadEntity.FormCookie), "form_cookie" },
                      { new StringContent(newThreadEntity.PostIcon.Id.ToString(CultureInfo.InvariantCulture)), "iconid" },
                      { new StringContent(HtmlHelpers.HtmlEncode(newThreadEntity.Subject)), "subject" },
                      { new StringContent(HtmlHelpers.HtmlEncode(newThreadEntity.Content)), "message" },
                      { new StringContent(newThreadEntity.ParseUrl.ToString()), "parseurl" },
                      { new StringContent("Submit Reply"), "submit" },
                  };
            return(await this.webManager.PostFormDataAsync(EndPoints.NewThreadBase, form, token).ConfigureAwait(false));
        }
示例#4
0
 public void StartAll()
 {
     CurrentThread.Start();
     Dispatcher.Start();
     Immediate.Start();
     NewThread.Start();
     ThreadPool.Start();
     TaskPool.Start();
 }
示例#5
0
        public async Task Init(Forum parameter)
        {
            if (WebManager == null)
            {
                LoginUser();
            }

            _threadManager = new ThreadManager(WebManager);
            Selected       = parameter;

            Title = "New Thread - " + Selected.Name;

            _newThread = await _threadManager.GetThreadCookiesAsync(Selected.ForumId);
        }
示例#6
0
        public async Task <Result> CreateNewThreadAsync(NewThread newThreadEntity)
        {
            var form = new MultipartFormDataContent
            {
                { new StringContent("postthread"), "action" },
                { new StringContent(newThreadEntity.ForumId.ToString(CultureInfo.InvariantCulture)), "forumid" },
                { new StringContent(newThreadEntity.FormKey), "formkey" },
                { new StringContent(newThreadEntity.FormCookie), "form_cookie" },
                { new StringContent(newThreadEntity.PostIcon.Id.ToString(CultureInfo.InvariantCulture)), "iconid" },
                { new StringContent(Extensions.HtmlEncode(newThreadEntity.Subject)), "subject" },
                { new StringContent(Extensions.HtmlEncode(newThreadEntity.Content)), "message" },
                { new StringContent(newThreadEntity.ParseUrl.ToString()), "parseurl" },
                { new StringContent("Submit Reply"), "submit" }
            };

            return(await _webManager.PostFormData(EndPoints.NewThreadBase, form));
        }
示例#7
0
        public async Task <Result> CreateNewThreadAsync(NewThread newThreadEntity)
        {
            if (!_webManager.IsAuthenticated)
            {
                throw new Exception("User must be authenticated before using this method.");
            }
            var form = new MultipartFormDataContent
            {
                { new StringContent("postthread"), "action" },
                { new StringContent(newThreadEntity.ForumId.ToString(CultureInfo.InvariantCulture)), "forumid" },
                { new StringContent(newThreadEntity.FormKey), "formkey" },
                { new StringContent(newThreadEntity.FormCookie), "form_cookie" },
                { new StringContent(newThreadEntity.PostIcon.Id.ToString(CultureInfo.InvariantCulture)), "iconid" },
                { new StringContent(HtmlHelpers.HtmlEncode(newThreadEntity.Subject)), "subject" },
                { new StringContent(HtmlHelpers.HtmlEncode(newThreadEntity.Content)), "message" },
                { new StringContent(newThreadEntity.ParseUrl.ToString()), "parseurl" },
                { new StringContent("Submit Reply"), "submit" }
            };

            return(await _webManager.PostFormDataAsync(EndPoints.NewThreadBase, form));
        }
示例#8
0
        public async Task <Result> CreateNewThreadPreview(NewThread newThreadEntity)
        {
            var form = new MultipartFormDataContent
            {
                { new StringContent("postthread"), "action" },
                { new StringContent(newThreadEntity.ForumId.ToString(CultureInfo.InvariantCulture)), "forumid" },
                { new StringContent(newThreadEntity.FormKey), "formkey" },
                { new StringContent(newThreadEntity.FormCookie), "form_cookie" },
                { new StringContent(newThreadEntity.PostIcon.Id.ToString(CultureInfo.InvariantCulture)), "iconid" },
                { new StringContent(Extensions.HtmlEncode(newThreadEntity.Subject)), "subject" },
                { new StringContent(Extensions.HtmlEncode(newThreadEntity.Content)), "message" },
                { new StringContent(newThreadEntity.ParseUrl.ToString()), "parseurl" },
                { new StringContent("Submit Post"), "submit" },
                { new StringContent("Preview Post"), "preview" }
            };

            // We post to SA the same way we would for a normal reply, but instead of getting a redirect back to the
            // thread, we'll get redirected to back to the reply screen with the preview message on it.
            // From here we can parse that preview and return it to the user.
            try
            {
                var result = await _webManager.PostFormData(EndPoints.NewThreadBase, form);

                var doc = new HtmlDocument();
                doc.LoadHtml(result.ResultHtml);
                HtmlNode[] replyNodes = doc.DocumentNode.Descendants("div").ToArray();

                HtmlNode previewNode =
                    replyNodes.FirstOrDefault(node => node.GetAttributeValue("class", "").Equals("inner postbody"));
                var post = new Post {
                    PostHtml = previewNode.OuterHtml
                };
                result.ResultJson = JsonConvert.SerializeObject(post);
                return(result);
            }
            catch (Exception exception)
            {
                throw new Exception("Failed to get preview HTML", exception);
            }
        }
示例#9
0
        public async Task <NewThread> GetThreadCookiesAsync(int forumId)
        {
            try
            {
                string url    = string.Format(EndPoints.NewThread, forumId);
                var    result = await _webManager.GetData(url);

                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(result.ResultHtml);
                HtmlNode[] formNodes = doc.DocumentNode.Descendants("input").ToArray();

                HtmlNode formKeyNode =
                    formNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("formkey"));

                HtmlNode formCookieNode =
                    formNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("form_cookie"));

                var newForumEntity = new NewThread();
                try
                {
                    string formKey    = formKeyNode.GetAttributeValue("value", "");
                    string formCookie = formCookieNode.GetAttributeValue("value", "");
                    newForumEntity.FormKey    = formKey;
                    newForumEntity.FormCookie = formCookie;
                    return(newForumEntity);
                }
                catch (Exception exception)
                {
                    throw new InvalidOperationException($"Could not parse new thread form data. {exception}");
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }
示例#10
0
        public Task <HttpResponseMessage> Index([FromBody] NewThread newThread)
        {
            Thread thread;

            try
            {
                thread = this.threadService.Create(newThread.AuthKey, newThread.Name, newThread.FirstWord);
            }
            catch (InstanceNotFoundException)
            {
                return(Task.FromResult(Request.CreateResponse(HttpStatusCode.Unauthorized)));
            }

            ThreadResponse response = new ThreadResponse
            {
                Id        = thread.Id,
                Name      = thread.Name,
                WordCount = thread.Posts.Count,
                Word      = thread.Posts.FirstOrDefault().Word,
                Author    = thread.User.UserName
            };

            return(Task.FromResult(Request.CreateResponse(HttpStatusCode.Created, response)));
        }