firer.js 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/**
 *
 * Wechaty: Wechat for Bot. Connecting ChatBots
 *
 * Class PuppetWeb Firer
 *
 * Process the Message to find which event to FIRE
 *
 * Licenst: ISC
 * https://github.com/wechaty/wechaty
 *
 */

/**************************************
 *
 * Firer for Class PuppetWeb
 *
 * here `this` is a PuppetWeb Instance
 *
 ***************************************/
const util  = require('util')
const fs    = require('fs')
const co    = require('co')

const log = require('../brolog-env')
const Contact = require('../contact')
const Message = require('../message')
const FriendRequest = require('./friend-request')

const PuppetWebFirer = {
  fireFriendConfirm
  , fireFriendRequest

  , fireRoomJoin
  , fireRoomLeave

  // for testing
  , checkFriendConfirm
  , checkRoomJoin
  , checkRoomLeave
}

const regexConfig = {
Huan (李卓桓)'s avatar
bug fix  
Huan (李卓桓) 已提交
44
  friendConfirm:  /^You have added (.+) as your WeChat contact. Start chatting!$/
45

Huan (李卓桓)'s avatar
bug fix  
Huan (李卓桓) 已提交
46 47
  , roomJoin:     /^"?(.+?)"? invited "(.+)" to the group chat$/
  , roomLeave:    /^You removed "(.+)" from the group chat$/
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
}

function fireFriendRequest(m) {
  const info = m.rawObj.RecommendInfo
  log.verbose('PuppetWebEvent', 'fireFriendRequest(%s)', info)

  const request = new FriendRequest()
  request.receive(info)
  this.emit('friend', request.contact, request)
}

/**
 * try to find FriendRequest Confirmation Message
 */
function checkFriendConfirm(content) {
  const re = regexConfig.friendConfirm
  if (re.test(content)) {
    return true
  } else {
    return false
  }
}

function fireFriendConfirm(m) {
  const content = m.content()
  log.silly('PuppetWebEvent', 'fireFriendConfirm(%s)', content)

  if (!checkFriendConfirm(content)) {
    return
  }
  const request = new FriendRequest()
  const contact = Contact.load(m.get('from'))
  request.confirm(contact)

  this.emit('friend', contact)
}


/**
 * try to find 'join' event for Room
 *
1.
  You've invited "李卓桓" to the group chat
  You've invited "李卓桓.PreAngel、Bruce LEE" to the group chat
2.
   "李卓桓.PreAngel" invited "Bruce LEE" to the group chat
*/
function checkRoomJoin(content) {
  const re = regexConfig.roomJoin

  const found = content.match(re)
  if (!found) {
    return false
  }
  const [_, inviter, invitee] = found
Huan (李卓桓)'s avatar
bug fix  
Huan (李卓桓) 已提交
103
  return [invitee, inviter] // put invitee at first place
104 105 106 107 108 109
}

function fireRoomJoin(m) {
  const room    = m.room()
  const content = m.content()

Huan (李卓桓)'s avatar
bug fix  
Huan (李卓桓) 已提交
110 111
  let result = checkRoomJoin(content)
  if (!result) {
112 113
    return
  }
Huan (李卓桓)'s avatar
bug fix  
Huan (李卓桓) 已提交
114
  const [invitee, inviter] = result
115 116 117

  let inviterContact, inviteeContact

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  co.call(this, function* () {
    yield room.refresh()

    yield new Promise(resolve => {
      setTimeout(_ => resolve(), 1000)
    })

    if (inviter === "You've") {
      inviterContact = Contact.load(this.userId)
    } else {
      inviterContact = room.member(inviter)
    }

    inviteeContact = room.member(invitee)

    if (!inviterContact || !inviteeContact) {
      log.error('PuppetWebEvent', 'inivter or invitee not found for %s, %s', inviter.name(), invitee.name())
      return
    }
    room.emit('join', inviteeContact, inviterContact)
  })
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
}

function checkRoomLeave(content) {
  const re = regexConfig.roomLeave

  const found = content.match(re)
  if (!found) {
    return false
  }
  const [_, leaver] = found
  return leaver
}

/**
 * You removed "Bruce LEE" from the group chat
 */
function fireRoomLeave(m) {
  const leaver = checkRoomLeave(m.content())
  if (!leaver) {
    return
  }

  const room = m.room()
162

163 164 165 166 167 168 169
  leaverContact = room.member(leaver)

  if (!leaverContact) {
    log.error('PuppetWebEvent', 'leaver not found for %s', leaver)
    return
  }
  room.emit('leave', leaverContact)
170
  room.refresh()
171 172 173
}

module.exports = PuppetWebFirer