Skip to content

Commit fa94428

Browse files
authored
Disable caching for author in message list because accessing author does not access DB anymore (#540)
1 parent 7af5c68 commit fa94428

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1010
- Smoother and more performant view updates in channel and message lists [#522](https://github.com/GetStream/stream-chat-swiftui/pull/522)
1111
- Fix scrolling location when jumping to a message not in the currently loaded message list [#533](https://github.com/GetStream/stream-chat-swiftui/pull/533)
1212
- Fix display of the most votes icon in Polls [#538](https://github.com/GetStream/stream-chat-swiftui/pull/538)
13+
- Fix message author information not reflecting the latest state [#540](https://github.com/GetStream/stream-chat-swiftui/pull/540)
1314

1415
# [4.58.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.58.0)
1516
_June 27, 2024_

Sources/StreamChatSwiftUI/Utils/MessageCachingUtils.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class MessageCachingUtils {
1212

1313
private var messageAuthorMapping = [String: String]()
1414
private var messageAuthors = [String: UserDisplayInfo]()
15-
private var messageAttachments = [String: Bool]()
1615
private var checkedMessageIds = Set<String>()
1716
private var quotedMessageMapping = [String: ChatMessage]()
1817

@@ -101,14 +100,23 @@ class MessageCachingUtils {
101100
messageThreadShown = false
102101
messageAuthorMapping = [String: String]()
103102
messageAuthors = [String: UserDisplayInfo]()
104-
messageAttachments = [String: Bool]()
105103
checkedMessageIds = Set<String>()
106104
quotedMessageMapping = [String: ChatMessage]()
107105
}
108106

109107
// MARK: - private
110108

111109
private func userDisplayInfo(for message: ChatMessage) -> UserDisplayInfo? {
110+
if StreamRuntimeCheck._isDatabaseObserverItemReusingEnabled {
111+
let user = message.author
112+
return UserDisplayInfo(
113+
id: user.id,
114+
name: user.name ?? user.id,
115+
imageURL: user.imageURL,
116+
role: user.userRole
117+
)
118+
}
119+
112120
if let userId = messageAuthorMapping[message.id],
113121
let userDisplayInfo = messageAuthors[userId] {
114122
return userDisplayInfo
@@ -130,12 +138,6 @@ class MessageCachingUtils {
130138

131139
return userDisplayInfo
132140
}
133-
134-
private func checkAttachments(for message: ChatMessage) -> Bool {
135-
let hasAttachments = !message.attachmentCounts.isEmpty
136-
messageAttachments[message.id] = hasAttachments
137-
return hasAttachments
138-
}
139141
}
140142

141143
/// Contains display information for the user.

StreamChatSwiftUITests/Tests/ChatChannel/MessageCachingUtils_Tests.swift

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,49 @@ class MessageCachingUtils_Tests: StreamChatTestCase {
179179
// Then
180180
XCTAssert(userDisplayInfo == nil)
181181
}
182+
183+
func test_messageCachingUtils_userDisplayInfoWithoutCaching() {
184+
// Given
185+
StreamRuntimeCheck._isDatabaseObserverItemReusingEnabled = true
186+
let utils = MessageCachingUtils()
187+
let authorId: String = .unique
188+
let messageId: MessageId = .unique
189+
let cid: ChannelId = .unique
190+
let url1 = URL(string: "https://imageurl.com")
191+
let author1 = ChatUser.mock(id: authorId, name: "Martin", imageURL: url1)
192+
let message1 = ChatMessage.mock(
193+
id: messageId,
194+
cid: cid,
195+
text: "Test message",
196+
author: author1
197+
)
198+
let url2 = URL(string: "https://anotherimageurl.com")
199+
let author2 = ChatUser.mock(id: authorId, name: "Toomas", imageURL: url2)
200+
let message2 = ChatMessage.mock(
201+
id: messageId,
202+
cid: cid,
203+
text: "Test message",
204+
author: author2
205+
)
206+
207+
// When
208+
let authorInfo1 = utils.authorInfo(from: message1)
209+
let authorInfo2 = utils.authorInfo(from: message2)
210+
211+
// Then
212+
// Accessing the same message returns updated author information
213+
XCTAssertEqual("Martin", authorInfo1.name)
214+
XCTAssertEqual(url1, authorInfo1.imageURL)
215+
XCTAssertEqual("Toomas", authorInfo2.name)
216+
XCTAssertEqual(url2, authorInfo2.imageURL)
217+
}
182218
}
183219

184220
extension UserDisplayInfo: Equatable {
185221
public static func == (lhs: UserDisplayInfo, rhs: UserDisplayInfo) -> Bool {
186-
lhs.id == rhs.id && lhs.name == rhs.name && lhs.imageURL == rhs.imageURL
222+
lhs.id == rhs.id &&
223+
lhs.name == rhs.name &&
224+
lhs.imageURL == rhs.imageURL &&
225+
lhs.role == rhs.role
187226
}
188227
}

0 commit comments

Comments
 (0)