如何从服务端数据库获取消息,并转换为 web 端定义的 Message 类型

x86 11月前 484

服务端处理:

  1. 参考DatabaseStore 中的 MessageBundle getMessage(long messageId)方法,获取到 WFCMessage.MessageContent
  2. 通过OutputMessageData 中的 OutputMessageData fromProtoMessage(WFCMessage.Message protoMessage, OutputClient fromClient)WFCMessage.MessageContent转换成OutputMessageData
  3. OutputMessageData返回给前端,可以通过 json 等格式

Web 处理:

  1. 拿到从服务端返回的OutputMessageData
  2. 参考message.js里面的fromProtoMessage方法,该方法的参数obj和上面的OutputMessageData非常类似,但还是有一点区别,请参考下面的方法:
    static fromOutputMessageData(obj) {
        let msg = new Message();
        msg.from = obj.sender;
        msg.content = obj.payload;
        msg.messageUid = obj.messageId;
        msg.timestamp = obj.timestamp;
        let contentClazz = MessageConfig.getMessageContentClazz(obj.payload.type);
        if (contentClazz) {
            let content = new contentClazz();
            try {
                // outputMessageData.payload.base64edData 已经是base64 格式
                obj.payload.binaryContent = obj.payload.base64edData;
                delete obj.payload.base64edData;
                content.decode(obj.payload);
                content.extra = obj.payload.extra;
                if (content instanceof NotificationMessageContent) {
                    content.fromSelf = msg.from === wfc.getUserId();
                }
            } catch (error) {
                console.error('decode message payload failed, fallback to unkownMessage', obj.payload, error);
                let flag = MessageConfig.getMessageContentPersitFlag(obj.payload.type);
                if (PersistFlag.Persist === flag || PersistFlag.Persist_And_Count === flag) {
                    content = new UnknownMessageContent(obj.payload);
                } else {
                    return null;
                }
            }
            msg.messageContent = content;

            if (content instanceof UnknownMessageContent) {
                console.log('unknownMessage Content', obj)
            }
        } else {
            console.error('message content not register', obj);
        }

        if (msg.from === wfc.getUserId()) {
            msg.conversation = new Conversation(obj.conv.type, obj.conv.target, obj.conv.line);
            // out
            msg.direction = 0;
            msg.status = MessageStatus.Sent;
        } else {
            if (obj.conv.type === ConversationType.Single) {
                msg.conversation = new Conversation(obj.conv.type, obj.fromUser, obj.conv.line);
            } else {
                msg.conversation = new Conversation(obj.conv.type, obj.conv.target, obj.conv.line);
            }

            // in
            msg.direction = 1;

            // 无法计算已读状态
            // msg.status = MessageStatus.Unread;
            //
            // if (msg.content.mentionedType === 2) {
            //     msg.status = MessageStatus.AllMentioned;
            // } else if (msg.content.mentionedType === 1) {
            //     for (const target of msg.content.mentionedTarget) {
            //         if (target === wfc.getUserId()) {
            //             msg.status = MessageStatus.Mentioned;
            //             break;
            //         }
            //     }
            // }
        }
        return msg;
    }
最新回复 (1)
返回