From 4656ab92a9667cf83ff08ce53582256acb8d4ddb Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 1 Apr 2019 21:44:22 +0200 Subject: [PATCH] Implement sync --- royalgames.py | 4 ++-- royalnet/commands/__init__.py | 4 +++- royalnet/commands/sync.py | 18 ++++++++++++++---- royalnet/database/alchemy.py | 4 ++-- royalnet/database/tables/__init__.py | 3 ++- royalnet/database/tables/telegram.py | 8 ++++++-- 6 files changed, 29 insertions(+), 12 deletions(-) diff --git a/royalgames.py b/royalgames.py index 9ed768c7..da03bb3d 100644 --- a/royalgames.py +++ b/royalgames.py @@ -1,13 +1,13 @@ import os import asyncio from royalnet.bots import TelegramBot -from royalnet.commands import PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand +from royalnet.commands import PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, SyncCommand from royalnet.commands.debug_create import DebugCreateCommand from royalnet.network import RoyalnetServer loop = asyncio.get_event_loop() -commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, DebugCreateCommand] +commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, DebugCreateCommand, SyncCommand] master = RoyalnetServer("localhost", 1234, "sas") tg_bot = TelegramBot(os.environ["TG_AK"], "localhost:1234", "sas", commands, "sqlite://") diff --git a/royalnet/commands/__init__.py b/royalnet/commands/__init__.py index 01f2db59..44ffda9a 100644 --- a/royalnet/commands/__init__.py +++ b/royalnet/commands/__init__.py @@ -4,6 +4,8 @@ from .ship import ShipCommand from .smecds import SmecdsCommand from .ciaoruozi import CiaoruoziCommand from .color import ColorCommand +from .sync import SyncCommand -__all__ = ["NullCommand", "PingCommand", "ShipCommand", "SmecdsCommand", "CiaoruoziCommand", "ColorCommand"] +__all__ = ["NullCommand", "PingCommand", "ShipCommand", "SmecdsCommand", "CiaoruoziCommand", "ColorCommand", + "SyncCommand"] diff --git a/royalnet/commands/sync.py b/royalnet/commands/sync.py index 741ee9f4..2aabff77 100644 --- a/royalnet/commands/sync.py +++ b/royalnet/commands/sync.py @@ -1,7 +1,7 @@ import typing from telegram import Update, User -from ..utils import Command, CommandArgs, Call -from ..database.tables import Royal +from ..utils import Command, CommandArgs, Call, asyncify +from ..database.tables import Royal, Telegram class SyncCommand(Command): @@ -9,7 +9,7 @@ class SyncCommand(Command): command_name = "sync" command_title = "Connect your current account to Royalnet" - require_alchemy_tables = [Royal] + require_alchemy_tables = [Royal, Telegram] async def common(self, call: Call, args: CommandArgs): raise NotImplementedError() @@ -21,6 +21,16 @@ class SyncCommand(Command): if user is None: raise ValueError("Trying to sync a None user.") # Find the Royal - royal = call.session.query(call.interface_alchemy.Royal).filter_by(username=args[0]).one_or_none() + royal = await asyncify(call.session.query(call.interface_alchemy.Royal).filter_by(username=args[0]).one_or_none) if royal is None: await call.reply("⚠️ Non esiste alcun account Royalnet con quel nome.") + # Create a Telegram to connect to the Royal + # Avatar is WIP + telegram = call.interface_alchemy.Telegram(royal=royal, + tg_id=user.id, + tg_first_name=user.first_name, + tg_last_name=user.last_name, + tg_username=user.username) + call.session.add(telegram) + # Commit the session + await asyncify(call.session.commit()) diff --git a/royalnet/database/alchemy.py b/royalnet/database/alchemy.py index 1c0af022..8dd9ba4c 100644 --- a/royalnet/database/alchemy.py +++ b/royalnet/database/alchemy.py @@ -4,7 +4,7 @@ from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker from contextlib import contextmanager, asynccontextmanager -from ..utils import cdj +from ..utils import cdj, asyncify loop = asyncio.get_event_loop() @@ -41,7 +41,7 @@ class Alchemy: @asynccontextmanager async def session_acm(self): - session = await loop.run_in_executor(None, self.Session) + session = await asyncify(self.Session) try: yield session except Exception: diff --git a/royalnet/database/tables/__init__.py b/royalnet/database/tables/__init__.py index 947c88b2..7ec77e9c 100644 --- a/royalnet/database/tables/__init__.py +++ b/royalnet/database/tables/__init__.py @@ -1,3 +1,4 @@ from .royals import Royal +from .telegram import Telegram -__all__ = ["Royal"] +__all__ = ["Royal", "Telegram"] diff --git a/royalnet/database/tables/telegram.py b/royalnet/database/tables/telegram.py index 753abd89..dec361f0 100644 --- a/royalnet/database/tables/telegram.py +++ b/royalnet/database/tables/telegram.py @@ -2,19 +2,23 @@ from sqlalchemy import Column, \ Integer, \ String, \ BigInteger, \ - LargeBinary + LargeBinary, \ + ForeignKey +from sqlalchemy.orm import relationship class Telegram: __tablename__ = "telegram" - royal_id = Column(Integer) + royal_id = Column(Integer, ForeignKey("royals.uid")) tg_id = Column(BigInteger, primary_key=True) tg_first_name = Column(String) tg_last_name = Column(String) tg_username = Column(String) tg_avatar = Column(LargeBinary) + royal = relationship("Royal") + def __repr__(self): return f""