Move Mention parsing into the class itself

This commit is contained in:
Mads Marquart
2019-12-11 15:10:18 +01:00
parent 1f96c624e7
commit aaf26691d6

View File

@@ -53,6 +53,26 @@ class Mention:
#: The length of the mention #: The length of the mention
length = attr.ib(10) length = attr.ib(10)
@classmethod
def _from_range(cls, data):
return cls(
thread_id=data.get("entity", {}).get("id"),
offset=data.get("offset"),
length=data.get("length"),
)
@classmethod
def _from_prng(cls, data):
return cls(thread_id=data.get("i"), offset=data.get("o"), length=data.get("l"))
def _to_send_data(self, i):
return {
"profile_xmd[{}][id]".format(i): self.thread_id,
"profile_xmd[{}][offset]".format(i): self.offset,
"profile_xmd[{}][length]".format(i): self.length,
"profile_xmd[{}][type]".format(i): "p",
}
@attrs_default @attrs_default
class Message: class Message:
@@ -157,10 +177,7 @@ class Message:
data["body"] = self.text data["body"] = self.text
for i, mention in enumerate(self.mentions): for i, mention in enumerate(self.mentions):
data["profile_xmd[{}][id]".format(i)] = mention.thread_id data.update(mention._to_send_data(i))
data["profile_xmd[{}][offset]".format(i)] = mention.offset
data["profile_xmd[{}][length]".format(i)] = mention.length
data["profile_xmd[{}][type]".format(i)] = "p"
if self.emoji_size: if self.emoji_size:
if self.text: if self.text:
@@ -235,12 +252,7 @@ class Message:
return cls( return cls(
text=data["message"].get("text"), text=data["message"].get("text"),
mentions=[ mentions=[
Mention( Mention._from_range(m) for m in data["message"].get("ranges") or ()
thread_id=m.get("entity", {}).get("id"),
offset=m.get("offset"),
length=m.get("length"),
)
for m in data["message"].get("ranges") or ()
], ],
emoji_size=EmojiSize._from_tags(tags), emoji_size=EmojiSize._from_tags(tags),
uid=str(data["message_id"]), uid=str(data["message_id"]),
@@ -295,8 +307,8 @@ class Message:
return cls( return cls(
text=data.get("body"), text=data.get("body"),
mentions=[ mentions=[
Mention(thread_id=m.get("i"), offset=m.get("o"), length=m.get("l")) Mention._from_prng(m)
for m in json.loads(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),
uid=metadata.get("messageId"), uid=metadata.get("messageId"),
@@ -317,12 +329,8 @@ class Message:
if data.get("data") and data["data"].get("prng"): if data.get("data") and data["data"].get("prng"):
try: try:
mentions = [ mentions = [
Mention( Mention._from_prng(m)
thread_id=str(mention.get("i")), for m in _util.parse_json(data["data"]["prng"])
offset=mention.get("o"),
length=mention.get("l"),
)
for mention in _util.parse_json(data["data"]["prng"])
] ]
except Exception: except Exception:
log.exception("An exception occured while reading attachments") log.exception("An exception occured while reading attachments")