Skip to content

Commit 143696d

Browse files
Fix updating of mentioned users when sending a message (#582)
1 parent 7f16cbf commit 143696d

File tree

2 files changed

+63
-53
lines changed

2 files changed

+63
-53
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
55

66
### 🐞 Fixed
77
- Fix markdown links with query parameters [#581](https://github.com/GetStream/stream-chat-swiftui/pull/581)
8+
- Limitation: markdown link that includes parameters without protocol prefix is not handled at the moment.
9+
- Example: [text](link.com?a=b) will not be presented as markdown, while [text](https://link.com?a=b) will be.
10+
- Fix updating of mentioned users when sending a message [#582](https://github.com/GetStream/stream-chat-swiftui/pull/582)
811

912
# [4.61.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.61.0)
1013
_July 31, 2024_

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageView.swift

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -289,65 +289,72 @@ public struct LinkDetectionTextView: View {
289289
.font(fonts.body)
290290
.tint(tintColor)
291291
.onAppear {
292-
guard utils.messageListConfig.localLinkDetectionEnabled else { return }
293-
var attributes: [NSAttributedString.Key: Any] = [
294-
.foregroundColor: textColor(for: message),
295-
.font: fonts.body
296-
]
297-
298-
let additional = utils.messageListConfig.messageDisplayOptions.messageLinkDisplayResolver(message)
299-
for (key, value) in additional {
300-
if key == .foregroundColor, let value = value as? UIColor {
301-
tintColor = Color(value)
302-
} else {
303-
attributes[key] = value
304-
}
292+
detectLinks(for: message)
293+
}
294+
.onChange(of: message, perform: { updated in
295+
detectLinks(for: updated)
296+
})
297+
}
298+
299+
func detectLinks(for message: ChatMessage) {
300+
guard utils.messageListConfig.localLinkDetectionEnabled else { return }
301+
var attributes: [NSAttributedString.Key: Any] = [
302+
.foregroundColor: textColor(for: message),
303+
.font: fonts.body
304+
]
305+
306+
let additional = utils.messageListConfig.messageDisplayOptions.messageLinkDisplayResolver(message)
307+
for (key, value) in additional {
308+
if key == .foregroundColor, let value = value as? UIColor {
309+
tintColor = Color(value)
310+
} else {
311+
attributes[key] = value
305312
}
306-
307-
let attributedText = NSMutableAttributedString(
308-
string: message.adjustedText,
309-
attributes: attributes
310-
)
311-
let attributedTextString = attributedText.string
312-
var containsLinks = false
313+
}
314+
315+
let attributedText = NSMutableAttributedString(
316+
string: message.adjustedText,
317+
attributes: attributes
318+
)
319+
let attributedTextString = attributedText.string
320+
var containsLinks = false
313321

314-
message.mentionedUsers.forEach { user in
315-
containsLinks = true
316-
let mention = "@\(user.name ?? user.id)"
317-
attributedTextString
318-
.ranges(of: mention, options: [.caseInsensitive])
319-
.map { NSRange($0, in: attributedTextString) }
320-
.forEach {
321-
let messageId = message.messageId.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)
322-
if let messageId {
323-
attributedText.addAttribute(.link, value: "getstream://mention/\(messageId)/\(user.id)", range: $0)
324-
}
322+
message.mentionedUsers.forEach { user in
323+
containsLinks = true
324+
let mention = "@\(user.name ?? user.id)"
325+
attributedTextString
326+
.ranges(of: mention, options: [.caseInsensitive])
327+
.map { NSRange($0, in: attributedTextString) }
328+
.forEach {
329+
let messageId = message.messageId.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)
330+
if let messageId {
331+
attributedText.addAttribute(.link, value: "getstream://mention/\(messageId)/\(user.id)", range: $0)
325332
}
326-
}
327-
328-
let range = NSRange(location: 0, length: message.adjustedText.utf16.count)
329-
linkDetector.links(in: message.adjustedText).forEach { textLink in
330-
let escapedOriginalText = NSRegularExpression.escapedPattern(for: textLink.originalText)
331-
let pattern = "\\[([^\\]]+)\\]\\(\(escapedOriginalText)\\)"
332-
if let regex = try? NSRegularExpression(pattern: pattern) {
333-
containsLinks = (regex.firstMatch(
334-
in: message.adjustedText,
335-
options: [],
336-
range: range
337-
) == nil) || !markdownEnabled
338-
} else {
339-
containsLinks = true
340-
}
341-
342-
if !message.adjustedText.contains("](\(textLink.originalText))") {
343-
containsLinks = true
344333
}
345-
attributedText.addAttribute(.link, value: textLink.url, range: textLink.range)
334+
}
335+
336+
let range = NSRange(location: 0, length: message.adjustedText.utf16.count)
337+
linkDetector.links(in: message.adjustedText).forEach { textLink in
338+
let escapedOriginalText = NSRegularExpression.escapedPattern(for: textLink.originalText)
339+
let pattern = "\\[([^\\]]+)\\]\\(\(escapedOriginalText)\\)"
340+
if let regex = try? NSRegularExpression(pattern: pattern) {
341+
containsLinks = (regex.firstMatch(
342+
in: message.adjustedText,
343+
options: [],
344+
range: range
345+
) == nil) || !markdownEnabled
346+
} else {
347+
containsLinks = true
346348
}
347-
348-
if containsLinks {
349-
self.displayedText = AttributedString(attributedText)
349+
350+
if !message.adjustedText.contains("](\(textLink.originalText))") {
351+
containsLinks = true
350352
}
353+
attributedText.addAttribute(.link, value: textLink.url, range: textLink.range)
354+
}
355+
356+
if containsLinks {
357+
displayedText = AttributedString(attributedText)
351358
}
352359
}
353360
}

0 commit comments

Comments
 (0)