Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.Veneno.
wechaty
提交
e5a1c459
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,发现更多精彩内容 >>
提交
e5a1c459
编写于
4月 25, 2018
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
connect to abstract puppet
上级
2758f432
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
92 addition
and
65 deletion
+92
-65
src/puppet-web/puppet-web.ts
src/puppet-web/puppet-web.ts
+92
-65
未找到文件。
src/puppet-web/puppet-web.ts
浏览文件 @
e5a1c459
...
...
@@ -16,6 +16,9 @@
* limitations under the License.
*
*/
import
*
as
request
from
'
request
'
import
*
as
bl
from
'
bl
'
import
cloneClass
from
'
clone-class
'
import
{
ThrottleQueue
,
...
...
@@ -25,23 +28,25 @@ import {
WatchdogFood
,
}
from
'
watchdog
'
import
{
Contact
,
Message
,
MediaMessage
,
Puppet
,
PuppetOptions
,
Room
,
RoomQueryFilter
,
ScanData
,
}
from
'
../puppet/
'
import
{
config
,
log
,
Raven
,
}
from
'
../config
'
import
Contact
from
'
../contact
'
import
{
Message
,
MediaMessage
,
}
from
'
../message
'
import
Profile
from
'
../profile
'
import
{
Puppet
,
PuppetOptions
,
ScanData
,
}
from
'
../puppet
'
import
Room
from
'
../room
'
import
Misc
from
'
../misc
'
import
{
Bridge
,
...
...
@@ -53,10 +58,14 @@ import {
MediaData
,
MsgRawObj
,
MediaType
,
}
from
'
./schema
'
}
from
'
./schema
'
import
*
as
request
from
'
request
'
import
*
as
bl
from
'
bl
'
import
WebContact
from
'
./web-contact
'
import
{
// WebMessage,
WebMediaMessage
,
}
from
'
./web-message
'
import
WebRoom
from
'
./web-room
'
export
type
PuppetFoodType
=
'
scan
'
|
'
ding
'
export
type
ScanFoodType
=
'
scan
'
|
'
login
'
|
'
logout
'
...
...
@@ -65,7 +74,6 @@ export class PuppetWeb extends Puppet {
public
bridge
:
Bridge
public
scanInfo
:
ScanData
|
null
public
puppetWatchdog
:
Watchdog
<
PuppetFoodType
>
public
scanWatchdog
:
Watchdog
<
ScanFoodType
>
private
fileId
:
number
...
...
@@ -76,8 +84,8 @@ export class PuppetWeb extends Puppet {
super
(
options
)
this
.
fileId
=
0
const
PUPPET_TIMEOUT
=
1
*
60
*
1000
// 1 minute
this
.
puppetW
atchdog
=
new
Watchdog
<
PuppetFoodType
>
(
PUPPET_TIMEOUT
,
'
PuppetWeb
'
)
//
const PUPPET_TIMEOUT = 1 * 60 * 1000 // 1 minute
// this.w
atchdog = new Watchdog<PuppetFoodType>(PUPPET_TIMEOUT, 'PuppetWeb')
const
SCAN_TIMEOUT
=
2
*
60
*
1000
// 2 minutes
this
.
scanWatchdog
=
new
Watchdog
<
ScanFoodType
>
(
SCAN_TIMEOUT
,
'
Scan
'
)
...
...
@@ -93,7 +101,7 @@ export class PuppetWeb extends Puppet {
this
.
state
.
on
(
'
pending
'
)
try
{
this
.
initWatchdog
ForPuppet
()
this
.
initWatchdog
()
this
.
initWatchdogForScan
()
this
.
bridge
=
await
this
.
initBridge
(
this
.
options
.
profile
)
...
...
@@ -133,23 +141,22 @@ export class PuppetWeb extends Puppet {
}
}
public
initWatchdog
ForPuppet
():
void
{
public
initWatchdog
():
void
{
log
.
verbose
(
'
PuppetWeb
'
,
'
initWatchdogForPuppet()
'
)
const
puppet
=
this
const
dog
=
this
.
puppetWatchdog
// clean the dog because this could be re-inited
dog
.
removeAllListeners
()
this
.
watch
dog
.
removeAllListeners
()
puppet
.
on
(
'
watchdog
'
,
food
=>
dog
.
feed
(
food
))
dog
.
on
(
'
feed
'
,
food
=>
{
puppet
.
on
(
'
watchdog
'
,
food
=>
this
.
watch
dog
.
feed
(
food
))
this
.
watch
dog
.
on
(
'
feed
'
,
food
=>
{
log
.
silly
(
'
PuppetWeb
'
,
'
initWatchdogForPuppet() dog.on(feed, food={type=%s, data=%s})
'
,
food
.
type
,
food
.
data
)
// feed the dog, heartbeat the puppet.
puppet
.
emit
(
'
heartbeat
'
,
food
.
data
)
})
dog
.
on
(
'
reset
'
,
async
(
food
,
timeout
)
=>
{
this
.
watch
dog
.
on
(
'
reset
'
,
async
(
food
,
timeout
)
=>
{
log
.
warn
(
'
PuppetWeb
'
,
'
initWatchdogForPuppet() dog.on(reset) last food:%s, timeout:%s
'
,
food
.
data
,
timeout
)
try
{
...
...
@@ -227,7 +234,7 @@ export class PuppetWeb extends Puppet {
}
log
.
verbose
(
'
PuppetWeb
'
,
'
quit() make watchdog sleep before do quit
'
)
this
.
puppetW
atchdog
.
sleep
()
this
.
w
atchdog
.
sleep
()
this
.
scanWatchdog
.
sleep
()
this
.
state
.
off
(
'
pending
'
)
...
...
@@ -318,7 +325,7 @@ export class PuppetWeb extends Puppet {
}
}
private
async
uploadMedia
(
mediaMessage
:
MediaMessage
,
toUserName
:
string
):
Promise
<
MediaData
>
{
private
async
uploadMedia
(
mediaMessage
:
Web
MediaMessage
,
toUserName
:
string
):
Promise
<
MediaData
>
{
if
(
!
mediaMessage
)
throw
new
Error
(
'
require mediaMessage
'
)
...
...
@@ -538,7 +545,7 @@ export class PuppetWeb extends Puppet {
return
Object
.
assign
(
mediaData
,
{
MediaId
:
mediaId
as
string
})
}
public
async
sendMedia
(
message
:
MediaMessage
):
Promise
<
boolean
>
{
public
async
sendMedia
(
message
:
Web
MediaMessage
):
Promise
<
boolean
>
{
const
to
=
message
.
to
()
const
room
=
message
.
room
()
...
...
@@ -603,7 +610,7 @@ export class PuppetWeb extends Puppet {
* TODO: Test this function if it could work...
*/
// public async forward(baseData: MsgRawObj, patchData: MsgRawObj): Promise<boolean> {
public
async
forward
(
message
:
MediaMessage
,
sendTo
:
Contact
|
Room
):
Promise
<
boolean
>
{
public
async
forward
(
message
:
WebMediaMessage
,
sendTo
:
Contact
|
WebRoom
):
Promise
<
void
>
{
log
.
silly
(
'
PuppetWeb
'
,
'
forward() to: %s, message: %s)
'
,
sendTo
,
message
.
filename
(),
...
...
@@ -633,16 +640,16 @@ export class PuppetWeb extends Puppet {
if
(
m
.
FileSize
>=
largeFileSize
&&
!
m
.
Signature
)
{
// if has RawObj.Signature, can forward the 25Mb+ file
log
.
warn
(
'
MediaMessage
'
,
'
forward() Due to webWx restrictions, more than 25MB of files can not be downloaded and can not be forwarded.
'
)
return
false
throw
new
Error
(
'
forward() Due to webWx restrictions, more than 25MB of files can not be downloaded and can not be forwarded.
'
)
}
newMsg
.
FromUserName
=
this
.
user
I
d
||
''
newMsg
.
FromUserName
=
this
.
user
.
i
d
||
''
newMsg
.
isTranspond
=
true
newMsg
.
MsgIdBeforeTranspond
=
m
.
MsgIdBeforeTranspond
||
m
.
MsgId
newMsg
.
MMSourceMsgId
=
m
.
MsgId
// In room msg, the content prefix sender:, need to be removed, otherwise the forwarded sender will display the source message sender, causing self () to determine the error
newMsg
.
Content
=
Misc
.
unescapeHtml
(
m
.
Content
.
replace
(
/^@
\w
+:<br
\/
>/
,
''
)).
replace
(
/^
[\w\-]
+:<br
\/
>/
,
''
)
newMsg
.
MMIsChatRoom
=
sendTo
instanceof
Room
?
true
:
false
newMsg
.
MMIsChatRoom
=
sendTo
instanceof
Web
Room
?
true
:
false
// The following parameters need to be overridden after calling createMessage()
...
...
@@ -658,18 +665,19 @@ export class PuppetWeb extends Puppet {
const
baseData
=
m
const
patchData
=
newMsg
let
ret
=
false
try
{
ret
=
await
this
.
bridge
.
forward
(
baseData
,
patchData
)
const
ret
=
await
this
.
bridge
.
forward
(
baseData
,
patchData
)
if
(
!
ret
)
{
throw
new
Error
(
'
forward failed
'
)
}
}
catch
(
e
)
{
log
.
error
(
'
PuppetWeb
'
,
'
forward() exception: %s
'
,
e
.
message
)
Raven
.
captureException
(
e
)
throw
e
}
return
ret
}
public
async
send
(
message
:
Message
|
MediaMessage
):
Promise
<
boolean
>
{
public
async
send
(
message
:
Message
|
MediaMessage
):
Promise
<
void
>
{
const
to
=
message
.
to
()
const
room
=
message
.
room
()
...
...
@@ -684,10 +692,11 @@ export class PuppetWeb extends Puppet {
destinationId
=
to
.
id
}
let
ret
=
false
if
(
message
instanceof
MediaMessage
)
{
ret
=
await
this
.
sendMedia
(
message
)
if
(
message
instanceof
WebMediaMessage
)
{
const
ret
=
await
this
.
sendMedia
(
message
)
if
(
!
ret
)
{
throw
new
Error
(
'
sendMedia fail
'
)
}
}
else
{
const
content
=
message
.
content
()
...
...
@@ -697,34 +706,33 @@ export class PuppetWeb extends Puppet {
)
try
{
ret
=
await
this
.
bridge
.
send
(
destinationId
,
content
)
await
this
.
bridge
.
send
(
destinationId
,
content
)
}
catch
(
e
)
{
log
.
error
(
'
PuppetWeb
'
,
'
send() exception: %s
'
,
e
.
message
)
Raven
.
captureException
(
e
)
throw
e
}
}
return
ret
}
/**
* Bot say...
* send to `self` for notice / log
*/
public
async
say
(
content
:
string
):
Promise
<
boolean
>
{
public
async
say
(
content
:
string
):
Promise
<
void
>
{
if
(
!
this
.
logonoff
())
{
throw
new
Error
(
'
can not say before login
'
)
}
if
(
!
content
)
{
log
.
warn
(
'
PuppetWeb
'
,
'
say(%s) can not say nothing
'
,
content
)
return
false
return
}
if
(
!
this
.
user
)
{
log
.
warn
(
'
PuppetWeb
'
,
'
say(%s) can not say because no user
'
,
content
)
this
.
emit
(
'
error
'
,
new
Error
(
'
no this.user for PuppetWeb
'
))
return
false
return
}
// const m = new Message()
...
...
@@ -741,8 +749,8 @@ export class PuppetWeb extends Puppet {
public
async
logout
():
Promise
<
void
>
{
log
.
verbose
(
'
PuppetWeb
'
,
'
logout()
'
)
const
data
=
this
.
user
||
this
.
userId
||
''
this
.
user
Id
=
this
.
user
=
undefined
const
data
=
this
.
user
||
''
this
.
user
=
undefined
try
{
await
this
.
bridge
.
logout
()
...
...
@@ -774,16 +782,15 @@ export class PuppetWeb extends Puppet {
}
}
public
async
contactAlias
(
contact
:
Contact
,
remark
:
string
|
null
):
Promise
<
boolean
>
{
public
async
contactAlias
(
contact
:
Contact
,
remark
:
string
|
null
):
Promise
<
void
>
{
try
{
const
ret
=
await
this
.
bridge
.
contactRemark
(
contact
.
id
,
remark
)
if
(
!
ret
)
{
log
.
warn
(
'
PuppetWeb
'
,
'
contactRemark(%s, %s) bridge.contact
Remark
() return false
'
,
log
.
warn
(
'
PuppetWeb
'
,
'
contactRemark(%s, %s) bridge.contact
Alias
() return false
'
,
contact
.
id
,
remark
,
)
)
throw
new
Error
(
'
bridge.contactAlias fail
'
)
}
return
ret
}
catch
(
e
)
{
log
.
warn
(
'
PuppetWeb
'
,
'
contactRemark(%s, %s) rejected: %s
'
,
contact
.
id
,
remark
,
e
.
message
)
Raven
.
captureException
(
e
)
...
...
@@ -791,7 +798,7 @@ export class PuppetWeb extends Puppet {
}
}
public
async
contactFind
(
filterFunc
:
string
):
Promise
<
Contact
[]
>
{
public
async
contactFind
All
(
filterFunc
:
string
):
Promise
<
Contact
[]
>
{
try
{
const
idList
=
await
this
.
bridge
.
contactFind
(
filterFunc
)
return
idList
.
map
(
id
=>
{
...
...
@@ -806,26 +813,46 @@ export class PuppetWeb extends Puppet {
}
}
public
async
roomFind
(
filterFunc
:
string
):
Promise
<
Room
[]
>
{
public
async
roomFindAll
(
query
:
RoomQueryFilter
=
{
topic
:
/.*/
},
):
Promise
<
Room
[]
>
{
let
topicFilter
=
query
.
topic
if
(
!
topicFilter
)
{
throw
new
Error
(
'
topicFilter not found
'
)
}
let
filterFunction
:
string
if
(
topicFilter
instanceof
RegExp
)
{
filterFunction
=
`(function (c) { return
${
topicFilter
.
toString
()}
.test(c) })`
}
else
if
(
typeof
topicFilter
===
'
string
'
)
{
topicFilter
=
topicFilter
.
replace
(
/'/g
,
'
\\\'
'
)
filterFunction
=
`(function (c) { return c === '
${
topicFilter
}
' })`
}
else
{
throw
new
Error
(
'
unsupport topic type
'
)
}
try
{
const
idList
=
await
this
.
bridge
.
roomFind
(
filterFunc
)
const
idList
=
await
this
.
bridge
.
roomFind
(
filterFunc
tion
)
return
idList
.
map
(
id
=>
{
const
r
=
Room
.
load
(
id
)
const
r
=
Web
Room
.
load
(
id
)
r
.
puppet
=
this
return
r
})
}
catch
(
e
)
{
log
.
warn
(
'
PuppetWeb
'
,
'
roomFind(%s) rejected: %s
'
,
filterFunc
,
e
.
message
)
log
.
warn
(
'
PuppetWeb
'
,
'
roomFind(%s) rejected: %s
'
,
filterFunc
tion
,
e
.
message
)
Raven
.
captureException
(
e
)
throw
e
}
}
public
async
roomDel
(
room
:
Room
,
contact
:
Contact
):
Promise
<
number
>
{
public
async
roomDel
(
room
:
WebRoom
,
contact
:
Contact
):
Promise
<
void
>
{
const
roomId
=
room
.
id
const
contactId
=
contact
.
id
try
{
return
await
this
.
bridge
.
roomDelMember
(
roomId
,
contactId
)
await
this
.
bridge
.
roomDelMember
(
roomId
,
contactId
)
}
catch
(
e
)
{
log
.
warn
(
'
PuppetWeb
'
,
'
roomDelMember(%s, %d) rejected: %s
'
,
roomId
,
contactId
,
e
.
message
)
Raven
.
captureException
(
e
)
...
...
@@ -833,11 +860,11 @@ export class PuppetWeb extends Puppet {
}
}
public
async
roomAdd
(
room
:
Room
,
contact
:
Contact
):
Promise
<
number
>
{
public
async
roomAdd
(
room
:
WebRoom
,
contact
:
Contact
):
Promise
<
void
>
{
const
roomId
=
room
.
id
const
contactId
=
contact
.
id
try
{
return
await
this
.
bridge
.
roomAddMember
(
roomId
,
contactId
)
await
this
.
bridge
.
roomAddMember
(
roomId
,
contactId
)
}
catch
(
e
)
{
log
.
warn
(
'
PuppetWeb
'
,
'
roomAddMember(%s) rejected: %s
'
,
contact
,
e
.
message
)
Raven
.
captureException
(
e
)
...
...
@@ -845,7 +872,7 @@ export class PuppetWeb extends Puppet {
}
}
public
async
roomTopic
(
room
:
Room
,
topic
:
string
):
Promise
<
string
>
{
public
async
roomTopic
(
room
:
Web
Room
,
topic
:
string
):
Promise
<
string
>
{
if
(
!
room
||
typeof
topic
===
'
undefined
'
)
{
return
Promise
.
reject
(
new
Error
(
'
room or topic not found
'
))
}
...
...
@@ -872,7 +899,7 @@ export class PuppetWeb extends Puppet {
if
(
!
roomId
)
{
throw
new
Error
(
'
PuppetWeb.roomCreate() roomId "
'
+
roomId
+
'
" not found
'
)
}
const
r
=
Room
.
load
(
roomId
)
const
r
=
Web
Room
.
load
(
roomId
)
r
.
puppet
=
this
return
r
...
...
@@ -886,13 +913,13 @@ export class PuppetWeb extends Puppet {
/**
* FriendRequest
*/
public
async
friendRequestSend
(
contact
:
Contact
,
hello
:
string
):
Promise
<
boolean
>
{
public
async
friendRequestSend
(
contact
:
Contact
,
hello
:
string
):
Promise
<
void
>
{
if
(
!
contact
)
{
throw
new
Error
(
'
contact not found
'
)
}
try
{
return
await
this
.
bridge
.
verifyUserRequest
(
contact
.
id
,
hello
)
await
this
.
bridge
.
verifyUserRequest
(
contact
.
id
,
hello
)
}
catch
(
e
)
{
log
.
warn
(
'
PuppetWeb
'
,
'
bridge.verifyUserRequest(%s, %s) rejected: %s
'
,
contact
.
id
,
hello
,
e
.
message
)
Raven
.
captureException
(
e
)
...
...
@@ -900,13 +927,13 @@ export class PuppetWeb extends Puppet {
}
}
public
async
friendRequestAccept
(
contact
:
Contact
,
ticket
:
string
):
Promise
<
boolean
>
{
public
async
friendRequestAccept
(
contact
:
Contact
,
ticket
:
string
):
Promise
<
void
>
{
if
(
!
contact
||
!
ticket
)
{
throw
new
Error
(
'
contact or ticket not found
'
)
}
try
{
return
await
this
.
bridge
.
verifyUserOk
(
contact
.
id
,
ticket
)
await
this
.
bridge
.
verifyUserOk
(
contact
.
id
,
ticket
)
}
catch
(
e
)
{
log
.
warn
(
'
PuppetWeb
'
,
'
bridge.verifyUserOk(%s, %s) rejected: %s
'
,
contact
.
id
,
ticket
,
e
.
message
)
Raven
.
captureException
(
e
)
...
...
@@ -923,7 +950,7 @@ export class PuppetWeb extends Puppet {
let
counter
=
-
1
// tslint:disable-next-line:variable-name
const
MyContact
=
cloneClass
(
Contact
)
const
MyContact
=
cloneClass
(
Web
Contact
)
MyContact
.
puppet
=
this
async
function
stable
(
done
:
Function
):
Promise
<
void
>
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录