Skip to content

Commit

Permalink
Merge pull request #81 from roundrop/feature/comment_with_attachment
Browse files Browse the repository at this point in the history
Comment with attachment/replies support
  • Loading branch information
roundrop committed Nov 16, 2015
2 parents 750f06e + 91660f9 commit a117555
Show file tree
Hide file tree
Showing 25 changed files with 1,155 additions and 106 deletions.
28 changes: 28 additions & 0 deletions facebook4j-core/src/main/java/facebook4j/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package facebook4j;

import java.util.Date;
import java.util.List;

/**
* @author Ryuji Yamashita - roundrop at gmail.com
Expand All @@ -25,8 +26,35 @@ public interface Comment extends FacebookResponse {
String getId();
Category getFrom();
String getMessage();
List<Tag> getMessageTags();
Boolean canComment();
Boolean canRemove();
Boolean canHide();
Boolean canLike();
Date getCreatedTime();
Integer getLikeCount();
Integer getCommentCount();
Boolean isUserLikes();

Attachment getAttachment();

Comment getParent();

interface Attachment {
String getDescription();
AttachmentMedia getMedia();
AttachmentTarget getTarget();
String getTitle();
String getType();
String getUrl();

interface AttachmentMedia {
Image getImage();
}

interface AttachmentTarget {
String getId();
String getUrl();
}
}
}
135 changes: 135 additions & 0 deletions facebook4j-core/src/main/java/facebook4j/CommentUpdate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright 2012 Ryuji Yamashita
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package facebook4j;

import facebook4j.internal.http.HttpParameter;

import java.util.ArrayList;
import java.util.List;

public final class CommentUpdate implements java.io.Serializable {
private static final long serialVersionUID = -2155334084962409798L;

private String message;
private String attachmentId;
private String attachmentUrl;
private Media source;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public CommentUpdate message(String message) {
setMessage(message);
return this;
}

public String getAttachmentId() {
return attachmentId;
}

public void setAttachmentId(String attachmentId) {
this.attachmentId = attachmentId;
}

public CommentUpdate attachmentId(String attachmentId) {
setAttachmentId(attachmentId);
return this;
}

public String getAttachmentUrl() {
return attachmentUrl;
}

public void setAttachmentUrl(String attachmentUrl) {
this.attachmentUrl = attachmentUrl;
}

public CommentUpdate attachmentUrl(String attachmentUrl) {
setAttachmentUrl(attachmentUrl);
return this;
}

public Media getSource() {
return source;
}

public void setSource(Media source) {
this.source = source;
}

public CommentUpdate source(Media source) {
setSource(source);
return this;
}

/*package*/ HttpParameter[] asHttpParameterArray() {
List<HttpParameter> params = new ArrayList<HttpParameter>();
if (message != null) {
params.add(new HttpParameter("message", message));
}
if (attachmentId != null) {
params.add(new HttpParameter("attachment_id", attachmentId));
}
if (attachmentUrl != null) {
params.add(new HttpParameter("attachment_url", attachmentUrl));
}
if (source != null) {
params.add(source.asHttpParameter("source"));
}
return params.toArray(new HttpParameter[params.size()]);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CommentUpdate)) return false;

CommentUpdate that = (CommentUpdate) o;

if (attachmentId != null ? !attachmentId.equals(that.attachmentId) : that.attachmentId != null) return false;
if (attachmentUrl != null ? !attachmentUrl.equals(that.attachmentUrl) : that.attachmentUrl != null)
return false;
if (message != null ? !message.equals(that.message) : that.message != null) return false;
if (source != null ? !source.equals(that.source) : that.source != null) return false;

return true;
}

