Skip to content

Commit 0aff02c

Browse files
Exposed methods for accessing cached user display info
1 parent 8e17c96 commit 0aff02c

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

Sources/StreamChatSwiftUI/Utils/MessageCachingUtils.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ class MessageCachingUtils {
7373
return quoted
7474
}
7575

76+
func userDisplayInfo(with id: String) -> UserDisplayInfo? {
77+
for userInfo in messageAuthors.values {
78+
if userInfo.id == id {
79+
return userInfo
80+
}
81+
}
82+
return nil
83+
}
84+
7685
func clearCache() {
7786
log.debug("Clearing cached message data")
7887
scrollOffset = 0
@@ -126,3 +135,16 @@ public struct UserDisplayInfo {
126135
self.imageURL = imageURL
127136
}
128137
}
138+
139+
extension ChatMessage {
140+
141+
public var authorDisplayInfo: UserDisplayInfo {
142+
let cachingUtils = InjectedValues[\.utils].messageCachingUtils
143+
return cachingUtils.authorInfo(from: self)
144+
}
145+
146+
public func userDisplayInfo(from id: String) -> UserDisplayInfo? {
147+
let cachingUtils = InjectedValues[\.utils].messageCachingUtils
148+
return cachingUtils.userDisplayInfo(with: id)
149+
}
150+
}

StreamChatSwiftUITests/Tests/ChatChannel/MessageCachingUtils_Tests.swift

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@testable import StreamChatSwiftUI
77
import XCTest
88

9-
class MessageCachingUtils_Tests: XCTestCase {
9+
class MessageCachingUtils_Tests: StreamChatTestCase {
1010

1111
let author = ChatUser.mock(
1212
id: "test",
@@ -109,4 +109,69 @@ class MessageCachingUtils_Tests: XCTestCase {
109109
XCTAssert(authorIdInitial == "test")
110110
XCTAssert(authorIdInitial == authorIdAfterClear)
111111
}
112+
113+
func test_messageCachingUtils_userDisplayInfo() {
114+
// Given
115+
let id: String = .unique
116+
let url = URL(string: "https://imageurl.com")
117+
let author = ChatUser.mock(id: id, name: "Martin", imageURL: url)
118+
let message = ChatMessage.mock(
119+
id: .unique,
120+
cid: .unique,
121+
text: "Test message",
122+
author: author
123+
)
124+
let utils = MessageCachingUtils()
125+
126+
// When
127+
let authorInfo = utils.authorInfo(from: message)
128+
let userDisplayInfo = message.authorDisplayInfo
129+
130+
// Then
131+
XCTAssert(authorInfo == userDisplayInfo)
132+
XCTAssert(userDisplayInfo.id == id)
133+
XCTAssert(userDisplayInfo.name == author.name)
134+
XCTAssert(userDisplayInfo.imageURL == url)
135+
}
136+
137+
func test_messageCachingUtils_userDisplayInfoIdExisting() {
138+
// Given
139+
let id: String = .unique
140+
let url = URL(string: "https://imageurl.com")
141+
let author = ChatUser.mock(id: id, name: "Martin", imageURL: url)
142+
let message = ChatMessage.mock(
143+
id: .unique,
144+
cid: .unique,
145+
text: "Test message",
146+
author: author
147+
)
148+
let utils = MessageCachingUtils()
149+
150+
// When
151+
let authorInfo = utils.authorInfo(from: message)
152+
let userDisplayInfo = utils.userDisplayInfo(with: id)
153+
154+
// Then
155+
XCTAssert(userDisplayInfo != nil)
156+
XCTAssert(authorInfo == userDisplayInfo)
157+
XCTAssert(userDisplayInfo!.id == id)
158+
XCTAssert(userDisplayInfo!.name == author.name)
159+
XCTAssert(userDisplayInfo!.imageURL == url)
160+
}
161+
162+
func test_messageCachingUtils_userDisplayInfoIdNonExisting() {
163+
let utils = MessageCachingUtils()
164+
165+
// When
166+
let userDisplayInfo = utils.userDisplayInfo(with: "some id")
167+
168+
// Then
169+
XCTAssert(userDisplayInfo == nil)
170+
}
171+
}
172+
173+
extension UserDisplayInfo: Equatable {
174+
public static func == (lhs: UserDisplayInfo, rhs: UserDisplayInfo) -> Bool {
175+
lhs.id == rhs.id && lhs.name == rhs.name && lhs.imageURL == rhs.imageURL
176+
}
112177
}

0 commit comments

Comments
 (0)