184 lines
6.6 KiB
Python
184 lines
6.6 KiB
Python
"""
|
|
|
|
AUTHOR:
|
|
|
|
Khushal P Soonderji
|
|
|
|
DATE:
|
|
|
|
Saturday, 7th Dec., 2024.
|
|
|
|
OBJECTIVE:
|
|
|
|
To define how messages will be stored in the database.
|
|
|
|
REFERENCES:
|
|
|
|
N/A
|
|
|
|
DOWNLOADS:
|
|
|
|
N/A
|
|
|
|
"""
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** IMPORT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# To make sibling directories accessible for imports:
|
|
import sys
|
|
sys.path.append(".")
|
|
sys.path.append("..")
|
|
|
|
# For making data behaviour_models:
|
|
from pydantic import BaseModel, Field, field_validator, PastDatetime, AwareDatetime
|
|
from typing import Optional, Literal, Union
|
|
|
|
# My utils:
|
|
from utils_v2.string import regex
|
|
from utils_v2.date_time import date_time
|
|
|
|
# To work with MongoDB:
|
|
from bson.objectid import ObjectId
|
|
|
|
# To work with date and time:
|
|
import datetime
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MACROS / ONE-TIME INIT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** VARIABLES ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** FUNCTIONS ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
class CoreMessageModel(BaseModel):
|
|
|
|
version: str = Field(
|
|
description = "a hint about the version no. of this message",
|
|
min_length = 1,
|
|
frozen = True,
|
|
default = "1.0.0"
|
|
)
|
|
|
|
ts: AwareDatetime = Field(
|
|
description = "the time (utc) at which this message was sent by the sender",
|
|
frozen = True
|
|
)
|
|
|
|
readTs: AwareDatetime = Field(
|
|
description = "the time (utc) at which this message was read and stored by your server",
|
|
frozen = True,
|
|
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
|
|
)
|
|
|
|
tokenId: ObjectId = Field(
|
|
description = "the id of the auth token that is associated with this message",
|
|
frozen = True
|
|
)
|
|
|
|
serviceType: Literal["email", "sms", "chat"] = Field(
|
|
description = "the kind of service this message was sent/received from",
|
|
frozen = True
|
|
)
|
|
|
|
client: Literal[
|
|
"gmail", "outlook", # ...................... Mail Clients
|
|
"telegram", "whatsapp", # .................. Chat Clients
|
|
"nimbusSmsIndia", "savvyBulkSmsKenya", # ... SMS Clients
|
|
] = Field(
|
|
description = "the third-part client that was used",
|
|
frozen = True
|
|
)
|
|
|
|
clientMessageId: str | int = Field(
|
|
description = "how the client identifies this message",
|
|
frozen = True
|
|
)
|
|
|
|
clientThreadId: str | int | None = Field(
|
|
description = "how the client identifies the chat/thread in which this message was sent/received",
|
|
frozen = True,
|
|
default = None
|
|
)
|
|
|
|
payload: dict = Field(
|
|
description = "the actual contents of the message; will differ for each client",
|
|
frozen = True
|
|
)
|
|
|
|
# ┏┓ ┏•
|
|
# ┃ ┏┓┏┓╋┓┏┓
|
|
# ┗┛┗┛┛┗┛┗┗┫
|
|
# ┛
|
|
|
|
class Config:
|
|
extra = "allow"
|
|
arbitrary_types_allowed = True
|
|
|
|
# ┓┏ ┓• ┓ •
|
|
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
|
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
|
|
|
@field_validator("ts", "readTs", mode = "before")
|
|
def parse_date_time(cls, value):
|
|
return date_time.parse_date_time(input_value = value, timezone = date_time.TIMEZONE_UTC)
|
|
|
|
@field_validator("tokenId", mode = "before")
|
|
def parse_oid(cls, value):
|
|
try: value = ObjectId(value)
|
|
except: pass
|
|
return value
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MAIN PROGRAM ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
if __name__ == "__main__":
|
|
from utils_v2.string import json
|
|
|
|
message = CoreMessageModel(
|
|
ts = date_time.get_current_utc_date_time(as_string = False),
|
|
tokenId = "67519cf3a7804fcbc6f12452",
|
|
serviceType = "email",
|
|
client = "gmail",
|
|
clientMessageId = 123,
|
|
clientThreadId = 456,
|
|
payload = {
|
|
"from": "bhopli@gmil.com",
|
|
"to": "hello@thecaoffice.com",
|
|
"message": "Hello, World!"
|
|
}
|
|
)
|
|
|
|
print("MESSAGE MODEL:", json.to_string(message.model_dump(), default = str))
|