Add Message.session

This commit is contained in:
Mads Marquart
2020-01-09 00:51:47 +01:00
parent 53e4669fc1
commit 6b4327fa69
3 changed files with 27 additions and 13 deletions

View File

@@ -1524,8 +1524,12 @@ class Client:
i = d["deltaMessageReply"] i = d["deltaMessageReply"]
metadata = i["message"]["messageMetadata"] metadata = i["message"]["messageMetadata"]
thread_id, thread_type = get_thread_id_and_thread_type(metadata) thread_id, thread_type = get_thread_id_and_thread_type(metadata)
replied_to = Message._from_reply(i["repliedToMessage"]) replied_to = Message._from_reply(
message = Message._from_reply(i["message"], replied_to) self.session, i["repliedToMessage"]
)
message = Message._from_reply(
self.session, i["message"], replied_to
)
self.on_message( self.on_message(
mid=message.id, mid=message.id,
author_id=message.author, author_id=message.author,
@@ -1544,6 +1548,7 @@ class Client:
mid=mid, mid=mid,
author_id=author_id, author_id=author_id,
message_object=Message._from_pull( message_object=Message._from_pull(
self.session,
delta, delta,
mid=mid, mid=mid,
tags=metadata.get("tags"), tags=metadata.get("tags"),

View File

@@ -2,7 +2,7 @@ import attr
import json import json
from string import Formatter from string import Formatter
from ._core import log, attrs_default, Enum from ._core import log, attrs_default, Enum
from . import _util, _attachment, _location, _file, _quick_reply, _sticker from . import _util, _session, _attachment, _location, _file, _quick_reply, _sticker
class EmojiSize(Enum): class EmojiSize(Enum):
@@ -78,14 +78,18 @@ class Mention:
class Message: class Message:
"""Represents a Facebook message.""" """Represents a Facebook message."""
# TODO: Make these fields required!
#: The session to use when making requests.
session = attr.ib(None, type=_session.Session)
#: The message ID
id = attr.ib(None)
#: The actual message #: The actual message
text = attr.ib(None) text = attr.ib(None)
#: A list of `Mention` objects #: A list of `Mention` objects
mentions = attr.ib(factory=list) mentions = attr.ib(factory=list)
#: A `EmojiSize`. Size of a sent emoji #: A `EmojiSize`. Size of a sent emoji
emoji_size = attr.ib(None) emoji_size = attr.ib(None)
#: The message ID
id = attr.ib(None)
#: ID of the sender #: ID of the sender
author = attr.ib(None) author = attr.ib(None)
#: Datetime of when the message was sent #: Datetime of when the message was sent
@@ -119,7 +123,7 @@ class Message:
message_id: Message ID to fetch from message_id: Message ID to fetch from
""" """
message_info = thread._forced_fetch(message_id).get("message") message_info = thread._forced_fetch(message_id).get("message")
return Message._from_graphql(message_info) return Message._from_graphql(thread.session, message_info)
@classmethod @classmethod
def format_mentions(cls, text, *args, **kwargs): def format_mentions(cls, text, *args, **kwargs):
@@ -234,7 +238,7 @@ class Message:
return [] return []
@classmethod @classmethod
def _from_graphql(cls, data, read_receipts=None): def _from_graphql(cls, session, data, read_receipts=None):
if data.get("message_sender") is None: if data.get("message_sender") is None:
data["message_sender"] = {} data["message_sender"] = {}
if data.get("message") is None: if data.get("message") is None:
@@ -260,12 +264,13 @@ class Message:
replied_to = cls._from_graphql(data["replied_to_message"]["message"]) replied_to = cls._from_graphql(data["replied_to_message"]["message"])
return cls( return cls(
session=session,
id=str(data["message_id"]),
text=data["message"].get("text"), text=data["message"].get("text"),
mentions=[ mentions=[
Mention._from_range(m) for m in data["message"].get("ranges") or () Mention._from_range(m) for m in data["message"].get("ranges") or ()
], ],
emoji_size=EmojiSize._from_tags(tags), emoji_size=EmojiSize._from_tags(tags),
id=str(data["message_id"]),
author=str(data["message_sender"]["id"]), author=str(data["message_sender"]["id"]),
created_at=created_at, created_at=created_at,
is_read=not data["unread"] if data.get("unread") is not None else None, is_read=not data["unread"] if data.get("unread") is not None else None,
@@ -288,7 +293,7 @@ class Message:
) )
@classmethod @classmethod
def _from_reply(cls, data, replied_to=None): def _from_reply(cls, session, data, replied_to=None):
tags = data["messageMetadata"].get("tags") tags = data["messageMetadata"].get("tags")
metadata = data.get("messageMetadata", {}) metadata = data.get("messageMetadata", {})
@@ -315,13 +320,14 @@ class Message:
) )
return cls( return cls(
session=session,
id=metadata.get("messageId"),
text=data.get("body"), text=data.get("body"),
mentions=[ mentions=[
Mention._from_prng(m) Mention._from_prng(m)
for m in _util.parse_json(data.get("data", {}).get("prng", "[]")) for m in _util.parse_json(data.get("data", {}).get("prng", "[]"))
], ],
emoji_size=EmojiSize._from_tags(tags), emoji_size=EmojiSize._from_tags(tags),
id=metadata.get("messageId"),
author=str(metadata.get("actorFbId")), author=str(metadata.get("actorFbId")),
created_at=_util.millis_to_datetime(metadata.get("timestamp")), created_at=_util.millis_to_datetime(metadata.get("timestamp")),
sticker=sticker, sticker=sticker,
@@ -334,7 +340,9 @@ class Message:
) )
@classmethod @classmethod
def _from_pull(cls, data, mid=None, tags=None, author=None, created_at=None): def _from_pull(
cls, session, data, mid=None, tags=None, author=None, created_at=None
):
mentions = [] mentions = []
if data.get("data") and data["data"].get("prng"): if data.get("data") and data["data"].get("prng"):
try: try:
@@ -381,10 +389,11 @@ class Message:
) )
return cls( return cls(
session=session,
id=mid,
text=data.get("body"), text=data.get("body"),
mentions=mentions, mentions=mentions,
emoji_size=EmojiSize._from_tags(tags), emoji_size=EmojiSize._from_tags(tags),
id=mid,
author=author, author=author,
created_at=created_at, created_at=created_at,
sticker=sticker, sticker=sticker,

View File

@@ -270,7 +270,7 @@ class ThreadABC(metaclass=abc.ABCMeta):
read_receipts = j["message_thread"]["read_receipts"]["nodes"] read_receipts = j["message_thread"]["read_receipts"]["nodes"]
messages = [ messages = [
Message._from_graphql(message, read_receipts) Message._from_graphql(self.session, message, read_receipts)
for message in j["message_thread"]["messages"]["nodes"] for message in j["message_thread"]["messages"]["nodes"]
] ]
messages.reverse() messages.reverse()