wechaty-event.ts 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 *
 * Wechaty: Wechat for ChatBots.
 *
 * Class WechatyEvent
 *
 * Licenst: ISC
 * https://github.com/wechaty/wechaty
 *
 */
/**************************************
 *
 * Events Function Wrapper
 *
 *
 ***************************************/
17 18 19 20
import Config  import './config'
import Contact import './contact'
import Message import './message'
// import Room    import './room'
21

22
import log     import './brolog-env'
23 24 25 26 27 28

const WechatyEvent = {
  list
  , wrap
}

29
const EVENT_CONFIG = {
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  error:          wrapFilehelper
  , friend:       wrapContact
  , heartbeat:    wrapFilehelper
  , login:        wrapFilehelper
  , logout:       null  // NULL
  , message:      wrapMessage
  , 'room-join':  wrapRoom
  , 'room-leave': wrapRoom
  , 'room-topic': wrapRoom
  , scan:         null  // NULL
}

function list() {
  return Object.keys(EVENT_CONFIG)
}

function wrap(event, callback) {
  log.verbose('WechatyEvent', 'wrap(%s, %s)', event, typeof callback)

  // if (!(this instanceof Wechaty)) {
  //   throw new Error('`this` should be Wechaty instance')
  // }
  if (typeof callback !== 'function') {
    throw new Error('`callback` should be function')
  }

  if (!(event in EVENT_CONFIG)) {
    throw new Error('event not support: ' + event)
  }
  const wrapper = EVENT_CONFIG[event]

  /**
   * We assign a empty object to each event callback,
   * to carry the indenpendent scope
   */
  if (wrapper) {
    return wrapper(callback)
  } else {
    return callback
  }
}

function isContact(contact) {
  if (contact.map && contact[0] instanceof Contact) {
    return true
  } else if (contact instanceof Contact) {
    return true
  }
  return false
}

function isRoom(room) {
  /**
   * here not use `instanceof Room` is because circular dependence problem...
   */
  if (!room || !room.constructor
      || room.constructor.name !== 'Room') {
    return fasle
  }
  return true
}

function wrapContact(callback) {
  log.verbose('WechatyEvent', 'wrapContact()')

  return (...argList) => {
    log.silly('WechatyEvent', 'wrapContact() callback')

    if (!isContact(argList[0])) {
      throw new Error('contact not found in argList')
    }

    const contact = argList[0]

    const eventScope = {}
    eventScope.say = (content) => {
      const msg = new Message()
      msg.to(contact)
      msg.content(content)
      return Config.puppetInstance()
                    .send(msg)
    }

    return callback.apply(eventScope, argList)
  }
}

function wrapRoom(callback) {
  log.verbose('WechatyEvent', 'wrapRoom()')

  return (...argList) => {
    log.silly('WechatyEvent', 'wrapRoom() callback')
    let room, contact
    for (let arg of argList) {
      if (!room && isRoom(arg)) {
        room = arg
      } else if (!contact && isContact(arg)) {
        contact = arg
      }
    }

    if (!room || !contact) {
      throw new Error('room or contact not found')
    }

    const eventScope = {}
    eventScope.say = (content, replyTo = null) => {
      if (!replyTo) {
        replyTo = contact
      } else if (!isContact(replyTo)) {
        throw new Error('replyTo is not Contact instance(s)')
      }
      return room.say(content, replyTo)
    }

    return callback.apply(eventScope, argList)
  }
}

function wrapMessage(callback) {
  log.verbose('WechatyEvent', 'wrapMessage()')

  console.log('############### Message type: ')
  console.log(typeof Message)
  return (...argList) => {
    log.silly('WechatyEvent', 'wrapMessage() callback')

    console.log('############### wrapped on message callback')
    // console.log(typeof Message)
    // console.log(argList)
    if (!(argList[0] instanceof Message)) {
      throw new Error('Message instance not found')
    }

    const msg       = argList[0]

    const sender    = msg.from()
    const receiver  = msg.to()
    const room      = msg.room()

    const eventScope = {}
    eventScope.say = (content, replyTo) => {
      log.silly('WechatyEvent', 'wrapMessage() say("%s", "%s")', content, replyTo)

      if (room) {
        return room.say(content, replyTo)
      }

      const msg = new Message()
      msg.to(sender)
      msg.content(content)

      return Config.puppetInstance()
                    .send(msg)
    }

    return callback.apply(eventScope, argList)
  }
}

function wrapFilehelper(callback) {
  log.verbose('WechatyEvent', 'wrapFilehelper()')

  return (...argList) => {
    log.silly('WechatyEvent', 'wrapFilehelper() callback')
    const eventScope = {}
    eventScope.say = (content) =>{
      log.silly('WechatyEvent', 'wrapFilehelper() say(%s)', content)
      const msg = new Message()
      msg.to('filehelper')
      msg.content(content)
      return Config.puppetInstance()
                    .send(msg)
    }

    return callback.apply(eventScope, argList)
  }
}

209 210
// module.exports = WechatyEvent.default = WechatyEvent
export default WechatyEvent