Bring back room movements on new events

This commit is contained in:
miruka 2019-05-02 16:10:41 -04:00
parent 6bc6998fde
commit e124dcbff7
2 changed files with 60 additions and 18 deletions

View File

@ -43,6 +43,17 @@ class ListModel(QAbstractListModel):
return "%s(%r)" % (type(self).__name__, self._data)
def __contains__(self, index: Index) -> bool:
if isinstance(index, str):
try:
self.indexWhere(self.mainKey, index)
return True
except ValueError:
return False
return index in self._data
def __getitem__(self, index: Index) -> ListItem:
return self.get(index)
@ -260,27 +271,32 @@ class ListModel(QAbstractListModel):
@pyqtSlot(int, int)
@pyqtSlot(int, int, int)
def move(self, from_: int, to: int, n: int = 1) -> None:
@pyqtSlot(str, int)
@pyqtSlot(str, int, int)
def move(self, from_: Index, to: int, n: int = 1) -> None:
# pylint: disable=invalid-name
qlast = from_ + n - 1
i_from: int = self.indexWhere(self.mainKey, from_) \
if isinstance(from_, str) else from_
if (n <= 0) or (from_ == to) or (qlast == to) or \
qlast = i_from + n - 1
if (n <= 0) or (i_from == to) or (qlast == to) or \
not (len(self) > qlast >= 0) or \
not len(self) >= to >= 0:
return
qidx = QModelIndex()
qto = min(len(self), to + n if to > from_ else to)
# print(f"self.beginMoveRows(qidx, {from_}, {qlast}, qidx, {qto})")
valid = self.beginMoveRows(qidx, from_, qlast, qidx, qto)
qto = min(len(self), to + n if to > i_from else to)
# print(f"self.beginMoveRows(qidx, {i_from}, {qlast}, qidx, {qto})")
valid = self.beginMoveRows(qidx, i_from, qlast, qidx, qto)
if not valid:
logging.warning("Invalid move operation - %r", locals())
return
last = from_ + n
cut = self._data[from_:last]
del self._data[from_:last]
last = i_from + n
cut = self._data[i_from:last]
del self._data[i_from:last]
self._data[to:to] = cut
self.endMoveRows()

View File

@ -141,15 +141,39 @@ class SignalManager(QObject):
self.backend.past_tokens[room_id] = token
def _move_room(self, account_id: str, room_id: str, new_event: RoomEvent
) -> None:
# Find in which category our room is
for categ in self.backend.accounts[account_id].roomCategories:
if room_id not in categ.rooms:
continue
# Found the category, now find before which room we must move to
for index, room in enumerate(categ.rooms):
if not self.backend.roomEvents[room.roomId]:
# That other room has no events, move before it
categ.rooms.move(room_id, index)
return
other_room_last_event = self.backend.roomEvents[room.roomId][0]
if new_event.dateTime > other_room_last_event.dateTime:
# Our last event is newer than that other room, move before
categ.rooms.move(room_id, max(0, index - 1))
return
return
def onRoomEventReceived(self,
_: Client,
client: Client,
room_id: str,
etype: str,
edict: Dict[str, Any]) -> None:
def process() -> None:
def process() -> Optional[RoomEvent]:
# Prevent duplicate events in models due to multiple accounts
if edict["event_id"] in self.last_room_events:
return
return None
self.last_room_events.appendleft(edict["event_id"])
@ -166,7 +190,7 @@ class SignalManager(QObject):
)
if event_is_our_profile_changed:
return
return None
if etype == "RoomCreateEvent":
self.backend.fully_loaded_rooms.add(room_id)
@ -194,7 +218,7 @@ class SignalManager(QObject):
if update_at is not None:
model.update(update_at, new_event)
self._events_in_transfer -= 1
return
return new_event
for i, event in enumerate(model):
if event.isLocalEcho:
@ -203,13 +227,15 @@ class SignalManager(QObject):
# Model is sorted from newest to oldest message
if new_event.dateTime > event.dateTime:
model.insert(i, new_event)
return
return new_event
model.append(new_event)
return new_event
with self._lock:
process()
# self._move_room(client.userId, room_id)
new_event = process()
if new_event:
self._move_room(client.userId, room_id, new_event)
def onRoomTypingUsersUpdated(self,
@ -246,7 +272,7 @@ class SignalManager(QObject):
model.insert(0, event)
self._events_in_transfer += 1
# self._move_room(client.userId, room_id)
self._move_room(client.userId, room_id, event)
def onRoomAboutToBeForgotten(self, client: Client, room_id: str) -> None: