Skip to content

Commit 1620457

Browse files
authored
Fix accessing messages with invalid index (#663)
1 parent 7b0bd5c commit 1620457

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
99
- Fix message long press taking too much time to show actions [#648](https://github.com/GetStream/stream-chat-swiftui/pull/648)
1010
- Fix rendering link attachment preview with other attachment types [#659](https://github.com/GetStream/stream-chat-swiftui/pull/659)
1111
- Fix not using colors from the palette in some of the poll views [#661](https://github.com/GetStream/stream-chat-swiftui/pull/661)
12+
- Fix a rare crash when handling list change in the `ChatChannelViewModel` [#663](https://github.com/GetStream/stream-chat-swiftui/pull/663)
1213
### 🔄 Changed
1314
- Message composer now uses `.uploadFile` capability when showing attachment picker icon [#646](https://github.com/GetStream/stream-chat-swiftui/pull/646)
1415
- `ChannelInfoView` now uses `.updateChannelMembers` capability to show "Add Users" button [#651](https://github.com/GetStream/stream-chat-swiftui/pull/651)
@@ -59,8 +60,8 @@ _September 12, 2024_
5960

6061
### 🔄 Changed
6162
- Improved subtitle info in pinned messages view [#594](https://github.com/GetStream/stream-chat-swiftui/pull/594)
62-
- The `image(for channel: ChatChannel)` in `ChannelHeaderLoader` is now open [#595](https://github.com/GetStream/stream-chat-swiftui/pull/595)
63-
- FlagMessage Action is now only shown if the user has a permission to perform the action [#599](https://github.com/GetStream/stream-chat-swiftui/pull/599)
63+
- The `image(for channel: ChatChannel)` in `ChannelHeaderLoader` is now open [#595](https://github.com/GetStream/stream-chat-swiftui/pull/595)
64+
- FlagMessage Action is now only shown if the user has a permission to perform the action [#599](https://github.com/GetStream/stream-chat-swiftui/pull/599)
6465

6566
### 🐞 Fixed
6667
- Typing users did not update reliably in the message list [#591](https://github.com/GetStream/stream-chat-swiftui/pull/591)

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -709,19 +709,18 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
709709
.remove(_, index: _):
710710
return true
711711
case let .update(message, index: index):
712-
let animateReactions = message.reactionScoresId != messages[index.row].reactionScoresId
712+
guard index.row >= messages.startIndex, index.row < messages.endIndex else { continue }
713+
let existingDisplayedMessage = messages[index.row]
714+
let animateReactions = message.reactionScoresId != existingDisplayedMessage.reactionScoresId
713715
&& utils.messageListConfig.messageDisplayOptions.shouldAnimateReactions
714-
if index.row < messages.count,
715-
message.messageId != messages[index.row].messageId
716+
if animateReactions,
717+
message.messageId != existingDisplayedMessage.messageId
716718
|| message.type == .ephemeral
717719
|| !message.linkAttachments.isEmpty {
718-
if index.row < messages.count
719-
&& animateReactions {
720-
animateChanges = message.linkAttachments.isEmpty
721-
}
720+
animateChanges = message.linkAttachments.isEmpty
722721
}
723-
default:
724-
break
722+
case .move(_, fromIndex: _, toIndex: _):
723+
continue
725724
}
726725
}
727726

StreamChatSwiftUITests/Tests/ChatChannel/ChatChannelViewModel_Tests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,30 @@ class ChatChannelViewModel_Tests: StreamChatTestCase {
489489
// Then
490490
XCTAssert(shouldJump == false)
491491
}
492+
493+
func test_chatChannelVM_crashWhenIndexAccess() {
494+
// Given
495+
let message1 = ChatMessage.mock()
496+
let message2 = ChatMessage.mock()
497+
let message3 = ChatMessage.mock()
498+
let channelController = makeChannelController(messages: [message1, message2])
499+
let viewModel = ChatChannelViewModel(channelController: channelController)
500+
let newMessages = LazyCachedMapCollection(elements: [message1, message2, message3])
501+
502+
// When
503+
viewModel.dataSource(
504+
channelDataSource: ChatChannelDataSource(controller: channelController),
505+
didUpdateMessages: newMessages,
506+
changes: [
507+
.insert(message3, index: IndexPath(row: 2, section: 0)),
508+
.update(message3, index: IndexPath(row: 2, section: 0)),
509+
.update(message3, index: IndexPath(row: 3, section: 0)) // intentionally invalid path
510+
]
511+
)
512+
513+
// Then
514+
XCTAssertEqual(3, viewModel.messages.count)
515+
}
492516

493517
// MARK: - private
494518

0 commit comments

Comments
 (0)