diff --git a/fbchat/__init__.py b/fbchat/__init__.py index b1b3679..12a3470 100644 --- a/fbchat/__init__.py +++ b/fbchat/__init__.py @@ -16,7 +16,7 @@ from ._exception import FBchatException, FBchatFacebookError from ._session import Session from ._thread import ThreadLocation, ThreadColor, ThreadABC, Thread from ._user import TypingStatus, User, UserData, ActiveStatus -from ._group import Group +from ._group import Group, GroupData from ._page import Page, PageData from ._message import EmojiSize, MessageReaction, Mention, Message from ._attachment import Attachment, UnsentMessage, ShareAttachment diff --git a/fbchat/_client.py b/fbchat/_client.py index c37f785..fee1766 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -9,7 +9,7 @@ from . import _util, _graphql, _session from ._exception import FBchatException, FBchatFacebookError from ._thread import ThreadLocation, ThreadColor from ._user import TypingStatus, User, UserData, ActiveStatus -from ._group import Group +from ._group import Group, GroupData from ._page import Page, PageData from ._message import EmojiSize, MessageReaction, Mention, Message from ._attachment import Attachment @@ -265,7 +265,7 @@ class Client: (j,) = self.graphql_requests(_graphql.from_query(_graphql.SEARCH_GROUP, params)) return [ - Group._from_graphql(self.session, node) + GroupData._from_graphql(self.session, node) for node in j["viewer"]["groups"]["nodes"] ] @@ -293,7 +293,7 @@ class Client: rtn.append(UserData._from_graphql(self.session, node)) elif node["__typename"] == "MessageThread": # MessageThread => Group thread - rtn.append(Group._from_graphql(self.session, node)) + rtn.append(GroupData._from_graphql(self.session, node)) elif node["__typename"] == "Page": rtn.append(PageData._from_graphql(self.session, node)) elif node["__typename"] == "Group": @@ -495,7 +495,7 @@ class Client: entry = entry["message_thread"] if entry.get("thread_type") == "GROUP": _id = entry["thread_key"]["thread_fbid"] - rtn[_id] = Group._from_graphql(self.session, entry) + rtn[_id] = GroupData._from_graphql(self.session, entry) elif entry.get("thread_type") == "ONE_TO_ONE": _id = entry["thread_key"]["other_user_id"] if pages_and_users.get(_id) is None: @@ -549,7 +549,7 @@ class Client: for node in j["viewer"]["message_threads"]["nodes"]: _type = node.get("thread_type") if _type == "GROUP": - rtn.append(Group._from_graphql(self.session, node)) + rtn.append(GroupData._from_graphql(self.session, node)) elif _type == "ONE_TO_ONE": rtn.append(UserData._from_thread_fetch(self.session, node)) else: diff --git a/fbchat/_group.py b/fbchat/_group.py index d7e6b71..5972218 100644 --- a/fbchat/_group.py +++ b/fbchat/_group.py @@ -12,32 +12,9 @@ class Group(_thread.ThreadABC): session = attr.ib(type=_session.Session) #: The group's unique identifier. id = attr.ib(converter=str) - #: The group's picture - photo = attr.ib(None) - #: The name of the group - name = attr.ib(None) - #: Datetime when the group was last active / when the last message was sent - last_active = attr.ib(None) - #: Number of messages in the group - message_count = attr.ib(None) - #: Set `Plan` - plan = attr.ib(None) - #: Unique list (set) of the group thread's participant user IDs - participants = attr.ib(factory=set) - #: A dictionary, containing user nicknames mapped to their IDs - nicknames = attr.ib(factory=dict) - #: A `ThreadColor`. The groups's message color - color = attr.ib(None) - #: The groups's default emoji - emoji = attr.ib(None) - # Set containing user IDs of thread admins - admins = attr.ib(factory=set) - # True if users need approval to join - approval_mode = attr.ib(None) - # Set containing user IDs requesting to join - approval_requests = attr.ib(factory=set) - # Link for joining group - join_link = attr.ib(None) + + def _to_send_data(self): + return {"thread_fbid": self.id} def add_participants(self, user_ids: Iterable[str]): """Add users to the group. @@ -151,6 +128,41 @@ class Group(_thread.ThreadABC): """ self._users_approval(user_ids, False) + +@attrs_default +class GroupData(Group): + """Represents data about a Facebook group. + + Inherits `Group`, and implements `ThreadABC`. + """ + + #: The group's picture + photo = attr.ib(None) + #: The name of the group + name = attr.ib(None) + #: Datetime when the group was last active / when the last message was sent + last_active = attr.ib(None) + #: Number of messages in the group + message_count = attr.ib(None) + #: Set `Plan` + plan = attr.ib(None) + #: Unique list (set) of the group thread's participant user IDs + participants = attr.ib(factory=set) + #: A dictionary, containing user nicknames mapped to their IDs + nicknames = attr.ib(factory=dict) + #: A `ThreadColor`. The groups's message color + color = attr.ib(None) + #: The groups's default emoji + emoji = attr.ib(None) + # Set containing user IDs of thread admins + admins = attr.ib(factory=set) + # True if users need approval to join + approval_mode = attr.ib(None) + # Set containing user IDs requesting to join + approval_requests = attr.ib(factory=set) + # Link for joining group + join_link = attr.ib(None) + @classmethod def _from_graphql(cls, session, data): if data.get("image") is None: @@ -195,9 +207,6 @@ class Group(_thread.ThreadABC): plan=plan, ) - def _to_send_data(self): - return {"thread_fbid": self.id} - @attrs_default class NewGroup(_thread.ThreadABC): diff --git a/tests/test_group.py b/tests/test_group.py index c014e1d..5fc7135 100644 --- a/tests/test_group.py +++ b/tests/test_group.py @@ -1,4 +1,4 @@ -from fbchat._group import Group +from fbchat._group import GroupData def test_group_from_graphql(session): @@ -25,7 +25,7 @@ def test_group_from_graphql(session): "joinable_mode": {"mode": "0", "link": ""}, "event_reminders": {"nodes": []}, } - assert Group( + assert GroupData( session=session, id="11223344", photo=None, @@ -41,4 +41,4 @@ def test_group_from_graphql(session): approval_mode=False, approval_requests=set(), join_link="", - ) == Group._from_graphql(session, data) + ) == GroupData._from_graphql(session, data)