Edit a Message
While editing a message is straightforward, receiving events for edited messages with CometChat has two parts:
- Adding a listener to receive real-time message edits when your app is running
- Calling a method to retrieve missed message edits when your app was not running
Edit a Message
In other words, as a sender, how do I edit a message?
In order to edit a message, you can use the editMessage()
method. This method takes an object of the BaseMessage
class. At the moment, you are only allowed to edit TextMessage
and CustomMessage
. Thus, the BaseMessage
object must either be a Text or a Custom Message.
Add/Update Tags
While editing a message, you can update the tags associated with the Message. You can use the setTags()
method to do so. The tags added while editing a message will replace the tags set when the message was sent.
- Text Message
- Custom Message
- Text Message (Typescript)
- Custom Message (Typescript)
let tags = ["pinnedMessage"];
textMessage.setTags(tags);
let tags = ["pinnedMessage"];
customMessage.setTags(tags);
let tags: Array<String> = ["pinnedMessage"];
textMessage.setTags(tags);
let tags: Array<String> = ["pinnedMessage"];
customMessage.setTags(tags);
Once the message object is ready, you can use the editMessage()
method and pass the message object to it.
- Edit Message
- Typescript
let receiverID = "RECEIVER_UID";
let messageText = "Hello world!";
let receiverType = CometChat.RECEIVER_TYPE.USER;
let messageId = "MESSAGE_ID_OF_THE_MESSAGE_TO_BE_EDITED";
let textMessage = new CometChat.TextMessage(receiverID, messageText, receiverType);
textMessage.setId(messageId);
CometChat.editMessage(textMessage).then(
message => {
console.log("Message Edited", message);
}, error => {
console.log("Message editing failed with error:", error);
}
);
let receiverID: string = "RECEIVER_UID";
let messageText: string = "Hello world!";
let receiverType: string = CometChat.RECEIVER_TYPE.USER;
let messageId: number = 1;
let textMessage: CometChat.TextMessage = new CometChat.TextMessage(receiverID, messageText, receiverType);
textMessage.setId(messageId);
CometChat.editMessage(textMessage).then(
(message: CometChat.TextMessage) => {
console.log("Message Edited", message);
}, (error: CometChat.CometChatException) => {
console.log("Message editing failed with error:", error);
}
);
The object of the edited message will be returned in the onSuccess()
callback method of the listener. The message object will contain the editedAt
field set with the timestamp of the time the message was edited. This will help you identify if the message was edited while iterating through the list of messages. The editedBy
field is also set to the UID of the user who edited the message.
By default, CometChat allows certain roles to edit a message.
User Role | Conversation Type | Edit Capabilities |
---|---|---|
Message Sender | One-on-One Conversation | Messages they have sent. |
Message Sender | Group Conversation | Messages they have sent. |
Group Owner | Group Conversation | All messages in the group. |
Group Moderator | Group Conversation | All messages in the group. |
Real-time Message Edit Events
In other words, as a recipient, how do I know when someone has edited their message when my app is running?
In order to receive real-time events for message being edited, you need to override the onMessageEdited()
method of the MessageListener
class.
- Message Listener
- Typescript
let listenerID = "UNIQUE_LISTENER_ID";
CometChat.addMessageListener(
listenerID,
new CometChat.MessageListener({
onMessageEdited: message => {
console.log("Edited Message", message);
}
})
);
let listenerID: string = "UNIQUE_LISTENER_ID";
CometChat.addMessageListener(
listenerID,
new CometChat.MessageListener({
onMessageEdited: (message: CometChat.BaseMessage) => {
console.log("Edited Message", message);
}
})
);
Missed Message Edit Events
In other words, as a recipient, how do I know when someone edited their message when my app was not running?
When you retrieve the list of previous messages, for the message that was edited, the editedAt
and the editedBy
fields will be set. Also, for example, of the total number of messages for a conversation are 100, and the message with message ID 50 was edited. Now the message with id 50 will have the editedAt
and the editedBy
fields set whenever it is pulled from the history. Also, the 101st message will be and [Action] message informing you that the message with id 50 has been edited.
For the message edited event, in the Action
object received, the following fields can help you get the relevant information-
action
-edited
actionOn
- Updated message object with the edited details.actionBy
- User object containing the details of the user who has edited the message.actionFor
- User/group object having the details of the receiver to which the message was sent.
In order to edit a message, you need to be either the sender of the message or the admin/moderator of the group in which the message was sent.