diff --git a/example/gist/on-friend.ts b/example/gist/on-friend.ts index 349b4f9d0c4dd383722370adb1ed5df63f2ef7d5..9abe3abd365fdf2620c1dcdb1a3a3114ffb93c50 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 4f6792ba0c077dac88a200a7ccab39ee82c7b580..d883e7d04b3292afacec20785758a2f4b37aee26 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 e675a4c9a5e02f3fb7fbd925e993439a90a4c1a5..07c17f1fc922daa2a9a0ee158fe89f29be4895eb 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 5a552957e888d1294102a8e000727ec14ae9c7a3..4ed33b29da3ff3d86815c26ed6a97e51b7534410 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 9b2aa6fac9e5babb85723ef327dc16abdc6740b4..5d503876fa032882160be3d35bb5183b49a85c38 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 84fd333301a5600f4d6a068c0855ccc38f0003b0..72fad4cb5d0e66e6a0d14a2ddbd11feafc453af1 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 {