diff --git a/facebook4j-core/src/main/java/facebook4j/PagePhotoUpdate.java b/facebook4j-core/src/main/java/facebook4j/PagePhotoUpdate.java index 0da180ef..0a923392 100644 --- a/facebook4j-core/src/main/java/facebook4j/PagePhotoUpdate.java +++ b/facebook4j-core/src/main/java/facebook4j/PagePhotoUpdate.java @@ -18,6 +18,7 @@ import facebook4j.internal.http.HttpParameter; +import java.net.URL; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -30,21 +31,31 @@ public class PagePhotoUpdate implements java.io.Serializable { private static final long serialVersionUID = -2690799855513822140L; private Media source; + private URL url; private String message; + private String place; + private Boolean noStory; private TargetingParameter targeting; private FeedTargetingParameter feedTargeting; private Boolean published; private Integer scheduledPublishTime; public PagePhotoUpdate(Media source) { - super(); this.source = source; } + public PagePhotoUpdate(URL url) { + this.url = url; + } + public Media getSource() { return source; } + public URL getUrl() { + return url; + } + public String getMessage() { return message; } @@ -58,6 +69,32 @@ public PagePhotoUpdate message(String message) { return this; } + public String getPlace() { + return place; + } + + public void setPlace(String place) { + this.place = place; + } + + public PagePhotoUpdate place(String place) { + setPlace(place); + return this; + } + + public Boolean getNoStory() { + return noStory; + } + + public void setNoStory(boolean noStory) { + this.noStory = noStory; + } + + public PagePhotoUpdate noStory(boolean noStory) { + setNoStory(noStory); + return this; + } + public TargetingParameter getTargeting() { return targeting; } @@ -122,10 +159,23 @@ public PagePhotoUpdate scheduledPublishTime(Date scheduledPublishTime) { /*package*/ HttpParameter[] asHttpParameterArray() { List params = new ArrayList(); - params.add(new HttpParameter("source", source.getMediaFile())); + if (source != null) { + params.add(new HttpParameter("source", source.getMediaFile())); + } + if (url != null) { + params.add(new HttpParameter("url", url.toString())); + } if (message != null) { params.add(new HttpParameter("message", message)); } + if (place != null) { + params.add(new HttpParameter("place", place)); + } + if (noStory != null) { + if (noStory) { + params.add(new HttpParameter("no_story", 1)); + } + } if (targeting != null) { params.add(new HttpParameter("targeting", targeting.asJSONString())); } @@ -151,11 +201,14 @@ public boolean equals(Object o) { if (feedTargeting != null ? !feedTargeting.equals(that.feedTargeting) : that.feedTargeting != null) return false; if (message != null ? !message.equals(that.message) : that.message != null) return false; + if (noStory != null ? !noStory.equals(that.noStory) : that.noStory != null) return false; + if (place != null ? !place.equals(that.place) : that.place != null) return false; if (published != null ? !published.equals(that.published) : that.published != null) return false; if (scheduledPublishTime != null ? !scheduledPublishTime.equals(that.scheduledPublishTime) : that.scheduledPublishTime != null) return false; if (source != null ? !source.equals(that.source) : that.source != null) return false; if (targeting != null ? !targeting.equals(that.targeting) : that.targeting != null) return false; + if (url != null ? !url.equals(that.url) : that.url != null) return false; return true; } @@ -163,7 +216,10 @@ public boolean equals(Object o) { @Override public int hashCode() { int result = source != null ? source.hashCode() : 0; + result = 31 * result + (url != null ? url.hashCode() : 0); result = 31 * result + (message != null ? message.hashCode() : 0); + result = 31 * result + (place != null ? place.hashCode() : 0); + result = 31 * result + (noStory != null ? noStory.hashCode() : 0); result = 31 * result + (targeting != null ? targeting.hashCode() : 0); result = 31 * result + (feedTargeting != null ? feedTargeting.hashCode() : 0); result = 31 * result + (published != null ? published.hashCode() : 0); @@ -175,7 +231,10 @@ public int hashCode() { public String toString() { return "PagePhotoUpdate{" + "source=" + source + + ", url=" + url + ", message='" + message + '\'' + + ", place='" + place + '\'' + + ", noStory=" + noStory + ", targeting=" + targeting + ", feedTargeting=" + feedTargeting + ", published=" + published + diff --git a/facebook4j-core/src/main/java/facebook4j/PhotoUpdate.java b/facebook4j-core/src/main/java/facebook4j/PhotoUpdate.java index 213713c6..b76eb70d 100644 --- a/facebook4j-core/src/main/java/facebook4j/PhotoUpdate.java +++ b/facebook4j-core/src/main/java/facebook4j/PhotoUpdate.java @@ -18,6 +18,7 @@ import facebook4j.internal.http.HttpParameter; +import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -29,18 +30,28 @@ public class PhotoUpdate implements java.io.Serializable { private static final long serialVersionUID = -2679992754222742305L; private Media source; + private URL url; private String message; private String place; private Boolean noStory; + private PrivacyParameter privacy; public PhotoUpdate(Media source) { this.source = source; } + public PhotoUpdate(URL url) { + this.url = url; + } + public Media getSource() { return source; } + public URL getUrl() { + return url; + } + public String getMessage() { return message; } @@ -80,9 +91,27 @@ public PhotoUpdate noStory(boolean noStory) { return this; } + public PrivacyParameter getPrivacy() { + return privacy; + } + + public void setPrivacy(PrivacyParameter privacy) { + this.privacy = privacy; + } + + public PhotoUpdate privacy(PrivacyParameter privacy) { + setPrivacy(privacy); + return this; + } + /*package*/ HttpParameter[] asHttpParameterArray() { List params = new ArrayList(); - params.add(source.asHttpParameter("source")); + if (source != null) { + params.add(source.asHttpParameter("source")); + } + if (url != null) { + params.add(new HttpParameter("url", url.toString())); + } if (message != null) { params.add(new HttpParameter("message", message)); } @@ -94,6 +123,9 @@ public PhotoUpdate noStory(boolean noStory) { params.add(new HttpParameter("no_story", 1)); } } + if (privacy != null) { + params.add(new HttpParameter("privacy", privacy.asJSONString())); + } return params.toArray(new HttpParameter[params.size()]); } @@ -107,7 +139,9 @@ public boolean equals(Object o) { if (message != null ? !message.equals(that.message) : that.message != null) return false; if (noStory != null ? !noStory.equals(that.noStory) : that.noStory != null) return false; if (place != null ? !place.equals(that.place) : that.place != null) return false; + if (privacy != null ? !privacy.equals(that.privacy) : that.privacy != null) return false; if (source != null ? !source.equals(that.source) : that.source != null) return false; + if (url != null ? !url.equals(that.url) : that.url != null) return false; return true; } @@ -115,9 +149,11 @@ public boolean equals(Object o) { @Override public int hashCode() { int result = source != null ? source.hashCode() : 0; + result = 31 * result + (url != null ? url.hashCode() : 0); result = 31 * result + (message != null ? message.hashCode() : 0); result = 31 * result + (place != null ? place.hashCode() : 0); result = 31 * result + (noStory != null ? noStory.hashCode() : 0); + result = 31 * result + (privacy != null ? privacy.hashCode() : 0); return result; } @@ -125,9 +161,11 @@ public int hashCode() { public String toString() { return "PhotoUpdate{" + "source=" + source + + ", url=" + url + ", message='" + message + '\'' + ", place='" + place + '\'' + ", noStory=" + noStory + + ", privacy=" + privacy + '}'; } } diff --git a/facebook4j-core/src/test/java/facebook4j/PageMethodsTest.java b/facebook4j-core/src/test/java/facebook4j/PageMethodsTest.java index 1c71288e..7ba23d5d 100644 --- a/facebook4j-core/src/test/java/facebook4j/PageMethodsTest.java +++ b/facebook4j-core/src/test/java/facebook4j/PageMethodsTest.java @@ -1835,6 +1835,34 @@ public void id() throws Exception { assertThat(actual, is("137246726435626_185932178233747")); } + + @Test + public void url_place_nostory() throws Exception { + facebook.setMockJSON("mock_json/post_id.json"); + PagePhotoUpdate pagePhotoUpdate = new PagePhotoUpdate(new URL("https://fbstatic-a.akamaihd.net/rsrc.php/v2/yC/r/gfLdB3lVEL5.png")).message("upload photo to the page test."); + Set countries = new HashSet(); + countries.add("US"); + countries.add("GB"); + TargetingParameter targeting = new TargetingParameter().countries(countries); + pagePhotoUpdate.setTargeting(targeting); + FeedTargetingParameter feedTargeting = new FeedTargetingParameter().genders(FeedTargetingParameter.Gender.Male); + feedTargeting.setAgeMin(20); + feedTargeting.setAgeMax(40); + pagePhotoUpdate.setFeedTargeting(feedTargeting); + pagePhotoUpdate.setPlace("178106048903380"); + pagePhotoUpdate.setNoStory(true); + String actual = facebook.postPagePhoto("137246726435626", pagePhotoUpdate); + assertThat(facebook.getHttpMethod(), is(RequestMethod.POST)); + assertThat(facebook.getEndpointURL(), is(pathOf("/137246726435626/photos"))); + assertThat(facebook.getHttpParameters(), hasPostParameter("url", "https://fbstatic-a.akamaihd.net/rsrc.php/v2/yC/r/gfLdB3lVEL5.png")); + assertThat(facebook.getHttpParameters(), hasPostParameter("message", "upload photo to the page test.")); + assertThat(facebook.getHttpParameters(), hasPostParameter("targeting", "{\"countries\":[\"US\",\"GB\"]}")); + assertThat(facebook.getHttpParameters(), hasPostParameter("feed_targeting", "{\"age_min\":20,\"genders\":{\"value\":1},\"age_max\":40}")); + assertThat(facebook.getHttpParameters(), hasPostParameter("place", "178106048903380")); + assertThat(facebook.getHttpParameters(), hasPostParameter("no_story", "1")); + + assertThat(actual, is("137246726435626_185932178233747")); + } } public static class updatePageSetting extends MockFacebookTestBase { diff --git a/facebook4j-core/src/test/java/facebook4j/PhotoMethodsTest.java b/facebook4j-core/src/test/java/facebook4j/PhotoMethodsTest.java index 5d23c37f..424c5b18 100644 --- a/facebook4j-core/src/test/java/facebook4j/PhotoMethodsTest.java +++ b/facebook4j-core/src/test/java/facebook4j/PhotoMethodsTest.java @@ -17,6 +17,7 @@ package facebook4j; import facebook4j.internal.http.RequestMethod; +import facebook4j.junit.FacebookAPIVersion; import org.junit.Test; import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; @@ -687,6 +688,27 @@ public void id_withAllParams() throws Exception { assertThat(actual, is("1234567890123456")); } + + @Test + @FacebookAPIVersion("v2.2") + public void privacy() throws Exception { + facebook.setMockJSON("mock_json/id_and_post_id.json"); + PrivacyParameter privacyParameter = new PrivacyBuilder().setValue(PrivacyType.SELF).build(); + PhotoUpdate photoUpdate = new PhotoUpdate(new URL("https://fbstatic-a.akamaihd.net/rsrc.php/v2/yC/r/gfLdB3lVEL5.png")) + .message("privacy") + .noStory(true) + .privacy(privacyParameter); + ; + String actual = facebook.postPhoto(photoUpdate); + assertThat(facebook.getHttpMethod(), is(RequestMethod.POST)); + assertThat(facebook.getEndpointURL(), is(pathOf("/v2.2/me/photos"))); + assertThat(facebook.getHttpParameters(), hasPostParameter("url", "https://fbstatic-a.akamaihd.net/rsrc.php/v2/yC/r/gfLdB3lVEL5.png")); + assertThat(facebook.getHttpParameters(), hasPostParameter("message", "privacy")); + assertThat(facebook.getHttpParameters(), hasPostParameter("no_story", "1")); + assertThat(facebook.getHttpParameters(), hasPostParameter("privacy", "{\"value\":\"SELF\"}")); + + assertThat(actual, is("1234567890123456")); + } } public static class deletePhoto extends MockFacebookTestBase {