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

PuppetMock can emit new Message! (#1177)

上级 6eac5c3e
......@@ -73,6 +73,7 @@ export class PuppetMock extends Puppet {
const m = new MockMessage()
m.from(from).to(to).text('mock hello')
this.user = to
this.emit('login', to)
setInterval(() => {
......@@ -96,18 +97,21 @@ export class PuppetMock extends Puppet {
}
public logonoff(): boolean {
if (this.userSelf()) {
if (this.user) {
return true
} else {
return false
}
}
public userSelf(): MockContact | null {
public userSelf(): MockContact {
log.verbose('PuppetMock', 'self()')
if (!this.user) {
throw new Error('not logged in, no userSelf yet.')
}
return this.user
? this.user
: null
}
public async forward(message: MockMessage, sendTo: MockContact | MockRoom): Promise<void> {
......@@ -132,13 +136,7 @@ export class PuppetMock extends Puppet {
return
}
if (!this.user) {
log.warn('PuppetMock', 'say(%s) can not say because no user', text)
this.emit('error', new Error('no this.user for PuppetMock'))
return
}
return await this.user.say(text)
return await this.userSelf().say(text)
}
public async login(user: MockContact): Promise<void> {
......@@ -150,13 +148,12 @@ export class PuppetMock extends Puppet {
public async logout(): Promise<void> {
log.verbose('PuppetMock', 'logout()')
const user = this.user
if (!user) {
throw new Error('logout without user?')
if (!this.logonoff()) {
throw new Error('logout before login?')
}
this.emit('logout', this.user!) // becore we will throw above by logonoff() when this.user===undefined
this.user = undefined
this.emit('logout', user)
}
public contactAlias(contact: MockContact) : Promise<string>
......
......@@ -300,11 +300,14 @@ export class PuppetPuppeteer extends Puppet {
/**
* get self contact
*/
public userSelf(): PuppeteerContact | null {
public userSelf(): PuppeteerContact {
log.verbose('PuppetPuppeteer', 'self()')
if (!this.user) {
throw new Error('not logged in, no userSelf yet.')
}
return this.user
? this.user
: null
}
private async getBaseRequest(): Promise<any> {
......
......@@ -192,7 +192,7 @@ export abstract class Puppet extends EventEmitter implements Sayable {
public abstract async start() : Promise<void>
public abstract async stop() : Promise<void>
public abstract userSelf(): Contact | null
public abstract userSelf(): Contact
/**
* Message
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册