Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |


Expand Down
98 changes: 98 additions & 0 deletions app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,51 @@ def channel_new_channel(self, id):

return Response(json.dumps(data, sort_keys=True, indent=4), mimetype='application/json')

@route('<int:id>/channels/<int:channel_id>', methods=['PATCH'])
def update_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 = str(name)
if parent is not None:
channel.parent = int(parent)
if description is not None:
channel.description = str(description)
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(server.getChannelState(channel_id))

return Response(json.dumps(data, sort_keys=True, indent=4), mimetype='application/json')

@conditional(auth.login_required, auth_enabled)
@route('<int:id>/channels/<channel>', methods=['DELETE'])
def channel_del_channel(self, id, channel):
Expand Down Expand Up @@ -580,6 +625,59 @@ 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('<int:id>/channels/<int:channel_id>/acl', methods=['PATCH'])
def update_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]
Comment on lines +641 to +643

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what's going on here with the ACL settings. It's been a while since I've worked on Murmur Slice API, so I'm a bit rusty. :)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh this part, I want to follow RESTful PATCH.
Example :
If this request only pass acls, not pass groups and inherit, will only update acls.
update_groups will be groups of origin_acl, update_inherit will be inherit of origin_acl.
And update_acls will be new_acls

on server.setACL(channel_id, update_acls, update_groups, update_inherit)


params = request.get_json()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you're reading the request JSON body here. However, all of the methods read via multipart form data, so I would rather not change it if possible.

Looking back at the project, I do wish I went with reading the JSON body instead though. 🙂

@minghan9456 minghan9456 Nov 2, 2020

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is ok.
I just think json is easy to pass array data like acls or groups.
Because if want to get acls array I need to via request.form.get(acls[][applyHere]) & request.form.get(acls[][applySubs]) and .......
Or have other way can easy to get params.

Can back to use form data.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here form data with array, you have any idea?

if "acls" in params and 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 "groups" in params and 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'])
)
update_groups = new_groups

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)

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('<int:id>/sendmessage', methods=['POST'])
def send_message(self, id):
Expand Down