From 0ecd31c12d1dc294ad64c30e2db85e48fff1765f Mon Sep 17 00:00:00 2001 From: "Zhuohuan LI (CARPE DIEM)" Date: Sat, 22 Oct 2016 16:45:48 +0800 Subject: [PATCH] code clean --- example/gist/on-friend.ts | 27 ++++++++++++++++----------- example/gist/on-room-join.ts | 16 ++++++++++++---- index.ts | 6 +++++- src/friend-request.ts | 4 ++-- src/puppet-web/friend-request.ts | 9 +++++---- src/room.ts | 14 ++++++++------ 6 files changed, 48 insertions(+), 28 deletions(-) diff --git a/example/gist/on-friend.ts b/example/gist/on-friend.ts index 349b4f9d..9abe3abd 100644 --- a/example/gist/on-friend.ts +++ b/example/gist/on-friend.ts @@ -4,25 +4,30 @@ import { , Room } from '../../' -export default async function onFriend(contact: Contact, request: FriendRequest): Promise { +export default async function onFriend(contact: Contact, request?: FriendRequest): Promise { try { + if (!request) { + console.log('New friend ' + contact.name() + ' relationship confirmed!') + return + } /******************************************** * * 从这里开始修改 vvvvvvvvvvvv * */ + await request.accept() - if (request.hello !== '上课') { - return - } - - request.accept() - request.contact.say('thanks for coming for ' + request.hello) + setTimeout(function() { + contact.say('thank you for adding me') + }, 3000) - const myRoom = await Room.find({ - topic: 'ding' - }) - myRoom.add(request.contact) + if (request.hello === 'ding') { + const myRoom = await Room.find({ topic: 'ding' }) + setTimeout(function() { + myRoom.add(contact) + myRoom.say('welcome ' + contact.name()) + }, 3000) + } /** * diff --git a/example/gist/on-room-join.ts b/example/gist/on-room-join.ts index 4f6792ba..d883e7d0 100644 --- a/example/gist/on-room-join.ts +++ b/example/gist/on-room-join.ts @@ -1,17 +1,19 @@ import { Contact , Room + , Sayable } from '../../' const arrify = require('arrify') export default async function onRoomJoin( - room: Room + this: Sayable + , room: Room , invitee: Contact|Contact[] , inviter: Contact ): Promise { try { - + const inviteeName = arrify(invitee).map(c => c.name()).join(', ') /******************************************** * * 从这里开始修改 vvvvvvvvvvvv @@ -19,11 +21,17 @@ export default async function onRoomJoin( */ if (room.topic() !== 'ding') { + this.say('Room ' + room.topic() + + ' got new memeber ' + inviteeName + + ' invited by ' + inviter.name() + ) return } - if (inviter.self()) { - room.say('Welcome to my room: ' + arrify(invitee).join(', ')) + const inviterIsMyself = inviter.self() + + if (inviterIsMyself) { + room.say('Welcome to my room: ' + inviteeName) return } diff --git a/index.ts b/index.ts index e675a4c9..07c17f1f 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,7 @@ -import Config from './src/config' +import { + Config + , Sayable +} from './src/config' import Contact from './src/contact' import FriendRequest from './src/friend-request' import IoClient from './src/io-client' @@ -23,6 +26,7 @@ export { , Puppet , PuppetWeb , Room + , Sayable , UtilLib , Wechaty , log // for convenionce use npmlog with environment variable LEVEL diff --git a/src/friend-request.ts b/src/friend-request.ts index 5a552957..4ed33b29 100644 --- a/src/friend-request.ts +++ b/src/friend-request.ts @@ -25,8 +25,8 @@ abstract class FriendRequest { } } - public abstract send(contact: Contact, hello: string): void - public abstract accept(): void + public abstract async send(contact: Contact, hello: string): Promise + public abstract async accept(): Promise } diff --git a/src/puppet-web/friend-request.ts b/src/puppet-web/friend-request.ts index 9b2aa6fa..5d503876 100644 --- a/src/puppet-web/friend-request.ts +++ b/src/puppet-web/friend-request.ts @@ -76,7 +76,7 @@ class PuppetWebFriendRequest extends FriendRequest { this.type = 'confirm' } - public send(contact: Contact, hello = 'Hi'): Promise { + public async send(contact: Contact, hello = 'Hi'): Promise { log.verbose('PuppetWebFriendRequest', 'send(%s)', contact) if (!contact) { @@ -89,11 +89,12 @@ class PuppetWebFriendRequest extends FriendRequest { this.hello = hello } - return Config.puppetInstance() - .friendRequestSend(contact, hello) + await Config.puppetInstance() + .friendRequestSend(contact, hello) + return } - public async accept(): Promise { + public async accept(): Promise { log.verbose('FriendRequest', 'accept() %s', this.contact) if (this.type !== 'receive') { diff --git a/src/room.ts b/src/room.ts index 84fd3333..72fad4cb 100644 --- a/src/room.ts +++ b/src/room.ts @@ -220,26 +220,28 @@ export class Room extends EventEmitter implements Sayable { Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj && this.obj[k]}`)) } - public add(contact: Contact): Promise { + public async add(contact: Contact): Promise { log.verbose('Room', 'add(%s)', contact) if (!contact) { throw new Error('contact not found') } - return Config.puppetInstance() - .roomAdd(this, contact) + await Config.puppetInstance() + .roomAdd(this, contact) + return } - public del(contact: Contact): Promise { + public async del(contact: Contact): Promise { log.verbose('Room', 'del(%s)', contact.name()) if (!contact) { throw new Error('contact not found') } - return Config.puppetInstance() + const n = await Config.puppetInstance() .roomDel(this, contact) .then(_ => this.delLocal(contact)) + return n } // @private @@ -404,7 +406,7 @@ export class Room extends EventEmitter implements Sayable { if (!roomList || roomList.length < 1) { throw new Error('no room found') } - return roomList[0] + return roomList[0].ready() } public static load(id: string): Room | null { -- GitLab