From 2ce39ea06fb0d2c1a9e92a3ae4735d8e9615338c Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sun, 17 Jan 2021 21:45:50 -0800 Subject: [PATCH 1/3] fix link references for api v1 --- pnut-matrix.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/pnut-matrix.py b/pnut-matrix.py index c3eb2db..997a2b8 100644 --- a/pnut-matrix.py +++ b/pnut-matrix.py @@ -58,7 +58,7 @@ def new_message(msg): logger.debug('-set_display-') avatar = Avatars.query.filter(Avatars.pnut_user == msg.user.username).one_or_none() - if avatar is None or avatar.avatar != msg.user.content.avatar_image.link: + if avatar is None or avatar.avatar != msg.user.content.avatar_image.url: set_matrix_avatar(msg.user) logger.debug('-set_avatar-') @@ -76,8 +76,8 @@ def new_message(msg): for link in msg.content.entities.links: if 'title' in link: lnktext += link.title + "\n" - if 'link' in link: - lnktext += link.link + "\n" + if 'url' in link: + lnktext += link.url + "\n" if len(lnktext) > 0: text += "\n" + lnktext @@ -189,7 +189,7 @@ def set_matrix_avatar(user): token=config['MATRIX_AS_TOKEN'], identity=matrix_id) - dl = requests.get(user.content.avatar_image.link, stream=True) + dl = requests.get(user.content.avatar_image.url, stream=True) dl.raise_for_status() with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m: mtype = m.id_buffer(dl.content) @@ -199,10 +199,10 @@ def set_matrix_avatar(user): matrix_api.set_avatar_url(matrix_id, ul['content_uri']) avatar = Avatars.query.filter(Avatars.pnut_user == user.username).one_or_none() if avatar is None: - avatar = Avatars(pnut_user=user.username, avatar=user.content.avatar_image.link) + avatar = Avatars(pnut_user=user.username, avatar=user.content.avatar_image.url) db_session.add(avatar) else: - avatar.avatar = user.content.avatar_image.link + avatar.avatar = user.content.avatar_image.url db_session.commit() except MatrixRequestError: @@ -309,6 +309,7 @@ if __name__ == '__main__': '-d', action='store_true', dest='debug', help="debug logging" ) + # TODO: solve the database.py problem and enable this # a_parser.add_argument( # '-c', '--config', default="config.yaml", # help="configuration file" @@ -321,9 +322,6 @@ if __name__ == '__main__': else: logging.basicConfig(level=logging.INFO) - # with open(args.config, 'rb') as config_file: - # config = yaml.load(config_file, Loader=yaml.SafeLoader) - configyaml = os.environ.get("CONFIG_FILE") with open(configyaml, "rb") as config_file: From 49fd79e8822c28913fc710fcfd9f31c89e8d9946 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sun, 17 Jan 2021 21:47:31 -0800 Subject: [PATCH 2/3] fix raw object handling for api v1 --- pnut-matrix.py | 59 +++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/pnut-matrix.py b/pnut-matrix.py index 997a2b8..378ebe4 100644 --- a/pnut-matrix.py +++ b/pnut-matrix.py @@ -93,7 +93,7 @@ def new_message(msg): db_session.add(event) db_session.commit() - if len(msg.raw) > 0: + if 'raw' in msg: logger.debug('-handle media uploads-') new_media(room.room_id, msg) @@ -104,36 +104,45 @@ def new_media(room_id, msg): identity=matrix_id) ts = int(msg.created_at.strftime('%s')) * 1000 - for item in msg.raw: - if item.type == 'io.pnut.core.oembed' and 'url' in item.value: + if 'io.pnut.core.oembed' in msg.raw: - dl = requests.get(item.value.url, stream=True) + for oembed in msg.raw['io.pnut.core.oembed']: + info = {} + + if oembed.type == 'photo': + msgtype = 'm.image' + dl_url = oembed.url + info['h'] = oembed.height + info['w'] = oembed.width + elif oembed.type == 'audio': + logger.debug("* recieved audio attachment") + continue + elif oembed.type == 'video': + logger.debug("* recieved video attachment") + continue + elif oembed.type == 'html5video': + logger.debug("* recieved html5 video attachment") + continue + elif oembed.type == 'rich': + logger.debug("* recieved video attachment") + continue + else: + logger.debug("* recieved unknown attachment") + continue + + dl = requests.get(dl_url, stream=True) dl.raise_for_status() with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m: - mtype = m.id_buffer(dl.content) - info = {'mimetype': mtype} - + info['mimetype'] = m.id_buffer(dl.content) + info['size'] = dl.content ul = matrix_api.media_upload(dl.content, mtype) - if item.value.type == 'photo': - msgtype = 'm.image' - info['h'] = item.value.height - info['w'] = item.value.width - info['size'] = len(dl.content) - elif item.value.type == 'video' or item.value.type == 'html5video': - msgtype = 'm.video' - info['h'] = item.value.height - info['w'] = item.value.width - info['size'] = len(dl.content) - elif item.value.type == 'audio': - msgtype = 'm.audio' - info['duration'] = int(item.value.duration) * 1000 - info['size'] = len(dl.content) + if 'title' in oembed: + title = oembed.title else: - msgtype = 'm.file' - info['size'] = len(dl.content) + title = "" - r = matrix_api.send_content(room_id, ul['content_uri'], item.value.title, msgtype, extra_information=info, timestamp=ts) + r = matrix_api.send_content(room_id, ul['content_uri'], title, msgtype, extra_information=info, timestamp=ts) event = Events( event_id=r['event_id'], room_id=room_id, @@ -327,7 +336,7 @@ if __name__ == '__main__': with open(configyaml, "rb") as config_file: config = yaml.load(config_file, Loader=yaml.SafeLoader) - ws_url = 'wss://stream.pnut.io/v0/app?access_token=' + ws_url = 'wss://stream.pnut.io/v1/app?access_token=' ws_url += config['PNUT_APPTOKEN'] + '&key=' + config['PNUT_APPKEY'] ws_url += '&include_raw=1' matrix_url = config['MATRIX_HOST'] + '/_matrix/client/r0' From 07ea679ecd01b3a080c60ded0efa84c125db331d Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sat, 27 Feb 2021 10:08:59 -0800 Subject: [PATCH 3/3] fix channel type check and parsing payload --- pnut-matrix.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/pnut-matrix.py b/pnut-matrix.py index 378ebe4..e12f15e 100644 --- a/pnut-matrix.py +++ b/pnut-matrix.py @@ -252,22 +252,23 @@ def on_message(ws, message): if 'data' in msg: - if msg['meta']['type'] == "message": + if 'channel_type' in msg['meta']: # TODO: bypassed other channel types for now if msg['meta']['channel_type'] != 'io.pnut.core.chat': return - pmsg = pnutpy.models.Message.from_response_data(msg['data']) + for d_item in msg['data']: + pmsg = pnutpy.models.Message.from_response_data(d_item) - if 'is_deleted' in msg['meta']: - if msg['meta']['is_deleted']: - logger.debug("message: delete") - delete_message(pmsg) + if 'is_deleted' in msg['meta']: + if msg['meta']['is_deleted']: + logger.debug("message: delete") + delete_message(pmsg) + else: + logger.debug("uh whut?") else: - logger.debug("uh whut?") - else: - new_message(pmsg) + new_message(pmsg) def on_error(ws, error): logger.error("on_error: !!! ERROR !!!")