From 29b0ab109cd241e744d0f21566700abe203d31f0 Mon Sep 17 00:00:00 2001 From: "Zhuohuan LI (CARPE DIEM)" Date: Wed, 5 Oct 2016 12:44:20 +0800 Subject: [PATCH] #32 add Firer Class to match message with event --- src/puppet-web/event.js | 108 +++-------------------- src/puppet-web/firer.js | 162 +++++++++++++++++++++++++++++++++++ src/puppet-web/firer.spec.js | 81 ++++++++++++++++++ 3 files changed, 254 insertions(+), 97 deletions(-) create mode 100644 src/puppet-web/firer.js create mode 100755 src/puppet-web/firer.spec.js diff --git a/src/puppet-web/event.js b/src/puppet-web/event.js index 779c3ecc..c4787e3f 100644 --- a/src/puppet-web/event.js +++ b/src/puppet-web/event.js @@ -22,11 +22,13 @@ 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 MediaMessage = require('../message-media') +const log = require('../brolog-env') +const Contact = require('../contact') +const Message = require('../message') +const MediaMessage = require('../message-media') + const FriendRequest = require('./friend-request') +const Firer = require('./firer') const PuppetWebEvent = { onBrowserDead @@ -338,16 +340,17 @@ function onServerMessage(data) { * Fire Events if match message type & content */ switch (m.type()) { // data.MsgType + case Message.Type.VERIFYMSG: - fireFriendRequest(m) + Firer.fireFriendRequest.call(this, m) break case Message.Type.SYS: if (m.room()) { - fireRoomJoin(m) - fireRoomLeave(m) + Firer.fireRoomJoin.call(this, m) + Firer.fireRoomLeave.call(this, m) } else { - fireFriendConfirm(m.content()) + Firer.fireFriendConfirm.call(this, m) } break } @@ -382,93 +385,4 @@ function onServerMessage(data) { }) } -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) -} - -function fireFriendConfirm(m) { - const content = m.content() - log.silly('PuppetWebEvent', 'fireFriendConfirm(%s)', content) - - /** - * try to find FriendRequest Confirmation Message - */ - if (!/^You have added (.+) as your WeChat contact. Start chatting!$/.test(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 fireRoomJoin(m) { - const room = m.room() - const content = m.content() - - const inviteRe = /^"?(.+)"? invited "(.+)" to the group chat$/ - - const found = content.match(inviteRe) - if (!found) { - return - } - - const [_, inviter, invitee] = found - let inviterContact, inviteeContact - - 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, invitee) - return - } - room.emit('join', inviteeContact, inviterContact) -} - -/** - * You removed "Bruce LEE" from the group chat - */ -function fireRoomLeave(m) { - const room = m.room() - const content = m.content() - - const removeRe = /^You removed "(.+)" from the group chat$/ - - const found = content.match(removeRe) - if (!found) { - return - } - - let [_, leaver] = found - leaverContact = m.room().member(leaver) - - if (!leaverContact) { - log.error('PuppetWebEvent', 'leaver not found for %s', leaver) - return - } - room.emit('leave', leaverContact) -} - module.exports = PuppetWebEvent diff --git a/src/puppet-web/firer.js b/src/puppet-web/firer.js new file mode 100644 index 00000000..07fa1b49 --- /dev/null +++ b/src/puppet-web/firer.js @@ -0,0 +1,162 @@ +/** + * + * 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 = { + friendConfirm: /^You have added (.+) as your WeChat contact. Start chatting!$/ + + , roomJoin: /^"?(.+?)"? invited "(.+)" to the group chat$/ + , roomLeave: /^You removed "(.+)" from the group chat$/ +} + +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 + return [invitee, inviter] +} + +function fireRoomJoin(m) { + const room = m.room() + const content = m.content() + + const [invitee, inviter] = checkRoomJoin(cotent) + if (!invitee || !inviter) { + return + } + + let inviterContact, inviteeContact + + 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, invitee) + return + } + room.emit('join', inviteeContact, inviterContact) +} + +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() + leaverContact = room.member(leaver) + + if (!leaverContact) { + log.error('PuppetWebEvent', 'leaver not found for %s', leaver) + return + } + room.emit('leave', leaverContact) +} + +module.exports = PuppetWebFirer diff --git a/src/puppet-web/firer.spec.js b/src/puppet-web/firer.spec.js new file mode 100755 index 00000000..3e68cc3b --- /dev/null +++ b/src/puppet-web/firer.spec.js @@ -0,0 +1,81 @@ +/** + * + * 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 + * + */ +import { test } from 'ava' + +import Contact from '../contact' +import Message from '../message' + +import FriendRequest from './friend-request' +import Firer from './firer' + +test('Firer smoking test', t => { + + t.true(true, 'should be true') +}) + +test('Firer.checkFriendConfirm', t => { + const content = 'You have added 李卓桓 as your WeChat contact. Start chatting!' + let result + + result = Firer.checkFriendConfirm(content) + t.truthy(result, 'should be truthy for confirm msg') + + result = Firer.checkFriendConfirm('fsdfsdfasdfasdfadsa') + t.falsy(result, 'should be falsy for other msg') +}) + +test('Firer.checkRoomJoin', t => { + const contentList = [ + [ + `You've invited "李卓桓" to the group chat` + , `You've` + , `李卓桓` + ] + , [ + `You've invited "李卓桓.PreAngel、Bruce LEE" to the group chat` + , `You've` + , `李卓桓.PreAngel、Bruce LEE` + ] + , [ + `"李卓桓.PreAngel" invited "Bruce LEE" to the group chat` + , `李卓桓.PreAngel` + , `Bruce LEE` + ] + ] + + let result + contentList.forEach(([content, inviter, invitee]) => { + result = Firer.checkRoomJoin(content) + t.truthy(result, 'should check room join message right for ' + content) + t.is(result[0], invitee, 'should get invitee right') + t.is(result[1], inviter, 'should get inviter right') + }) + + result = Firer.checkRoomJoin('fsadfsadfsdfsdfs') + t.false(result, 'should get false if message is not expected') +}) + +test('Firer.checkRoomLeave', t => { + const data = [ + `You removed "Bruce LEE" from the group chat` + , `Bruce LEE` + ] + + let leaver + leaver = Firer.checkRoomLeave(data[0]) + t.truthy(leaver, 'should get leaver for leave message') + t.is(leaver, data[1], 'should get leaver name right') + + leaver = Firer.checkRoomLeave('fafdsfsdfafa') + t.false(leaver, 'should get false if message is not expected') +}) -- GitLab