diff --git a/appservice.py b/appservice.py index 62dee59..3c66a33 100644 --- a/appservice.py +++ b/appservice.py @@ -58,9 +58,11 @@ def query_alias(alias): room['name'] = channel_settings['name'] if 'description' in channel_settings: room['topic'] = channel_settings['description'] - if channel.acl.read.any_user: + if channel.acl.read.public: room['preset'] = 'public_chat' room['visibility'] = 'public' + else: + abort(401) url = app.config['MATRIX_HOST'] + '/_matrix/client/api/v1/createRoom?access_token=' url += app.config['MATRIX_AS_TOKEN'] @@ -350,7 +352,7 @@ def get_channel_settings(channel_id): return channel_settings -def create_room(channel, invite): +def create_room(channel, user): channel_settings = {} for item in channel.raw: if item.type == 'io.pnut.core.chat-settings': @@ -358,19 +360,21 @@ def create_room(channel, invite): # 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(user) logger.debug(room) - room['invite'] = [invite] + room['invite'] = [user.matrix_id] 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: + if channel.acl.read.public: room['preset'] = 'public_chat' room['visibility'] = 'public' - else: + elif channel.acl.read.any_user or channel.acl.read.you: room['preset'] = 'private_chat' room['visibility'] = 'private' + else: + abort(401) url = app.config['MATRIX_HOST'] + '/_matrix/client/api/v1/createRoom?access_token=' url += app.config['MATRIX_AS_TOKEN'] @@ -523,15 +527,15 @@ def cmd_admin_link(room_id, pnut_chan_id): logger.exception(errmsg) return errmsg -def cmd_admin_unlink(id): +def cmd_admin_unlink(rid): matrix_api = MatrixHttpApi(app.config['MATRIX_HOST'], token=app.config['MATRIX_AS_TOKEN']) pnutpy.api.add_authorization_token(app.config['MATRIX_PNUT_TOKEN']) - if id.startswith('!'): - room = Rooms.query.filter(Rooms.room_id == id).one_or_none() + if rid.startswith('!'): + room = Rooms.query.filter(Rooms.room_id == rid).one_or_none() else: - room = Rooms.query.filter(Rooms.pnut_chan == id).one_or_none() + room = Rooms.query.filter(Rooms.pnut_chan == rid).one_or_none() if hasattr(room, 'portal'): if room.portal: @@ -815,8 +819,14 @@ def cmd_user_join(sender=None, channel_id=None): 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?" + room = Rooms.query.filter(Rooms.pnut_chan == channel_id).one_or_none() + if room is None: + create_room(channel, user) + else: + matrix_api = MatrixHttpApi(app.config['MATRIX_HOST'], + token=app.config['MATRIX_AS_TOKEN']) + matrix_api.invite_user(room.room_id, sender) + reply = "ok" except pnutpy.errors.PnutAuthAPIException as e: reply = "You are currently not authorized on pnut.io"