refactor: remove misleading APIs from message_model

This commit is contained in:
Patryk Osmaczko 2023-01-09 12:11:57 +01:00 committed by osmaczko
parent fe4e14ff5b
commit bbc7da7c7d
4 changed files with 49 additions and 101 deletions

View File

@ -21,7 +21,9 @@ logScope:
topics = "messages-module"
const CHAT_IDENTIFIER_MESSAGE_ID = "chat-identifier-message-id"
const CHAT_IDENTIFIER_CLOCK = -2
const FETCH_MORE_MESSAGES_MESSAGE_ID = "fetch-more_messages-message-id"
const FETCH_MORE_MESSAGES_CLOCK = -1
type
Module* = ref object of io_interface.AccessInterface
@ -61,8 +63,8 @@ method isLoaded*(self: Module): bool =
method viewDidLoad*(self: Module) =
if self.controller.getChatDetails().hasMoreMessagesToRequest():
self.view.model().appendItem(self.createFetchMoreMessagesItem())
self.view.model().appendItem(self.createChatIdentifierItem())
self.view.model().insertItemBasedOnClock(self.createFetchMoreMessagesItem())
self.view.model().insertItemBasedOnClock(self.createChatIdentifierItem())
self.moduleLoaded = true
self.delegate.messagesDidLoad()
@ -88,7 +90,7 @@ proc createFetchMoreMessagesItem(self: Module): Item =
messageContainsMentions = false,
seen = true,
timestamp = 0,
clock = 0,
clock = FETCH_MORE_MESSAGES_CLOCK,
ContentType.FetchMoreMessagesButton,
messageType = -1,
contactRequestState = 0,
@ -131,7 +133,7 @@ proc createChatIdentifierItem(self: Module): Item =
messageContainsMentions = false,
seen = true,
timestamp = 0,
clock = 0,
clock = CHAT_IDENTIFIER_CLOCK,
ContentType.ChatIdentifier,
messageType = -1,
contactRequestState = 0,
@ -261,7 +263,7 @@ method newMessagesLoaded*(self: Module, messages: seq[MessageDto], reactions: se
self.view.model().removeItem(FETCH_MORE_MESSAGES_MESSAGE_ID)
self.view.model().removeItem(CHAT_IDENTIFIER_MESSAGE_ID)
# Add new loaded messages
self.view.model().appendItems(viewItems)
self.view.model().insertItemsBasedOnClock(viewItems)
self.view.model().resetNewMessagesMarker()
# check if this loading was caused by the click on a messages from the app search result
@ -508,13 +510,13 @@ method updateChatIdentifier*(self: Module) =
# Delete the old ChatIdentifier message first
self.view.model().removeItem(CHAT_IDENTIFIER_MESSAGE_ID)
# Add new loaded messages
self.view.model().appendItem(self.createChatIdentifierItem())
self.view.model().insertItemBasedOnClock(self.createChatIdentifierItem())
method updateChatFetchMoreMessages*(self: Module) =
self.view.model().removeItem(FETCH_MORE_MESSAGES_MESSAGE_ID)
if (self.controller.getChatDetails().hasMoreMessagesToRequest()):
self.view.model().appendItem(self.createFetchMoreMessagesItem())
self.view.model().insertItemBasedOnClock(self.createFetchMoreMessagesItem())
method getLinkPreviewData*(self: Module, link: string, uuid: string): string =
return self.controller.getLinkPreviewData(link, uuid)

View File

@ -228,7 +228,7 @@ method newPinnedMessagesLoaded*(self: Module, pinnedMessages: seq[PinnedMessageD
if(viewItems.len == 0):
return
self.view.pinnedModel().prependItems(viewItems)
self.view.pinnedModel().insertItemsBasedOnClock(viewItems)
method unpinMessage*(self: Module, messageId: string) =
self.controller.unpinMessage(messageId)
@ -241,7 +241,7 @@ method onPinMessage*(self: Module, messageId: string, actionInitiatedBy: string)
if(not self.buildPinnedMessageItem(messageId, actionInitiatedBy, item)):
return
self.view.pinnedModel().appendItem(item)
self.view.pinnedModel().insertItemBasedOnClock(item)
method getMyChatId*(self: Module): string =
self.controller.getMyChatId()

View File

@ -254,11 +254,6 @@ QtObject:
return i
return self.items.len
proc filterExistingItems(self: Model, items: seq[Item]): seq[Item] =
for item in items:
if(self.findIndexForMessageId(item.id) < 0):
result &= item
proc insertItemBasedOnClock*(self: Model, item: Item) =
if(self.findIndexForMessageId(item.id) != -1):
return
@ -278,54 +273,10 @@ QtObject:
self.updateItemAtIndex(position + 1)
self.countChanged()
proc prependItems*(self: Model, items: seq[Item]) =
let itemsToAppend = self.filterExistingItems(items)
if(itemsToAppend.len == 0):
return
proc insertItemsBasedOnClock*(self: Model, items: seq[Item]) =
for item in items:
self.insertItemBasedOnClock(item)
proc appendItems*(self: Model, items: seq[Item]) =
let itemsToAppend = self.filterExistingItems(items)
if(itemsToAppend.len == 0):
return
for item in items:
self.insertItemBasedOnClock(item)
proc appendItem*(self: Model, item: Item) =
if(self.findIndexForMessageId(item.id) != -1):
return
let parentModelIndex = newQModelIndex()
defer: parentModelIndex.delete
let position = self.items.len
self.beginInsertRows(parentModelIndex, position, position)
self.items.add(item)
self.endInsertRows()
if position > 0:
self.updateItemAtIndex(position - 1)
self.countChanged()
proc prependItem*(self: Model, item: Item) =
if(self.findIndexForMessageId(item.id) != -1):
return
let parentModelIndex = newQModelIndex()
defer: parentModelIndex.delete
self.beginInsertRows(parentModelIndex, 0, 0)
self.items.insert(item, 0)
self.endInsertRows()
if self.items.len > 1:
self.updateItemAtIndex(1)
self.countChanged()
proc replyDeleted*(self: Model, messageIndex: int) {.signal.}
proc updateMessagesWithResponseTo(self: Model, messageId: string) =

View File

@ -41,8 +41,8 @@ proc createTestMessageItem(id: string, clock: int64): Item =
mentioned = false
)
let message0_chatIdentifier = createTestMessageItem("chat-identifier", 0)
let message0_fetchMoreMessages = createTestMessageItem("fetch-more-messages", 0)
let message0_chatIdentifier = createTestMessageItem("chat-identifier", -2)
let message0_fetchMoreMessages = createTestMessageItem("fetch-more-messages", -1)
let message1 = createTestMessageItem("0xa", 1)
let message2 = createTestMessageItem("0xb", 2)
let message3 = createTestMessageItem("0xc", 3)
@ -69,8 +69,7 @@ suite "empty model":
suite "inserting new messages":
setup:
let model = newModel()
model.appendItem(message0_fetchMoreMessages)
model.appendItem(message0_chatIdentifier)
model.insertItemsBasedOnClock(@[message0_fetchMoreMessages, message0_chatIdentifier])
test "insert same message twice":
model.insertItemBasedOnClock(message1)
@ -117,53 +116,50 @@ suite "inserting new messages":
check(model.rowCount() == 7)
checkOrder(model)
# assumption: each append sequence is already sorted
suite "appending new messages":
suite "inserting multiple new messages":
setup:
let model = newModel()
model.appendItem(message0_fetchMoreMessages)
model.appendItem(message0_chatIdentifier)
model.insertItemsBasedOnClock(@[message0_fetchMoreMessages, message0_chatIdentifier])
test "append empty model":
model.appendItems(@[message5,
test "insert to empty model":
model.insertItemsBasedOnClock(@[message5,
message4,
message3,
message2,
message1])
checkOrder(model)
test "append to model with only newer messages":
test "insert to model with only newer messages":
model.insertItemBasedOnClock(message5)
model.insertItemBasedOnClock(message4)
model.appendItems(@[message3,
model.insertItemsBasedOnClock(@[message3,
message2,
message1])
checkOrder(model)
test "append to model with newer and older messages":
test "insert to model with newer and older messages":
model.insertItemBasedOnClock(message5)
model.insertItemBasedOnClock(message1)
model.appendItems(@[message4,
model.insertItemsBasedOnClock(@[message4,
message3,
message2])
checkOrder(model)
test "append to model with newer and older messages and some in between":
test "insert to model with newer and older messages and some in between":
model.insertItemBasedOnClock(message5)
model.insertItemBasedOnClock(message1)
model.insertItemBasedOnClock(message3) # in between
model.appendItems(@[message4,
model.insertItemsBasedOnClock(@[message4,
message2])
checkOrder(model)
suite "new messages marker":
setup:
let model = newModel()
model.appendItem(message0_fetchMoreMessages)
model.appendItem(message0_chatIdentifier)
model.insertItemsBasedOnClock(@[message0_fetchMoreMessages, message0_chatIdentifier])
test "set new messages marker":
model.appendItems(@[message1,
model.insertItemsBasedOnClock(@[message1,
message2])
require(model.items.len == 4)
@ -179,7 +175,7 @@ suite "new messages marker":
check(model.items[4].id == message0_chatIdentifier.id)
test "remove new messages marker":
model.appendItems(@[message1,
model.insertItemsBasedOnClock(@[message1,
message2])
require(model.items.len == 4)
@ -200,8 +196,7 @@ suite "new messages marker":
suite "simulations":
setup:
let model = newModel()
model.appendItem(message0_fetchMoreMessages)
model.appendItem(message0_chatIdentifier)
model.insertItemsBasedOnClock(@[message0_fetchMoreMessages, message0_chatIdentifier])
test "simulate messages loading":
# load first two messages
@ -212,7 +207,7 @@ suite "simulations":
model.removeItem(message0_chatIdentifier.id)
loadedMessages.add(message0_fetchMoreMessages)
loadedMessages.add(message0_chatIdentifier)
model.appendItems(loadedMessages)
model.insertItemsBasedOnClock(loadedMessages)
require(model.items.len == 4)
check(model.items[0].id == message5.id)
@ -239,7 +234,7 @@ suite "simulations":
model.removeItem(message0_chatIdentifier.id)
loadedMessages.add(message0_fetchMoreMessages)
loadedMessages.add(message0_chatIdentifier)
model.appendItems(loadedMessages)
model.insertItemsBasedOnClock(loadedMessages)
require(model.items.len == 7)
check(model.items[0].id == message5.id)
@ -257,7 +252,7 @@ suite "simulations":
model.removeItem(message0_chatIdentifier.id)
loadedMessages.add(message0_fetchMoreMessages)
loadedMessages.add(message0_chatIdentifier)
model.appendItems(loadedMessages)
model.insertItemsBasedOnClock(loadedMessages)
require(model.items.len == 8)
check(model.items[0].id == message5.id)
@ -270,7 +265,7 @@ suite "simulations":
check(model.items[7].id == message0_chatIdentifier.id)
test "simulate chat identifier update":
model.appendItems(@[message5,
model.insertItemsBasedOnClock(@[message5,
message4,
message3,
message2,
@ -283,7 +278,7 @@ suite "simulations":
# update chat identifier
model.removeItem(message0_chatIdentifier.id)
model.appendItem(message0_chatIdentifier)
model.insertItemBasedOnClock(message0_chatIdentifier)
require(model.items.len == 8)
check(model.items[0].id == message5.id)