Move graphql_to_extensible_attachment into _message.py

This commit is contained in:
Mads Marquart
2019-03-10 19:33:58 +01:00
parent 53856a3622
commit 6693ec9c36
4 changed files with 27 additions and 25 deletions

View File

@@ -8,7 +8,7 @@ from random import choice
from bs4 import BeautifulSoup as bs
from mimetypes import guess_type
from collections import OrderedDict
from . import _file
from . import _file, _message
from ._util import *
from .models import *
from .graphql import *
@@ -3077,7 +3077,7 @@ class Client(object):
)
elif mercury.get("extensible_attachment"):
attachment = graphql_to_extensible_attachment(
attachment = _message.graphql_to_extensible_attachment(
mercury["extensible_attachment"]
)
if isinstance(attachment, UnsentMessage):

View File

@@ -3,7 +3,7 @@
from __future__ import unicode_literals
import json
import re
from . import _file
from . import _file, _message
from .models import *
from ._util import *
@@ -28,26 +28,6 @@ class ConcatJSONDecoder(json.JSONDecoder):
# End shameless copy
def graphql_to_extensible_attachment(a):
story = a.get("story_attachment")
if not story:
return None
target = story.get("target")
if not target:
return UnsentMessage(uid=a.get("legacy_attachment_id"))
_type = target["__typename"]
if _type == "MessageLocation":
return LocationAttachment._from_graphql(story)
elif _type == "MessageLiveLocation":
return LiveLocationAttachment._from_graphql(story)
elif _type in ["ExternalUrl", "Story"]:
return ShareAttachment._from_graphql(story)
return None
def graphql_to_quick_reply(q, is_response=False):
data = dict()
_type = q.get("content_type").lower()
@@ -113,7 +93,9 @@ def graphql_to_message(message):
graphql_to_quick_reply(quick_replies, is_response=True)
]
if message.get("extensible_attachment") is not None:
attachment = graphql_to_extensible_attachment(message["extensible_attachment"])
attachment = _message.graphql_to_extensible_attachment(
message["extensible_attachment"]
)
if isinstance(attachment, UnsentMessage):
rtn.unsent = True
elif attachment:

View File

@@ -3,6 +3,7 @@ from __future__ import unicode_literals
import attr
from string import Formatter
from . import _attachment, _location
from ._core import Enum
@@ -137,3 +138,23 @@ class Message(object):
message = cls(text=result, mentions=mentions)
return message
def graphql_to_extensible_attachment(data):
story = data.get("story_attachment")
if not story:
return None
target = story.get("target")
if not target:
return _attachment.UnsentMessage(uid=data.get("legacy_attachment_id"))
_type = target["__typename"]
if _type == "MessageLocation":
return _location.LocationAttachment._from_graphql(story)
elif _type == "MessageLiveLocation":
return _location.LiveLocationAttachment._from_graphql(story)
elif _type in ["ExternalUrl", "Story"]:
return _attachment.ShareAttachment._from_graphql(story)
return None

View File

@@ -8,7 +8,6 @@ from ._graphql import (
FLAGS,
WHITESPACE,
ConcatJSONDecoder,
graphql_to_extensible_attachment,
graphql_to_quick_reply,
graphql_to_message,
graphql_to_thread,