2022-01-22 07:12:24 +00:00
|
|
|
from .database import Database
|
2022-09-08 12:32:31 +00:00
|
|
|
from .attachment import Attachment
|
2022-01-22 07:12:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Message:
|
|
|
|
def __init__(self, sender, recipient, id, date, text, attachments=[]):
|
|
|
|
self.sender = sender
|
|
|
|
self.recipient = recipient
|
|
|
|
self.id = id
|
|
|
|
self.date = date
|
|
|
|
self.text = text
|
|
|
|
self.attachments = attachments
|
|
|
|
|
2022-09-08 12:32:31 +00:00
|
|
|
def to_database(self, db=None, exceptions=False):
|
2022-01-22 07:12:24 +00:00
|
|
|
try:
|
2022-09-08 12:32:31 +00:00
|
|
|
db = db or Database()
|
|
|
|
db.execute("INSERT INTO message VALUES (?, ?, ?, ?, ?);",
|
2022-01-22 07:12:24 +00:00
|
|
|
(self.id, self.sender, self.recipient, self.date, self.text))
|
|
|
|
for attachment in self.attachments:
|
2022-09-08 12:32:31 +00:00
|
|
|
attachment.to_database(db)
|
2022-01-22 07:12:24 +00:00
|
|
|
except:
|
|
|
|
if not exceptions:
|
|
|
|
return
|
|
|
|
raise
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_dict(cls, message):
|
|
|
|
if not "attachments" in message.keys():
|
|
|
|
message["attachments"] = []
|
|
|
|
return cls(message["from"], message["to"], message["id"], message["date"], message["text"], [Attachment.from_dict(attachment, message["id"]) for attachment in message["attachments"] if attachment["type"] == "IMAGE"])
|