Skip to content

Commit 4a95ca9

Browse files
committed
wire up bans to bot api
* move ban logic to model.py * implement sogs.ban endpoint
1 parent 5425cba commit 4a95ca9

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
@@ -430,50 +430,23 @@ def ban_checks():
430430
return user, room, to_ban
431431

432432

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

464-
with db.conn as conn:
465-
apply_ban(conn, user, room, to_ban)
437+
if not model.ban_user(user, room, to_ban):
438+
abort(http.FORBIDDEN)
466439

467440
return jsonify({"status_code": 200})
468441

469442

470443
@app.post("/legacy/ban_and_delete_all")
471444
def handle_legacy_banhammer():
472445
user, room, to_ban = ban_checks()
446+
if not model.ban_user(user, room, to_ban):
447+
abort(http.FORBIDDEN)
473448

474449
with db.conn as conn:
475-
apply_ban(conn, user, room, to_ban)
476-
477450
cur = conn.cursor()
478451
cur.execute(
479452
"""

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

0 commit comments

Comments
 (0)