Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.www.
wechaty
提交
42693750
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,发现更多精彩内容 >>
提交
42693750
编写于
5月 26, 2018
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
clean code
上级
f0cd6834
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
70 addition
and
133 deletion
+70
-133
src/message.ts
src/message.ts
+47
-33
src/message.type.ts
src/message.type.ts
+0
-73
src/puppet-mock/puppet-mock.ts
src/puppet-mock/puppet-mock.ts
+1
-2
src/puppet-puppeteer/bridge.ts
src/puppet-puppeteer/bridge.ts
+1
-1
src/puppet-puppeteer/event.ts
src/puppet-puppeteer/event.ts
+3
-3
src/puppet-puppeteer/puppet-puppeteer.ts
src/puppet-puppeteer/puppet-puppeteer.ts
+3
-7
src/puppet-puppeteer/puppeteer-friend-request.spec.ts
src/puppet-puppeteer/puppeteer-friend-request.spec.ts
+1
-1
src/puppet-puppeteer/puppeteer-message.spec.ts
src/puppet-puppeteer/puppeteer-message.spec.ts
+11
-12
src/puppet-puppeteer/puppeteer-room.spec.ts
src/puppet-puppeteer/puppeteer-room.spec.ts
+3
-1
未找到文件。
src/message.ts
浏览文件 @
42693750
...
...
@@ -35,14 +35,31 @@ import PuppetAccessory from './puppet-accessory'
import
Contact
from
'
./contact
'
import
Room
from
'
./room
'
import
{
MessageDirection
,
// MessageMOOptions,
// MessageMOOptionsText,
// MessageMOOptionsFile,
MessagePayload
,
MessageType
,
}
from
'
./message.type
'
export
enum
MessageType
{
Unknown
=
0
,
Attachment
,
Audio
,
Emoticon
,
Image
,
Text
,
Video
,
}
/**
*
* MessagePayload
*
*/
export
interface
MessagePayload
{
type
:
MessageType
,
text
?
:
string
,
file
?
:
FileBox
,
// direction : MessageDirection,
fromId
?
:
string
,
date
:
Date
,
toId
?
:
null
|
string
,
// if to is not set, then room must be set
roomId
?
:
null
|
string
,
}
/**
* All wechat messages will be encapsulated as a Message.
...
...
@@ -59,9 +76,9 @@ export class Message extends PuppetAccessory implements Sayable {
*/
// tslint:disable-next-line:variable-name
public
static
readonly
Type
=
MessageType
public
static
readonly
Type
=
MessageType
// tslint:disable-next-line:variable-name
public
static
readonly
Direction
=
MessageDirection
//
public static readonly Direction = MessageDirection
/**
* @todo add function
...
...
@@ -170,14 +187,16 @@ export class Message extends PuppetAccessory implements Sayable {
// }
/**
* Create a Mobile Terminated Message
*
* "mobile originated" or "mobile terminated"
* https://www.tatango.com/resources/video-lessons/video-mo-mt-sms-messaging/
*/
public
static
create
MT
(
public
static
create
(
id
:
string
,
payload
?
:
MessagePayload
,
):
Message
{
log
.
verbose
(
'
Message
'
,
'
static create
MobileTerminated
(%s, %s)
'
,
log
.
verbose
(
'
Message
'
,
'
static create(%s, %s)
'
,
id
,
payload
?
payload
:
''
,
)
...
...
@@ -188,7 +207,7 @@ export class Message extends PuppetAccessory implements Sayable {
* because the class will be `cloneClass`-ed
*/
const
msg
=
new
this
(
id
)
msg
.
direction
=
MessageDirection
.
MT
//
msg.direction = MessageDirection.MT
if
(
payload
)
{
msg
.
payload
=
payload
...
...
@@ -201,15 +220,15 @@ export class Message extends PuppetAccessory implements Sayable {
* @alias createMT
* Create a Mobile Terminated Message
*/
public
static
create
(
id
:
string
,
payload
?
:
MessagePayload
,
):
Message
{
return
this
.
createMT
(
id
,
payload
,
)
}
//
public static create(
//
id : string,
//
payload? : MessagePayload,
//
): Message {
//
return this.createMT(
//
id,
//
payload,
//
)
//
}
/**
*
...
...
@@ -217,7 +236,7 @@ export class Message extends PuppetAccessory implements Sayable {
*
*/
private
payload
?
:
MessagePayload
private
direction
:
MessageDirection
//
private direction : MessageDirection
/**
* @private
...
...
@@ -243,7 +262,7 @@ export class Message extends PuppetAccessory implements Sayable {
}
// default set to MT because there's a id param
this
.
direction
=
MessageDirection
.
MT
//
this.direction = MessageDirection.MT
}
/**
...
...
@@ -256,7 +275,7 @@ export class Message extends PuppetAccessory implements Sayable {
const
msgStrList
=
[
'
Message
'
,
`#
${
MessageDirection
[
this
.
direction
]}
`
,
//
`#${MessageDirection[this.direction]}`,
`#
${
MessageType
[
this
.
type
()]}
`
,
]
if
(
this
.
type
()
===
Message
.
Type
.
Text
)
{
...
...
@@ -682,9 +701,9 @@ export class Message extends PuppetAccessory implements Sayable {
public
async
ready
():
Promise
<
void
>
{
log
.
verbose
(
'
Message
'
,
'
ready()
'
)
if
(
this
.
direction
!==
MessageDirection
.
MT
)
{
throw
new
Error
(
'
only Mobile Terminated message is permit to call ready()!
'
)
}
//
if (this.direction !== MessageDirection.MT) {
//
throw new Error('only Mobile Terminated message is permit to call ready()!')
//
}
if
(
this
.
isReady
())
{
return
...
...
@@ -861,9 +880,4 @@ export class Message extends PuppetAccessory implements Sayable {
}
}
export
{
MessageDirection
,
MessagePayload
,
MessageType
,
}
export
default
Message
src/message.type.ts
已删除
100644 → 0
浏览文件 @
f0cd6834
import
{
FileBox
,
}
from
'
file-box
'
import
{
Contact
,
}
from
'
./contact
'
import
{
Room
,
}
from
'
./room
'
export
enum
MessageDirection
{
// MO,
MT
,
}
export
enum
MessageType
{
Unknown
=
0
,
Attachment
,
Audio
,
Emoticon
,
Image
,
Text
,
Video
,
}
/**
*
* MessageMOOptions
*
*
*/
export
interface
MessageMOOptionsText
{
text
:
string
,
}
export
interface
MessageMOOptionsFile
{
file
:
FileBox
,
}
export
interface
MessageMOOptionsRoom
{
room
:
Room
,
to
?
:
null
|
Contact
,
}
export
interface
MessageMOOptionsTo
{
room
?
:
null
|
Room
,
to
:
Contact
,
}
export
type
MessageMOOptions
=
(
MessageMOOptionsText
|
MessageMOOptionsFile
)
&
(
|
MessageMOOptionsRoom
|
MessageMOOptionsTo
)
/**
*
* MessagePayload
*
*/
export
interface
MessagePayload
{
type
:
MessageType
,
text
?
:
string
,
file
?
:
FileBox
,
direction
:
MessageDirection
,
fromId
?
:
string
,
date
:
Date
,
toId
?
:
null
|
string
,
// if to is not set, then room must be set
roomId
?
:
null
|
string
,
}
src/puppet-mock/puppet-mock.ts
浏览文件 @
42693750
...
...
@@ -93,7 +93,7 @@ export class PuppetMock extends Puppet {
const
user
=
this
.
Contact
.
load
(
this
.
userId
)
this
.
emit
(
'
login
'
,
user
)
const
msg
=
this
.
Message
.
create
MT
(
'
mock_id
'
)
const
msg
=
this
.
Message
.
create
(
'
mock_id
'
)
await
msg
.
ready
()
setInterval
(()
=>
{
...
...
@@ -197,7 +197,6 @@ export class PuppetMock extends Puppet {
log
.
verbose
(
'
PuppetMock
'
,
'
messagePayload(%s)
'
,
rawPayload
)
const
payload
:
MessagePayload
=
{
date
:
new
Date
(),
direction
:
this
.
Message
.
Direction
.
MT
,
fromId
:
'
xxx
'
,
text
:
'
mock message text
'
,
toId
:
this
.
userSelf
().
id
,
...
...
src/puppet-puppeteer/bridge.ts
浏览文件 @
42693750
...
...
@@ -44,7 +44,7 @@ import {
}
from
'
../puppet-puppeteer/web-schemas
'
import
{
WebRoomRawPayload
,
}
from
'
./
puppet-puppeteer
'
}
from
'
./
web-schemas
'
export
interface
InjectResult
{
code
:
number
,
...
...
src/puppet-puppeteer/event.ts
浏览文件 @
42693750
...
...
@@ -190,7 +190,7 @@ async function onMessage(
this
:
PuppetPuppeteer
,
rawPayload
:
WebMessageRawPayload
,
):
Promise
<
void
>
{
const
msg
=
this
.
Message
.
create
MT
(
rawPayload
.
MsgId
)
const
msg
=
this
.
Message
.
create
(
rawPayload
.
MsgId
)
try
{
await
msg
.
ready
()
...
...
@@ -232,13 +232,13 @@ async function onMessage(
// case WebMessageType.MICROVIDEO:
// case WebMessageType.APP:
// log.verbose('PuppetPuppeteerEvent', 'onMessage() EMOTICON/IMAGE/VIDEO/VOICE/MICROVIDEO message')
// msg = this.Message.create
MT
(rawPayload.MsgId)
// msg = this.Message.create(rawPayload.MsgId)
// break
// case WebMessageType.TEXT:
// if (rawPayload.SubMsgType === WebMessageType.LOCATION) {
// log.verbose('PuppetPuppeteerEvent', 'onMessage() (TEXT&LOCATION) message')
// msg = this.Message.create
MT
(rawPayload.MsgId)
// msg = this.Message.create(rawPayload.MsgId)
// }
// break
// }
...
...
src/puppet-puppeteer/puppet-puppeteer.ts
浏览文件 @
42693750
...
...
@@ -74,7 +74,7 @@ import {
Gender
,
}
from
'
../contact
'
import
{
MessageDirection
,
//
MessageDirection,
MessagePayload
,
MessageType
,
}
from
'
../message
'
...
...
@@ -337,7 +337,6 @@ export class PuppetPuppeteer extends Puppet {
}
else
{
throw
new
Error
(
'
parse found a room message, but neither FromUserName nor ToUserName is a room(/^@@/)
'
)
}
room
=
room
}
if
(
rawPayload
.
ToUserName
)
{
...
...
@@ -346,7 +345,7 @@ export class PuppetPuppeteer extends Puppet {
}
}
const
file
:
FileBox
|
undefined
=
undefined
const
file
:
undefined
|
FileBox
=
undefined
const
type
:
MessageType
=
this
.
messageTypeFromWeb
(
rawPayload
.
MsgType
)
...
...
@@ -355,7 +354,7 @@ export class PuppetPuppeteer extends Puppet {
const
toId
=
to
&&
to
.
id
const
payload
:
MessagePayload
=
{
direction
:
MessageDirection
.
MT
,
//
direction: MessageDirection.MT,
type
,
fromId
,
toId
,
...
...
@@ -1697,7 +1696,4 @@ export class PuppetPuppeteer extends Puppet {
}
export
{
WebRoomRawPayload
,
}
export
default
PuppetPuppeteer
src/puppet-puppeteer/puppeteer-friend-request.spec.ts
浏览文件 @
42693750
...
...
@@ -109,7 +109,7 @@ test('PuppetPuppeteerFriendRequest.confirm smoke testing', async t => {
// ;
// (puppet as any).messageRawPayload()
const
msg
=
wechaty
.
Message
.
create
MT
(
rawMessagePayload
.
MsgId
)
const
msg
=
wechaty
.
Message
.
create
(
rawMessagePayload
.
MsgId
)
await
msg
.
ready
()
t
.
true
(
/^You have added
(
.+
)
as your WeChat contact. Start chatting!$/
.
test
(
msg
.
text
()),
'
should match confirm message
'
)
...
...
src/puppet-puppeteer/puppeteer-message.spec.ts
浏览文件 @
42693750
...
...
@@ -103,7 +103,7 @@ test('constructor()', async t => {
id
:
'
179242112323992762
'
,
from
:
'
@0bb3e4dd746fdbd4a80546aef66f4085
'
,
}
const
msg
=
wechaty
.
Message
.
create
MT
(
rawPayload
.
MsgId
)
const
msg
=
wechaty
.
Message
.
create
(
rawPayload
.
MsgId
)
const
sandbox
=
sinon
.
createSandbox
()
sandbox
.
stub
(
puppet
,
'
messagePayload
'
).
callsFake
((
_
:
string
)
=>
{
...
...
@@ -204,7 +204,7 @@ test('ready()', async t => {
sandbox
.
stub
(
puppet
as
any
,
'
contactRawPayload
'
).
callsFake
(
mockContactRawPayload
)
sandbox
.
stub
(
puppet
as
any
,
'
messageRawPayload
'
).
callsFake
(
mockMessageRawPayload
)
const
m
=
wechaty
.
Message
.
create
MT
(
rawPayload
.
MsgId
)
const
m
=
wechaty
.
Message
.
create
(
rawPayload
.
MsgId
)
t
.
is
(
m
.
id
,
expectedMsgId
,
'
id/MsgId right
'
)
...
...
@@ -254,7 +254,6 @@ test('self()', async t => {
fromId
:
MOCK_CONTACT
.
id
,
toId
:
'
to_id
'
,
type
:
{}
as
any
,
direction
:
{}
as
any
,
date
:
{}
as
any
,
}
return
payload
...
...
@@ -264,14 +263,14 @@ test('self()', async t => {
sandbox
.
stub
((
puppet
as
any
as
{
'
userId
'
:
string
}),
'
userId
'
)
.
value
(
MOCK_CONTACT
.
id
)
const
selfMsg
=
wechaty
.
Message
.
create
MT
(
'
xxx
'
)
const
selfMsg
=
wechaty
.
Message
.
create
(
'
xxx
'
)
await
selfMsg
.
ready
()
t
.
true
(
selfMsg
.
self
(),
'
should identify self message true where message from userId
'
)
sandbox
.
stub
((
puppet
as
any
as
{
'
userId
'
:
string
}),
'
userId
'
)
.
value
(
'
fasdfafasdfsdf
'
)
const
otherMsg
=
wechaty
.
Message
.
create
MT
(
'
xxx
'
)
const
otherMsg
=
wechaty
.
Message
.
create
(
'
xxx
'
)
await
otherMsg
.
ready
()
t
.
false
(
otherMsg
.
self
(),
'
should identify self message false when from a different fromId
'
)
...
...
@@ -365,7 +364,7 @@ test('mentioned()', async t => {
sandbox
.
stub
(
puppet
as
any
,
'
messageRawPayload
'
).
callsFake
(
mockMessageRawPayload
)
// MyRoom.puppet = wechaty.Message.puppet = MyContact.puppet = puppet
const
msg11
=
wechaty
.
Message
.
create
MT
(
rawPayload11
.
MsgId
)
const
msg11
=
wechaty
.
Message
.
create
(
rawPayload11
.
MsgId
)
await
msg11
.
ready
()
const
room11
=
msg11
.
room
()
...
...
@@ -375,7 +374,7 @@ test('mentioned()', async t => {
t
.
is
(
mentionContactList11
.
length
,
0
,
'
@_@ in message should not be treat as contact
'
)
}
const
msg12
=
wechaty
.
Message
.
create
MT
(
rawPayload12
.
MsgId
)
const
msg12
=
wechaty
.
Message
.
create
(
rawPayload12
.
MsgId
)
await
msg12
.
ready
()
const
room12
=
msg12
.
room
()
if
(
room12
)
{
...
...
@@ -384,7 +383,7 @@ test('mentioned()', async t => {
t
.
is
(
mentionContactList12
.
length
,
0
,
'
user@email.com in message should not be treat as contact
'
)
}
const
msg13
=
wechaty
.
Message
.
create
MT
(
rawPayload13
.
MsgId
)
const
msg13
=
wechaty
.
Message
.
create
(
rawPayload13
.
MsgId
)
await
msg13
.
ready
()
const
room13
=
msg13
.
room
()
if
(
room13
)
{
...
...
@@ -395,7 +394,7 @@ test('mentioned()', async t => {
// }, 1 * 1000)
}
const
msg21
=
wechaty
.
Message
.
create
MT
(
rawPayload21
.
MsgId
)
const
msg21
=
wechaty
.
Message
.
create
(
rawPayload21
.
MsgId
)
await
msg21
.
ready
()
const
room21
=
msg21
.
room
()
if
(
room21
)
{
...
...
@@ -405,7 +404,7 @@ test('mentioned()', async t => {
t
.
is
(
mentionContactList21
[
0
].
id
,
'
@cd7d467d7464e8ff6b0acd29364654f3666df5d04551f6082bfc875f90a6afd2
'
,
'
should get 小桔同学 id right in rawPayload21
'
)
}
const
msg22
=
wechaty
.
Message
.
create
MT
(
rawPayload22
.
MsgId
)
const
msg22
=
wechaty
.
Message
.
create
(
rawPayload22
.
MsgId
)
await
msg22
.
ready
()
const
room22
=
msg22
.
room
()
...
...
@@ -418,7 +417,7 @@ test('mentioned()', async t => {
t
.
is
(
mentionContactList22
[
1
].
id
,
'
@cd7d467d7464e8ff6b0acd29364654f3666df5d04551f6082bfc875f90a6afd2
'
,
'
should get wuli舞哩客服 id right in rawPayload22
'
)
}
const
msg31
=
wechaty
.
Message
.
create
MT
(
rawPayload31
.
MsgId
)
const
msg31
=
wechaty
.
Message
.
create
(
rawPayload31
.
MsgId
)
await
msg31
.
ready
()
const
room31
=
msg31
.
room
()
...
...
@@ -429,7 +428,7 @@ test('mentioned()', async t => {
t
.
is
(
mentionContactList31
[
0
].
id
,
'
@36d55130f6a91bae4a2ed2cc5f19c56a9258c65ce3db9777f74f607223ef0855
'
,
'
should get wuli舞哩客服 id right in rawPayload31
'
)
}
const
msg32
=
wechaty
.
Message
.
create
MT
(
rawPayload32
.
MsgId
)
const
msg32
=
wechaty
.
Message
.
create
(
rawPayload32
.
MsgId
)
await
msg32
.
ready
()
const
room32
=
msg32
.
room
()
...
...
src/puppet-puppeteer/puppeteer-room.spec.ts
浏览文件 @
42693750
...
...
@@ -29,7 +29,9 @@ import Profile from '../profile'
import
Wechaty
from
'
../wechaty
'
import
PuppetPuppeteer
,
{
WebRoomRawPayload
}
from
'
./puppet-puppeteer
'
import
{
PuppetPuppeteer
}
from
'
./puppet-puppeteer
'
import
{
WebRoomRawPayload
}
from
'
./web-schemas
'
// import Contact from '../contact'
// import Message from '../message'
// import Room from '../room'
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录