private Post Submit(SubmitData data) { if (Reddit.User == null) { throw new RedditException("No user logged in."); } var request = WebAgent.CreatePost(SubmitLinkUrl); WebAgent.WritePostBody(request.GetRequestStream(), data); var json = WebAgent.ExecuteRequest(request); ICaptchaSolver solver = Reddit.CaptchaSolver; if (json["errors"].Any() && json["errors"][0][0].ToString() == "BAD_CAPTCHA" && solver != null) { data.Iden = json["captcha"].ToString(); CaptchaResponse captchaResponse = solver.HandleCaptcha(new Captcha(data.Iden)); // We throw exception due to this method being expected to return a valid Post object, but we cannot // if we got a Captcha error. if (captchaResponse.Cancel) { throw new CaptchaFailedException("Captcha verification failed when submitting " + data.Kind + " post"); } data.Captcha = captchaResponse.Answer; return(Submit(data)); } return(new Post(Reddit, json, WebAgent)); }
/// <summary> /// Replaces the text in this post with the input text. /// </summary> /// <param name="newText">The text to replace the post's contents</param> public void EditText(string newText) { if (Reddit.User == null) { throw new Exception("No user logged in."); } if (!IsSelfPost) { throw new Exception("Submission to edit is not a self-post."); } var request = WebAgent.CreatePost(EditUserTextUrl); WebAgent.WritePostBody(request.GetRequestStream(), new { api_type = "json", text = newText, thing_id = FullName, uh = Reddit.User.Modhash }); var json = WebAgent.ExecuteRequest(request); try { SelfText = newText; } catch { throw new Exception("Error editing text."); } }
public Comment Comment(string message) { if (Reddit.User == null) { throw new AuthenticationException("No user logged in."); } var request = WebAgent.CreatePost(CommentUrl); var stream = request.GetRequestStream(); WebAgent.WritePostBody(stream, new { text = message, thing_id = FullName, uh = Reddit.User.Modhash, api_type = "json" }); stream.Close(); var json = WebAgent.ExecuteRequest(request); if (json["ratelimit"] != null) { throw new RateLimitException(TimeSpan.FromSeconds(json["ratelimit"].ValueOrDefault <double>())); } var comment = json["data"]["things"][0]; return(new Comment(Reddit, comment, WebAgent, this)); }
public void AcceptModeratorInvite() { var request = WebAgent.CreatePost(AcceptModeratorInviteUrl); WebAgent.WritePostBody(request.GetRequestStream(), new { api_type = "json", uh = Reddit.User.Modhash, r = Name }); WebAgent.ExecuteRequest(request); }
public void SetAsRead() { var request = WebAgent.CreatePost(SetAsReadUrl); WebAgent.WritePostBody(request.GetRequestStream(), new { id = FullName, uh = Reddit.User.Modhash, api_type = "json" }); WebAgent.ExecuteRequest(request); }
/// <summary> /// Resets the subreddit's header image to the Reddit logo /// </summary> public void ResetHeaderImage() { var request = WebAgent.CreatePost(DeleteHeaderImageUrl); var stream = request.GetRequestStream(); WebAgent.WritePostBody(stream, new { uh = Reddit.User.Modhash, r = Subreddit.Name }); stream.Close(); WebAgent.ExecuteRequest(request); }
public void Approve() { var request = WebAgent.CreatePost(ApproveUrl); var stream = request.GetRequestStream(); WebAgent.WritePostBody(stream, new { id = FullName, uh = Reddit.User.Modhash }); stream.Close(); WebAgent.ExecuteRequest(request); }
public void ClearFlairTemplates(FlairType flairType) { var request = WebAgent.CreatePost(ClearFlairTemplatesUrl); var stream = request.GetRequestStream(); WebAgent.WritePostBody(stream, new { flair_type = flairType == FlairType.Link ? "LINK_FLAIR" : "USER_FLAIR", uh = Reddit.User.Modhash, r = Name }); stream.Close(); WebAgent.ExecuteRequest(request); }
public void RemoveModerator(string id) { var request = WebAgent.CreatePost(LeaveModerationUrl); WebAgent.WritePostBody(request.GetRequestStream(), new { api_type = "json", uh = Reddit.User.Modhash, r = Name, type = "moderator", id }); WebAgent.ExecuteRequest(request); }
public void AddContributor(string user) { var request = WebAgent.CreatePost(AddContributorUrl); WebAgent.WritePostBody(request.GetRequestStream(), new { api_type = "json", uh = Reddit.User.Modhash, r = Name, type = "contributor", name = user }); WebAgent.ExecuteRequest(request); }
public void Delete() { var request = WebAgent.CreatePost(DeleteImageUrl); var stream = request.GetRequestStream(); WebAgent.WritePostBody(stream, new { img_name = Name, uh = Reddit.User.Modhash, r = SubredditStyle.Subreddit.Name }); stream.Close(); WebAgent.ExecuteRequest(request); SubredditStyle.Images.Remove(this); }
public void SetUserFlair(string user, string cssClass, string text) { var request = WebAgent.CreatePost(SetUserFlairUrl); var stream = request.GetRequestStream(); WebAgent.WritePostBody(stream, new { css_class = cssClass, text = text, uh = Reddit.User.Modhash, r = Name, name = user }); stream.Close(); WebAgent.ExecuteRequest(request); }
public void UpdateCss() { var request = WebAgent.CreatePost(UpdateCssUrl); var stream = request.GetRequestStream(); WebAgent.WritePostBody(stream, new { op = "save", stylesheet_contents = CSS, uh = Reddit.User.Modhash, api_type = "json", r = Subreddit.Name }); stream.Close(); WebAgent.ExecuteRequest(request); }
public void Unhide() { if (Reddit.User == null) { throw new AuthenticationException("No user logged in."); } var request = WebAgent.CreatePost(UnhideUrl); var stream = request.GetRequestStream(); WebAgent.WritePostBody(stream, new { id = FullName, uh = Reddit.User.Modhash }); stream.Close(); WebAgent.ExecuteRequest(request); }
public void UploadImage(string name, ImageType imageType, byte[] file) { var request = WebAgent.CreatePost(UploadImageUrl); var formData = new MultipartFormBuilder(request); formData.AddDynamic(new { name, uh = Reddit.User.Modhash, r = Subreddit.Name, formid = "image-upload", img_type = imageType == ImageType.PNG ? "png" : "jpg", upload = "" }); formData.AddFile("file", "foo.png", file, imageType == ImageType.PNG ? "image/png" : "image/jpeg"); formData.Finish(); WebAgent.ExecuteRequest(request); }
public void Reply(string message) { if (Reddit.User == null) { throw new AuthenticationException("No user logged in."); } var request = WebAgent.CreatePost(CommentUrl); var stream = request.GetRequestStream(); WebAgent.WritePostBody(stream, new { text = message, thing_id = FullName, uh = Reddit.User.Modhash }); stream.Close(); WebAgent.ExecuteRequest(request); }
public void BanUser(string user, string reason) { var request = WebAgent.CreatePost(BanUserUrl); WebAgent.WritePostBody(request.GetRequestStream(), new { api_type = "json", uh = Reddit.User.Modhash, r = Name, type = "banned", id = "#banned", name = user, note = reason, action = "add", container = FullName }); WebAgent.ExecuteRequest(request); }
public void AddFlairTemplate(string cssClass, FlairType flairType, string text, bool userEditable) { var request = WebAgent.CreatePost(FlairTemplateUrl); var stream = request.GetRequestStream(); WebAgent.WritePostBody(stream, new { css_class = cssClass, flair_type = flairType == FlairType.Link ? "LINK_FLAIR" : "USER_FLAIR", text = text, text_editable = userEditable, uh = Reddit.User.Modhash, r = Name, api_type = "json" }); stream.Close(); var json = WebAgent.ExecuteRequest(request); }
public void Unsubscribe() { if (Reddit.User == null) { throw new AuthenticationException("No user logged in."); } var request = WebAgent.CreatePost(SubscribeUrl); var stream = request.GetRequestStream(); WebAgent.WritePostBody(stream, new { action = "unsub", sr = FullName, uh = Reddit.User.Modhash }); stream.Close(); WebAgent.ExecuteRequest(request); // Discard results }
public void SetFlair(string flairText, string flairClass) { if (Reddit.User == null) { throw new Exception("No user logged in."); } var request = WebAgent.CreatePost(SetFlairUrl); WebAgent.WritePostBody(request.GetRequestStream(), new { api_type = "json", r = Subreddit, css_class = flairClass, link = FullName, //name = Name, text = flairText, uh = Reddit.User.Modhash }); WebAgent.ExecuteRequest(request); LinkFlairText = flairText; }
public void SetVote(VoteType type) { if (this.Vote == type) { return; } var request = WebAgent.CreatePost(VoteUrl); var stream = request.GetRequestStream(); WebAgent.WritePostBody(stream, new { dir = (int)type, id = FullName, uh = Reddit.User.Modhash }); stream.Close(); WebAgent.ExecuteRequest(request); if (Liked == true) { Upvotes--; } if (Liked == false) { Downvotes--; } switch (type) { case VoteType.Upvote: Liked = true; Upvotes++; return; case VoteType.None: Liked = null; return; case VoteType.Downvote: Liked = false; Downvotes++; return; } }
public void UpdateSettings() { var request = WebAgent.CreatePost(SiteAdminUrl); var stream = request.GetRequestStream(); string link_type; string type; string wikimode; switch (ContentOptions) { case ContentOptions.All: link_type = "any"; break; case ContentOptions.LinkOnly: link_type = "link"; break; default: link_type = "self"; break; } switch (SubredditType) { case SubredditType.Public: type = "public"; break; case SubredditType.Private: type = "private"; break; default: type = "restricted"; break; } switch (WikiEditMode) { case WikiEditMode.All: wikimode = "anyone"; break; case WikiEditMode.Moderators: wikimode = "modonly"; break; default: wikimode = "disabled"; break; } WebAgent.WritePostBody(stream, new { allow_top = AllowAsDefault, description = Sidebar, domain = Domain, lang = Language, link_type, over_18 = NSFW, public_description = PublicDescription, show_media = ShowThumbnails, sr = Subreddit.FullName, title = Title, type, uh = Reddit.User.Modhash, wiki_edit_age = WikiEditAge, wiki_edit_karma = WikiEditKarma, wikimode, api_type = "json" }, "header-title", HeaderHoverText); stream.Close(); WebAgent.ExecuteRequest(request); }