Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

107 рядки
3.6 KiB

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