diff --git a/src/pnut_matrix/appservice.py b/src/pnut_matrix/appservice.py index 60f70fe..70b0481 100644 --- a/src/pnut_matrix/appservice.py +++ b/src/pnut_matrix/appservice.py @@ -187,6 +187,8 @@ async def new_message(event, user): raw = {} raw['io.pnut.core.crosspost'] = [crosspost_raw(event)] text, oembed = await msg_from_event(event, user) + if text is None: + return text = prefix + text if oembed: raw['io.pnut.core.oembed'] = [oembed] @@ -298,13 +300,31 @@ async def msg_from_event(event, user): event['content']['msgtype'] == 'm.notice'): text = event['content']['body'] + fregex = re.compile(r'!file\s(\d+)') + m = fregex.search(text) + if m is not None: + file_id = m.group(1) + text = fregex.sub('', text) + + pnut_file = PnutUploads.select().where(PnutUploads.pnut_file_id == + file_id).first() + if pnut_file is not None: + oembed = { + '+io.pnut.core.file': { + 'file_id': pnut_file.pnut_file_id, + 'file_token': pnut_file.pnut_file_token, + 'format': 'oembed'}} + elif event['content']['msgtype'] == 'm.emote': text = "* " + event['content']['body'] - elif (event['content']['msgtype'] == 'm.image' and + elif ((event['content']['msgtype'] == 'm.image' or + event['content']['msgtype'] == 'm.video' or + event['content']['msgtype'] == 'm.audio') and user is not None): - oembed = await media_from_event(event, user) - text = event['content']['body'] + await media_from_event(event, user) + return None, None + elif (event['content']['msgtype'] == 'm.image' or event['content']['msgtype'] == 'm.video' or event['content']['msgtype'] == 'm.audio'): @@ -345,11 +365,14 @@ def crosspost_raw(event): return crosspost -# TODO: This could be used for uploading the media to pnut, maybe async def media_from_event(event, user): mxc_url = event['content']['url'] if event['content']['msgtype'] == 'm.image': kind = 'image' + elif event['content']['msgtype'] == 'm.video': + kind = 'video' + elif event['content']['msgtype'] == 'm.audio': + kind = 'audio' else: kind = 'other' mime_type = event['content']['info']['mimetype'] @@ -368,16 +391,24 @@ async def media_from_event(event, user): pnut_file, meta = pnutpy.api.create_file(files={'content': media_file}, data=file_data) + qf = PnutUploads(pnut_user_id=user.pnut_user_id, + pnut_file_id=pnut_file.id, + pnut_file_token=pnut_file.file_token) + qf.save() + + reply = f"* Media upload ready, " + reply += f"attach to message with !file {pnut_file.id}" + except pnutpy.errors.PnutPermissionDenied: - return {} + reply = f"* Permission denied, you may need to authenticate again." - oembed = { - '+io.pnut.core.file': { - 'file_id': pnut_file.id, - 'file_token': pnut_file.file_token, - 'format': 'oembed'}} + try: + message = TextMessageEventContent(msgtype='m.text', body=reply) + await matrix_api.send_message(event['room_id'], message) - return oembed + except Exception: + errmsg = "- media_from_event -" + logging.exception(errmsg) def oembed_from_event(event): media_url = event['content']['url'][6:] diff --git a/src/pnut_matrix/models.py b/src/pnut_matrix/models.py index 80ce5a0..6bd8c57 100644 --- a/src/pnut_matrix/models.py +++ b/src/pnut_matrix/models.py @@ -42,7 +42,12 @@ class PnutUsers(BaseModel): pnut_user_id = IntegerField(unique=True, null=True) pnut_user_token = CharField(null=True) +class PnutUploads(BaseModel): + pnut_user_id = IntegerField() + pnut_file_id = IntegerField() + pnut_file_token = CharField() + def db_create_tables(): with db: db.create_tables([PnutUsers, Events, PnutChannels, PnutAvatars, - PnutPrivateChanMembers]) + PnutPrivateChanMembers, PnutUploads])