@@ -170,6 +170,26 @@ func deleteMessageSoft() {
170170 // snippet.end
171171}
172172
173+ // MARK: - Restore Message
174+
175+ func restoreMessage( ) {
176+ // snippet.messages.restore
177+ // Assumes a "ChatImpl" reference named "chat"
178+ Task {
179+ if let channel = try await chat. getChannel ( channelId: " support " ) {
180+ if let message = try await channel. getMessage ( timetoken: 16200000000000001 ) {
181+ let restoredMessage = try await message. restore ( )
182+ debugPrint ( " Message restored successfully: \( restoredMessage. text) " )
183+ } else {
184+ debugPrint ( " Message not found " )
185+ }
186+ } else {
187+ debugPrint ( " Channel not found " )
188+ }
189+ }
190+ // snippet.end
191+ }
192+
173193// MARK: - Edit Message
174194
175195func editMessage( ) {
@@ -931,21 +951,78 @@ func sendMessageDraft() {
931951// MARK: - Add Link
932952
933953func addLink( ) {
934- // snippet.messages.addLink
954+ // snippet.messages.links.add
935955 // Assumes a "ChannelImpl" reference named "channel"
956+ // Create a message draft.
936957 let messageDraft = channel. createMessageDraft ( isTypingIndicatorTriggered: channel. type != . public)
958+ // Update the text of the message draft
937959 messageDraft. update ( text: " Hello Alex! I have sent you this link on the #offtopic channel. " )
960+ // Add a URL mention to the word "link"
938961 messageDraft. addMention ( offset: 33 , length: 4 , target: MentionTarget . url ( url: " https://example.com " ) )
962+
963+ // Additional logic can be implemented as needed
964+ // For example, sending the draft or adding listeners
965+ // snippet.end
966+ }
967+
968+ // MARK: - Remove Link
969+
970+ func removeLink( ) {
971+ // snippet.messages.links.remove
972+ // Assumes a "MessageDraftImpl" reference named "messageDraft"
973+
974+ // Assume the message reads: "Hello Alex! I have sent you this link on the #offtopic channel."
975+ // Remove the link mention
976+ messageDraft. removeMention ( offset: 33 )
977+ // snippet.end
978+ }
979+
980+ // MARK: - Get Link Suggestions
981+
982+ func getLinkSuggestions( ) {
983+ // snippet.messages.links.suggestions
984+ // Define the listener conforming to MessageDraftChangeListener protocol.
985+ // You can also use our ClosureMessageDraftChangeListener class to reduce the need for your custom types to implement the MessageDraftChangeListener protocol
986+ class LinkSuggestionListener : MessageDraftChangeListener {
987+ func onChange( messageElements: [ MessageElement ] , suggestedMentions: any FutureObject < [ SuggestedMention ] > ) {
988+ // Update UI with message elements
989+ // This function is your own function for updating UI
990+ updateUI ( with: messageElements)
991+ // Asynchronously process suggested URL mentions
992+ suggestedMentions. async { result in
993+ switch result {
994+ case . success( let mentions) :
995+ // This is your own function for updating URL suggestions
996+ updateLinkSuggestions ( with: mentions)
997+ case . failure( let error) :
998+ print ( " Error retrieving URL suggestions: \( error) " )
999+ }
1000+ }
1001+ }
1002+ }
1003+
1004+ // Create a message draft.
1005+ // Assumes a "ChannelImpl" reference named "channel"
1006+ let messageDraft = channel. createMessageDraft ( isTypingIndicatorTriggered: channel. type != . public)
1007+ // Instantiate the listener
1008+ let listener = LinkSuggestionListener ( )
1009+ // Add the listener to the message draft
1010+ messageDraft. addChangeListener ( listener)
1011+
1012+ func updateUI( with: [ MessageElement ] ) { }
1013+ func updateLinkSuggestions( with: [ SuggestedMention ] ) { }
9391014 // snippet.end
9401015}
9411016
9421017// MARK: - Get Text Links
9431018
9441019func getTextLinks( ) {
945- // snippet.messages.getTextLinks
1020+ // snippet.messages.links.get
9461021 // Assumes a "MessageImpl" reference named "message"
1022+ // Retrieve the message elements
9471023 let messageElements = message. getMessageElements ( )
948-
1024+
1025+ // Filter the message elements to get only text links
9491026 let textLinks = messageElements. compactMap {
9501027 switch $0 {
9511028 case let . link( text, target) :
@@ -958,7 +1035,8 @@ func getTextLinks() {
9581035 return nil
9591036 }
9601037 }
961-
1038+
1039+ // Print the text links found, if any
9621040 if !textLinks. isEmpty {
9631041 print ( " The message contains the following text links: " )
9641042 textLinks. forEach { link in
@@ -970,6 +1048,31 @@ func getTextLinks() {
9701048 // snippet.end
9711049}
9721050
1051+ // MARK: - Get Text Links (Deprecated)
1052+
1053+ func getTextLinksDeprecated( ) {
1054+ // snippet.messages.links.getDeprecated
1055+ // Assumes a "ChatImpl" reference named "chat"
1056+ Task {
1057+ if let channel = try await chat. getChannel ( channelId: " your-channel " ) {
1058+ if let message = try await channel. getMessage ( timetoken: 16200000000000000 ) {
1059+ if let textLinks = message. textLinks, !textLinks. isEmpty {
1060+ for textLink in textLinks {
1061+ debugPrint ( " Link: \( textLink. link) , Start Index: \( textLink. startIndex) , End Index: \( textLink. endIndex) " )
1062+ }
1063+ } else {
1064+ debugPrint ( " The message does not contain any text links. " )
1065+ }
1066+ } else {
1067+ debugPrint ( " Message does not exist " )
1068+ }
1069+ } else {
1070+ debugPrint ( " Channel not found " )
1071+ }
1072+ }
1073+ // snippet.end
1074+ }
1075+
9731076// MARK: - Stream Thread Message Updates (AsyncStream)
9741077
9751078func streamThreadMessageUpdatesOnAsyncStream( ) {
@@ -1091,3 +1194,103 @@ func getUnreadMessagesCountDeprecated() {
10911194 }
10921195 // snippet.end
10931196}
1197+
1198+ // MARK: - Report Message
1199+
1200+ func reportMessage( ) {
1201+ // snippet.messages.report
1202+ // Assumes a "ChatImpl" reference named "chat"
1203+ Task {
1204+ if let channel = try await chat. getChannel ( channelId: " support " ) {
1205+ if let message = try await channel. getHistory ( count: 1 ) . messages. first {
1206+ let timetoken = try await message. report ( reason: " Offensive Content " )
1207+ debugPrint ( " Reported message successfully: \( timetoken) " )
1208+ } else {
1209+ debugPrint ( " No messages found in the \" support \" channel " )
1210+ }
1211+ } else {
1212+ debugPrint ( " Channel not found " )
1213+ }
1214+ }
1215+ // snippet.end
1216+ }
1217+
1218+ // MARK: - Get Message Reports History
1219+
1220+ func getMessageReportsHistory( ) {
1221+ // snippet.messages.getReportsHistory
1222+ // Define timetokens for the message history period
1223+ let startTimetoken : Timetoken = 1725100800000 // July 1, 2024, 00:00:00 UTC
1224+ let endTimetoken : Timetoken = 1726780799000 // July 21, 2024, 23:59:59 UTC
1225+
1226+ // Assumes a "ChatImpl" reference named "chat"
1227+ Task {
1228+ if let channel = try await chat. getChannel ( channelId: " support " ) {
1229+ let historyResponse = try await channel. getMessageReportsHistory (
1230+ startTimetoken: startTimetoken,
1231+ endTimetoken: endTimetoken,
1232+ count: 25
1233+ )
1234+ historyResponse. events. forEach { eventWrapper in
1235+ print ( " Payload: \( eventWrapper. event. payload) " )
1236+ }
1237+ }
1238+ }
1239+ // snippet.end
1240+ }
1241+
1242+ // MARK: - Stream Message Reports
1243+
1244+ func streamMessageReports( ) {
1245+ // snippet.messages.streamReports
1246+ // Assumes a "ChatImpl" reference named "chat"
1247+ Task {
1248+ if let channel = try await chat. getChannel ( channelId: " support " ) {
1249+ for await event in channel. streamMessageReports ( ) {
1250+ // Access the report details from the event's payload
1251+ let reportPayload = event. event. payload
1252+ let reportReason = reportPayload. reason
1253+ // Print the notification
1254+ if reportReason. lowercased ( ) == " offensive " {
1255+ print ( " Notification: An offensive message was reported on the 'support' channel by user \( event. event. userId) . Reason: \( reportReason) " )
1256+ }
1257+ }
1258+ } else {
1259+ debugPrint ( " Channel not found " )
1260+ }
1261+ }
1262+ // snippet.end
1263+ }
1264+
1265+ // MARK: - Stream Read Receipts (AsyncStream)
1266+
1267+ func streamReadReceiptsAsyncStream( ) {
1268+ // snippet.messages.readReceipts.asyncStream
1269+ // Assumes a "ChatImpl" reference named "chat"
1270+ Task {
1271+ if let channel = try await chat. getChannel ( channelId: " support " ) {
1272+ for await readReceipt in channel. streamReadReceipts ( ) {
1273+ readReceipt. forEach { ( messageTimetoken, users) in
1274+ debugPrint ( " Message timetoken: \( messageTimetoken) was read by users: \( users) " )
1275+ }
1276+ }
1277+ } else {
1278+ debugPrint ( " Channel not found " )
1279+ }
1280+ }
1281+ // snippet.end
1282+ }
1283+
1284+ // MARK: - Stream Read Receipts (Closure)
1285+
1286+ func streamReadReceiptsClosure( ) {
1287+ // snippet.messages.readReceipts.closure
1288+ // Assumes a "ChannelImpl" reference named "channel"
1289+ autoCloseable = channel. streamReadReceipts { receipts in
1290+ print ( " Read Receipts Received: " )
1291+ receipts. forEach { ( messageTimetoken, users) in
1292+ print ( " Message Timetoken: \( messageTimetoken) was read by users: \( users) " )
1293+ }
1294+ }
1295+ // snippet.end
1296+ }
0 commit comments