From dfccd44b1d527b100edcfe3d0a999895cf56a503 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Thu, 4 May 2017 16:40:32 -0700 Subject: [PATCH] only pay attention to memeber events if they involve the AS user id. Should resolve issue #8 --- appservice.py | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/appservice.py b/appservice.py index 93c8431..b442ba9 100644 --- a/appservice.py +++ b/appservice.py @@ -108,21 +108,36 @@ def on_receive_events(transaction): elif event['type'] == 'm.room.member': - if event['content']['membership'] == 'invite' and event['content']['is_direct'] == True: - logging.info('>> GOT INVITE') - txId += 1 - url = app.config['MATRIX_HOST'] - url += '/_matrix/client/r0/join/' + event['room_id'] - url += "?access_token=" + app.config['MATRIX_AS_TOKEN'] - r = requests.post(url, headers={"Content-Type": "application/json"}, data=json.dumps({})) + if event['state_key'] == app.config['MATRIX_AS_ID']: - elif event['membership'] == 'leave': - logging.info('>> GOT LEAVE') - txId += 1 - url = app.config['MATRIX_HOST'] - url += '/_matrix/client/r0/rooms/' + event['room_id'] + "/leave" - url += "?access_token=" + app.config['MATRIX_AS_TOKEN'] - r = requests.post(url, headers={"Content-Type": "application/json"}, data=json.dumps({})) + if event['content']['membership'] == 'invite' + and 'is_direct' in event['content'] + and event['content']['is_direct'] == True: + + logging.info('>> GOT INVITE') + txId += 1 + url = app.config['MATRIX_HOST'] + url += '/_matrix/client/r0/join/' + event['room_id'] + url += "?access_token=" + app.config['MATRIX_AS_TOKEN'] + r = requests.post(url, headers={"Content-Type": "application/json"}, + data=json.dumps({})) + if r.status_code == 200: + addadminrm = MatrixAdminRooms(matrix_id=event['sender'], room_id=event['room_id']) + db.session.add(addadminrm) + db.session.commit() + + elif event['membership'] == 'leave': + logging.info('>> GOT LEAVE') + txId += 1 + url = app.config['MATRIX_HOST'] + url += '/_matrix/client/r0/rooms/' + event['room_id'] + "/leave" + url += "?access_token=" + app.config['MATRIX_AS_TOKEN'] + r = requests.post(url, headers={"Content-Type": "application/json"}, + data=json.dumps({})) + if r.status_code == 200: + adminrm = MatrixAdminRooms.query.filter_by(room_id=event['room_id']).first() + db.session.delete(adminrm) + db.session.commit() return jsonify({})