Skip to content

Commit bf18637

Browse files
author
Craig Walls
committed
Refine a few PageOperations methods. Introduce a facebookOperations() method to return page-specific FacebookTemplate. SOCIALFB-181
1 parent cb2a441 commit bf18637

11 files changed

+171
-13
lines changed

spring-social-facebook/src/main/java/org/springframework/social/facebook/api/GraphApi.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ public interface GraphApi {
111111
*/
112112
String publish(String objectId, String connectionName, MultiValueMap<String, Object> data);
113113

114+
/**
115+
* Publishes data to an object.
116+
* Requires appropriate permission to publish to the object connection.
117+
* This differs from publish() only in that it doesn't attempt to extract the ID from the response.
118+
* This is because some publish operations do not return an ID in the response.
119+
* @param objectId the object ID to publish to.
120+
* @param data the data to publish to the object.
121+
*/
122+
void post(String objectId, MultiValueMap<String, Object> data);
123+
114124
/**
115125
* Publishes data to an object's connection.
116126
* Requires appropriate permission to publish to the object connection.
@@ -120,7 +130,7 @@ public interface GraphApi {
120130
* @param connectionName the connection name to publish to.
121131
* @param data the data to publish to the connection.
122132
*/
123-
void post(String objectId, String connectionName, MultiValueMap<String, String> data);
133+
void post(String objectId, String connectionName, MultiValueMap<String, Object> data);
124134

125135
/**
126136
* Deletes an object.

spring-social-facebook/src/main/java/org/springframework/social/facebook/api/MediaOperations.java

+13
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ public interface MediaOperations {
9393
*/
9494
String createAlbum(String name, String description);
9595

96+
/**
97+
* Creates a new photo album.
98+
* Requires "user_photos" permission.
99+
* @param ownerId the owner of the album, possibly a page ID.
100+
* @param name the name of the album.
101+
* @param description the album's description.
102+
* @return the ID of the newly created album.
103+
* @throws ApiException if there is an error while communicating with Facebook.
104+
* @throws InsufficientPermissionException if the user has not granted "user_photos" permission.
105+
* @throws MissingAuthorizationException if FacebookTemplate was not created with an access token.
106+
*/
107+
String createAlbum(String ownerId, String name, String description);
108+
96109
/**
97110
* Retrieves an album's image as an array of bytes. Returns the image in Facebook's "normal" type.
98111
* Requires "user_photos" permission if the album is not public.

spring-social-facebook/src/main/java/org/springframework/social/facebook/api/PageOperations.java

+14
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ public interface PageOperations {
3535
*/
3636
Page getPage(String pageId);
3737

38+
/**
39+
* Updates page information.
40+
* Requires that the application is granted "manage_pages" permission and that the authenticated user be an administrator of the page.
41+
* @param pageUpdate a {@link PageUpdate} object that carries the fields to update on the page.
42+
*/
43+
void updatePage(PageUpdate pageUpdate);
44+
3845
/**
3946
* Checks whether the logged-in user for this session is an admin of the page with the given page ID.
4047
* Requires "manage_pages" permission.
@@ -164,4 +171,11 @@ public interface PageOperations {
164171
*/
165172
Account getAccount(String pageId);
166173

174+
/**
175+
* Returns a {@link Facebook} instance that will act on behalf of the given page.
176+
* @param pageId the page to create a {@link Facebook} instance for.
177+
* @return a {@link Facebook} instance for the page.
178+
*/
179+
Facebook facebookOperations(String pageId);
180+
167181
}

