-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
61 lines (50 loc) · 2.03 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from dataclasses import dataclass
from dataURI import DataURI
@dataclass
class Profile:
displayName: str # non-empty string without spaces, must not start with # or @
fullName: str # suprisingly not optional
image: DataURI = None # optional field
from dataclasses import dataclass
@dataclass
class MessageContent:
type: str
content: dict
def __post_init__(self):
if self.type not in ["text", "link", "image", "file"]:
if "text" in self.content and self.content["text"] != "":
self.content["from"] = self.type
self.type = "text" # default to text type
else:
raise ValueError("Unrecognized type without a valid text property.")
@dataclass
class TextContent(MessageContent):
def __init__(self, text: str):
if text == "":
raise ValueError("The 'text' property cannot be an empty string.")
super().__init__(type="text", content={"text": text})
@dataclass
class LinkContent(MessageContent):
def __init__(self, text: str, preview: str = None):
if text == "":
raise ValueError("The 'text' property cannot be an empty string.")
super().__init__(type="link", content={"text": text, "preview": preview})
@dataclass
class ImageContent(MessageContent):
def __init__(self, text: str, image: str):
if image == "":
raise ValueError("The 'image' property cannot be empty.")
super().__init__(type="image", content={"text": text, "image": DataURI(image)})
@dataclass
class FileContent(MessageContent):
def __init__(self, text: str):
super().__init__(type="file", content={"text": text})
@dataclass
class MessageContainer:
content: MessageContent
file: str = None # placeholder for file invitations
quote: str = None # placeholder for quotes
forward: bool = False # placeholder for forward flag
def __post_init__(self):
if self.quote and self.forward:
raise ValueError("A message cannot have both a quote and a forward flag.")