From 249d21eea48ef029e8e5044d7b624913b5e5d356 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sun, 28 Feb 2021 11:05:58 -0800 Subject: [PATCH 1/6] add support for private message #1 --- appservice.py | 317 ++++++++++++++++++++++++++++++++----------------- models.py | 7 ++ pnut-matrix.py | 55 +++++++-- 3 files changed, 259 insertions(+), 120 deletions(-) diff --git a/appservice.py b/appservice.py index 59050f5..550dc60 100644 --- a/appservice.py +++ b/appservice.py @@ -8,7 +8,7 @@ import time from matrix_client.api import MatrixHttpApi from matrix_client.api import MatrixError, MatrixRequestError -from models import Avatars, Rooms, Events, Users, DirectRooms +from models import Avatars, Rooms, Events, Users, DirectRooms, ControlRooms from database import db_session from sqlalchemy import and_ from flask import Flask, jsonify, request, abort @@ -85,7 +85,7 @@ def query_alias(alias): @app.route("/transactions/", methods=["PUT"]) def on_receive_events(transaction): - + access_token = request.args.get('access_token', '') if access_token != app.config['MATRIX_HS_TOKEN']: abort(403) @@ -94,7 +94,6 @@ def on_receive_events(transaction): for event in events: logger.debug(event) - # TODO: route event if it's in the control room if app.config['MATRIX_ADMIN_ROOM'] and app.config['MATRIX_ADMIN_ROOM'] == event['room_id']: return on_admin_event(event) @@ -109,8 +108,8 @@ def on_receive_events(transaction): elif event['type'] == 'm.room.member': if 'is_direct' in event['content'] and 'membership' in event['content']: if event['content']['membership'] == "invite" and event['content']['is_direct']: - return on_admin_invite(event) - + return on_direct_invite(event) + if 'membership' in event['content']: if event['content']['membership'] == "leave": return on_leave_event(event) @@ -122,28 +121,26 @@ def on_receive_events(transaction): return jsonify({}) def new_message(event, user): - if app.config['MATRIX_PNUT_PREFIX'] in event['user_id'] or 'pnut-bridge' in event['user_id']: logger.debug('-skipping dup event-') return - direct = DirectRooms.query.filter(DirectRooms.room_id == event['room_id']).one_or_none() - if direct is not None: - return on_direct_message(event) - - room = Rooms.query.filter(Rooms.room_id == event['room_id']).one_or_none() - if room is None: - logger.debug('-room not mapped-') - return - if 'msgtype' not in event['content']: logger.debug('-unknown message type-') return - - cross_profile = {'username': event['user_id']} - matrix_profile = get_profile(event['user_id']) - if "avatar_url" in matrix_profile: - cross_profile['avatar_image'] = app.config['MATRIX_URL'] + '/_matrix/media/r0/download/' + matrix_profile['avatar_url'][6:] + + control = ControlRooms.query.filter(ControlRooms.room_id == event['room_id']).one_or_none() + if control is not None: + return on_control_message(event) + + direct = DirectRooms.query.filter(DirectRooms.room_id == event['room_id']).one_or_none() + if direct is not None: + return on_direct_message(event, user, direct) + + room = Rooms.query.filter(Rooms.room_id == event['room_id']).one_or_none() + if room is None: + logger.debug('-room not mapped-') + return if user is not None: token = user.pnut_user_token @@ -154,49 +151,10 @@ def new_message(event, user): prefix = "[" + matrix_profile['displayname'] + "] (" + event['user_id'] + ")\n" else: prefix = "(" + event['user_id'] + ")\n" - pnutpy.api.add_authorization_token(token) - text = None - embed = [{ - 'type': "io.pnut.core.crosspost", - 'value': { - 'canonical_url': "https://matrix.to/#/" + event['room_id'] + "/" + event['event_id'] + ":" + app.config['MATRIX_DOMAIN'], - 'source': { - 'name': "matrix", - 'url': "https://matrix.org" - }, - 'user': cross_profile - } - }] - if event['content']['msgtype'] == 'm.text' or event['content']['msgtype'] == 'm.notice': - text = event['content']['body'] - - elif event['content']['msgtype'] == 'm.emote': - text = "* " + event['content']['body'] - - elif event['content']['msgtype'] == 'm.image': - text = event['content']['body'] + "\n" - text += app.config['MATRIX_URL'] + '/_matrix/media/r0/download/' + event['content']['url'][6:] - embed.append(raw_from_event(event)) - - elif event['content']['msgtype'] == 'm.video': - text = event['content']['body'] + "\n" - text += app.config['MATRIX_URL'] + '/_matrix/media/r0/download/' + event['content']['url'][6:] - - elif event['content']['msgtype'] == 'm.audio': - text = event['content']['body'] + "\n" - text += app.config['MATRIX_URL'] + '/_matrix/media/r0/download/' + event['content']['url'][6:] - - elif event['content']['msgtype'] == 'm.file': - text = event['content']['body'] + "\n" - text += app.config['MATRIX_URL'] + '/_matrix/media/r0/download/' + event['content']['url'][6:] - - else: - logger.debug('-unknown msg type- ' + event['content']['msgtype']) - return - - text = prefix + text + embed = [crosspost_raw(event)] + text = prefix + msg_from_event(event) try: msg, meta = pnutpy.api.create_message(room.pnut_chan, data={'text': text, 'raw': embed}) revent = Events( @@ -235,13 +193,62 @@ def new_message(event, user): except pnutpy.errors.PnutAuthAPIException: logger.exception('-unable to post to pnut channel-') return - + except Exception: logger.exception('-something bad happened here-') return -def raw_from_event(event): +def msg_from_event(event): + text = None + if event['content']['msgtype'] == 'm.text' or event['content']['msgtype'] == 'm.notice': + text = event['content']['body'] + elif event['content']['msgtype'] == 'm.emote': + text = "* " + event['content']['body'] + + elif event['content']['msgtype'] == 'm.image': + text = event['content']['body'] + "\n" + text += app.config['MATRIX_URL'] + '/_matrix/media/r0/download/' + event['content']['url'][6:] + embed.append(raw_from_event(event)) + + elif event['content']['msgtype'] == 'm.video': + text = event['content']['body'] + "\n" + text += app.config['MATRIX_URL'] + '/_matrix/media/r0/download/' + event['content']['url'][6:] + + elif event['content']['msgtype'] == 'm.audio': + text = event['content']['body'] + "\n" + text += app.config['MATRIX_URL'] + '/_matrix/media/r0/download/' + event['content']['url'][6:] + + elif event['content']['msgtype'] == 'm.file': + text = event['content']['body'] + "\n" + text += app.config['MATRIX_URL'] + '/_matrix/media/r0/download/' + event['content']['url'][6:] + + else: + logger.debug('-unknown msg type- ' + event['content']['msgtype']) + return + + return text + +def crosspost_raw(event): + cross_profile = {'username': event['user_id']} + matrix_profile = get_profile(event['user_id']) + if "avatar_url" in matrix_profile: + cross_profile['avatar_image'] = app.config['MATRIX_URL'] + '/_matrix/media/r0/download/' + matrix_profile['avatar_url'][6:] + + embed = { + 'type': "io.pnut.core.crosspost", + 'value': { + 'canonical_url': "https://matrix.to/#/" + event['room_id'] + "/" + event['event_id'] + ":" + app.config['MATRIX_DOMAIN'], + 'source': { + 'name': "matrix", + 'url': "https://matrix.org" + }, + 'user': cross_profile + } + } + return embed + +def raw_from_event(event): url = app.config['MATRIX_URL'] + '/_matrix/media/r0/download/' + event['content']['url'][6:] if event['content']['msgtype'] == 'm.image': @@ -345,26 +352,31 @@ def on_admin_event(event): msg = event['content']['body'].split(' ') - if msg[0] == 'help': - if len(msg) > 1: - matrix_api.send_message(event['room_id'], cmd_admin_help(msg[1])) - else: - matrix_api.send_message(event['room_id'], cmd_admin_help()) + try: + if msg[0] == 'help': + if len(msg) > 1: + matrix_api.send_message(event['room_id'], cmd_admin_help(msg[1])) + else: + matrix_api.send_message(event['room_id'], cmd_admin_help()) - elif msg[0] == 'list': - matrix_api.send_message(event['room_id'], cmd_admin_list()) + elif msg[0] == 'list': + matrix_api.send_message(event['room_id'], cmd_admin_list()) - elif msg[0] == 'unlink': - if len(msg) > 1: - matrix_api.send_message(event['room_id'], cmd_admin_unlink(msg[1])) - else: - matrix_api.send_message(event['room_id'], cmd_admin_help('unlink')) + elif msg[0] == 'unlink': + if len(msg) > 1: + matrix_api.send_message(event['room_id'], cmd_admin_unlink(msg[1])) + else: + matrix_api.send_message(event['room_id'], cmd_admin_help('unlink')) - elif msg[0] == 'link': - if len(msg) > 2: - matrix_api.send_message(event['room_id'], cmd_admin_link(msg[1], msg[2])) - else: - matrix_api.send_message(event['room_id'], cmd_admin_help('link')) + elif msg[0] == 'link': + if len(msg) > 2: + matrix_api.send_message(event['room_id'], cmd_admin_link(msg[1], msg[2])) + else: + matrix_api.send_message(event['room_id'], cmd_admin_help('link')) + + except Exception: + errmsg = "- on_admin_event -" + logger.exception(errmsg) return jsonify({}) @@ -496,31 +508,107 @@ def cmd_admin_unlink(id): logger.exception(errmsg) return errmsg -def on_admin_invite(event): - matrix_api = MatrixHttpApi(app.config['MATRIX_HOST'], - token=app.config['MATRIX_AS_TOKEN']) - - matrix_api.join_room(event['room_id']) +def on_direct_invite(event): + if event['state_key'] == app.config['MATRIX_AS_ID']: + matrix_api = MatrixHttpApi(app.config['MATRIX_HOST'], + token=app.config['MATRIX_AS_TOKEN']) + dm = ControlRooms(room_id=event['room_id']) - direct = DirectRooms(room_id=event['room_id']) - db_session.add(direct) - db_session.commit() + else: + matrix_api = MatrixHttpApi(app.config['MATRIX_HOST'], + identity=event['state_key'], + token=app.config['MATRIX_AS_TOKEN']) + bridge_user = event['state_key'] + pnut_user = bridge_user.replace(app.config['MATRIX_PNUT_PREFIX'],'').split(':')[0] + + user = Users.query.filter(Users.matrix_id == event['sender']).one_or_none() + if user is not None: + # TODO: need to handle if the user isn't registered + pnutpy.api.add_authorization_token(user.pnut_user_token) + channel, meta = pnutpy.api.existing_pm(ids=pnut_user) + + dm = DirectRooms(room_id=event['room_id'], + bridge_user=bridge_user, pnut_chan=channel.id) + + try: + matrix_api.join_room(event['room_id']) + + db_session.add(dm) + db_session.commit() + + except Exception: + errmsg = "- on_direct_invite -" + logger.exception(errmsg) return jsonify({}) def on_leave_event(event): - matrix_api = MatrixHttpApi(app.config['MATRIX_HOST'], - token=app.config['MATRIX_AS_TOKEN']) - direct = DirectRooms.query.filter(DirectRooms.room_id == event['room_id']).one_or_none() if direct is not None: - db_session.delete(direct) - db_session.commit() - matrix_api.leave_room(event['room_id']) - + + matrix_api = MatrixHttpApi(app.config['MATRIX_HOST'], + identity=direct.bridge_user, + token=app.config['MATRIX_AS_TOKEN']) + try: + matrix_api.leave_room(event['room_id']) + db_session.delete(direct) + db_session.commit() + + except Exception: + errmsg = "- on_leave_event -" + logger.exception(errmsg) + + control = ControlRooms.query.filter(ControlRooms.room_id == event['room_id']).one_or_none() + if control is not None: + matrix_api = MatrixHttpApi(app.config['MATRIX_HOST'], + token=app.config['MATRIX_AS_TOKEN']) + try: + matrix_api.leave_room(event['room_id']) + db_session.delete(control) + db_session.commit() + + except Exception: + errmsg = "- on_leave_event -" + logger.exception(errmsg) + return jsonify({}) -def on_direct_message(event): +def on_direct_message(event, user, room): + if user is not None: + token = user.pnut_user_token + prefix = "" + else: + token = app.config['MATRIX_PNUT_TOKEN'] + if "displayname" in matrix_profile: + prefix = "[" + matrix_profile['displayname'] + "] (" + event['user_id'] + ")\n" + else: + prefix = "(" + event['user_id'] + ")\n" + pnutpy.api.add_authorization_token(token) + + embed = [crosspost_raw(event)] + text = prefix + msg_from_event(event) + try: + msg, meta = pnutpy.api.create_message(room.pnut_chan, data={'text': text, 'raw': embed}) + revent = Events( + event_id=event['event_id'], + room_id=event['room_id'], + pnut_msg_id=msg.id, + pnut_user_id=msg.user.id, + pnut_chan_id=room.pnut_chan, + deleted=False + ) + db_session.add(revent) + db_session.commit() + + except pnutpy.errors.PnutAuthAPIException: + logger.exception('-unable to post to pnut channel-') + + except Exception: + logger.exception('-something bad happened here-') + + return jsonify({}) + +def on_control_message(event): matrix_api = MatrixHttpApi(app.config['MATRIX_HOST'], token=app.config['MATRIX_AS_TOKEN']) logger.debug("- direct room event received -") @@ -530,26 +618,31 @@ def on_direct_message(event): msg = event['content']['body'].split(' ') - if msg[0] == '!help' or msg[0] == 'help': - if len(msg) > 1: - matrix_api.send_message(event['room_id'], cmd_user_help(msg[1])) - else: - matrix_api.send_message(event['room_id'], cmd_user_help()) + try: + if msg[0] == '!help' or msg[0] == 'help': + if len(msg) > 1: + matrix_api.send_message(event['room_id'], cmd_user_help(msg[1])) + else: + matrix_api.send_message(event['room_id'], cmd_user_help()) - elif msg[0] == '!auth': - matrix_api.send_message(event['room_id'], cmd_user_auth()) + elif msg[0] == '!auth': + matrix_api.send_message(event['room_id'], cmd_user_auth()) - elif msg[0] == '!save': - if len(msg) > 1: - matrix_api.send_message(event['room_id'], cmd_user_save(event['sender'], msg[1])) - else: - matrix_api.send_message(event['room_id'], cmd_user_save()) + elif msg[0] == '!save': + if len(msg) > 1: + matrix_api.send_message(event['room_id'], cmd_user_save(event['sender'], msg[1])) + else: + matrix_api.send_message(event['room_id'], cmd_user_save()) - elif msg[0] == '!drop': - matrix_api.send_message(event['room_id'], cmd_user_drop(event['sender'])) + elif msg[0] == '!drop': + matrix_api.send_message(event['room_id'], cmd_user_drop(event['sender'])) - elif msg[0] == '!status': - matrix_api.send_message(event['room_id'], cmd_user_status(event['sender'])) + elif msg[0] == '!status': + matrix_api.send_message(event['room_id'], cmd_user_status(event['sender'])) + + except Exception: + errmsg = "- on_direct_message -" + logger.exception(errmsg) return jsonify({}) diff --git a/models.py b/models.py index a201b60..247da58 100644 --- a/models.py +++ b/models.py @@ -18,6 +18,13 @@ class DirectRooms(Base): __tablename__ = 'direct' id = Column(Integer, primary_key=True) room_id = Column(String(250), unique=True) + pnut_chan = Column(Integer, unique=True) + bridge_user = Column(String(250)) + +class ControlRooms(Base): + __tablename__ = 'control' + id = Column(Integer, primary_key=True) + room_id = Column(String(250), unique=True) class Events(Base): __tablename__ = 'events' diff --git a/pnut-matrix.py b/pnut-matrix.py index 85c634f..0617b39 100644 --- a/pnut-matrix.py +++ b/pnut-matrix.py @@ -12,7 +12,7 @@ import os from matrix_client.api import MatrixHttpApi from matrix_client.api import MatrixError, MatrixRequestError -from models import Avatars, Rooms, Events +from models import Avatars, Rooms, Events, DirectRooms, Users from database import db_session, init_db from sqlalchemy import and_ from appservice import app @@ -22,7 +22,7 @@ logger = logging.getLogger() _shutdown = threading.Event() _reconnect = threading.Event() -def new_message(msg): +def new_message(msg, meta): logger.debug("channel: " + msg.channel_id) logger.debug("username: " + msg.user.username) if 'name' in msg.user: @@ -36,7 +36,27 @@ def new_message(msg): if msg.source.id == config['PNUTCLIENT_ID']: return - room = Rooms.query.filter(Rooms.pnut_chan == msg.channel_id).one_or_none() + if meta['channel_type'] == 'io.pnut.core.chat': + room = Rooms.query.filter(Rooms.pnut_chan == msg.channel_id).one_or_none() + elif meta['channel_type'] == 'io.pnut.core.pm': + room = DirectRooms.query.filter(DirectRooms.pnut_chan == msg.channel_id).one_or_none() + if room is None: + # Do do an invite from the bridge user? + logger.debug('new invite?') + # create room and included matrix recpient + # subscribed_user_ids from meta + logger.debug(meta['subscribed_user_ids']) + pnut_user = matrix_id_from_pnut(msg.user.username) + invitees=[] + for pm_user in meta['subscribed_user_ids']: + user = Users.query.filter(Users.pnut_user_id == pm_user).one_or_none() + if user is not None: + invitees.append(user.matrix_id) + if len(invitees) > 0: + room = new_room(pnut_user, invitees, msg.channel_id) + else: + room = None + logger.debug(room) if room is None: logger.debug('-not_mapped-') @@ -245,6 +265,27 @@ def join_room(room_id, matrix_id): logger.debug('-room_join-') +def new_room(pnut_user, invitees, chan): + dr = None + matrix_api = MatrixHttpApi(config['MATRIX_HOST'], + token=config['MATRIX_AS_TOKEN'], + identity=pnut_user) + url = matrix_url + '/createRoom' + params = {"access_token": config['MATRIX_AS_TOKEN'], "user_id": pnut_user} + content = {"visibility": "private", "is_direct": True, "invite": invitees} + headers = {"Content-Type": "application/json"} + r = requests.post(url, headers=headers, params=params, + data=json.dumps(content)) + response = r.json() + for bridge_user in invitees: + dr = DirectRooms(room_id=response['room_id'], + bridge_user=pnut_user, pnut_chan=chan) + logger.debug(dr) + db_session.add(dr) + db_session.commit() + + return dr + def on_message(ws, message): # logger.debug("on_message: " + message) msg = json.loads(message) @@ -254,8 +295,8 @@ def on_message(ws, message): if 'channel_type' in msg['meta']: - # TODO: bypassed other channel types for now - if msg['meta']['channel_type'] != 'io.pnut.core.chat': + if msg['meta']['channel_type'] not in ['io.pnut.core.chat', + 'io.pnut.core.pm']: return for d_item in msg['data']: @@ -265,10 +306,8 @@ def on_message(ws, message): if msg['meta']['is_deleted']: logger.debug("message: delete") delete_message(pmsg) - else: - logger.debug("uh whut?") else: - new_message(pmsg) + new_message(pmsg, msg['meta']) def on_error(ws, error): logger.error("on_error: !!! ERROR !!!") -- 2.45.2 From 4bebfcbc76e2163c00c495585b7ae9aa233cdc63 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sat, 6 Mar 2021 11:06:38 -0800 Subject: [PATCH 2/6] handle permission error correctly --- appservice.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/appservice.py b/appservice.py index 550dc60..f0d75cf 100644 --- a/appservice.py +++ b/appservice.py @@ -77,6 +77,9 @@ def query_alias(alias): db_session.add(rr) db_session.commit() + except pnutpy.errors.PnutPermissionDenied: + abort(401) + except Exception: logger.exception("-couldn't get the pnut channel-") abort(404) -- 2.45.2 From a16732c8c68df009d9f52f4c9d746e7907725d81 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sat, 6 Mar 2021 11:07:08 -0800 Subject: [PATCH 3/6] initial private channel support issue #11 --- appservice.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/appservice.py b/appservice.py index f0d75cf..b673a05 100644 --- a/appservice.py +++ b/appservice.py @@ -345,6 +345,46 @@ def get_channel_settings(channel_id): return channel_settings +def create_room(channel, invite): + channel_settings = {} + for item in channel.raw: + if item.type == 'io.pnut.core.chat-settings': + channel_settings = item.value + + # Matrix sdk doesn't include all details in a single call + room = {'room_alias_name': app.config['MATRIX_PNUT_PREFIX'] + channel.id} + logger.debug(invite) + logger.debug(room) + room['invite'] = [invite] + if 'name' in channel_settings: + room['name'] = channel_settings['name'] + if 'description' in channel_settings: + room['topic'] = channel_settings['description'] + if channel.acl.read.any_user: + room['preset'] = 'public_chat' + room['visibility'] = 'public' + else: + room['preset'] = 'private_chat' + room['visibility'] = 'private' + + url = app.config['MATRIX_HOST'] + '/_matrix/client/api/v1/createRoom?access_token=' + url += app.config['MATRIX_AS_TOKEN'] + headers = {"Content-Type":"application/json"} + r = requests.post(url, headers=headers, data=json.dumps(room)) + + if r.status_code == 200: + #pnutpy.api.subscribe_channel(channel.id) + rdata = r.json() + rr = Rooms( + room_id=rdata['room_id'], + pnut_chan=channel.id, + portal=True + ) + db_session.add(rr) + db_session.commit() + logger.debug(r.status_code) + logger.debug(r) + def on_admin_event(event): matrix_api = MatrixHttpApi(app.config['MATRIX_HOST'], token=app.config['MATRIX_AS_TOKEN']) @@ -643,6 +683,13 @@ def on_control_message(event): elif msg[0] == '!status': matrix_api.send_message(event['room_id'], cmd_user_status(event['sender'])) + elif msg[0] == '!join': + if len(msg) > 1: + matrix_api.send_message(event['room_id'], cmd_user_join(event['sender'], msg[1])) + else: + matrix_api.send_message(event['room_id'], cmd_user_join(event['sender'])) + + except Exception: errmsg = "- on_direct_message -" logger.exception(errmsg) @@ -656,6 +703,7 @@ def cmd_user_help(cmd=None): reply += "!save \t- Save your pnut.io auth token\n" reply += "!drop\t\t\t- Drop your pnut.io auth token\n" reply += "!status\t\t\t- Status of your pnut.io auth token\n" + reply += "!join \t- Join a private channel on pnut.io\n" return reply @@ -732,3 +780,31 @@ def cmd_user_status(sender=None): reply = "Error! There was a problem checking your account." return reply + +def cmd_user_join(sender=None, channel_id=None): + if channel_id is None: + reply = "You must provide a channel id number with this command.\n" + reply += "!join " + return reply + + try: + user = Users.query.filter(Users.matrix_id == sender).one_or_none() + if user is None: + reply = "You are currently not authorized on pnut.io" + else: + pnutpy.api.add_authorization_token(user.pnut_user_token) + channel, meta = pnutpy.api.get_channel(channel_id, include_raw=1) + create_room(channel, user.matrix_id) + reply = "is working?" + + except pnutpy.errors.PnutAuthAPIException as e: + reply = "You are currently not authorized on pnut.io" + + except pnutpy.errors.PnutPermissionDenied: + reply = "You are not authorized for this channel" + + except Exception: + logging.exception('!join') + reply = "Error! There was a problem joining the channel." + + return reply -- 2.45.2 From d1cc3f5949cbf23e0924fdc3b2eb09533f9c1323 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sat, 6 Mar 2021 12:33:09 -0800 Subject: [PATCH 4/6] create ghost user if not exist --- pnut-matrix.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pnut-matrix.py b/pnut-matrix.py index 0617b39..19eb9ce 100644 --- a/pnut-matrix.py +++ b/pnut-matrix.py @@ -47,6 +47,9 @@ def new_message(msg, meta): # subscribed_user_ids from meta logger.debug(meta['subscribed_user_ids']) pnut_user = matrix_id_from_pnut(msg.user.username) + profile = get_matrix_profile(pnut_user) + if not profile: + new_matrix_user(msg.user.username) invitees=[] for pm_user in meta['subscribed_user_ids']: user = Users.query.filter(Users.pnut_user_id == pm_user).one_or_none() -- 2.45.2 From 319ff228655c585f3c8c3bdaf463123251fea3c3 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sat, 6 Mar 2021 14:41:38 -0800 Subject: [PATCH 5/6] fix media from event in pm --- appservice.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/appservice.py b/appservice.py index b673a05..a0d4ca2 100644 --- a/appservice.py +++ b/appservice.py @@ -157,7 +157,10 @@ def new_message(event, user): pnutpy.api.add_authorization_token(token) embed = [crosspost_raw(event)] - text = prefix + msg_from_event(event) + evtext, evraw = msg_from_event(event) + text = prefix + evtext + if len(evraw) > 0: + embed.append(evraw) try: msg, meta = pnutpy.api.create_message(room.pnut_chan, data={'text': text, 'raw': embed}) revent = Events( @@ -203,6 +206,7 @@ def new_message(event, user): def msg_from_event(event): text = None + raw = {} if event['content']['msgtype'] == 'm.text' or event['content']['msgtype'] == 'm.notice': text = event['content']['body'] @@ -212,7 +216,7 @@ def msg_from_event(event): elif event['content']['msgtype'] == 'm.image': text = event['content']['body'] + "\n" text += app.config['MATRIX_URL'] + '/_matrix/media/r0/download/' + event['content']['url'][6:] - embed.append(raw_from_event(event)) + raw = raw_from_event(event) elif event['content']['msgtype'] == 'm.video': text = event['content']['body'] + "\n" @@ -230,7 +234,7 @@ def msg_from_event(event): logger.debug('-unknown msg type- ' + event['content']['msgtype']) return - return text + return text, raw def crosspost_raw(event): cross_profile = {'username': event['user_id']} @@ -629,7 +633,10 @@ def on_direct_message(event, user, room): pnutpy.api.add_authorization_token(token) embed = [crosspost_raw(event)] - text = prefix + msg_from_event(event) + evtext, evraw = msg_from_event(event) + text = prefix + evtext + if len(evraw) > 0: + embed.append(evraw) try: msg, meta = pnutpy.api.create_message(room.pnut_chan, data={'text': text, 'raw': embed}) revent = Events( -- 2.45.2 From 1d7aa671eaa91f2cda2e5899a097cd9bc126a219 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sat, 20 Mar 2021 06:08:00 -0700 Subject: [PATCH 6/6] fetch the users profile --- appservice.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/appservice.py b/appservice.py index a0d4ca2..8c14bb9 100644 --- a/appservice.py +++ b/appservice.py @@ -150,6 +150,7 @@ def new_message(event, user): prefix = "" else: token = app.config['MATRIX_PNUT_TOKEN'] + matrix_profile = get_profile(event['user_id']) if "displayname" in matrix_profile: prefix = "[" + matrix_profile['displayname'] + "] (" + event['user_id'] + ")\n" else: @@ -626,6 +627,7 @@ def on_direct_message(event, user, room): prefix = "" else: token = app.config['MATRIX_PNUT_TOKEN'] + matrix_profile = get_profile(event['user_id']) if "displayname" in matrix_profile: prefix = "[" + matrix_profile['displayname'] + "] (" + event['user_id'] + ")\n" else: -- 2.45.2