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

use Config.puppetInstance() instead of attach by self

上级 d104b583
......@@ -6,13 +6,14 @@
* https://github.com/zixia/wechaty
*
*/
const log = require('./brolog-env')
const UtilLib = require('./util-lib')
const Wechaty = require('./wechaty')
const { log } = require('./brolog-env')
const { UtilLib } = require('./util-lib')
const { Config } = require('./config')
class Contact {
constructor(id) {
log.silly('Contact', `constructor(${id})`)
if (!Contact.puppet) { throw new Error('no puppet attached to Contact') }
if (id && typeof id !== 'string') { throw new Error('id must be string if provided. we got: ' + typeof id) }
this.id = id
......@@ -59,8 +60,11 @@ class Contact {
}
if (!contactGetter) {
log.silly('Contact', 'get contact via ' + Contact.puppet.constructor.name)
contactGetter = Contact.puppet.getContact.bind(Contact.puppet)
if (!Config.puppetInstance()) { throw new Error('Config.puppetInstance() is not found by Contact') }
log.silly('Contact', 'get contact via ' + Config.puppetInstance().constructor.name)
contactGetter = Config.puppetInstance()
.getContact.bind(Config.puppetInstance())
}
return contactGetter(this.id)
.then(data => {
......@@ -117,9 +121,9 @@ Contact.load = function(id) {
// return []
// }
Contact.attach = function(puppet) {
log.verbose('Contact', 'attach() to %s', puppet && puppet.constructor.name)
Contact.puppet = puppet
}
// Contact.attach = function(puppet) {
// log.warn('Contact', '@deprecated attach() to %s', puppet && puppet.constructor.name)
// // Config.puppetInstance(puppet)
// }
module.exports = Contact
module.exports = Contact.default = Contact.Contact = Contact
......@@ -8,16 +8,22 @@
*/
const co = require('co')
const Wechaty = require('./wechaty')
const Contact = require('./contact')
const Room = require('./room')
const Config = require('./config')
const UtilLib = require('./util-lib')
const log = require('./brolog-env')
const UtilLib = require('./util-lib')
const log = require('./brolog-env')
class Message {
constructor(rawObj) {
Message.counter++
if (typeof rawObj === 'string') {
rawObj = JSON.parse(rawObj)
}
this.rawObj = rawObj = rawObj || {}
this.obj = this.parse(rawObj)
this.id = this.obj.id
......@@ -99,7 +105,8 @@ class Message {
return this // return this for chain
}).catch(e => { // Exception
log.error('Message', 'ready() exception: %s', e.message)
log.error('Message', 'ready() exception: %s', e)
console.log(e)
throw e
})
}
......@@ -140,36 +147,36 @@ class Message {
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,
TEXT: 1,
IMAGE: 3,
VOICE: 34,
VERIFYMSG: 37,
POSSIBLEFRIEND_MSG: 40,
VERIFYMSG: 37,
SHARECARD: 42,
SYS: 1e4,
RECALLED: 10002
SHARECARD: 42,
VIDEO: 43,
EMOTICON: 47,
LOCATION: 48,
APP: 49,
VOIPMSG: 50,
STATUSNOTIFY: 51,
VOIPNOTIFY: 52,
VOIPINVITE: 53,
MICROVIDEO: 62,
SYSNOTICE: 9999,
SYS: 10000,
RECALLED: 10002
}
Object.keys(Message.Type).forEach(k => {
const v = Message.Type[k]
Message.Type[v] = k // Message.Type[1] = 'TEXT'
})
Message.attach = function(puppet) {
log.verbose('Message', 'attach() to %s', puppet && puppet.constructor.name)
Message.puppet = puppet
}
// Message.attach = function(puppet) {
// log.verbose('Message', 'attach() to %s', puppet && puppet.constructor.name)
// Message.puppet = puppet
// }
module.exports = Message
module.exports = Message.default = Message.Message = Message
/*
* join room in mac client: https://support.weixin.qq.com/cgi-bin/mmsupport-bin/addchatroombyinvite?ticket=AUbv%2B4GQA1Oo65ozlIqRNw%3D%3D&exportkey=AS9GWEg4L82fl3Y8e2OeDbA%3D&lang=en&pass_ticket=T6dAZXE27Y6R29%2FFppQPqaBlNwZzw9DAN5RJzzzqeBA%3D&wechat_real_lang=en
......
......@@ -5,9 +5,13 @@
* Licenst: ISC
* https://github.com/zixia/wechaty
*
* Add/Del/Topic: https://github.com/wechaty/wechaty/issues/32
*
*/
const log = require('./brolog-env')
const UtilLib = require('./util-lib')
const Wechaty = require('./wechaty')
const log = require('./brolog-env')
const UtilLib = require('./util-lib')
const Config = require('./config')
class Room {
constructor(id) {
......@@ -15,8 +19,8 @@ class Room {
this.id = id
this.obj = {}
this.dirtyObj = {} // when refresh, use this to save dirty data for query
if (!Room.puppet) {
throw new Error('no puppet attached to Room')
if (!Config.puppetInstance) {
throw new Error('Config.puppetInstance not found')
}
}
......@@ -46,7 +50,8 @@ class Room {
log.warn('Room', 'ready() has obj.id but memberList empty in room %s. reloading', this.obj.topic)
}
contactGetter = contactGetter || Room.puppet.getContact.bind(Room.puppet)
contactGetter = contactGetter || Config.puppetInstance()
.getContact.bind(Config.puppetInstance())
return contactGetter(this.id)
.then(data => {
log.silly('Room', `contactGetter(${this.id}) resolved`)
......@@ -101,7 +106,7 @@ class Room {
if (!contact) {
throw new Error('contact not found')
}
return Room.puppet.roomDel(this, contact)
return Config.puppetInstance.roomDel(this, contact)
.then(r => this.delLocal(contact))
}
......@@ -148,14 +153,14 @@ class Room {
throw new Error('contact not found')
}
return Room.puppet.roomAdd(this, contact)
return Config.puppetInstance.roomAdd(this, contact)
}
topic(newTopic) {
log.verbose('Room', 'topic(%s)', newTopic)
if (newTopic) {
Room.puppet.roomTopic(this, newTopic)
Config.puppetInstance.roomTopic(this, newTopic)
return newTopic
}
return this.get('topic')
......@@ -167,7 +172,7 @@ class Room {
if (!contactList || ! typeof contactList === 'array') {
throw new Error('contactList not found')
}
return Room.puppet.roomCreate(contactList)
return Config.puppetInstance.roomCreate(contactList)
}
// private
......@@ -189,7 +194,7 @@ class Room {
throw new Error('unsupport name type')
}
return Room.puppet.roomFind(filterFunction)
return Config.puppetInstance.roomFind(filterFunction)
.then(idList => {
return idList
})
......@@ -254,12 +259,12 @@ class Room {
return Room.pool[id] = new Room(id)
}
static attach(puppet) {
// if (!puppet) {
// throw new Error('Room.attach got no puppet to attach!')
// }
Room.puppet = puppet
}
// static attach(puppet) {
// // if (!puppet) {
// // throw new Error('Room.attach got no puppet to attach!')
// // }
// Config.puppetInstance = puppet
// }
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册