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

#32 add Firer Class to match message with event

上级 2546084f
......@@ -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
/**
*
* 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
/**
*
* 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')
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册