提交 feb80397 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

add image message class

上级 5ae7e2dc
...@@ -13,7 +13,7 @@ const log = require('npmlog') ...@@ -13,7 +13,7 @@ const log = require('npmlog')
class Message { class Message {
constructor(rawObj) { constructor(rawObj) {
Message.counter++; Message.counter++
this.rawObj = rawObj = rawObj || {} this.rawObj = rawObj = rawObj || {}
this.obj = this.parse(rawObj) this.obj = this.parse(rawObj)
...@@ -35,25 +35,24 @@ class Message { ...@@ -35,25 +35,24 @@ class Message {
} }
} }
toString() { toString() { return `Message#${Message.counter}(` + this.getFromString() + `: ${content})` }
const name = html2str(this.obj.from.get('name'))
getFromString() {
const name = this.obj.from.get('name')
const group = this.obj.group const group = this.obj.group
let content = html2str(this.obj.content) let content = this.unescapeHtml(this.stripHtml(this.obj.content))
if (content.length > 20) content = content.substring(0,17) + '...'; if (content.length > 20) { content = content.substring(0,17) + '...' }
let groupStr = group ? html2str(group) : '' return '<' + name + (group ? `@[${group}]` : '') + '>'
let fromStr = '<' + name + (groupStr ? `@[${groupStr}]` : '') + '>' }
return `Message#${Message.counter}(${fromStr}: ${content})` stripHtml(str) { return String(str).replace(/(<([^>]+)>)/ig,'') }
unescapeHtml(str) {
function html2str(html) { return String(str)
return String(html) .replace(/&apos;/g, "'")
.replace(/(<([^>]+)>)/ig,'') .replace(/&quot;/g, '"')
.replace(/&apos;/g, "'") .replace(/&gt;/g, '>')
.replace(/&quot;/g, '"') .replace(/&lt;/g, '<')
.replace(/&gt;/g, '>') .replace(/&amp;/g, '&')
.replace(/&lt;/g, '<') }
.replace(/&amp;/g, '&')
}
}
ready() { ready() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
...@@ -80,16 +79,16 @@ class Message { ...@@ -80,16 +79,16 @@ class Message {
return this return this
} }
dump() { dump() {
console.error('======= dump message =======') console.error('======= dump message =======')
Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj[k]}`)) Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj[k]}`))
} }
dumpRaw() { dumpRaw() {
console.error('======= dump raw message =======') console.error('======= dump raw message =======')
Object.keys(this.rawObj).forEach(k => console.error(`${k}: ${this.rawObj[k]}`)) Object.keys(this.rawObj).forEach(k => console.error(`${k}: ${this.rawObj[k]}`))
} }
getCount() { return Message.counter } count() { return Message.counter }
static find(selector, option) { static find(selector, option) {
return new Message({MsgId: '-1'}) return new Message({MsgId: '-1'})
...@@ -104,5 +103,29 @@ class Message { ...@@ -104,5 +103,29 @@ class Message {
} }
Message.counter = 0 Message.counter = 0
Message.Type = {
TEXT: 1,
IMAGE: 3,
VOICE: 34,
VIDEO: 43,
MICROVIDEO: 62,
EMOTICON: 47,
APP: 49,
VOIPMSG: 50,
VOIPNOTIFY: 52,
VOIPINVITE: 53,
LOCATION: 48,
STATUSNOTIFY: 51,
SYSNOTICE: 9999,
POSSIBLEFRIEND_MSG: 40,
VERIFYMSG: 37,
SHARECARD: 42,
SYS: 1e4,
RECALLED: 10002
}
Object.keys(Message.Type).forEach(k => {
const v = Message.Type[k]
Message.Type[v] = k // Message.Type[1] = 'TEXT'
})
module.exports = Message module.exports = Message
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册