From aeca4865ae8a3419c024129f6d03bffc9e4d80a8 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 9 Jan 2020 00:10:39 +0100 Subject: [PATCH] Add unfinished NewGroup helper class --- fbchat/_client.py | 28 ---------------------------- fbchat/_group.py | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/fbchat/_client.py b/fbchat/_client.py index 2a01d97..3328d7d 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -1158,34 +1158,6 @@ class Client: fb_error_message=j["error"], ) - def create_group(self, message, user_ids): - """Create a group with the given user ids. - - Args: - message: The initial message - user_ids: A list of users to create the group with. - - Returns: - ID of the new group - - Raises: - FBchatException: If request failed - """ - data = self._old_message(message)._to_send_data() - - if len(user_ids) < 2: - raise ValueError("Error when creating group: Not enough participants") - - for i, user_id in enumerate(user_ids + [self._session.user_id]): - data["specific_to_list[{}]".format(i)] = "fbid:{}".format(user_id) - - message_id, thread_id = self._do_send_request(data, get_thread_id=True) - if not thread_id: - raise FBchatException( - "Error when creating group: No thread_id could be found" - ) - return 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 d72eb04..ddaadc9 100644 --- a/fbchat/_group.py +++ b/fbchat/_group.py @@ -1,7 +1,7 @@ import attr from ._core import attrs_default, Image -from . import _util, _session, _plan, _thread -from typing import Iterable +from . import _util, _session, _plan, _thread, _user +from typing import Sequence, Iterable @attrs_default @@ -197,3 +197,32 @@ class Group(_thread.ThreadABC): def _to_send_data(self): return {"thread_fbid": self.id} + + +@attrs_default +class NewGroup(_thread.ThreadABC): + """Helper class to create new groups. + + TODO: Complete this! + + Construct this class with the desired users, and call a method like `wave`, to... + """ + + #: The session to use when making requests. + session = attr.ib(type=_session.Session) + #: The users that should be added to the group. + _users = attr.ib(type=Sequence[_user.User]) + + @property + def id(self): + raise NotImplementedError( + "The method you called is not supported on NewGroup objects." + " Please use the supported methods to create the group, before attempting" + " to call the method." + ) + + def _to_send_data(self) -> dict: + return { + "specific_to_list[{}]".format(i): "fbid:{}".format(user.id) + for i, user in enumerate(self._users) + }