2018-06-25 14:42:55 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2018 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2019-07-23 15:00:55 +02:00
|
|
|
import operator
|
2018-06-25 14:42:55 +02:00
|
|
|
|
|
|
|
from synapse.storage._base import SQLBaseStore
|
2018-07-09 08:09:20 +02:00
|
|
|
from synapse.util.caches.descriptors import cached, cachedList
|
2018-06-25 14:42:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
class UserErasureWorkerStore(SQLBaseStore):
|
|
|
|
@cached()
|
|
|
|
def is_user_erased(self, user_id):
|
|
|
|
"""
|
|
|
|
Check if the given user id has requested erasure
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str): full user id to check
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Deferred[bool]: True if the user has requested erasure
|
|
|
|
"""
|
|
|
|
return self._simple_select_onecol(
|
|
|
|
table="erased_users",
|
|
|
|
keyvalues={"user_id": user_id},
|
|
|
|
retcol="1",
|
|
|
|
desc="is_user_erased",
|
|
|
|
).addCallback(operator.truth)
|
|
|
|
|
|
|
|
@cachedList(
|
2019-04-03 11:07:29 +02:00
|
|
|
cached_method_name="is_user_erased", list_name="user_ids", inlineCallbacks=True
|
2018-06-25 14:42:55 +02:00
|
|
|
)
|
|
|
|
def are_users_erased(self, user_ids):
|
|
|
|
"""
|
|
|
|
Checks which users in a list have requested erasure
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_ids (iterable[str]): full user id to check
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Deferred[dict[str, bool]]:
|
|
|
|
for each user, whether the user has requested erasure.
|
|
|
|
"""
|
|
|
|
# this serves the dual purpose of (a) making sure we can do len and
|
|
|
|
# iterate it multiple times, and (b) avoiding duplicates.
|
|
|
|
user_ids = tuple(set(user_ids))
|
|
|
|
|
2019-10-02 20:07:07 +02:00
|
|
|
rows = yield self._simple_select_many_batch(
|
|
|
|
table="erased_users",
|
|
|
|
column="user_id",
|
|
|
|
iterable=user_ids,
|
|
|
|
retcols=("user_id",),
|
|
|
|
desc="are_users_erased",
|
|
|
|
)
|
|
|
|
erased_users = set(row["user_id"] for row in rows)
|
|
|
|
|
2018-06-25 14:42:55 +02:00
|
|
|
res = dict((u, u in erased_users) for u in user_ids)
|
2019-07-23 15:00:55 +02:00
|
|
|
return res
|
2018-06-25 14:42:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
class UserErasureStore(UserErasureWorkerStore):
|
|
|
|
def mark_user_erased(self, user_id):
|
|
|
|
"""Indicate that user_id wishes their message history to be erased.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str): full user_id to be erased
|
|
|
|
"""
|
2019-04-03 11:07:29 +02:00
|
|
|
|
2018-06-25 14:42:55 +02:00
|
|
|
def f(txn):
|
|
|
|
# first check if they are already in the list
|
2019-04-03 11:07:29 +02:00
|
|
|
txn.execute("SELECT 1 FROM erased_users WHERE user_id = ?", (user_id,))
|
2018-06-25 14:42:55 +02:00
|
|
|
if txn.fetchone():
|
|
|
|
return
|
|
|
|
|
|
|
|
# they are not already there: do the insert.
|
2019-04-03 11:07:29 +02:00
|
|
|
txn.execute("INSERT INTO erased_users (user_id) VALUES (?)", (user_id,))
|
|
|
|
|
|
|
|
self._invalidate_cache_and_stream(txn, self.is_user_erased, (user_id,))
|
2018-06-25 14:42:55 +02:00
|
|
|
|
|
|
|
return self.runInteraction("mark_user_erased", f)
|