Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wechaty
wechaty
提交
4bf6a5f2
W
wechaty
项目概览
wechaty
/
wechaty
上一次同步 大约 3 年
通知
304
Star
10499
Fork
6
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
152
列表
看板
标记
里程碑
合并请求
1
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
wechaty
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
152
Issue
152
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4bf6a5f2
编写于
4月 23, 2018
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
get rid of the global puppetInstance (
#518
)
上级
89fe5e04
变更
17
隐藏空白更改
内联
并排
Showing
17 changed file
with
261 addition
and
187 deletion
+261
-187
src/config.spec.ts
src/config.spec.ts
+21
-21
src/config.ts
src/config.ts
+20
-20
src/contact.ts
src/contact.ts
+36
-33
src/friend-request.ts
src/friend-request.ts
+9
-7
src/message.spec.ts
src/message.spec.ts
+24
-13
src/message.ts
src/message.ts
+15
-12
src/puppet-accessory.spec.ts
src/puppet-accessory.spec.ts
+1
-1
src/puppet-accessory.ts
src/puppet-accessory.ts
+25
-10
src/puppet-web/event.ts
src/puppet-web/event.ts
+1
-1
src/puppet-web/friend-request.spec.ts
src/puppet-web/friend-request.spec.ts
+6
-3
src/puppet-web/friend-request.ts
src/puppet-web/friend-request.ts
+4
-4
src/puppet-web/index.spec.ts
src/puppet-web/index.spec.ts
+1
-1
src/room.ts
src/room.ts
+39
-40
src/wechaty.ts
src/wechaty.ts
+30
-5
tests/contact.spec.ts
tests/contact.spec.ts
+6
-3
tests/puppet-web/puppet-web.spec.ts
tests/puppet-web/puppet-web.spec.ts
+3
-2
tests/room.spec.ts
tests/room.spec.ts
+20
-11
未找到文件。
src/config.spec.ts
浏览文件 @
4bf6a5f2
...
...
@@ -23,7 +23,7 @@ import * as test from 'blue-tape'
// const sinonTest = require('sinon-test')(sinon)
import
{
config
}
from
'
./config
'
import
{
Puppet
}
from
'
./puppet
'
//
import { Puppet } from './puppet'
test
(
'
important variables
'
,
async
t
=>
{
t
.
true
(
'
puppet
'
in
config
,
'
should exist `puppet` in Config
'
)
...
...
@@ -59,28 +59,28 @@ test('validApiHost()', async t => {
})
test
(
'
puppetInstance()
'
,
async
t
=>
{
// BUG Compitable with Win32 CI
// global instance infected across unit tests... :(
const
bak
=
config
.
puppetInstance
()
//
test('puppetInstance()', async t => {
//
// BUG Compitable with Win32 CI
//
// global instance infected across unit tests... :(
//
const bak = config.puppetInstance()
config
.
puppetInstance
(
null
)
t
.
throws
(()
=>
{
config
.
puppetInstance
()
},
Error
,
'
should throw when not initialized
'
)
config
.
puppetInstance
(
bak
)
//
config.puppetInstance(null)
//
t.throws(() => {
//
config.puppetInstance()
//
}, Error, 'should throw when not initialized')
//
config.puppetInstance(bak)
const
EXPECTED
:
Puppet
=
{
userId
:
'
test
'
}
as
any
const
mockPuppet
=
EXPECTED
//
const EXPECTED: Puppet = {userId: 'test'} as any
//
const mockPuppet = EXPECTED
config
.
puppetInstance
(
mockPuppet
)
const
instance
=
config
.
puppetInstance
()
t
.
deepEqual
(
instance
,
EXPECTED
,
'
should equal with initialized data
'
)
//
config.puppetInstance(mockPuppet)
//
const instance = config.puppetInstance()
//
t.deepEqual(instance, EXPECTED, 'should equal with initialized data')
config
.
puppetInstance
(
null
)
t
.
throws
(()
=>
{
config
.
puppetInstance
()
},
Error
,
'
should throw after set to null
'
)
//
config.puppetInstance(null)
//
t.throws(() => {
//
config.puppetInstance()
//
}, Error, 'should throw after set to null')
config
.
puppetInstance
(
bak
)
})
//
config.puppetInstance(bak)
//
})
src/config.ts
浏览文件 @
4bf6a5f2
...
...
@@ -24,7 +24,7 @@ import * as readPkgUp from 'read-pkg-up'
import
*
as
Raven
from
'
raven
'
import
{
log
}
from
'
brolog
'
import
Puppet
from
'
./puppet
'
//
import Puppet from './puppet'
const
pkg
=
readPkgUp
.
sync
({
cwd
:
__dirname
}).
pkg
export
const
VERSION
=
pkg
.
version
...
...
@@ -116,7 +116,7 @@ export class Config {
public
httpPort
=
process
.
env
[
'
PORT
'
]
||
process
.
env
[
'
WECHATY_PORT
'
]
||
DEFAULT_SETTING
.
DEFAULT_PORT
public
docker
=
!!
(
process
.
env
[
'
WECHATY_DOCKER
'
])
private
_puppetInstance
:
Puppet
|
null
=
null
//
private _puppetInstance: Puppet | null = null
constructor
()
{
log
.
verbose
(
'
Config
'
,
'
constructor()
'
)
...
...
@@ -126,29 +126,29 @@ export class Config {
/**
* 5. live setting
*/
public
puppetInstance
():
Puppet
public
puppetInstance
(
empty
:
null
):
void
public
puppetInstance
(
instance
:
Puppet
):
void
//
public puppetInstance(): Puppet
//
public puppetInstance(empty: null): void
//
public puppetInstance(instance: Puppet): void
public
puppetInstance
(
instance
?:
Puppet
|
null
):
Puppet
|
void
{
//
public puppetInstance(instance?: Puppet | null): Puppet | void {
if
(
typeof
instance
===
'
undefined
'
)
{
if
(
!
this
.
_puppetInstance
)
{
throw
new
Error
(
'
no puppet instance
'
)
}
return
this
.
_puppetInstance
//
if (typeof instance === 'undefined') {
//
if (!this._puppetInstance) {
//
throw new Error('no puppet instance')
//
}
//
return this._puppetInstance
}
else
if
(
instance
===
null
)
{
log
.
verbose
(
'
Config
'
,
'
puppetInstance(null)
'
)
this
.
_puppetInstance
=
null
return
}
//
} else if (instance === null) {
//
log.verbose('Config', 'puppetInstance(null)')
//
this._puppetInstance = null
//
return
//
}
log
.
verbose
(
'
Config
'
,
'
puppetInstance(%s)
'
,
instance
.
constructor
.
name
)
this
.
_puppetInstance
=
instance
return
//
log.verbose('Config', 'puppetInstance(%s)', instance.constructor.name)
//
this._puppetInstance = instance
//
return
}
//
}
public
gitRevision
():
string
|
null
{
const
dotGitPath
=
path
.
join
(
__dirname
,
'
..
'
,
'
.git
'
)
// only for ts-node, not for dist
...
...
src/contact.ts
浏览文件 @
4bf6a5f2
...
...
@@ -18,18 +18,20 @@
* @ignore
*/
import
{
config
,
//
config,
Raven
,
Sayable
,
log
,
}
from
'
./config
'
}
from
'
./config
'
import
{
Message
,
MediaMessage
,
}
from
'
./message
'
import
Misc
from
'
./misc
'
import
PuppetWeb
from
'
./puppet-web
'
import
Wechaty
from
'
./wechaty
'
}
from
'
./message
'
import
Misc
from
'
./misc
'
import
PuppetAccessory
from
'
./puppet-accessory
'
import
Wechaty
from
'
./wechaty
'
import
PuppetWeb
from
'
./puppet-web/
'
export
interface
ContactObj
{
address
:
string
,
...
...
@@ -104,7 +106,7 @@ const specialContactList: string[] = [
* `Contact` is `Sayable`,
* [Examples/Contact-Bot]{@link https://github.com/Chatie/wechaty/blob/master/examples/contact-bot.ts}
*/
export
class
Contact
implements
Sayable
{
export
class
Contact
extends
PuppetAccessory
implements
Sayable
{
private
static
pool
=
new
Map
<
string
,
Contact
>
()
public
obj
:
ContactObj
|
null
...
...
@@ -117,6 +119,7 @@ export class Contact implements Sayable {
constructor
(
public
readonly
id
:
string
,
)
{
super
()
log
.
silly
(
'
Contact
'
,
`constructor(
${
id
}
)`
)
if
(
typeof
id
!==
'
string
'
)
{
...
...
@@ -277,7 +280,7 @@ export class Contact implements Sayable {
/**
* must be string because we need inject variable value
* into code as variable name
* into code as variable name
specialContactList
*/
let
filterFunction
:
string
...
...
@@ -291,7 +294,7 @@ export class Contact implements Sayable {
}
try
{
const
contactList
=
await
config
.
puppetInstance
()
const
contactList
=
await
this
.
puppet
//
config.puppetInstance()
.
contactFind
(
filterFunction
)
await
Promise
.
all
(
contactList
.
map
(
c
=>
c
.
ready
()))
...
...
@@ -406,25 +409,25 @@ export class Contact implements Sayable {
return
this
.
obj
&&
this
.
obj
.
alias
||
null
}
return
config
.
puppetInstance
()
.
contactAlias
(
this
,
newAlias
)
.
then
(
ret
=>
{
if
(
ret
)
{
if
(
this
.
obj
)
{
this
.
obj
.
alias
=
newAlias
}
else
{
log
.
error
(
'
Contact
'
,
'
alias() without this.obj?
'
)
}
return
this
.
puppet
// config.puppetInstance()
.
contactAlias
(
this
,
newAlias
)
.
then
(
ret
=>
{
if
(
ret
)
{
if
(
this
.
obj
)
{
this
.
obj
.
alias
=
newAlias
}
else
{
log
.
warn
(
'
Contact
'
,
'
alias(%s) fail
'
,
newAlias
)
log
.
error
(
'
Contact
'
,
'
alias() without this.obj?
'
)
}
return
ret
})
.
catch
(
e
=>
{
log
.
error
(
'
Contact
'
,
'
alias(%s) rejected: %s
'
,
newAlias
,
e
.
message
)
Raven
.
captureException
(
e
)
return
false
// fail safe
})
}
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
})
}
/**
...
...
@@ -542,9 +545,9 @@ export class Contact implements Sayable {
}
try
{
const
hostname
=
await
(
config
.
puppetInstance
()
as
PuppetWeb
).
hostname
()
const
hostname
=
await
(
/* config.puppetInstance() */
this
.
puppet
as
PuppetWeb
).
hostname
()
const
avatarUrl
=
`http://
${
hostname
}${
this
.
obj
.
avatar
}
&type=big`
// add '&type=big' to get big image
const
cookies
=
await
(
config
.
puppetInstance
()
as
PuppetWeb
).
cookies
()
const
cookies
=
await
(
/* config.puppetInstance() */
this
.
puppet
as
PuppetWeb
).
cookies
()
log
.
silly
(
'
Contact
'
,
'
avatar() url: %s
'
,
avatarUrl
)
return
Misc
.
urlStream
(
avatarUrl
,
cookies
)
...
...
@@ -599,9 +602,9 @@ export class Contact implements Sayable {
}
if
(
!
contactGetter
)
{
log
.
silly
(
'
Contact
'
,
'
get contact via
'
+
config
.
puppetInstance
()
.
constructor
.
name
)
contactGetter
=
config
.
puppetInstance
()
.
getContact
.
bind
(
config
.
puppetInstance
()
)
log
.
silly
(
'
Contact
'
,
'
get contact via
'
+
/* config.puppetInstance() */
this
.
puppet
.
constructor
.
name
)
contactGetter
=
/* config.puppetInstance() */
this
.
puppet
.
getContact
.
bind
(
/* config.puppetInstance() */
this
.
puppet
)
}
if
(
!
contactGetter
)
{
throw
new
Error
(
'
no contatGetter
'
)
...
...
@@ -648,8 +651,8 @@ export class Contact implements Sayable {
* const isSelf = contact.self()
*/
public
self
():
boolean
{
const
userId
=
config
.
puppetInstance
()
.
userId
const
userId
=
this
.
puppet
//
config.puppetInstance()
.
userId
const
selfId
=
this
.
id
...
...
src/friend-request.ts
浏览文件 @
4bf6a5f2
...
...
@@ -19,10 +19,11 @@
*/
import
{
config
,
//
config,
log
,
}
from
'
./config
'
import
Contact
from
'
./contact
'
}
from
'
./config
'
import
Contact
from
'
./contact
'
import
PuppetAccessory
from
'
./puppet-accessory
'
/**
* Send, receive friend request, and friend confirmation events.
...
...
@@ -33,18 +34,19 @@ import Contact from './contact'
*
* [Examples/Friend-Bot]{@link https://github.com/Chatie/wechaty/blob/master/examples/friend-bot.ts}
*/
export
abstract
class
FriendRequest
{
export
abstract
class
FriendRequest
extends
PuppetAccessory
{
public
contact
:
Contact
public
hello
:
string
public
type
:
'
send
'
|
'
receive
'
|
'
confirm
'
constructor
()
{
super
()
log
.
verbose
(
'
FriendRequest
'
,
'
constructor()
'
)
if
(
!
config
.
puppetInstance
())
{
throw
new
Error
(
'
no Config.puppetInstance() instanciated
'
)
}
//
if (!config.puppetInstance()) {
//
throw new Error('no Config.puppetInstance() instanciated')
//
}
}
public
abstract
send
(
contact
:
Contact
,
hello
:
string
):
Promise
<
boolean
>
...
...
src/message.spec.ts
浏览文件 @
4bf6a5f2
...
...
@@ -23,12 +23,14 @@ import * as test from 'blue-tape'
// const sinonTest = require('sinon-test')(sinon)
import
{
config
,
//
config,
log
,
}
from
'
./config
'
import
Contact
from
'
./contact
'
import
Message
from
'
./message
'
import
Profile
from
'
./profile
'
import
PuppetWeb
from
'
./puppet-web/
'
import
Room
from
'
./room
'
const
MOCK_USER_ID
=
'
TEST-USER-ID
'
...
...
@@ -36,7 +38,8 @@ const puppet = new PuppetWeb({
profile
:
new
Profile
(),
})
puppet
.
userId
=
MOCK_USER_ID
config
.
puppetInstance
(
puppet
)
// config.puppetInstance(puppet)
Message
.
puppet
=
puppet
test
(
'
constructor()
'
,
async
t
=>
{
/* tslint:disable:max-line-length */
...
...
@@ -100,8 +103,12 @@ test('ready()', async t => {
})
}
config
.
puppetInstance
()
.
getContact
=
mockGetContact
// config.puppetInstance()
// .getContact = mockGetContact
Room
.
puppet
=
Contact
.
puppet
=
Message
.
puppet
=
{
...
puppet
,
getContact
:
mockGetContact
,
}
as
any
const
m
=
new
Message
(
rawData
)
...
...
@@ -138,7 +145,8 @@ test('findAll()', async t => {
})
test
(
'
self()
'
,
async
t
=>
{
config
.
puppetInstance
(
puppet
)
// config.puppetInstance(puppet)
Room
.
puppet
=
Contact
.
puppet
=
Message
.
puppet
=
puppet
const
m
=
new
Message
()
m
.
from
(
MOCK_USER_ID
)
...
...
@@ -186,14 +194,17 @@ test('mentioned()', async t => {
})
}
let
puppet1
try
{
puppet1
=
config
.
puppetInstance
()
puppet1
.
getContact
=
mockContactGetter
}
catch
(
err
)
{
puppet1
=
{
getContact
:
mockContactGetter
}
config
.
puppetInstance
(
puppet1
)
}
// let puppet1
// try {
// puppet1 = config.puppetInstance()
// puppet1.getContact = mockContactGetter
// } catch (err) {
// puppet1 = { getContact: mockContactGetter }
// config.puppetInstance(puppet1)
// }
Contact
.
puppet
=
Room
.
puppet
=
Message
.
puppet
=
{
getContact
:
mockContactGetter
,
}
as
any
const
msg11
=
new
Message
(
rawObj11
)
const
room11
=
msg11
.
room
()
if
(
room11
)
{
...
...
src/message.ts
浏览文件 @
4bf6a5f2
...
...
@@ -25,15 +25,17 @@ import {
import
*
as
mime
from
'
mime
'
import
{
config
,
//
config,
Raven
,
Sayable
,
log
,
}
from
'
./config
'
}
from
'
./config
'
import
Contact
from
'
./contact
'
import
Room
from
'
./room
'
import
Misc
from
'
./misc
'
import
PuppetAccessory
from
'
./puppet-accessory
'
import
Contact
from
'
./contact
'
import
Room
from
'
./room
'
import
Misc
from
'
./misc
'
import
PuppetWeb
from
'
./puppet-web/puppet-web
'
import
Bridge
from
'
./puppet-web/bridge
'
...
...
@@ -55,7 +57,7 @@ export type TypeName = 'attachment'
* `Message` is `Sayable`,
* [Examples/Ding-Dong-Bot]{@link https://github.com/Chatie/wechaty/blob/master/examples/ding-dong-bot.ts}
*/
export
class
Message
implements
Sayable
{
export
class
Message
extends
PuppetAccessory
implements
Sayable
{
/**
* @private
*/
...
...
@@ -87,6 +89,7 @@ export class Message implements Sayable {
* @private
*/
constructor
(
public
rawObj
?:
MsgRawObj
)
{
super
()
this
.
_counter
=
Message
.
counter
++
log
.
silly
(
'
Message
'
,
'
constructor() SN:%d
'
,
this
.
_counter
)
...
...
@@ -243,8 +246,8 @@ export class Message implements Sayable {
}
}
return
config
.
puppetInstance
()
.
send
(
m
)
return
this
.
puppet
//
config.puppetInstance()
.
send
(
m
)
}
/**
...
...
@@ -403,7 +406,7 @@ export class Message implements Sayable {
* }
*/
public
self
():
boolean
{
const
userId
=
config
.
puppetInstance
()
const
userId
=
this
.
puppet
//
config.puppetInstance()
.
userId
const
fromId
=
this
.
obj
.
from
...
...
@@ -690,7 +693,7 @@ export class MediaMessage extends Message {
}
// FIXME: decoupling needed
this
.
bridge
=
(
config
.
puppetInstance
()
as
PuppetWeb
)
this
.
bridge
=
(
/* config.puppetInstance() */
this
.
puppet
as
PuppetWeb
)
.
bridge
}
...
...
@@ -886,7 +889,7 @@ export class MediaMessage extends Message {
try
{
await
this
.
ready
()
// FIXME: decoupling needed
const
cookies
=
await
(
config
.
puppetInstance
()
as
PuppetWeb
).
cookies
()
const
cookies
=
await
(
/* config.puppetInstance() */
this
.
puppet
as
PuppetWeb
).
cookies
()
if
(
!
this
.
obj
.
url
)
{
throw
new
Error
(
'
no url
'
)
}
...
...
@@ -983,7 +986,7 @@ export class MediaMessage extends Message {
*/
public
async
forward
(
to
:
Room
|
Contact
):
Promise
<
boolean
>
{
try
{
const
ret
=
await
config
.
puppetInstance
()
.
forward
(
this
,
to
)
const
ret
=
await
/* config.puppetInstance() */
this
.
puppet
.
forward
(
this
,
to
)
return
ret
}
catch
(
e
)
{
log
.
error
(
'
Message
'
,
'
forward(%s) exception: %s
'
,
to
,
e
)
...
...
src/puppet-accessory.spec.ts
浏览文件 @
4bf6a5f2
...
...
@@ -29,7 +29,7 @@ const EXPECTED_PUPPET2 = {p: 2} as any as Puppet
test
(
'
PuppetAccessory smoke testing
'
,
async
t
=>
{
class
FixtureClass
extends
PuppetAccessory
{}
t
.
ok
(
FixtureClass
.
puppet
===
undefined
,
'
should be undefined initialy for static puppet
'
)
t
.
throws
(()
=>
FixtureClass
.
puppet
,
'
should throw if read static puppet before initialize
'
)
const
c
=
new
FixtureClass
()
t
.
throws
(()
=>
c
.
puppet
,
'
should throw if read instance puppet before initialization
'
)
...
...
src/puppet-accessory.ts
浏览文件 @
4bf6a5f2
import
{
EventEmitter
}
from
'
events
'
import
{
log
}
from
'
./config
'
import
{
Puppet
}
from
'
./puppet
'
export
abstract
class
PuppetAccessory
{
public
static
puppet
?:
Puppet
export
abstract
class
PuppetAccessory
extends
EventEmitter
{
/**
* 1. Static puppet property
*/
private
static
_puppet
?:
Puppet
public
static
set
puppet
(
puppet
:
Puppet
)
{
log
.
verbose
(
'
PuppetAssessory
'
,
'
static set puppet()
'
)
this
.
_puppet
=
puppet
}
public
static
get
puppet
():
Puppet
{
log
.
silly
(
'
PuppetAssessory
'
,
'
static get puppet()
'
)
if
(
this
.
_puppet
)
{
log
.
silly
(
'
PuppetAssessory
'
,
'
static get puppet() from instance properties
'
)
return
this
.
_puppet
}
throw
new
Error
(
'
static puppet not found
'
)
}
/**
* 2. Instance puppet property
*/
private
_puppet
?:
Puppet
public
set
puppet
(
puppet
:
Puppet
)
{
log
.
verbose
(
'
PuppetAssessory
'
,
'
set puppet()
'
)
this
.
_puppet
=
puppet
}
public
get
puppet
()
{
public
get
puppet
()
:
Puppet
{
log
.
silly
(
'
PuppetAssessory
'
,
'
get puppet()
'
)
if
(
this
.
_puppet
)
{
...
...
@@ -19,13 +40,7 @@ export abstract class PuppetAccessory {
return
this
.
_puppet
}
const
staticPuppet
=
(
this
.
constructor
as
any
).
puppet
if
(
staticPuppet
)
{
log
.
silly
(
'
PuppetAssessory
'
,
'
get puppet() from static properties
'
)
return
staticPuppet
}
throw
new
Error
(
'
puppet not found
'
)
return
(
this
.
constructor
as
any
).
puppet
}
}
...
...
src/puppet-web/event.ts
浏览文件 @
4bf6a5f2
...
...
@@ -177,7 +177,7 @@ function onLogout(this: PuppetWeb, data) {
async
function
onMessage
(
this
:
PuppetWeb
,
obj
:
MsgRawObj
,
obj
:
MsgRawObj
,
):
Promise
<
void
>
{
let
m
=
new
Message
(
obj
)
...
...
src/puppet-web/friend-request.spec.ts
浏览文件 @
4bf6a5f2
...
...
@@ -22,15 +22,18 @@ import * as test from 'blue-tape'
// import * as sinon from 'sinon'
// const sinonTest = require('sinon-test')(sinon)
import
config
from
'
../config
'
//
import config from '../config'
import
Contact
from
'
../contact
'
import
Message
from
'
../message
'
import
Puppet
from
'
../puppet
'
import
PuppetWebFriendRequest
from
'
./friend-request
'
config
.
puppetInstance
(
{
PuppetWebFriendRequest
.
puppet
=
{
userId
:
'
xxx
'
,
}
as
any
as
Puppet
)
}
as
any
as
Puppet
// config.puppetInstance({
// userId: 'xxx',
// } as any as Puppet)
test
(
'
PuppetWebFriendRequest.receive smoke testing
'
,
async
t
=>
{
/* tslint:disable:max-line-length */
...
...
src/puppet-web/friend-request.ts
浏览文件 @
4bf6a5f2
...
...
@@ -31,7 +31,7 @@ const retryPromise = require('retry-promise').default
import
{
Contact
}
from
'
../contact
'
import
{
config
,
//
config,
log
,
}
from
'
../config
'
import
FriendRequest
from
'
../friend-request
'
...
...
@@ -114,7 +114,7 @@ export class PuppetWebFriendRequest extends FriendRequest {
this
.
hello
=
hello
}
return
config
.
puppetInstance
()
return
this
.
puppet
//
config.puppetInstance()
.
friendRequestSend
(
contact
,
hello
)
}
...
...
@@ -130,8 +130,8 @@ export class PuppetWebFriendRequest extends FriendRequest {
throw
new
Error
(
'
request is not a `receive` type. it is a
'
+
this
.
type
+
'
type
'
)
}
const
ret
=
await
config
.
puppetInstance
()
.
friendRequestAccept
(
this
.
contact
,
this
.
ticket
)
const
ret
=
await
this
.
puppet
//
config.puppetInstance()
.
friendRequestAccept
(
this
.
contact
,
this
.
ticket
)
const
max
=
20
const
backoff
=
300
...
...
src/puppet-web/index.spec.ts
浏览文件 @
4bf6a5f2
...
...
@@ -25,7 +25,7 @@ import {
Bridge
,
Event
,
PuppetWeb
,
}
from
'
./
index
'
}
from
'
./
'
test
(
'
PuppetWeb Module Exports
'
,
async
t
=>
{
t
.
ok
(
PuppetWeb
,
'
should export PuppetWeb
'
)
...
...
src/room.ts
浏览文件 @
4bf6a5f2
...
...
@@ -17,20 +17,19 @@
*
* @ignore
*/
import
{
EventEmitter
}
from
'
events
'
import
{
config
,
//
config,
Raven
,
Sayable
,
log
,
}
from
'
./config
'
import
Contact
from
'
./contact
'
}
from
'
./config
'
import
Contact
from
'
./contact
'
import
{
Message
,
MediaMessage
,
}
from
'
./message
'
import
Misc
from
'
./misc
'
}
from
'
./message
'
import
Misc
from
'
./misc
'
import
PuppetAccessory
from
'
./puppet-accessory
'
interface
RoomObj
{
id
:
string
,
...
...
@@ -82,7 +81,7 @@ export interface MemberQueryFilter {
* `Room` is `Sayable`,
* [Examples/Room-Bot]{@link https://github.com/Chatie/wechaty/blob/master/examples/room-bot.ts}
*/
export
class
Room
extends
EventEmitter
implements
Sayable
{
export
class
Room
extends
PuppetAccessory
implements
Sayable
{
private
static
pool
=
new
Map
<
string
,
Room
>
()
private
dirtyObj
:
RoomObj
|
null
// when refresh, use this to save dirty data for query
...
...
@@ -141,8 +140,8 @@ export class Room extends EventEmitter implements Sayable {
}
if
(
!
contactGetter
)
{
contactGetter
=
config
.
puppetInstance
()
.
getContact
.
bind
(
config
.
puppetInstance
()
)
contactGetter
=
this
.
puppet
//
config.puppetInstance()
.
getContact
.
bind
(
/* config.puppetInstance() */
this
.
puppet
)
}
if
(
!
contactGetter
)
{
throw
new
Error
(
'
no contactGetter
'
)
...
...
@@ -247,8 +246,8 @@ export class Room extends EventEmitter implements Sayable {
m
.
room
(
this
)
return
config
.
puppetInstance
()
.
send
(
m
)
return
this
.
puppet
//
config.puppetInstance()
.
send
(
m
)
}
public
on
(
event
:
'
leave
'
,
listener
:
(
this
:
Room
,
leaver
:
Contact
)
=>
void
):
this
...
...
@@ -427,8 +426,8 @@ export class Room extends EventEmitter implements Sayable {
throw
new
Error
(
'
contact not found
'
)
}
const
n
=
config
.
puppetInstance
()
.
roomAdd
(
this
,
contact
)
const
n
=
this
.
puppet
//
config.puppetInstance()
.
roomAdd
(
this
,
contact
)
return
n
}
...
...
@@ -455,9 +454,9 @@ export class Room extends EventEmitter implements Sayable {
if
(
!
contact
)
{
throw
new
Error
(
'
contact not found
'
)
}
const
n
=
await
config
.
puppetInstance
()
.
roomDel
(
this
,
contact
)
.
then
(
_
=>
this
.
delLocal
(
contact
))
const
n
=
await
this
.
puppet
//
config.puppetInstance()
.
roomDel
(
this
,
contact
)
.
then
(
_
=>
this
.
delLocal
(
contact
))
return
n
}
...
...
@@ -536,14 +535,14 @@ export class Room extends EventEmitter implements Sayable {
return
Misc
.
plainText
(
this
.
obj
?
this
.
obj
.
topic
:
''
)
}
config
.
puppetInstance
()
.
roomTopic
(
this
,
newTopic
)
.
catch
(
e
=>
{
log
.
warn
(
'
Room
'
,
'
topic(newTopic=%s) exception: %s
'
,
newTopic
,
e
&&
e
.
message
||
e
,
)
Raven
.
captureException
(
e
)
})
this
.
puppet
//
config.puppetInstance()
.
roomTopic
(
this
,
newTopic
)
.
catch
(
e
=>
{
log
.
warn
(
'
Room
'
,
'
topic(newTopic=%s) exception: %s
'
,
newTopic
,
e
&&
e
.
message
||
e
,
)
Raven
.
captureException
(
e
)
})
if
(
!
this
.
obj
)
{
this
.
obj
=
<
RoomObj
>
{}
...
...
@@ -820,13 +819,13 @@ export class Room extends EventEmitter implements Sayable {
throw
new
Error
(
'
contactList not found
'
)
}
return
config
.
puppetInstance
()
.
roomCreate
(
contactList
,
topic
)
.
catch
(
e
=>
{
log
.
error
(
'
Room
'
,
'
create() exception: %s
'
,
e
&&
e
.
stack
||
e
.
message
||
e
)
Raven
.
captureException
(
e
)
throw
e
})
return
this
.
puppet
//
config.puppetInstance()
.
roomCreate
(
contactList
,
topic
)
.
catch
(
e
=>
{
log
.
error
(
'
Room
'
,
'
create() exception: %s
'
,
e
&&
e
.
stack
||
e
.
message
||
e
)
Raven
.
captureException
(
e
)
throw
e
})
}
/**
...
...
@@ -862,13 +861,13 @@ export class Room extends EventEmitter implements Sayable {
throw
new
Error
(
'
unsupport topic type
'
)
}
const
roomList
=
await
config
.
puppetInstance
()
.
roomFind
(
filterFunction
)
.
catch
(
e
=>
{
log
.
verbose
(
'
Room
'
,
'
findAll() rejected: %s
'
,
e
.
message
)
Raven
.
captureException
(
e
)
return
[]
as
Room
[]
// fail safe
})
const
roomList
=
await
this
.
puppet
//
config.puppetInstance()
.
roomFind
(
filterFunction
)
.
catch
(
e
=>
{
log
.
verbose
(
'
Room
'
,
'
findAll() rejected: %s
'
,
e
.
message
)
Raven
.
captureException
(
e
)
return
[]
as
Room
[]
// fail safe
})
await
Promise
.
all
(
roomList
.
map
(
room
=>
room
.
ready
()))
// for (let i = 0; i < roomList.length; i++) {
...
...
@@ -919,7 +918,7 @@ export class Room extends EventEmitter implements Sayable {
public
owner
():
Contact
|
null
{
const
ownerUin
=
this
.
obj
&&
this
.
obj
.
ownerUin
const
user
=
config
.
puppetInstance
()
const
user
=
this
.
puppet
//
config.puppetInstance()
.
user
if
(
user
&&
user
.
get
(
'
uin
'
)
===
ownerUin
)
{
...
...
src/wechaty.ts
浏览文件 @
4bf6a5f2
...
...
@@ -27,6 +27,7 @@ import {
hotImport
,
}
from
'
hot-import
'
import
cloneClass
from
'
./clone-class
'
import
{
config
,
log
,
...
...
@@ -36,16 +37,17 @@ import {
VERSION
,
WechatyEvent
,
}
from
'
./config
'
import
Contact
from
'
./contact
'
import
FriendRequest
from
'
./friend-request
'
import
{
Message
,
MediaMessage
,
}
from
'
./message
'
import
Profile
from
'
./profile
'
import
Puppet
from
'
./puppet
'
import
PuppetWeb
from
'
./puppet-web/
'
import
{
FriendRequest
,
PuppetWeb
,
}
from
'
./puppet-web/
'
import
Room
from
'
./room
'
// import Misc from './misc'
...
...
@@ -91,6 +93,15 @@ export class Wechaty extends EventEmitter implements Sayable {
*/
public
cuid
:
string
// tslint:disable-next-line:variable-name
public
Contact
:
typeof
Contact
// tslint:disable-next-line:variable-name
public
FriendRequest
:
typeof
FriendRequest
// tslint:disable-next-line:variable-name
public
Message
:
typeof
Message
// tslint:disable-next-line:variable-name
public
Room
:
typeof
Room
/**
* get the singleton instance of Wechaty
*
...
...
@@ -408,7 +419,17 @@ export class Wechaty extends EventEmitter implements Sayable {
}
// set puppet instance to Wechaty Static variable, for using by Contact/Room/Message/FriendRequest etc.
config
.
puppetInstance
(
puppet
)
// config.puppetInstance(puppet)
this
.
Contact
=
cloneClass
(
Contact
)
this
.
FriendRequest
=
cloneClass
(
FriendRequest
)
this
.
Message
=
cloneClass
(
Message
)
this
.
Room
=
cloneClass
(
Room
)
this
.
Contact
.
puppet
=
puppet
this
.
FriendRequest
.
puppet
=
puppet
this
.
Message
.
puppet
=
puppet
this
.
Room
.
puppet
=
puppet
await
puppet
.
start
()
return
puppet
...
...
@@ -441,7 +462,11 @@ export class Wechaty extends EventEmitter implements Sayable {
const
puppet
=
this
.
puppet
this
.
puppet
=
null
config
.
puppetInstance
(
null
)
// config.puppetInstance(null)
// this.Contact.puppet = undefined
// this.FriendRequest.puppet = undefined
// this.Message.puppet = undefined
// this.Room.puppet = undefined
try
{
await
puppet
.
stop
()
...
...
tests/contact.spec.ts
浏览文件 @
4bf6a5f2
...
...
@@ -22,14 +22,17 @@
import
*
as
test
from
'
blue-tape
'
// import * as sinon from 'sinon'
import
config
from
'
../src/config
'
//
import config from '../src/config'
import
Contact
from
'
../src/contact
'
import
Profile
from
'
../src/profile
'
import
PuppetWeb
from
'
../src/puppet-web
'
config
.
puppetInstance
(
new
PuppetWeb
({
// config.puppetInstance(new PuppetWeb({
// profile: new Profile(),
// }))
Contact
.
puppet
=
new
PuppetWeb
({
profile
:
new
Profile
(),
})
)
})
test
(
'
Contact smoke testing
'
,
async
t
=>
{
/* tslint:disable:variable-name */
...
...
tests/puppet-web/puppet-web.spec.ts
浏览文件 @
4bf6a5f2
...
...
@@ -31,7 +31,7 @@ const sinonTest = require('sinon-test')(sinon, {
// log.level('silly')
import
{
config
,
//
config,
Contact
,
Profile
,
}
from
'
../../
'
...
...
@@ -60,7 +60,8 @@ test('login/logout events', sinonTest(async function (t: test.Test) {
const
pw
=
new
PuppetWeb
({
profile
})
t
.
ok
(
pw
,
'
should instantiated a PuppetWeb
'
)
config
.
puppetInstance
(
pw
)
// config.puppetInstance(pw)
Contact
.
puppet
=
pw
await
pw
.
start
()
t
.
pass
(
'
should be inited
'
)
...
...
tests/room.spec.ts
浏览文件 @
4bf6a5f2
...
...
@@ -22,15 +22,19 @@
import
*
as
test
from
'
blue-tape
'
// import * as sinon from 'sinon'
import
config
from
'
../src/config
'
//
import config from '../src/config'
import
Contact
from
'
../src/contact
'
import
Message
from
'
../src/message
'
import
Profile
from
'
../src/profile
'
import
PuppetWeb
from
'
../src/puppet-web
'
import
Room
from
'
../src/room
'
config
.
puppetInstance
(
new
PuppetWeb
({
// config.puppetInstance(new PuppetWeb({
// profile: new Profile(),
// }))
Room
.
puppet
=
new
PuppetWeb
({
profile
:
new
Profile
(),
})
)
})
// Room.attach(new PuppetWeb())
// test('Room smoke testing', async t => {
...
...
@@ -110,14 +114,15 @@ test('Room smoking test', async t => {
t
.
is
(
r
.
id
,
EXPECTED
.
id
,
'
should set id/UserName right
'
)
let
puppet
try
{
puppet
=
config
.
puppetInstance
()
puppet
.
getContact
=
mockContactGetter
}
catch
(
err
)
{
puppet
=
{
getContact
:
mockContactGetter
}
config
.
puppetInstance
(
puppet
)
}
// let puppet
// try {
// puppet = config.puppetInstance()
// puppet.getContact = mockContactGetter
// } catch (err) {
// puppet = { getContact: mockContactGetter }
// config.puppetInstance(puppet)
// }
Contact
.
puppet
=
Message
.
puppet
=
Room
.
puppet
=
{
getContact
:
mockContactGetter
}
as
any
await
r
.
ready
()
t
.
is
(
r
.
get
(
'
id
'
)
,
EXPECTED
.
id
,
'
should set id/UserName
'
)
...
...
@@ -175,6 +180,10 @@ test('Room smoking test', async t => {
})
test
(
'
Room static method
'
,
async
t
=>
{
Room
.
puppet
=
new
PuppetWeb
({
profile
:
new
Profile
(),
})
try
{
const
result
=
await
Room
.
find
({
topic
:
'
xxx
'
})
t
.
is
(
result
,
null
,
`should return null if cannot find the room`
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录