diff --git a/src/main/java/com/github/jreddit/submissions/Submission.java b/src/main/java/com/github/jreddit/submissions/Submission.java index 2d59f82..5b3920f 100644 --- a/src/main/java/com/github/jreddit/submissions/Submission.java +++ b/src/main/java/com/github/jreddit/submissions/Submission.java @@ -300,6 +300,28 @@ public void unsave() throws IOException, ParseException { ApiEndpointUtils.SUBMISSION_UNSAVE, user.getCookie()); } + /** + * This function hides a submission. + * @throws IOException If connection fails + * @throws ParseException If JSON parsing fails + */ + public void hide() throws IOException, ParseException { + restClient.post( + "id=" + fullName + "&uh=" + user.getModhash(), + ApiEndpointUtils.SUBMISSION_HIDE, user.getCookie()); + } + + /** + * This function unhides a submission. + * @throws IOException If connection fails + * @throws ParseException If JSON parsing fails + */ + public void unhide() throws IOException, ParseException { + restClient.post( + "id=" + fullName + "&uh=" + user.getModhash(), + ApiEndpointUtils.SUBMISSION_UNHIDE, user.getCookie()); + } + /** Upvote/downvote a submission */ private JSONObject voteResponse(int dir) throws IOException, ParseException { return (JSONObject) restClient.post( diff --git a/src/main/java/com/github/jreddit/utils/ApiEndpointUtils.java b/src/main/java/com/github/jreddit/utils/ApiEndpointUtils.java index 66985fe..96c8fa4 100644 --- a/src/main/java/com/github/jreddit/utils/ApiEndpointUtils.java +++ b/src/main/java/com/github/jreddit/utils/ApiEndpointUtils.java @@ -39,6 +39,10 @@ public class ApiEndpointUtils { public static final String SUBMISSION_UNSAVE = REDDIT_BASE_API_ENDPOINT + "/unsave"; + public static final String SUBMISSION_HIDE = REDDIT_BASE_API_ENDPOINT + "/hide"; + + public static final String SUBMISSION_UNHIDE = REDDIT_BASE_API_ENDPOINT + "/unhide"; + public static final String SUBMISSION_COMMENTS = "/r/%s/comments/%s.json?%s"; /* User specific constants */ diff --git a/src/test/java/com/github/jreddit/submissions/SubmissionTest.java b/src/test/java/com/github/jreddit/submissions/SubmissionTest.java index 2991635..aae7d26 100644 --- a/src/test/java/com/github/jreddit/submissions/SubmissionTest.java +++ b/src/test/java/com/github/jreddit/submissions/SubmissionTest.java @@ -10,10 +10,7 @@ import java.io.IOException; import static com.github.jreddit.testsupport.JsonHelpers.createSubmission; -import static com.github.jreddit.utils.ApiEndpointUtils.SUBMISSION_MARK_AS_NSFW; -import static com.github.jreddit.utils.ApiEndpointUtils.SUBMISSION_UNMARK_AS_NSFW; -import static com.github.jreddit.utils.ApiEndpointUtils.SUBMISSION_SAVE; -import static com.github.jreddit.utils.ApiEndpointUtils.SUBMISSION_UNSAVE; +import static com.github.jreddit.utils.ApiEndpointUtils.*; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -43,7 +40,7 @@ public void setUp() { @Test public void markNSFW() throws IOException, ParseException { - underTest = new Submission(createSubmission(REDDIT_OBJ_ID, false)); + underTest = new Submission(createSubmission(REDDIT_OBJ_ID, false, false, false)); underTest.setUser(user); underTest.setRestClient(restClient); underTest.markNSFW(); @@ -53,7 +50,7 @@ public void markNSFW() throws IOException, ParseException { @Test public void unmarkNSFW() throws IOException, ParseException { - underTest = new Submission(createSubmission(REDDIT_OBJ_ID, true)); + underTest = new Submission(createSubmission(REDDIT_OBJ_ID, true, false, false)); underTest.setUser(user); underTest.setRestClient(restClient); underTest.unmarkNSFW(); @@ -63,7 +60,7 @@ public void unmarkNSFW() throws IOException, ParseException { @Test public void save() throws IOException, ParseException { - underTest = new Submission(createSubmission(REDDIT_OBJ_ID, false)); + underTest = new Submission(createSubmission(REDDIT_OBJ_ID, false, true, false)); underTest.setUser(user); underTest.setRestClient(restClient); underTest.save(); @@ -74,7 +71,7 @@ public void save() throws IOException, ParseException { @Test public void unsave() throws IOException, ParseException { - underTest = new Submission(createSubmission(REDDIT_OBJ_ID, false)); + underTest = new Submission(createSubmission(REDDIT_OBJ_ID, false, false, false)); underTest.setUser(user); underTest.setRestClient(restClient); underTest.unsave(); @@ -82,4 +79,26 @@ public void unsave() throws IOException, ParseException { verify(restClient).post("id=" + REDDIT_OBJ_ID + "&uh=" + MOD_HASH, SUBMISSION_UNSAVE, COOKIE); } + + @Test + public void hide() throws IOException, ParseException { + underTest = new Submission(createSubmission(REDDIT_OBJ_ID, false, false, true)); + underTest.setUser(user); + underTest.setRestClient(restClient); + underTest.hide(); + + verify(restClient).post("id=" + REDDIT_OBJ_ID + "&uh=" + MOD_HASH, + SUBMISSION_HIDE, COOKIE); + } + + @Test + public void unhide() throws IOException, ParseException { + underTest = new Submission(createSubmission(REDDIT_OBJ_ID, false, false, false)); + underTest.setUser(user); + underTest.setRestClient(restClient); + underTest.unhide(); + + verify(restClient).post("id=" + REDDIT_OBJ_ID + "&uh=" + MOD_HASH, + SUBMISSION_UNHIDE, COOKIE); + } } \ No newline at end of file diff --git a/src/test/java/com/github/jreddit/testsupport/JsonHelpers.java b/src/test/java/com/github/jreddit/testsupport/JsonHelpers.java index bbcf0e4..e643ca5 100644 --- a/src/test/java/com/github/jreddit/testsupport/JsonHelpers.java +++ b/src/test/java/com/github/jreddit/testsupport/JsonHelpers.java @@ -172,11 +172,12 @@ public static JSONArray emptyJsonArray() { return new JSONArray(); } - public static JSONObject createSubmission(String redditObjId, boolean nsfw) { - return createSubmission(redditObjId, nsfw, null, new JSONObject()); + public static JSONObject createSubmission(String redditObjId, boolean nsfw, boolean saved, boolean hidden) { + return createSubmission(redditObjId, nsfw, saved, hidden, null, new JSONObject()); } - public static JSONObject createSubmission(String redditObjId, boolean nsfw, JSONObject media, JSONObject mediaEmbed) { + public static JSONObject createSubmission(String redditObjId, boolean nsfw, boolean saved, boolean hidden, + JSONObject media, JSONObject mediaEmbed) { JSONObject submission = new JSONObject(); submission.put("approved_by", null); submission.put("author", "jReddittest"); @@ -191,7 +192,7 @@ public static JSONObject createSubmission(String redditObjId, boolean nsfw, JSON submission.put("downs", 0L); submission.put("edited", false); submission.put("gilded", 0); - submission.put("hidden", false); + submission.put("hidden", hidden); submission.put("id", "1ikxpg"); submission.put("is_self", false); submission.put("likes", true); @@ -204,7 +205,7 @@ public static JSONObject createSubmission(String redditObjId, boolean nsfw, JSON submission.put("num_reports", 0); submission.put("over_18", nsfw); submission.put("permalink", "/r/jReddit/comments/1ikxpg/thekarangoeljreddit_github/"); - submission.put("saved", false); + submission.put("saved", saved); submission.put("score", 1L); submission.put("secure_media", null); submission.put("secure_media_embed", new JSONObject());