Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an Admin API endpoint to redact all a user's events #17506

Merged
merged 15 commits into from
Sep 18, 2024
2 changes: 1 addition & 1 deletion docs/admin_api/user_admin_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1440,4 +1440,4 @@ The following fields are returned in the JSON response body:
- failed_redactions: a dict where the keys are event ids the process was unable to redact, if any, and the values are
the corresponding error that caused the redaction to fail

_Added in Synapse 1.114.0._
_Added in Synapse 1.115.0._
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 4 additions & 13 deletions synapse/handlers/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,28 +431,19 @@ async def _redact_all_events(
user_id,
room,
limit,
[
"m.room.member",
"m.text",
"m.emote",
"m.image",
"m.file",
"m.audio",
"m.video",
],
["m.room.member", "m.room.message"],
)
if not event_ids:
# there's nothing to redact
return TaskStatus.COMPLETE, result, None

events = await self._store.get_events_as_list(set(event_ids))
events = await self._store.get_events_as_list(event_ids)
for event in events:
# we care about join events but not other membership events
if event.type == "m.room.member":
dict = event.get_dict()
content = dict.get("content")
content = event.content
if content:
if content.get("membership") == "join":
if content.get("membership") == "Membership.JOIN":
Copy link
Member

Choose a reason for hiding this comment

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

Oh err, Membership.JOIN is a type. Looks like we may be missing a test for this codepath? 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right I have switched it back and ensured that the tests are verifying that we are redacting the join event.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for updating the tests! Using Membership.JOIN (without quotes) should work though? It's equivalent to "join", but uses a Final instead of a loose string.

pass
else:
continue
Expand Down
4 changes: 3 additions & 1 deletion synapse/rest/admin/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -1435,7 +1435,9 @@ async def on_POST(
body = parse_json_object_from_request(request, allow_empty_body=True)
rooms = body.get("rooms")
if rooms is None:
raise SynapseError(400, "Must provide a value for rooms.")
raise SynapseError(
HTTPStatus.BAD_REQUEST, "Must provide a value for rooms."
)

reason = body.get("reason")
limit = body.get("limit")
Expand Down
41 changes: 17 additions & 24 deletions synapse/storage/databases/main/events_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2462,31 +2462,24 @@ def _get_events_by_user_in_room_txn(
offset: int,
) -> Tuple[Optional[List[str]], int]:
if filter:
filter_sql = " AND type in ("
for i, _ in enumerate(filter):
if i < len(filter) - 1:
filter_sql += "?, "
else:
filter_sql += "?)"

sql = f"""
SELECT event_id FROM events
WHERE sender = ? AND room_id = ?
{filter_sql}
ORDER BY received_ts DESC
LIMIT ?
OFFSET ?
"""
txn.execute(sql, (user_id, room_id, *filter, batch_size, offset))
base_clause, args = make_in_list_sql_clause(
txn.database_engine, "type", filter
)
clause = f"AND {base_clause}"
parameters = (user_id, room_id, *args, batch_size, offset)
else:
sql = """
SELECT event_id FROM events
WHERE sender = ? AND room_id = ?
ORDER BY received_ts DESC
LIMIT ?
OFFSET ?
"""
txn.execute(sql, (user_id, room_id, batch_size, offset))
clause = ""
parameters = (user_id, room_id, batch_size, offset)

sql = f"""
SELECT event_id FROM events
WHERE sender = ? AND room_id = ?
{clause}
ORDER BY received_ts DESC
LIMIT ?
OFFSET ?
"""
txn.execute(sql, parameters)
res = txn.fetchall()
if res:
events = [row[0] for row in res]
Expand Down
31 changes: 16 additions & 15 deletions tests/rest/admin/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5129,12 +5129,11 @@ def test_redact_messages_all_rooms(self) -> None:
# join rooms, send some messages
originals = []
for rm in [rm1, rm2, rm3]:
join = self.helper.join(rm, self.bad_user, tok=self.bad_user_tok)
originals.append(join["event_id"])
self.helper.join(rm, self.bad_user, tok=self.bad_user_tok)
for i in range(15):
event = {"body": f"hello{i}", "msgtype": "m.text"}
res = self.helper.send_event(
rm, "m.text", event, tok=self.bad_user_tok, expect_code=200
rm, "m.room.message", event, tok=self.bad_user_tok, expect_code=200
)
originals.append(res["event_id"])

Expand Down Expand Up @@ -5180,11 +5179,12 @@ def test_redact_messages_specific_rooms(self) -> None:

originals = []
for rm in [rm1, rm2, rm3]:
join = self.helper.join(rm, self.bad_user, tok=self.bad_user_tok)
originals.append(join["event_id"])
self.helper.join(rm, self.bad_user, tok=self.bad_user_tok)
for i in range(15):
event = {"body": f"hello{i}", "msgtype": "m.text"}
res = self.helper.send_event(rm, "m.text", event, tok=self.bad_user_tok)
res = self.helper.send_event(
rm, "m.room.message", event, tok=self.bad_user_tok
)
originals.append(res["event_id"])

# redact messages in rooms 1 and 3
Expand Down Expand Up @@ -5214,8 +5214,8 @@ def test_redact_messages_specific_rooms(self) -> None:
and event["redacts"] == event_id
):
matches.append((event_id, event))
# we redacted 16 messages
self.assertEqual(len(matches), 16)
# we redacted 15 messages
self.assertEqual(len(matches), 15)

channel = self.make_request(
"GET", f"rooms/{rm2}/messages?limit=50", access_token=self.admin_tok
Expand All @@ -5238,12 +5238,12 @@ def test_redact_status(self) -> None:

originals = []
for rm in [rm1, rm2, rm3]:
join = self.helper.join(rm, self.bad_user, tok=self.bad_user_tok)
if rm == rm2:
originals.append(join["event_id"])
self.helper.join(rm, self.bad_user, tok=self.bad_user_tok)
for i in range(5):
event = {"body": f"hello{i}", "msgtype": "m.text"}
res = self.helper.send_event(rm, "m.text", event, tok=self.bad_user_tok)
res = self.helper.send_event(
rm, "m.room.message", event, tok=self.bad_user_tok
)
if rm == rm2:
originals.append(res["event_id"])

Expand Down Expand Up @@ -5307,11 +5307,12 @@ def test_admin_redact_works_if_user_kicked_or_banned(self) -> None:

originals = []
for rm in [rm1, rm2, rm3]:
join = self.helper.join(rm, self.bad_user, tok=self.bad_user_tok)
originals.append(join["event_id"])
self.helper.join(rm, self.bad_user, tok=self.bad_user_tok)
for i in range(5):
event = {"body": f"hello{i}", "msgtype": "m.text"}
res = self.helper.send_event(rm, "m.text", event, tok=self.bad_user_tok)
res = self.helper.send_event(
rm, "m.room.message", event, tok=self.bad_user_tok
)
originals.append(res["event_id"])

# kick user from rooms 1 and 3
Expand Down