2016-08-25 10:35:37 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2016 OpenMarket 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.
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import ujson
|
|
|
|
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
2017-01-10 08:04:57 -07:00
|
|
|
from .background_updates import BackgroundUpdateStore
|
2016-08-25 10:35:37 -06:00
|
|
|
|
2017-03-09 07:50:40 -07:00
|
|
|
from synapse.util.caches.expiringcache import ExpiringCache
|
|
|
|
|
2016-08-25 10:35:37 -06:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-01-10 08:04:57 -07:00
|
|
|
class DeviceInboxStore(BackgroundUpdateStore):
|
|
|
|
DEVICE_INBOX_STREAM_ID = "device_inbox_stream_drop"
|
|
|
|
|
2017-11-09 11:51:27 -07:00
|
|
|
def __init__(self, db_conn, hs):
|
|
|
|
super(DeviceInboxStore, self).__init__(db_conn, hs)
|
2017-01-10 08:04:57 -07:00
|
|
|
|
|
|
|
self.register_background_index_update(
|
|
|
|
"device_inbox_stream_index",
|
|
|
|
index_name="device_inbox_stream_id_user_id",
|
|
|
|
table="device_inbox",
|
|
|
|
columns=["stream_id", "user_id"],
|
|
|
|
)
|
|
|
|
|
|
|
|
self.register_background_update_handler(
|
|
|
|
self.DEVICE_INBOX_STREAM_ID,
|
|
|
|
self._background_drop_index_device_inbox,
|
|
|
|
)
|
2016-08-25 10:35:37 -06:00
|
|
|
|
2017-03-09 07:50:40 -07:00
|
|
|
# Map of (user_id, device_id) to the last stream_id that has been
|
|
|
|
# deleted up to. This is so that we can no op deletions.
|
|
|
|
self._last_device_delete_cache = ExpiringCache(
|
|
|
|
cache_name="last_device_delete_cache",
|
|
|
|
clock=self._clock,
|
|
|
|
max_len=10000,
|
|
|
|
expiry_ms=30 * 60 * 1000,
|
|
|
|
)
|
|
|
|
|
2016-08-25 10:35:37 -06:00
|
|
|
@defer.inlineCallbacks
|
2016-09-06 08:12:13 -06:00
|
|
|
def add_messages_to_device_inbox(self, local_messages_by_user_then_device,
|
|
|
|
remote_messages_by_destination):
|
|
|
|
"""Used to send messages from this server.
|
|
|
|
|
2016-08-25 10:35:37 -06:00
|
|
|
Args:
|
2016-09-06 08:12:13 -06:00
|
|
|
sender_user_id(str): The ID of the user sending these messages.
|
|
|
|
local_messages_by_user_and_device(dict):
|
2016-08-25 10:35:37 -06:00
|
|
|
Dictionary of user_id to device_id to message.
|
2016-09-06 08:12:13 -06:00
|
|
|
remote_messages_by_destination(dict):
|
|
|
|
Dictionary of destination server_name to the EDU JSON to send.
|
2016-08-25 10:35:37 -06:00
|
|
|
Returns:
|
2016-08-31 03:42:52 -06:00
|
|
|
A deferred stream_id that resolves when the messages have been
|
|
|
|
inserted.
|
2016-08-25 10:35:37 -06:00
|
|
|
"""
|
|
|
|
|
2016-09-07 05:03:37 -06:00
|
|
|
def add_messages_txn(txn, now_ms, stream_id):
|
|
|
|
# Add the local messages directly to the local inbox.
|
|
|
|
self._add_messages_to_local_device_inbox_txn(
|
|
|
|
txn, stream_id, local_messages_by_user_then_device
|
|
|
|
)
|
|
|
|
|
|
|
|
# Add the remote messages to the federation outbox.
|
|
|
|
# We'll send them to a remote server when we next send a
|
|
|
|
# federation transaction to that destination.
|
2016-09-06 08:12:13 -06:00
|
|
|
sql = (
|
|
|
|
"INSERT INTO device_federation_outbox"
|
|
|
|
" (destination, stream_id, queued_ts, messages_json)"
|
|
|
|
" VALUES (?,?,?,?)"
|
|
|
|
)
|
|
|
|
rows = []
|
|
|
|
for destination, edu in remote_messages_by_destination.items():
|
|
|
|
edu_json = ujson.dumps(edu)
|
|
|
|
rows.append((destination, stream_id, now_ms, edu_json))
|
|
|
|
txn.executemany(sql, rows)
|
|
|
|
|
2016-09-06 04:26:37 -06:00
|
|
|
with self._device_inbox_id_gen.get_next() as stream_id:
|
2016-09-06 11:16:20 -06:00
|
|
|
now_ms = self.clock.time_msec()
|
2016-09-06 04:26:37 -06:00
|
|
|
yield self.runInteraction(
|
|
|
|
"add_messages_to_device_inbox",
|
2016-09-06 08:12:13 -06:00
|
|
|
add_messages_txn,
|
|
|
|
now_ms,
|
2016-09-06 04:26:37 -06:00
|
|
|
stream_id,
|
|
|
|
)
|
2016-09-07 08:27:07 -06:00
|
|
|
for user_id in local_messages_by_user_then_device.keys():
|
|
|
|
self._device_inbox_stream_cache.entity_has_changed(
|
|
|
|
user_id, stream_id
|
|
|
|
)
|
|
|
|
for destination in remote_messages_by_destination.keys():
|
|
|
|
self._device_federation_outbox_stream_cache.entity_has_changed(
|
|
|
|
destination, stream_id
|
|
|
|
)
|
2016-09-06 04:26:37 -06:00
|
|
|
|
|
|
|
defer.returnValue(self._device_inbox_id_gen.get_current_token())
|
|
|
|
|
2016-09-06 08:12:13 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def add_messages_from_remote_to_device_inbox(
|
|
|
|
self, origin, message_id, local_messages_by_user_then_device
|
|
|
|
):
|
|
|
|
def add_messages_txn(txn, now_ms, stream_id):
|
2016-09-07 05:03:37 -06:00
|
|
|
# Check if we've already inserted a matching message_id for that
|
|
|
|
# origin. This can happen if the origin doesn't receive our
|
|
|
|
# acknowledgement from the first time we received the message.
|
2016-09-06 08:12:13 -06:00
|
|
|
already_inserted = self._simple_select_one_txn(
|
|
|
|
txn, table="device_federation_inbox",
|
|
|
|
keyvalues={"origin": origin, "message_id": message_id},
|
|
|
|
retcols=("message_id",),
|
|
|
|
allow_none=True,
|
|
|
|
)
|
|
|
|
if already_inserted is not None:
|
|
|
|
return
|
|
|
|
|
2016-09-07 05:03:37 -06:00
|
|
|
# Add an entry for this message_id so that we know we've processed
|
|
|
|
# it.
|
2016-09-06 08:12:13 -06:00
|
|
|
self._simple_insert_txn(
|
|
|
|
txn, table="device_federation_inbox",
|
|
|
|
values={
|
|
|
|
"origin": origin,
|
|
|
|
"message_id": message_id,
|
|
|
|
"received_ts": now_ms,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2016-09-07 05:03:37 -06:00
|
|
|
# Add the messages to the approriate local device inboxes so that
|
|
|
|
# they'll be sent to the devices when they next sync.
|
2016-09-06 08:12:13 -06:00
|
|
|
self._add_messages_to_local_device_inbox_txn(
|
|
|
|
txn, stream_id, local_messages_by_user_then_device
|
|
|
|
)
|
|
|
|
|
|
|
|
with self._device_inbox_id_gen.get_next() as stream_id:
|
2016-09-06 11:16:20 -06:00
|
|
|
now_ms = self.clock.time_msec()
|
2016-09-06 08:12:13 -06:00
|
|
|
yield self.runInteraction(
|
|
|
|
"add_messages_from_remote_to_device_inbox",
|
|
|
|
add_messages_txn,
|
|
|
|
now_ms,
|
|
|
|
stream_id,
|
|
|
|
)
|
2016-09-07 08:27:07 -06:00
|
|
|
for user_id in local_messages_by_user_then_device.keys():
|
|
|
|
self._device_inbox_stream_cache.entity_has_changed(
|
|
|
|
user_id, stream_id
|
|
|
|
)
|
2016-09-06 08:12:13 -06:00
|
|
|
|
2016-09-12 05:30:46 -06:00
|
|
|
defer.returnValue(stream_id)
|
|
|
|
|
2016-09-06 08:12:13 -06:00
|
|
|
def _add_messages_to_local_device_inbox_txn(self, txn, stream_id,
|
|
|
|
messages_by_user_then_device):
|
2016-09-09 04:48:23 -06:00
|
|
|
sql = (
|
|
|
|
"UPDATE device_max_stream_id"
|
|
|
|
" SET stream_id = ?"
|
|
|
|
" WHERE stream_id < ?"
|
|
|
|
)
|
|
|
|
txn.execute(sql, (stream_id, stream_id))
|
|
|
|
|
2016-09-08 08:13:05 -06:00
|
|
|
local_by_user_then_device = {}
|
2016-09-06 04:26:37 -06:00
|
|
|
for user_id, messages_by_device in messages_by_user_then_device.items():
|
2016-09-08 08:13:05 -06:00
|
|
|
messages_json_for_user = {}
|
2016-09-06 04:26:37 -06:00
|
|
|
devices = messages_by_device.keys()
|
2016-09-08 08:13:05 -06:00
|
|
|
if len(devices) == 1 and devices[0] == "*":
|
|
|
|
# Handle wildcard device_ids.
|
|
|
|
sql = (
|
|
|
|
"SELECT device_id FROM devices"
|
|
|
|
" WHERE user_id = ?"
|
|
|
|
)
|
|
|
|
txn.execute(sql, (user_id,))
|
|
|
|
message_json = ujson.dumps(messages_by_device["*"])
|
2017-03-23 11:53:49 -06:00
|
|
|
for row in txn:
|
2016-09-08 08:13:05 -06:00
|
|
|
# Add the message for all devices for this user on this
|
|
|
|
# server.
|
|
|
|
device = row[0]
|
|
|
|
messages_json_for_user[device] = message_json
|
|
|
|
else:
|
2016-09-09 04:48:23 -06:00
|
|
|
if not devices:
|
|
|
|
continue
|
2016-09-08 08:13:05 -06:00
|
|
|
sql = (
|
|
|
|
"SELECT device_id FROM devices"
|
|
|
|
" WHERE user_id = ? AND device_id IN ("
|
|
|
|
+ ",".join("?" * len(devices))
|
|
|
|
+ ")"
|
|
|
|
)
|
|
|
|
# TODO: Maybe this needs to be done in batches if there are
|
|
|
|
# too many local devices for a given user.
|
|
|
|
txn.execute(sql, [user_id] + devices)
|
2017-03-23 11:53:49 -06:00
|
|
|
for row in txn:
|
2016-09-08 08:13:05 -06:00
|
|
|
# Only insert into the local inbox if the device exists on
|
|
|
|
# this server
|
|
|
|
device = row[0]
|
|
|
|
message_json = ujson.dumps(messages_by_device[device])
|
|
|
|
messages_json_for_user[device] = message_json
|
|
|
|
|
2016-09-09 04:48:23 -06:00
|
|
|
if messages_json_for_user:
|
|
|
|
local_by_user_then_device[user_id] = messages_json_for_user
|
|
|
|
|
|
|
|
if not local_by_user_then_device:
|
|
|
|
return
|
2016-08-25 10:35:37 -06:00
|
|
|
|
2016-09-06 04:26:37 -06:00
|
|
|
sql = (
|
|
|
|
"INSERT INTO device_inbox"
|
|
|
|
" (user_id, device_id, stream_id, message_json)"
|
|
|
|
" VALUES (?,?,?,?)"
|
|
|
|
)
|
|
|
|
rows = []
|
2016-09-08 08:13:05 -06:00
|
|
|
for user_id, messages_by_device in local_by_user_then_device.items():
|
|
|
|
for device_id, message_json in messages_by_device.items():
|
|
|
|
rows.append((user_id, device_id, stream_id, message_json))
|
2016-08-25 10:35:37 -06:00
|
|
|
|
2016-09-06 04:26:37 -06:00
|
|
|
txn.executemany(sql, rows)
|
2016-08-31 03:42:52 -06:00
|
|
|
|
2016-08-25 10:35:37 -06:00
|
|
|
def get_new_messages_for_device(
|
2016-09-02 08:50:37 -06:00
|
|
|
self, user_id, device_id, last_stream_id, current_stream_id, limit=100
|
2016-08-25 10:35:37 -06:00
|
|
|
):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
user_id(str): The recipient user_id.
|
|
|
|
device_id(str): The recipient device_id.
|
|
|
|
current_stream_id(int): The current position of the to device
|
|
|
|
message stream.
|
|
|
|
Returns:
|
|
|
|
Deferred ([dict], int): List of messages for the device and where
|
|
|
|
in the stream the messages got to.
|
|
|
|
"""
|
2016-09-07 08:27:07 -06:00
|
|
|
has_changed = self._device_inbox_stream_cache.has_entity_changed(
|
|
|
|
user_id, last_stream_id
|
|
|
|
)
|
|
|
|
if not has_changed:
|
|
|
|
return defer.succeed(([], current_stream_id))
|
|
|
|
|
2016-08-25 10:35:37 -06:00
|
|
|
def get_new_messages_for_device_txn(txn):
|
|
|
|
sql = (
|
|
|
|
"SELECT stream_id, message_json FROM device_inbox"
|
|
|
|
" WHERE user_id = ? AND device_id = ?"
|
2016-09-02 08:50:37 -06:00
|
|
|
" AND ? < stream_id AND stream_id <= ?"
|
2016-08-25 10:35:37 -06:00
|
|
|
" ORDER BY stream_id ASC"
|
|
|
|
" LIMIT ?"
|
|
|
|
)
|
2016-09-02 08:50:37 -06:00
|
|
|
txn.execute(sql, (
|
|
|
|
user_id, device_id, last_stream_id, current_stream_id, limit
|
|
|
|
))
|
2016-08-25 10:35:37 -06:00
|
|
|
messages = []
|
2017-03-23 11:53:49 -06:00
|
|
|
for row in txn:
|
2016-08-25 10:35:37 -06:00
|
|
|
stream_pos = row[0]
|
|
|
|
messages.append(ujson.loads(row[1]))
|
|
|
|
if len(messages) < limit:
|
|
|
|
stream_pos = current_stream_id
|
|
|
|
return (messages, stream_pos)
|
|
|
|
|
|
|
|
return self.runInteraction(
|
|
|
|
"get_new_messages_for_device", get_new_messages_for_device_txn,
|
|
|
|
)
|
|
|
|
|
2017-03-09 07:50:40 -07:00
|
|
|
@defer.inlineCallbacks
|
2016-08-25 10:35:37 -06:00
|
|
|
def delete_messages_for_device(self, user_id, device_id, up_to_stream_id):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
user_id(str): The recipient user_id.
|
|
|
|
device_id(str): The recipient device_id.
|
|
|
|
up_to_stream_id(int): Where to delete messages up to.
|
|
|
|
Returns:
|
2016-12-15 11:13:58 -07:00
|
|
|
A deferred that resolves to the number of messages deleted.
|
2016-08-25 10:35:37 -06:00
|
|
|
"""
|
2017-03-09 07:50:40 -07:00
|
|
|
# If we have cached the last stream id we've deleted up to, we can
|
|
|
|
# check if there is likely to be anything that needs deleting
|
|
|
|
last_deleted_stream_id = self._last_device_delete_cache.get(
|
|
|
|
(user_id, device_id), None
|
|
|
|
)
|
|
|
|
if last_deleted_stream_id:
|
|
|
|
has_changed = self._device_inbox_stream_cache.has_entity_changed(
|
|
|
|
user_id, last_deleted_stream_id
|
|
|
|
)
|
|
|
|
if not has_changed:
|
|
|
|
defer.returnValue(0)
|
|
|
|
|
2016-08-25 10:35:37 -06:00
|
|
|
def delete_messages_for_device_txn(txn):
|
|
|
|
sql = (
|
|
|
|
"DELETE FROM device_inbox"
|
|
|
|
" WHERE user_id = ? AND device_id = ?"
|
|
|
|
" AND stream_id <= ?"
|
|
|
|
)
|
|
|
|
txn.execute(sql, (user_id, device_id, up_to_stream_id))
|
2016-12-15 11:13:58 -07:00
|
|
|
return txn.rowcount
|
2016-08-25 10:35:37 -06:00
|
|
|
|
2017-03-09 07:50:40 -07:00
|
|
|
count = yield self.runInteraction(
|
2016-08-25 11:14:02 -06:00
|
|
|
"delete_messages_for_device", delete_messages_for_device_txn
|
|
|
|
)
|
|
|
|
|
2017-03-09 07:50:40 -07:00
|
|
|
# Update the cache, ensuring that we only ever increase the value
|
|
|
|
last_deleted_stream_id = self._last_device_delete_cache.get(
|
|
|
|
(user_id, device_id), 0
|
|
|
|
)
|
|
|
|
self._last_device_delete_cache[(user_id, device_id)] = max(
|
|
|
|
last_deleted_stream_id, up_to_stream_id
|
|
|
|
)
|
|
|
|
|
|
|
|
defer.returnValue(count)
|
|
|
|
|
2016-08-31 03:38:58 -06:00
|
|
|
def get_all_new_device_messages(self, last_pos, current_pos, limit):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
last_pos(int):
|
|
|
|
current_pos(int):
|
|
|
|
limit(int):
|
|
|
|
Returns:
|
|
|
|
A deferred list of rows from the device inbox
|
|
|
|
"""
|
|
|
|
if last_pos == current_pos:
|
|
|
|
return defer.succeed([])
|
|
|
|
|
|
|
|
def get_all_new_device_messages_txn(txn):
|
2016-11-17 08:46:44 -07:00
|
|
|
# We limit like this as we might have multiple rows per stream_id, and
|
|
|
|
# we want to make sure we always get all entries for any stream_id
|
|
|
|
# we return.
|
|
|
|
upper_pos = min(current_pos, last_pos + limit)
|
2016-08-31 03:38:58 -06:00
|
|
|
sql = (
|
2017-04-04 09:21:21 -06:00
|
|
|
"SELECT max(stream_id), user_id"
|
2016-11-17 08:46:44 -07:00
|
|
|
" FROM device_inbox"
|
2016-08-31 03:38:58 -06:00
|
|
|
" WHERE ? < stream_id AND stream_id <= ?"
|
2017-04-04 09:21:21 -06:00
|
|
|
" GROUP BY user_id"
|
2016-08-31 03:38:58 -06:00
|
|
|
)
|
2016-11-17 08:46:44 -07:00
|
|
|
txn.execute(sql, (last_pos, upper_pos))
|
|
|
|
rows = txn.fetchall()
|
2016-08-31 03:38:58 -06:00
|
|
|
|
|
|
|
sql = (
|
2017-04-04 09:21:21 -06:00
|
|
|
"SELECT max(stream_id), destination"
|
2016-11-17 08:46:44 -07:00
|
|
|
" FROM device_federation_outbox"
|
2016-08-31 03:38:58 -06:00
|
|
|
" WHERE ? < stream_id AND stream_id <= ?"
|
2017-04-04 09:21:21 -06:00
|
|
|
" GROUP BY destination"
|
2016-08-31 03:38:58 -06:00
|
|
|
)
|
2016-11-17 08:46:44 -07:00
|
|
|
txn.execute(sql, (last_pos, upper_pos))
|
2017-03-23 11:53:49 -06:00
|
|
|
rows.extend(txn)
|
2016-11-17 08:46:44 -07:00
|
|
|
|
2017-04-04 09:21:21 -06:00
|
|
|
# Order by ascending stream ordering
|
|
|
|
rows.sort()
|
|
|
|
|
2016-11-17 08:46:44 -07:00
|
|
|
return rows
|
2016-08-31 03:38:58 -06:00
|
|
|
|
|
|
|
return self.runInteraction(
|
|
|
|
"get_all_new_device_messages", get_all_new_device_messages_txn
|
|
|
|
)
|
|
|
|
|
2016-08-25 10:35:37 -06:00
|
|
|
def get_to_device_stream_token(self):
|
|
|
|
return self._device_inbox_id_gen.get_current_token()
|
2016-09-06 08:12:13 -06:00
|
|
|
|
2016-09-06 11:16:20 -06:00
|
|
|
def get_new_device_msgs_for_remote(
|
2016-09-06 08:12:13 -06:00
|
|
|
self, destination, last_stream_id, current_stream_id, limit=100
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
destination(str): The name of the remote server.
|
2017-03-15 06:16:55 -06:00
|
|
|
last_stream_id(int|long): The last position of the device message stream
|
2016-09-06 08:12:13 -06:00
|
|
|
that the server sent up to.
|
2017-03-15 06:16:55 -06:00
|
|
|
current_stream_id(int|long): The current position of the device
|
2016-09-06 08:12:13 -06:00
|
|
|
message stream.
|
|
|
|
Returns:
|
2017-03-15 06:16:55 -06:00
|
|
|
Deferred ([dict], int|long): List of messages for the device and where
|
2016-09-06 08:12:13 -06:00
|
|
|
in the stream the messages got to.
|
|
|
|
"""
|
2016-09-07 08:27:07 -06:00
|
|
|
|
|
|
|
has_changed = self._device_federation_outbox_stream_cache.has_entity_changed(
|
|
|
|
destination, last_stream_id
|
|
|
|
)
|
2016-09-09 04:48:23 -06:00
|
|
|
if not has_changed or last_stream_id == current_stream_id:
|
2016-09-07 08:27:07 -06:00
|
|
|
return defer.succeed(([], current_stream_id))
|
|
|
|
|
2016-09-06 08:12:13 -06:00
|
|
|
def get_new_messages_for_remote_destination_txn(txn):
|
|
|
|
sql = (
|
|
|
|
"SELECT stream_id, messages_json FROM device_federation_outbox"
|
|
|
|
" WHERE destination = ?"
|
|
|
|
" AND ? < stream_id AND stream_id <= ?"
|
|
|
|
" ORDER BY stream_id ASC"
|
|
|
|
" LIMIT ?"
|
|
|
|
)
|
|
|
|
txn.execute(sql, (
|
|
|
|
destination, last_stream_id, current_stream_id, limit
|
|
|
|
))
|
|
|
|
messages = []
|
2017-03-23 11:53:49 -06:00
|
|
|
for row in txn:
|
2016-09-06 08:12:13 -06:00
|
|
|
stream_pos = row[0]
|
|
|
|
messages.append(ujson.loads(row[1]))
|
|
|
|
if len(messages) < limit:
|
|
|
|
stream_pos = current_stream_id
|
|
|
|
return (messages, stream_pos)
|
|
|
|
|
|
|
|
return self.runInteraction(
|
2016-09-06 11:16:20 -06:00
|
|
|
"get_new_device_msgs_for_remote",
|
2016-09-06 08:12:13 -06:00
|
|
|
get_new_messages_for_remote_destination_txn,
|
|
|
|
)
|
|
|
|
|
2016-09-06 11:16:20 -06:00
|
|
|
def delete_device_msgs_for_remote(self, destination, up_to_stream_id):
|
2016-09-06 08:12:13 -06:00
|
|
|
"""Used to delete messages when the remote destination acknowledges
|
|
|
|
their receipt.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
destination(str): The destination server_name
|
|
|
|
up_to_stream_id(int): Where to delete messages up to.
|
|
|
|
Returns:
|
|
|
|
A deferred that resolves when the messages have been deleted.
|
|
|
|
"""
|
|
|
|
def delete_messages_for_remote_destination_txn(txn):
|
|
|
|
sql = (
|
|
|
|
"DELETE FROM device_federation_outbox"
|
2016-09-06 11:16:20 -06:00
|
|
|
" WHERE destination = ?"
|
2016-09-06 08:12:13 -06:00
|
|
|
" AND stream_id <= ?"
|
|
|
|
)
|
|
|
|
txn.execute(sql, (destination, up_to_stream_id))
|
|
|
|
|
|
|
|
return self.runInteraction(
|
2016-09-06 11:16:20 -06:00
|
|
|
"delete_device_msgs_for_remote",
|
2016-09-06 08:12:13 -06:00
|
|
|
delete_messages_for_remote_destination_txn
|
|
|
|
)
|
2017-01-10 08:04:57 -07:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _background_drop_index_device_inbox(self, progress, batch_size):
|
|
|
|
def reindex_txn(conn):
|
2017-01-10 08:11:27 -07:00
|
|
|
txn = conn.cursor()
|
|
|
|
txn.execute(
|
|
|
|
"DROP INDEX IF EXISTS device_inbox_stream_id"
|
|
|
|
)
|
2017-01-10 08:15:25 -07:00
|
|
|
txn.close()
|
2017-01-10 08:04:57 -07:00
|
|
|
|
|
|
|
yield self.runWithConnection(reindex_txn)
|
|
|
|
|
|
|
|
yield self._end_background_update(self.DEVICE_INBOX_STREAM_ID)
|
|
|
|
|
|
|
|
defer.returnValue(1)
|