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