From a16732c8c68df009d9f52f4c9d746e7907725d81 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sat, 6 Mar 2021 11:07:08 -0800 Subject: [PATCH] initial private channel support issue #11 --- appservice.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/appservice.py b/appservice.py index f0d75cf..b673a05 100644 --- a/appservice.py +++ b/appservice.py @@ -345,6 +345,46 @@ def get_channel_settings(channel_id): return channel_settings +def create_room(channel, invite): + channel_settings = {} + for item in channel.raw: + if item.type == 'io.pnut.core.chat-settings': + channel_settings = item.value + + # Matrix sdk doesn't include all details in a single call + room = {'room_alias_name': app.config['MATRIX_PNUT_PREFIX'] + channel.id} + logger.debug(invite) + logger.debug(room) + room['invite'] = [invite] + if 'name' in channel_settings: + room['name'] = channel_settings['name'] + if 'description' in channel_settings: + room['topic'] = channel_settings['description'] + if channel.acl.read.any_user: + room['preset'] = 'public_chat' + room['visibility'] = 'public' + else: + room['preset'] = 'private_chat' + room['visibility'] = 'private' + + url = app.config['MATRIX_HOST'] + '/_matrix/client/api/v1/createRoom?access_token=' + url += app.config['MATRIX_AS_TOKEN'] + headers = {"Content-Type":"application/json"} + r = requests.post(url, headers=headers, data=json.dumps(room)) + + if r.status_code == 200: + #pnutpy.api.subscribe_channel(channel.id) + rdata = r.json() + rr = Rooms( + room_id=rdata['room_id'], + pnut_chan=channel.id, + portal=True + ) + db_session.add(rr) + db_session.commit() + logger.debug(r.status_code) + logger.debug(r) + def on_admin_event(event): matrix_api = MatrixHttpApi(app.config['MATRIX_HOST'], token=app.config['MATRIX_AS_TOKEN']) @@ -643,6 +683,13 @@ def on_control_message(event): elif msg[0] == '!status': matrix_api.send_message(event['room_id'], cmd_user_status(event['sender'])) + elif msg[0] == '!join': + if len(msg) > 1: + matrix_api.send_message(event['room_id'], cmd_user_join(event['sender'], msg[1])) + else: + matrix_api.send_message(event['room_id'], cmd_user_join(event['sender'])) + + except Exception: errmsg = "- on_direct_message -" logger.exception(errmsg) @@ -656,6 +703,7 @@ def cmd_user_help(cmd=None): reply += "!save \t- Save your pnut.io auth token\n" reply += "!drop\t\t\t- Drop your pnut.io auth token\n" reply += "!status\t\t\t- Status of your pnut.io auth token\n" + reply += "!join \t- Join a private channel on pnut.io\n" return reply @@ -732,3 +780,31 @@ def cmd_user_status(sender=None): reply = "Error! There was a problem checking your account." return reply + +def cmd_user_join(sender=None, channel_id=None): + if channel_id is None: + reply = "You must provide a channel id number with this command.\n" + reply += "!join " + return reply + + try: + user = Users.query.filter(Users.matrix_id == sender).one_or_none() + if user is None: + reply = "You are currently not authorized on pnut.io" + else: + pnutpy.api.add_authorization_token(user.pnut_user_token) + channel, meta = pnutpy.api.get_channel(channel_id, include_raw=1) + create_room(channel, user.matrix_id) + reply = "is working?" + + except pnutpy.errors.PnutAuthAPIException as e: + reply = "You are currently not authorized on pnut.io" + + except pnutpy.errors.PnutPermissionDenied: + reply = "You are not authorized for this channel" + + except Exception: + logging.exception('!join') + reply = "Error! There was a problem joining the channel." + + return reply