diff --git a/fbchat/_client.py b/fbchat/_client.py index 8d7cb8c..60a6754 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -8,7 +8,6 @@ from random import choice from bs4 import BeautifulSoup as bs from mimetypes import guess_type from collections import OrderedDict -from . import _file, _message from ._util import * from .models import * from .graphql import * @@ -3042,85 +3041,14 @@ class Client(object): # New message elif delta.get("class") == "NewMessage": - mentions = [] - if delta.get("data") and delta["data"].get("prng"): - try: - mentions = [ - Mention( - str(mention.get("i")), - offset=mention.get("o"), - length=mention.get("l"), - ) - for mention in parse_json(delta["data"]["prng"]) - ] - except Exception: - log.exception("An exception occured while reading attachments") - - sticker = None - attachments = [] - unsent = False - if delta.get("attachments"): - try: - for a in delta["attachments"]: - mercury = a["mercury"] - if mercury.get("blob_attachment"): - image_metadata = a.get("imageMetadata", {}) - attach_type = mercury["blob_attachment"]["__typename"] - attachment = _file.graphql_to_attachment( - mercury["blob_attachment"] - ) - - if attach_type in [ - "MessageFile", - "MessageVideo", - "MessageAudio", - ]: - # TODO: Add more data here for audio files - attachment.size = int(a["fileSize"]) - attachments.append(attachment) - - elif mercury.get("sticker_attachment"): - sticker = Sticker._from_graphql( - mercury["sticker_attachment"] - ) - - elif mercury.get("extensible_attachment"): - attachment = _message.graphql_to_extensible_attachment( - mercury["extensible_attachment"] - ) - if isinstance(attachment, UnsentMessage): - unsent = True - elif attachment: - attachments.append(attachment) - - except Exception: - log.exception( - "An exception occured while reading attachments: {}".format( - delta["attachments"] - ) - ) - - if metadata and metadata.get("tags"): - emoji_size = EmojiSize._from_tags(metadata.get("tags")) - - message = Message( - text=delta.get("body"), - mentions=mentions, - emoji_size=emoji_size, - sticker=sticker, - attachments=attachments, - ) - message.uid = mid - message.author = author_id - message.timestamp = ts - # message.reactions = {} - message.unsent = unsent thread_id, thread_type = getThreadIdAndThreadType(metadata) self.onMessage( mid=mid, author_id=author_id, message=delta.get("body", ""), - message_object=message, + message_object=Message._from_pull( + delta, tags=metadata.get("tags"), author=author_id, timestamp=ts + ), thread_id=thread_id, thread_type=thread_type, ts=ts, diff --git a/fbchat/_message.py b/fbchat/_message.py index 21eb5d3..b5a9423 100644 --- a/fbchat/_message.py +++ b/fbchat/_message.py @@ -4,7 +4,7 @@ from __future__ import unicode_literals import attr import json from string import Formatter -from . import _attachment, _location, _file, _quick_reply, _sticker +from . import _util, _attachment, _location, _file, _quick_reply, _sticker from ._core import Enum @@ -194,6 +194,71 @@ class Message(object): rtn.attachments.append(attachment) return rtn + @classmethod + def _from_pull(cls, data, mid=None, tags=None, author=None, timestamp=None): + rtn = cls(text=data.get("body")) + rtn.uid = mid + rtn.author = author + rtn.timestamp = timestamp + + if data.get("data") and data["data"].get("prng"): + try: + rtn.mentions = [ + Mention( + str(mention.get("i")), + offset=mention.get("o"), + length=mention.get("l"), + ) + for mention in _util.parse_json(data["data"]["prng"]) + ] + except Exception: + _util.log.exception("An exception occured while reading attachments") + + if data.get("attachments"): + try: + for a in data["attachments"]: + mercury = a["mercury"] + if mercury.get("blob_attachment"): + image_metadata = a.get("imageMetadata", {}) + attach_type = mercury["blob_attachment"]["__typename"] + attachment = _file.graphql_to_attachment( + mercury["blob_attachment"] + ) + + if attach_type in [ + "MessageFile", + "MessageVideo", + "MessageAudio", + ]: + # TODO: Add more data here for audio files + attachment.size = int(a["fileSize"]) + rtn.attachments.append(attachment) + + elif mercury.get("sticker_attachment"): + rtn.sticker = _sticker.Sticker._from_graphql( + mercury["sticker_attachment"] + ) + + elif mercury.get("extensible_attachment"): + attachment = graphql_to_extensible_attachment( + mercury["extensible_attachment"] + ) + if isinstance(attachment, _attachment.UnsentMessage): + rtn.unsent = True + elif attachment: + rtn.attachments.append(attachment) + + except Exception: + _util.log.exception( + "An exception occured while reading attachments: {}".format( + data["attachments"] + ) + ) + + rtn.emoji_size = EmojiSize._from_tags(tags) + + return rtn + def graphql_to_extensible_attachment(data): story = data.get("story_attachment")