user_erasure_store.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import operator
  16. from synapse.storage._base import SQLBaseStore
  17. from synapse.util.caches.descriptors import cached, cachedList
  18. class UserErasureWorkerStore(SQLBaseStore):
  19. @cached()
  20. def is_user_erased(self, user_id):
  21. """
  22. Check if the given user id has requested erasure
  23. Args:
  24. user_id (str): full user id to check
  25. Returns:
  26. Deferred[bool]: True if the user has requested erasure
  27. """
  28. return self.db_pool.simple_select_onecol(
  29. table="erased_users",
  30. keyvalues={"user_id": user_id},
  31. retcol="1",
  32. desc="is_user_erased",
  33. ).addCallback(operator.truth)
  34. @cachedList(
  35. cached_method_name="is_user_erased", list_name="user_ids", inlineCallbacks=True
  36. )
  37. def are_users_erased(self, user_ids):
  38. """
  39. Checks which users in a list have requested erasure
  40. Args:
  41. user_ids (iterable[str]): full user id to check
  42. Returns:
  43. Deferred[dict[str, bool]]:
  44. for each user, whether the user has requested erasure.
  45. """
  46. # this serves the dual purpose of (a) making sure we can do len and
  47. # iterate it multiple times, and (b) avoiding duplicates.
  48. user_ids = tuple(set(user_ids))
  49. rows = yield self.db_pool.simple_select_many_batch(
  50. table="erased_users",
  51. column="user_id",
  52. iterable=user_ids,
  53. retcols=("user_id",),
  54. desc="are_users_erased",
  55. )
  56. erased_users = {row["user_id"] for row in rows}
  57. res = {u: u in erased_users for u in user_ids}
  58. return res
  59. class UserErasureStore(UserErasureWorkerStore):
  60. def mark_user_erased(self, user_id: str) -> None:
  61. """Indicate that user_id wishes their message history to be erased.
  62. Args:
  63. user_id: full user_id to be erased
  64. """
  65. def f(txn):
  66. # first check if they are already in the list
  67. txn.execute("SELECT 1 FROM erased_users WHERE user_id = ?", (user_id,))
  68. if txn.fetchone():
  69. return
  70. # they are not already there: do the insert.
  71. txn.execute("INSERT INTO erased_users (user_id) VALUES (?)", (user_id,))
  72. self._invalidate_cache_and_stream(txn, self.is_user_erased, (user_id,))
  73. return self.db_pool.runInteraction("mark_user_erased", f)
  74. def mark_user_not_erased(self, user_id: str) -> None:
  75. """Indicate that user_id is no longer erased.
  76. Args:
  77. user_id: full user_id to be un-erased
  78. """
  79. def f(txn):
  80. # first check if they are already in the list
  81. txn.execute("SELECT 1 FROM erased_users WHERE user_id = ?", (user_id,))
  82. if not txn.fetchone():
  83. return
  84. # They are there, delete them.
  85. self.simple_delete_one_txn(
  86. txn, "erased_users", keyvalues={"user_id": user_id}
  87. )
  88. self._invalidate_cache_and_stream(txn, self.is_user_erased, (user_id,))
  89. return self.db_pool.runInteraction("mark_user_not_erased", f)