spring-social-facebook/src/main/java/org/springframework/social/facebook/api/PagePostData.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/**
2323
* An object that represents a new post to be created.
2424
* Offers a builder-like way of creating a new post.
25-
* Given to {@link FeedOperations#post(PagePostData)}.
25+
* Given to {@link PageOperations#post(PagePostData)}.
2626
* @author Craig Walls
2727
*/
2828
public class PagePostData {
@@ -71,7 +71,7 @@ public PagePostData message(String message) {
7171
* @param picture A preview image associated with the link. May be null.
7272
* @param name Overwrites the title of the link preview. May be null.
7373
* @param caption Overwrites the caption of the link preview. May be null.
74-
* @description Overwrites the caption of hte link preview. May be null.
74+
* @param description Overwrites the caption of hte link preview. May be null.
7575
* @return the PagePostData object for additional configuration
7676
*/
7777
public PagePostData link(String linkUrl, String picture, String name, String caption, String description) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.social.facebook.api;
17+
18+
import org.springframework.util.LinkedMultiValueMap;
19+
import org.springframework.util.MultiValueMap;
20+
21+
/**
22+
* An object that captures data used to update a Page.
23+
* @author Craig Walls
24+
*/
25+
public class PageUpdate {
26+
27+
private final String pageId;
28+
29+
private String about;
30+
31+
private String bio;
32+
33+
private String cover;
34+
35+
private String name;
36+
37+
private Integer offsetX;
38+
39+
private Integer offsetY;
40+
41+
private Float zoomScaleX;
42+
43+
private Float zoomScaleY;
44+
45+
private Float focusX;
46+
47+
private Float focusY;
48+
49+
/**
50+
* Creates a new {@link PageUpdate}.
51+
* @param pageId The ID of the page to update.
52+
*/
53+
public PageUpdate(String pageId) {
54+
this.pageId = pageId;
55+
}
56+
57+
public String getPageId() {
58+
return pageId;
59+
}
60+
61+
public PageUpdate about(String about) {
62+
this.about = about;
63+
return this;
64+
}
65+
66+
public PageUpdate bio(String bio) {
67+
this.bio = bio;
68+
return this;
69+
}
70+
71+
public PageUpdate cover(String cover, Integer offsetX, Integer offsetY, Float zoomScaleX, Float zoomScaleY, Float focusX, Float focusY) {
72+
this.cover = cover;
73+
this.offsetX = offsetX;
74+
this.offsetY = offsetY;
75+
this.zoomScaleX = zoomScaleX;
76+
this.zoomScaleY = zoomScaleY;
77+
this.focusX = focusX;
78+
this.focusY = focusY;
79+
return this;
80+
}
81+
82+
public PageUpdate name(String name) {
83+
this.name = name;
84+
return this;
85+
}
86+
87+
public MultiValueMap<String, Object> toRequestParameters() {
88+
MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<String, Object>();
89+
if (about != null) { parameters.add("about", about); }
90+
if (bio != null) { parameters.add("bio", bio); }
91+
if (cover != null) { parameters.add("cover", cover); }
92+
if (name != null) { parameters.add("name", name); }
93+
if (offsetX != null) { parameters.add("offset_x", offsetX.toString()); }
94+
if (offsetY != null) { parameters.add("offset_y", offsetY.toString()); }
95+
if (zoomScaleX != null) { parameters.add("zoom_scale_x", zoomScaleX.toString()); }
96+
if (zoomScaleY != null) { parameters.add("zoom_scale_y", zoomScaleY.toString()); }
97+
if (focusX != null) { parameters.add("focus_x", focusX.toString()); }
98+
if (focusY != null) { parameters.add("focus_y", focusY.toString()); }
99+
return parameters;
100+
}
101+
102+
}

spring-social-facebook/src/main/java/org/springframework/social/facebook/api/PostData.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public PostData message(String message) {
7777
* @param picture A preview image associated with the link. May be null.
7878
* @param name Overwrites the title of the link preview. May be null.
7979
* @param caption Overwrites the caption of the link preview. May be null.
80-
* @description Overwrites the caption of hte link preview. May be null.
80+
* @param description Overwrites the caption of hte link preview. May be null.
8181
* @return the PagePostData object for additional configuration
8282
*/
8383
public PostData link(String linkUrl, String picture, String name, String caption, String description) {

spring-social-facebook/src/main/java/org/springframework/social/facebook/api/impl/AchievementTemplate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public AchievementType getAchievementType(String achievementTypeId) {
6464
}
6565

6666
public void createAchievementType(String achievementTypeUrl, int displayOrder) {
67-
MultiValueMap<String, String> data = new LinkedMultiValueMap<String, String>();
67+
MultiValueMap<String, Object> data = new LinkedMultiValueMap<String, Object>();
6868
data.set("achievement", achievementTypeUrl);
6969
data.set("display_order", String.valueOf(displayOrder));
7070
graphApi.post("app", "achievements", data);

spring-social-facebook/src/main/java/org/springframework/social/facebook/api/impl/EventTemplate.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,15 @@ public PagedList<EventInvitee> getDeclined(String eventId) {
109109
}
110110

111111
public void acceptInvitation(String eventId) {
112-
graphApi.post(eventId, "attending", new LinkedMultiValueMap<String, String>());
112+
graphApi.post(eventId, "attending", new LinkedMultiValueMap<String, Object>());
113113
}
114114

115115
public void maybeInvitation(String eventId) {
116-
graphApi.post(eventId, "maybe", new LinkedMultiValueMap<String, String>());
116+
graphApi.post(eventId, "maybe", new LinkedMultiValueMap<String, Object>());
117117
}
118118

119119
public void declineInvitation(String eventId) {
120-
graphApi.post(eventId, "declined", new LinkedMultiValueMap<String, String>());
120+
graphApi.post(eventId, "declined", new LinkedMultiValueMap<String, Object>());
121121
}
122122

123123
public PagedList<Event> search(String query) {

spring-social-facebook/src/main/java/org/springframework/social/facebook/api/impl/FacebookTemplate.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,14 @@ public String publish(String objectId, String connectionType, MultiValueMap<Stri
284284
return (String) response.get("id");
285285
}
286286

287-
public void post(String objectId, String connectionType, MultiValueMap<String, String> data) {
288-
URI uri = URIBuilder.fromUri(GRAPH_API_URL + objectId + "/" + connectionType).build();
289-
getRestTemplate().postForObject(uri, new LinkedMultiValueMap<String, String>(data), String.class);
287+
public void post(String objectId, MultiValueMap<String, Object> data) {
288+
post(objectId, null, data);
289+
}
290+
291+
public void post(String objectId, String connectionType, MultiValueMap<String, Object> data) {
292+
String connectionPath = connectionType != null ? "/" + connectionType : "";
293+
URI uri = URIBuilder.fromUri(GRAPH_API_URL + objectId + connectionPath).build();
294+
getRestTemplate().postForObject(uri, new LinkedMultiValueMap<String, Object>(data), String.class);
290295
}
291296

292297
public void delete(String objectId) {

spring-social-facebook/src/main/java/org/springframework/social/facebook/api/impl/LikeTemplate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public LikeTemplate(GraphApi graphApi) {
3232
}
3333

3434
public void like(String objectId) {
35-
graphApi.post(objectId, "likes", new LinkedMultiValueMap<String, String>());
35+
graphApi.post(objectId, "likes", new LinkedMultiValueMap<String, Object>());
3636
}
3737

3838
public void unlike(String objectId) {

spring-social-facebook/src/main/java/org/springframework/social/facebook/api/impl/PageTemplate.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,22 @@
2121

2222
import org.springframework.core.io.Resource;
2323
import org.springframework.social.facebook.api.Account;
24+
import org.springframework.social.facebook.api.Facebook;
2425
import org.springframework.social.facebook.api.FacebookLink;
2526
import org.springframework.social.facebook.api.GraphApi;
2627
import org.springframework.social.facebook.api.Page;
2728
import org.springframework.social.facebook.api.PageAdministrationException;
2829
import org.springframework.social.facebook.api.PageOperations;
2930
import org.springframework.social.facebook.api.PagePostData;
31+
import org.springframework.social.facebook.api.PageUpdate;
3032
import org.springframework.social.facebook.api.PagedList;
3133
import org.springframework.util.LinkedMultiValueMap;
3234
import org.springframework.util.MultiValueMap;
3335

3436
class PageTemplate implements PageOperations {
3537

3638
private final GraphApi graphApi;
37-
39+
3840
public PageTemplate(GraphApi graphApi) {
3941
this.graphApi = graphApi;
4042
}
@@ -43,6 +45,14 @@ public Page getPage(String pageId) {
4345
return graphApi.fetchObject(pageId, Page.class);
4446
}
4547

48+
public void updatePage(PageUpdate pageUpdate) {
49+
String pageId = pageUpdate.getPageId();
50+
String pageAccessToken = getAccessToken(pageId);
51+
MultiValueMap<String, Object> map = pageUpdate.toRequestParameters();
52+
map.add("access_token", pageAccessToken);
53+
graphApi.post(pageId, map);
54+
}
55+
4656
public boolean isPageAdmin(String pageId) {
4757
return getAccount(pageId) != null;
4858
}
@@ -119,6 +129,10 @@ public Account getAccount(String pageId) {
119129
}
120130
return accountCache.get(pageId);
121131
}
132+
133+
public Facebook facebookOperations(String pageId) {
134+
return new FacebookTemplate(getAccessToken(pageId));
135+
}
122136

123137
// private helper methods
124138

0 commit comments

Comments
 (0)