parent
3d96f910ee
commit
eef37d1360
1 changed files with 76 additions and 30 deletions
|
@ -14,8 +14,6 @@ from mautrix.types import *
|
||||||
from pnut_matrix.models import *
|
from pnut_matrix.models import *
|
||||||
from flask import Flask, jsonify, request, abort
|
from flask import Flask, jsonify, request, abort
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
@app.errorhandler(404)
|
@app.errorhandler(404)
|
||||||
|
@ -159,8 +157,13 @@ async def new_message(event, user):
|
||||||
logging.debug(f'room: {room}')
|
logging.debug(f'room: {room}')
|
||||||
|
|
||||||
if room is None:
|
if room is None:
|
||||||
logging.debug('-room not mapped-')
|
if event['room_id'] == app.config['MATRIX_GLOBAL_ROOM']:
|
||||||
return
|
room = PnutChannels(pnut_chan=0,
|
||||||
|
room_id=app.config['MATRIX_GLOBAL_ROOM'])
|
||||||
|
|
||||||
|
else:
|
||||||
|
logging.debug('-room not mapped-')
|
||||||
|
return
|
||||||
|
|
||||||
if room.is_direct:
|
if room.is_direct:
|
||||||
logging.debug('>----on_direct_message----<')
|
logging.debug('>----on_direct_message----<')
|
||||||
|
@ -189,13 +192,64 @@ async def new_message(event, user):
|
||||||
raw['io.pnut.core.oembed'] = [oembed]
|
raw['io.pnut.core.oembed'] = [oembed]
|
||||||
logging.debug(oembed)
|
logging.debug(oembed)
|
||||||
|
|
||||||
|
reply_to_id = None
|
||||||
|
ev_content = event['content']
|
||||||
|
if 'm.relates_to' in ev_content:
|
||||||
|
m_relates_to = ev_content['m.relates_to']
|
||||||
|
if 'm.in_reply_to' in m_relates_to:
|
||||||
|
reply_event_id = m_relates_to['m.in_reply_to']['event_id']
|
||||||
|
e = Events.select().where(Events.event_id ==
|
||||||
|
reply_event_id).first()
|
||||||
|
if e is not None:
|
||||||
|
reply_to_id = e.pnut_id
|
||||||
|
|
||||||
try:
|
try:
|
||||||
msg, meta = pnutpy.api.create_message(room.pnut_chan,
|
|
||||||
data={'text': text, 'raw': raw})
|
payload = {'raw': raw}
|
||||||
|
if room.pnut_chan == 0:
|
||||||
|
if reply_to_id is not None:
|
||||||
|
orig, meta = pnutpy.api.get_post(reply_to_id)
|
||||||
|
if orig.user.id != user.pnut_user_id:
|
||||||
|
author = orig.user.username
|
||||||
|
text = f"@{author} {text}"
|
||||||
|
if 'content' in orig:
|
||||||
|
cc_list = []
|
||||||
|
for m in orig.content.entities.mentions:
|
||||||
|
if m.text == author:
|
||||||
|
continue
|
||||||
|
cc_list.append(f"@{m.text}")
|
||||||
|
if len(cc_list) > 0:
|
||||||
|
copy = " ".join(cc_list)
|
||||||
|
text = f"{text} /{copy}"
|
||||||
|
payload['reply_to'] = reply_to_id
|
||||||
|
|
||||||
|
payload['text'] = text
|
||||||
|
data, meta = pnutpy.api.create_post(data=payload)
|
||||||
|
|
||||||
|
else:
|
||||||
|
if reply_to_id is not None:
|
||||||
|
orig, meta = pnutpy.api.get_message(room.pnut_chan, reply_to_id)
|
||||||
|
if orig.user.id != user.pnut_user_id:
|
||||||
|
author = orig.user.username
|
||||||
|
text = f"@{author} {text}"
|
||||||
|
if 'content' in orig:
|
||||||
|
cc_list = []
|
||||||
|
for m in orig.content.entities.mentions:
|
||||||
|
if m.text == author:
|
||||||
|
continue
|
||||||
|
cc_list.append(f"@{m.text}")
|
||||||
|
if len(cc_list) > 0:
|
||||||
|
copy = " ".join(cc_list)
|
||||||
|
text = f"{text} /{copy}"
|
||||||
|
payload['reply_to'] = reply_to_id
|
||||||
|
|
||||||
|
payload['text'] = text
|
||||||
|
data, meta = pnutpy.api.create_message(room.pnut_chan,
|
||||||
|
data=payload)
|
||||||
bridge_event = Events(
|
bridge_event = Events(
|
||||||
event_id=event['event_id'],
|
event_id=event['event_id'],
|
||||||
room_id=event['room_id'],
|
room_id=event['room_id'],
|
||||||
pnut_id=msg.id,
|
pnut_id=data.id,
|
||||||
pnut_channel=room.pnut_chan
|
pnut_channel=room.pnut_chan
|
||||||
)
|
)
|
||||||
bridge_event.save()
|
bridge_event.save()
|
||||||
|
@ -224,8 +278,14 @@ async def new_message(event, user):
|
||||||
# r, meta = pnutpy.api.create_post(data={'text': text, 'raw': raw})
|
# r, meta = pnutpy.api.create_post(data={'text': text, 'raw': raw})
|
||||||
|
|
||||||
except pnutpy.errors.PnutAuthAPIException:
|
except pnutpy.errors.PnutAuthAPIException:
|
||||||
logging.exception('-unable to post to pnut channel-')
|
if room.pnut_chan != 0:
|
||||||
return
|
logging.exception('-unable to post to pnut channel-')
|
||||||
|
else:
|
||||||
|
matrix_api = ClientAPI(app.config['MATRIX_AS_ID'],
|
||||||
|
base_url=app.config['MATRIX_HOST'],
|
||||||
|
token=app.config['MATRIX_AS_TOKEN'])
|
||||||
|
await matrix_api.redact(event['room_id'], event['event_id'],
|
||||||
|
reason='user not authenticated')
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
logging.exception('-something bad happened here-')
|
logging.exception('-something bad happened here-')
|
||||||
|
@ -338,27 +398,24 @@ def oembed_from_event(event):
|
||||||
return oembed
|
return oembed
|
||||||
|
|
||||||
def delete_message(event, user):
|
def delete_message(event, user):
|
||||||
|
|
||||||
# TODO: should there be moderator handled redactions?
|
|
||||||
|
|
||||||
if user is not None:
|
if user is not None:
|
||||||
token = user.pnut_user_token
|
token = user.pnut_user_token
|
||||||
else:
|
else:
|
||||||
token = app.config['MATRIX_PNUT_TOKEN']
|
token = app.config['MATRIX_PNUT_TOKEN']
|
||||||
pnutpy.api.add_authorization_token(token)
|
pnutpy.api.add_authorization_token(token)
|
||||||
|
|
||||||
e = Events.select().where((Events.event_id == events['redacts']) &
|
e = Events.select().where((Events.event_id == event['redacts']) &
|
||||||
(Events.deleted == False)).first()
|
(Events.deleted == False)).first()
|
||||||
if e is None:
|
if e is None:
|
||||||
logging.debug("- can't find the event to remove -")
|
logging.debug("- can't find the event to remove -")
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
r, meta = pnutpy.api.delete_message(e.pnut_chan_id, e.pnut_msg_id)
|
r, meta = pnutpy.api.delete_message(e.pnut_channel, e.pnut_id)
|
||||||
e.deleted = True
|
e.deleted = True
|
||||||
e.save()
|
e.save()
|
||||||
|
|
||||||
except pnutpy.errors.PnutPermissionDenied as e:
|
except pnutpy.errors.PnutPermissionDenied:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def get_profile(userid):
|
def get_profile(userid):
|
||||||
|
@ -580,8 +637,10 @@ async def on_direct_invite(event):
|
||||||
logging.exception(errmsg)
|
logging.exception(errmsg)
|
||||||
|
|
||||||
async def on_leave_event(event):
|
async def on_leave_event(event):
|
||||||
direct_room = PnutChannels.select().where(PnutChannels.room_id ==
|
direct_room = PnutChannels.select().where(
|
||||||
event['room_id']).first()
|
(PnutChannels.room_id == event['room_id']) &
|
||||||
|
(PnutChannels.direct_mtrx_user == event['sender']) &
|
||||||
|
(PnutChannels.is_direct == True)).first()
|
||||||
|
|
||||||
user = PnutUsers.select().where(PnutUsers.room_id ==
|
user = PnutUsers.select().where(PnutUsers.room_id ==
|
||||||
event['room_id']).first()
|
event['room_id']).first()
|
||||||
|
@ -600,19 +659,6 @@ async def on_leave_event(event):
|
||||||
errmsg = "- on_leave_event -"
|
errmsg = "- on_leave_event -"
|
||||||
logging.exception(errmsg)
|
logging.exception(errmsg)
|
||||||
|
|
||||||
if user is not None:
|
|
||||||
matrix_api = ClientAPI(app.config['MATRIX_AS_ID'],
|
|
||||||
base_url=app.config['MATRIX_HOST'],
|
|
||||||
token=app.config['MATRIX_AS_TOKEN'])
|
|
||||||
try:
|
|
||||||
await matrix_api.leave_room(event['room_id'])
|
|
||||||
user.room_id = None
|
|
||||||
user.save()
|
|
||||||
|
|
||||||
except Exception:
|
|
||||||
errmsg = "- on_leave_event -"
|
|
||||||
logging.exception(errmsg)
|
|
||||||
|
|
||||||
def on_direct_message(event, user, room):
|
def on_direct_message(event, user, room):
|
||||||
|
|
||||||
if user is not None:
|
if user is not None:
|
||||||
|
|
Loading…
Reference in a new issue