add: repository implementation

This commit is contained in:
def 2023-01-14 23:03:56 +04:00
parent 063e25e578
commit 6ae22a36ef
12 changed files with 195 additions and 91 deletions

0
mirrortea/__init__.py Normal file
View File

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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]

View File

@ -1,5 +1,5 @@
from repository.sqlite_repository import (
from .sqlite_repository import (
SQLiteDatabaseRepository,
)
repository = SQLiteDatabaseRepository()
repository = SQLiteDatabaseRepository

View File

@ -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:
""""""

View File

@ -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!"""

View File

@ -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
);
"""

View File

@ -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:
""""""

0
tests/__init__.py Normal file
View File

39
tests/test_repository.py Normal file
View File

@ -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
)