@Override
public int hashCode() {
int result = message != null ? message.hashCode() : 0;
result = 31 * result + (attachmentId != null ? attachmentId.hashCode() : 0);
result = 31 * result + (attachmentUrl != null ? attachmentUrl.hashCode() : 0);
result = 31 * result + (source != null ? source.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "CommentUpdate{" +
"message='" + message + '\'' +
", attachmentId='" + attachmentId + '\'' +
", attachmentUrl='" + attachmentUrl + '\'' +
", source=" + source +
'}';
}
}
45 changes: 43 additions & 2 deletions facebook4j-core/src/main/java/facebook4j/FacebookImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ public String commentAlbum(String albumId, String message) throws FacebookExcept
return _comment(albumId, message);
}

public String commentAlbum(String albumId, CommentUpdate commentUpdate) throws FacebookException {
return _comment(albumId, commentUpdate);
}

public ResponseList<Like> getAlbumLikes(String albumId) throws FacebookException {
return getAlbumLikes(albumId, null);
}
Expand Down Expand Up @@ -795,7 +799,11 @@ public String commentPost(String postId, String message) throws FacebookExceptio
ensureAuthorizationEnabled();
return _comment(postId, message);
}


public String commentPost(String postId, CommentUpdate commentUpdate) throws FacebookException {
return _comment(postId, commentUpdate);
}

public ResponseList<Like> getPostLikes(String postId) throws FacebookException {
return getPostLikes(postId, null);
}
Expand Down Expand Up @@ -1212,8 +1220,22 @@ public ResponseList<Like> getUserLikes(String userId, Reading reading) throws Fa
/* Comment Methods */

public Comment getComment(String commentId) throws FacebookException {
return factory.createComment(get(buildEndpoint(commentId)));
return getComment(commentId, null);
}

public Comment getComment(String commentId, Reading reading) throws FacebookException {
return factory.createComment(get(buildEndpoint(commentId, reading)));
}

public ResponseList<Comment> getCommentReplies(String commentId) throws FacebookException {
return getCommentReplies(commentId, null);
}

public ResponseList<Comment> getCommentReplies(String commentId, Reading reading) throws FacebookException {
ensureAuthorizationEnabled();
return factory.createCommentList(get(buildEndpoint(commentId, "comments", reading)));
}

public boolean deleteComment(String commentId) throws FacebookException {
ensureAuthorizationEnabled();
HttpResponse res = delete(buildEndpoint(commentId));
Expand Down Expand Up @@ -1273,6 +1295,10 @@ public String commentLink(String linkId, String message) throws FacebookExceptio
return _comment(linkId, message);
}

public String commentLink(String linkId, CommentUpdate commentUpdate) throws FacebookException {
return _comment(linkId, commentUpdate);
}

public ResponseList<Like> getLinkLikes(String linkId) throws FacebookException {
return getLinkLikes(linkId, null);
}
Expand Down Expand Up @@ -1890,6 +1916,10 @@ public String commentPhoto(String photoId, String message) throws FacebookExcept
return _comment(photoId, message);
}

public String commentPhoto(String photoId, CommentUpdate commentUpdate) throws FacebookException {
return _comment(photoId, commentUpdate);
}

public ResponseList<Like> getPhotoLikes(String photoId) throws FacebookException {
return getPhotoLikes(photoId, null);
}
Expand Down Expand Up @@ -2197,6 +2227,10 @@ public String commentVideo(String videoId, String message) throws FacebookExcept
return _comment(videoId, message);
}

public String commentVideo(String videoId, CommentUpdate commentUpdate) throws FacebookException {
return _comment(videoId, commentUpdate);
}

public URL getVideoCover(String videoId) throws FacebookException {
ensureAuthorizationEnabled();
return _getPictureURL(videoId, null);
Expand Down Expand Up @@ -2640,6 +2674,13 @@ private String _comment(String objectId, String message) throws FacebookExceptio
}
}

private String _comment(String objectId, CommentUpdate commentUpdate) throws FacebookException {
ensureAuthorizationEnabled();
JSONObject json = post(buildEndpoint(objectId, "comments"), commentUpdate.asHttpParameterArray())
.asJSONObject();
return getRawString("id", json);
}

