提交 42693750 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

clean code

上级 f0cd6834
......@@ -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 createMT(
public static create(
id : string,
payload? : MessagePayload,
): Message {
log.verbose('Message', 'static createMobileTerminated(%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
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,
}
......@@ -93,7 +93,7 @@ export class PuppetMock extends Puppet {
const user = this.Contact.load(this.userId)
this.emit('login', user)
const msg = this.Message.createMT('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,
......
......@@ -44,7 +44,7 @@ import {
} from '../puppet-puppeteer/web-schemas'
import {
WebRoomRawPayload,
} from './puppet-puppeteer'
} from './web-schemas'
export interface InjectResult {
code: number,
......
......@@ -190,7 +190,7 @@ async function onMessage(
this : PuppetPuppeteer,
rawPayload : WebMessageRawPayload,
): Promise<void> {
const msg = this.Message.createMT(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.createMT(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.createMT(rawPayload.MsgId)
// msg = this.Message.create(rawPayload.MsgId)
// }
// break
// }
......
......@@ -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
......@@ -109,7 +109,7 @@ test('PuppetPuppeteerFriendRequest.confirm smoke testing', async t => {
// ;
// (puppet as any).messageRawPayload()
const msg = wechaty.Message.createMT(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')
......
......@@ -103,7 +103,7 @@ test('constructor()', async t => {
id: '179242112323992762',
from: '@0bb3e4dd746fdbd4a80546aef66f4085',
}
const msg = wechaty.Message.createMT(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.createMT(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.createMT('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.createMT('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.createMT(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.createMT(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.createMT(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.createMT(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.createMT(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.createMT(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.createMT(rawPayload32.MsgId)
const msg32 = wechaty.Message.create(rawPayload32.MsgId)
await msg32.ready()
const room32 = msg32.room()
......
......@@ -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.
先完成此消息的编辑!
想要评论请 注册