Skip to content

Commit

Permalink
FFJ-26 Add privacy parameter to PhotoUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
roundrop committed Feb 8, 2015
1 parent 5a75256 commit 2be12db
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 3 deletions.
63 changes: 61 additions & 2 deletions facebook4j-core/src/main/java/facebook4j/PagePhotoUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -122,10 +159,23 @@ public PagePhotoUpdate scheduledPublishTime(Date scheduledPublishTime) {

/*package*/ HttpParameter[] asHttpParameterArray() {
List<HttpParameter> params = new ArrayList<HttpParameter>();
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()));
}
Expand All @@ -151,19 +201,25 @@ 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;
}

@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);
Expand All @@ -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 +
Expand Down
40 changes: 39 additions & 1 deletion facebook4j-core/src/main/java/facebook4j/PhotoUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import facebook4j.internal.http.HttpParameter;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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<HttpParameter> params = new ArrayList<HttpParameter>();
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));
}
Expand All @@ -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()]);
}

Expand All @@ -107,27 +139,33 @@ 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;
}

@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;
}

@Override
public String toString() {
return "PhotoUpdate{" +
"source=" + source +
", url=" + url +
", message='" + message + '\'' +
", place='" + place + '\'' +
", noStory=" + noStory +
", privacy=" + privacy +
'}';
}
}
28 changes: 28 additions & 0 deletions facebook4j-core/src/test/java/facebook4j/PageMethodsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> countries = new HashSet<String>();
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 {
Expand Down
22 changes: 22 additions & 0 deletions facebook4j-core/src/test/java/facebook4j/PhotoMethodsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2be12db

Please sign in to comment.