private boolean _like(String objectId) throws FacebookException {
ensureAuthorizationEnabled();
HttpResponse res = post(buildEndpoint(objectId, "likes"));
Expand Down
28 changes: 28 additions & 0 deletions facebook4j-core/src/main/java/facebook4j/Image.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2012 Ryuji Yamashita
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package facebook4j;

import java.net.URL;

/**
* @author Ryuji Yamashita - roundrop at gmail.com
*/
public interface Image {
Integer getHeight();
Integer getWidth();
URL getSource();
}
9 changes: 2 additions & 7 deletions facebook4j-core/src/main/java/facebook4j/Photo.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface Photo extends FacebookResponse {
URL getSource();
Integer getHeight();
Integer getWidth();
List<Photo.Image> getImages();
List<Image> getImages();
URL getLink();
Place getPlace();
Date getCreatedTime();
Expand All @@ -42,10 +42,5 @@ public interface Photo extends FacebookResponse {
PagableList<Comment> getComments();
PagableList<Like> getLikes();
Category getAlbum();

interface Image {
Integer getHeight();
Integer getWidth();
URL getSource();
}

}
11 changes: 11 additions & 0 deletions facebook4j-core/src/main/java/facebook4j/api/AlbumMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import facebook4j.Album;
import facebook4j.AlbumUpdate;
import facebook4j.Comment;
import facebook4j.CommentUpdate;
import facebook4j.FacebookException;
import facebook4j.Like;
import facebook4j.Media;
Expand Down Expand Up @@ -179,6 +180,16 @@ public interface AlbumMethods {
*/
String commentAlbum(String albumId, String message) throws FacebookException;

/**
* Comments on the album.
* @param albumId the ID of a album
* @param commentUpdate comment content
* @return The new comment ID
* @throws FacebookException when Facebook service or network is unavailable
* @see <a href="https://developers.facebook.com/docs/reference/api/album/#comments">Album#comments - Facebook Developers</a>
*/
String commentAlbum(String albumId, CommentUpdate commentUpdate) throws FacebookException;


/**
* Returns likes made on the album.
Expand Down
29 changes: 29 additions & 0 deletions facebook4j-core/src/main/java/facebook4j/api/CommentMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,35 @@ public interface CommentMethods {
*/
Comment getComment(String commentId) throws FacebookException;

/**
* Returns a single comment.
* @param commentId the ID of a comment
* @param reading optional reading parameters. see <a href="https://developers.facebook.com/docs/reference/api/#reading">Graph API#reading - Facebook Developers</a>
* @return comment
* @throws FacebookException when Facebook service or network is unavailable
* @see <a href="https://developers.facebook.com/docs/reference/api/Comment/">Comment - Facebook Developers</a>
*/
Comment getComment(String commentId, Reading reading) throws FacebookException;

/**
* Returns the replies on a comment.
* @param commentId the ID of a comment
* @return replies
* @throws FacebookException when Facebook service or network is unavailable
* @see <a href="https://developers.facebook.com/docs/reference/api/Comment/">Comment - Facebook Developers</a>
*/
ResponseList<Comment> getCommentReplies(String commentId) throws FacebookException;

/**
* Returns the replies on a comment.
* @param commentId the ID of a comment
* @param reading optional reading parameters. see <a href="https://developers.facebook.com/docs/reference/api/#reading">Graph API#reading - Facebook Developers</a>
* @return replies
* @throws FacebookException when Facebook service or network is unavailable
* @see <a href="https://developers.facebook.com/docs/reference/api/Comment/">Comment - Facebook Developers</a>
*/
ResponseList<Comment> getCommentReplies(String commentId, Reading reading) throws FacebookException;

/**
* Deletes the comment.
* @param commentId the ID of a comment
Expand Down

0 comments on commit a117555

Please sign in to comment.