diff --git a/fbchat/_client.py b/fbchat/_client.py index 5fab73a..746ce68 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -763,7 +763,7 @@ class Client(object): GraphQL(query=GraphQL.SEARCH_GROUP, params={"search": name, "limit": limit}) ) - return [graphql_to_group(node) for node in j["viewer"]["groups"]["nodes"]] + return [Group._from_graphql(node) for node in j["viewer"]["groups"]["nodes"]] def searchForThreads(self, name, limit=10): """ @@ -788,7 +788,7 @@ class Client(object): rtn.append(User._from_graphql(node)) elif node["__typename"] == "MessageThread": # MessageThread => Group thread - rtn.append(graphql_to_group(node)) + rtn.append(Group._from_graphql(node)) elif node["__typename"] == "Page": rtn.append(graphql_to_page(node)) elif node["__typename"] == "Group": @@ -1049,7 +1049,7 @@ class Client(object): entry = entry["message_thread"] if entry.get("thread_type") == "GROUP": _id = entry["thread_key"]["thread_fbid"] - rtn[_id] = graphql_to_group(entry) + rtn[_id] = Group._from_graphql(entry) elif entry.get("thread_type") == "ONE_TO_ONE": _id = entry["thread_key"]["other_user_id"] if pages_and_users.get(_id) is None: diff --git a/fbchat/_graphql.py b/fbchat/_graphql.py index 69067df..0df4032 100644 --- a/fbchat/_graphql.py +++ b/fbchat/_graphql.py @@ -148,7 +148,7 @@ def graphql_to_message(message): def graphql_to_thread(thread): if thread["thread_type"] == "GROUP": - return graphql_to_group(thread) + return Group._from_graphql(thread) elif thread["thread_type"] == "ONE_TO_ONE": return User._from_thread_fetch(thread) else: @@ -159,46 +159,6 @@ def graphql_to_thread(thread): ) -def graphql_to_group(group): - if group.get("image") is None: - group["image"] = {} - c_info = Group._parse_customization_info(group) - last_message_timestamp = None - if "last_message" in group: - last_message_timestamp = group["last_message"]["nodes"][0]["timestamp_precise"] - plan = None - if group.get("event_reminders") and group["event_reminders"].get("nodes"): - plan = Plan._from_graphql(group["event_reminders"]["nodes"][0]) - - return Group( - group["thread_key"]["thread_fbid"], - participants=set( - [ - node["messaging_actor"]["id"] - for node in group["all_participants"]["nodes"] - ] - ), - nicknames=c_info.get("nicknames"), - color=c_info.get("color"), - emoji=c_info.get("emoji"), - admins=set([node.get("id") for node in group.get("thread_admins")]), - approval_mode=bool(group.get("approval_mode")) - if group.get("approval_mode") is not None - else None, - approval_requests=set( - node["requester"]["id"] for node in group["group_approval_queue"]["nodes"] - ) - if group.get("group_approval_queue") - else None, - join_link=group["joinable_mode"].get("link"), - photo=group["image"].get("uri"), - name=group.get("name"), - message_count=group.get("messages_count"), - last_message_timestamp=last_message_timestamp, - plan=plan, - ) - - def graphql_to_page(page): if page.get("profile_picture") is None: page["profile_picture"] = {} diff --git a/fbchat/_group.py b/fbchat/_group.py index 62cf020..5b01bec 100644 --- a/fbchat/_group.py +++ b/fbchat/_group.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals import attr +from . import _plan from ._thread import ThreadType, Thread @@ -60,6 +61,49 @@ class Group(Thread): self.approval_requests = approval_requests self.join_link = join_link + @classmethod + def _from_graphql(cls, data): + if data.get("image") is None: + data["image"] = {} + c_info = cls._parse_customization_info(data) + last_message_timestamp = None + if "last_message" in data: + last_message_timestamp = data["last_message"]["nodes"][0][ + "timestamp_precise" + ] + plan = None + if data.get("event_reminders") and data["event_reminders"].get("nodes"): + plan = _plan.Plan._from_graphql(data["event_reminders"]["nodes"][0]) + + return cls( + data["thread_key"]["thread_fbid"], + participants=set( + [ + node["messaging_actor"]["id"] + for node in data["all_participants"]["nodes"] + ] + ), + nicknames=c_info.get("nicknames"), + color=c_info.get("color"), + emoji=c_info.get("emoji"), + admins=set([node.get("id") for node in data.get("thread_admins")]), + approval_mode=bool(data.get("approval_mode")) + if data.get("approval_mode") is not None + else None, + approval_requests=set( + node["requester"]["id"] + for node in data["group_approval_queue"]["nodes"] + ) + if data.get("group_approval_queue") + else None, + join_link=data["joinable_mode"].get("link"), + photo=data["image"].get("uri"), + name=data.get("name"), + message_count=data.get("messages_count"), + last_message_timestamp=last_message_timestamp, + plan=plan, + ) + @attr.s(cmp=False, init=False) class Room(Group): diff --git a/fbchat/graphql.py b/fbchat/graphql.py index 0c27db1..b7f0253 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -14,7 +14,6 @@ from ._graphql import ( graphql_to_quick_reply, graphql_to_message, graphql_to_thread, - graphql_to_group, graphql_to_page, graphql_queries_to_json, graphql_response_to_json,