Skip to content

Commit

Permalink
Remove remaining traces of ORM
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Mar 16, 2019
1 parent 7c82580 commit 7c46bf4
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 50 deletions.
15 changes: 5 additions & 10 deletions mautrix_telegram/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import copy
import signal

from sqlalchemy import orm
import sqlalchemy as sql

from mautrix_appservice import AppService
Expand Down Expand Up @@ -73,13 +72,10 @@
log.debug(f"Initializing mautrix-telegram {__version__}")

db_engine = sql.create_engine(config["appservice.database"] or "sqlite:///mautrix-telegram.db")
db_factory = orm.sessionmaker(bind=db_engine)
db_session = orm.scoping.scoped_session(db_factory)
Base.metadata.bind = db_engine

session_container = AlchemySessionContainer(engine=db_engine, session=db_session,
table_base=Base, table_prefix="telethon_",
manage_tables=False)
session_container = AlchemySessionContainer(engine=db_engine, table_base=Base, session=False,
table_prefix="telethon_", manage_tables=False)
session_container.core_mode = True

try:
Expand All @@ -102,8 +98,9 @@
aiohttp_params={
"client_max_size": config["appservice.max_body_size"] * mebibyte
})

context = Context(appserv, db_session, config, loop, session_container)
bot = init_bot(config)
context = Context(appserv, config, loop, session_container, bot)
context.mx = MatrixHandler(context)

