first commit
This commit is contained in:
BIN
client/models/chats/__pycache__/conversation.cpython-313.pyc
Normal file
BIN
client/models/chats/__pycache__/conversation.cpython-313.pyc
Normal file
Binary file not shown.
BIN
client/models/chats/__pycache__/message.cpython-313.pyc
Normal file
BIN
client/models/chats/__pycache__/message.cpython-313.pyc
Normal file
Binary file not shown.
BIN
client/models/chats/__pycache__/participant.cpython-313.pyc
Normal file
BIN
client/models/chats/__pycache__/participant.cpython-313.pyc
Normal file
Binary file not shown.
49
client/models/chats/conversation.py
Normal file
49
client/models/chats/conversation.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import sqlite3
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, List
|
||||
|
||||
@dataclass
|
||||
class ConversationModel:
|
||||
id: Optional[int] = None
|
||||
is_group: Optional[int] = None
|
||||
name: Optional[str] = None
|
||||
created_at: Optional[str] = None
|
||||
|
||||
class Conversations:
|
||||
def __init__(self, db_path="instance/app_database.db"):
|
||||
self.db_path = db_path
|
||||
self._create_tables()
|
||||
|
||||
def _create_tables(self):
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS conversations (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
is_group INTEGER DEFAULT 0,
|
||||
name TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
""")
|
||||
conn.commit()
|
||||
|
||||
def create(self, is_group: int, name: Optional[str] = None) -> int:
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"INSERT INTO conversations (is_group, name) VALUES (?, ?)",
|
||||
(is_group, name)
|
||||
)
|
||||
return cursor.lastrowid
|
||||
|
||||
def get_by_id(self, conv_id: int) -> Optional[ConversationModel]:
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
conn.row_factory = sqlite3.Row
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT * FROM conversations WHERE id = ?", (conv_id,))
|
||||
row = cursor.fetchone()
|
||||
return ConversationModel(**dict(row)) if row else None
|
||||
|
||||
def delete(self, conv_id: int):
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
conn.execute("DELETE FROM conversations WHERE id = ?", (conv_id,))
|
||||
57
client/models/chats/message.py
Normal file
57
client/models/chats/message.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import sqlite3
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, List
|
||||
|
||||
# --- DATACLASSES ---
|
||||
|
||||
@dataclass
|
||||
class MessageModel:
|
||||
id: Optional[int] = None
|
||||
conversation_id: Optional[int] = None
|
||||
sender_id: Optional[int] = None
|
||||
content: Optional[str] = None
|
||||
created_at: Optional[str] = None
|
||||
|
||||
class Messages:
|
||||
def __init__(self, db_path="instance/app_database.db"):
|
||||
self.db_path = db_path
|
||||
self._create_tables()
|
||||
|
||||
def _create_tables(self):
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
conversation_id INTEGER,
|
||||
sender_id INTEGER,
|
||||
content TEXT NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (conversation_id) REFERENCES conversations(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (sender_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
);
|
||||
""")
|
||||
conn.commit()
|
||||
|
||||
def send(self, conversation_id: int, sender_id: int, content: str) -> int:
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"INSERT INTO messages (conversation_id, sender_id, content) VALUES (?, ?, ?)",
|
||||
(conversation_id, sender_id, content)
|
||||
)
|
||||
return cursor.lastrowid
|
||||
|
||||
def get_history(self, conversation_id: int, limit: int = 50) -> List[MessageModel]:
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
conn.row_factory = sqlite3.Row
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"SELECT * FROM messages WHERE conversation_id = ? ORDER BY created_at DESC LIMIT ?",
|
||||
(conversation_id, limit)
|
||||
)
|
||||
return [MessageModel(**dict(row)) for row in cursor.fetchall()]
|
||||
|
||||
def delete_message(self, message_id: int):
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
conn.execute("DELETE FROM messages WHERE id = ?", (message_id,))
|
||||
49
client/models/chats/participant.py
Normal file
49
client/models/chats/participant.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import sqlite3
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, List
|
||||
|
||||
@dataclass
|
||||
class ParticipantModel:
|
||||
conversation_id: Optional[int] = None
|
||||
user_id: Optional[int] = None
|
||||
joined_at: Optional[str] = None
|
||||
|
||||
class Participants:
|
||||
def __init__(self, db_path="instance/app_database.db"):
|
||||
self.db_path = db_path
|
||||
self._create_tables()
|
||||
|
||||
def _create_tables(self):
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS participants (
|
||||
conversation_id INTEGER,
|
||||
user_id INTEGER,
|
||||
joined_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (conversation_id, user_id),
|
||||
FOREIGN KEY (conversation_id) REFERENCES conversations(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
);
|
||||
""")
|
||||
conn.commit()
|
||||
|
||||
def add_user(self, conversation_id: int, user_id: int):
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO participants (conversation_id, user_id) VALUES (?, ?)",
|
||||
(conversation_id, user_id)
|
||||
)
|
||||
|
||||
def get_conversation_members(self, conversation_id: int) -> List[int]:
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT user_id FROM participants WHERE conversation_id = ?", (conversation_id,))
|
||||
return [row[0] for row in cursor.fetchall()]
|
||||
|
||||
def remove_user(self, conversation_id: int, user_id: int):
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
conn.execute(
|
||||
"DELETE FROM participants WHERE conversation_id = ? AND user_id = ?",
|
||||
(conversation_id, user_id)
|
||||
)
|
||||
Reference in New Issue
Block a user