From 0b2a0aa9e58b195a2363fecf44da70ce56b88817 Mon Sep 17 00:00:00 2001 From: origami-john Date: Thu, 29 Oct 2020 16:18:24 +0800 Subject: [PATCH 1/6] add channel update API & channel acl udpate API --- README.md | 2 ++ app/api.py | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/README.md b/README.md index b6e4268..3518f7a 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,10 @@ If you find any issues or would like to help contribute to the project, please r | ---- | --------------- | | GET /servers/:serverid/channels | Get all channels in a server | | GET /servers/:serverid/channels/:channelid | Get a channel from a server by ID | +| PATCH /servers/:serverid/channels/:channelid | Update specific channel attributes by ID | | POST /servers/:serverid/channels | Create Channel, formdata: name&parent | | GET /servers/:serverid/channels/:channelid/acl | Get ACL list for channel ID | +| PATCH /servers/:serverid/channels/:channelid/acl | Update specific channel ACL by channel ID | | DELETE /servers/:serverid/channels/:channelid | Delete Channel | diff --git a/app/api.py b/app/api.py index 257f651..25d00de 100644 --- a/app/api.py +++ b/app/api.py @@ -448,6 +448,51 @@ def channel_new_channel(self, id): return Response(json.dumps(data, sort_keys=True, indent=4), mimetype='application/json') + @route('/channels/', methods=['PATCH']) + def updates_channel(self, id, channel_id): + """ Update specific channel attributes + """ + + server = meta.getServer(id) + + # Return 404 if not found + if server is None: + return jsonify(message="Not Found"), 404 + + # Return 404 if not found + channel = server.getChannelState(channel_id) + if channel is None: + return jsonify(message="Not Found"), 404 + + name = request.form.get('name') + parent = request.form.get('parent') + description = request.form.get('description') + links = request.form.getlist('links[]') + description = request.form.get('description') + position = request.form.get('position') + temporary = request.form.get('temporary') + + if name is not None: + channel.name = name.encode('utf-8') + if parent is not None: + channel.parent = int(parent) + if description is not None: + channel.description = description.encode('utf-8') + if links is not None: + for idx in range(len(links)): + links[idx] = int(links[idx]) + channel.links = links + if position is not None: + channel.position = int(position) + if temporary is not None: + channel.temporary = bool(temporary) + + server.setChannelState(channel) + + data = obj_to_dict(channel) + + return Response(json.dumps(data, sort_keys=True, indent=4), mimetype='application/json') + @conditional(auth.login_required, auth_enabled) @route('/channels/', methods=['DELETE']) def channel_del_channel(self, id, channel): @@ -580,6 +625,62 @@ def channel_acl(self, id, channel_id): data = obj_to_dict(server.getACL(channel_id)) return Response(json.dumps(data, sort_keys=True, indent=4), mimetype='application/json') + @conditional(auth.login_required, auth_enabled) + @route('/channels//acl', methods=['PATCH']) + def upadte_channel_acl(self, id, channel_id): + """ Update specific channel ACL + """ + + server = meta.getServer(id) + + # Return 404 if not found + if server is None: + return jsonify(message="Not Found"), 404 + + origin_acl = server.getACL(channel_id) + update_acls = origin_acl[0] + update_groups = origin_acl[1] + update_inherit = origin_acl[2] + + params = request.get_json() + if params['acls'] is not None: + new_acls = [] + for props in params['acls']: + new_acls.append(Murmur.ACL( + props['applyHere'], + props['applySubs'], + props['inherited'], + props['userid'], + props['group'], + props['allow'], + props['deny'] + )) + + update_acls = new_acls + + if params['groups'] is not None: + new_groups = [] + for props in params['groups']: + new_groups.append(Murmur.Group( + props['name'], + props['inherited'], + props['inherit'], + props['inheritable'], + props['add'], + props['remove'], + props['members'] + )) + + update_groups = new_groups + + if params['inherit'] is not None: + update_inherit = bool(params['inherit']) + + server.setACL(channel_id, update_acls, update_groups, update_inherit) + + data = obj_to_dict(server.getACL(channel_id)) + return Response(json.dumps(data, sort_keys=True, indent=4), mimetype='application/json') + @conditional(auth.login_required, auth_enabled) @route('/sendmessage', methods=['POST']) def send_message(self, id): From 1f2c7e3364fa30ece8467aad1c0e9ac150e17a66 Mon Sep 17 00:00:00 2001 From: origami-john Date: Fri, 30 Oct 2020 17:14:30 +0800 Subject: [PATCH 2/6] john update about indentation error & string utf-8 --- app/api.py | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/app/api.py b/app/api.py index 25d00de..30a183a 100644 --- a/app/api.py +++ b/app/api.py @@ -473,11 +473,11 @@ def updates_channel(self, id, channel_id): temporary = request.form.get('temporary') if name is not None: - channel.name = name.encode('utf-8') + channel.name = str(name) if parent is not None: channel.parent = int(parent) if description is not None: - channel.description = description.encode('utf-8') + channel.description = str(description) if links is not None: for idx in range(len(links)): links[idx] = int(links[idx]) @@ -489,7 +489,7 @@ def updates_channel(self, id, channel_id): server.setChannelState(channel) - data = obj_to_dict(channel) + data = obj_to_dict(server.getChannelState(channel_id)) return Response(json.dumps(data, sort_keys=True, indent=4), mimetype='application/json') @@ -646,30 +646,14 @@ def upadte_channel_acl(self, id, channel_id): if params['acls'] is not None: new_acls = [] for props in params['acls']: - new_acls.append(Murmur.ACL( - props['applyHere'], - props['applySubs'], - props['inherited'], - props['userid'], - props['group'], - props['allow'], - props['deny'] - )) + new_acls.append(Murmur.ACL(props['applyHere'], props['applySubs'], props['inherited'], props['userid'], props['group'], props['allow'], props['deny'])) update_acls = new_acls if params['groups'] is not None: new_groups = [] for props in params['groups']: - new_groups.append(Murmur.Group( - props['name'], - props['inherited'], - props['inherit'], - props['inheritable'], - props['add'], - props['remove'], - props['members'] - )) + new_groups.append(Murmur.Group(props['name'], props['inherited'], props['inherit'], props['inheritable'], props['add'], props['remove'], props['members'])) update_groups = new_groups From 44943f9a25e4c114dcddc32f322f04c38af967da Mon Sep 17 00:00:00 2001 From: Alf Date: Sun, 1 Nov 2020 21:17:22 -0800 Subject: [PATCH 3/6] Fix syntax issue to get running again and renamed function names. --- app/api.py | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/app/api.py b/app/api.py index 30a183a..f4c2857 100644 --- a/app/api.py +++ b/app/api.py @@ -449,7 +449,7 @@ def channel_new_channel(self, id): return Response(json.dumps(data, sort_keys=True, indent=4), mimetype='application/json') @route('/channels/', methods=['PATCH']) - def updates_channel(self, id, channel_id): + def update_channel(self, id, channel_id): """ Update specific channel attributes """ @@ -627,7 +627,7 @@ def channel_acl(self, id, channel_id): @conditional(auth.login_required, auth_enabled) @route('/channels//acl', methods=['PATCH']) - def upadte_channel_acl(self, id, channel_id): + def update_channel_acl(self, id, channel_id): """ Update specific channel ACL """ @@ -645,17 +645,31 @@ def upadte_channel_acl(self, id, channel_id): params = request.get_json() if params['acls'] is not None: new_acls = [] - for props in params['acls']: - new_acls.append(Murmur.ACL(props['applyHere'], props['applySubs'], props['inherited'], props['userid'], props['group'], props['allow'], props['deny'])) - - update_acls = new_acls + for props in params['acls']: + new_acls.append(Murmur.ACL( + props['applyHere'], + props['applySubs'], + props['inherited'], + props['userid'], + props['group'], + props['allow'], + props['deny']) + ) + update_acls = new_acls if params['groups'] is not None: new_groups = [] - for props in params['groups']: - new_groups.append(Murmur.Group(props['name'], props['inherited'], props['inherit'], props['inheritable'], props['add'], props['remove'], props['members'])) - - update_groups = new_groups + for props in params['groups']: + new_groups.append(Murmur.Group( + props['name'], + props['inherited'], + props['inherit'], + props['inheritable'], + props['add'], + props['remove'], + props['members']) + ) + update_groups = new_groups if params['inherit'] is not None: update_inherit = bool(params['inherit']) From 65f62c1c8fb0780e1b735dc867751abe06bf3160 Mon Sep 17 00:00:00 2001 From: origami-john Date: Wed, 4 Nov 2020 12:02:16 +0800 Subject: [PATCH 4/6] remove channel acl update API params --- app/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/api.py b/app/api.py index 30a183a..b0a0af6 100644 --- a/app/api.py +++ b/app/api.py @@ -653,7 +653,7 @@ def upadte_channel_acl(self, id, channel_id): if params['groups'] is not None: new_groups = [] for props in params['groups']: - new_groups.append(Murmur.Group(props['name'], props['inherited'], props['inherit'], props['inheritable'], props['add'], props['remove'], props['members'])) + new_groups.append(Murmur.Group(props['name'], props['inherited'], props['inherit'], props['inheritable'], props['add'], props['remove']) update_groups = new_groups From 103757bfdc1e4db2fd7c2ce957648d6af726ea47 Mon Sep 17 00:00:00 2001 From: origami-john Date: Wed, 4 Nov 2020 12:10:07 +0800 Subject: [PATCH 5/6] fix bug on channel acl upadte API --- app/api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/api.py b/app/api.py index c3c543f..5bda340 100644 --- a/app/api.py +++ b/app/api.py @@ -655,7 +655,7 @@ def update_channel_acl(self, id, channel_id): props['allow'], props['deny']) ) - update_acls = new_acls + update_acls = new_acls if params['groups'] is not None: new_groups = [] @@ -668,7 +668,7 @@ def update_channel_acl(self, id, channel_id): props['add'], props['remove']) ) - update_groups = new_groups + update_groups = new_groups if params['inherit'] is not None: update_inherit = bool(params['inherit']) From c1a4ea955fed27c5b9587e0115965607fc7a79d9 Mon Sep 17 00:00:00 2001 From: origami-john Date: Wed, 4 Nov 2020 17:23:52 +0800 Subject: [PATCH 6/6] udpate request params checking --- app/api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/api.py b/app/api.py index 5bda340..93dd73b 100644 --- a/app/api.py +++ b/app/api.py @@ -643,7 +643,7 @@ def update_channel_acl(self, id, channel_id): update_inherit = origin_acl[2] params = request.get_json() - if params['acls'] is not None: + if "acls" in params and params['acls'] is not None: new_acls = [] for props in params['acls']: new_acls.append(Murmur.ACL( @@ -657,7 +657,7 @@ def update_channel_acl(self, id, channel_id): ) update_acls = new_acls - if params['groups'] is not None: + if "groups" in params and params['groups'] is not None: new_groups = [] for props in params['groups']: new_groups.append(Murmur.Group( @@ -670,7 +670,7 @@ def update_channel_acl(self, id, channel_id): ) update_groups = new_groups - if params['inherit'] is not None: + if "inherit" in params and params['inherit'] is not None: update_inherit = bool(params['inherit']) server.setACL(channel_id, update_acls, update_groups, update_inherit)