UNPKG

rcs-data

Version:

RCS消息数据结构

152 lines 5.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parse = exports.ImdnReplyDisplay = exports.ImdnReplyDelivery = exports.ImdnReply = void 0; const RcsMsgMono_1 = require("./RcsMsgMono"); const mime_1 = require("../mime"); const fast_xml_parser_1 = require("fast-xml-parser"); const cpim_1 = require("../cpim"); const common_1 = require("../common"); const constants_1 = require("./constants"); const constants_2 = require("./constants"); const debug = require('debug')('rcs:imdnreply'); function xmlBuilder() { const builder = new fast_xml_parser_1.XMLBuilder({ format: true, ignoreAttributes: false, suppressEmptyNode: true, }); builder['tagEndChar'] = '>\r\n'; builder['newLine'] = '\r\n'; return builder; } class ImdnReply extends RcsMsgMono_1.RcsMsgMono { constructor(replied, catalog, value, from, to, dateTime, messageId) { let builder = xmlBuilder(); let imdn = { '@_xmlns': 'urn:ietf:params:xml:ns:imdn', }; let xmlImdn = (0, mime_1.factory)({ contentType: 'message/imdn+xml', body: () => { if (this.replied.messageId) imdn['message-id'] = this.replied.messageId; if (this.replied.dateTime) imdn['datetime'] = this.replied.dateTime.value; let status = {}; status[value] = null; imdn[`${catalog}-notification`] = { status }; let body = builder.build({ imdn }); body = '<?xml version="1.0" encoding="UTF-8" ?>\r\n' + body; xmlImdn.header('Content-Length', body.length); return body; }, }); xmlImdn.header('Content-Disposition', 'notification'); xmlImdn.header('Content-Length', '0'); let cpim = (0, cpim_1.factory)({ from, to, dateTime, mime: xmlImdn }); cpim.addNS(constants_2.IMDN_NS_URI, constants_2.IMDN_NS_PREFIX); cpim.orderedHeaderNames.push(...constants_2.Cpim_Ordered_Header_Names); if (messageId) cpim.header(constants_2.IMDN_NS_URI, 'Message-ID', messageId); let mime = (0, mime_1.factory)({ contentType: 'message/cpim', body: () => { cpim.to = this.to; cpim.from = this.from; cpim.header(constants_2.IMDN_NS_URI, 'Message-ID', this.messageId); cpim.dateTime = this.dateTime; return cpim.toString(); }, }); super(mime, from, to); this._replied = replied ? replied : {}; this._notification = catalog; this._status = value; this._cpim = cpim; } get replied() { return this._replied; } get conversationId() { return null; } get contributionId() { return null; } get messageId() { return this._cpim.header(constants_2.IMDN_NS_URI, 'Message-ID'); } set messageId(value) { this._cpim.header(constants_2.IMDN_NS_URI, 'Message-ID', value); } get dateTime() { return this._cpim.dateTime; } set dateTime(value) { this._cpim.dateTime = value; } get initialContentType() { return this._cpim.mime.contentType; } get content() { return { notification: this._notification, status: this._status }; } get messagePlainObject() { let msg = {}; return msg; } get processingNotification() { throw Error('递送报告消息不支持查看处理情况'); } set processingNotification(status) { throw Error('递送报告消息不支持查看递送情况'); } get deliveryNotification() { throw Error('递送报告消息不支持查看递送情况'); } get displayNotification() { throw Error('递送报告消息不支持查看已读情况'); } get deliveryInfoList() { throw Error('递送报告消息不支持递送情况列表'); } } exports.ImdnReply = ImdnReply; class ImdnReplyDelivery extends ImdnReply { constructor(replied, status = constants_1.Delivery_Status.delivered, from, to, dateTime, messageId) { super(replied, 'delivery', status, from, to, dateTime, messageId); } } exports.ImdnReplyDelivery = ImdnReplyDelivery; class ImdnReplyDisplay extends ImdnReply { constructor(replied, status = constants_1.Display_Status.displayed, from, to, dateTime, messageId) { super(replied, 'display', status, from, to, dateTime, messageId); } } exports.ImdnReplyDisplay = ImdnReplyDisplay; function parse(rawXml) { let options = { attributeNamePrefix: '', ignoreAttributes: false }; const parser = new fast_xml_parser_1.XMLParser(options); let jObj = parser.parse(rawXml); debug(`解析后的对象:${JSON.stringify(jObj, null, 2)}`); if (!jObj.imdn || typeof jObj.imdn !== 'object') throw Error('要解析的数据没有包含imdn字段,或者该字段的数据类型不是对象'); let { imdn } = jObj; let rcsmsg; let messageId = imdn['message-id']; let dateTime = new common_1.DateTime(imdn.datetime); if (imdn.hasOwnProperty('display-notification')) { let status = Object.keys(imdn['display-notification'].status)[0]; rcsmsg = new ImdnReplyDisplay({ messageId, dateTime }, constants_1.Display_Status[status]); } else if (imdn.hasOwnProperty('delivery-notification')) { let status = Object.keys(imdn['display-notification'].status)[0]; rcsmsg = new ImdnReplyDelivery({ messageId, dateTime }, constants_1.Delivery_Status[status]); } else { throw Error('无法解析传入的消息内容'); } return rcsmsg; } exports.parse = parse; //# sourceMappingURL=ImdnReply.js.map