Merge pull request #400 from carpedm20/pull-delta-refactor
Move pull delta parsing into separate method
This commit is contained in:
@@ -2526,26 +2526,7 @@ class Client(object):
|
|||||||
self.seq = j.get("seq", "0")
|
self.seq = j.get("seq", "0")
|
||||||
return j
|
return j
|
||||||
|
|
||||||
def _parseMessage(self, content):
|
def _parseDelta(self, m):
|
||||||
"""Get message and author name from content. May contain multiple messages in the content."""
|
|
||||||
|
|
||||||
if "lb_info" in content:
|
|
||||||
self.sticky = content["lb_info"]["sticky"]
|
|
||||||
self.pool = content["lb_info"]["pool"]
|
|
||||||
|
|
||||||
if "batches" in content:
|
|
||||||
for batch in content["batches"]:
|
|
||||||
self._parseMessage(batch)
|
|
||||||
|
|
||||||
if "ms" not in content:
|
|
||||||
return
|
|
||||||
|
|
||||||
for m in content["ms"]:
|
|
||||||
mtype = m.get("type")
|
|
||||||
try:
|
|
||||||
# Things that directly change chat
|
|
||||||
if mtype == "delta":
|
|
||||||
|
|
||||||
def getThreadIdAndThreadType(msg_metadata):
|
def getThreadIdAndThreadType(msg_metadata):
|
||||||
"""Returns a tuple consisting of thread ID and thread type"""
|
"""Returns a tuple consisting of thread ID and thread type"""
|
||||||
id_thread = None
|
id_thread = None
|
||||||
@@ -2570,9 +2551,7 @@ class Client(object):
|
|||||||
|
|
||||||
# Added participants
|
# Added participants
|
||||||
if "addedParticipants" in delta:
|
if "addedParticipants" in delta:
|
||||||
added_ids = [
|
added_ids = [str(x["userFbId"]) for x in delta["addedParticipants"]]
|
||||||
str(x["userFbId"]) for x in delta["addedParticipants"]
|
|
||||||
]
|
|
||||||
thread_id = str(metadata["threadKey"]["threadFbId"])
|
thread_id = str(metadata["threadKey"]["threadFbId"])
|
||||||
self.onPeopleAdded(
|
self.onPeopleAdded(
|
||||||
mid=mid,
|
mid=mid,
|
||||||
@@ -2598,9 +2577,7 @@ class Client(object):
|
|||||||
|
|
||||||
# Color change
|
# Color change
|
||||||
elif delta_type == "change_thread_theme":
|
elif delta_type == "change_thread_theme":
|
||||||
new_color = graphql_color_to_enum(
|
new_color = graphql_color_to_enum(delta["untypedData"]["theme_color"])
|
||||||
delta["untypedData"]["theme_color"]
|
|
||||||
)
|
|
||||||
thread_id, thread_type = getThreadIdAndThreadType(metadata)
|
thread_id, thread_type = getThreadIdAndThreadType(metadata)
|
||||||
self.onColorChange(
|
self.onColorChange(
|
||||||
mid=mid,
|
mid=mid,
|
||||||
@@ -2733,8 +2710,7 @@ class Client(object):
|
|||||||
elif delta_class == "DeliveryReceipt":
|
elif delta_class == "DeliveryReceipt":
|
||||||
message_ids = delta["messageIds"]
|
message_ids = delta["messageIds"]
|
||||||
delivered_for = str(
|
delivered_for = str(
|
||||||
delta.get("actorFbId")
|
delta.get("actorFbId") or delta["threadKey"]["otherUserFbId"]
|
||||||
or delta["threadKey"]["otherUserFbId"]
|
|
||||||
)
|
)
|
||||||
ts = int(delta["deliveredWatermarkTimestampMs"])
|
ts = int(delta["deliveredWatermarkTimestampMs"])
|
||||||
thread_id, thread_type = getThreadIdAndThreadType(delta)
|
thread_id, thread_type = getThreadIdAndThreadType(delta)
|
||||||
@@ -2750,10 +2726,7 @@ class Client(object):
|
|||||||
|
|
||||||
# Message seen
|
# Message seen
|
||||||
elif delta_class == "ReadReceipt":
|
elif delta_class == "ReadReceipt":
|
||||||
seen_by = str(
|
seen_by = str(delta.get("actorFbId") or delta["threadKey"]["otherUserFbId"])
|
||||||
delta.get("actorFbId")
|
|
||||||
or delta["threadKey"]["otherUserFbId"]
|
|
||||||
)
|
|
||||||
seen_ts = int(delta["actionTimestampMs"])
|
seen_ts = int(delta["actionTimestampMs"])
|
||||||
delivered_ts = int(delta["watermarkTimestampMs"])
|
delivered_ts = int(delta["watermarkTimestampMs"])
|
||||||
thread_id, thread_type = getThreadIdAndThreadType(delta)
|
thread_id, thread_type = getThreadIdAndThreadType(delta)
|
||||||
@@ -2770,12 +2743,10 @@ class Client(object):
|
|||||||
# Messages marked as seen
|
# Messages marked as seen
|
||||||
elif delta_class == "MarkRead":
|
elif delta_class == "MarkRead":
|
||||||
seen_ts = int(
|
seen_ts = int(
|
||||||
delta.get("actionTimestampMs")
|
delta.get("actionTimestampMs") or delta.get("actionTimestamp")
|
||||||
or delta.get("actionTimestamp")
|
|
||||||
)
|
)
|
||||||
delivered_ts = int(
|
delivered_ts = int(
|
||||||
delta.get("watermarkTimestampMs")
|
delta.get("watermarkTimestampMs") or delta.get("watermarkTimestamp")
|
||||||
or delta.get("watermarkTimestamp")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
threads = []
|
threads = []
|
||||||
@@ -2787,11 +2758,7 @@ class Client(object):
|
|||||||
|
|
||||||
# thread_id, thread_type = getThreadIdAndThreadType(delta)
|
# thread_id, thread_type = getThreadIdAndThreadType(delta)
|
||||||
self.onMarkedSeen(
|
self.onMarkedSeen(
|
||||||
threads=threads,
|
threads=threads, seen_ts=seen_ts, ts=delivered_ts, metadata=delta, msg=m
|
||||||
seen_ts=seen_ts,
|
|
||||||
ts=delivered_ts,
|
|
||||||
metadata=delta,
|
|
||||||
msg=m,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Game played
|
# Game played
|
||||||
@@ -2852,9 +2819,7 @@ class Client(object):
|
|||||||
# User joined to group call
|
# User joined to group call
|
||||||
elif delta_type == "participant_joined_group_call":
|
elif delta_type == "participant_joined_group_call":
|
||||||
thread_id, thread_type = getThreadIdAndThreadType(metadata)
|
thread_id, thread_type = getThreadIdAndThreadType(metadata)
|
||||||
is_video_call = bool(
|
is_video_call = bool(int(delta["untypedData"]["group_call_type"]))
|
||||||
int(delta["untypedData"]["group_call_type"])
|
|
||||||
)
|
|
||||||
self.onUserJoinedCall(
|
self.onUserJoinedCall(
|
||||||
mid=mid,
|
mid=mid,
|
||||||
joined_id=author_id,
|
joined_id=author_id,
|
||||||
@@ -2886,12 +2851,8 @@ class Client(object):
|
|||||||
)
|
)
|
||||||
elif event_type == "update_vote":
|
elif event_type == "update_vote":
|
||||||
# User voted on group poll
|
# User voted on group poll
|
||||||
added_options = json.loads(
|
added_options = json.loads(delta["untypedData"]["added_option_ids"])
|
||||||
delta["untypedData"]["added_option_ids"]
|
removed_options = json.loads(delta["untypedData"]["removed_option_ids"])
|
||||||
)
|
|
||||||
removed_options = json.loads(
|
|
||||||
delta["untypedData"]["removed_option_ids"]
|
|
||||||
)
|
|
||||||
self.onPollVoted(
|
self.onPollVoted(
|
||||||
mid=mid,
|
mid=mid,
|
||||||
poll=poll,
|
poll=poll,
|
||||||
@@ -2994,9 +2955,7 @@ class Client(object):
|
|||||||
mid = i["messageId"]
|
mid = i["messageId"]
|
||||||
author_id = str(i["userId"])
|
author_id = str(i["userId"])
|
||||||
reaction = (
|
reaction = (
|
||||||
MessageReaction(i["reaction"])
|
MessageReaction(i["reaction"]) if i.get("reaction") else None
|
||||||
if i.get("reaction")
|
|
||||||
else None
|
|
||||||
)
|
)
|
||||||
add_reaction = not bool(i["action"])
|
add_reaction = not bool(i["action"])
|
||||||
if add_reaction:
|
if add_reaction:
|
||||||
@@ -3092,9 +3051,7 @@ class Client(object):
|
|||||||
for mention in parse_json(delta["data"]["prng"])
|
for mention in parse_json(delta["data"]["prng"])
|
||||||
]
|
]
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(
|
log.exception("An exception occured while reading attachments")
|
||||||
"An exception occured while reading attachments"
|
|
||||||
)
|
|
||||||
|
|
||||||
sticker = None
|
sticker = None
|
||||||
attachments = []
|
attachments = []
|
||||||
@@ -3105,9 +3062,7 @@ class Client(object):
|
|||||||
mercury = a["mercury"]
|
mercury = a["mercury"]
|
||||||
if mercury.get("blob_attachment"):
|
if mercury.get("blob_attachment"):
|
||||||
image_metadata = a.get("imageMetadata", {})
|
image_metadata = a.get("imageMetadata", {})
|
||||||
attach_type = mercury["blob_attachment"][
|
attach_type = mercury["blob_attachment"]["__typename"]
|
||||||
"__typename"
|
|
||||||
]
|
|
||||||
attachment = graphql_to_attachment(
|
attachment = graphql_to_attachment(
|
||||||
mercury["blob_attachment"]
|
mercury["blob_attachment"]
|
||||||
)
|
)
|
||||||
@@ -3122,9 +3077,7 @@ class Client(object):
|
|||||||
attachments.append(attachment)
|
attachments.append(attachment)
|
||||||
|
|
||||||
elif mercury.get("sticker_attachment"):
|
elif mercury.get("sticker_attachment"):
|
||||||
sticker = graphql_to_sticker(
|
sticker = graphql_to_sticker(mercury["sticker_attachment"])
|
||||||
mercury["sticker_attachment"]
|
|
||||||
)
|
|
||||||
|
|
||||||
elif mercury.get("extensible_attachment"):
|
elif mercury.get("extensible_attachment"):
|
||||||
attachment = graphql_to_extensible_attachment(
|
attachment = graphql_to_extensible_attachment(
|
||||||
@@ -3174,6 +3127,26 @@ class Client(object):
|
|||||||
else:
|
else:
|
||||||
self.onUnknownMesssageType(msg=m)
|
self.onUnknownMesssageType(msg=m)
|
||||||
|
|
||||||
|
def _parseMessage(self, content):
|
||||||
|
"""Get message and author name from content. May contain multiple messages in the content."""
|
||||||
|
|
||||||
|
if "lb_info" in content:
|
||||||
|
self.sticky = content["lb_info"]["sticky"]
|
||||||
|
self.pool = content["lb_info"]["pool"]
|
||||||
|
|
||||||
|
if "batches" in content:
|
||||||
|
for batch in content["batches"]:
|
||||||
|
self._parseMessage(batch)
|
||||||
|
|
||||||
|
if "ms" not in content:
|
||||||
|
return
|
||||||
|
|
||||||
|
for m in content["ms"]:
|
||||||
|
mtype = m.get("type")
|
||||||
|
try:
|
||||||
|
# Things that directly change chat
|
||||||
|
if mtype == "delta":
|
||||||
|
self._parseDelta(m)
|
||||||
# Inbox
|
# Inbox
|
||||||
elif mtype == "inbox":
|
elif mtype == "inbox":
|
||||||
self.onInbox(
|
self.onInbox(
|
||||||
|
Reference in New Issue
Block a user