164 lines
5.7 KiB
Python
164 lines
5.7 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
|
|
)
|
|
|
|
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: str = Field(
|
|
description = "the third-part client that was used",
|
|
frozen = True,
|
|
examples = ["gmail", "outlook", "telegram", "whatsapp"]
|
|
)
|
|
|
|
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",
|
|
frozen = True
|
|
)
|
|
|
|
# ┏┓ ┏•
|
|
# ┃ ┏┓┏┓╋┓┏┓
|
|
# ┗┛┗┛┛┗┛┗┗┫
|
|
# ┛
|
|
|
|
class Config:
|
|
extra = "allow"
|
|
|
|
# ┓┏ ┓• ┓ •
|
|
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
|
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
|
|
|
@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__":
|
|
|
|
pass
|