hypothesis / h

Annotate with anyone, anywhere.
https://hypothes.is/
BSD 2-Clause "Simplified" License
2.96k stars 427 forks source link

Update `UserDeleteService` to use roles not users #9049

Closed seanh closed 1 week ago

seanh commented 3 weeks ago

When deleting a user we also delete any groups that user created that don't contain any annotations by other users. Change this to ignore whether the user created a group and instead delete any groups that the user is the only owner of, again as long as the group doesn't contain any annotations by other users.

With the current version of the code the SQL query that's ultimately generated is:

SELECT "group".id 
FROM "group"
LEFT OUTER JOIN annotation ON annotation.groupid = "group".pubid AND annotation.deleted IS false 
WHERE "group".id IN (
  SELECT user_group.group_id 
  FROM user_group 
  WHERE user_group.group_id IN (
    SELECT user_group.group_id 
    FROM user_group 
    WHERE (user_group.roles @> '"owner"'::jsonb)
    GROUP BY user_group.group_id 
    HAVING count(user_group.group_id) = 1
  )
  AND (user_group.roles @> '"owner"'::jsonb)
  AND user_group.user_id = 22562
)
GROUP BY "group".id 
HAVING count(annotation.id) = 0;

It's not currently possible to test that query in production because the roles column hasn't been deployed to production yet.

marcospri commented 2 weeks ago

This now runs fine in metabase, I guess we'd get more relevant numbers once https://github.com/hypothesis/h/pull/9031 is merged