Fix minor things and type hints
This commit is contained in:
@@ -4,6 +4,6 @@ from .from_telegram import (telegram_reply_to_matrix, telegram_to_matrix, init_t
|
|||||||
from .. import context as c
|
from .. import context as c
|
||||||
|
|
||||||
|
|
||||||
def init(context: c.Context):
|
def init(context: c.Context) -> None:
|
||||||
init_mx(context)
|
init_mx(context)
|
||||||
init_tg(context)
|
init_tg(context)
|
||||||
|
|||||||
@@ -148,7 +148,7 @@ def plain_mention_to_text() -> Tuple[List[TypeMessageEntity], Callable[[str], st
|
|||||||
return entities, replacer
|
return entities, replacer
|
||||||
|
|
||||||
|
|
||||||
def init_mx(context: "Context"):
|
def init_mx(context: "Context") -> None:
|
||||||
global plain_mention_regex, should_bridge_plaintext_highlights
|
global plain_mention_regex, should_bridge_plaintext_highlights
|
||||||
config = context.config
|
config = context.config
|
||||||
dn_template = config.get("bridge.displayname_template", "{displayname} (Telegram)")
|
dn_template = config.get("bridge.displayname_template", "{displayname} (Telegram)")
|
||||||
|
|||||||
@@ -26,11 +26,11 @@ class MatrixParserCommon:
|
|||||||
"ol", "ul", "li",
|
"ol", "ul", "li",
|
||||||
"h1", "h2", "h3", "h4", "h5", "h6",
|
"h1", "h2", "h3", "h4", "h5", "h6",
|
||||||
"div", "hr", "table") # type: Tuple[str, ...]
|
"div", "hr", "table") # type: Tuple[str, ...]
|
||||||
list_bullets = ("●", "○", "■", "‣")
|
list_bullets = ("●", "○", "■", "‣") # type: Tuple[str, ...]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def list_bullet(cls, depth: int) -> str:
|
def list_bullet(cls, depth: int) -> str:
|
||||||
return cls.list_bullets[(depth - 1) % len(cls.list_bullets)]
|
return cls.list_bullets[(depth - 1) % len(cls.list_bullets)] + " "
|
||||||
|
|
||||||
|
|
||||||
ParsedMessage = Tuple[str, List[TypeMessageEntity]]
|
ParsedMessage = Tuple[str, List[TypeMessageEntity]]
|
||||||
|
|||||||
@@ -202,7 +202,7 @@ class MatrixParser(HTMLParser, MatrixParserCommon):
|
|||||||
else:
|
else:
|
||||||
prefix = int(math.log(n, 10)) * 3 * " " + 4 * " "
|
prefix = int(math.log(n, 10)) * 3 * " " + 4 * " "
|
||||||
else:
|
else:
|
||||||
prefix = (f"{self.list_bullet(self._open_tags.count('ul'))} "
|
prefix = (self.list_bullet(self._open_tags.count('ul'))
|
||||||
if self._list_entry_is_new else 3 * " ")
|
if self._list_entry_is_new else 3 * " ")
|
||||||
if not self._list_entry_is_new and not self._line_is_new:
|
if not self._list_entry_is_new and not self._line_is_new:
|
||||||
prefix = ""
|
prefix = ""
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ class MatrixParser(MatrixParserCommon):
|
|||||||
prefix = f"{counter}. "
|
prefix = f"{counter}. "
|
||||||
counter += 1
|
counter += 1
|
||||||
else:
|
else:
|
||||||
prefix = f"{cls.list_bullet(ctx.ul_depth)} "
|
prefix = cls.list_bullet(ctx.ul_depth)
|
||||||
child = child.prepend(prefix)
|
child = child.prepend(prefix)
|
||||||
parts = child.split("\n")
|
parts = child.split("\n")
|
||||||
parts = parts[:1] + [part.prepend(indent) for part in parts[1:]]
|
parts = parts[:1] + [part.prepend(indent) for part in parts[1:]]
|
||||||
|
|||||||
@@ -19,11 +19,11 @@ from html import escape
|
|||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from telethon.tl.types import (MessageEntityMention, MessageEntityMentionName,
|
from telethon.tl.types import (MessageEntityMention, MessageEntityMentionName, MessageEntityUrl,
|
||||||
MessageEntityEmail, MessageEntityUrl, MessageEntityTextUrl,
|
MessageEntityEmail, MessageEntityTextUrl, MessageEntityBold,
|
||||||
MessageEntityBold, MessageEntityItalic, MessageEntityCode,
|
MessageEntityItalic, MessageEntityCode, MessageEntityPre,
|
||||||
MessageEntityPre, MessageEntityBotCommand, Message, PeerChannel,
|
MessageEntityBotCommand, Message, PeerChannel, MessageEntityHashtag,
|
||||||
MessageEntityHashtag, TypeMessageEntity, MessageFwdHeader, PeerUser)
|
TypeMessageEntity, MessageFwdHeader, PeerUser)
|
||||||
|
|
||||||
from mautrix_appservice import MatrixRequestError
|
from mautrix_appservice import MatrixRequestError
|
||||||
from mautrix_appservice.intent_api import IntentAPI
|
from mautrix_appservice.intent_api import IntentAPI
|
||||||
|
|||||||
@@ -32,14 +32,14 @@ TelematrixBase.metadata.bind = telematrix_db_engine
|
|||||||
chat_links = telematrix.query(ChatLink).all()
|
chat_links = telematrix.query(ChatLink).all()
|
||||||
tg_users = telematrix.query(TgUser).all()
|
tg_users = telematrix.query(TgUser).all()
|
||||||
mx_users = telematrix.query(MatrixUser).all()
|
mx_users = telematrix.query(MatrixUser).all()
|
||||||
messages = telematrix.query(TMMessage).all()
|
tm_messages = telematrix.query(TMMessage).all()
|
||||||
|
|
||||||
telematrix.close()
|
telematrix.close()
|
||||||
telematrix_db_engine.dispose()
|
telematrix_db_engine.dispose()
|
||||||
|
|
||||||
portals = {}
|
portals = {} # Dict[int, Portal]
|
||||||
chats = {}
|
chats = {} # Dict[int, BotChat]
|
||||||
messages = {}
|
messages = {} # Dict[str, Message]
|
||||||
puppets = {} # Dict[int, Puppet]
|
puppets = {} # Dict[int, Puppet]
|
||||||
|
|
||||||
for chat_link in chat_links:
|
for chat_link in chat_links:
|
||||||
@@ -65,11 +65,12 @@ for chat_link in chat_links:
|
|||||||
portals[chat_link.tg_room] = portal
|
portals[chat_link.tg_room] = portal
|
||||||
chats[tgid] = bot_chat
|
chats[tgid] = bot_chat
|
||||||
|
|
||||||
for tm_msg in messages:
|
for tm_msg in tm_messages:
|
||||||
try:
|
try:
|
||||||
portal = portals[tm_msg.tg_group_id]
|
portal = portals[tm_msg.tg_group_id]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
print("Found message entry %d in unlinked chat %d, ignoring..." % (tm_msg.tg_message_id, tm_msg.tg_group_id))
|
print("Found message entry %d in unlinked chat %d, ignoring..." % (tm_msg.tg_message_id,
|
||||||
|
tm_msg.tg_group_id))
|
||||||
continue
|
continue
|
||||||
tg_space = portal.tgid if portal.peer_type == "channel" else args.bot_id
|
tg_space = portal.tgid if portal.peer_type == "channel" else args.bot_id
|
||||||
message = Message(mxid=tm_msg.matrix_event_id, mx_room=tm_msg.matrix_room_id,
|
message = Message(mxid=tm_msg.matrix_event_id, mx_room=tm_msg.matrix_room_id,
|
||||||
@@ -77,7 +78,8 @@ for tm_msg in messages:
|
|||||||
messages[tm_msg.matrix_event_id] = message
|
messages[tm_msg.matrix_event_id] = message
|
||||||
|
|
||||||
for user in tg_users:
|
for user in tg_users:
|
||||||
puppets[user.tg_id] = Puppet(id=user.tg_id, displayname=user.name, displayname_source=args.bot_id)
|
puppets[user.tg_id] = Puppet(id=user.tg_id, displayname=user.name,
|
||||||
|
displayname_source=args.bot_id)
|
||||||
|
|
||||||
for k, v in portals.items():
|
for k, v in portals.items():
|
||||||
mxtg.add(v)
|
mxtg.add(v)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ Base = declarative_base()
|
|||||||
|
|
||||||
|
|
||||||
class ChatLink(Base):
|
class ChatLink(Base):
|
||||||
__tablename__ = 'chat_link'
|
__tablename__ = "chat_link"
|
||||||
|
|
||||||
id = sa.Column(sa.Integer, primary_key=True)
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
matrix_room = sa.Column(sa.String)
|
matrix_room = sa.Column(sa.String)
|
||||||
@@ -14,7 +14,7 @@ class ChatLink(Base):
|
|||||||
|
|
||||||
|
|
||||||
class TgUser(Base):
|
class TgUser(Base):
|
||||||
__tablename__ = 'tg_user'
|
__tablename__ = "tg_user"
|
||||||
|
|
||||||
id = sa.Column(sa.Integer, primary_key=True)
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
tg_id = sa.Column(sa.BigInteger)
|
tg_id = sa.Column(sa.BigInteger)
|
||||||
@@ -23,7 +23,7 @@ class TgUser(Base):
|
|||||||
|
|
||||||
|
|
||||||
class MatrixUser(Base):
|
class MatrixUser(Base):
|
||||||
__tablename__ = 'matrix_user'
|
__tablename__ = "matrix_user"
|
||||||
|
|
||||||
id = sa.Column(sa.Integer, primary_key=True)
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
matrix_id = sa.Column(sa.String)
|
matrix_id = sa.Column(sa.String)
|
||||||
|
|||||||
Reference in New Issue
Block a user