From 14a76b710b1121f66e48fb8f59d9835f4be473d3 Mon Sep 17 00:00:00 2001 From: miruka Date: Sun, 14 Apr 2019 14:49:26 -0400 Subject: [PATCH] Prevent duplicate events due to multiple accounts --- harmonyqml/backend/signal_manager.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/harmonyqml/backend/signal_manager.py b/harmonyqml/backend/signal_manager.py index 1f3e846d..4c8b8591 100644 --- a/harmonyqml/backend/signal_manager.py +++ b/harmonyqml/backend/signal_manager.py @@ -1,7 +1,8 @@ # Copyright 2019 miruka # This file is part of harmonyqml, licensed under GPLv3. -from typing import Any, Dict, Optional +from threading import Lock +from typing import Any, Deque, Dict, Optional from PyQt5.QtCore import QDateTime, QObject, pyqtBoundSignal @@ -11,10 +12,14 @@ from .model.items import Room, RoomEvent, User class SignalManager(QObject): + _duplicate_check_lock: Lock = Lock() + def __init__(self, backend: Backend) -> None: super().__init__(parent=backend) self.backend = backend + self.last_room_events: Deque[str] = Deque(maxlen=1000) + cm = self.backend.clientManager cm.clientAdded.connect(self.onClientAdded) cm.clientDeleted.connect(self.onClientDeleted) @@ -79,6 +84,14 @@ class SignalManager(QObject): def onRoomEventReceived( self, _: Client, room_id: str, etype: str, edict: Dict[str, Any] ) -> None: + + # Prevent duplicate events in models due to multiple accounts + with self._duplicate_check_lock: + if edict["event_id"] in self.last_room_events: + return + + self.last_room_events.appendleft(edict["event_id"]) + model = self.backend.models.roomEvents[room_id] date_time = QDateTime.fromMSecsSinceEpoch(edict["server_timestamp"]) new_event = RoomEvent(type=etype, date_time=date_time, dict=edict)