From 60ebbd87d8d6dd367ab910b8386678b7473de864 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 10 Mar 2019 17:26:47 +0100 Subject: [PATCH] Move graphql_to_thread user parsing to User._from_thread_fetch --- fbchat/_graphql.py | 44 +------------------------------------------- fbchat/_user.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 43 deletions(-) diff --git a/fbchat/_graphql.py b/fbchat/_graphql.py index ab9b976..69067df 100644 --- a/fbchat/_graphql.py +++ b/fbchat/_graphql.py @@ -150,49 +150,7 @@ def graphql_to_thread(thread): if thread["thread_type"] == "GROUP": return graphql_to_group(thread) elif thread["thread_type"] == "ONE_TO_ONE": - if thread.get("big_image_src") is None: - thread["big_image_src"] = {} - c_info = User._parse_customization_info(thread) - participants = [ - node["messaging_actor"] for node in thread["all_participants"]["nodes"] - ] - user = next( - p for p in participants if p["id"] == thread["thread_key"]["other_user_id"] - ) - last_message_timestamp = None - if "last_message" in thread: - last_message_timestamp = thread["last_message"]["nodes"][0][ - "timestamp_precise" - ] - - first_name = user.get("short_name") - if first_name is None: - last_name = None - else: - last_name = user.get("name").split(first_name, 1).pop().strip() - - plan = None - if thread.get("event_reminders") and thread["event_reminders"].get("nodes"): - plan = Plan._from_graphql(thread["event_reminders"]["nodes"][0]) - - return User( - user["id"], - url=user.get("url"), - name=user.get("name"), - first_name=first_name, - last_name=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["big_image_src"].get("uri"), - message_count=thread.get("messages_count"), - last_message_timestamp=last_message_timestamp, - plan=plan, - ) + return User._from_thread_fetch(thread) else: raise FBchatException( "Unknown thread type: {}, with data: {}".format( diff --git a/fbchat/_user.py b/fbchat/_user.py index 3bb8015..45fb021 100644 --- a/fbchat/_user.py +++ b/fbchat/_user.py @@ -93,6 +93,52 @@ class User(Thread): plan=plan, ) + @classmethod + def _from_thread_fetch(cls, data): + if data.get("big_image_src") is None: + data["big_image_src"] = {} + c_info = cls._parse_customization_info(data) + participants = [ + node["messaging_actor"] for node in data["all_participants"]["nodes"] + ] + user = next( + p for p in participants if p["id"] == data["thread_key"]["other_user_id"] + ) + last_message_timestamp = None + if "last_message" in data: + last_message_timestamp = data["last_message"]["nodes"][0][ + "timestamp_precise" + ] + + first_name = user.get("short_name") + if first_name is None: + last_name = None + else: + last_name = user.get("name").split(first_name, 1).pop().strip() + + 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( + user["id"], + url=user.get("url"), + name=user.get("name"), + first_name=first_name, + last_name=last_name, + is_friend=user.get("is_viewer_friend"), + gender=_util.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["big_image_src"].get("uri"), + message_count=data.get("messages_count"), + last_message_timestamp=last_message_timestamp, + plan=plan, + ) + @attr.s(cmp=False) class ActiveStatus(object):