From 72599e1c652649933442ef8c6c6ad9bfd8cb53f5 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 4 Apr 2019 00:10:41 +0200 Subject: [PATCH] Progress on the diario --- royalgames.py | 4 ++-- royalnet/commands/__init__.py | 3 ++- royalnet/commands/debug_create.py | 10 +++++---- royalnet/commands/diario.py | 33 +++++++++++++++++++++++------- royalnet/database/tables/diario.py | 6 +----- 5 files changed, 37 insertions(+), 19 deletions(-) diff --git a/royalgames.py b/royalgames.py index c198b58d..b9d72c27 100644 --- a/royalgames.py +++ b/royalgames.py @@ -1,7 +1,7 @@ import os import asyncio from royalnet.bots import TelegramBot -from royalnet.commands import PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, SyncCommand +from royalnet.commands import PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, SyncCommand, DiarioCommand from royalnet.commands.debug_create import DebugCreateCommand from royalnet.commands.debug_author import DebugAuthorCommand from royalnet.network import RoyalnetServer @@ -10,7 +10,7 @@ from royalnet.database.tables import Royal, Telegram loop = asyncio.get_event_loop() commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, DebugCreateCommand, SyncCommand, - DebugAuthorCommand] + DebugAuthorCommand, DiarioCommand] master = RoyalnetServer("localhost", 1234, "sas") tg_bot = TelegramBot(os.environ["TG_AK"], "localhost:1234", "sas", commands, os.environ["DB_PATH"], Royal, Telegram, "tg_id") diff --git a/royalnet/commands/__init__.py b/royalnet/commands/__init__.py index 44ffda9a..edc01972 100644 --- a/royalnet/commands/__init__.py +++ b/royalnet/commands/__init__.py @@ -5,7 +5,8 @@ from .smecds import SmecdsCommand from .ciaoruozi import CiaoruoziCommand from .color import ColorCommand from .sync import SyncCommand +from .diario import DiarioCommand __all__ = ["NullCommand", "PingCommand", "ShipCommand", "SmecdsCommand", "CiaoruoziCommand", "ColorCommand", - "SyncCommand"] + "SyncCommand", "DiarioCommand"] diff --git a/royalnet/commands/debug_create.py b/royalnet/commands/debug_create.py index 940899e2..6327ca2b 100644 --- a/royalnet/commands/debug_create.py +++ b/royalnet/commands/debug_create.py @@ -1,5 +1,5 @@ -from ..utils import Command, CommandArgs, Call -from ..database.tables import Royal +from ..utils import Command, CommandArgs, Call, asyncify +from ..database.tables import Royal, Alias class DebugCreateCommand(Command): @@ -7,10 +7,12 @@ class DebugCreateCommand(Command): command_name = "debug_create" command_title = "Create a new Royalnet user account" - require_alchemy_tables = {Royal} + require_alchemy_tables = {Royal, Alias} async def common(self, call: Call, args: CommandArgs): royal = call.alchemy.Royal(username=args[0], role="Member") call.session.add(royal) - call.session.commit() + alias = call.alchemy.Alias(royal=royal, alias=royal.username) + call.session.add(alias) + await asyncify(call.session.commit) await call.reply(f"✅ Utente {royal} creato!") diff --git a/royalnet/commands/diario.py b/royalnet/commands/diario.py index 97f3bb6c..b25ede61 100644 --- a/royalnet/commands/diario.py +++ b/royalnet/commands/diario.py @@ -2,6 +2,7 @@ import re import datetime from ..utils import Command, CommandArgs, Call, InvalidInputError from ..database.tables import Royal, Diario, Alias +from ..utils import asyncify class DiarioCommand(Command): @@ -13,17 +14,35 @@ class DiarioCommand(Command): async def common(self, call: Call, args: CommandArgs): # Recreate the full sentence - text = " ".join(args) + text = " ".join(args.args) # Pass the sentence through the diario regex - match = re.match(r'["«‘“‛‟❛❝〝"`]([^"]+)["»’”❜❞〞"`] *(?:(?:-{1,2}|—) *(\w+))?(?:,? *([^ ].*))?', text) + match = re.match(r'(!)? *["«‘“‛‟❛❝〝"`]([^"]+)["»’”❜❞〞"`] *(?:(?:-{1,2}|—) *([\w ]+))?(?:, *([^ ].*))?', text) # Find the corresponding matches if match is None: + await call.reply(f"✅ Comando skippato per frase non valida") raise InvalidInputError("No match found.") - text = match.group(1) - quoted = match.group(2) - context = match.group(3) + spoiler = bool(match.group(1)) + text = match.group(2) + quoted = match.group(3) + context = match.group(4) timestamp = datetime.datetime.now() # Find if there's a Royalnet account associated with the quoted name - quoted_alias = call.session.query(call.alchemy.Alias).filter_by(alias=quoted).one_or_none() + if quoted is not None: + quoted_alias = await asyncify(call.session.query(call.alchemy.Alias).filter_by(alias=quoted.lower()).one_or_none) + else: + quoted_alias = None quoted_account = quoted_alias.royal if quoted_alias is not None else None - # Find the creator of the quote \ No newline at end of file + # Find the creator of the quotes + creator = await call.get_author() + # Create the diario quote + diario = call.alchemy.Diario(creator=creator, + quoted_account=quoted_account, + quoted=quoted, + text=text, + context=context, + timestamp=timestamp, + media_url=None, + spoiler=spoiler) + call.session.add(diario) + await asyncify(call.session.commit) + await call.reply(f"✅ Aggiunto al diario: {repr(diario)}") diff --git a/royalnet/database/tables/diario.py b/royalnet/database/tables/diario.py index c3418cf9..5f91dd5c 100644 --- a/royalnet/database/tables/diario.py +++ b/royalnet/database/tables/diario.py @@ -13,7 +13,6 @@ class Diario: __tablename__ = "diario" diario_id = Column(Integer, primary_key=True) - creator_id = Column(Integer, ForeignKey("royals.uid")) quoted_account_id = Column(Integer, ForeignKey("royals.uid")) quoted = Column(String) @@ -27,7 +26,4 @@ class Diario: quoted_account = relationship("Royal", foreign_keys=quoted_account_id, backref="diario_quoted") def __repr__(self): - return f"" - - def __str__(self): - return f"diario:{self.diario_id}" + return f"<Diario diario_id={self.diario_id} creator_id={self.creator_id} quoted_account_id={self.quoted_account_id} quoted={self.quoted} text={self.text} context={self.context} timestamp={self.timestamp} media_url={self.media_url} spoiler={self.spoiler}>"