Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.www.
wechaty
提交
a54953e5
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,体验更适合开发者的 AI 搜索 >>
提交
a54953e5
编写于
8月 14, 2018
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make sure the contact/room/message isReady()-ed before `findAll()` return (#1545)
上级
2579253a
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
139 addition
and
67 deletion
+139
-67
src/user/contact.spec.ts
src/user/contact.spec.ts
+55
-0
src/user/contact.ts
src/user/contact.ts
+11
-13
src/user/message.ts
src/user/message.ts
+9
-9
src/user/room.spec.ts
src/user/room.spec.ts
+55
-36
src/user/room.ts
src/user/room.ts
+9
-9
未找到文件。
src/user/contact.spec.ts
0 → 100755
浏览文件 @
a54953e5
#!/usr/bin/env ts-node
/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2018 Huan LI <zixia@zixia.net>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// tslint:disable:no-shadowed-variable
// tslint:disable:max-classes-per-file
import
test
from
'
blue-tape
'
import
sinon
from
'
sinon
'
import
{
PuppetMock
}
from
'
wechaty-puppet-mock
'
import
{
Wechaty
}
from
'
../wechaty
'
test
(
'
findAll()
'
,
async
t
=>
{
const
EXPECTED_CONTACT_ID
=
'
test-id
'
const
EXPECTED_CONTACT_NAME
=
'
test-name
'
const
EXPECTED_CONTACT_ID_LIST
=
[
EXPECTED_CONTACT_ID
]
const
sandbox
=
sinon
.
createSandbox
()
const
puppet
=
new
PuppetMock
()
const
wechaty
=
new
Wechaty
({
puppet
})
await
wechaty
.
start
()
sandbox
.
stub
(
puppet
,
'
contactSearch
'
).
resolves
(
EXPECTED_CONTACT_ID_LIST
)
sandbox
.
stub
(
puppet
,
'
contactPayload
'
).
callsFake
(
async
()
=>
{
await
new
Promise
(
r
=>
setImmediate
(
r
))
return
{
name
:
EXPECTED_CONTACT_NAME
,
}
})
const
contactList
=
await
wechaty
.
Contact
.
findAll
()
t
.
equal
(
contactList
.
length
,
1
,
'
should find 1 contact
'
)
t
.
equal
(
contactList
[
0
].
name
(),
EXPECTED_CONTACT_NAME
,
'
should get name from payload
'
)
await
wechaty
.
stop
()
})
src/user/contact.ts
浏览文件 @
a54953e5
...
...
@@ -201,14 +201,14 @@ export class Contact extends Accessory implements Sayable {
* const bot = new Wechaty()
* await bot.start()
* const contactList = await bot.Contact.findAll() // get the contact list of the bot
* const contactList = await bot.Contact.findAll({
name: 'ruirui'
}) // find allof the contacts whose name is 'ruirui'
* const contactList = await bot.Contact.findAll({
alias: 'lijiarui'
}) // find all of the contacts whose alias is 'lijiarui'
* const contactList = await bot.Contact.findAll({
name: 'ruirui'
}) // find allof the contacts whose name is 'ruirui'
* const contactList = await bot.Contact.findAll({
alias: 'lijiarui'
}) // find all of the contacts whose alias is 'lijiarui'
*/
public
static
async
findAll
<
T
extends
typeof
Contact
>
(
this
:
T
,
query
?
:
string
|
ContactQueryFilter
,
):
Promise
<
Array
<
T
[
'
prototype
'
]
>>
{
log
.
verbose
(
'
Contact
'
,
'
findAll(%s)
'
,
JSON
.
stringify
(
query
))
log
.
verbose
(
'
Contact
'
,
'
findAll(%s)
'
,
JSON
.
stringify
(
query
)
||
''
)
if
(
query
&&
Object
.
keys
(
query
).
length
!==
1
)
{
throw
new
Error
(
'
query only support one key. multi key support is not availble now.
'
)
...
...
@@ -221,7 +221,7 @@ export class Contact extends Accessory implements Sayable {
const
BATCH_SIZE
=
16
let
batchIndex
=
0
const
invalid
ContactId
:
string
[]
=
[]
const
invalid
Dict
:
{
[
id
:
string
]:
true
}
=
{}
while
(
batchIndex
*
BATCH_SIZE
<
contactList
.
length
)
{
const
batchContactList
=
contactList
.
slice
(
...
...
@@ -230,20 +230,18 @@ export class Contact extends Accessory implements Sayable {
)
await
Promise
.
all
(
batchContactList
.
map
(
c
=>
{
c
.
ready
()
c
=>
c
.
ready
()
.
catch
(
e
=>
{
log
.
error
(
'
Contact
'
,
'
findAll() ready() exception: %s
'
,
e
.
message
)
invalidContactId
.
push
(
c
.
id
)
})
},
invalidDict
[
c
.
id
]
=
true
}),
),
)
batchIndex
++
}
return
contactList
.
filter
(
contact
=>
!
(
contact
.
id
in
invalidContactId
)
)
return
contactList
.
filter
(
contact
=>
!
invalidDict
[
contact
.
id
]
)
}
catch
(
e
)
{
log
.
error
(
'
Contact
'
,
'
this.puppet.contactFindAll() rejected: %s
'
,
e
.
message
)
...
...
src/user/message.ts
浏览文件 @
a54953e5
...
...
@@ -110,7 +110,7 @@ export class Message extends Accessory implements Sayable {
this
:
T
,
userQuery
?
:
MessageUserQueryFilter
,
):
Promise
<
Array
<
T
[
'
prototype
'
]
>>
{
log
.
verbose
(
'
Message
'
,
'
findAll(%s)
'
,
JSON
.
stringify
(
userQuery
))
log
.
verbose
(
'
Message
'
,
'
findAll(%s)
'
,
JSON
.
stringify
(
userQuery
)
||
''
)
let
puppetQuery
:
undefined
|
MessageQueryFilter
...
...
@@ -124,22 +124,22 @@ export class Message extends Accessory implements Sayable {
}
}
const
invalidDict
:
{
[
id
:
string
]:
true
}
=
{}
try
{
const
MessageIdList
=
await
this
.
puppet
.
messageSearch
(
puppetQuery
)
const
messageList
=
MessageIdList
.
map
(
id
=>
this
.
load
(
id
))
await
Promise
.
all
(
messageList
.
map
(
message
=>
{
try
{
return
message
.
ready
()
}
catch
(
e
)
{
return
{}
as
any
}
},
message
=>
message
.
ready
()
.
catch
(
e
=>
{
log
.
warn
(
'
Room
'
,
'
findAll() message.ready() rejection: %s
'
,
e
)
invalidDict
[
message
.
id
]
=
true
})
),
)
return
messageList
return
messageList
.
filter
(
message
=>
!
invalidDict
[
message
.
id
])
}
catch
(
e
)
{
log
.
warn
(
'
Message
'
,
'
findAll() rejected: %s
'
,
e
.
message
)
...
...
src/user/room.spec.ts
浏览文件 @
a54953e5
// test('Room iterator for contact in it', async t => {
// // Mock
// const mockContactRoomRawPayload = (id: string) => {
// log.verbose('PuppeteerRoomTest', 'mockContactRawPayload(%s)', id)
// return new Promise(resolve => {
// if (id === ROOM_EXPECTED.id) {
// setImmediate(() => resolve(ROOM_RAW_PAYLOAD))
// } else if (id in CONTACT_RAW_PAYLOAD_DICT) {
// setImmediate(() => resolve(CONTACT_RAW_PAYLOAD_DICT[id]))
// } else {
// // ignore other ids
// setImmediate(() => resolve({ id }))
// }
// })
// }
// const sandbox = sinon.createSandbox()
// const puppet = new PuppetPuppeteer()
// sandbox.stub(puppet, 'contactRawPayload').callsFake(mockContactRoomRawPayload)
// sandbox.stub(puppet, 'roomRawPayload').callsFake(mockContactRoomRawPayload)
// const roomPayload = await puppet.roomPayload(ROOM_EXPECTED.id)
// const MEMBER_CONTACT_ID_LIST = ROOM_RAW_PAYLOAD.MemberList!.map(rawMember => rawMember.UserName)
// let n = 0
// for await (const memberContact of room) {
// t.ok(MEMBER_CONTACT_ID_LIST.includes(memberContact.id), 'should get one of the room member: ' + memberContact.id)
// n++
// }
// const memberList = await room.memberList()
// t.equal(n, memberList.length, 'should iterate all the members of the room')
// })
#!/usr/bin/env ts-node
/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2018 Huan LI <zixia@zixia.net>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// tslint:disable:no-shadowed-variable
// tslint:disable:max-classes-per-file
import
test
from
'
blue-tape
'
import
sinon
from
'
sinon
'
import
{
PuppetMock
}
from
'
wechaty-puppet-mock
'
import
{
Wechaty
}
from
'
../wechaty
'
test
(
'
findAll()
'
,
async
t
=>
{
const
EXPECTED_ROOM_ID
=
'
test-id
'
const
EXPECTED_ROOM_TOPIC
=
'
test-topic
'
const
EXPECTED_ROOM_ID_LIST
=
[
EXPECTED_ROOM_ID
]
const
sandbox
=
sinon
.
createSandbox
()
const
puppet
=
new
PuppetMock
()
const
wechaty
=
new
Wechaty
({
puppet
})
await
wechaty
.
start
()
sandbox
.
stub
(
puppet
,
'
roomSearch
'
).
resolves
(
EXPECTED_ROOM_ID_LIST
)
sandbox
.
stub
(
puppet
,
'
roomPayload
'
).
callsFake
(
async
()
=>
{
await
new
Promise
(
r
=>
setImmediate
(
r
))
return
{
topic
:
EXPECTED_ROOM_TOPIC
,
}
})
const
roomList
=
await
wechaty
.
Room
.
findAll
()
t
.
equal
(
roomList
.
length
,
1
,
'
should find 1 room
'
)
t
.
equal
(
await
roomList
[
0
].
topic
(),
EXPECTED_ROOM_TOPIC
,
'
should get topic from payload
'
)
await
wechaty
.
stop
()
})
src/user/room.ts
浏览文件 @
a54953e5
...
...
@@ -132,28 +132,28 @@ export class Room extends Accessory implements Sayable {
this
:
T
,
query
:
RoomQueryFilter
=
{
topic
:
/.*/
},
):
Promise
<
Array
<
T
[
'
prototype
'
]
>>
{
log
.
verbose
(
'
Room
'
,
'
findAll(%s)
'
,
JSON
.
stringify
(
query
))
log
.
verbose
(
'
Room
'
,
'
findAll(%s)
'
,
JSON
.
stringify
(
query
)
||
''
)
if
(
!
query
.
topic
)
{
throw
new
Error
(
'
topicFilter not found
'
)
}
const
invalidDict
:
{
[
id
:
string
]:
true
}
=
{}
try
{
const
roomIdList
=
await
this
.
puppet
.
roomSearch
(
query
)
const
roomList
=
roomIdList
.
map
(
id
=>
this
.
load
(
id
))
await
Promise
.
all
(
roomList
.
map
(
room
=>
{
try
{
return
room
.
ready
()
}
catch
(
e
)
{
return
{}
as
any
}
},
room
=>
room
.
ready
()
.
catch
(
e
=>
{
log
.
warn
(
'
Room
'
,
'
findAll() room.ready() rejection: %s
'
,
e
)
invalidDict
[
room
.
id
]
=
true
})
),
)
return
roomList
return
roomList
.
filter
(
room
=>
!
invalidDict
[
room
.
id
])
}
catch
(
e
)
{
log
.
verbose
(
'
Room
'
,
'
findAll() rejected: %s
'
,
e
.
message
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录