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

code clean

上级 46faeb9a
...@@ -4,25 +4,30 @@ import { ...@@ -4,25 +4,30 @@ import {
, Room , Room
} from '../../' } from '../../'
export default async function onFriend(contact: Contact, request: FriendRequest): Promise<void> { export default async function onFriend(contact: Contact, request?: FriendRequest): Promise<void> {
try { try {
if (!request) {
console.log('New friend ' + contact.name() + ' relationship confirmed!')
return
}
/******************************************** /********************************************
* *
* 从这里开始修改 vvvvvvvvvvvv * 从这里开始修改 vvvvvvvvvvvv
* *
*/ */
await request.accept()
if (request.hello !== '上课') { setTimeout(function() {
return contact.say('thank you for adding me')
} }, 3000)
request.accept()
request.contact.say('thanks for coming for ' + request.hello)
const myRoom = await Room.find({ if (request.hello === 'ding') {
topic: 'ding' const myRoom = await Room.find({ topic: 'ding' })
}) setTimeout(function() {
myRoom.add(request.contact) myRoom.add(contact)
myRoom.say('welcome ' + contact.name())
}, 3000)
}
/** /**
* *
......
import { import {
Contact Contact
, Room , Room
, Sayable
} from '../../' } from '../../'
const arrify = require('arrify') const arrify = require('arrify')
export default async function onRoomJoin( export default async function onRoomJoin(
room: Room this: Sayable
, room: Room
, invitee: Contact|Contact[] , invitee: Contact|Contact[]
, inviter: Contact , inviter: Contact
): Promise<void> { ): Promise<void> {
try { try {
const inviteeName = arrify(invitee).map(c => c.name()).join(', ')
/******************************************** /********************************************
* *
* 从这里开始修改 vvvvvvvvvvvv * 从这里开始修改 vvvvvvvvvvvv
...@@ -19,11 +21,17 @@ export default async function onRoomJoin( ...@@ -19,11 +21,17 @@ export default async function onRoomJoin(
*/ */
if (room.topic() !== 'ding') { if (room.topic() !== 'ding') {
this.say('Room ' + room.topic()
+ ' got new memeber ' + inviteeName
+ ' invited by ' + inviter.name()
)
return return
} }
if (inviter.self()) { const inviterIsMyself = inviter.self()
room.say('Welcome to my room: ' + arrify(invitee).join(', '))
if (inviterIsMyself) {
room.say('Welcome to my room: ' + inviteeName)
return return
} }
......
import Config from './src/config' import {
Config
, Sayable
} from './src/config'
import Contact from './src/contact' import Contact from './src/contact'
import FriendRequest from './src/friend-request' import FriendRequest from './src/friend-request'
import IoClient from './src/io-client' import IoClient from './src/io-client'
...@@ -23,6 +26,7 @@ export { ...@@ -23,6 +26,7 @@ export {
, Puppet , Puppet
, PuppetWeb , PuppetWeb
, Room , Room
, Sayable
, UtilLib , UtilLib
, Wechaty , Wechaty
, log // for convenionce use npmlog with environment variable LEVEL , log // for convenionce use npmlog with environment variable LEVEL
......
...@@ -25,8 +25,8 @@ abstract class FriendRequest { ...@@ -25,8 +25,8 @@ abstract class FriendRequest {
} }
} }
public abstract send(contact: Contact, hello: string): void public abstract async send(contact: Contact, hello: string): Promise<void>
public abstract accept(): void public abstract async accept(): Promise<void>
} }
......
...@@ -76,7 +76,7 @@ class PuppetWebFriendRequest extends FriendRequest { ...@@ -76,7 +76,7 @@ class PuppetWebFriendRequest extends FriendRequest {
this.type = 'confirm' this.type = 'confirm'
} }
public send(contact: Contact, hello = 'Hi'): Promise<any> { public async send(contact: Contact, hello = 'Hi'): Promise<void> {
log.verbose('PuppetWebFriendRequest', 'send(%s)', contact) log.verbose('PuppetWebFriendRequest', 'send(%s)', contact)
if (!contact) { if (!contact) {
...@@ -89,11 +89,12 @@ class PuppetWebFriendRequest extends FriendRequest { ...@@ -89,11 +89,12 @@ class PuppetWebFriendRequest extends FriendRequest {
this.hello = hello this.hello = hello
} }
return Config.puppetInstance() await Config.puppetInstance()
.friendRequestSend(contact, hello) .friendRequestSend(contact, hello)
return
} }
public async accept(): Promise<any> { public async accept(): Promise<void> {
log.verbose('FriendRequest', 'accept() %s', this.contact) log.verbose('FriendRequest', 'accept() %s', this.contact)
if (this.type !== 'receive') { if (this.type !== 'receive') {
......
...@@ -220,26 +220,28 @@ export class Room extends EventEmitter implements Sayable { ...@@ -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]}`)) Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj && this.obj[k]}`))
} }
public add(contact: Contact): Promise<any> { public async add(contact: Contact): Promise<any> {
log.verbose('Room', 'add(%s)', contact) log.verbose('Room', 'add(%s)', contact)
if (!contact) { if (!contact) {
throw new Error('contact not found') throw new Error('contact not found')
} }
return Config.puppetInstance() await Config.puppetInstance()
.roomAdd(this, contact) .roomAdd(this, contact)
return
} }
public del(contact: Contact): Promise<number> { public async del(contact: Contact): Promise<number> {
log.verbose('Room', 'del(%s)', contact.name()) log.verbose('Room', 'del(%s)', contact.name())
if (!contact) { if (!contact) {
throw new Error('contact not found') throw new Error('contact not found')
} }
return Config.puppetInstance() const n = await Config.puppetInstance()
.roomDel(this, contact) .roomDel(this, contact)
.then(_ => this.delLocal(contact)) .then(_ => this.delLocal(contact))
return n
} }
// @private // @private
...@@ -404,7 +406,7 @@ export class Room extends EventEmitter implements Sayable { ...@@ -404,7 +406,7 @@ export class Room extends EventEmitter implements Sayable {
if (!roomList || roomList.length < 1) { if (!roomList || roomList.length < 1) {
throw new Error('no room found') throw new Error('no room found')
} }
return roomList[0] return roomList[0].ready()
} }
public static load(id: string): Room | null { public static load(id: string): Room | null {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册