Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.Veneno.
wechaty
提交
04987d02
W
wechaty
项目概览
.Veneno.
/
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,发现更多精彩内容 >>
提交
04987d02
编写于
2月 07, 2017
作者:
ruiruibupt
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
1
上级
1b889120
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
53 addition
and
46 deletion
+53
-46
src/contact.ts
src/contact.ts
+3
-3
src/room.ts
src/room.ts
+21
-28
test/contact.spec.ts
test/contact.spec.ts
+10
-6
test/room.spec.ts
test/room.spec.ts
+19
-9
未找到文件。
src/contact.ts
浏览文件 @
04987d02
...
...
@@ -227,9 +227,9 @@ export class Contact implements Sayable {
public
static
async
findAll
(
queryArg
?:
ContactQueryFilter
|
{
remark
:
string
|
RegExp
}):
Promise
<
Contact
[]
>
{
let
query
:
ContactQueryFilter
if
(
queryArg
)
{
if
(
queryArg
[
0
]
===
'
remark
'
)
{
if
(
Object
.
keys
(
queryArg
)
[
0
]
===
'
remark
'
)
{
log
.
warn
(
'
Contact
'
,
'
Contact.findAll(remark:%s) DEPRECATED, use Contact.findAll(alias:%s) instead.
'
)
query
=
{
alias
:
queryArg
[
1
]}
query
=
{
alias
:
queryArg
[
'
remark
'
]}
}
else
{
query
=
queryArg
}
...
...
@@ -348,7 +348,7 @@ export class Contact implements Sayable {
* try to find a contact by filter: {name: string | RegExp}
*/
public
static
async
find
(
query
:
ContactQueryFilter
|
{
remark
:
string
|
RegExp
}):
Promise
<
Contact
>
{
log
.
verbose
(
'
Contact
'
,
'
find(%s)
'
,
query
)
log
.
verbose
(
'
Contact
'
,
'
find(%s)
'
,
JSON
.
stringify
(
query
)
)
const
contactList
=
await
Contact
.
findAll
(
query
)
if
(
!
contactList
||
!
contactList
.
length
)
{
...
...
src/room.ts
浏览文件 @
04987d02
...
...
@@ -25,12 +25,11 @@ type RoomObj = {
topic
:
string
ownerUin
:
number
memberList
:
Contact
[]
nameMap
:
Map
<
string
,
string
>
aliasMap
:
Map
<
string
,
string
>
roomAliasMap
:
Map
<
string
,
string
>
nameMap
:
Map
<
string
,
string
>
aliasMap
:
Map
<
string
,
string
>
}
type
NameType
=
'
n
ame
'
|
'
alias
'
|
'
roomA
lias
'
type
NameType
=
'
n
ick
'
|
'
a
lias
'
export
type
RoomRawMember
=
{
UserName
:
string
...
...
@@ -57,9 +56,8 @@ export type RoomQueryFilter = {
}
export
type
MemberQueryFilter
=
{
name
?:
string
alias
?:
string
roomAlias
?:
string
name
?:
string
alias
?:
string
}
export
class
Room
extends
EventEmitter
implements
Sayable
{
...
...
@@ -206,10 +204,9 @@ export class Room extends EventEmitter implements Sayable {
return
null
}
const
memberList
=
this
.
parseMemberList
(
rawObj
.
MemberList
)
const
nameMap
=
this
.
parseMap
(
rawObj
.
MemberList
,
'
name
'
)
const
aliasMap
=
this
.
parseMap
(
rawObj
.
MemberList
,
'
alias
'
)
const
roomAliasMap
=
this
.
parseMap
(
rawObj
.
MemberList
,
'
roomAlias
'
)
const
memberList
=
this
.
parseMemberList
(
rawObj
.
MemberList
)
const
nameMap
=
this
.
parseMap
(
rawObj
.
MemberList
,
'
nick
'
)
const
aliasMap
=
this
.
parseMap
(
rawObj
.
MemberList
,
'
alias
'
)
return
{
id
:
rawObj
.
UserName
,
...
...
@@ -220,7 +217,6 @@ export class Room extends EventEmitter implements Sayable {
memberList
,
nameMap
,
aliasMap
,
roomAliasMap
,
}
}
...
...
@@ -237,14 +233,11 @@ export class Room extends EventEmitter implements Sayable {
let
tmpName
:
string
let
contact
=
Contact
.
load
(
member
.
UserName
)
switch
(
parseContent
)
{
case
'
n
ame
'
:
tmpName
=
contact
.
name
()
case
'
n
ick
'
:
tmpName
=
contact
.
alias
()
||
contact
.
name
()
break
case
'
alias
'
:
tmpName
=
contact
.
alias
()
||
''
break
case
'
roomAlias
'
:
tmpName
=
member
.
DisplayName
tmpName
=
member
.
DisplayName
||
contact
.
name
()
break
default
:
throw
new
Error
(
'
parseMap failed, member not found
'
)
...
...
@@ -253,7 +246,8 @@ export class Room extends EventEmitter implements Sayable {
* ISSUE #64 emoji need to be striped
* ISSUE #104 never use remark name because sys group message will never use that
* @rui: Wrong for 'never use remark name because sys group message will never use that', see more in the latest comment in #104
* @rui: cannot use argument NickName because it mix real name and alias
* @rui: webwx's NickName here return contactAlias, if not set contactAlias, return name
* @rui: 2017-7-2 webwx's NickName just ruturn name, no contactAlias
*/
mapList
[
member
.
UserName
]
=
UtilLib
.
stripEmoji
(
tmpName
)
})
...
...
@@ -353,15 +347,15 @@ export class Room extends EventEmitter implements Sayable {
// should be deprecated
public
nick
(
contact
:
Contact
):
string
{
log
.
warn
(
'
Room
'
,
'
nick(Contact) DEPRECATED, use
name
(Contact) instead.
'
)
log
.
warn
(
'
Room
'
,
'
nick(Contact) DEPRECATED, use
alias
(Contact) instead.
'
)
return
this
.
alias
(
contact
)
}
public
alias
(
contact
:
Contact
):
string
{
if
(
!
this
.
obj
||
!
this
.
obj
.
nameMap
)
{
if
(
!
this
.
obj
)
{
return
''
}
return
this
.
obj
.
roomAliasMap
[
contact
.
id
]
||
this
.
obj
.
name
Map
[
contact
.
id
]
return
this
.
obj
.
alias
Map
[
contact
.
id
]
}
public
has
(
contact
:
Contact
):
boolean
{
...
...
@@ -397,8 +391,8 @@ export class Room extends EventEmitter implements Sayable {
}
/**
* find member
by `name` / `roomAlias` / `alias`
* when use member(name:string),
find all name by default
* find member
priority by `name`(contactAlias) / `alias`(roomAlias)
* when use member(name:string),
equals to member({name:string})
*/
public
member
(
filter
:
MemberQueryFilter
):
Contact
|
null
...
...
@@ -406,7 +400,7 @@ export class Room extends EventEmitter implements Sayable {
public
member
(
queryArg
:
MemberQueryFilter
|
string
):
Contact
|
null
{
if
(
typeof
queryArg
===
'
string
'
)
{
return
this
.
member
({
alias
:
queryArg
})
||
this
.
member
({
roomAlias
:
queryArg
})
||
this
.
member
({
name
:
queryArg
})
return
this
.
member
({
name
:
queryArg
})
}
log
.
silly
(
'
Room
'
,
'
member({ %s })
'
...
...
@@ -416,7 +410,7 @@ export class Room extends EventEmitter implements Sayable {
)
if
(
Object
.
keys
(
queryArg
).
length
!==
1
)
{
throw
new
Error
(
'
Room member find qu
a
ryArg only support one key. multi key support is not availble now.
'
)
throw
new
Error
(
'
Room member find qu
e
ryArg only support one key. multi key support is not availble now.
'
)
}
if
(
!
this
.
obj
||
!
this
.
obj
.
memberList
)
{
...
...
@@ -432,7 +426,6 @@ export class Room extends EventEmitter implements Sayable {
const
keyMap
=
{
name
:
'
nameMap
'
,
alias
:
'
aliasMap
'
,
roomAlias
:
'
roomAliasMap
'
}
filterKey
=
keyMap
[
filterKey
]
...
...
@@ -448,7 +441,7 @@ export class Room extends EventEmitter implements Sayable {
const
idList
=
Object
.
keys
(
filterMap
)
.
filter
(
k
=>
filterMap
[
k
]
===
filterValue
)
log
.
silly
(
'
Room
'
,
'
member() check %s
: %s
'
,
filterKey
,
filterValue
)
log
.
silly
(
'
Room
'
,
'
member() check %s
from %s: %s
'
,
filterValue
,
filterKey
,
JSON
.
stringify
(
filterMap
)
)
if
(
idList
.
length
)
{
return
Contact
.
load
(
idList
[
0
])
...
...
test/contact.spec.ts
浏览文件 @
04987d02
...
...
@@ -5,10 +5,8 @@
* https://github.com/wechaty/wechaty
*
*/
import
{
test
}
from
'
ava
'
import
{
Config
}
from
'
../src/config
'
import
{
test
}
from
'
ava
'
import
{
Config
}
from
'
../src/config
'
import
{
Contact
}
from
'
../src/contact
'
import
{
PuppetWeb
}
from
'
../src/puppet-web
'
...
...
@@ -17,8 +15,8 @@ Config.puppetInstance(new PuppetWeb())
test
(
'
Contact smoke testing
'
,
async
t
=>
{
/* tslint:disable:variable-name */
const
UserName
=
'
@0bb3e4dd746fdbd4a80546aef66f4085
'
const
NickName
=
'
Nick
Name
Test
'
const
RemarkName
=
'
Alias
Test
'
const
NickName
=
'
Nick
Name
Test
'
const
RemarkName
=
'
AliasTest
'
// Mock
const
mockContactGetter
=
function
(
id
)
{
...
...
@@ -45,4 +43,10 @@ test('Contact smoke testing', async t => {
const
s
=
r
.
toString
()
t
.
is
(
typeof
s
,
'
string
'
,
'
toString()
'
)
// const contact1 = await Contact.find({name: 'NickNameTest'})
// t.is(contact1.id, UserName, 'should find contact by name')
// const contact2 = await Contact.find({alias: 'AliasTest'})
// t.is(contact2.id, UserName, 'should find contact by alias')
})
test/room.spec.ts
浏览文件 @
04987d02
...
...
@@ -109,17 +109,17 @@ test('Room smoking test', async t => {
const
contact1
=
new
Contact
(
EXPECTED
.
memberId1
)
const
nick1
=
r
.
nick
(
contact1
)
t
.
is
(
nick1
,
EXPECTED
.
memberNick1
,
'
should get
nick1 from
roomAlias
'
)
t
.
is
(
nick1
,
EXPECTED
.
memberNick1
,
'
should get roomAlias
'
)
const
name1
=
r
.
alias
(
contact1
)
t
.
is
(
name1
,
EXPECTED
.
memberNick1
,
'
should get
name1 from
roomAlias
'
)
t
.
is
(
name1
,
EXPECTED
.
memberNick1
,
'
should get roomAlias
'
)
const
contact2
=
new
Contact
(
EXPECTED
.
memberId2
)
const
nick2
=
r
.
nick
(
contact2
)
t
.
is
(
nick2
,
EXPECTED
.
memberNick2
,
'
should get n
ick2 from name because there is no roomAlias,
'
)
t
.
is
(
nick2
,
EXPECTED
.
memberNick2
,
'
should get n
ame if not set roomAlias
'
)
const
name2
=
r
.
alias
(
contact2
)
t
.
is
(
name2
,
EXPECTED
.
memberNick2
,
'
should get n
ick2 from name because there is no roomAlias,
'
)
t
.
is
(
name2
,
EXPECTED
.
memberNick2
,
'
should get n
ame if not set roomAlias
'
)
t
.
truthy
(
r
.
has
(
contact1
),
'
should has contact1
'
)
const
noSuchContact
=
new
Contact
(
'
not exist id
'
)
...
...
@@ -134,12 +134,22 @@ test('Room smoking test', async t => {
const
contactA
=
r
.
member
(
EXPECTED
.
memberNick1
)
const
contactB
=
r
.
member
(
EXPECTED
.
memberNick2
)
const
contactC
=
r
.
member
(
EXPECTED
.
memberNick3
)
if
(
!
contactA
||
!
contactB
||
!
contactC
)
{
throw
new
Error
(
'
no a or b
'
)
const
contactD
=
r
.
member
({
alias
:
EXPECTED
.
memberNick1
})
if
(
contactA
)
{
throw
new
Error
(
`member(
${
EXPECTED
.
memberNick1
}
) cannot get contact by roomAlias`
)
}
t
.
is
(
contactA
.
id
,
EXPECTED
.
memberId1
,
'
should get the right id from nick 1, find member by name
'
)
t
.
is
(
contactB
.
id
,
EXPECTED
.
memberId2
,
'
should get the right id from nick 2, find member by roomAlias
'
)
t
.
is
(
contactC
.
id
,
EXPECTED
.
memberId3
,
'
should get the right id from nick 3, find member by alias
'
)
if
(
!
contactB
)
{
throw
new
Error
(
`member(
${
EXPECTED
.
memberNick2
}
) should get member by name when the contact does not have contactAlias`
)
}
if
(
!
contactC
)
{
throw
new
Error
(
`member(
${
EXPECTED
.
memberNick3
}
) should get member by name when the contact have contactAlias`
)
}
if
(
!
contactD
)
{
throw
new
Error
(
`member({alias:
${
EXPECTED
.
memberNick3
}
}) should get member by roomAlias`
)
}
t
.
is
(
contactB
.
id
,
EXPECTED
.
memberId2
,
`should get the right id from
${
EXPECTED
.
memberId2
}
, find member by when the contact does not have contactAlias`
)
t
.
is
(
contactC
.
id
,
EXPECTED
.
memberId3
,
`should get the right id from
${
EXPECTED
.
memberId3
}
, find member by when the contact have contactAlias`
)
t
.
is
(
contactD
.
id
,
EXPECTED
.
memberId1
,
`should get the right id from
${
EXPECTED
.
memberId1
}
, find member by roomAlias`
)
const
s
=
r
.
toString
()
t
.
is
(
typeof
s
,
'
string
'
,
'
toString()
'
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录