2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2018-02-23 03:33:55 -07:00
|
|
|
# Copyright 2018 New Vector Ltd
|
2015-12-01 11:41:32 -07:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2018-07-09 00:09:20 -06:00
|
|
|
import logging
|
2021-01-07 06:03:38 -07:00
|
|
|
from typing import Dict, List, Optional, Set, Tuple
|
2015-12-01 11:41:32 -07:00
|
|
|
|
2020-10-05 07:28:05 -06:00
|
|
|
from synapse.api.constants import AccountDataTypes
|
2021-01-18 08:47:59 -07:00
|
|
|
from synapse.replication.slave.storage._slaved_id_tracker import SlavedIdTracker
|
|
|
|
from synapse.replication.tcp.streams import AccountDataStream, TagAccountDataStream
|
2020-07-16 09:32:19 -06:00
|
|
|
from synapse.storage._base import SQLBaseStore, db_to_json
|
2020-08-05 14:38:57 -06:00
|
|
|
from synapse.storage.database import DatabasePool
|
2021-01-18 08:47:59 -07:00
|
|
|
from synapse.storage.engines import PostgresEngine
|
|
|
|
from synapse.storage.util.id_generators import MultiWriterIdGenerator, StreamIdGenerator
|
2020-08-12 07:29:06 -06:00
|
|
|
from synapse.types import JsonDict
|
2020-08-07 06:02:55 -06:00
|
|
|
from synapse.util import json_encoder
|
2021-01-07 06:03:38 -07:00
|
|
|
from synapse.util.caches.descriptors import cached
|
2018-07-09 00:09:20 -06:00
|
|
|
from synapse.util.caches.stream_change_cache import StreamChangeCache
|
2015-12-01 11:41:32 -07:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-01-18 08:47:59 -07:00
|
|
|
class AccountDataWorkerStore(SQLBaseStore):
|
2018-02-16 05:08:42 -07:00
|
|
|
"""This is an abstract base class where subclasses must implement
|
|
|
|
`get_max_account_data_stream_id` which can be called in the initializer.
|
|
|
|
"""
|
|
|
|
|
2020-08-05 14:38:57 -06:00
|
|
|
def __init__(self, database: DatabasePool, db_conn, hs):
|
2021-01-18 08:47:59 -07:00
|
|
|
self._instance_name = hs.get_instance_name()
|
|
|
|
|
|
|
|
if isinstance(database.engine, PostgresEngine):
|
|
|
|
self._can_write_to_account_data = (
|
|
|
|
self._instance_name in hs.config.worker.writers.account_data
|
|
|
|
)
|
|
|
|
|
|
|
|
self._account_data_id_gen = MultiWriterIdGenerator(
|
|
|
|
db_conn=db_conn,
|
|
|
|
db=database,
|
|
|
|
stream_name="account_data",
|
|
|
|
instance_name=self._instance_name,
|
|
|
|
tables=[
|
|
|
|
("room_account_data", "instance_name", "stream_id"),
|
|
|
|
("room_tags_revisions", "instance_name", "stream_id"),
|
|
|
|
("account_data", "instance_name", "stream_id"),
|
|
|
|
],
|
|
|
|
sequence_name="account_data_sequence",
|
|
|
|
writers=hs.config.worker.writers.account_data,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self._can_write_to_account_data = True
|
|
|
|
|
|
|
|
# We shouldn't be running in worker mode with SQLite, but its useful
|
|
|
|
# to support it for unit tests.
|
|
|
|
#
|
|
|
|
# If this process is the writer than we need to use
|
|
|
|
# `StreamIdGenerator`, otherwise we use `SlavedIdTracker` which gets
|
|
|
|
# updated over replication. (Multiple writers are not supported for
|
|
|
|
# SQLite).
|
2021-01-21 08:09:09 -07:00
|
|
|
if hs.get_instance_name() in hs.config.worker.writers.account_data:
|
2021-01-18 08:47:59 -07:00
|
|
|
self._account_data_id_gen = StreamIdGenerator(
|
|
|
|
db_conn,
|
|
|
|
"room_account_data",
|
|
|
|
"stream_id",
|
|
|
|
extra_tables=[("room_tags_revisions", "stream_id")],
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self._account_data_id_gen = SlavedIdTracker(
|
|
|
|
db_conn,
|
|
|
|
"room_account_data",
|
|
|
|
"stream_id",
|
|
|
|
extra_tables=[("room_tags_revisions", "stream_id")],
|
|
|
|
)
|
|
|
|
|
2018-02-16 05:08:42 -07:00
|
|
|
account_max = self.get_max_account_data_stream_id()
|
|
|
|
self._account_data_stream_cache = StreamChangeCache(
|
2019-04-03 03:07:29 -06:00
|
|
|
"AccountDataAndTagsChangeCache", account_max
|
2018-02-16 05:08:42 -07:00
|
|
|
)
|
|
|
|
|
2020-09-18 07:56:44 -06:00
|
|
|
super().__init__(database, db_conn, hs)
|
2018-02-16 05:08:42 -07:00
|
|
|
|
2021-01-18 08:47:59 -07:00
|
|
|
def get_max_account_data_stream_id(self) -> int:
|
2018-02-16 05:08:42 -07:00
|
|
|
"""Get the current max stream ID for account data stream
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
int
|
|
|
|
"""
|
2021-01-18 08:47:59 -07:00
|
|
|
return self._account_data_id_gen.get_current_token()
|
2015-12-01 11:41:32 -07:00
|
|
|
|
2016-05-03 09:01:24 -06:00
|
|
|
@cached()
|
2020-09-01 09:04:17 -06:00
|
|
|
async def get_account_data_for_user(
|
|
|
|
self, user_id: str
|
|
|
|
) -> Tuple[Dict[str, JsonDict], Dict[str, Dict[str, JsonDict]]]:
|
2015-12-01 11:41:32 -07:00
|
|
|
"""Get all the client account_data for a user.
|
|
|
|
|
|
|
|
Args:
|
2020-09-01 09:04:17 -06:00
|
|
|
user_id: The user to get the account_data for.
|
2015-12-01 11:41:32 -07:00
|
|
|
Returns:
|
2020-09-01 09:04:17 -06:00
|
|
|
A 2-tuple of a dict of global account_data and a dict mapping from
|
|
|
|
room_id string to per room account_data dicts.
|
2015-12-01 11:41:32 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
def get_account_data_for_user_txn(txn):
|
2020-08-05 14:38:57 -06:00
|
|
|
rows = self.db_pool.simple_select_list_txn(
|
2019-04-03 03:07:29 -06:00
|
|
|
txn,
|
|
|
|
"account_data",
|
|
|
|
{"user_id": user_id},
|
|
|
|
["account_data_type", "content"],
|
2015-12-01 11:41:32 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
global_account_data = {
|
2020-07-16 09:32:19 -06:00
|
|
|
row["account_data_type"]: db_to_json(row["content"]) for row in rows
|
2015-12-01 11:41:32 -07:00
|
|
|
}
|
|
|
|
|
2020-08-05 14:38:57 -06:00
|
|
|
rows = self.db_pool.simple_select_list_txn(
|
2019-04-03 03:07:29 -06:00
|
|
|
txn,
|
|
|
|
"room_account_data",
|
|
|
|
{"user_id": user_id},
|
|
|
|
["room_id", "account_data_type", "content"],
|
2015-12-01 11:41:32 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
by_room = {}
|
|
|
|
for row in rows:
|
|
|
|
room_data = by_room.setdefault(row["room_id"], {})
|
2020-07-16 09:32:19 -06:00
|
|
|
room_data[row["account_data_type"]] = db_to_json(row["content"])
|
2015-12-01 11:41:32 -07:00
|
|
|
|
2019-08-30 09:28:26 -06:00
|
|
|
return global_account_data, by_room
|
2015-12-01 11:41:32 -07:00
|
|
|
|
2020-09-01 09:04:17 -06:00
|
|
|
return await self.db_pool.runInteraction(
|
2015-12-01 11:41:32 -07:00
|
|
|
"get_account_data_for_user", get_account_data_for_user_txn
|
|
|
|
)
|
|
|
|
|
2020-08-12 07:29:06 -06:00
|
|
|
@cached(num_args=2, max_entries=5000)
|
|
|
|
async def get_global_account_data_by_type_for_user(
|
|
|
|
self, data_type: str, user_id: str
|
|
|
|
) -> Optional[JsonDict]:
|
2016-05-03 09:01:24 -06:00
|
|
|
"""
|
|
|
|
Returns:
|
2020-08-12 07:29:06 -06:00
|
|
|
The account data.
|
2016-05-03 09:01:24 -06:00
|
|
|
"""
|
2020-08-12 07:29:06 -06:00
|
|
|
result = await self.db_pool.simple_select_one_onecol(
|
2016-05-03 09:01:24 -06:00
|
|
|
table="account_data",
|
2019-04-03 03:07:29 -06:00
|
|
|
keyvalues={"user_id": user_id, "account_data_type": data_type},
|
2016-05-03 09:01:24 -06:00
|
|
|
retcol="content",
|
|
|
|
desc="get_global_account_data_by_type_for_user",
|
|
|
|
allow_none=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
if result:
|
2020-07-16 09:32:19 -06:00
|
|
|
return db_to_json(result)
|
2016-05-03 09:01:24 -06:00
|
|
|
else:
|
2019-07-23 07:00:55 -06:00
|
|
|
return None
|
2016-05-03 09:01:24 -06:00
|
|
|
|
2018-03-01 08:30:57 -07:00
|
|
|
@cached(num_args=2)
|
2020-09-01 09:04:17 -06:00
|
|
|
async def get_account_data_for_room(
|
|
|
|
self, user_id: str, room_id: str
|
|
|
|
) -> Dict[str, JsonDict]:
|
2015-12-01 11:41:32 -07:00
|
|
|
"""Get all the client account_data for a user for a room.
|
|
|
|
|
|
|
|
Args:
|
2020-09-01 09:04:17 -06:00
|
|
|
user_id: The user to get the account_data for.
|
|
|
|
room_id: The room to get the account_data for.
|
2015-12-01 11:41:32 -07:00
|
|
|
Returns:
|
2020-09-01 09:04:17 -06:00
|
|
|
A dict of the room account_data
|
2015-12-01 11:41:32 -07:00
|
|
|
"""
|
2019-04-03 03:07:29 -06:00
|
|
|
|
2015-12-01 11:41:32 -07:00
|
|
|
def get_account_data_for_room_txn(txn):
|
2020-08-05 14:38:57 -06:00
|
|
|
rows = self.db_pool.simple_select_list_txn(
|
2019-04-03 03:07:29 -06:00
|
|
|
txn,
|
|
|
|
"room_account_data",
|
|
|
|
{"user_id": user_id, "room_id": room_id},
|
|
|
|
["account_data_type", "content"],
|
2015-12-01 11:41:32 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
2020-07-16 09:32:19 -06:00
|
|
|
row["account_data_type"]: db_to_json(row["content"]) for row in rows
|
2015-12-01 11:41:32 -07:00
|
|
|
}
|
|
|
|
|
2020-09-01 09:04:17 -06:00
|
|
|
return await self.db_pool.runInteraction(
|
2015-12-01 11:41:32 -07:00
|
|
|
"get_account_data_for_room", get_account_data_for_room_txn
|
|
|
|
)
|
|
|
|
|
2018-03-01 08:53:04 -07:00
|
|
|
@cached(num_args=3, max_entries=5000)
|
2020-09-01 09:04:17 -06:00
|
|
|
async def get_account_data_for_room_and_type(
|
|
|
|
self, user_id: str, room_id: str, account_data_type: str
|
|
|
|
) -> Optional[JsonDict]:
|
2018-03-01 10:00:35 -07:00
|
|
|
"""Get the client account_data of given type for a user for a room.
|
2018-03-01 08:53:04 -07:00
|
|
|
|
|
|
|
Args:
|
2020-09-01 09:04:17 -06:00
|
|
|
user_id: The user to get the account_data for.
|
|
|
|
room_id: The room to get the account_data for.
|
|
|
|
account_data_type: The account data type to get.
|
2018-03-01 08:53:04 -07:00
|
|
|
Returns:
|
2020-09-01 09:04:17 -06:00
|
|
|
The room account_data for that type, or None if there isn't any set.
|
2018-03-01 08:53:04 -07:00
|
|
|
"""
|
2019-04-03 03:07:29 -06:00
|
|
|
|
2018-03-01 08:53:04 -07:00
|
|
|
def get_account_data_for_room_and_type_txn(txn):
|
2020-08-05 14:38:57 -06:00
|
|
|
content_json = self.db_pool.simple_select_one_onecol_txn(
|
2018-03-01 08:53:04 -07:00
|
|
|
txn,
|
|
|
|
table="room_account_data",
|
|
|
|
keyvalues={
|
|
|
|
"user_id": user_id,
|
|
|
|
"room_id": room_id,
|
|
|
|
"account_data_type": account_data_type,
|
|
|
|
},
|
|
|
|
retcol="content",
|
2019-04-03 03:07:29 -06:00
|
|
|
allow_none=True,
|
2018-03-01 08:53:04 -07:00
|
|
|
)
|
|
|
|
|
2020-07-16 09:32:19 -06:00
|
|
|
return db_to_json(content_json) if content_json else None
|
2018-03-01 08:53:04 -07:00
|
|
|
|
2020-09-01 09:04:17 -06:00
|
|
|
return await self.db_pool.runInteraction(
|
2019-04-03 03:07:29 -06:00
|
|
|
"get_account_data_for_room_and_type", get_account_data_for_room_and_type_txn
|
2018-03-01 08:53:04 -07:00
|
|
|
)
|
|
|
|
|
2020-05-15 12:03:25 -06:00
|
|
|
async def get_updated_global_account_data(
|
|
|
|
self, last_id: int, current_id: int, limit: int
|
|
|
|
) -> List[Tuple[int, str, str]]:
|
|
|
|
"""Get the global account_data that has changed, for the account_data stream
|
|
|
|
|
2016-03-01 07:49:41 -07:00
|
|
|
Args:
|
2020-05-15 12:03:25 -06:00
|
|
|
last_id: the last stream_id from the previous batch.
|
|
|
|
current_id: the maximum stream_id to return up to
|
|
|
|
limit: the maximum number of rows to return
|
|
|
|
|
2016-03-01 07:49:41 -07:00
|
|
|
Returns:
|
2020-05-15 12:03:25 -06:00
|
|
|
A list of tuples of stream_id int, user_id string,
|
|
|
|
and type string.
|
2016-03-01 07:49:41 -07:00
|
|
|
"""
|
2020-05-15 12:03:25 -06:00
|
|
|
if last_id == current_id:
|
|
|
|
return []
|
2016-06-08 04:33:30 -06:00
|
|
|
|
2020-05-15 12:03:25 -06:00
|
|
|
def get_updated_global_account_data_txn(txn):
|
2016-03-01 07:49:41 -07:00
|
|
|
sql = (
|
2019-11-08 04:42:55 -07:00
|
|
|
"SELECT stream_id, user_id, account_data_type"
|
2016-03-01 07:49:41 -07:00
|
|
|
" FROM account_data WHERE ? < stream_id AND stream_id <= ?"
|
|
|
|
" ORDER BY stream_id ASC LIMIT ?"
|
|
|
|
)
|
2020-05-15 12:03:25 -06:00
|
|
|
txn.execute(sql, (last_id, current_id, limit))
|
|
|
|
return txn.fetchall()
|
|
|
|
|
2020-08-05 14:38:57 -06:00
|
|
|
return await self.db_pool.runInteraction(
|
2020-05-15 12:03:25 -06:00
|
|
|
"get_updated_global_account_data", get_updated_global_account_data_txn
|
|
|
|
)
|
|
|
|
|
|
|
|
async def get_updated_room_account_data(
|
|
|
|
self, last_id: int, current_id: int, limit: int
|
|
|
|
) -> List[Tuple[int, str, str, str]]:
|
|
|
|
"""Get the global account_data that has changed, for the account_data stream
|
2016-03-01 07:49:41 -07:00
|
|
|
|
2020-05-15 12:03:25 -06:00
|
|
|
Args:
|
|
|
|
last_id: the last stream_id from the previous batch.
|
|
|
|
current_id: the maximum stream_id to return up to
|
|
|
|
limit: the maximum number of rows to return
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of tuples of stream_id int, user_id string,
|
|
|
|
room_id string and type string.
|
|
|
|
"""
|
|
|
|
if last_id == current_id:
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get_updated_room_account_data_txn(txn):
|
2016-03-01 07:49:41 -07:00
|
|
|
sql = (
|
2019-11-08 04:42:55 -07:00
|
|
|
"SELECT stream_id, user_id, room_id, account_data_type"
|
2016-03-01 07:49:41 -07:00
|
|
|
" FROM room_account_data WHERE ? < stream_id AND stream_id <= ?"
|
|
|
|
" ORDER BY stream_id ASC LIMIT ?"
|
|
|
|
)
|
2020-05-15 12:03:25 -06:00
|
|
|
txn.execute(sql, (last_id, current_id, limit))
|
|
|
|
return txn.fetchall()
|
2019-04-03 03:07:29 -06:00
|
|
|
|
2020-08-05 14:38:57 -06:00
|
|
|
return await self.db_pool.runInteraction(
|
2020-05-15 12:03:25 -06:00
|
|
|
"get_updated_room_account_data", get_updated_room_account_data_txn
|
2016-03-01 07:49:41 -07:00
|
|
|
)
|
|
|
|
|
2020-09-01 09:04:17 -06:00
|
|
|
async def get_updated_account_data_for_user(
|
|
|
|
self, user_id: str, stream_id: int
|
|
|
|
) -> Tuple[Dict[str, JsonDict], Dict[str, Dict[str, JsonDict]]]:
|
2016-03-01 07:49:41 -07:00
|
|
|
"""Get all the client account_data for a that's changed for a user
|
2015-12-01 11:41:32 -07:00
|
|
|
|
|
|
|
Args:
|
2020-09-01 09:04:17 -06:00
|
|
|
user_id: The user to get the account_data for.
|
|
|
|
stream_id: The point in the stream since which to get updates
|
2015-12-01 11:41:32 -07:00
|
|
|
Returns:
|
|
|
|
A deferred pair of a dict of global account_data and a dict
|
|
|
|
mapping from room_id string to per room account_data dicts.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get_updated_account_data_for_user_txn(txn):
|
|
|
|
sql = (
|
|
|
|
"SELECT account_data_type, content FROM account_data"
|
|
|
|
" WHERE user_id = ? AND stream_id > ?"
|
|
|
|
)
|
|
|
|
|
|
|
|
txn.execute(sql, (user_id, stream_id))
|
|
|
|
|
2020-07-16 09:32:19 -06:00
|
|
|
global_account_data = {row[0]: db_to_json(row[1]) for row in txn}
|
2015-12-01 11:41:32 -07:00
|
|
|
|
|
|
|
sql = (
|
|
|
|
"SELECT room_id, account_data_type, content FROM room_account_data"
|
|
|
|
" WHERE user_id = ? AND stream_id > ?"
|
|
|
|
)
|
|
|
|
|
|
|
|
txn.execute(sql, (user_id, stream_id))
|
|
|
|
|
|
|
|
account_data_by_room = {}
|
2017-03-23 11:53:49 -06:00
|
|
|
for row in txn:
|
2015-12-01 11:41:32 -07:00
|
|
|
room_account_data = account_data_by_room.setdefault(row[0], {})
|
2020-07-16 09:32:19 -06:00
|
|
|
room_account_data[row[1]] = db_to_json(row[2])
|
2015-12-01 11:41:32 -07:00
|
|
|
|
2019-08-30 09:28:26 -06:00
|
|
|
return global_account_data, account_data_by_room
|
2015-12-01 11:41:32 -07:00
|
|
|
|
2016-01-28 09:39:18 -07:00
|
|
|
changed = self._account_data_stream_cache.has_entity_changed(
|
2016-01-28 09:37:41 -07:00
|
|
|
user_id, int(stream_id)
|
|
|
|
)
|
|
|
|
if not changed:
|
2020-09-01 09:04:17 -06:00
|
|
|
return ({}, {})
|
2016-01-28 09:37:41 -07:00
|
|
|
|
2020-09-01 09:04:17 -06:00
|
|
|
return await self.db_pool.runInteraction(
|
2015-12-01 11:41:32 -07:00
|
|
|
"get_updated_account_data_for_user", get_updated_account_data_for_user_txn
|
|
|
|
)
|
|
|
|
|
2021-01-07 06:03:38 -07:00
|
|
|
@cached(max_entries=5000, iterable=True)
|
|
|
|
async def ignored_by(self, user_id: str) -> Set[str]:
|
|
|
|
"""
|
|
|
|
Get users which ignore the given user.
|
2018-02-16 05:08:42 -07:00
|
|
|
|
2021-01-07 06:03:38 -07:00
|
|
|
Params:
|
|
|
|
user_id: The user ID which might be ignored.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
The user IDs which ignore the given user.
|
|
|
|
"""
|
|
|
|
return set(
|
|
|
|
await self.db_pool.simple_select_onecol(
|
|
|
|
table="ignored_users",
|
|
|
|
keyvalues={"ignored_user_id": user_id},
|
|
|
|
retcol="ignorer_user_id",
|
|
|
|
desc="ignored_by",
|
|
|
|
)
|
|
|
|
)
|
2018-02-16 05:08:42 -07:00
|
|
|
|
2021-01-18 08:47:59 -07:00
|
|
|
def process_replication_rows(self, stream_name, instance_name, token, rows):
|
|
|
|
if stream_name == TagAccountDataStream.NAME:
|
|
|
|
self._account_data_id_gen.advance(instance_name, token)
|
|
|
|
for row in rows:
|
|
|
|
self.get_tags_for_user.invalidate((row.user_id,))
|
|
|
|
self._account_data_stream_cache.entity_has_changed(row.user_id, token)
|
|
|
|
elif stream_name == AccountDataStream.NAME:
|
|
|
|
self._account_data_id_gen.advance(instance_name, token)
|
|
|
|
for row in rows:
|
|
|
|
if not row.room_id:
|
|
|
|
self.get_global_account_data_by_type_for_user.invalidate(
|
|
|
|
(row.data_type, row.user_id)
|
|
|
|
)
|
|
|
|
self.get_account_data_for_user.invalidate((row.user_id,))
|
|
|
|
self.get_account_data_for_room.invalidate((row.user_id, row.room_id))
|
|
|
|
self.get_account_data_for_room_and_type.invalidate(
|
|
|
|
(row.user_id, row.room_id, row.data_type)
|
|
|
|
)
|
|
|
|
self._account_data_stream_cache.entity_has_changed(row.user_id, token)
|
|
|
|
return super().process_replication_rows(stream_name, instance_name, token, rows)
|
2018-02-16 05:08:42 -07:00
|
|
|
|
2020-08-12 07:29:06 -06:00
|
|
|
async def add_account_data_to_room(
|
|
|
|
self, user_id: str, room_id: str, account_data_type: str, content: JsonDict
|
|
|
|
) -> int:
|
2015-12-01 11:41:32 -07:00
|
|
|
"""Add some account_data to a room for a user.
|
2020-08-12 07:29:06 -06:00
|
|
|
|
2015-12-01 11:41:32 -07:00
|
|
|
Args:
|
2020-08-12 07:29:06 -06:00
|
|
|
user_id: The user to add a tag for.
|
|
|
|
room_id: The room to add a tag for.
|
|
|
|
account_data_type: The type of account_data to add.
|
|
|
|
content: A json object to associate with the tag.
|
|
|
|
|
2015-12-01 11:41:32 -07:00
|
|
|
Returns:
|
2020-08-12 07:29:06 -06:00
|
|
|
The maximum stream ID.
|
2015-12-01 11:41:32 -07:00
|
|
|
"""
|
2021-01-18 08:47:59 -07:00
|
|
|
assert self._can_write_to_account_data
|
|
|
|
|
2020-08-07 06:02:55 -06:00
|
|
|
content_json = json_encoder.encode(content)
|
2015-12-01 11:41:32 -07:00
|
|
|
|
2020-09-23 09:11:18 -06:00
|
|
|
async with self._account_data_id_gen.get_next() as next_id:
|
2017-11-16 11:07:01 -07:00
|
|
|
# no need to lock here as room_account_data has a unique constraint
|
2019-12-04 03:15:55 -07:00
|
|
|
# on (user_id, room_id, account_data_type) so simple_upsert will
|
2017-11-16 11:07:01 -07:00
|
|
|
# retry if there is a conflict.
|
2020-08-12 07:29:06 -06:00
|
|
|
await self.db_pool.simple_upsert(
|
2017-11-16 11:07:01 -07:00
|
|
|
desc="add_room_account_data",
|
2015-12-01 11:41:32 -07:00
|
|
|
table="room_account_data",
|
|
|
|
keyvalues={
|
|
|
|
"user_id": user_id,
|
|
|
|
"room_id": room_id,
|
|
|
|
"account_data_type": account_data_type,
|
|
|
|
},
|
2019-04-03 03:07:29 -06:00
|
|
|
values={"stream_id": next_id, "content": content_json},
|
2017-11-16 11:07:01 -07:00
|
|
|
lock=False,
|
2016-01-29 09:41:51 -07:00
|
|
|
)
|
2015-12-01 11:41:32 -07:00
|
|
|
|
2017-11-16 11:07:01 -07:00
|
|
|
self._account_data_stream_cache.entity_has_changed(user_id, next_id)
|
|
|
|
self.get_account_data_for_user.invalidate((user_id,))
|
2019-04-03 03:07:29 -06:00
|
|
|
self.get_account_data_for_room.invalidate((user_id, room_id))
|
2018-03-01 08:53:04 -07:00
|
|
|
self.get_account_data_for_room_and_type.prefill(
|
2019-04-03 03:07:29 -06:00
|
|
|
(user_id, room_id, account_data_type), content
|
2018-03-01 08:53:04 -07:00
|
|
|
)
|
2015-12-01 11:41:32 -07:00
|
|
|
|
2020-08-12 07:29:06 -06:00
|
|
|
return self._account_data_id_gen.get_current_token()
|
2015-12-01 11:41:32 -07:00
|
|
|
|
2020-08-12 07:29:06 -06:00
|
|
|
async def add_account_data_for_user(
|
|
|
|
self, user_id: str, account_data_type: str, content: JsonDict
|
|
|
|
) -> int:
|
2015-12-01 11:41:32 -07:00
|
|
|
"""Add some account_data to a room for a user.
|
2020-08-12 07:29:06 -06:00
|
|
|
|
2015-12-01 11:41:32 -07:00
|
|
|
Args:
|
2020-08-12 07:29:06 -06:00
|
|
|
user_id: The user to add a tag for.
|
|
|
|
account_data_type: The type of account_data to add.
|
|
|
|
content: A json object to associate with the tag.
|
|
|
|
|
2015-12-01 11:41:32 -07:00
|
|
|
Returns:
|
2020-08-12 07:29:06 -06:00
|
|
|
The maximum stream ID.
|
2015-12-01 11:41:32 -07:00
|
|
|
"""
|
2021-01-18 08:47:59 -07:00
|
|
|
assert self._can_write_to_account_data
|
|
|
|
|
2020-09-23 09:11:18 -06:00
|
|
|
async with self._account_data_id_gen.get_next() as next_id:
|
2021-01-07 06:03:38 -07:00
|
|
|
await self.db_pool.runInteraction(
|
|
|
|
"add_user_account_data",
|
|
|
|
self._add_account_data_for_user,
|
|
|
|
next_id,
|
|
|
|
user_id,
|
|
|
|
account_data_type,
|
|
|
|
content,
|
2015-12-01 11:41:32 -07:00
|
|
|
)
|
2017-11-16 11:07:01 -07:00
|
|
|
|
2019-04-03 03:07:29 -06:00
|
|
|
self._account_data_stream_cache.entity_has_changed(user_id, next_id)
|
2017-11-16 11:07:01 -07:00
|
|
|
self.get_account_data_for_user.invalidate((user_id,))
|
|
|
|
self.get_global_account_data_by_type_for_user.invalidate(
|
2019-04-03 03:07:29 -06:00
|
|
|
(account_data_type, user_id)
|
2016-05-03 09:01:24 -06:00
|
|
|
)
|
2015-12-01 11:41:32 -07:00
|
|
|
|
2020-08-12 07:29:06 -06:00
|
|
|
return self._account_data_id_gen.get_current_token()
|
2015-12-01 11:41:32 -07:00
|
|
|
|
2021-01-07 06:03:38 -07:00
|
|
|
def _add_account_data_for_user(
|
|
|
|
self,
|
|
|
|
txn,
|
|
|
|
next_id: int,
|
|
|
|
user_id: str,
|
|
|
|
account_data_type: str,
|
|
|
|
content: JsonDict,
|
|
|
|
) -> None:
|
|
|
|
content_json = json_encoder.encode(content)
|
|
|
|
|
|
|
|
# no need to lock here as account_data has a unique constraint on
|
|
|
|
# (user_id, account_data_type) so simple_upsert will retry if
|
|
|
|
# there is a conflict.
|
|
|
|
self.db_pool.simple_upsert_txn(
|
|
|
|
txn,
|
|
|
|
table="account_data",
|
|
|
|
keyvalues={"user_id": user_id, "account_data_type": account_data_type},
|
|
|
|
values={"stream_id": next_id, "content": content_json},
|
|
|
|
lock=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Ignored users get denormalized into a separate table as an optimisation.
|
|
|
|
if account_data_type != AccountDataTypes.IGNORED_USER_LIST:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Insert / delete to sync the list of ignored users.
|
|
|
|
previously_ignored_users = set(
|
|
|
|
self.db_pool.simple_select_onecol_txn(
|
|
|
|
txn,
|
|
|
|
table="ignored_users",
|
|
|
|
keyvalues={"ignorer_user_id": user_id},
|
|
|
|
retcol="ignored_user_id",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# If the data is invalid, no one is ignored.
|
|
|
|
ignored_users_content = content.get("ignored_users", {})
|
|
|
|
if isinstance(ignored_users_content, dict):
|
|
|
|
currently_ignored_users = set(ignored_users_content)
|
|
|
|
else:
|
|
|
|
currently_ignored_users = set()
|
|
|
|
|
|
|
|
# Delete entries which are no longer ignored.
|
|
|
|
self.db_pool.simple_delete_many_txn(
|
|
|
|
txn,
|
|
|
|
table="ignored_users",
|
|
|
|
column="ignored_user_id",
|
|
|
|
iterable=previously_ignored_users - currently_ignored_users,
|
|
|
|
keyvalues={"ignorer_user_id": user_id},
|
|
|
|
)
|
|
|
|
|
|
|
|
# Add entries which are newly ignored.
|
|
|
|
self.db_pool.simple_insert_many_txn(
|
|
|
|
txn,
|
|
|
|
table="ignored_users",
|
|
|
|
values=[
|
|
|
|
{"ignorer_user_id": user_id, "ignored_user_id": u}
|
|
|
|
for u in currently_ignored_users - previously_ignored_users
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
# Invalidate the cache for any ignored users which were added or removed.
|
|
|
|
for ignored_user_id in previously_ignored_users ^ currently_ignored_users:
|
|
|
|
self._invalidate_cache_and_stream(txn, self.ignored_by, (ignored_user_id,))
|
2021-01-18 08:47:59 -07:00
|
|
|
|
|
|
|
|
|
|
|
class AccountDataStore(AccountDataWorkerStore):
|
|
|
|
pass
|