Skip to content

Implementing Soft Delete Message #3525

@ikaru19

Description

@ikaru19

What did you do?

I want to implement soft-delete, move the text data to extrada["original_message"] and also for attachment to extradata["original_attachment"], so our app moderator can see the message on moderation tool.
Here is my code to implement the soft-delete on action.

func softDeleteActionItem() -> ChatMessageActionItem {
        SoftDeleteMessage(
            action: { [weak self] _ in
                guard let self = self,
                      let message = self.messageController.message else {
                    return
                }
                
                self.alertsRouter.showMessageDeletionConfirmationAlert { confirmed in
                    guard confirmed else { return }
                    
                    var newExtraData = message.extraData
                    newExtraData["original_message"] = .string(message.text)
                    
                    // Initialize an empty array to store decoded attachments
                    var decodedAttachments: [RawJSON] = []

                    
                    for attachment in message.allAttachments {
                        print("Attachment ID: \(attachment.id)")
                        print("Attachment Type: \(attachment.type.rawValue)")

                        do {
                            // Try encoding the payload into JSON data
                            let payloadData = try JSONEncoder().encode(attachment.payload)
                            
                            // Decode the JSON data into a human-readable format
                            if let jsonObject = try? JSONSerialization.jsonObject(with: payloadData, options: .allowFragments) {
                                let jsonString = String(describing: jsonObject)
                                print("PayLoad String: \(jsonString)")
                                if  let decodedData = Data(base64Encoded: jsonString),
                                    let jsonObject = try? JSONSerialization.jsonObject(with: decodedData, options: []),
                                    let dictionary = jsonObject as? [String: Any] {
                                    print("Decoded JSON Object: \(jsonObject)")
                                    
                                     // Convert to JSON string for printing
                                    if let rawJSONData = try? JSONSerialization.data(withJSONObject: dictionary, options: []),
                                       let rawJSON = try? JSONDecoder().decode(RawJSON.self, from: rawJSONData) {
                                        decodedAttachments.append(rawJSON)
                                    }
                                } else {
                                    print("Decoded data is not valid JSON.")
                                }
                            } else {
                                print("Payload could not be converted to a JSON object")
                            }
                        } catch {
                            print("Failed to encode payload: \(error)")
                        }
                    }
                    

                    debugPrint("[KV] deleted_attachment \(decodedAttachments)")
                    // Store the decoded attachments in newExtraData
                    newExtraData["original_attachment"] = .array(decodedAttachments)
                    
                    self.messageController.editMessage(
                        text: message.text,
                        extraData: newExtraData
                    ) { error in
                        if let error = error {
                            // Handle the error
                            print("Failed to edit the message: \(error.localizedDescription)")
                            self.finishAction = .error(message: "Failed to delete the message")
                            self.delegate?.chatMessageActionsVCDidFinish(self)
                        } else {
                            // Handle the success
                            print("Message edited successfully!")
                            self.messageController.deleteMessage { _ in
                                self.finishAction = .none
                                self.delegate?.chatMessageActionsVCDidFinish(self)
                            }
                        }
                    }
                }
            },
            appearance: appearance
        )
    }

What did you expect to happen?

So the message still can delete normally without any changing issue, but i can see the message trough the extra data

What happened instead?

If i only want to hold the original_message it work like intended, but when i try to store the attachment to original_attachment , the extradata i store is gone.

GetStream Environment

**GetStream Chat version:4.62.0
GetStream Chat frameworks: StreamChat, StreamChatUI
**iOS version:18
**Xcode version:15

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions