Compare commits
2 commits
9d7df8202b
...
67856a849d
Author | SHA1 | Date | |
---|---|---|---|
def | 67856a849d | ||
def | 6ae22a36ef |
0
mirrortea/__init__.py
Normal file
0
mirrortea/__init__.py
Normal file
11
mirrortea/models/matrix_room.py
Normal file
11
mirrortea/models/matrix_room.py
Normal 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
|
|
@ -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]
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from repository.sqlite_repository import (
|
||||
from .sqlite_repository import (
|
||||
SQLiteDatabaseRepository,
|
||||
)
|
||||
|
||||
repository = SQLiteDatabaseRepository()
|
||||
repository = SQLiteDatabaseRepository
|
||||
|
|
|
@ -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:
|
||||
""""""
|
||||
|
|
14
mirrortea/repository/exceptions.py
Normal file
14
mirrortea/repository/exceptions.py
Normal 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!"""
|
|
@ -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
|
||||
);
|
||||
"""
|
||||
|
|
|
@ -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
0
tests/__init__.py
Normal file
39
tests/test_repository.py
Normal file
39
tests/test_repository.py
Normal 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
|
||||
)
|
Reference in a new issue