add media upload for authenticated users

This commit is contained in:
Morgan McMillian 2024-12-23 14:53:26 -08:00
parent eef37d1360
commit 7dad1fef3b

View file

@ -186,7 +186,7 @@ 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 = msg_from_event(event) text, oembed = await msg_from_event(event, user)
text = prefix + text text = prefix + text
if oembed: if oembed:
raw['io.pnut.core.oembed'] = [oembed] raw['io.pnut.core.oembed'] = [oembed]
@ -291,7 +291,7 @@ async def new_message(event, user):
logging.exception('-something bad happened here-') logging.exception('-something bad happened here-')
return return
def msg_from_event(event): async def msg_from_event(event, user):
text = None text = None
oembed = None oembed = None
if (event['content']['msgtype'] == 'm.text' or if (event['content']['msgtype'] == 'm.text' or
@ -301,6 +301,10 @@ def msg_from_event(event):
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
user is not None):
oembed = await media_from_event(event, user)
text = event['content']['body']
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'):
@ -342,13 +346,38 @@ def crosspost_raw(event):
return crosspost return crosspost
# TODO: This could be used for uploading the media to pnut, maybe # TODO: This could be used for uploading the media to pnut, maybe
# async def media_from_event(event): async def media_from_event(event, user):
# matrix_api = ClientAPI(app.config['MATRIX_AS_ID'], mxc_url = event['content']['url']
# base_url=app.config['MATRIX_HOST'], if event['content']['msgtype'] == 'm.image':
# token=app.config['MATRIX_AS_TOKEN']) kind = 'image'
# else:
# mxc_url = event['content']['url'] kind = 'other'
# media_file = await matrix_api.download_media(mxc_url) mime_type = event['content']['info']['mimetype']
file_name = event['content']['body']
file_data = {'type': f'dev.mcmillian.pnut-matrix.{kind}',
'name': file_name, 'kind': kind,
'mimetype': mime_type, 'is_public': True}
matrix_api = ClientAPI(app.config['MATRIX_AS_ID'],
base_url=app.config['MATRIX_HOST'],
token=app.config['MATRIX_AS_TOKEN'])
media_file = await matrix_api.download_media(mxc_url)
pnutpy.api.add_authorization_token(user.pnut_user_token)
try:
pnut_file, meta = pnutpy.api.create_file(files={'content': media_file},
data=file_data)
except pnutpy.errors.PnutPermissionDenied:
return {}
oembed = {
'+io.pnut.core.file': {
'file_id': pnut_file.id,
'file_token': pnut_file.file_token,
'format': 'oembed'}}
return oembed
def oembed_from_event(event): def oembed_from_event(event):
media_url = event['content']['url'][6:] media_url = event['content']['url'][6:]
@ -457,7 +486,9 @@ async def create_pnut_matrix_room(channel, user):
if 'io.pnut.core.chat-settings' in channel.raw: if 'io.pnut.core.chat-settings' in channel.raw:
for setting in channel.raw['io.pnut.core.chat-settings']: for setting in channel.raw['io.pnut.core.chat-settings']:
if 'name' in setting: if 'name' in setting:
name = setting['name'] name = f"🥜 {setting['name']}"
else:
name = f"🥜 channel {channel.id}"
if 'description' in setting: if 'description' in setting:
topic = setting['description']['text'] topic = setting['description']['text']
@ -764,7 +795,7 @@ def cmd_user_auth():
reply += "https://pnut.io/oauth/authenticate" reply += "https://pnut.io/oauth/authenticate"
reply += "?client_id=6SeCRCpCZkmZOKFLFGWbcdAeq2fX1M5t" reply += "?client_id=6SeCRCpCZkmZOKFLFGWbcdAeq2fX1M5t"
reply += "&redirect_uri=urn:ietf:wg:oauth:2.0:oob" reply += "&redirect_uri=urn:ietf:wg:oauth:2.0:oob"
reply += "&scope=write_post,presence,messages&response_type=token\n\n" reply += "&scope=files,write_post,presence,messages&response_type=token\n\n"
reply += "You will be provided with a token that you store with the !save command.\n" reply += "You will be provided with a token that you store with the !save command.\n"
return TextMessageEventContent(msgtype='m.text', body=reply) return TextMessageEventContent(msgtype='m.text', body=reply)