From a4c6dd34c66e2c3b00f461519911976a45a2337f Mon Sep 17 00:00:00 2001 From: "Zhuohuan LI (CARPE DIEM)" Date: Thu, 6 Oct 2016 15:35:34 +0800 Subject: [PATCH] #32 Room new event: `topic` --- README.md | 6 +++++ example/room-bot.js | 42 +++++++++++++++++++++++++++----- src/puppet-web/event.js | 5 ++-- src/puppet-web/firer.js | 46 +++++++++++++++++++++++++++++++++++- src/puppet-web/firer.spec.js | 15 ++++++++++++ src/room.js | 9 ++++--- 6 files changed, 109 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 2c5d5322..2dfa36aa 100644 --- a/README.md +++ b/README.md @@ -555,6 +555,12 @@ room.on('join', (invitee, inviter) => { Room.on('leave', (leaver) => void) ``` +### Event: `topic` + +```typescript +Room.on('topic', (topic, oldTopic, changer) => void) +``` + ### static Room.find(query: Query): Promise ### static Room.findAll(query: Query): Promise diff --git a/example/room-bot.js b/example/room-bot.js index 70137946..2d359065 100644 --- a/example/room-bot.js +++ b/example/room-bot.js @@ -44,6 +44,12 @@ bot setTimeout(_ => { Room.find({ name: /^ding/i }) .then(room => { + + if (!room) { + log.warn('Bot', 'there is no room named ding(yet)') + return + } + log.info('Bot', 'start monitor "ding" room join/leave event') room.on('join', (invitee, inviter) => { try { @@ -74,9 +80,16 @@ bot room.on('leave', (leaver) => { log.info('Bot', 'room event: %s leave, byebye', leaver.name()) }) + room.on('topic', (topic, oldTopic, changer) => { + log.info('Bot', 'room event: topic changed from %s to %s by member %s' + , oldTopic + , topic + , changer.name() + ) + }) }) .catch(e => { - log.warn('Bot', 'there is no room named ding(yet)') + log.warn('Bot', 'Room.find rejected: %s', e.stack) }) }, 3000) }) @@ -93,6 +106,13 @@ bot , leaver.name() ) }) +.on('room-topic', (room, topic, oldTopic, changer) => { + log.info('Bot', 'room-topic event: Room %s change topic to %s by member %s' + , oldTopic + , topic + , changer.name() + ) +}) .on('logout' , user => log.info('Bot', `${user.name()} logouted`)) .on('error' , e => log.info('Bot', 'error: %s', e)) .on('message', m => { @@ -132,19 +152,29 @@ bot log.error('Bot', 'room.add() exception: %s', e.stack) }) } else { + log.info('Bot', 'ding room not found, try to created a new one') Contact.find({ name: 'Bruce LEE' }) .then(c => { - Room.create([contact, c], 'ding') - .then(_ => { - log.info('Bot', 'ding room not found, created one') + if (!c) { + log.warn('Bot', 'Contact.find found nobody') + return + } + c.ready().then(_ => log.info('Bot', 'Contact.find succ, got: %s', c.name())) + const contactList = [contact, c] + log.verbose('Bot', 'contactList: %s', contactList.join(',')) + + Room.create(contactList, 'ding') + .then(r => { + log.info('Bot', 'new ding room created: %s', r) + console.log(r) }) .catch(e => { - log.error('Bot', 'ding room create fail: %s', e.stack) + log.error('Bot', 'new ding room create fail: %s', e.stack) }) }) .catch(e => { - log.error('Bot', 'Contact.find not found') + log.error('Bot', 'Contact.find not found: %s', e.stack) }) } }) diff --git a/src/puppet-web/event.js b/src/puppet-web/event.js index 6d418e28..0080ff61 100644 --- a/src/puppet-web/event.js +++ b/src/puppet-web/event.js @@ -350,8 +350,9 @@ function onServerMessage(data) { case Message.Type.SYS: if (m.room()) { - Firer.fireRoomJoin.call(this, m) - Firer.fireRoomLeave.call(this, m) + Firer.fireRoomJoin.call(this , m) + Firer.fireRoomLeave.call(this , m) + Firer.fireRoomTopic.call(this , m) } else { Firer.fireFriendConfirm.call(this, m) } diff --git a/src/puppet-web/firer.js b/src/puppet-web/firer.js index 03dcbad1..0cc100f7 100644 --- a/src/puppet-web/firer.js +++ b/src/puppet-web/firer.js @@ -34,11 +34,16 @@ const PuppetWebFirer = { , fireRoomJoin , fireRoomLeave + , fireRoomTopic - // for testing + /** + * for testing + */ , checkFriendConfirm + , checkRoomJoin , checkRoomLeave + , checkRoomTopic } const regexConfig = { @@ -46,6 +51,7 @@ const regexConfig = { , roomJoin: /^"?(.+?)"? invited "(.+)" to the group chat$/ , roomLeave: /^You removed "(.+)" from the group chat$/ + , roomTopic: /^"?(.+?)"? changed the group name to "(.+)"$/ } function fireFriendRequest(m) { @@ -205,4 +211,42 @@ function fireRoomLeave(m) { }) } +function checkRoomTopic(content) { + const re = regexConfig.roomTopic + + const found = content.match(re) + if (!found) { + return false + } + const [_, changer, topic] = found + return [topic, changer] +} + +function fireRoomTopic(m) { + const result = checkRoomTopic(m.content()) + if (!result) { + return + } + + const [topic, changer] = result + const room = m.room() + const oldTopic = room.topic() + + changerContact = room.member(changer) + + if (!changerContact) { + log.error('PuppetWebFirer', 'fireRoomTopic() changer contact not found for %s', changer) + return + } + + co.call(this, function* () { + yield changerContact.ready() + this.emit('room-topic', room, topic, oldTopic, changerContact) + room.emit('topic', topic, oldTopic, changerContact) + room.refresh() + }).catch(e => { + log.error('PuppetWebFirer', 'fireRoomTopic() co exception: %s', e.stack) + }) +} + module.exports = PuppetWebFirer diff --git a/src/puppet-web/firer.spec.js b/src/puppet-web/firer.spec.js index 3e68cc3b..25afd177 100755 --- a/src/puppet-web/firer.spec.js +++ b/src/puppet-web/firer.spec.js @@ -79,3 +79,18 @@ test('Firer.checkRoomLeave', t => { leaver = Firer.checkRoomLeave('fafdsfsdfafa') t.false(leaver, 'should get false if message is not expected') }) + +test('Firer.checkRoomTopic', t => { + const data = [ + `"李卓桓.PreAngel" changed the group name to "ding"` + , `李卓桓.PreAngel` + , `ding` + ] + + const result = Firer.checkRoomTopic(data[0]) + t.truthy(result, 'should check topic right') + + const [topic, changer] = result + t.is(topic , data[2], 'should get right topic') + t.is(changer, data[1], 'should get right changer') +}) diff --git a/src/room.js b/src/room.js index c40f5192..84eff860 100644 --- a/src/room.js +++ b/src/room.js @@ -161,7 +161,7 @@ class Room extends EventEmitter{ } topic(newTopic) { - log.verbose('Room', 'topic(%s)', newTopic) + log.verbose('Room', 'topic(%s)', newTopic ? newTopic : '') if (newTopic) { Config.puppetInstance().roomTopic(this, newTopic) @@ -233,10 +233,8 @@ class Room extends EventEmitter{ throw new Error('contactList not found') } - const contactIdList = contactList.map(c => c.id) - return Config.puppetInstance() - .roomCreate(contactIdList, topic) + .roomCreate(contactList, topic) } // private @@ -287,6 +285,7 @@ class Room extends EventEmitter{ .catch(e => { log.error('Room', 'find() rejected: %s', e.message) return null // fail safe + // throw e }) } @@ -307,7 +306,7 @@ class Room extends EventEmitter{ return idList.map(i => Room.load(i)) }) .catch(e => { - log.error('Room', 'findAll() rejected: %s', e.message) + // log.error('Room', 'findAll() rejected: %s', e.message) return [] // fail safe }) } -- GitLab