Exemplo n.º 1
0
        public async Task <Result> SendPrivateMessageAsync(NewPrivateMessage newPrivateMessageEntity, CancellationToken token = default)
        {
            if (newPrivateMessageEntity == null)
            {
                throw new ArgumentNullException(nameof(newPrivateMessageEntity));
            }

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

            using var form = new MultipartFormDataContent
                  {
                      { new StringContent("dosend"), "action" },
                      { new StringContent(newPrivateMessageEntity.Receiver), "touser" },
                      { new StringContent(HtmlHelpers.HtmlEncode(newPrivateMessageEntity.Title)), "title" },
                      { new StringContent(HtmlHelpers.HtmlEncode(newPrivateMessageEntity.Body)), "message" },
                      { new StringContent("yes"), "parseurl" },
                      { new StringContent("yes"), "parseurl" },
                      { new StringContent("Send Message"), "submit" },
                  };
            if (newPrivateMessageEntity.Icon != null)
            {
                form.Add(new StringContent(newPrivateMessageEntity.Icon.Id.ToString(CultureInfo.InvariantCulture)), "iconid");
            }

            return(await this.webManager.PostFormDataAsync(EndPoints.NewPrivateMessageBase, form, token).ConfigureAwait(false));
        }
        public async Task <Result> SendPrivateMessageAsync(NewPrivateMessage newPrivateMessageEntity)
        {
            if (!_webManager.IsAuthenticated)
            {
                throw new Exception("User must be authenticated before using this method.");
            }
            Result result = new Result();
            MultipartFormDataContent form;

            try
            {
                form = new MultipartFormDataContent
                {
                    { new StringContent("dosend"), "action" },
                    { new StringContent(newPrivateMessageEntity.Receiver), "touser" },
                    { new StringContent(HtmlHelpers.HtmlEncode(newPrivateMessageEntity.Title)), "title" },
                    { new StringContent(HtmlHelpers.HtmlEncode(newPrivateMessageEntity.Body)), "message" },
                    { new StringContent("yes"), "parseurl" },
                    { new StringContent("yes"), "parseurl" },
                    { new StringContent("Send Message"), "submit" }
                };
                if (newPrivateMessageEntity.Icon != null)
                {
                    form.Add(new StringContent(newPrivateMessageEntity.Icon.Id.ToString(CultureInfo.InvariantCulture)), "iconid");
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.CreateErrorObject(result, ex.Message, ex.StackTrace);
                return(result);
            }
            try
            {
                result = await _webManager.PostFormDataAsync(EndPoints.NewPrivateMessageBase, form);

                return(result);
            }
            catch (Exception ex)
            {
                ErrorHandler.CreateErrorObject(result, ex.Message, ex.StackTrace);
                return(result);
            }
        }