Skip to content

Commit 98ed125

Browse files
committed
wire up bans to bot api
* move ban logic to model.py * implement sogs.ban endpoint
1 parent 5c9b8b0 commit 98ed125

File tree

3 files changed

+58
-37
lines changed

3 files changed

+58
-37
lines changed

sogs/events.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,20 @@ def _handle_find_user(metric, query, encode=json.dumps):
7171
return encode(reply)
7272

7373

74+
def _decode_str(data):
75+
""" decode bytes to lowercase string """
76+
return data.decode('utf-8').lower()
77+
78+
7479
def _handle_find_request(msg):
7580
""" finds a user / room by id / token / pubkey """
7681
parts = msg.dataviews()
7782
if len(parts) != 3:
7883
raise Exception('3 arguments required: entity-kind, query-metric, query-value')
7984

80-
decode = lambda x: x.decode('utf-8').lower()
81-
82-
kind = decode(parts[0])
83-
metric = decode(parts[1])
84-
query = decode(parts[2])
85+
kind = _decode_str(parts[0])
86+
metric = _decode_str(parts[1])
87+
query = _decode_str(parts[2])
8588

8689
_kinds = {'room': _handle_find_room, 'user': _handle_find_user}
8790

@@ -90,6 +93,19 @@ def _handle_find_request(msg):
9093
raise Exception("cannot find a '{}' we dont have those".format(kind))
9194

9295

96+
def _handle_mod_ban(msg):
97+
""" handle a ban user from room rqeuest """
98+
parts = msg.dataviews()
99+
if len(parts) < 2:
100+
raise Exception("Not enough arguments, need 2 arguments: user_id, room_id")
101+
room_id = int(_decode_str(parts[1]))
102+
user_id = int(_decode_str(parts[0]))
103+
room = model.Room(id=room_id)
104+
user = model.User(id=user_id)
105+
if not model.ban_user(None, room, user):
106+
raise Exception("user not banned")
107+
108+
93109
def _propagate_event(eventname, *args):
94110
""" propagate an event to everyone who cares about it """
95111
assert event_name_valid(eventname)
@@ -122,6 +138,7 @@ def start():
122138
_bot_category.add_request_handler(
123139
'find', lambda msg: _handle_request(_handle_find_request, msg)
124140
)
141+
_bot_category.add_request_handler('ban', lambda msg: _handle_request(_handle_mod_ban, msg))
125142

126143
for addr in config.API_ADDRS:
127144
# TODO: implement curve?

sogs/legacy_routes.py

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -427,50 +427,23 @@ def ban_checks():
427427
return user, room, to_ban
428428

429429

430-
def apply_ban(conn, user, room, to_ban):
431-
is_mod = bool(
432-
conn.execute(
433-
"SELECT moderator FROM user_permissions WHERE room = ? AND user = ?",
434-
(room.id, to_ban.id),
435-
).fetchone()[0]
436-
)
437-
438-
if is_mod and not model.check_permission(user, room, admin=True):
439-
app.logger.warn(
440-
"Cannot ban {} from {}: the ban target is a room moderator, "
441-
"but the ban initiator ({}) is not an admin".format(
442-
to_ban.session_id, room.token, user.session_id
443-
)
444-
)
445-
abort(http.FORBIDDEN)
446-
447-
conn.execute(
448-
"""
449-
INSERT INTO user_permission_overrides (room, user, banned, moderator, admin)
450-
VALUES (?, ?, TRUE, FALSE, FALSE)
451-
ON CONFLICT (room, user) DO UPDATE SET banned = TRUE, moderator = FALSE, admin = FALSE
452-
""",
453-
(room.id, to_ban.id),
454-
)
455-
456-
457430
@app.post("/legacy/block_list")
458431
def handle_legacy_ban():
459432
user, room, to_ban = ban_checks()
460433

461-
with db.conn as conn:
462-
apply_ban(conn, user, room, to_ban)
434+
if not model.ban_user(user, room, to_ban):
435+
abort(http.FORBIDDEN)
463436

464437
return jsonify({"status_code": 200})
465438

466439

467440
@app.post("/legacy/ban_and_delete_all")
468441
def handle_legacy_banhammer():
469442
user, room, to_ban = ban_checks()
443+
if not model.ban_user(user, room, to_ban):
444+
abort(http.FORBIDDEN)
470445

471446
with db.conn as conn:
472-
apply_ban(conn, user, room, to_ban)
473-
474447
cur = conn.cursor()
475448
cur.execute(
476449
"""

sogs/model.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ def get_mods(self, user=None):
184184
if session_id is not None and session_id == curr_session_id:
185185
we_are_hidden = not visible
186186
we_are_admin = admin
187-
188187
(mods if visible else hidden_mods).append(session_id)
189188

190189
if we_are_admin:
@@ -635,3 +634,35 @@ def get_message_deprecated(room_id, since, limit=256):
635634
}
636635
)
637636
return msgs
637+
638+
639+
def ban_user(mod_user, room, ban_user):
640+
"""as a moderator mod_user ban a user from a room
641+
return True if the user was banned, otherwise return False
642+
"""
643+
with db.conn as conn:
644+
is_mod = bool(
645+
conn.execute(
646+
"SELECT moderator FROM user_permissions WHERE room = ? AND user = ?",
647+
(room.id, ban_user.id),
648+
).fetchone()[0]
649+
)
650+
651+
if mod_user and is_mod and not check_permission(mod_user, room, admin=True):
652+
app.logger.warn(
653+
"Cannot ban {} from {}: the ban target is a room moderator, "
654+
"but the ban initiator ({}) is not an admin".format(
655+
ban_user.session_id, room.token, mod_user.session_id
656+
)
657+
)
658+
return False
659+
660+
conn.execute(
661+
"""
662+
INSERT INTO user_permission_overrides (room, user, banned, moderator, admin)
663+
VALUES (?, ?, TRUE, FALSE, FALSE)
664+
ON CONFLICT (room, user) DO UPDATE SET banned = TRUE, moderator = FALSE, admin = FALSE
665+
""",
666+
(room.id, ban_user.id),
667+
)
668+
return True

0 commit comments

Comments
 (0)