diff --git a/fbchat/_threads/_abc.py b/fbchat/_threads/_abc.py index 6dd6f56..92ce6b4 100644 --- a/fbchat/_threads/_abc.py +++ b/fbchat/_threads/_abc.py @@ -65,6 +65,16 @@ class ThreadABC(metaclass=abc.ABCMeta): # We won't support those use cases, it'll make for a confusing API! # If we absolutely need to in the future, we can always add extra functionality + @abc.abstractmethod + def _copy(self) -> "ThreadABC": + """It may or may not be a good idea to attach the current thread to new objects. + + So for now, we use this method to create a new thread. + + This should return the minimal representation of the thread (e.g. not UserData). + """ + raise NotImplementedError + def wave(self, first: bool = True) -> str: """Wave hello to the thread. @@ -289,9 +299,7 @@ class ThreadABC(metaclass=abc.ABCMeta): if not result: return (0, []) - # TODO: May or may not be a good idea to attach the current thread? - # For now, we just create a new thread: - thread = self.__class__(session=self.session, id=self.id) + thread = self._copy() snippets = [ _models.MessageSnippet._parse(thread, snippet) for snippet in result["snippets"] @@ -351,9 +359,7 @@ class ThreadABC(metaclass=abc.ABCMeta): read_receipts = j["message_thread"]["read_receipts"]["nodes"] - # TODO: May or may not be a good idea to attach the current thread? - # For now, we just create a new thread: - thread = self.__class__(session=self.session, id=self.id) + thread = self._copy() return [ _models.MessageData._from_graphql(thread, message, read_receipts) for message in j["message_thread"]["messages"]["nodes"] @@ -779,3 +785,6 @@ class Thread(ThreadABC): "The method you called is not supported on raw Thread objects." " Please use an appropriate User/Group/Page object instead!" ) + + def _copy(self) -> "Thread": + return Thread(session=self.session, id=self.id) diff --git a/fbchat/_threads/_group.py b/fbchat/_threads/_group.py index e8d0ec3..0020d6a 100644 --- a/fbchat/_threads/_group.py +++ b/fbchat/_threads/_group.py @@ -23,6 +23,9 @@ class Group(ThreadABC): def _to_send_data(self): return {"thread_fbid": self.id} + def _copy(self) -> "Group": + return Group(session=self.session, id=self.id) + def add_participants(self, user_ids: Iterable[str]): """Add users to the group. diff --git a/fbchat/_threads/_page.py b/fbchat/_threads/_page.py index a2c7429..daae1d9 100644 --- a/fbchat/_threads/_page.py +++ b/fbchat/_threads/_page.py @@ -23,6 +23,9 @@ class Page(ThreadABC): def _to_send_data(self): return {"other_user_fbid": self.id} + def _copy(self) -> "Page": + return Page(session=self.session, id=self.id) + @attrs_default class PageData(Page): diff --git a/fbchat/_threads/_user.py b/fbchat/_threads/_user.py index 297b890..e09a92f 100644 --- a/fbchat/_threads/_user.py +++ b/fbchat/_threads/_user.py @@ -55,6 +55,9 @@ class User(ThreadABC): "specific_to_list[0]": "fbid:{}".format(self.id), } + def _copy(self) -> "User": + return User(session=self.session, id=self.id) + def confirm_friend_request(self): """Confirm a friend request, adding the user to your friend list.