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

#32 Room new event: `topic`

上级 5b7500c1
......@@ -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<Room|null>
### static Room.findAll(query: Query): Promise<Room[]>
......
......@@ -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)
})
}
})
......
......@@ -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)
}
......
......@@ -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
......@@ -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')
})
......@@ -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
})
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册