if config["appservice.public.enabled"]:
public_website = PublicBridgeWebsite(loop)
Expand All @@ -120,8 +117,6 @@
start_ts = time()
init_db(db_engine)
init_abstract_user(context)
context.bot = init_bot(context)
context.mx = MatrixHandler(context)
init_formatter(context)
init_portal(context)
startup_actions = (init_puppet(context) +
Expand Down
11 changes: 3 additions & 8 deletions mautrix_telegram/abstract_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import logging
import platform

from sqlalchemy import orm
from telethon.tl.patched import MessageService, Message
from telethon.tl.types import (
Channel, ChannelForbidden, Chat, ChatForbidden, MessageActionChannelMigrateFrom, PeerUser,
Expand Down Expand Up @@ -56,7 +55,6 @@ class AbstractUser(ABC):
session_container = None # type: AlchemySessionContainer
loop = None # type: asyncio.AbstractEventLoop
log = None # type: logging.Logger
db = None # type: orm.Session
az = None # type: AppService
bot = None # type: Bot
ignore_incoming_bot_events = True # type: bool
Expand Down Expand Up @@ -175,11 +173,8 @@ async def start(self, delete_unless_authenticated: bool = False) -> 'AbstractUse
async def ensure_started(self, even_if_no_session=False) -> 'AbstractUser':
if not self.puppet_whitelisted or self.connected:
return self
session_count = self.session_container.Session.query.filter(
self.session_container.Session.session_id == self.mxid).count()
self.log.debug("ensure_started(%s, even_if_no_session=%s, session_count=%s)",
self.mxid, even_if_no_session, session_count)
if even_if_no_session or session_count > 0:
self.log.debug("ensure_started(%s, even_if_no_session=%s)", self.mxid, even_if_no_session)
if even_if_no_session or self.session_container.has_session(self.mxid):
await self.start(delete_unless_authenticated=not even_if_no_session)
return self

Expand Down Expand Up @@ -388,7 +383,7 @@ async def update_message(self, original_update: UpdateMessage) -> None:

def init(context: "Context") -> None:
global config, MAX_DELETIONS
AbstractUser.az, AbstractUser.db, config, AbstractUser.loop, AbstractUser.relaybot = context.core
AbstractUser.az, config, AbstractUser.loop, AbstractUser.relaybot = context.core
AbstractUser.ignore_incoming_bot_events = config["bridge.relaybot.ignore_own_incoming_events"]
AbstractUser.session_container = context.session_container
MAX_DELETIONS = config.get("bridge.max_telegram_delete", 10)
7 changes: 4 additions & 3 deletions mautrix_telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __init__(self, token: str) -> None:
self.username = None # type: str
self.is_relaybot = True # type: bool
self.is_bot = True # type: bool
self.chats = {chat.id: chat.type for chat in BotChat.all()} # type: Dict[int, str]
self.chats = {} # type: Dict[int, str]
self.tg_whitelist = [] # type: List[int]
self.whitelist_group_admins = (config["bridge.relaybot.whitelist_group_admins"]
or False) # type: bool
Expand All @@ -74,6 +74,7 @@ async def init_permissions(self) -> None:
self.tg_whitelist.append(user_id)

async def start(self, delete_unless_authenticated: bool = False) -> 'Bot':
self.chats = {chat.id: chat.type for chat in BotChat.all()}
await super().start(delete_unless_authenticated)
if not await self.is_logged_in():
await self.client.sign_in(bot_token=self.token)
Expand Down Expand Up @@ -280,9 +281,9 @@ def name(self) -> str:
return "bot"


def init(context: 'Context') -> Optional[Bot]:
def init(cfg: 'Config') -> Optional[Bot]:
global config
config = context.config
config = cfg
token = config["telegram.bot_token"]
if token and not token.lower().startswith("disable"):
return Bot(token)
Expand Down
2 changes: 1 addition & 1 deletion mautrix_telegram/commands/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class CommandProcessor:
log = logging.getLogger("mau.commands")

def __init__(self, context: c.Context) -> None:
self.az, self.db, self.config, self.loop, self.tgbot = context.core
self.az, self.config, self.loop, self.tgbot = context.core
self.public_website = context.public_website
self.command_prefix = self.config["bridge.command_prefix"]

Expand Down
21 changes: 8 additions & 13 deletions mautrix_telegram/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
if TYPE_CHECKING:
import asyncio

from sqlalchemy.orm import scoped_session

from alchemysession import AlchemySessionContainer
from mautrix_appservice import AppService

Expand All @@ -31,20 +29,17 @@


class Context:
def __init__(self, az: 'AppService', db: 'scoped_session', config: 'Config',
loop: 'asyncio.AbstractEventLoop', session_container: 'AlchemySessionContainer'
) -> None:
def __init__(self, az: 'AppService', config: 'Config', loop: 'asyncio.AbstractEventLoop',
session_container: 'AlchemySessionContainer', bot: Optional['Bot']) -> None:
self.az = az # type: AppService
self.db = db # type: scoped_session
self.config = config # type: Config
self.loop = loop # type: asyncio.AbstractEventLoop
self.bot = None # type: Optional[Bot]
self.mx = None # type: MatrixHandler
self.bot = bot # type: Optional[Bot]
self.mx = None # type: Optional[MatrixHandler]
self.session_container = session_container # type: AlchemySessionContainer
self.public_website = None # type: PublicBridgeWebsite
self.provisioning_api = None # type: ProvisioningAPI
self.public_website = None # type: Optional[PublicBridgeWebsite]
self.provisioning_api = None # type: Optional[ProvisioningAPI]

@property
def core(self) -> Tuple['AppService', 'scoped_session', 'Config',
'asyncio.AbstractEventLoop', Optional['Bot']]:
return (self.az, self.db, self.config, self.loop, self.bot)
def core(self) -> Tuple['AppService', 'Config', 'asyncio.AbstractEventLoop', Optional['Bot']]:
return self.az, self.config, self.loop, self.bot
3 changes: 1 addition & 2 deletions mautrix_telegram/db/telegram_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from sqlalchemy import Column, ForeignKey, Integer, BigInteger, String, Boolean
from sqlalchemy.orm import relationship
from typing import Optional

from .base import Base
Expand All @@ -33,7 +32,7 @@ class TelegramFile(Base):
width = Column(Integer, nullable=True)
height = Column(Integer, nullable=True)
thumbnail_id = Column("thumbnail", String, ForeignKey("telegram_file.id"), nullable=True)
thumbnail = relationship("TelegramFile", uselist=False)
thumbnail = None # type: Optional[TelegramFile]

@classmethod
def get(cls, id: str) -> Optional['TelegramFile']:
Expand Down
2 changes: 1 addition & 1 deletion mautrix_telegram/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MatrixHandler:
log = logging.getLogger("mau.mx") # type: logging.Logger

def __init__(self, context: 'Context') -> None:
self.az, self.db, self.config, _, self.tgbot = context.core
self.az, self.config, _, self.tgbot = context.core
self.commands = com.CommandProcessor(context) # type: com.CommandProcessor
self.previously_typing = [] # type: List[MatrixUserID]

Expand Down
2 changes: 1 addition & 1 deletion mautrix_telegram/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2039,7 +2039,7 @@ def get_by_entity(cls, entity: Union[TypeChat, TypePeer, TypeUser, TypeUserFull,

def init(context: Context) -> None:
global config
Portal.az, _, config, Portal.loop, Portal.bot = context.core
Portal.az, config, Portal.loop, Portal.bot = context.core
Portal.max_initial_member_sync = config["bridge.max_initial_member_sync"]
Portal.sync_channel_members = config["bridge.sync_channel_members"]
Portal.sync_matrix_state = config["bridge.sync_matrix_state"]
Expand Down
13 changes: 4 additions & 9 deletions mautrix_telegram/puppet.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import (Awaitable, Coroutine, Dict, List, Iterable, Optional, Pattern, Union,
TYPE_CHECKING)
from typing import Awaitable, Any, Dict, List, Iterable, Optional, Pattern, Union, TYPE_CHECKING
from difflib import SequenceMatcher
from enum import Enum
from aiohttp import ServerDisconnectedError
import asyncio
import logging
import re

from sqlalchemy import orm

from telethon.tl.types import UserProfilePhoto, User, FileLocation, UpdateUserName, PeerUser
from mautrix_appservice import AppService, IntentAPI, IntentError, MatrixRequestError

Expand All @@ -45,7 +42,6 @@

class Puppet:
log = logging.getLogger("mau.puppet") # type: logging.Logger
db = None # type: orm.Session
az = None # type: AppService
mx = None # type: MatrixHandler
loop = None # type: asyncio.AbstractEventLoop
Expand Down Expand Up @@ -400,8 +396,7 @@ def get(cls, tgid: TelegramID, create: bool = True) -> Optional['Puppet']:

if create:
puppet = cls(tgid)
cls.db.add(puppet.db_instance)
cls.db.commit()
puppet.db_instance.insert()
return puppet

return None
Expand Down Expand Up @@ -481,9 +476,9 @@ def find_by_displayname(cls, displayname: str) -> Optional['Puppet']:
# endregion


def init(context: 'Context') -> List[Coroutine]: # [None, None, PuppetError]
def init(context: 'Context') -> List[Awaitable[Any]]: # [None, None, PuppetError]
global config
Puppet.az, Puppet.db, config, Puppet.loop, _ = context.core
Puppet.az, config, Puppet.loop, _ = context.core
Puppet.mx = context.mx
Puppet.username_template = config.get("bridge.username_template", "telegram_{userid}")
Puppet.hs_domain = config["homeserver"]["domain"]
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
"ruamel.yaml>=0.15.35,<0.16",
"future-fstrings>=0.4.2",
"python-magic>=0.4.15,<0.5",
"telethon>=1.5.5,<1.6",
"telethon-session-sqlalchemy>=0.2.9,<0.3",
"telethon>=1.5.5,<1.7",
"telethon-session-sqlalchemy>=0.2.11,<0.3",
],
extras_require=extras,

Expand Down

0 comments on commit 7c46bf4

Please sign in to comment.