Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add functionality to remove deactivated users from the monthly_active_users table #10947

Merged
merged 9 commits into from
Oct 4, 2021
6 changes: 2 additions & 4 deletions synapse/handlers/deactivate_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,9 @@ async def deactivate_account(
# delete from user directory
await self.user_directory_handler.handle_local_user_deactivated(user_id)

# If the user is present in the monthly active users table,
# If the user is present in the monthly active users table
# remove them
monthly_user = await self.store.user_last_seen_monthly_active(user_id)
if monthly_user:
await self.store.remove_deactivated_user(user_id)
await self.store.remove_deactivated_user_from_mau_table(user_id)

# Mark the user as erased, if they asked for that
if erase_data:
Expand Down
20 changes: 15 additions & 5 deletions synapse/storage/databases/main/monthly_active_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,16 +355,26 @@ async def populate_monthly_active_users(self, user_id):
elif now - last_seen_timestamp > LAST_SEEN_GRANULARITY:
await self.upsert_monthly_active_user(user_id)

async def remove_deactivated_user(self, user_id: str) -> None:
async def remove_deactivated_user_from_mau_table(self, user_id: str) -> None:
"""
Removes a deactivated user from the monthly active user
table and resets the "get_monthly_active_count" cache.
table and resets affected caches.

Args:
user_id(str): the user_id to remove
"""

await self.db_pool.simple_delete_one(
table="monthly_active_users", keyvalues={"user_id": user_id}
rows_deleted = await self.db_pool.simple_delete(
table="monthly_active_users",
keyvalues={"user_id": user_id},
desc="simple_delete",
)
await self.invalidate_cache_and_stream("get_monthly_active_count", ())

if rows_deleted != 0:
await self.invalidate_cache_and_stream(
"user_last_seen_monthly_active", (user_id)
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
)
await self.invalidate_cache_and_stream("get_monthly_active_count", ())
await self.invalidate_cache_and_stream(
"get_monthly_active_count_by_service", ()
)
1 change: 0 additions & 1 deletion tests/test_mau.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ def test_deactivated_users_dont_count_towards_mau(self):
# Deactivate user1
url = "/_synapse/admin/v1/deactivate/%s" % user1
channel = self.make_request("POST", url, access_token=admin_token)
print(channel.json_body)
self.assertIn("success", channel.json_body["id_server_unbind_result"])

# Check that deactivated user is no longer counted
Expand Down