Skip to content

Commit e41bba0

Browse files
committed
Room info fixes
The actual data we were returning for rooms didn't quite match what is in api.yaml, and was missing the room defaults info. Changes: - Don't include `'moderator': True` unless we are a moderator - Don't include `'admin': True` unless we are an admin - Don't include room description when it is null - Add default_read/default_write/default_upload fields to room info if we have mod permissions so that a moderator client can see the current value. - Fix tests for the above.
1 parent 4891025 commit e41bba0

File tree

3 files changed

+61
-36
lines changed

3 files changed

+61
-36
lines changed

api.yaml

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ paths:
184184
$ref: "#/components/schemas/Room/properties/global_moderator"
185185
global_admin:
186186
$ref: "#/components/schemas/Room/properties/global_admin"
187+
default_read:
188+
$ref: "#/components/schemas/Room/properties/default_read"
189+
default_write:
190+
$ref: "#/components/schemas/Room/properties/default_write"
191+
default_upload:
192+
$ref: "#/components/schemas/Room/properties/default_upload"
187193
details:
188194
allOf:
189195
- $ref: "#/components/schemas/Room"
@@ -1526,6 +1532,26 @@ components:
15261532
Whether the requesting user has permissions to upload attachments to messages posted to
15271533
the room. (Note that changes to this property do not cause an `info_update` increment.)
15281534
example: true
1535+
default_read:
1536+
type: boolean
1537+
description: >
1538+
Whether new users have permission to read posts in this room by default. This property
1539+
is only returned if the calling user has moderator/admin permissions.
1540+
example: true
1541+
default_write:
1542+
type: boolean
1543+
description: >
1544+
Whether new users have permission to write posts in this room by default. This property
1545+
is only returned if the calling user has moderator/admin permissions.
1546+
example: true
1547+
default_upload:
1548+
type: boolean
1549+
description: >
1550+
Whether new users have permission to upload attachments to posts in this room by
1551+
default. This property is only returned if the calling user has moderator/admin
1552+
permissions.
1553+
example: true
1554+
15291555
Message:
15301556
title: The content of a posted message
15311557
type: object
@@ -1637,9 +1663,6 @@ components:
16371663
description: "Session ID of a user."
16381664
schema:
16391665
$ref: "#/components/schemas/SessionID"
1640-
1641-
1642-
16431666

16441667
securitySchemes:
16451668
pubkey:

sogs/routes/rooms.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ def get_one_room(room):
1818
rr = {
1919
'token': room.token,
2020
'name': room.name,
21-
'description': room.description,
2221
'info_updates': room.info_updates,
2322
'message_sequence': room.message_sequence,
2423
'created': room.created,
2524
'active_users': room.active_users(),
2625
'active_users_cutoff': int(config.ROOM_DEFAULT_ACTIVE_THRESHOLD * 86400),
2726
'moderators': mods,
2827
'admins': admins,
29-
'moderator': room.check_moderator(g.user),
30-
'admin': room.check_admin(g.user),
3128
'read': room.check_read(g.user),
3229
'write': room.check_write(g.user),
3330
'upload': room.check_upload(g.user),
3431
}
3532

33+
if room.description is not None:
34+
rr['description'] = room.description
35+
3636
if room.image_id is not None:
3737
rr['image_id'] = room.image_id
3838

@@ -45,6 +45,13 @@ def get_one_room(room):
4545
if h_admins:
4646
rr['hidden_admins'] = h_admins
4747

