(20241207) Started making data models for how things will be stored in the database (mongo).
This commit is contained in:
@@ -6,11 +6,11 @@
|
||||
|
||||
DATE:
|
||||
|
||||
Friday, 6th Dec., 2024.
|
||||
Saturday, 7th Dec., 2024.
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
To provide a structure to receive auth details of various chat apps (like Telegram and WhatsApp).
|
||||
To define how messages will be stored in the database.
|
||||
|
||||
REFERENCES:
|
||||
|
||||
@@ -36,13 +36,16 @@ sys.path.append(".")
|
||||
sys.path.append("..")
|
||||
|
||||
# For making data behaviour_models:
|
||||
from pydantic import BaseModel, Field, field_validator, PastDatetime
|
||||
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
|
||||
|
||||
@@ -54,8 +57,7 @@ import datetime
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# RegEx Patterns:
|
||||
REGEX_SESSION_TOKEN = r"^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$"
|
||||
# --- Nothing Yet
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
@@ -75,11 +77,58 @@ REGEX_SESSION_TOKEN = r"^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
class TelegramAuth(BaseModel):
|
||||
class CoreMessageModel(BaseModel):
|
||||
|
||||
botToken: str = Field(
|
||||
description = "the token granted by BotFather",
|
||||
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
|
||||
)
|
||||
|
||||
@@ -88,49 +137,23 @@ class TelegramAuth(BaseModel):
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
class ChatAuthRequestHeaders(BaseModel):
|
||||
|
||||
sessionToken: str = Field(
|
||||
description = "the session token of the user who is requesting the service",
|
||||
pattern = REGEX_SESSION_TOKEN,
|
||||
frozen = True,
|
||||
alias = "X-Session-Token"
|
||||
)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
def model_dump(self, *args, **kwargs):
|
||||
return super().model_dump(*args, by_alias = True, **kwargs)
|
||||
# ┓┏ ┓• ┓ •
|
||||
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
||||
|
||||
@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)
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
class ChatAuthRequestData(BaseModel):
|
||||
|
||||
chatClient: Literal["telegram", "whatsapp"] = Field(alias = "client")
|
||||
auth: Union[TelegramAuth]
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
@field_validator("tokenId", mode = "before")
|
||||
def parse_oid(cls, value):
|
||||
try: value = ObjectId(value)
|
||||
except: pass
|
||||
return value
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
@@ -141,5 +164,20 @@ class ChatAuthRequestData(BaseModel):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from utils_v2.string import json
|
||||
|
||||
pass
|
||||
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))
|
||||
|
||||
Reference in New Issue
Block a user