From 64f55a572e76f5474c7fbe68859f487d82a0ae5b Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 8 Jan 2020 23:32:45 +0100 Subject: [PATCH] Move group-related functions to Group model --- fbchat/_client.py | 171 ---------------------------------------------- fbchat/_group.py | 113 ++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 171 deletions(-) diff --git a/fbchat/_client.py b/fbchat/_client.py index 8c6b564..3cbe7bd 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -1191,177 +1191,6 @@ class Client: ) return thread_id - def add_users_to_group(self, user_ids, thread_id=None): - """Add users to a group. - - Args: - user_ids (list): One or more user IDs to add - thread_id: Group ID to add people to. See :ref:`intro_threads` - - Raises: - FBchatException: If request failed - """ - data = Group(id=thread_id)._to_send_data() - - data["action_type"] = "ma-type:log-message" - data["log_message_type"] = "log:subscribe" - - user_ids = _util.require_list(user_ids) - - for i, user_id in enumerate(user_ids): - if user_id == self._session.user_id: - raise ValueError( - "Error when adding users: Cannot add self to group thread" - ) - else: - data[ - "log_message_data[added_participants][{}]".format(i) - ] = "fbid:{}".format(user_id) - - return self._do_send_request(data) - - def remove_user_from_group(self, user_id, thread_id=None): - """Remove user from a group. - - Args: - user_id: User ID to remove - thread_id: Group ID to remove people from. See :ref:`intro_threads` - - Raises: - FBchatException: If request failed - """ - data = {"uid": user_id, "tid": thread_id} - j = self._payload_post("/chat/remove_participants/", data) - - def _admin_status(self, admin_ids, admin, thread_id=None): - data = {"add": admin, "thread_fbid": thread_id} - - admin_ids = _util.require_list(admin_ids) - - for i, admin_id in enumerate(admin_ids): - data["admin_ids[{}]".format(i)] = str(admin_id) - - j = self._payload_post("/messaging/save_admins/?dpr=1", data) - - def add_group_admins(self, admin_ids, thread_id=None): - """Set specified users as group admins. - - Args: - admin_ids: One or more user IDs to set admin - thread_id: Group ID to remove people from. See :ref:`intro_threads` - - Raises: - FBchatException: If request failed - """ - self._admin_status(admin_ids, True, thread_id) - - def remove_group_admins(self, admin_ids, thread_id=None): - """Remove admin status from specified users. - - Args: - admin_ids: One or more user IDs to remove admin - thread_id: Group ID to remove people from. See :ref:`intro_threads` - - Raises: - FBchatException: If request failed - """ - self._admin_status(admin_ids, False, thread_id) - - def change_group_approval_mode(self, require_admin_approval, thread_id=None): - """Change group's approval mode. - - Args: - require_admin_approval: True or False - thread_id: Group ID to remove people from. See :ref:`intro_threads` - - Raises: - FBchatException: If request failed - """ - data = {"set_mode": int(require_admin_approval), "thread_fbid": thread_id} - j = self._payload_post("/messaging/set_approval_mode/?dpr=1", data) - - def _users_approval(self, user_ids, approve, thread_id=None): - user_ids = _util.require_list(user_ids) - - data = { - "client_mutation_id": "0", - "actor_id": self._session.user_id, - "thread_fbid": thread_id, - "user_ids": user_ids, - "response": "ACCEPT" if approve else "DENY", - "surface": "ADMIN_MODEL_APPROVAL_CENTER", - } - (j,) = self.graphql_requests( - _graphql.from_doc_id("1574519202665847", {"data": data}) - ) - - def accept_users_to_group(self, user_ids, thread_id=None): - """Accept users to the group from the group's approval. - - Args: - user_ids: One or more user IDs to accept - thread_id: Group ID to accept users to. See :ref:`intro_threads` - - Raises: - FBchatException: If request failed - """ - self._users_approval(user_ids, True, thread_id) - - def deny_users_from_group(self, user_ids, thread_id=None): - """Deny users from joining the group. - - Args: - user_ids: One or more user IDs to deny - thread_id: Group ID to deny users from. See :ref:`intro_threads` - - Raises: - FBchatException: If request failed - """ - self._users_approval(user_ids, False, thread_id) - - def _change_group_image(self, image_id, thread_id=None): - """Change a thread image from an image id. - - Args: - image_id: ID of uploaded image - thread_id: User/Group ID to change image. See :ref:`intro_threads` - - Raises: - FBchatException: If request failed - """ - data = {"thread_image_id": image_id, "thread_id": thread_id} - - j = self._payload_post("/messaging/set_thread_image/?dpr=1", data) - return image_id - - def change_group_image_remote(self, image_url, thread_id=None): - """Change a thread image from a URL. - - Args: - image_url: URL of an image to upload and change - thread_id: User/Group ID to change image. See :ref:`intro_threads` - - Raises: - FBchatException: If request failed - """ - ((image_id, mimetype),) = self._upload(_util.get_files_from_urls([image_url])) - return self._change_group_image(image_id, thread_id) - - def change_group_image_local(self, image_path, thread_id=None): - """Change a thread image from a local path. - - Args: - image_path: Path of an image to upload and change - thread_id: User/Group ID to change image. See :ref:`intro_threads` - - Raises: - FBchatException: If request failed - """ - with _util.get_files_from_paths([image_path]) as files: - ((image_id, mimetype),) = self._upload(files) - - return self._change_group_image(image_id, thread_id) - def change_thread_title(self, title, thread_id=None, thread_type=ThreadType.USER): """Change title of a thread. diff --git a/fbchat/_group.py b/fbchat/_group.py index e0c347a..e418a44 100644 --- a/fbchat/_group.py +++ b/fbchat/_group.py @@ -2,6 +2,7 @@ import attr from ._core import attrs_default, Image from . import _util, _session, _plan from ._thread import ThreadType, Thread +from typing import Iterable @attrs_default @@ -41,6 +42,118 @@ class Group(Thread): # Link for joining group join_link = attr.ib(None) + def add_participants(self, user_ids: Iterable[str]): + """Add users to the group. + + Args: + user_ids: One or more user IDs to add + """ + data = self._to_send_data() + + data["action_type"] = "ma-type:log-message" + data["log_message_type"] = "log:subscribe" + + for i, user_id in enumerate(user_ids): + if user_id == self.session.user_id: + raise ValueError( + "Error when adding users: Cannot add self to group thread" + ) + else: + data[ + "log_message_data[added_participants][{}]".format(i) + ] = "fbid:{}".format(user_id) + + return self.session._do_send_request(data) + + def remove_participant(self, user_id: str): + """Remove user from the group. + + Args: + user_id: User ID to remove + """ + data = {"uid": user_id, "tid": self.id} + j = self._payload_post("/chat/remove_participants/", data) + + def _admin_status(self, user_ids: Iterable[str], status: bool): + data = {"add": admin, "thread_fbid": self.id} + + for i, user_id in enumerate(user_ids): + data["admin_ids[{}]".format(i)] = str(user_id) + + j = self.session._payload_post("/messaging/save_admins/?dpr=1", data) + + def add_admins(self, user_ids: Iterable[str]): + """Set specified users as group admins. + + Args: + user_ids: One or more user IDs to set admin + """ + self._admin_status(user_ids, True) + + def remove_admins(self, user_ids: Iterable[str]): + """Remove admin status from specified users. + + Args: + user_ids: One or more user IDs to remove admin + """ + self._admin_status(user_ids, False) + + def set_title(self, title: str): + """Change title of the group. + + Args: + title: New title + """ + data = {"thread_name": title, "thread_id": self.id} + j = self.session._payload_post("/messaging/set_thread_name/?dpr=1", data) + + def set_image(self, image_id: str): + """Change the group image from an image id. + + Args: + image_id: ID of uploaded image + """ + data = {"thread_image_id": image_id, "thread_id": self.id} + j = self.session._payload_post("/messaging/set_thread_image/?dpr=1", data) + + def set_approval_mode(self, require_admin_approval: bool): + """Change the group's approval mode. + + Args: + require_admin_approval: True or False + """ + data = {"set_mode": int(require_admin_approval), "thread_fbid": thread_id} + j = self.session._payload_post("/messaging/set_approval_mode/?dpr=1", data) + + def _users_approval(self, user_ids: Iterable[str], approve: bool): + data = { + "client_mutation_id": "0", + "actor_id": self.session.user_id, + "thread_fbid": self.id, + "user_ids": list(user_ids), + "response": "ACCEPT" if approve else "DENY", + "surface": "ADMIN_MODEL_APPROVAL_CENTER", + } + (j,) = self.session._graphql_requests( + _graphql.from_doc_id("1574519202665847", {"data": data}) + ) + + def accept_users(self, user_ids: Iterable[str]): + """Accept users to the group from the group's approval. + + Args: + user_ids: One or more user IDs to accept + """ + self._users_approval(user_ids, True) + + def deny_users(self, user_ids: Iterable[str]): + """Deny users from joining the group. + + Args: + user_ids: One or more user IDs to deny + """ + self._users_approval(user_ids, False) + @classmethod def _from_graphql(cls, session, data): if data.get("image") is None: