From 3a5185fcc811b75ee4479e60b1a1bfacad12ebd4 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 10 Mar 2019 17:21:06 +0100 Subject: [PATCH] Move graphql_to_user -> User._from_graphql --- fbchat/_client.py | 6 +++--- fbchat/_graphql.py | 27 --------------------------- fbchat/_user.py | 28 ++++++++++++++++++++++++++++ fbchat/graphql.py | 1 - 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/fbchat/_client.py b/fbchat/_client.py index f6e4b6b..5fab73a 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -730,7 +730,7 @@ class Client(object): GraphQL(query=GraphQL.SEARCH_USER, params={"search": name, "limit": limit}) ) - return [graphql_to_user(node) for node in j[name]["users"]["nodes"]] + return [User._from_graphql(node) for node in j[name]["users"]["nodes"]] def searchForPages(self, name, limit=10): """ @@ -785,7 +785,7 @@ class Client(object): rtn = [] for node in j[name]["threads"]["nodes"]: if node["__typename"] == "User": - rtn.append(graphql_to_user(node)) + rtn.append(User._from_graphql(node)) elif node["__typename"] == "MessageThread": # MessageThread => Group thread rtn.append(graphql_to_group(node)) @@ -1056,7 +1056,7 @@ class Client(object): raise FBchatException("Could not fetch thread {}".format(_id)) entry.update(pages_and_users[_id]) if entry["type"] == ThreadType.USER: - rtn[_id] = graphql_to_user(entry) + rtn[_id] = User._from_graphql(entry) else: rtn[_id] = graphql_to_page(entry) else: diff --git a/fbchat/_graphql.py b/fbchat/_graphql.py index cb2dd0a..ab9b976 100644 --- a/fbchat/_graphql.py +++ b/fbchat/_graphql.py @@ -146,33 +146,6 @@ def graphql_to_message(message): return rtn -def graphql_to_user(user): - if user.get("profile_picture") is None: - user["profile_picture"] = {} - c_info = User._parse_customization_info(user) - plan = None - if user.get("event_reminders") and user["event_reminders"].get("nodes"): - plan = Plan._from_graphql(user["event_reminders"]["nodes"][0]) - - return User( - user["id"], - url=user.get("url"), - first_name=user.get("first_name"), - last_name=user.get("last_name"), - is_friend=user.get("is_viewer_friend"), - gender=GENDERS.get(user.get("gender")), - affinity=user.get("affinity"), - nickname=c_info.get("nickname"), - color=c_info.get("color"), - emoji=c_info.get("emoji"), - own_nickname=c_info.get("own_nickname"), - photo=user["profile_picture"].get("uri"), - name=user.get("name"), - message_count=user.get("messages_count"), - plan=plan, - ) - - def graphql_to_thread(thread): if thread["thread_type"] == "GROUP": return graphql_to_group(thread) diff --git a/fbchat/_user.py b/fbchat/_user.py index a42390f..3bb8015 100644 --- a/fbchat/_user.py +++ b/fbchat/_user.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals import attr from ._core import Enum +from . import _util, _plan from ._thread import ThreadType, Thread @@ -65,6 +66,33 @@ class User(Thread): self.color = color self.emoji = emoji + @classmethod + def _from_graphql(cls, data): + if data.get("profile_picture") is None: + data["profile_picture"] = {} + c_info = cls._parse_customization_info(data) + 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["id"], + url=data.get("url"), + first_name=data.get("first_name"), + last_name=data.get("last_name"), + is_friend=data.get("is_viewer_friend"), + gender=_util.GENDERS.get(data.get("gender")), + affinity=data.get("affinity"), + nickname=c_info.get("nickname"), + color=c_info.get("color"), + emoji=c_info.get("emoji"), + own_nickname=c_info.get("own_nickname"), + photo=data["profile_picture"].get("uri"), + name=data.get("name"), + message_count=data.get("messages_count"), + plan=plan, + ) + @attr.s(cmp=False) class ActiveStatus(object): diff --git a/fbchat/graphql.py b/fbchat/graphql.py index 7ccef05..0c27db1 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -13,7 +13,6 @@ from ._graphql import ( graphql_to_subattachment, graphql_to_quick_reply, graphql_to_message, - graphql_to_user, graphql_to_thread, graphql_to_group, graphql_to_page,