appservice: send links for matrix attachment

This commit is contained in:
git-bruh 2021-04-20 18:19:49 +05:30
parent a14efadfd6
commit a481efeee3
No known key found for this signature in database
GPG key ID: E1475C50075ADCE6
3 changed files with 32 additions and 18 deletions

View file

@ -8,7 +8,7 @@ import bottle
import urllib3 import urllib3
import matrix import matrix
from misc import dict_cls, log_except, request from misc import dict_cls, except_deleted, log_except, request
class AppService(bottle.Bottle): class AppService(bottle.Bottle):
@ -83,7 +83,20 @@ class AppService(bottle.Bottle):
return {} return {}
def mxc_url(self, mxc: str) -> str:
try:
homeserver, media_id = mxc.replace("mxc://", "").split("/")
converted = (
f"https://{self.server_name}/_matrix/media/r0/download/"
f"{homeserver}/{media_id}"
)
except ValueError:
converted = ""
return converted
def get_event_object(self, event: dict) -> matrix.Event: def get_event_object(self, event: dict) -> matrix.Event:
# TODO use caching and invalidate old cache on member events.
event["author"] = dict_cls( event["author"] = dict_cls(
self.get_profile(event["sender"]), matrix.User self.get_profile(event["sender"]), matrix.User
) )
@ -105,18 +118,16 @@ class AppService(bottle.Bottle):
) )
def get_profile(self, mxid: str) -> dict: def get_profile(self, mxid: str) -> dict:
# TODO handle failure, avoid querying this endpoint repeatedly. resp = except_deleted(self.send)("GET", f"/profile/{mxid}")
resp = self.send("GET", f"/profile/{mxid}")
avatar_url = resp.get("avatar_url", "")[6:].split("/") # No profile exists for the user.
avatar_url = ( if not resp:
( return {}
f"https://{self.server_name}/_matrix/media/r0/download/"
f"{avatar_url[0]}/{avatar_url[1]}" avatar_url = resp.get("avatar_url")
)
if len(avatar_url) > 1 if avatar_url:
else None avatar_url = self.mxc_url(avatar_url)
)
return { return {
"avatar_url": avatar_url, "avatar_url": avatar_url,

View file

@ -91,7 +91,6 @@ class MatrixClient(AppService):
webhook = self.discord.get_webhook(channel_id, "matrix_bridge") webhook = self.discord.get_webhook(channel_id, "matrix_bridge")
if message.relates_to and message.reltype == "m.replace": if message.relates_to and message.reltype == "m.replace":
relation = message_cache.get(message.relates_to) relation = message_cache.get(message.relates_to)
if not message.new_body or not relation: if not message.new_body or not relation:
@ -106,7 +105,12 @@ class MatrixClient(AppService):
) )
else: else:
message.body = self.process_message(channel_id, message.body) message.body = (
f"`{message.body}`: {self.mxc_url(message.attachment)}"
if message.attachment
else self.process_message(channel_id, message.body)
)
message_cache[message.event_id] = { message_cache[message.event_id] = {
"message_id": self.discord.send_webhook( "message_id": self.discord.send_webhook(
webhook, webhook,
@ -362,8 +366,6 @@ class DiscordClient(Gateway):
) )
def sync_users(guilds: set): def sync_users(guilds: set):
users = []
for guild in guilds: for guild in guilds:
[ [
self.sync_profile(user, self.matrixify(user.id, user=True)) self.sync_profile(user, self.matrixify(user.id, user=True))

View file

@ -3,14 +3,15 @@ from dataclasses import dataclass
@dataclass @dataclass
class User(object): class User(object):
avatar_url: str avatar_url: str = ""
displayname: str displayname: str = ""
class Event(object): class Event(object):
def __init__(self, event: dict): def __init__(self, event: dict):
content = event["content"] content = event["content"]
self.attachment = content.get("url")
self.author = event["author"] self.author = event["author"]
self.body = content.get("body", "") self.body = content.get("body", "")
self.event_id = event["event_id"] self.event_id = event["event_id"]