Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Handle RRs which arrive before their events
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed Jun 1, 2018
1 parent 857e6fd commit 9f797a2
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions synapse/storage/receipts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# 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.
from synapse.api.errors import NotFoundError

from ._base import SQLBaseStore
from .util.id_generators import StreamIdGenerator
from synapse.util.caches.descriptors import cachedInlineCallbacks, cachedList, cached
Expand Down Expand Up @@ -340,32 +340,26 @@ def insert_linearized_receipt_txn(self, txn, room_id, receipt_type,
allow_none=True
)

if not res:
raise NotFoundError(
"Cannot set read receipt on unknown event %s" % (
event_id,
),
)

stream_ordering = int(res["stream_ordering"])
stream_ordering = int(res["stream_ordering"]) if res else None

# We don't want to clobber receipts for more recent events, so we
# have to compare orderings of existing receipts
sql = (
"SELECT stream_ordering, event_id FROM events"
" INNER JOIN receipts_linearized as r USING (event_id, room_id)"
" WHERE r.room_id = ? AND r.receipt_type = ? AND r.user_id = ?"
)
txn.execute(sql, (room_id, receipt_type, user_id))

for so, eid in txn:
if int(so) >= stream_ordering:
logger.debug(
"Ignoring new receipt for %s in favour of existing "
"one for later event %s",
event_id, eid,
)
return False
if stream_ordering is not None:
sql = (
"SELECT stream_ordering, event_id FROM events"
" INNER JOIN receipts_linearized as r USING (event_id, room_id)"
" WHERE r.room_id = ? AND r.receipt_type = ? AND r.user_id = ?"
)
txn.execute(sql, (room_id, receipt_type, user_id))

for so, eid in txn:
if int(so) >= stream_ordering:
logger.debug(
"Ignoring new receipt for %s in favour of existing "
"one for later event %s",
event_id, eid,
)
return False

txn.call_after(
self.get_receipts_for_room.invalidate, (room_id, receipt_type)
Expand Down Expand Up @@ -413,7 +407,7 @@ def insert_linearized_receipt_txn(self, txn, room_id, receipt_type,
}
)

if receipt_type == "m.read":
if receipt_type == "m.read" and stream_ordering is not None:
self._remove_old_push_actions_before_txn(
txn,
room_id=room_id,
Expand Down

0 comments on commit 9f797a2

Please sign in to comment.