Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_46011646
wechaty
提交
8863a8e5
W
wechaty
项目概览
weixin_46011646
/
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,发现更多精彩内容 >>
提交
8863a8e5
编写于
10月 06, 2016
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
#34
Contact.find && findAll
上级
abf5b258
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
119 addition
and
18 deletion
+119
-18
src/contact.js
src/contact.js
+71
-12
src/puppet-web/bridge.js
src/puppet-web/bridge.js
+8
-0
src/puppet-web/puppet-web.js
src/puppet-web/puppet-web.js
+15
-4
src/puppet-web/wechaty-bro.js
src/puppet-web/wechaty-bro.js
+25
-2
未找到文件。
src/contact.js
浏览文件 @
8863a8e5
...
...
@@ -88,16 +88,80 @@ class Contact {
Object
.
keys
(
this
.
obj
).
forEach
(
k
=>
console
.
error
(
`
${
k
}
:
${
this
.
obj
[
k
]}
`
))
}
static
find
()
{
return
new
Contact
(
'
-1
'
)
// private
static
_find
({
name
})
{
log
.
silly
(
'
Cotnact
'
,
'
_find(%s)
'
,
name
)
if
(
!
name
)
{
throw
new
Error
(
'
name not found
'
)
}
let
filterFunction
if
(
name
instanceof
RegExp
)
{
filterFunction
=
`c =>
${
name
.
toString
()}
.test(c)`
}
else
if
(
typeof
name
===
'
string
'
)
{
filterFunction
=
`c => c === '
${
name
}
'`
}
else
{
throw
new
Error
(
'
unsupport name type
'
)
}
return
Config
.
puppetInstance
()
.
contactFind
(
filterFunction
)
.
then
(
idList
=>
{
return
idList
})
.
catch
(
e
=>
{
log
.
error
(
'
Contact
'
,
'
_find() rejected: %s
'
,
e
.
message
)
throw
e
})
}
static
findAll
()
{
return
[
new
Contact
(
'
-2
'
)
,
new
Contact
(
'
-3
'
)
]
static
find
({
name
})
{
log
.
verbose
(
'
Contact
'
,
'
find(%s)
'
,
name
)
return
Contact
.
_find
({
name
})
.
then
(
idList
=>
{
if
(
!
idList
||
!
Array
.
isArray
(
idList
)){
throw
new
Error
(
'
_find return error
'
)
}
if
(
idList
.
length
<
1
)
{
return
null
}
const
id
=
idList
[
0
]
return
Contact
.
load
(
id
)
})
.
catch
(
e
=>
{
log
.
error
(
'
Contact
'
,
'
find() rejected: %s
'
,
e
.
message
)
return
null
// fail safe
})
}
static
findAll
({
name
})
{
log
.
verbose
(
'
Contact
'
,
'
findAll(%s)
'
,
name
)
return
Contact
.
_find
({
name
})
.
then
(
idList
=>
{
// console.log(idList)
if
(
!
idList
||
!
Array
.
isArray
(
idList
)){
throw
new
Error
(
'
_find return error
'
)
}
if
(
idList
.
length
<
1
)
{
return
[]
}
return
idList
.
map
(
i
=>
Contact
.
load
(
i
))
})
.
catch
(
e
=>
{
log
.
error
(
'
Contact
'
,
'
findAll() rejected: %s
'
,
e
.
message
)
return
[]
// fail safe
})
}
}
Contact
.
init
=
function
()
{
Contact
.
pool
=
{}
}
...
...
@@ -124,9 +188,4 @@ Contact.load = function(id) {
// return []
// }
// Contact.attach = function(puppet) {
// log.warn('Contact', '@deprecated attach() to %s', puppet && puppet.constructor.name)
// // Config.puppetInstance(puppet)
// }
module
.
exports
=
Contact
.
default
=
Contact
.
Contact
=
Contact
src/puppet-web/bridge.js
浏览文件 @
8863a8e5
...
...
@@ -143,6 +143,14 @@ class Bridge {
})
}
contactFind
(
filterFunction
)
{
return
this
.
proxyWechaty
(
'
contactFind
'
,
filterFunction
)
.
catch
(
e
=>
{
log
.
error
(
'
PuppetWebBridge
'
,
'
contactFind() exception: %s
'
,
e
.
message
)
throw
e
})
}
roomFind
(
filterFunction
)
{
return
this
.
proxyWechaty
(
'
roomFind
'
,
filterFunction
)
.
catch
(
e
=>
{
...
...
src/puppet-web/puppet-web.js
浏览文件 @
8863a8e5
...
...
@@ -348,6 +348,17 @@ class PuppetWeb extends Puppet {
})
}
contactFind
(
filterFunction
)
{
if
(
!
this
.
bridge
)
{
return
Promise
.
reject
(
new
Error
(
'
contactFind fail: no bridge(yet)!
'
))
}
return
this
.
bridge
.
contactFind
(
filterFunction
)
.
catch
(
e
=>
{
log
.
warn
(
'
PuppetWeb
'
,
'
contactFind(%s) rejected: %s
'
,
filterFunction
,
e
.
message
)
throw
e
})
}
roomFind
(
filterFunction
)
{
if
(
!
this
.
bridge
)
{
return
Promise
.
reject
(
new
Error
(
'
findRoom fail: no bridge(yet)!
'
))
...
...
@@ -400,7 +411,7 @@ class PuppetWeb extends Puppet {
})
}
roomCreate
(
contactList
)
{
roomCreate
(
contactList
,
topic
)
{
if
(
!
this
.
bridge
)
{
return
Promise
.
reject
(
new
Error
(
'
fail: no bridge(yet)!
'
))
}
...
...
@@ -409,11 +420,11 @@ class PuppetWeb extends Puppet {
throw
new
Error
(
'
contactList not found
'
)
}
const
contactIdList
=
contactList
.
map
(
c
=>
c
.
get
(
'
id
'
)
)
const
contactIdList
=
contactList
.
map
(
c
=>
c
.
id
)
return
this
.
bridge
.
roomCreate
(
contactIdList
)
return
this
.
bridge
.
roomCreate
(
contactIdList
,
topic
)
.
catch
(
e
=>
{
log
.
warn
(
'
PuppetWeb
'
,
'
roomCreate(%s
) rejected: %s
'
,
contact
,
e
.
message
)
log
.
warn
(
'
PuppetWeb
'
,
'
roomCreate(%s
, %s) rejected: %s
'
,
contactIdList
.
join
(,),
topic
,
e
.
message
)
throw
e
})
}
...
...
src/puppet-web/wechaty-bro.js
浏览文件 @
8863a8e5
...
...
@@ -451,6 +451,21 @@
:
null
}
function
contactFind
(
filterFunction
)
{
var
contactFactory
=
WechatyBro
.
glue
.
contactFactory
var
match
if
(
!
filterFunction
)
{
match
=
function
()
{
return
true
}
}
else
{
match
=
eval
(
filterFunction
)
}
// log(match.toString())
return
contactFactory
.
getAllFriendContact
()
.
filter
(
r
=>
match
(
r
.
NickName
))
.
map
(
r
=>
r
.
UserName
)
}
function
roomFind
(
filterFunction
)
{
var
contactFactory
=
WechatyBro
.
glue
.
contactFactory
...
...
@@ -484,20 +499,25 @@
return
chatroomFactory
.
modTopic
(
ChatRoomName
,
topic
)
}
function
roomCreate
(
UserNameList
)
{
function
roomCreate
(
UserNameList
,
topic
)
{
const
UserNameListArg
=
UserNameList
.
map
(
n
=>
{
return
{
UserName
:
n
}
})
const
chatroomFactory
=
WechatyBro
.
glue
.
chatroomFactory
const
state
=
WechatyBro
.
glue
.
state
chatroomFactory
.
create
(
UserNameListArg
)
.
then
(
r
=>
{
if
(
r
.
BaseResponse
&&
0
==
r
.
BaseResponse
.
Ret
||
-
2013
==
e
.
BaseResponse
.
Ret
)
{
state
.
go
(
'
chat
'
,
{
userName
:
r
.
ChatRoomName
})
// BE CAREFUL: key name is userName, not UserName! 20161001
if
(
topic
)
{
roomModTopic
(
r
.
ChatRoomName
,
topic
)
}
}
return
r
.
ChatRoomName
})
.
catch
(
e
=>
{
// TBD
console
.
log
(
e
)
log
(
e
)
})
return
'
no callback (yet)
'
}
...
...
@@ -607,6 +627,9 @@
,
getUserName
:
getUserName
,
getMsgImg
:
getMsgImg
// for Wechaty Contact Class
,
contactFind
// for Wechaty Room Class
,
roomFind
,
roomCreate
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录