Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.Veneno.
wechaty
提交
e550671d
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,发现更多精彩内容 >>
提交
e550671d
编写于
4月 24, 2018
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
abstract contact
上级
89f94e55
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
57 addition
and
119 deletion
+57
-119
src/puppet-web/web-contact.ts
src/puppet-web/web-contact.ts
+55
-117
src/puppet/contact.ts
src/puppet/contact.ts
+2
-2
未找到文件。
src/puppet-web/web-contact.ts
浏览文件 @
e550671d
...
...
@@ -22,18 +22,27 @@ import {
Raven
,
Sayable
,
log
,
}
from
'
./config
'
}
from
'
../config
'
import
{
Contact
,
Gender
,
ContactQueryFilter
,
}
from
'
../puppet/contact
'
import
{
Message
,
MediaMessage
,
}
from
'
./message
'
import
Misc
from
'
./misc
'
import
PuppetAccessory
from
'
./puppet-accessory
'
// PuppetAccessory,
}
from
'
../puppet/
'
import
Misc
from
'
../misc
'
// import Wechaty from './wechaty'
import
PuppetWeb
from
'
./puppet-web/
'
import
PuppetWeb
from
'
./puppet-web
'
import
WebMessage
from
'
./web-message
'
export
interface
ContactObj
{
export
interface
Web
ContactObj
{
address
:
string
,
city
:
string
,
id
:
string
,
...
...
@@ -51,7 +60,7 @@ export interface ContactObj {
special
:
boolean
,
}
export
interface
ContactRawObj
{
export
interface
Web
ContactRawObj
{
Alias
:
string
,
City
:
string
,
NickName
:
string
,
...
...
@@ -68,27 +77,6 @@ export interface ContactRawObj {
VerifyFlag
:
number
,
}
/**
* Enum for Gender values.
*
* @enum {number}
* @property {number} Unknown - 0 for Unknown
* @property {number} Male - 1 for Male
* @property {number} Female - 2 for Female
*/
export
enum
Gender
{
Unknown
=
0
,
Male
=
1
,
Female
=
2
,
}
export
interface
ContactQueryFilter
{
name
?:
string
|
RegExp
,
alias
?:
string
|
RegExp
,
// remark is DEPRECATED
remark
?:
string
|
RegExp
,
}
/**
* @see https://github.com/Chatie/webwx-app-tracker/blob/7c59d35c6ea0cff38426a4c5c912a086c4c512b2/formatted/webwxApp.js#L3848
* @ignore
...
...
@@ -106,12 +94,11 @@ const specialContactList: string[] = [
* `Contact` is `Sayable`,
* [Examples/Contact-Bot]{@link https://github.com/Chatie/wechaty/blob/master/examples/contact-bot.ts}
*/
export
class
Contact
extends
PuppetAccessory
implements
Sayable
{
private
static
pool
=
new
Map
<
string
,
Contact
>
()
export
class
WebContact
extends
Contact
implements
Sayable
{
public
obj
:
ContactObj
|
null
public
obj
:
Web
ContactObj
|
null
// private dirtyObj: ContactObj | null
private
rawObj
:
ContactRawObj
private
rawObj
:
Web
ContactRawObj
/**
* @private
...
...
@@ -119,7 +106,7 @@ export class Contact extends PuppetAccessory implements Sayable {
constructor
(
public
readonly
id
:
string
,
)
{
super
()
super
(
id
)
log
.
silly
(
'
Contact
'
,
`constructor(
${
id
}
)`
)
if
(
typeof
id
!==
'
string
'
)
{
...
...
@@ -147,7 +134,7 @@ export class Contact extends PuppetAccessory implements Sayable {
/**
* @private
*/
private
parse
(
rawObj
:
ContactRawObj
):
ContactObj
|
null
{
private
parse
(
rawObj
:
WebContactRawObj
):
Web
ContactObj
|
null
{
if
(
!
rawObj
||
!
rawObj
.
UserName
)
{
log
.
warn
(
'
Contact
'
,
'
parse() got empty rawObj!
'
)
// config.puppetInstance().emit('error', e)
...
...
@@ -237,19 +224,9 @@ export class Contact extends PuppetAccessory implements Sayable {
* const contactList = await Contact.findAll({name: 'ruirui'}) // find allof the contacts whose name is 'ruirui'
* const contactList = await Contact.findAll({alias: 'lijiarui'}) // find all of the contacts whose alias is 'lijiarui'
*/
public
static
async
findAll
(
queryArg
?:
ContactQueryFilter
):
Promise
<
Contact
[]
>
{
let
query
:
ContactQueryFilter
if
(
queryArg
)
{
if
(
queryArg
.
remark
)
{
log
.
warn
(
'
Contact
'
,
'
Contact.findAll({remark:%s}) DEPRECATED, use Contact.findAll({alias:%s}) instead.
'
,
queryArg
.
remark
,
queryArg
.
remark
)
query
=
{
alias
:
queryArg
.
remark
}
}
else
{
query
=
queryArg
}
}
else
{
query
=
{
name
:
/.*/
}
}
public
static
async
findAll
(
query
:
ContactQueryFilter
=
{
name
:
/.*/
},
):
Promise
<
Contact
[]
>
{
// log.verbose('Cotnact', 'findAll({ name: %s })', query.name)
log
.
verbose
(
'
Cotnact
'
,
'
findAll({ %s })
'
,
Object
.
keys
(
query
)
...
...
@@ -294,8 +271,7 @@ export class Contact extends PuppetAccessory implements Sayable {
}
try
{
const
contactList
=
await
this
.
puppet
// config.puppetInstance()
.
contactFind
(
filterFunction
)
const
contactList
=
await
this
.
puppet
.
contactFindAll
(
query
)
await
Promise
.
all
(
contactList
.
map
(
c
=>
c
.
ready
()))
return
contactList
...
...
@@ -311,7 +287,7 @@ export class Contact extends PuppetAccessory implements Sayable {
*
* @param {string} text
*/
public
async
say
(
text
:
string
):
Promise
<
boolean
>
public
async
say
(
text
:
string
):
Promise
<
void
>
/**
* Send Media File to Contact
...
...
@@ -319,7 +295,7 @@ export class Contact extends PuppetAccessory implements Sayable {
* @param {MediaMessage} mediaMessage
* @memberof Contact
*/
public
async
say
(
mediaMessage
:
MediaMessage
):
Promise
<
boolean
>
public
async
say
(
mediaMessage
:
MediaMessage
):
Promise
<
void
>
/**
* Send Text or Media File to Contact.
...
...
@@ -331,7 +307,7 @@ export class Contact extends PuppetAccessory implements Sayable {
* await contact.say('welcome to wechaty!')
* await contact.say(new MediaMessage(__dirname + '/wechaty.png') // put the filePath you want to send here
*/
public
async
say
(
textOrMedia
:
string
|
MediaMessage
):
Promise
<
boolean
>
{
public
async
say
(
textOrMedia
:
string
|
MediaMessage
):
Promise
<
void
>
{
const
content
=
textOrMedia
instanceof
MediaMessage
?
textOrMedia
.
filename
()
:
textOrMedia
log
.
verbose
(
'
Contact
'
,
'
say(%s)
'
,
content
)
...
...
@@ -342,7 +318,7 @@ export class Contact extends PuppetAccessory implements Sayable {
}
let
m
if
(
typeof
textOrMedia
===
'
string
'
)
{
m
=
new
Message
()
m
=
new
Web
Message
()
m
.
puppet
=
this
.
puppet
m
.
content
(
textOrMedia
)
...
...
@@ -369,9 +345,9 @@ export class Contact extends PuppetAccessory implements Sayable {
public
alias
():
string
|
null
public
alias
(
newAlias
:
string
):
Promise
<
boolean
>
public
alias
(
newAlias
:
string
):
Promise
<
void
>
public
alias
(
empty
:
null
):
Promise
<
boolean
>
public
alias
(
empty
:
null
):
Promise
<
void
>
/**
* GET / SET / DELETE the alias for a contact
...
...
@@ -403,32 +379,29 @@ export class Contact extends PuppetAccessory implements Sayable {
* console.log(`failed to delete ${contact.name()}'s alias!`)
* }
*/
public
alias
(
newAlias
?:
string
|
null
):
Promise
<
boolean
>
|
string
|
null
{
public
alias
(
newAlias
?:
string
|
null
):
Promise
<
void
>
|
string
|
null
{
// log.silly('Contact', 'alias(%s)', newAlias || '')
if
(
newAlias
===
undefined
)
{
return
this
.
obj
&&
this
.
obj
.
alias
||
null
}
return
this
.
puppet
// config.puppetInstance()
.
contactAlias
(
this
,
newAlias
)
.
then
(
ret
=>
{
if
(
ret
)
{
if
(
this
.
obj
)
{
this
.
obj
.
alias
=
newAlias
}
else
{
log
.
error
(
'
Contact
'
,
'
alias() without this.obj?
'
)
}
}
else
{
log
.
warn
(
'
Contact
'
,
'
alias(%s) fail
'
,
newAlias
)
}
return
ret
})
.
catch
(
e
=>
{
log
.
error
(
'
Contact
'
,
'
alias(%s) rejected: %s
'
,
newAlias
,
e
.
message
)
Raven
.
captureException
(
e
)
return
false
// fail safe
})
const
future
=
this
.
puppet
.
contactAlias
(
this
,
newAlias
)
future
.
then
(()
=>
{
if
(
this
.
obj
)
{
this
.
obj
.
alias
=
newAlias
}
else
{
log
.
error
(
'
Contact
'
,
'
alias() without this.obj?
'
)
}
})
.
catch
(
e
=>
{
log
.
error
(
'
Contact
'
,
'
alias(%s) rejected: %s
'
,
newAlias
,
e
.
message
)
Raven
.
captureException
(
e
)
})
return
future
}
/**
...
...
@@ -546,9 +519,9 @@ export class Contact extends PuppetAccessory implements Sayable {
}
try
{
const
hostname
=
await
(
/* config.puppetInstance() */
this
.
puppet
as
PuppetWeb
).
hostname
()
const
hostname
=
await
(
this
.
puppet
as
any
as
PuppetWeb
).
hostname
()
const
avatarUrl
=
`http://
${
hostname
}${
this
.
obj
.
avatar
}
&type=big`
// add '&type=big' to get big image
const
cookies
=
await
(
/* config.puppetInstance() */
this
.
puppet
as
PuppetWeb
).
cookies
()
const
cookies
=
await
(
this
.
puppet
as
any
as
PuppetWeb
).
cookies
()
log
.
silly
(
'
Contact
'
,
'
avatar() url: %s
'
,
avatarUrl
)
return
Misc
.
urlStream
(
avatarUrl
,
cookies
)
...
...
@@ -591,7 +564,7 @@ export class Contact extends PuppetAccessory implements Sayable {
/**
* @private
*/
public
async
ready
(
contactGetter
?:
(
id
:
string
)
=>
Promise
<
ContactRawObj
>
):
Promise
<
this
>
{
public
async
ready
(
contactGetter
?:
(
id
:
string
)
=>
Promise
<
Web
ContactRawObj
>
):
Promise
<
this
>
{
// log.silly('Contact', 'ready(' + (contactGetter ? typeof contactGetter : '') + ')')
if
(
!
this
.
id
)
{
const
e
=
new
Error
(
'
ready() call on an un-inited contact
'
)
...
...
@@ -652,48 +625,13 @@ export class Contact extends PuppetAccessory implements Sayable {
* const isSelf = contact.self()
*/
public
self
():
boolean
{
const
userId
=
this
.
puppet
// config.puppetInstance()
.
userId
const
userId
=
this
.
puppet
.
user
!
.
id
const
selfId
=
this
.
id
if
(
!
userId
||
!
selfId
)
{
if
(
!
userId
||
!
this
.
id
)
{
throw
new
Error
(
'
no user or no self id
'
)
}
return
selfId
===
userId
}
/**
* @private
*/
// function should be deprecated
public
remark
(
newRemark
?:
string
|
null
):
Promise
<
boolean
>
|
string
|
null
{
log
.
warn
(
'
Contact
'
,
'
remark(%s) DEPRECATED, use alias(%s) instead.
'
)
log
.
silly
(
'
Contact
'
,
'
remark(%s)
'
,
newRemark
||
''
)
switch
(
newRemark
)
{
case
undefined
:
return
this
.
alias
()
case
null
:
return
this
.
alias
(
null
)
default
:
return
this
.
alias
(
newRemark
)
}
}
/**
* @private
*/
public
static
load
(
id
:
string
):
Contact
{
if
(
!
id
||
typeof
id
!==
'
string
'
)
{
throw
new
Error
(
'
Contact.load(): id not found
'
)
}
if
(
!
(
id
in
Contact
.
pool
))
{
Contact
.
pool
[
id
]
=
new
Contact
(
id
)
}
return
Contact
.
pool
[
id
]
return
this
.
id
===
userId
}
/**
...
...
src/puppet/contact.ts
浏览文件 @
e550671d
...
...
@@ -20,10 +20,10 @@
import
{
log
,
Sayable
,
}
from
'
./config
'
}
from
'
.
.
/config
'
import
{
MediaMessage
,
}
from
'
./message
'
}
from
'
./me
dia-me
ssage
'
import
PuppetAccessory
from
'
./puppet-accessory
'
/**
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录