编辑消息
编辑消息
环信即时通讯 IM 提供编辑消息功能,支持对发送成功的消息进行编辑,然后再发送,提高沟通效率和准确性,例如,发送消息后发觉消息内容中包含错别字、遗漏了的关键信息、内容不够完善清晰、甚至临时想更改自己的想法,可以编辑消息后再次发送。
功能开通和版本支持
对于单聊、群组和聊天室聊天会话中已经发送成功的消息,SDK 支持对这些消息的内容进行编辑。若使用该功能,需联系环信商务开通。
- SDK 4.13.0 之前的版本仅支持对单聊和群组会话中发送后的文本消息进行编辑。
- SDK 4.13.0 及之后的版本支持对单聊、群组和聊天室会话中各类消息进行编辑:
- 文本消息:支持编辑消息内容字段
msg和扩展字段ext。 - 自定义消息:支持编辑
customEvent、customExts和 扩展字段ext。 - 图片/语音/视频/文件/位置/合并消息:仅支持编辑扩展字段
ext。 - 透传消息:不支持编辑。
技术原理
环信即时通讯 IM 通过 modifyMessage 方法实现消息编辑。
消息编辑流程
- 用户调用 SDK 的 API 编辑一条消息。
- 服务端存储的该条消息,编辑成功后回调给 SDK。
- SDK 编辑客户端上的该条消息。成功后,SDK 将编辑后的消息回调给用户。
各类会话的消息编辑权限
- 对于单聊会话,只有消息发送方才能对消息进行编辑。
- 对于群组/聊天室会话,普通成员只能编辑自己发送的消息。群主/聊天室所有者和管理员除了可以编辑自己发送的消息,还可以编辑普通成员发送的消息。这种情况下,消息的发送方不变,消息体中的编辑者的用户 ID 属性为群主/聊天室所有者或管理员的用户 ID。
消息编辑后的生命周期
编辑消息没有时间限制,即只要这条消息仍在服务端存储就可以编辑。消息编辑后,消息生命周期(在服务端的保存时间)会重新计算,例如,消息可在服务器上保存 180 天,用户在消息发送后的第 30 天(服务器上的保存时间剩余 150 天)编辑了消息,编辑成功后该消息还可以在服务器上保存 180 天。
对于编辑后的消息,消息体中除了内容或扩展字段变化,还新增了编辑者的用户 ID、编辑时间和编辑次数属性。除消息体外,该消息的其他信息(例如,消息发送方、接收方)均不会发生变化。
前提条件
开始前,请确保满足以下条件:
实现方法
你可以调用 modifyMessage 方法编辑已经发送成功的消息。除消息体和消息扩展属性 ext 外,该消息的其他信息(例如,消息 ID、消息发送方、接收方)均不会发生变化。
SDK 4.13.0 及以后版本中编辑消息成功后会返回 modifiedInfo 字段。你可以从res.modifiedInfo 中获取消息的最新编辑时间(operationTime)、最新编辑的操作者(operatorId)以及消息编辑次数(operationCount)。
消息编辑后,消息的接收方会收到 onModifiedMessage 事件,该事件中会携带编辑后的消息对象、最新一次编辑消息的用户以及消息的最新编辑时间。对于群组和聊天室会话,除了编辑消息的用户,群组/聊天室内的其他成员均会收到该事件。
提示
- 一条消息默认最多可编辑 10 次。
- 若通过 RESTful API 编辑自定义消息,消息的接收方也通过
onModifiedMessage事件接收编辑后的自定义消息。
示例代码如下:
// 注册编辑消息事件
conn.addEventHandler("modifiedMessage", {
onModifiedMessage: message => {
console.log('onModifiedMessage', message)
},
});
// 编辑文本消息只支持编辑 `msg` 和 `ext` 字段
const textMessage = WebIM.message.create({
chatType: 'singleChat',
type: 'txt',
to: 'to',
msg: 'message content',
ext: { key: 'new value' }
})
// 自定义消息支持编辑 `customEvent`、`customExts` 和 `ext` 字段
const customMessage = WebIM.message.create({
chatType: 'singleChat',
type: 'custom',
to: 'to',
customEvent: 'new event',
customExts: { key: 'new value' },
ext: { key: 'new value' }
})
// 图片/语音/视频/文件/位置/合并消息只支持编辑 `ext` 字段
const imageMessage = WebIM.message.create({
chatType: 'singleChat',
type: 'img',
url: 'origin message url',
to: 'to',
ext: { key: 'new value' }
})
const videoMessage = WebIM.message.create({
chatType: 'singleChat',
type: 'video',
to: 'to',
body: {
url: 'origin message url'
},
ext: { key: 'new value' }
})
const fileMessage = WebIM.message.create({
chatType: 'singleChat',
type: 'file',
to: 'to',
body: {
url: 'origin message url'
},
ext: { key: 'new value' }
})
const audioMessage = WebIM.message.create({
chatType: 'singleChat',
type: 'audio',
to: 'to',
body: {
url: 'origin message url'
},
ext: { key: 'new value' }
})
const combineMessage = WebIM.message.create({
chatType: 'singleChat',
type: 'combine',
to: 'to',
compatibleText: 'origin message compatibleText',
title: 'origin message title',
summary: 'origin message summary',
messageList: [],
ext: { key: 'new value' }
})
const locationMessage = WebIM.message.create({
chatType: 'singleChat',
type: 'loc',
to: 'to',
addr: 'origin message addr',
buildingName: 'origin message buildingName',
lat: 'origin message lat',
lng: 'origin message lng',
ext: { key: 'new value' }
})
conn
.modifyMessage({
messageId: 'origin message id',
modifiedMessage: textMessage
})
.then((res) => {
console.log('modify msg success', res.modifiedInfo)
})
.catch((e) => {
console.log('modify failed', e)
})
