Move some utility methods from portal to separate files
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
from .color_log import ColorFormatter
|
||||
from .deduplication import PortalDedup
|
||||
from .file_transfer import convert_image, transfer_file_to_matrix
|
||||
from .media_fallback import make_contact_event_content, make_dice_event_content
|
||||
from .parallel_file_transfer import parallel_transfer_to_telegram
|
||||
from .recursive_dict import recursive_del, recursive_get, recursive_set
|
||||
from .send_lock import PortalSendLock
|
||||
|
||||
@@ -1,134 +0,0 @@
|
||||
# mautrix-telegram - A Matrix-Telegram puppeting bridge
|
||||
# Copyright (C) 2021 Tulir Asokan
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# 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 __future__ import annotations
|
||||
|
||||
from typing import Tuple
|
||||
from collections import deque
|
||||
import hashlib
|
||||
|
||||
from telethon.tl.patched import Message, MessageService
|
||||
from telethon.tl.types import (
|
||||
MessageMediaContact,
|
||||
MessageMediaDocument,
|
||||
MessageMediaGeo,
|
||||
MessageMediaPhoto,
|
||||
TypeMessage,
|
||||
TypeUpdates,
|
||||
UpdateNewChannelMessage,
|
||||
UpdateNewMessage,
|
||||
)
|
||||
|
||||
from mautrix.types import EventID
|
||||
|
||||
from .. import portal as po
|
||||
from ..types import TelegramID
|
||||
|
||||
DedupMXID = Tuple[EventID, TelegramID]
|
||||
|
||||
|
||||
class PortalDedup:
|
||||
pre_db_check: bool = False
|
||||
cache_queue_length: int = 20
|
||||
|
||||
_dedup: deque[str]
|
||||
_dedup_mxid: dict[str, DedupMXID]
|
||||
_dedup_action: deque[str]
|
||||
_portal: po.Portal
|
||||
|
||||
def __init__(self, portal: po.Portal) -> None:
|
||||
self._dedup = deque()
|
||||
self._dedup_mxid = {}
|
||||
self._dedup_action = deque()
|
||||
self._portal = portal
|
||||
|
||||
@property
|
||||
def _always_force_hash(self) -> bool:
|
||||
return self._portal.peer_type == "chat"
|
||||
|
||||
@staticmethod
|
||||
def _hash_event(event: TypeMessage) -> str:
|
||||
# Non-channel messages are unique per-user (wtf telegram), so we have no other choice than
|
||||
# to deduplicate based on a hash of the message content.
|
||||
|
||||
# The timestamp is only accurate to the second, so we can't rely solely on that either.
|
||||
if isinstance(event, MessageService):
|
||||
hash_content = [event.date.timestamp(), event.from_id, event.action]
|
||||
else:
|
||||
hash_content = [event.date.timestamp(), event.message.strip()]
|
||||
if event.fwd_from:
|
||||
hash_content += [event.fwd_from.from_id]
|
||||
elif isinstance(event, Message) and event.media:
|
||||
try:
|
||||
hash_content += {
|
||||
MessageMediaContact: lambda media: [media.user_id],
|
||||
MessageMediaDocument: lambda media: [media.document.id],
|
||||
MessageMediaPhoto: lambda media: [media.photo.id if media.photo else 0],
|
||||
MessageMediaGeo: lambda media: [media.geo.long, media.geo.lat],
|
||||
}[type(event.media)](event.media)
|
||||
except KeyError:
|
||||
pass
|
||||
return hashlib.md5("-".join(str(a) for a in hash_content).encode("utf-8")).hexdigest()
|
||||
|
||||
def check_action(self, event: TypeMessage) -> bool:
|
||||
evt_hash = self._hash_event(event) if self._always_force_hash else event.id
|
||||
if evt_hash in self._dedup_action:
|
||||
return True
|
||||
|
||||
self._dedup_action.append(evt_hash)
|
||||
|
||||
if len(self._dedup_action) > self.cache_queue_length:
|
||||
self._dedup_action.popleft()
|
||||
return False
|
||||
|
||||
def update(
|
||||
self,
|
||||
event: TypeMessage,
|
||||
mxid: DedupMXID = None,
|
||||
expected_mxid: DedupMXID | None = None,
|
||||
force_hash: bool = False,
|
||||
) -> DedupMXID | None:
|
||||
evt_hash = self._hash_event(event) if self._always_force_hash or force_hash else event.id
|
||||
try:
|
||||
found_mxid = self._dedup_mxid[evt_hash]
|
||||
except KeyError:
|
||||
return EventID("None"), TelegramID(0)
|
||||
|
||||
if found_mxid != expected_mxid:
|
||||
return found_mxid
|
||||
self._dedup_mxid[evt_hash] = mxid
|
||||
return None
|
||||
|
||||
def check(
|
||||
self, event: TypeMessage, mxid: DedupMXID = None, force_hash: bool = False
|
||||
) -> DedupMXID | None:
|
||||
evt_hash = self._hash_event(event) if self._always_force_hash or force_hash else event.id
|
||||
if evt_hash in self._dedup:
|
||||
return self._dedup_mxid[evt_hash]
|
||||
|
||||
self._dedup_mxid[evt_hash] = mxid
|
||||
self._dedup.append(evt_hash)
|
||||
|
||||
if len(self._dedup) > self.cache_queue_length:
|
||||
del self._dedup_mxid[self._dedup.popleft()]
|
||||
return None
|
||||
|
||||
def register_outgoing_actions(self, response: TypeUpdates) -> None:
|
||||
for update in response.updates:
|
||||
check_dedup = isinstance(
|
||||
update, (UpdateNewMessage, UpdateNewChannelMessage)
|
||||
) and isinstance(update.message, MessageService)
|
||||
if check_dedup:
|
||||
self.check(update.message)
|
||||
@@ -1,130 +0,0 @@
|
||||
# mautrix-telegram - A Matrix-Telegram puppeting bridge
|
||||
# Copyright (C) 2021 Tulir Asokan
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# 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 __future__ import annotations
|
||||
|
||||
import html
|
||||
|
||||
from telethon.tl.types import MessageMediaContact, MessageMediaDice, PeerUser
|
||||
|
||||
from mautrix.types import Format, MessageType, TextMessageEventContent
|
||||
|
||||
from .. import abstract_user as au, puppet as pu
|
||||
from ..types import TelegramID
|
||||
|
||||
try:
|
||||
import phonenumbers
|
||||
except ImportError:
|
||||
phonenumbers = None
|
||||
|
||||
|
||||
def _format_dice(roll: MessageMediaDice) -> str:
|
||||
if roll.emoticon == "\U0001F3B0":
|
||||
emojis = {
|
||||
0: "\U0001F36B", # "🍫",
|
||||
1: "\U0001F352", # "🍒",
|
||||
2: "\U0001F34B", # "🍋",
|
||||
3: "7\ufe0f\u20e3", # "7️⃣",
|
||||
}
|
||||
res = roll.value - 1
|
||||
slot1, slot2, slot3 = emojis[res % 4], emojis[res // 4 % 4], emojis[res // 16]
|
||||
return f"{slot1} {slot2} {slot3} ({roll.value})"
|
||||
elif roll.emoticon == "\u26BD":
|
||||
results = {
|
||||
1: "miss",
|
||||
2: "hit the woodwork",
|
||||
3: "goal", # seems to go in through the center
|
||||
4: "goal",
|
||||
5: "goal 🎉", # seems to go in through the top right corner, includes confetti
|
||||
}
|
||||
elif roll.emoticon == "\U0001F3B3":
|
||||
results = {
|
||||
1: "miss",
|
||||
2: "1 pin down",
|
||||
3: "3 pins down, split",
|
||||
4: "4 pins down, split",
|
||||
5: "5 pins down",
|
||||
6: "strike 🎉",
|
||||
}
|
||||
# elif roll.emoticon == "\U0001F3C0":
|
||||
# results = {
|
||||
# 2: "rolled off",
|
||||
# 3: "stuck",
|
||||
# }
|
||||
# elif roll.emoticon == "\U0001F3AF":
|
||||
# results = {
|
||||
# 1: "bounced off",
|
||||
# 2: "outer rim",
|
||||
#
|
||||
# 6: "bullseye",
|
||||
# }
|
||||
else:
|
||||
return str(roll.value)
|
||||
return f"{results[roll.value]} ({roll.value})"
|
||||
|
||||
|
||||
def make_dice_event_content(roll: MessageMediaDice) -> TextMessageEventContent:
|
||||
emoji_text = {
|
||||
"\U0001F3AF": " Dart throw",
|
||||
"\U0001F3B2": " Dice roll",
|
||||
"\U0001F3C0": " Basketball throw",
|
||||
"\U0001F3B0": " Slot machine",
|
||||
"\U0001F3B3": " Bowling",
|
||||
"\u26BD": " Football kick",
|
||||
}
|
||||
text = f"{roll.emoticon}{emoji_text.get(roll.emoticon, '')} result: {_format_dice(roll)}"
|
||||
content = TextMessageEventContent(
|
||||
msgtype=MessageType.TEXT, format=Format.HTML, body=text, formatted_body=f"<h4>{text}</h4>"
|
||||
)
|
||||
content["net.maunium.telegram.dice"] = {"emoticon": roll.emoticon, "value": roll.value}
|
||||
return content
|
||||
|
||||
|
||||
async def make_contact_event_content(
|
||||
source: au.AbstractUser, contact: MessageMediaContact
|
||||
) -> TextMessageEventContent:
|
||||
name = " ".join(x for x in [contact.first_name, contact.last_name] if x)
|
||||
formatted_phone = f"+{contact.phone_number}"
|
||||
if phonenumbers is not None:
|
||||
parsed = phonenumbers.parse(formatted_phone)
|
||||
fmt = phonenumbers.PhoneNumberFormat.INTERNATIONAL
|
||||
formatted_phone = phonenumbers.format_number(parsed, fmt)
|
||||
content = TextMessageEventContent(
|
||||
msgtype=MessageType.TEXT,
|
||||
body=f"Shared contact info for {name}: {formatted_phone}",
|
||||
)
|
||||
content["net.maunium.telegram.contact"] = {
|
||||
"user_id": contact.user_id,
|
||||
"first_name": contact.first_name,
|
||||
"last_name": contact.last_name,
|
||||
"phone_number": contact.phone_number,
|
||||
"vcard": contact.vcard,
|
||||
}
|
||||
|
||||
puppet = await pu.Puppet.get_by_tgid(TelegramID(contact.user_id))
|
||||
if not puppet.displayname:
|
||||
try:
|
||||
entity = await source.client.get_entity(PeerUser(contact.user_id))
|
||||
await puppet.update_info(source, entity)
|
||||
except Exception as e:
|
||||
source.log.warning(f"Failed to sync puppet info of received contact: {e}")
|
||||
else:
|
||||
content.format = Format.HTML
|
||||
content.formatted_body = (
|
||||
f"Shared contact info for "
|
||||
f"<a href='https://matrix.to/#/{puppet.mxid}'>{html.escape(name)}</a>: "
|
||||
f"{html.escape(formatted_phone)}"
|
||||
)
|
||||
return content
|
||||
@@ -1,44 +0,0 @@
|
||||
# mautrix-telegram - A Matrix-Telegram puppeting bridge
|
||||
# Copyright (C) 2021 Tulir Asokan
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# 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 __future__ import annotations
|
||||
|
||||
from asyncio import Lock
|
||||
|
||||
from ..types import TelegramID
|
||||
|
||||
|
||||
class FakeLock:
|
||||
async def __aenter__(self) -> None:
|
||||
pass
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb) -> None:
|
||||
pass
|
||||
|
||||
|
||||
class PortalSendLock:
|
||||
_send_locks: dict[int, Lock]
|
||||
_noop_lock: Lock = FakeLock()
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._send_locks = {}
|
||||
|
||||
def __call__(self, user_id: TelegramID, required: bool = True) -> Lock:
|
||||
if user_id is None and required:
|
||||
raise ValueError("Required send lock for none id")
|
||||
try:
|
||||
return self._send_locks[user_id]
|
||||
except KeyError:
|
||||
return self._send_locks.setdefault(user_id, Lock()) if required else self._noop_lock
|
||||
Reference in New Issue
Block a user