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

contact class worked!

上级 c97d65a5
/**
*
* wechaty: Wechat for Bot. and for human who talk to bot/robot
*
* Licenst: ISC
* https://github.com/zixia/wechaty
*
*/
class Contact {
constructor(id) {
if (!Contact.puppet) throw new Error('no puppet attached to Contact');
this.id = id
this.obj = {}
Contact.puppet.getContact(id)
.then(data => {
this.rawObj = data
this.obj = this.parse(this.rawObj)
}).catch(e => {
throw new Error('getContact: ' + e)
})
}
parse(rawObj) {
return !rawObj ? {} : {
id: rawObj.UserName
, weixin: rawObj.Alias
, name: rawObj.NickName
, remark: rawObj.RemarkName
, sex: rawObj.Sex
, province: rawObj.Province
, city: rawObj.City
, signature: rawObj.Signature
}
}
dumpRaw() {
console.error('======= dump raw contact =======')
Object.keys(this.rawObj).forEach(k => console.error(`${k}: ${this.rawObj[k]}`))
}
dump() {
console.error('======= dump contact =======')
Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj[k]}`))
}
toString() {
return `Contact({id:${this.id})`
}
getId() { return this.id }
get(prop) { return this.obj[prop] }
send(message) {
}
......@@ -21,4 +66,14 @@ class Contact {
}
}
Contact.pool = {}
Contact.load = function (id) {
if (id in Contact.pool) {
return Contact.pool[id]
}
return Contact.pool[id] = new Contact(id)
}
Contact.attach = function (puppet) { Contact.puppet = puppet }
module.exports = Contact
/**
*
* wechaty: Wechat for Bot. and for human who talk to bot/robot
*
* Licenst: ISC
* https://github.com/zixia/wechaty
*
*/
class Group {
constructor(id) {
this.id = id
......
//const EventEmitter = require('events')
/**
*
* wechaty: Wechat for Bot. and for human who talk to bot/robot
*
* Licenst: ISC
* https://github.com/zixia/wechaty
*
*/
const Contact = require('./contact')
const Group = require('./group')
......@@ -7,11 +15,11 @@ class Message {
this.rawObj = rawObj = rawObj || {}
// Transform rawObj to local m
this.m = {
this.obj = {
id: rawObj.MsgId
, type: rawObj.MsgType
, from: new Contact(rawObj.MMActualSender)
, to: new Contact(rawObj.MMPeerUserName)
, from: Contact.load(rawObj.MMActualSender)
, to: Contact.load(rawObj.MMPeerUserName)
, group: rawObj.MMIsChatRoom ? new Group(rawObj.FromUserName) : null
, content: rawObj.MMActualContent
, status: rawObj.Status
......@@ -23,31 +31,27 @@ class Message {
}
toString() {
const id = this.m.id
// Contact
const from = this.m.from.getId()
const to = this.m.to.getId()
//return `Message({id:${id}, from:${from}, to:${to})`
let content = this.m.content
const name = this.obj.from.get('name')
let content = this.obj.content
if (content.length > 20) content = content.substring(0,17) + '...';
return `Message("${content}")`
return `Message("${name}: ${content}")`
}
get(prop) {
if (!prop || !(prop in this.m)) {
const s = '[' + Object.keys(this.m).join(',') + ']'
if (!prop || !(prop in this.obj)) {
const s = '[' + Object.keys(this.obj).join(',') + ']'
throw new Error(`Message.get(${prop}) must be in: ${s}`)
}
return this.m[prop]
return this.obj[prop]
}
set(prop, value) {
this.m[prop] = value
this.obj[prop] = value
}
dump() {
console.error('======= dump message =======')
Object.keys(this.m).forEach(k => console.error(`${k}: ${this.m[k]}`))
Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj[k]}`))
}
dumpRaw() {
console.error('======= dump raw message =======')
......
......@@ -57,16 +57,14 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?';
, slog: slog // log throw Socket IO
, ding: ding
, quit: quit
, getContact: getContact
}
function isReady() {
return !!((typeof angular)!=='undefined' && angular.element && angular.element("body"))
}
function init() {
// XXX
// return 'init skiped in browser'
if (!isReady()) {
clog('angular not ready. wait 500ms...')
setTimeout(init, 500)
......@@ -87,8 +85,9 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?';
var injector = angular.element(document).injector()
var rootScope = injector.get("$rootScope")
var http = injector.get("$http")
var chatFactory = injector.get("chatFactory")
var confFactory = injector.get("confFactory")
var chatFactory = injector.get("chatFactory")
var confFactory = injector.get("confFactory")
var contactFactory = injector.get('contactFactory')
var loginScope = angular.element(".login_box").scope()
// get all we need from wx in browser(angularjs)
......@@ -96,8 +95,9 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?';
injector: injector
, rootScope: rootScope
, http: http
, chatFactory: chatFactory
, confFactory: confFactory
, chatFactory: chatFactory
, confFactory: confFactory
, contactFactory: contactFactory
, loginScope: loginScope
}
}
......@@ -122,6 +122,7 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?';
chat.appendMessage(m)
return chat.sendMessage(m)
}
function getContact(id) { return Wechaty.glue.contactFactory.getContact(id) }
function hookMessage() {
var rootScope = Wechaty.glue.rootScope
rootScope.$on("message:add:success", function (event, data) {
......@@ -174,7 +175,7 @@ if (typeof Wechaty!=='undefined') return 'Wechaty already injected?';
var callback = arguments[arguments.length - 1]
if (typeof callback==='function')
callback('Wechaty')
return callback('Wechaty')
return 'Wechaty'
......
......@@ -96,12 +96,9 @@ class PuppetWeb extends Puppet {
* Public Methods
*
*/
getLoginQrImgUrl() {
return this.server.proxyWechaty('getLoginQrImgUrl')
}
getLoginStatusCode() {
return this.server.proxyWechaty('getLoginStatusCode')
}
getLoginQrImgUrl() { return this.server.proxyWechaty('getLoginQrImgUrl') }
getLoginStatusCode() { return this.server.proxyWechaty('getLoginStatusCode') }
getContact(id) { return this.server.proxyWechaty('getContact', id) }
}
module.exports = PuppetWeb
......@@ -32,6 +32,8 @@ class Puppet extends EventEmitter {
logout() { throw new Error('To Be Implementsd') }
alive() { throw new Error('To Be Implementsd') }
getContact() { throw new Error('To Be Implementsd') }
// () { throw new Error('To Be Implemented') }
/**
......
/**
*
* wechaty: Wechat for Bot. and for human who talk to bot/robot
*
* Licenst: ISC
* https://github.com/zixia/wechaty
*
*/
const EventEmitter = require('events')
//const Util = require('util');
......@@ -23,6 +32,7 @@ class Wechaty extends EventEmitter {
throw new Error('Puppet unknown: ' + puppet)
break
}
Contact.attach(this.puppet)
this.puppet.on('message', (e) => {
this.emit('message', e)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册