Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.www.
wechaty
提交
a4c6dd34
W
wechaty
项目概览
.www.
/
wechaty
与 Fork 源项目一致
Fork自
wechaty / wechaty
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
wechaty
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a4c6dd34
编写于
10月 06, 2016
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
#32 Room new event: `topic`
上级
5b7500c1
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
109 addition
and
14 deletion
+109
-14
README.md
README.md
+6
-0
example/room-bot.js
example/room-bot.js
+36
-6
src/puppet-web/event.js
src/puppet-web/event.js
+3
-2
src/puppet-web/firer.js
src/puppet-web/firer.js
+45
-1
src/puppet-web/firer.spec.js
src/puppet-web/firer.spec.js
+15
-0
src/room.js
src/room.js
+4
-5
未找到文件。
README.md
浏览文件 @
a4c6dd34
...
...
@@ -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[]>
...
...
example/room-bot.js
浏览文件 @
a4c6dd34
...
...
@@ -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
)
})
}
})
...
...
src/puppet-web/event.js
浏览文件 @
a4c6dd34
...
...
@@ -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
)
}
...
...
src/puppet-web/firer.js
浏览文件 @
a4c6dd34
...
...
@@ -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
src/puppet-web/firer.spec.js
浏览文件 @
a4c6dd34
...
...
@@ -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
'
)
})
src/room.js
浏览文件 @
a4c6dd34
...
...
@@ -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
(
contact
Id
List
,
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录