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

code clean

上级 74a6efd0
......@@ -28,7 +28,6 @@ const finis = require('finis')
* when you are runing with Docker or NPM instead of Git Source.
*/
import {
config,
Wechaty,
log,
} from '../src/'
......@@ -62,7 +61,7 @@ Please wait... I'm trying to login in...
`
console.log(welcome)
const bot = Wechaty.instance({ profile: config.default.DEFAULT_PROFILE })
const bot = Wechaty.instance()
bot
.on('logout' , user => log.info('Bot', `${user.name()} logouted`))
......
......@@ -8,14 +8,14 @@
"DEFAULT_HEAD": 0,
"DEFAULT_PORT": 8080,
"DEFAULT_PUPPET": "puppeteer",
"DEFAULT_PROFILE": "demo",
"DEFAULT_PROFILE": "default",
"DEFAULT_PROTOCOL": "io|0.0.1",
"DEFAULT_TOKEN": "WECHATY_IO_TOKEN",
"DEFAULT_APIHOST": "api.chatie.io"
},
"scripts": {
"clean": "shx rm -fr dist/*",
"dist": "npm run clean && tsc && shx cp src/puppet-web/*.js dist/src/puppet-web/",
"dist": "npm run clean && tsc && shx cp src/puppet-puppeteer/*.js dist/src/puppet-puppeteer/",
"doc": "npm run dist && echo '# Wechaty v'$(jq -r .version package.json)' Documentation\n* https://blog.chatie.io\n' > docs/index.md && jsdoc2md dist/src/{wechaty,room,contact,friend-request,message}.js dist/src/puppet-web/{friend-request,schema}.js>> docs/index.md",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"changelog": "github_changelog_generator -u chatie -p wechaty && sed -i'.bak' /greenkeeper/d CHANGELOG.md && sed -i'.bak' '/An in-range update of/d' CHANGELOG.md && ts-node scripts/sort-contributiveness.ts < CHANGELOG.md > CHANGELOG.new.md 2>/dev/null && cat CHANGELOG.md >> CHANGELOG.new.md && mv CHANGELOG.new.md CHANGELOG.md",
......
......@@ -28,9 +28,12 @@ import PuppetAccessory from '../puppet-accessory'
import Contact from './contact'
import Message from './message'
export type RoomEventName = 'join'
| 'leave'
| 'topic'
export const ROOM_EVENT_DICT = {
join: 'tbw',
leave: 'tbw',
topic: 'tbw',
}
export type RoomEventName = keyof typeof ROOM_EVENT_DICT
export interface RoomMemberQueryFilter {
name?: string,
......@@ -51,6 +54,104 @@ export interface RoomQueryFilter {
export abstract class Room extends PuppetAccessory implements Sayable {
protected static readonly pool = new Map<string, Room>()
/**
* Create a new room.
*
* @static
* @param {Contact[]} contactList
* @param {string} [topic]
* @returns {Promise<Room>}
* @example <caption>Creat a room with 'lijiarui' and 'juxiaomi', the room topic is 'ding - created'</caption>
* const helperContactA = await Contact.find({ name: 'lijiarui' }) // change 'lijiarui' to any contact in your wechat
* const helperContactB = await Contact.find({ name: 'juxiaomi' }) // change 'juxiaomi' to any contact in your wechat
* const contactList = [helperContactA, helperContactB]
* console.log('Bot', 'contactList: %s', contactList.join(','))
* const room = await Room.create(contactList, 'ding')
* console.log('Bot', 'createDingRoom() new ding room created: %s', room)
* await room.topic('ding - created')
* await room.say('ding - created')
*/
public static async create(contactList: Contact[], topic?: string): Promise<Room> {
log.verbose('Room', 'create(%s, %s)', contactList.join(','), topic)
if (!contactList || !Array.isArray(contactList)) {
throw new Error('contactList not found')
}
try {
const room = await this.puppet.roomCreate(contactList, topic)
return room
} catch (e) {
log.error('Room', 'create() exception: %s', e && e.stack || e.message || e)
Raven.captureException(e)
throw e
}
}
/**
* Find room by topic, return all the matched room
*
* @static
* @param {RoomQueryFilter} [query]
* @returns {Promise<Room[]>}
* @example
* const roomList = await Room.findAll() // get the room list of the bot
* const roomList = await Room.findAll({name: 'wechaty'}) // find all of the rooms with name 'wechaty'
*/
public static async findAll(
query: RoomQueryFilter = { topic: /.*/ },
): Promise<Room[]> {
log.verbose('Room', 'findAll({ topic: %s })', query.topic)
if (!query.topic) {
throw new Error('topicFilter not found')
}
try {
const roomList = await this.puppet.roomFindAll(query)
await Promise.all(roomList.map(room => room.ready()))
return roomList
} catch (e) {
log.verbose('Room', 'findAll() rejected: %s', e.message)
Raven.captureException(e)
return [] as Room[] // fail safe
}
}
/**
* Try to find a room by filter: {topic: string | RegExp}. If get many, return the first one.
*
* @param {RoomQueryFilter} query
* @returns {Promise<Room | null>} If can find the room, return Room, or return null
*/
public static async find(query: RoomQueryFilter): Promise<Room | null> {
log.verbose('Room', 'find({ topic: %s })', query.topic)
const roomList = await this.findAll(query)
if (!roomList || roomList.length < 1) {
return null
} else if (roomList.length > 1) {
log.warn('Room', 'find() got more than one result, return the 1st one.')
}
return roomList[0]
}
/**
* @private
*/
public static load(id: string): Room {
if (!id) {
throw new Error('Room.load() no id')
}
if (id in this.pool) {
return this.pool[id]
}
return this.pool[id] = new (this as any)(id)
}
/**
* @private
*/
......@@ -347,90 +448,6 @@ export abstract class Room extends PuppetAccessory implements Sayable {
*/
public abstract memberList(): Contact[]
/**
* Create a new room.
*
* @static
* @param {Contact[]} contactList
* @param {string} [topic]
* @returns {Promise<Room>}
* @example <caption>Creat a room with 'lijiarui' and 'juxiaomi', the room topic is 'ding - created'</caption>
* const helperContactA = await Contact.find({ name: 'lijiarui' }) // change 'lijiarui' to any contact in your wechat
* const helperContactB = await Contact.find({ name: 'juxiaomi' }) // change 'juxiaomi' to any contact in your wechat
* const contactList = [helperContactA, helperContactB]
* console.log('Bot', 'contactList: %s', contactList.join(','))
* const room = await Room.create(contactList, 'ding')
* console.log('Bot', 'createDingRoom() new ding room created: %s', room)
* await room.topic('ding - created')
* await room.say('ding - created')
*/
public static async create(contactList: Contact[], topic?: string): Promise<Room> {
log.verbose('Room', 'create(%s, %s)', contactList.join(','), topic)
if (!contactList || !Array.isArray(contactList)) {
throw new Error('contactList not found')
}
try {
const room = await this.puppet.roomCreate(contactList, topic)
return room
} catch (e) {
log.error('Room', 'create() exception: %s', e && e.stack || e.message || e)
Raven.captureException(e)
throw e
}
}
/**
* Find room by topic, return all the matched room
*
* @static
* @param {RoomQueryFilter} [query]
* @returns {Promise<Room[]>}
* @example
* const roomList = await Room.findAll() // get the room list of the bot
* const roomList = await Room.findAll({name: 'wechaty'}) // find all of the rooms with name 'wechaty'
*/
public static async findAll(
query: RoomQueryFilter = { topic: /.*/ },
): Promise<Room[]> {
log.verbose('Room', 'findAll({ topic: %s })', query.topic)
if (!query.topic) {
throw new Error('topicFilter not found')
}
try {
const roomList = await this.puppet.roomFindAll(query)
await Promise.all(roomList.map(room => room.ready()))
return roomList
} catch (e) {
log.verbose('Room', 'findAll() rejected: %s', e.message)
Raven.captureException(e)
return [] as Room[] // fail safe
}
}
/**
* Try to find a room by filter: {topic: string | RegExp}. If get many, return the first one.
*
* @param {RoomQueryFilter} query
* @returns {Promise<Room | null>} If can find the room, return Room, or return null
*/
public static async find(query: RoomQueryFilter): Promise<Room | null> {
log.verbose('Room', 'find({ topic: %s })', query.topic)
const roomList = await this.findAll(query)
if (!roomList || roomList.length < 1) {
return null
} else if (roomList.length > 1) {
log.warn('Room', 'find() got more than one result, return the 1st one.')
}
return roomList[0]
}
/**
* Force reload data for Room
*
......@@ -446,20 +463,6 @@ export abstract class Room extends PuppetAccessory implements Sayable {
*/
public abstract owner(): Contact | null
/**
* @private
*/
public static load(id: string): Room {
if (!id) {
throw new Error('Room.load() no id')
}
if (id in this.pool) {
return this.pool[id]
}
return this.pool[id] = new (this as any)(id)
}
}
export default Room
......@@ -4,6 +4,14 @@ export {
VERSION,
} from './config'
/**
* We need to put `Wechaty` at the beginning of this file for import
* because we have circluar dependencies between `Puppet` & `Wechaty`
*/
import {
Wechaty,
} from './wechaty'
export {
Contact,
FriendRequest,
......@@ -16,16 +24,13 @@ export {
} from './deprecated'
export { IoClient } from './io-client'
export { Profile } from './profile'
export { Misc } from './misc'
export { Profile } from './profile'
export {
PuppetPuppeteer,
} from './puppet-puppeteer/'
import {
Wechaty,
} from './wechaty'
export {
Wechaty,
}
......
......@@ -15,6 +15,7 @@ export abstract class PuppetAccessory extends EventEmitter {
log.silly('PuppetAssessory', 'static set puppet(%s)', puppet.constructor.name)
this._puppet = puppet
}
public static get puppet(): Puppet {
log.silly('PuppetAssessory', 'static get puppet()')
......@@ -34,6 +35,7 @@ export abstract class PuppetAccessory extends EventEmitter {
log.silly('PuppetAssessory', 'set puppet(%s)', puppet.constructor.name)
this._puppet = puppet
}
public get puppet(): Puppet {
log.silly('PuppetAssessory', 'get puppet() from instance')
......
......@@ -2,14 +2,7 @@ import PuppetPuppeteer from './puppet-puppeteer/'
import PuppetMock from './puppet-mock/'
/**
* would be nice if we have a typing from package.json schema
*
* https://github.com/DefinitelyTyped/DefinitelyTyped/issues/15602
* https://github.com/Microsoft/TypeScript/issues/3136
*/
/**
* Puppet Official Plugins List
* Wechaty Official Puppet Plugins List
*/
export const PUPPET_DICT = {
mock: PuppetMock,
......
......@@ -109,7 +109,7 @@ async function onLogin(this: PuppetPuppeteer, note: string, ttl = 30): Promise<v
return
}
this.scanInfo = null
this.scanInfo = undefined
if (this.user) {
log.warn('PuppetPuppeteerEvent', 'onLogin(%s) user had already set: "%s"', note, this.user)
......
......@@ -56,17 +56,17 @@ import {
MsgRawObj,
MediaType,
MsgType,
} from './schema'
} from './schema'
import {
PuppeteerContact,
PuppeteerContactRawObj,
} from './puppeteer-contact'
} from './puppeteer-contact'
import PuppeteerMessage from './puppeteer-message'
import {
PuppeteerRoom,
PuppeteerRoomRawObj,
} from './puppeteer-room'
} from './puppeteer-room'
import PuppeteerFriendRequest from './puppeteer-friend-request'
export type PuppetFoodType = 'scan' | 'ding'
......@@ -74,7 +74,7 @@ export type ScanFoodType = 'scan' | 'login' | 'logout'
export class PuppetPuppeteer extends Puppet {
public bridge : Bridge
public scanInfo : ScanData | null
public scanInfo?: ScanData
public scanWatchdog: Watchdog<ScanFoodType>
......
......@@ -776,7 +776,7 @@
/*
* WechatyBro injectio must return this object.
* PuppetWebBridge need this to decide if injection is successful.
* PuppetPuppeteerBridge need this to decide if injection is successful.
*/
var retObj = {
code: 200, // 2XX ok, 4XX/5XX error. HTTP like
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册