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

View file

@ -91,7 +91,6 @@ class MatrixClient(AppService):
webhook = self.discord.get_webhook(channel_id, "matrix_bridge")
if message.relates_to and message.reltype == "m.replace":
relation = message_cache.get(message.relates_to)
if not message.new_body or not relation:
@ -106,7 +105,12 @@ class MatrixClient(AppService):
)
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_id": self.discord.send_webhook(
webhook,
@ -362,8 +366,6 @@ class DiscordClient(Gateway):
)
def sync_users(guilds: set):
users = []
for guild in guilds:
[
self.sync_profile(user, self.matrixify(user.id, user=True))

View file

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