contact.js 2.1 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1 2 3 4 5 6 7 8
/**
 *
 * wechaty: Wechat for Bot. and for human who talk to bot/robot
 *
 * Licenst: ISC
 * https://github.com/zixia/wechaty
 *
 */
9
const log = require('npmlog')
10 11

class Contact {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
12
  constructor(id) {
13
    log.silly('Contact', `constructor(${id})`)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
14
    if (!Contact.puppet) { throw new Error('no puppet attached to Contact') }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
15

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
16
    this.id = id
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
17
    this.obj = {}
18 19
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
20
  toString() { return `Contact(${this.obj.name}[${this.id}])` }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
21 22 23 24

  parse(rawObj) {
    return !rawObj ? {} : {
      id:       rawObj.UserName
25
      , uin:    rawObj.Uin  // stable id? 4763975
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
26 27 28 29 30 31 32 33 34
      , weixin: rawObj.Alias
      , name:   rawObj.NickName
      , remark: rawObj.RemarkName
      , sex:    rawObj.Sex
      , province:   rawObj.Province
      , city:       rawObj.City
      , signature:  rawObj.Signature
    }
  }
35
  name() { return this.obj.name }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  ready(contactGetter) {
    log.silly('Contact', 'ready(' + typeof contactGetter + ')')
    if (this.obj.id) { return Promise.resolve(this) }

    if (!contactGetter) {
      log.silly('Contact', 'get contact via ' + Contact.puppet.constructor.name)
      contactGetter = Contact.puppet.getContact.bind(Contact.puppet)
    }
    return contactGetter(this.id)
    .then(data => {
      log.silly('Contact', `contactGetter(${this.id}) resolved`)
      this.rawObj = data
      this.obj    = this.parse(data)
      return this
    }).catch(e => {
      log.error('Contact', `contactGetter(${this.id}) rejected: %s`, e)
      throw e
    })
  }

57
  dumpRaw() {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
58
    console.error('======= dump raw contact =======')
59
    Object.keys(this.rawObj).forEach(k => console.error(`${k}: ${this.rawObj[k]}`))
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
60
  }
61
  dump()    {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
62
    console.error('======= dump contact =======')
63
    Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj[k]}`))
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
64
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
65

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
66 67
  get(prop) { return this.obj[prop] }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
68
  send(message) {
69

70 71
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
72
  static find() {
73 74
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
75
  static findAll() {
76 77 78
  }
}

79
Contact.init = function() { Contact.pool = {} }
80
Contact.init()
81
Contact.load = function(id) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
82 83 84 85 86 87
  if (id in Contact.pool) {
    return Contact.pool[id]
  }
  return Contact.pool[id] = new Contact(id)
}

88
Contact.attach = function(puppet) { Contact.puppet = puppet }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
89

90
module.exports = Contact