48+
if room.check_moderator(g.user):
49+
rr['moderator'] = True
50+
rr['default_read'] = room.default_read
51+
rr['default_write'] = room.default_write
52+
rr['default_upload'] = room.default_upload
53+
if room.check_admin(g.user):
54+
rr['admin'] = True
4855
if g.user:
4956
if g.user.global_moderator:
5057
rr['global_moderator'] = True
@@ -126,8 +133,6 @@ def poll_room_info(room, info_updated):
126133
result = {
127134
'token': room.token,
128135
'active_users': room.active_users(),
129-
'moderator': room.check_moderator(g.user),
130-
'admin': room.check_admin(g.user),
131136
'read': room.check_read(g.user),
132137
'write': room.check_write(g.user),
133138
'upload': room.check_upload(g.user),
@@ -136,6 +141,13 @@ def poll_room_info(room, info_updated):
136141
if room.info_updates != info_updated:
137142
result['details'] = get_one_room(room)
138143

144+
if room.check_moderator(g.user):
145+
result['moderator'] = True
146+
result['default_read'] = room.default_read
147+
result['default_write'] = room.default_write
148+
result['default_upload'] = room.default_upload
149+
if room.check_admin(g.user):
150+
result['admin'] = True
139151
if g.user:
140152
if g.user.global_moderator:
141153
result['global_moderator'] = True

tests/test_room_routes.py

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ def test_list(client, room, user, user2, admin, mod, global_mod, global_admin):
3131
"active_users_cutoff": int(86400 * sogs.config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
3232
"moderators": [],
3333
"admins": [],
34-
"moderator": False,
35-
"admin": False,
3634
"read": True,
3735
"write": False,
3836
"upload": False,
3937
}
38+
r2_exp_defs = {"default_read": True, "default_write": False, "default_upload": False}
4039
r_expected = {
4140
"token": "test-room",
4241
"name": "Test room",
@@ -48,12 +47,11 @@ def test_list(client, room, user, user2, admin, mod, global_mod, global_admin):
4847
"active_users_cutoff": int(86400 * sogs.config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
4948
"moderators": [mod.session_id],
5049
"admins": [admin.session_id],
51-
"moderator": False,
52-
"admin": False,
5350
"read": True,
5451
"write": True,
5552
"upload": True,
5653
}
54+
r_exp_defs = {"default_read": True, "default_write": True, "default_upload": True}
5755
r3_expected = {
5856
"token": "room3",
5957
"name": "Room 3",
@@ -65,12 +63,11 @@ def test_list(client, room, user, user2, admin, mod, global_mod, global_admin):
6563
"active_users_cutoff": int(86400 * sogs.config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
6664
"moderators": [],
6765
"admins": [],
68-
"moderator": False,
69-
"admin": False,
7066
"read": True,
7167
"write": False,
7268
"upload": False,
7369
}
70+
r3_exp_defs = {"default_read": False, "default_write": False, "default_upload": False}
7471

7572
exp_mod = {
7673
**{p: True for p in ("moderator", "read", "write", "upload")},
@@ -99,26 +96,26 @@ def test_list(client, room, user, user2, admin, mod, global_mod, global_admin):
9996

10097
r = sogs_get(client, "/rooms", mod)
10198
assert r.status_code == 200
102-
assert r.json == [r2_expected, {**r_expected, **exp_mod}]
99+
assert r.json == [r2_expected, {**r_expected, **r_exp_defs, **exp_mod}]
103100

104101
r = sogs_get(client, "/rooms", admin)
105102
assert r.status_code == 200
106-
assert r.json == [r2_expected, {**r_expected, **exp_admin}]
103+
assert r.json == [r2_expected, {**r_expected, **r_exp_defs, **exp_admin}]
107104

108105
r = sogs_get(client, "/rooms", global_mod)
109106
assert r.status_code == 200
110107
assert r.json == [
111-
{**r2_expected, **exp_gmod},
112-
{**r3_expected, **exp_gmod},
113-
{**r_expected, **exp_gmod},
108+
{**r2_expected, **r2_exp_defs, **exp_gmod},
109+
{**r3_expected, **r3_exp_defs, **exp_gmod},
110+
{**r_expected, **r_exp_defs, **exp_gmod},
114111
]
115112

116113
r = sogs_get(client, "/rooms", global_admin)
117114
assert r.status_code == 200
118115
assert r.json == [
119-
{**r2_expected, **exp_gadmin},
120-
{**r3_expected, **exp_gadmin},
121-
{**r_expected, **exp_gadmin},
116+
{**r2_expected, **r2_exp_defs, **exp_gadmin},
117+
{**r3_expected, **r3_exp_defs, **exp_gadmin},
118+
{**r_expected, **r_exp_defs, **exp_gadmin},
122119
]
123120

124121
r = sogs_get(client, "/room/room3", user)
@@ -131,7 +128,7 @@ def test_list(client, room, user, user2, admin, mod, global_mod, global_admin):
131128

132129
r = sogs_get(client, "/room/room3", global_admin)
133130
assert r.status_code == 200
134-
assert r.json == {**r3_expected, **exp_gadmin}
131+
assert r.json == {**r3_expected, **r3_exp_defs, **exp_gadmin}
135132

136133

137134
def test_updates(client, room, user, user2, mod, admin, global_mod, global_admin):
@@ -322,15 +319,7 @@ def test_polling(client, room, user, user2, mod, admin, global_mod, global_admin
322319
info_up = r.json['info_updates']
323320
assert info_up == 2
324321

325-
basic = {
326-
'token': 'test-room',
327-
'active_users': 1,
328-
'moderator': False,
329-
'admin': False,
330-
'read': True,
331-
'write': True,
332-
'upload': True,
333-
}
322+
basic = {'token': 'test-room', 'active_users': 1, 'read': True, 'write': True, 'upload': True}
334323
details = {
335324
"token": "test-room",
336325
"name": "Test room",
@@ -342,12 +331,11 @@ def test_polling(client, room, user, user2, mod, admin, global_mod, global_admin
342331
"active_users_cutoff": int(86400 * sogs.config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
343332
"moderators": [mod.session_id],
344333
"admins": [admin.session_id],
345-
"moderator": False,
346-
"admin": False,
347334
"read": True,
348335
"write": True,
349336
"upload": True,
350337
}
338+
defs = {'default_' + x: True for x in ('read', 'write', 'upload')}
351339

352340
r = sogs_get(client, f"/room/test-room/pollInfo/{info_up}", user)
353341
assert r.status_code == 200
@@ -368,13 +356,13 @@ def test_polling(client, room, user, user2, mod, admin, global_mod, global_admin
368356
basic['active_users'] += 1
369357
details['active_users'] += 1
370358
assert r.status_code == 200
371-
assert r.json == {**basic, 'moderator': True}
359+
assert r.json == {**basic, 'moderator': True, **defs}
372360

373361
r = sogs_get(client, f"/room/test-room/pollInfo/{info_up}", admin)
374362
assert r.status_code == 200
375363
basic['active_users'] += 1
376364
details['active_users'] += 1
377-
assert r.json == {**basic, 'moderator': True, 'admin': True}
365+
assert r.json == {**basic, 'moderator': True, 'admin': True, **defs}
378366

379367
# Changing description
380368
room.description = 'Test suite testing room new desc'
@@ -418,9 +406,11 @@ def test_polling(client, room, user, user2, mod, admin, global_mod, global_admin
418406
details['moderators'] = sorted(details['moderators'] + [user.session_id])
419407
assert r.json == {
420408
**basic,
409+
**defs,
421410
'moderator': True,
422411
'details': {
423412
**details,
413+
**defs,
424414
'moderator': True,
425415
'hidden_admins': [global_admin.session_id],
426416
'hidden_moderators': [global_mod.session_id],

0 commit comments

Comments
 (0)