(20241212) Reorganizing code to perform core actions in one place.
This commit is contained in:
@@ -0,0 +1,250 @@
|
||||
"""
|
||||
|
||||
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, List, Any
|
||||
|
||||
# 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
|
||||
|
||||
# Data models:
|
||||
from models.core.ai.llm import LLMOutput
|
||||
|
||||
# To work with date and time:
|
||||
import datetime
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MACROS / ONE-TIME INIT ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# --- Nothing Yet
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** VARIABLES ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# --- Nothing Yet
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** FUNCTIONS ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
class CoreMessageModel(BaseModel):
|
||||
|
||||
messageId: ObjectId = Field(
|
||||
description = "the id of the document in mongodb that holds this information",
|
||||
frozen = True,
|
||||
default = None,
|
||||
alias = "_id"
|
||||
)
|
||||
|
||||
ts: AwareDatetime = Field(
|
||||
description = "the time (utc) at which this message was sent by the sender",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
syncTs: AwareDatetime = Field(
|
||||
description = "the time (utc) at which this message was pulled and stored in your server",
|
||||
frozen = True,
|
||||
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
|
||||
)
|
||||
|
||||
readTs: AwareDatetime | None = Field(
|
||||
description = "the time (utc) at which this message was read by the user",
|
||||
frozen = True,
|
||||
default = None
|
||||
)
|
||||
|
||||
markedAsUnread: bool = Field(
|
||||
description = "to note when the user has marked this message as unread",
|
||||
frozen = False,
|
||||
default = 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 | None = 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
|
||||
)
|
||||
|
||||
isSent: bool = Field(
|
||||
description = "to understand whether this message was an incoming message or outgoing message",
|
||||
frozen = False,
|
||||
default = False
|
||||
)
|
||||
|
||||
isBroadcast: bool = Field(
|
||||
description = "to understand if this message was broadcasted or sent one-to-one",
|
||||
frozen = True,
|
||||
default = False
|
||||
)
|
||||
|
||||
sentSuccessfully: bool | None = Field(
|
||||
description = "when a message is an outgoing message, this indicates if the message was send successfully",
|
||||
frozen = False,
|
||||
default = False
|
||||
)
|
||||
|
||||
aiSnippet: LLMOutput | None = Field(
|
||||
description = "holds a short summary generated by ",
|
||||
frozen = False
|
||||
)
|
||||
|
||||
preview: str = Field(
|
||||
description = "a truncated version of the actual textual content of the message",
|
||||
frozen = False
|
||||
)
|
||||
|
||||
message: dict = Field(
|
||||
description = "the actual contents of the message; will differ for each client",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
tags: List[Any] = Field(
|
||||
description = "a list of keywords to apply to this file/dir to filter it later",
|
||||
frozen = False,
|
||||
default = [],
|
||||
examples = ["urgent", "otp", "GST"]
|
||||
)
|
||||
|
||||
usedAi: bool | None = Field(
|
||||
description = "to mark when a sent message was generated by ai; null means the status is not known",
|
||||
frozen = True,
|
||||
default = None
|
||||
)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
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", "syncTs", "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:
|
||||
if isinstance(value, str):
|
||||
value = ObjectId(value)
|
||||
except: pass
|
||||
return value
|
||||
|
||||
@field_validator("tags", mode = "before")
|
||||
def validate_tags(cls, value):
|
||||
if value is None: value = []
|
||||
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,
|
||||
isSent = False,
|
||||
message = {
|
||||
"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