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

add: Wechaty.self() to test if a message is from self. del: Message.self()

上级 d6110329
......@@ -46,7 +46,7 @@ bot
.on('login' , user => log.info('Bot', `bot login: ${user}`))
.on('logout' , e => log.info('Bot', 'bot logout.'))
.on('message', m => {
if (m.self()) { return }
if (bot.self(m)) { return }
co(function* () {
const msg = yield m.ready()
......
......@@ -32,11 +32,11 @@ bot
.on('login' , user => log.info('Bot', `${user.name()} logined`))
.on('logout' , user => log.info('Bot', `${user.name()} logouted`))
.on('scan', ({url, code}) => {
console.log(`Scan QR Code in url to login: ${code}\n${url}`)
if (!/201|200/.test(code)) {
let loginUrl = url.replace(/\/qrcode\//, '/l/')
require('qrcode-terminal').generate(loginUrl)
}
console.log(`${url}\n[${code}] Scan QR Code in above url to login: `)
})
.on('message', m => {
m.ready()
......@@ -44,7 +44,7 @@ bot
log.info('Bot', 'recv: %s', msg.toStringEx())
// logToFile(JSON.stringify(msg.rawObj))
if (/^(ding|ping|bing)$/i.test(m.get('content')) && !m.self()) {
if (/^(ding|ping|bing)$/i.test(m.get('content')) && !bot.self(m)) {
bot.reply(m, 'dong')
.then(() => { log.warn('Bot', 'REPLY: dong') })
}
......
......@@ -6,7 +6,7 @@ bot
console.log(`Use Wechat to Scan QR Code in url to login: ${code}\n${url}`)
})
.on('message', m => {
(!m.self()) && bot.reply(m, 'roger') // 1. reply others' msg
(!bot.self(m)) && bot.reply(m, 'roger') // 1. reply others' msg
.then(() => console.log(`RECV: ${m}, REPLY: "roger"`)) // 2. log message
.catch(e => console.error(e)) // 3. catch exception
})
......
......@@ -47,7 +47,7 @@ bot
console.log(`Scan QR Code in url to login: w${code}\n${url}`)
})
.on('message', m => {
if (m.self()) return
if (bot.self(m)) return
co(function* () {
const msg = yield m.ready()
......
......@@ -34,8 +34,6 @@ class Message {
, status: rawObj.Status
, digest: rawObj.MMDigest
, date: rawObj.MMDisplayTime // Javascript timestamp of milliseconds
, self: undefined // to store the logined user id
}
// FIXME: has ther any better method to know the room ID?
......@@ -87,14 +85,6 @@ class Message {
typeEx() { return Message.Type[this.obj.type] }
count() { return Message.counter }
self() {
if (!this.obj.self) {
log.warn('Message', 'self not set')
return false
}
return this.obj.self === this.obj.from
}
ready() {
log.silly('Message', 'ready()')
......
......@@ -210,16 +210,16 @@ function onServerLogin(data) {
/**
* save login user id to this.userId
*/
const userName = this.userId = yield this.bridge.getUserName()
this.userId = yield this.bridge.getUserName()
if (!userName) {
if (!this.userId) {
log.verbose('PuppetWebEvent', 'onServerLogin: browser not full loaded, retry later.')
setTimeout(onServerLogin.bind(this), 500)
return
}
log.verbose('PuppetWebEvent', 'bridge.getUserName: %s', userName)
this.user = yield Contact.load(userName).ready()
log.verbose('PuppetWebEvent', 'bridge.getUserName: %s', this.userId)
this.user = yield Contact.load(this.userId).ready()
log.verbose('PuppetWebEvent', `onServerLogin() user ${this.user.name()} logined`)
this.emit('login', this.user)
......@@ -263,8 +263,8 @@ function onServerMessage(data) {
break;
}
if (this.user) {
m.set('self', this.user.id)
if (this.userId) {
m.set('self', this.userId)
} else {
log.warn('PuppetWebEvent', 'onServerMessage() without this.user')
}
......
......@@ -42,7 +42,8 @@ class PuppetWeb extends Puppet {
this.head = options.head
this.session = options.session // if not set session, then dont store session.
this.user = null // <Contact> of user self
this.userId = null // user id
this.user = null // <Contact> of user self
}
toString() { return `Class PuppetWeb({browser:${this.browser},port:${this.port}})` }
......@@ -235,6 +236,17 @@ class PuppetWeb extends Puppet {
return Event.onBrowserDead.call(this, e)
}
self(message) {
if (!this.userId) {
log.verbose('PuppetWeb', 'self() got no this.userId')
return false
}
if (!message || !message.id) {
log.verbose('PuppetWeb', 'self() got no message')
return false
}
return this.userId == message.get('from')
}
send(message) {
const to = message.get('to')
const room = message.get('room')
......@@ -268,10 +280,8 @@ class PuppetWeb extends Puppet {
.set('to' , message.obj.from)
.set('room' , message.obj.room)
if (this.user) {
if (this.userId) {
// FIXME: find a alternate way to check a message create by `self`
m.set('self', this.user.id)
} else if (this.userId) {
m.set('self', this.userId)
}
else {
......
......@@ -129,7 +129,14 @@ class Wechaty extends EventEmitter {
})
}
send(message) {
self(message) {
return this.puppet.self(message)
.catch(e => {
log.error('Wechaty', 'self() exception: %s', e.message)
throw e
})
}
send(message) {
return this.puppet.send(message)
.catch(e => {
log.error('Wechaty', 'send() exception: %s', e.message)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册