Move graphql_to_message -> Message._from_graphql
This commit is contained in:
@@ -1102,7 +1102,7 @@ class Client(object):
|
||||
messages = list(
|
||||
reversed(
|
||||
[
|
||||
graphql_to_message(message)
|
||||
Message._from_graphql(message)
|
||||
for message in j["message_thread"]["messages"]["nodes"]
|
||||
]
|
||||
)
|
||||
@@ -1241,8 +1241,7 @@ class Client(object):
|
||||
"""
|
||||
thread_id, thread_type = self._getThread(thread_id, None)
|
||||
message_info = self._forcedFetch(thread_id, mid).get("message")
|
||||
message = graphql_to_message(message_info)
|
||||
return message
|
||||
return Message._from_graphql(message_info)
|
||||
|
||||
def fetchPollOptions(self, poll_id):
|
||||
"""
|
||||
|
@@ -28,60 +28,6 @@ class ConcatJSONDecoder(json.JSONDecoder):
|
||||
# End shameless copy
|
||||
|
||||
|
||||
def graphql_to_message(message):
|
||||
if message.get("message_sender") is None:
|
||||
message["message_sender"] = {}
|
||||
if message.get("message") is None:
|
||||
message["message"] = {}
|
||||
rtn = Message(
|
||||
text=message.get("message").get("text"),
|
||||
mentions=[
|
||||
Mention(
|
||||
m.get("entity", {}).get("id"),
|
||||
offset=m.get("offset"),
|
||||
length=m.get("length"),
|
||||
)
|
||||
for m in message.get("message").get("ranges", [])
|
||||
],
|
||||
emoji_size=EmojiSize._from_tags(message.get("tags_list")),
|
||||
sticker=Sticker._from_graphql(message.get("sticker")),
|
||||
)
|
||||
rtn.uid = str(message.get("message_id"))
|
||||
rtn.author = str(message.get("message_sender").get("id"))
|
||||
rtn.timestamp = message.get("timestamp_precise")
|
||||
rtn.unsent = False
|
||||
if message.get("unread") is not None:
|
||||
rtn.is_read = not message["unread"]
|
||||
rtn.reactions = {
|
||||
str(r["user"]["id"]): MessageReaction._extend_if_invalid(r["reaction"])
|
||||
for r in message.get("message_reactions")
|
||||
}
|
||||
if message.get("blob_attachments") is not None:
|
||||
rtn.attachments = [
|
||||
_file.graphql_to_attachment(attachment)
|
||||
for attachment in message["blob_attachments"]
|
||||
]
|
||||
if message.get("platform_xmd_encoded"):
|
||||
quick_replies = json.loads(message["platform_xmd_encoded"]).get("quick_replies")
|
||||
if isinstance(quick_replies, list):
|
||||
rtn.quick_replies = [
|
||||
_quick_reply.graphql_to_quick_reply(q) for q in quick_replies
|
||||
]
|
||||
elif isinstance(quick_replies, dict):
|
||||
rtn.quick_replies = [
|
||||
_quick_reply.graphql_to_quick_reply(quick_replies, is_response=True)
|
||||
]
|
||||
if message.get("extensible_attachment") is not None:
|
||||
attachment = _message.graphql_to_extensible_attachment(
|
||||
message["extensible_attachment"]
|
||||
)
|
||||
if isinstance(attachment, UnsentMessage):
|
||||
rtn.unsent = True
|
||||
elif attachment:
|
||||
rtn.attachments.append(attachment)
|
||||
return rtn
|
||||
|
||||
|
||||
def graphql_queries_to_json(*queries):
|
||||
"""
|
||||
Queries should be a list of GraphQL objects
|
||||
|
@@ -2,8 +2,9 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import attr
|
||||
import json
|
||||
from string import Formatter
|
||||
from . import _attachment, _location
|
||||
from . import _attachment, _location, _file, _quick_reply, _sticker
|
||||
from ._core import Enum
|
||||
|
||||
|
||||
@@ -139,6 +140,60 @@ class Message(object):
|
||||
message = cls(text=result, mentions=mentions)
|
||||
return message
|
||||
|
||||
@classmethod
|
||||
def _from_graphql(cls, data):
|
||||
if data.get("message_sender") is None:
|
||||
data["message_sender"] = {}
|
||||
if data.get("message") is None:
|
||||
data["message"] = {}
|
||||
rtn = cls(
|
||||
text=data["message"].get("text"),
|
||||
mentions=[
|
||||
Mention(
|
||||
m.get("entity", {}).get("id"),
|
||||
offset=m.get("offset"),
|
||||
length=m.get("length"),
|
||||
)
|
||||
for m in data["message"].get("ranges") or ()
|
||||
],
|
||||
emoji_size=EmojiSize._from_tags(data.get("tags_list")),
|
||||
sticker=_sticker.Sticker._from_graphql(data.get("sticker")),
|
||||
)
|
||||
rtn.uid = str(data["message_id"])
|
||||
rtn.author = str(data["message_sender"]["id"])
|
||||
rtn.timestamp = data.get("timestamp_precise")
|
||||
rtn.unsent = False
|
||||
if data.get("unread") is not None:
|
||||
rtn.is_read = not data["unread"]
|
||||
rtn.reactions = {
|
||||
str(r["user"]["id"]): MessageReaction._extend_if_invalid(r["reaction"])
|
||||
for r in data["message_reactions"]
|
||||
}
|
||||
if data.get("blob_attachments") is not None:
|
||||
rtn.attachments = [
|
||||
_file.graphql_to_attachment(attachment)
|
||||
for attachment in data["blob_attachments"]
|
||||
]
|
||||
if data.get("platform_xmd_encoded"):
|
||||
quick_replies = json.loads(data["platform_xmd_encoded"]).get(
|
||||
"quick_replies"
|
||||
)
|
||||
if isinstance(quick_replies, list):
|
||||
rtn.quick_replies = [
|
||||
_quick_reply.graphql_to_quick_reply(q) for q in quick_replies
|
||||
]
|
||||
elif isinstance(quick_replies, dict):
|
||||
rtn.quick_replies = [
|
||||
_quick_reply.graphql_to_quick_reply(quick_replies, is_response=True)
|
||||
]
|
||||
if data.get("extensible_attachment") is not None:
|
||||
attachment = graphql_to_extensible_attachment(data["extensible_attachment"])
|
||||
if isinstance(attachment, _attachment.UnsentMessage):
|
||||
rtn.unsent = True
|
||||
elif attachment:
|
||||
rtn.attachments.append(attachment)
|
||||
return rtn
|
||||
|
||||
|
||||
def graphql_to_extensible_attachment(data):
|
||||
story = data.get("story_attachment")
|
||||
|
@@ -8,7 +8,6 @@ from ._graphql import (
|
||||
FLAGS,
|
||||
WHITESPACE,
|
||||
ConcatJSONDecoder,
|
||||
graphql_to_message,
|
||||
graphql_queries_to_json,
|
||||
graphql_response_to_json,
|
||||
GraphQL,
|
||||
|
Reference in New Issue
Block a user