diff --git a/mirrortea/__init__.py b/mirrortea/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mirrortea/abstract_source_platform/telegram.py b/mirrortea/abstract_source_platform/telegram.py deleted file mode 100644 index 5c4e383..0000000 --- a/mirrortea/abstract_source_platform/telegram.py +++ /dev/null @@ -1,24 +0,0 @@ -import sys -import aiogram as telegram - -from abstract_source_platform.abstact_source_platform import ( - AbstractSourcePlatform, -) -from models.user import User - - -class Telegram(AbstractSourcePlatform): - def __init__(self, app): - self.app = app - self.bot = telegram.Bot(token=app.config.telegram_bot_token) - self.dispatcher = telegram.Dispatcher(bot=self.bot) - self.dispatcher.register_message_handler(self.on_message) - - async def run(self) -> None: - await self.dispatcher.start_polling() - - async def on_message(self, msg): - print(msg, file=sys.stderr) - - async def get_user_information(self) -> User: - pass diff --git a/mirrortea/matrix.py b/mirrortea/matrix.py deleted file mode 100644 index 061b4a3..0000000 --- a/mirrortea/matrix.py +++ /dev/null @@ -1,35 +0,0 @@ -import nio as matrix -import sys - - -class MatrixLoop: - def __init__(self, app): - self.app = app - self.client = matrix.AsyncClient( - app.config.matrix_homeserver_url, - app.config.matrix_full_bot_id, - ) - self.client.add_event_callback(self.on_message, matrix.RoomMessage) - - async def prepare(self): - await self.client.login(self.app.config.matrix_bot_password) - - async def finish(self): - await self.client.close() - - async def run(self): - await self.client.sync_forever(timeout=30000) - - async def on_message(self, room, event): - print(room, event, file=sys.stderr) - - def upgrade_room(self, room, telegram_nickname): - event_dict = matrix.event_builders.event_builder.EventBuilder( - name=telegram_nickname - ).as_dict() - client.room_send( - room_id=room, - message_type=event_dict["type"], - content=event_dict["content"], - ) # предположу что оно так работает - # https://matrix-nio.readthedocs.io/en/latest/nio.html#module-nio.event_builders.state_events diff --git a/mirrortea/models/matrix_room.py b/mirrortea/models/matrix_room.py new file mode 100644 index 0000000..420fb96 --- /dev/null +++ b/mirrortea/models/matrix_room.py @@ -0,0 +1,11 @@ +from typing import Optional +from pydantic import BaseModel + +from mirrortea.models.user import User + + +class MatrixRoom(BaseModel): + """""" + + matrix_room_id: str + connect_to_user: User diff --git a/mirrortea/models/user.py b/mirrortea/models/user.py index e8315e5..8c977a9 100644 --- a/mirrortea/models/user.py +++ b/mirrortea/models/user.py @@ -8,8 +8,8 @@ class User(BaseModel): """ first_name: str - last_named: Optional[str] + last_name: Optional[str] - id: str + user_id: str username: Optional[str] avatar_hash: Optional[str] diff --git a/mirrortea/repository/__init__.py b/mirrortea/repository/__init__.py index 53fd20a..9ee09a1 100644 --- a/mirrortea/repository/__init__.py +++ b/mirrortea/repository/__init__.py @@ -1,5 +1,5 @@ -from repository.sqlite_repository import ( +from .sqlite_repository import ( SQLiteDatabaseRepository, ) -repository = SQLiteDatabaseRepository() +repository = SQLiteDatabaseRepository diff --git a/mirrortea/repository/abstract_db_repository.py b/mirrortea/repository/abstract_db_repository.py index a0fd9fc..2b98deb 100644 --- a/mirrortea/repository/abstract_db_repository.py +++ b/mirrortea/repository/abstract_db_repository.py @@ -1,7 +1,34 @@ from abc import ABC, abstractmethod +from ..models.matrix_room import MatrixRoom +from ..models.user import User + class AbstractDatabaseRepository(ABC): @abstractmethod def create_tables(self) -> None: """Init tables in database""" + + @abstractmethod + def create_user(self, user: User) -> User: + """""" + + @abstractmethod + def get_user_by_id(self) -> User: + """""" + + @abstractmethod + def delete_user(self) -> None: + """""" + + @abstractmethod + def create_matrix_room(self) -> MatrixRoom: + """""" + + @abstractmethod + def get_matrix_room(self) -> MatrixRoom: + """""" + + @abstractmethod + def delete_matrix_room(self) -> None: + """""" diff --git a/mirrortea/repository/exceptions.py b/mirrortea/repository/exceptions.py new file mode 100644 index 0000000..65650fc --- /dev/null +++ b/mirrortea/repository/exceptions.py @@ -0,0 +1,14 @@ +class UserNotFoundError(Exception): + """User not found!""" + + +class UserAlreadyExistError(Exception): + """Token already exits!""" + + +class MatrixRoomNotFoundError(Exception): + """Matrix room not found!""" + + +class MatrixRoomAlreadyExistError(Exception): + """matrix room already exits!""" diff --git a/mirrortea/repository/sqlite_init_tables.py b/mirrortea/repository/sqlite_init_tables.py index be530c6..9bff2c2 100644 --- a/mirrortea/repository/sqlite_init_tables.py +++ b/mirrortea/repository/sqlite_init_tables.py @@ -1,27 +1,27 @@ -TELEGRAM_USER_MATRIX_CHATS_SQL = """ -CREATE TABLE IF NOT EXISTS telegram_user_matrix_chats ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - telegram_user_id INTEGER NOT NULL, - matrix_chat_id INTEGER NOT NULL, - FOREIGN KEY(telegram_user_id) REFERENCES telegram_users(id), - FOREIGN KEY(matrix_chat_id) REFERENCES matrix_chats(id) -); -""" +# PLATFORM_USER_MATRIX_CHATS_SQL = """ +# CREATE TABLE IF NOT EXISTS platforms_user_matrix_chats ( +# id INTEGER PRIMARY KEY AUTOINCREMENT, +# plstform_user_id INTEGER NOT NULL, +# matrix_chat_id INTEGER NOT NULL, +# FOREIGN KEY(platform_user_id) REFERENCES platforms_users(id), +# FOREIGN KEY(matrix_chat_id) REFERENCES matrix_chats(id) +# ); +# """ -TELEGRAM_USERS_SQL = """ -CREATE TABLE IF NOT EXISTS telegram_users +PLATFORMS_USERS_SQL = """ +CREATE TABLE IF NOT EXISTS platforms_users ( - id INTEGER PRIMARY KEY NOT NULL (15), - first_name TEXT NOT NULL (50), - last_name TEXT (50), - username TEXT (50), - avatar_hash BLOB, + user_id INTEGER(100) PRIMARY KEY NOT NULL, + first_name TEXT(100) NOT NULL, + last_name TEXT(100), + username TEXT(100), + avatar_hash BLOB ); """ MATRIX_ROOMS_SQL = """ -CREATE TABLE IF NOT EXISTS telegram_users +CREATE TABLE IF NOT EXISTS matrix_rooms ( - id TEXT PRIMARY KEY NOT NULL + matrix_room_id TEXT(100) PRIMARY KEY NOT NULL ); """ diff --git a/mirrortea/repository/sqlite_repository.py b/mirrortea/repository/sqlite_repository.py index 65ce56c..d8337b1 100644 --- a/mirrortea/repository/sqlite_repository.py +++ b/mirrortea/repository/sqlite_repository.py @@ -1,31 +1,103 @@ import sqlite3 -from pathlib import Path from typing import Optional +from collections import namedtuple -from repository.abstract_db_repository import AbstractDatabaseRepository +from mirrortea.repository.abstract_db_repository import AbstractDatabaseRepository -from repository.sqlite_init_tables import ( - TELEGRAM_USER_MATRIX_CHATS_SQL, - TELEGRAM_USERS_SQL, +from mirrortea.models.matrix_room import MatrixRoom +from mirrortea.models.user import User + +from mirrortea.repository.exceptions import ( + UserNotFoundError, + MatrixRoomAlreadyExistError, + MatrixRoomNotFoundError, + UserAlreadyExistError, +) + +from mirrortea.repository.sqlite_init_tables import ( + # PLATFORM_USER_MATRIX_CHATS_SQL, + PLATFORMS_USERS_SQL, MATRIX_ROOMS_SQL, ) TABLES_LIST = [ - TELEGRAM_USER_MATRIX_CHATS_SQL, - TELEGRAM_USERS_SQL, + # PLATFORM_USER_MATRIX_CHATS_SQL, + PLATFORMS_USERS_SQL, MATRIX_ROOMS_SQL, ] +PATH_TO_DB = "database.db" + class SQLiteDatabaseRepository(AbstractDatabaseRepository): - def __init__(self, app, path: Path): + def __init__(self, path=PATH_TO_DB): self.path = path - self.conn = sqlite3.connect(path) + self.conn = sqlite3.connect(self.path) + self.cursor = self.conn.cursor() + + def _namedtuple_factory(cursor, row): + fields = [column[0] for column in cursor.description] + cls = namedtuple("Row", fields) + return cls._make(row) - @staticmethod def create_tables(self) -> None: """Init tables in database""" for table in TABLES_LIST: self.conn.execute(table) self.conn.commit() + + def get_user_by_id(self, user_id: str) -> User: + """Get user by user id""" + + self.conn.row_factory = self._namedtuple_factory + self.cur_userdata = self.conn.execute( + "SELECT user_id AS user_id, first_name AS first_name, last_name AS last_name, username AS username, avatar_hash AS avatar_hash FROM platforms_users WHERE user_id = ?", + (user_id,), + ) + + self.userdata = self.cur_userdata.fetchone() + + if self.userdata == (): + return UserNotFoundError("User not found!") + + return User( + id=user_id, + first_name=self.userdata.user_id, + last_name=self.userdata.first_name, + username=self.userdata.username, + avatar_hash=self.userdata.avatar_hash, + ) + + def create_user(self, user: User) -> User: + """Create user""" + try: + self.cursor.execute( + "INSERT INTO platforms_users VALUES (?, ?, ?, ?, ?)", + ( + user.user_id, + user.first_name, + user.last_name, + user.username, + user.avatar_hash, + ), + ).fetchall() + + except sqlite3.IntegrityError: + return UserAlreadyExistError("User already exist!") + + self.conn.commit() + + return self.get_user_by_id(user_id=user.user_id) + + def delete_user(self) -> None: + """""" + + def create_matrix_room(self) -> MatrixRoom: + """""" + + def get_matrix_room(self) -> MatrixRoom: + """""" + + def delete_matrix_room(self) -> None: + """""" diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_repository.py b/tests/test_repository.py new file mode 100644 index 0000000..4cb36c6 --- /dev/null +++ b/tests/test_repository.py @@ -0,0 +1,39 @@ +import pytest +import os + +from mirrortea.models.user import User +from mirrortea.repository import repository + + +PATH = "tests/test_database.db" + + +def create_repository_object(): + return repository(PATH) + + +def test_create_tables(): + os.system("rm -rf " + PATH) + repo = create_repository_object() + assert repo.create_tables() is None + + +def test_get_user_by_id(): + repo = create_repository_object() + assert repo.get_user_by_id(user_id="123") is None + + +def test_create_user(): + repo = create_repository_object() + assert ( + repo.create_user( + User( + user_id="123", + first_name="dettlaff", + # last_name=None, + # username=None, + # avatar_hash=None, + ) + ) + is None + )