sticker support

closes issue #43
This commit is contained in:
Morgan McMillian 2022-07-01 14:58:32 -07:00
parent ad49c3a354
commit 05893172f8

View file

@ -107,6 +107,9 @@ def on_receive_events(transaction):
if event['type'] == 'm.room.message': if event['type'] == 'm.room.message':
new_message(event, user) new_message(event, user)
elif event['type'] == 'm.sticker':
new_sticker(event, user)
elif event['type'] == 'm.room.redaction': elif event['type'] == 'm.room.redaction':
delete_message(event, user) delete_message(event, user)
@ -125,6 +128,60 @@ def on_receive_events(transaction):
return jsonify({}) return jsonify({})
def new_sticker(event, user):
if app.config['MATRIX_PNUT_PREFIX'] in event['user_id'] or 'pnut-bridge' in event['user_id']:
logger.debug('-skipping dup event-')
return
room = Rooms.query.filter(Rooms.room_id == event['room_id']).one_or_none()
if room is None:
logger.debug('-room not mapped-')
return
if user is not None:
token = user.pnut_user_token
prefix = ""
else:
token = app.config['MATRIX_PNUT_TOKEN']
matrix_profile = get_profile(event['user_id'])
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)
# evtext, evraw = msg_from_event(event)
text = "sticker::" + event['content']['body'] + "\n"
value = {'type': "photo", 'version': "1.0"}
value['url'] = app.config['MATRIX_URL'] + '/_matrix/media/r0/download/' + event['content']['url'][6:]
value['title'] = event['content']['body']
value['width'] = event['content']['info']['thumbnail_info']['w']
value['height'] = event['content']['info']['thumbnail_info']['h']
raw = {'type': "io.pnut.core.oembed", 'value': value}
embed = [raw]
text = prefix + text
try:
msg, meta = pnutpy.api.create_message(room.pnut_chan, data={'text': text, 'raw': embed})
revent = Events(
event_id=event['event_id'],
room_id=event['room_id'],
pnut_msg_id=msg.id,
pnut_user_id=msg.user.id,
pnut_chan_id=room.pnut_chan,
deleted=False
)
db_session.add(revent)
db_session.commit()
except pnutpy.errors.PnutAuthAPIException:
logger.exception('-unable to post to pnut channel-')
return
except Exception:
logger.exception('-something bad happened here-')
return
def new_message(event, user): def new_message(event, user):
if app.config['MATRIX_PNUT_PREFIX'] in event['user_id'] or 'pnut-bridge' in event['user_id']: if app.config['MATRIX_PNUT_PREFIX'] in event['user_id'] or 'pnut-bridge' in event['user_id']:
logger.debug('-skipping dup event-') logger.debug('-skipping dup event-')