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

code clean

上级 f0a53a1c
The Internet Software Consortium License (ISC) The Internet Software Consortium License (ISC)
Copyright (c) 2016, Zhuohuan LI Copyright (c) 2016, Zhuohuan LI <zixia@zixia.net> https://github.com/zixia/
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
......
...@@ -9,6 +9,11 @@ export default async function onMessage(message: Message): Promise<void> { ...@@ -9,6 +9,11 @@ export default async function onMessage(message: Message): Promise<void> {
const sender = message.from() const sender = message.from()
const content = message.content() const content = message.content()
if (room) {
await room.ready()
}
await sender.ready()
console.log((room ? '[' + room.topic() + ']' : '') console.log((room ? '[' + room.topic() + ']' : '')
+ '<' + sender.name() + '>' + '<' + sender.name() + '>'
+ ':' + message.toStringDigest() + ':' + message.toStringDigest()
......
...@@ -84,6 +84,7 @@ export class Message implements Sayable { ...@@ -84,6 +84,7 @@ export class Message implements Sayable {
constructor(public rawObj?: MessageRawObj) { constructor(public rawObj?: MessageRawObj) {
this._counter = Message.counter++ this._counter = Message.counter++
log.silly('Message', 'constructor() #%d', this._counter)
if (typeof rawObj === 'string') { if (typeof rawObj === 'string') {
this.rawObj = JSON.parse(rawObj) this.rawObj = JSON.parse(rawObj)
...@@ -172,8 +173,8 @@ export class Message implements Sayable { ...@@ -172,8 +173,8 @@ export class Message implements Sayable {
public to(contact: Contact): Contact public to(contact: Contact): Contact
public to(room: Room): Room public to(room: Room): Room
public to(id: string): Contact | Room public to(id: string): Contact|Room
public to(): Contact | Room public to(): Contact|Room
public to(contact?: Contact|Room|string): Contact|Room { public to(contact?: Contact|Room|string): Contact|Room {
if (contact) { if (contact) {
if (contact instanceof Contact || contact instanceof Room) { if (contact instanceof Contact || contact instanceof Room) {
...@@ -240,7 +241,6 @@ export class Message implements Sayable { ...@@ -240,7 +241,6 @@ export class Message implements Sayable {
public async ready(): Promise<this> { public async ready(): Promise<this> {
log.silly('Message', 'ready()') log.silly('Message', 'ready()')
// return co.call(this, function* () {
try { try {
const from = Contact.load(this.obj.from) const from = Contact.load(this.obj.from)
const to = Contact.load(this.obj.to) const to = Contact.load(this.obj.to)
...@@ -254,7 +254,6 @@ export class Message implements Sayable { ...@@ -254,7 +254,6 @@ export class Message implements Sayable {
if (room) { await room.ready() } // Room member list if (room) { await room.ready() } // Room member list
return this // return this for chain return this // return this for chain
// }).catch(e => { // Exception
} catch (e) { } catch (e) {
log.error('Message', 'ready() exception: %s', e) log.error('Message', 'ready() exception: %s', e)
// console.log(e) // console.log(e)
...@@ -264,7 +263,12 @@ export class Message implements Sayable { ...@@ -264,7 +263,12 @@ export class Message implements Sayable {
} }
} }
/**
* @deprecated
*/
public get(prop: string): string { public get(prop: string): string {
log.warn('Message', 'DEPRECATED get()')
if (!prop || !(prop in this.obj)) { if (!prop || !(prop in this.obj)) {
const s = '[' + Object.keys(this.obj).join(',') + ']' const s = '[' + Object.keys(this.obj).join(',') + ']'
throw new Error(`Message.get(${prop}) must be in: ${s}`) throw new Error(`Message.get(${prop}) must be in: ${s}`)
...@@ -272,7 +276,12 @@ export class Message implements Sayable { ...@@ -272,7 +276,12 @@ export class Message implements Sayable {
return this.obj[prop] return this.obj[prop]
} }
/**
* @deprecated
*/
public set(prop: string, value: string): this { public set(prop: string, value: string): this {
log.warn('Message', 'DEPRECATED set()')
if (typeof value !== 'string') { if (typeof value !== 'string') {
throw new Error('value must be string, we got: ' + typeof value) throw new Error('value must be string, we got: ' + typeof value)
} }
......
/** /**
* Wechat for Bot. and for human who can talk with bot/robot * Wechat for Bot. Connecting ChatBots
* *
* Interface for puppet * Interface for puppet
* *
...@@ -353,7 +353,7 @@ export class Browser extends EventEmitter { ...@@ -353,7 +353,7 @@ export class Browser extends EventEmitter {
return return
} }
public clean(): Promise<any> { public clean(): Promise<void> {
const max = 30 const max = 30
const backoff = 100 const backoff = 100
......
...@@ -378,9 +378,7 @@ async function onServerMessage(this: PuppetWeb, data): Promise<void> { ...@@ -378,9 +378,7 @@ async function onServerMessage(this: PuppetWeb, data): Promise<void> {
} }
// To Be Deleted: set self... // To Be Deleted: set self...
if (this.userId) { if (!this.userId) {
m.set('self', this.userId)
} else {
log.warn('PuppetWebEvent', 'onServerMessage() without this.userId') log.warn('PuppetWebEvent', 'onServerMessage() without this.userId')
} }
......
...@@ -35,10 +35,10 @@ test('PuppetWebFriendRequest.confirm smoking test', t => { ...@@ -35,10 +35,10 @@ test('PuppetWebFriendRequest.confirm smoking test', t => {
const rawObj = JSON.parse(rawMessageData) const rawObj = JSON.parse(rawMessageData)
const m = new Message(rawObj) const m = new Message(rawObj)
t.true(/^You have added (.+) as your WeChat contact. Start chatting!$/.test(m.get('content')), 'should match confirm message') t.true(/^You have added (.+) as your WeChat contact. Start chatting!$/.test(m.content()), 'should match confirm message')
const fr = new PuppetWebFriendRequest() const fr = new PuppetWebFriendRequest()
const contact = Contact.load(m.get('from')) const contact = m.from()
fr.confirm(contact || new Contact('xx')) fr.confirm(contact || new Contact('xx'))
t.true(fr.contact instanceof Contact, 'should have a Contact instance') t.true(fr.contact instanceof Contact, 'should have a Contact instance')
......
/** /**
* Wechat for Bot. and for human who can talk with bot/robot * Wechat for Bot. Connecting ChatBots
* *
* Web Server for puppet * Web Server for puppet
* *
......
/** /**
* Wechat for Bot. and for human who can talk with bot/robot * Wechat for Bot. Connecting ChatBots
* *
* Interface for Puppet * Interface for Puppet
* *
......
...@@ -125,16 +125,17 @@ export class Room extends EventEmitter implements Sayable { ...@@ -125,16 +125,17 @@ export class Room extends EventEmitter implements Sayable {
public on(event: RoomEventName, listener: Function): this { public on(event: RoomEventName, listener: Function): this {
log.verbose('Room', 'on(%s, %s)', event, typeof listener) log.verbose('Room', 'on(%s, %s)', event, typeof listener)
const thisWithSay = { // const thisWithSay = {
say: (content: string) => { // say: (content: string) => {
return Config.puppetInstance() // return Config.puppetInstance()
.say(content) // .say(content)
} // }
} // }
super.on(event, function() { // super.on(event, function() {
return listener.apply(thisWithSay, arguments) // return listener.apply(thisWithSay, arguments)
}) // })
super.on(event, listener) // Room is `Sayable`
return this return this
} }
......
...@@ -188,15 +188,19 @@ export class Wechaty extends EventEmitter implements Sayable { ...@@ -188,15 +188,19 @@ export class Wechaty extends EventEmitter implements Sayable {
public on(event: WechatyEventName, listener: Function): this { public on(event: WechatyEventName, listener: Function): this {
log.verbose('Wechaty', 'on(%s, %s)', event, typeof listener) log.verbose('Wechaty', 'on(%s, %s)', event, typeof listener)
const thisWithSay: Sayable = { // const thisWithSay: Sayable = {
say: (content: string) => { // say: (content: string) => {
return Config.puppetInstance() // return Config.puppetInstance()
.say(content) // .say(content)
} // }
} // }
super.on(event, function() {
return listener.apply(thisWithSay, arguments) super.on(event, listener) // `this: Wechaty` is Sayable
})
// (...args) => {
//
// return listener.apply(this, args)
// })
return this return this
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册