Skip to content

Commit

Permalink
feat: 🎸 comments
Browse files Browse the repository at this point in the history
add support for manage facebook comment
  • Loading branch information
osikun committed Jul 14, 2023
1 parent 2fe7d62 commit 4eab237
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 1 deletion.
122 changes: 122 additions & 0 deletions pyfacebook/api/facebook/resource/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,125 @@ def get_batch(
comment_id: Comment.new_from_json_dict(item)
for comment_id, item in data.items()
}

def create_comment(
self,
object_id: str,
attachment_id: Optional[str] = None,
attachment_share_url: Optional[str] = None,
attachment_url: Optional[str] = None,
files: Optional[dict] = None,
message: Optional[str] = None,
fields: Optional[Union[str, list, dict]] = None,
return_json: bool = False,
**kwargs,
) -> Union[Comment, dict]:
"""
:param object_id: Object ID to create comment.
:param attachment_id: An optional ID of an unpublished photo.
:param attachment_share_url: The URL of a GIF to include as an animated GIF comment
:param attachment_url: The URL of an image to include as a photo comment.
:param files: A photo file, encoded as form data, to use as a photo comment.
:param message: The comment text.
:param fields: Fields for comment which created.
:param return_json: Set to false will return a dataclass for Photo.
Or return json data. Default is false.
:param kwargs: Additional parameters for different object
:return: Created comment.
"""
params = (
{"fields": enf_comma_separated(field="fields", value=fields)}
if fields
else None
)
data = {}
if attachment_id is not None:
data["attachment_id"] = attachment_id
elif attachment_share_url is not None:
data["attachment_share_url"] = attachment_share_url
elif attachment_url is not None:
data["attachment_url"] = attachment_url

if message is not None:
data["message"] = message

data = self.client.post_object(
object_id=object_id,
connection="comments",
params=params,
data=data,
files=files,
**kwargs,
)
if return_json:
return data
else:
return Comment.new_from_json_dict(data=data)

def update_comment(
self,
comment_id: str,
attachment_id: Optional[str] = None,
attachment_share_url: Optional[str] = None,
attachment_url: Optional[str] = None,
files: Optional[dict] = None,
message: Optional[str] = None,
is_hidden: Optional[bool] = None,
fields: Optional[Union[str, list, dict]] = None,
return_json: bool = False,
**kwargs,
) -> Union[Comment, dict]:
"""
:param comment_id: ID for comment which to update.
:param attachment_id: An optional ID of an unpublished photo.
:param attachment_share_url: The URL of a GIF to include as an animated GIF comment
:param attachment_url: The URL of an image to include as a photo comment.
:param files: A photo file, encoded as form data, to use as a photo comment.
:param message: The comment text.
:param is_hidden: Comment hidden status.
:param fields: Fields for comment which created.
:param return_json: Set to false will return a dataclass for Photo.
Or return json data. Default is false.
:param kwargs: Additional parameters for different object
:return:
"""
# If not point fields to read will return status data.
if fields:
params = {"fields": enf_comma_separated(field="fields", value=fields)}
else:
params, return_json = None, True

data = {}
if attachment_id is not None:
data["attachment_id"] = attachment_id
elif attachment_share_url is not None:
data["attachment_share_url"] = attachment_share_url
elif attachment_url is not None:
data["attachment_url"] = attachment_url

if message is not None:
data["message"] = message
if is_hidden is not None:
data["is_hidden"] = is_hidden

data = self.client.post_object(
object_id=comment_id,
params=params,
data=data,
files=files,
**kwargs,
)
if return_json:
return data
else:
return Comment.new_from_json_dict(data=data)

def delete_comment(self, comment_id: str):
"""
Delete a comment by using this endpoint
:param comment_id: ID for the comment.
:return: status for delete comment.
"""
data = self.client.delete_object(object_id=comment_id)
return data
10 changes: 9 additions & 1 deletion pyfacebook/api/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def _request(
url: str,
args: Optional[dict] = None,
post_args: Optional[dict] = None,
files: Optional[dict] = None,
verb: str = "GET",
auth_need: bool = True,
**kwargs,
Expand All @@ -176,8 +177,10 @@ def _request(
:param url: Resource url for Graph.
:param args: Query parameters.
:param post_args: Form parameters.
:param files: Dictionary of ``'filename': file-like-objects``
for multipart encoding upload.
:param verb: HTTP method
:param auth_need: Whether need access token.
:param auth_need: Whether request need access token.
:param kwargs: Additional parameters.
:return:
"""
Expand All @@ -197,6 +200,7 @@ def _request(
timeout=self.__timeout,
params=args,
data=post_args,
files=files,
proxies=self.proxies,
**kwargs,
)
Expand Down Expand Up @@ -434,6 +438,7 @@ def post_object(
connection: Optional[str] = None,
params: Optional[dict] = None,
data: Optional[dict] = None,
files: Optional[dict] = None,
**kwargs,
) -> dict:
"""
Expand All @@ -443,6 +448,8 @@ def post_object(
:param connection: Edge for the object.
:param params: Parameters for url path.
:param data: Parameters for Form data.
:param files: Dictionary of ``'filename': file-like-objects``
for multipart encoding upload.
:param kwargs: Additional parameters.
:return: Response data.
"""
Expand All @@ -454,6 +461,7 @@ def post_object(
url=path,
args=params,
post_args=data,
files=files,
verb="POST",
**kwargs,
)
Expand Down

0 comments on commit 4eab237

Please sign in to comment.