setup media upload queue to handle embedding media in posts to pnut

This commit is contained in:
Morgan McMillian 2024-12-24 16:04:02 -08:00
parent 7dad1fef3b
commit fece9465e0
2 changed files with 48 additions and 12 deletions

View file

@ -187,6 +187,8 @@ async def new_message(event, user):
raw = {} raw = {}
raw['io.pnut.core.crosspost'] = [crosspost_raw(event)] raw['io.pnut.core.crosspost'] = [crosspost_raw(event)]
text, oembed = await msg_from_event(event, user) text, oembed = await msg_from_event(event, user)
if text is None:
return
text = prefix + text text = prefix + text
if oembed: if oembed:
raw['io.pnut.core.oembed'] = [oembed] raw['io.pnut.core.oembed'] = [oembed]
@ -298,13 +300,31 @@ async def msg_from_event(event, user):
event['content']['msgtype'] == 'm.notice'): event['content']['msgtype'] == 'm.notice'):
text = event['content']['body'] 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': elif event['content']['msgtype'] == 'm.emote':
text = "* " + event['content']['body'] 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): user is not None):
oembed = await media_from_event(event, user) await media_from_event(event, user)
text = event['content']['body'] return None, None
elif (event['content']['msgtype'] == 'm.image' or elif (event['content']['msgtype'] == 'm.image' or
event['content']['msgtype'] == 'm.video' or event['content']['msgtype'] == 'm.video' or
event['content']['msgtype'] == 'm.audio'): event['content']['msgtype'] == 'm.audio'):
@ -345,11 +365,14 @@ def crosspost_raw(event):
return crosspost return crosspost
# TODO: This could be used for uploading the media to pnut, maybe
async def media_from_event(event, user): async def media_from_event(event, user):
mxc_url = event['content']['url'] mxc_url = event['content']['url']
if event['content']['msgtype'] == 'm.image': if event['content']['msgtype'] == 'm.image':
kind = 'image' kind = 'image'
elif event['content']['msgtype'] == 'm.video':
kind = 'video'
elif event['content']['msgtype'] == 'm.audio':
kind = 'audio'
else: else:
kind = 'other' kind = 'other'
mime_type = event['content']['info']['mimetype'] 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}, pnut_file, meta = pnutpy.api.create_file(files={'content': media_file},
data=file_data) 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: except pnutpy.errors.PnutPermissionDenied:
return {} reply = f"* Permission denied, you may need to authenticate again."
oembed = { try:
'+io.pnut.core.file': { message = TextMessageEventContent(msgtype='m.text', body=reply)
'file_id': pnut_file.id, await matrix_api.send_message(event['room_id'], message)
'file_token': pnut_file.file_token,
'format': 'oembed'}}
return oembed except Exception:
errmsg = "- media_from_event -"
logging.exception(errmsg)
def oembed_from_event(event): def oembed_from_event(event):
media_url = event['content']['url'][6:] media_url = event['content']['url'][6:]

View file

@ -42,7 +42,12 @@ class PnutUsers(BaseModel):
pnut_user_id = IntegerField(unique=True, null=True) pnut_user_id = IntegerField(unique=True, null=True)
pnut_user_token = CharField(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(): def db_create_tables():
with db: with db:
db.create_tables([PnutUsers, Events, PnutChannels, PnutAvatars, db.create_tables([PnutUsers, Events, PnutChannels, PnutAvatars,
PnutPrivateChanMembers]) PnutPrivateChanMembers, PnutUploads])