1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

Implement sync

This commit is contained in:
Steffo 2019-04-01 21:44:22 +02:00
parent 7beb2fb193
commit 4656ab92a9
6 changed files with 29 additions and 12 deletions

View file

@ -1,13 +1,13 @@
import os import os
import asyncio import asyncio
from royalnet.bots import TelegramBot 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.commands.debug_create import DebugCreateCommand
from royalnet.network import RoyalnetServer from royalnet.network import RoyalnetServer
loop = asyncio.get_event_loop() 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") master = RoyalnetServer("localhost", 1234, "sas")
tg_bot = TelegramBot(os.environ["TG_AK"], "localhost:1234", "sas", commands, "sqlite://") tg_bot = TelegramBot(os.environ["TG_AK"], "localhost:1234", "sas", commands, "sqlite://")

View file

@ -4,6 +4,8 @@ from .ship import ShipCommand
from .smecds import SmecdsCommand from .smecds import SmecdsCommand
from .ciaoruozi import CiaoruoziCommand from .ciaoruozi import CiaoruoziCommand
from .color import ColorCommand from .color import ColorCommand
from .sync import SyncCommand
__all__ = ["NullCommand", "PingCommand", "ShipCommand", "SmecdsCommand", "CiaoruoziCommand", "ColorCommand"] __all__ = ["NullCommand", "PingCommand", "ShipCommand", "SmecdsCommand", "CiaoruoziCommand", "ColorCommand",
"SyncCommand"]

View file

@ -1,7 +1,7 @@
import typing import typing
from telegram import Update, User from telegram import Update, User
from ..utils import Command, CommandArgs, Call from ..utils import Command, CommandArgs, Call, asyncify
from ..database.tables import Royal from ..database.tables import Royal, Telegram
class SyncCommand(Command): class SyncCommand(Command):
@ -9,7 +9,7 @@ class SyncCommand(Command):
command_name = "sync" command_name = "sync"
command_title = "Connect your current account to Royalnet" 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): async def common(self, call: Call, args: CommandArgs):
raise NotImplementedError() raise NotImplementedError()
@ -21,6 +21,16 @@ class SyncCommand(Command):
if user is None: if user is None:
raise ValueError("Trying to sync a None user.") raise ValueError("Trying to sync a None user.")
# Find the Royal # 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: if royal is None:
await call.reply("⚠️ Non esiste alcun account Royalnet con quel nome.") 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())

View file

@ -4,7 +4,7 @@ from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from contextlib import contextmanager, asynccontextmanager from contextlib import contextmanager, asynccontextmanager
from ..utils import cdj from ..utils import cdj, asyncify
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
@ -41,7 +41,7 @@ class Alchemy:
@asynccontextmanager @asynccontextmanager
async def session_acm(self): async def session_acm(self):
session = await loop.run_in_executor(None, self.Session) session = await asyncify(self.Session)
try: try:
yield session yield session
except Exception: except Exception:

View file

@ -1,3 +1,4 @@
from .royals import Royal from .royals import Royal
from .telegram import Telegram
__all__ = ["Royal"] __all__ = ["Royal", "Telegram"]

View file

@ -2,19 +2,23 @@ from sqlalchemy import Column, \
Integer, \ Integer, \
String, \ String, \
BigInteger, \ BigInteger, \
LargeBinary LargeBinary, \
ForeignKey
from sqlalchemy.orm import relationship
class Telegram: class Telegram:
__tablename__ = "telegram" __tablename__ = "telegram"
royal_id = Column(Integer) royal_id = Column(Integer, ForeignKey("royals.uid"))
tg_id = Column(BigInteger, primary_key=True) tg_id = Column(BigInteger, primary_key=True)
tg_first_name = Column(String) tg_first_name = Column(String)
tg_last_name = Column(String) tg_last_name = Column(String)
tg_username = Column(String) tg_username = Column(String)
tg_avatar = Column(LargeBinary) tg_avatar = Column(LargeBinary)
royal = relationship("Royal")
def __repr__(self): def __repr__(self):
return f"<Telegram {str(self)}>" return f"<Telegram {str(self)}>"