Skip to content

Commit d58e17d

Browse files
Added possibility to override the message id creation
1 parent 75d3a4f commit d58e17d

File tree

6 files changed

+60
-7
lines changed

6 files changed

+60
-7
lines changed

CHANGELOG.md

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

66
### ✅ Added
77
- Support for channel own capabilities in the UI
8+
- Added possibility to override the message id creation with `MessageIdBuilder`
89

910
### 🐞 Fixed
1011
- Renaming of a channel in ChannelInfo not persisting extra data

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -499,18 +499,14 @@ extension ChatMessage: Identifiable {
499499
}
500500

501501
var messageId: String {
502-
var statesId = "empty"
503-
if localState != nil {
504-
statesId = uploadingStatesId
505-
}
506-
return baseId + statesId + reactionScoresId + repliesCountId + "\(updatedAt)" + pinStateId
502+
InjectedValues[\.utils].messageIdBuilder.makeMessageId(for: self)
507503
}
508504

509-
private var baseId: String {
505+
var baseId: String {
510506
isDeleted ? "\(id)-deleted" : id
511507
}
512508

513-
private var pinStateId: String {
509+
var pinStateId: String {
514510
pinDetails != nil ? "pinned" : "notPinned"
515511
}
516512

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Copyright © 2022 Stream.io Inc. All rights reserved.
3+
//
4+
5+
import StreamChat
6+
import SwiftUI
7+
8+
public protocol MessageIdBuilder {
9+
10+
/// Creates a message id for the provided message.
11+
func makeMessageId(for message: ChatMessage) -> String
12+
}
13+
14+
public class DefaultMessageIdBuilder: MessageIdBuilder {
15+
16+
public init() { /* Public init. */ }
17+
18+
public func makeMessageId(for message: ChatMessage) -> String {
19+
var statesId = "empty"
20+
if message.localState != nil {
21+
statesId = message.uploadingStatesId
22+
}
23+
return message.baseId + statesId + message.reactionScoresId
24+
+ message.repliesCountId + "\(message.updatedAt)" + message.pinStateId
25+
}
26+
}

Sources/StreamChatSwiftUI/Utils.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class Utils {
2424
public var composerConfig: ComposerConfig
2525
public var shouldSyncChannelControllerOnAppear: (ChatChannelController) -> Bool
2626
public var snapshotCreator: SnapshotCreator
27+
public var messageIdBuilder: MessageIdBuilder
2728

2829
var messageCachingUtils = MessageCachingUtils()
2930
var messageListDateUtils: MessageListDateUtils
@@ -45,6 +46,7 @@ public class Utils {
4546
channelNamer: @escaping ChatChannelNamer = DefaultChatChannelNamer(),
4647
chatUserNamer: ChatUserNamer = DefaultChatUserNamer(),
4748
snapshotCreator: SnapshotCreator = DefaultSnapshotCreator(),
49+
messageIdBuilder: MessageIdBuilder = DefaultMessageIdBuilder(),
4850
shouldSyncChannelControllerOnAppear: @escaping (ChatChannelController) -> Bool = { _ in true }
4951
) {
5052
self.dateFormatter = dateFormatter
@@ -62,6 +64,7 @@ public class Utils {
6264
self.messageListConfig = messageListConfig
6365
self.composerConfig = composerConfig
6466
self.snapshotCreator = snapshotCreator
67+
self.messageIdBuilder = messageIdBuilder
6568
self.shouldSyncChannelControllerOnAppear = shouldSyncChannelControllerOnAppear
6669
messageListDateUtils = MessageListDateUtils(messageListConfig: messageListConfig)
6770
}

StreamChatSwiftUI.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@
315315
84E04796284A444E00BAFA17 /* EventBatcherMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 847BA08227E0B9C600ED20C7 /* EventBatcherMock.swift */; };
316316
84E04797284A444E00BAFA17 /* WebSocketPingControllerMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 847BA08627E0BACB00ED20C7 /* WebSocketPingControllerMock.swift */; };
317317
84E04798284A444E00BAFA17 /* InternetConnectionMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C94D23275794D3007FE2B9 /* InternetConnectionMock.swift */; };
318+
84E4F7CF294C69F300DD4CE3 /* MessageIdBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84E4F7CE294C69F200DD4CE3 /* MessageIdBuilder.swift */; };
318319
84E57C5928103822002213C1 /* TestDataModel2.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 84E57C5328103822002213C1 /* TestDataModel2.xcdatamodeld */; };
319320
84E57C5B28103822002213C1 /* TestDataModel.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 84E57C5728103822002213C1 /* TestDataModel.xcdatamodeld */; };
320321
84E6EC23279AEE6B0017207B /* MessageContainerView_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84E6EC22279AEE6B0017207B /* MessageContainerView_Tests.swift */; };
@@ -720,6 +721,7 @@
720721
84DEC8E72760EABC00172876 /* ChatChannelDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatChannelDataSource.swift; sourceTree = "<group>"; };
721722
84DEC8E92761089A00172876 /* MessageThreadHeaderViewModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageThreadHeaderViewModifier.swift; sourceTree = "<group>"; };
722723
84DEC8EB27611CAE00172876 /* SendInChannelView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendInChannelView.swift; sourceTree = "<group>"; };
724+
84E4F7CE294C69F200DD4CE3 /* MessageIdBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageIdBuilder.swift; sourceTree = "<group>"; };
723725
84E57C46281037B2002213C1 /* AssertJSONEqual.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssertJSONEqual.swift; sourceTree = "<group>"; };
724726
84E57C47281037B2002213C1 /* AssertResult.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssertResult.swift; sourceTree = "<group>"; };
725727
84E57C48281037B2002213C1 /* AssertAsync.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssertAsync.swift; sourceTree = "<group>"; };
@@ -1121,6 +1123,7 @@
11211123
841B2EF3278DB9E500ED619E /* MessageListHelperViews.swift */,
11221124
84B55F692798154C00B99B01 /* MessageListConfig.swift */,
11231125
84BB4C4B2841104700CBE004 /* MessageListDateUtils.swift */,
1126+
84E4F7CE294C69F200DD4CE3 /* MessageIdBuilder.swift */,
11241127
);
11251128
path = MessageList;
11261129
sourceTree = "<group>";
@@ -2009,6 +2012,7 @@
20092012
846D6564279FF0800094B36E /* ReactionUserView.swift in Sources */,
20102013
8465FDB12746A95700AF091E /* ChatChannelNamer.swift in Sources */,
20112014
8465FD9E2746A95700AF091E /* ChatChannelHelpers.swift in Sources */,
2015+
84E4F7CF294C69F300DD4CE3 /* MessageIdBuilder.swift in Sources */,
20122016
);
20132017
runOnlyForDeploymentPostprocessing = 0;
20142018
};

StreamChatSwiftUITests/Tests/ChatChannel/ChatMessageIDs_Tests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,27 @@ class ChatMessageIDs_Tests: XCTestCase {
114114
// Then
115115
XCTAssert(messageId.contains(expectedId))
116116
}
117+
118+
func test_chatMessage_messageBuilder() {
119+
// Given
120+
let id: String = .unique
121+
let reaction = "like"
122+
let expectedId = id + "empty" + "\(reaction)\(3)"
123+
let message = ChatMessage.mock(
124+
id: id,
125+
cid: .unique,
126+
text: "test",
127+
author: .mock(id: .unique),
128+
reactionScores: [
129+
MessageReactionType(rawValue: reaction): 3
130+
]
131+
)
132+
let defaultMessageBuilder = DefaultMessageIdBuilder()
133+
134+
// When
135+
let messageId = defaultMessageBuilder.makeMessageId(for: message)
136+
137+
// Then
138+
XCTAssert(messageId.starts(with: expectedId))
139+
}
117140
}

0 commit comments

Comments
 (0)