diff --git a/appservice.py b/appservice.py index 23ad6b4..8ffcfc8 100644 --- a/appservice.py +++ b/appservice.py @@ -140,16 +140,34 @@ def new_message(event, user): 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:] + if user is not None: token = user.pnut_user_token prefix = "" else: token = app.config['MATRIX_PNUT_TOKEN'] - prefix = "[" + get_displayname(event['user_id']) + "] (" + event['user_id'] + ")\n" + 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) text = None - embed = 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'] @@ -160,7 +178,7 @@ def new_message(event, user): 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 = [raw_from_event(event)] + embed.append(raw_from_event(event)) elif event['content']['msgtype'] == 'm.video': text = event['content']['body'] + "\n" @@ -295,13 +313,11 @@ def delete_message(event, user): except pnutpy.errors.PnutPermissionDenied as e: pass -def get_displayname(userid): +def get_profile(userid): url = app.config['MATRIX_HOST'] + "/_matrix/client/r0/profile/" + userid r = requests.get(url) if r.status_code == 200: - data = json.loads(r.text) - if 'displayname' in data: - return data["displayname"] + return json.loads(r.text) return userid def get_channel_settings(channel_id):