Skip to content

Commit 04fa434

Browse files
Jeffjagerman
authored andcommitted
Fetch room perminssions routes. Basic unit tests and basic functionality.
1 parent 8e582df commit 04fa434

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

sogs/model/room.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,8 +1498,34 @@ def unpin(self, msg_id: int, admin: User):
14981498

14991499
@property
15001500
def url(self):
1501+
"""
1502+
URL of the web based room viewer for this room
1503+
"""
15011504
return utils.server_url(self.token)
15021505

1506+
def export_permissions(self, mod):
1507+
"""
1508+
export room permissions in full,
1509+
returns a dict of session_id -> permissions dict (a dict of permission type to bool)
1510+
"""
1511+
if not self.check_moderator(mod):
1512+
app.logger.warning("unable to get room permissions for user")
1513+
raise BadPermission()
1514+
with db.transaction():
1515+
ret = dict()
1516+
for row in query(
1517+
"""SELECT session_id, upo.* FROM user_permission_overrides upo
1518+
JOIN users ON "user" = users.id WHERE room = :r""",
1519+
r=self.id,
1520+
):
1521+
data = dict()
1522+
for k in row.keys():
1523+
if k not in ('session_id', 'room', 'user'):
1524+
if row[k] is not None:
1525+
data[k] = bool(row[k])
1526+
ret[row['session_id']] = data
1527+
return ret
1528+
15031529

15041530
def get_rooms():
15051531
"""Get a list of all rooms; does not check permissions."""

sogs/routes/rooms.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,24 @@ def update_room(room):
278278
return jsonify({})
279279

280280

281+
@rooms.get("/room/<Room:room>/permInfo")
282+
@auth.mod_required
283+
def get_permission_info(room):
284+
"""
285+
Fetches permissions about the room, like ban info etc.
286+
287+
# Query Parameters
288+
289+
TODO: implement me
290+
291+
# Return Value
292+
293+
dict of session_id to current permission info
294+
295+
"""
296+
return jsonify(room.export_permissions(g.user))
297+
298+
281299
@rooms.get("/room/<Room:room>/pollInfo/<int:info_updated>")
282300
@auth.read_required
283301
def poll_room_info(room, info_updated):

tests/test_room_routes.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,3 +1139,24 @@ def test_remove_all_self_posts_from_room(client, room, mod, user, no_rate_limit)
11391139
assert r.status_code == 200
11401140
assert len(room.get_messages_for(u, recent=True)) == 0
11411141
assert room.check_unbanned(u)
1142+
1143+
1144+
def test_get_room_perms(client, room, user):
1145+
r = sogs_get(client, f'/room/{room.token}/permInfo', user)
1146+
assert r.status_code == 403
1147+
1148+
1149+
def test_get_room_perms_as_mod(client, room, mod):
1150+
r = sogs_get(client, f'/room/{room.token}/permInfo', mod)
1151+
assert r.status_code == 200
1152+
assert mod.session_id in r.json
1153+
perm_info = r.json[mod.session_id]
1154+
assert perm_info['visible_mod'] is True
1155+
1156+
1157+
def test_get_room_perms_as_admin(client, room, admin):
1158+
r = sogs_get(client, f'/room/{room.token}/permInfo', admin)
1159+
assert r.status_code == 200
1160+
assert admin.session_id in r.json
1161+
perm_info = r.json[admin.session_id]
1162+
assert perm_info['admin'] is True

0 commit comments

Comments
 (0)