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

fix all static puppet bugs (#518)

上级 d60b9ce2
......@@ -18,32 +18,33 @@ cache:
directories:
- node_modules
before_install:
- if [ "$TRAVIS_OS_NAME" == 'osx' ]; then brew update; fi
- if [ "$TRAVIS_OS_NAME" == 'osx' ]; then brew cleanup; fi
- if [ "$TRAVIS_OS_NAME" == 'osx' ]; then brew cask cleanup; fi
- if [ "$TRAVIS_OS_NAME" == 'osx' ]; then brew install jq; fi
- if [ "$TRAVIS_OS_NAME" == 'osx' ]; then brew install moreutils; fi
- if [ "$TRAVIS_OS_NAME" == 'osx' ]; then brew install shellcheck; fi
script:
- echo $TRAVIS_OS_NAME
- node --version
- npm --version
- npm test
after_success:
- if [ "$TRAVIS_OS_NAME" == 'osx' ]; then npm run coverage; fi
stages:
- test
- pack
- name: deploy
if: branch =~ ^(master|v\d+\.\d+)
if: branch =~ ^(master|v\d+\.\d+)$
AND (type NOT IN (cron, pull_request))
jobs:
include:
- stage: test
before_install:
- if [ "$TRAVIS_OS_NAME" == 'osx' ]; then brew update; fi
- if [ "$TRAVIS_OS_NAME" == 'osx' ]; then brew cleanup; fi
- if [ "$TRAVIS_OS_NAME" == 'osx' ]; then brew cask cleanup; fi
- if [ "$TRAVIS_OS_NAME" == 'osx' ]; then brew install jq; fi
- if [ "$TRAVIS_OS_NAME" == 'osx' ]; then brew install moreutils; fi
- if [ "$TRAVIS_OS_NAME" == 'osx' ]; then brew install shellcheck; fi
script:
- echo $TRAVIS_OS_NAME
- node --version
- npm --version
- npm test
after_success:
- if [ "$TRAVIS_OS_NAME" == 'osx' ]; then npm run coverage; fi
- stage: pack
script:
- npm run test:pack && echo 'Npm pack testing is passed'
......@@ -60,6 +61,8 @@ jobs:
email: zixia@zixia.net
api_key: "$NPM_TOKEN"
skip_cleanup: true
on:
all_branches: true
notifications:
webhooks:
......
......@@ -29,7 +29,7 @@ const QrcodeTerminal = require('qrcode-terminal')
*/
import {
config,
Contact,
// Contact,
Wechaty,
log,
} from '../'
......@@ -88,7 +88,7 @@ bot.start()
* Main Contact Bot
*/
async function onLogin() {
const contactList = await Contact.findAll()
const contactList = await bot.Contact.findAll()
log.info('Bot', '#######################')
log.info('Bot', 'Contact number: %d\n', contactList.length)
......
......@@ -27,7 +27,7 @@ const QrcodeTerminal = require('qrcode-terminal')
*/
import {
config,
Contact,
// Contact,
log,
Wechaty,
} from '../'
......@@ -77,7 +77,7 @@ bot
*/
.on('friend', async (contact, request) => {
let logMsg
const fileHelper = Contact.load('filehelper')
const fileHelper = bot.Contact.load('filehelper')
try {
logMsg = 'received `friend` event from ' + contact.get('name')
......
......@@ -41,8 +41,9 @@ Please wait... I'm trying to login in...
`
console.log(welcome)
Wechaty.instance({ profile: config.default.DEFAULT_PROFILE })
const bot = Wechaty.instance({ profile: config.default.DEFAULT_PROFILE })
bot
.on('scan', (url, code) => {
if (!/201|200/.test(String(code))) {
const loginUrl = url.replace(/\/qrcode\//, '/l/')
......
......@@ -25,10 +25,11 @@
import {
Contact,
FriendRequest,
Room,
Wechaty,
// Room,
} from '../../'
export async function onFriend(contact: Contact, request?: FriendRequest): Promise<void> {
export async function onFriend(this: Wechaty, contact: Contact, request?: FriendRequest): Promise<void> {
try {
if (!request) {
console.log('New friend ' + contact.name() + ' relationship confirmed!')
......@@ -49,7 +50,7 @@ export async function onFriend(contact: Contact, request?: FriendRequest): Promi
)
if (request.hello === 'ding') {
const myRoom = await Room.find({ topic: 'ding' })
const myRoom = await this.Room.find({ topic: 'ding' })
if (!myRoom) return
setTimeout(
async _ => {
......
......@@ -24,10 +24,10 @@
*/
import {
Message,
Room,
Wechaty,
} from '../../'
export async function onMessage(message: Message): Promise<void> {
export async function onMessage(this: Wechaty, message: Message): Promise<void> {
try {
const room = message.room()
const sender = message.from()
......@@ -52,7 +52,7 @@ export async function onMessage(message: Message): Promise<void> {
if (content === 'ding') {
await message.say('thanks for ding me')
const myRoom = await Room.find({ topic: 'ding' })
const myRoom = await this.Room.find({ topic: 'ding' })
if (!myRoom) return
if (myRoom.has(sender)) {
......
......@@ -157,7 +157,7 @@ bot
/**
* Global Event: message
*/
.on('message', async function(this, message) {
.on('message', async function(this: Wechaty, message) {
const room = message.room()
const sender = message.from()
const content = message.content()
......@@ -197,7 +197,7 @@ bot
* find room name start with "ding"
*/
try {
const dingRoom = await Room.find({ topic: /^ding/i })
const dingRoom = await this.Room.find({ topic: /^ding/i })
if (dingRoom) {
/**
* room found
......@@ -254,7 +254,7 @@ async function manageDingRoom() {
* Find Room
*/
try {
const room = await Room.find({ topic: /^ding/i })
const room = await bot.Room.find({ topic: /^ding/i })
if (!room) {
log.warn('Bot', 'there is no room topic ding(yet)')
return
......
......@@ -112,7 +112,7 @@
"rx-queue": "^0.3.1",
"rxjs": "^5.5.0",
"state-switch": "^0.4.5",
"watchdog": "^0.3.0",
"watchdog": "^0.5.1",
"wechat4u": "^0.7.6",
"ws": "^5.1.0",
"xml2js": "^0.4.0"
......
......@@ -344,6 +344,8 @@ export class Contact extends PuppetAccessory implements Sayable {
let m
if (typeof textOrMedia === 'string') {
m = new Message()
m.puppet = this.puppet
m.content(textOrMedia)
} else if (textOrMedia instanceof MediaMessage) {
m = textOrMedia
......
......@@ -166,7 +166,10 @@ export class Message extends PuppetAccessory implements Sayable {
* @private
*/
public getSenderString() {
const fromName = Contact.load(this.obj.from).name()
const from = Contact.load(this.obj.from)
from.puppet = this.puppet
const fromName = from.name()
const roomTopic = this.obj.room
? (':' + Room.load(this.obj.room).topic())
: ''
......@@ -213,6 +216,8 @@ export class Message extends PuppetAccessory implements Sayable {
let m
if (typeof textOrMedia === 'string') {
m = new Message()
m.puppet = this.puppet
const room = this.room()
if (room) {
m.room(room)
......@@ -279,9 +284,8 @@ export class Message extends PuppetAccessory implements Sayable {
}
const loadedContact = Contact.load(this.obj.from)
if (!loadedContact) {
throw new Error('no from')
}
loadedContact.puppet = this.puppet
return loadedContact
}
......@@ -315,7 +319,9 @@ export class Message extends PuppetAccessory implements Sayable {
return
}
if (this.obj.room) {
return Room.load(this.obj.room)
const r = Room.load(this.obj.room)
r.puppet = this.puppet
return r
}
return null
}
......@@ -494,15 +500,20 @@ export class Message extends PuppetAccessory implements Sayable {
try {
const from = Contact.load(this.obj.from)
from.puppet = this.puppet
await from.ready() // Contact from
if (this.obj.to) {
const to = Contact.load(this.obj.to)
to.puppet = this.puppet
await to.ready()
}
if (this.obj.room) {
const room = Room.load(this.obj.room)
room.puppet = this.puppet
await room.ready() // Room member list
}
......@@ -616,7 +627,10 @@ export class Message extends PuppetAccessory implements Sayable {
if (!this.obj.to) {
return null
}
return Contact.load(this.obj.to)
const to = Contact.load(this.obj.to)
to.puppet = this.puppet
return to
}
/**
......@@ -691,10 +705,6 @@ export class MediaMessage extends Message {
} else {
throw new Error('not supported construct param')
}
// FIXME: decoupling needed
this.bridge = (/* config.puppetInstance() */ this.puppet as PuppetWeb)
.bridge
}
/**
......@@ -710,6 +720,11 @@ export class MediaMessage extends Message {
public async ready(): Promise<void> {
log.silly('MediaMessage', 'ready()')
// FIXME: decoupling needed
if (!this.bridge) {
this.bridge = (this.puppet as PuppetWeb).bridge
}
try {
await super.ready()
......
......@@ -135,6 +135,8 @@ async function onLogin(this: PuppetWeb, memo: string, ttl = 30): Promise<void> {
log.silly('PuppetWebEvent', 'bridge.getUserName: %s', this.userId)
this.user = Contact.load(this.userId)
this.user.puppet = this
await this.user.ready()
log.silly('PuppetWebEvent', `onLogin() user ${this.user.name()} logined`)
......@@ -180,6 +182,7 @@ async function onMessage(
obj: MsgRawObj,
): Promise<void> {
let m = new Message(obj)
m.puppet = this
try {
await m.ready()
......@@ -222,6 +225,7 @@ async function onMessage(
case MsgType.APP:
log.verbose('PuppetWebEvent', 'onMessage() EMOTICON/IMAGE/VIDEO/VOICE/MICROVIDEO message')
m = new MediaMessage(obj)
m.puppet = this
break
case MsgType.TEXT:
......
......@@ -112,6 +112,8 @@ async function checkFriendRequest(m: Message) {
}
const request = new FriendRequest()
request.puppet = m.puppet
request.receive(info)
await request.contact.ready()
......@@ -145,6 +147,8 @@ async function checkFriendConfirm(m: Message) {
return
}
const request = new FriendRequest()
request.puppet = m.puppet
const contact = m.from()
request.confirm(contact)
......@@ -216,6 +220,7 @@ async function checkRoomJoin(m: Message): Promise<boolean> {
try {
if (inviter === 'You' || inviter === '' || inviter === 'you') {
inviterContact = Contact.load(this.userId)
inviterContact.puppet = m.puppet
}
const max = 20
......@@ -354,6 +359,8 @@ async function checkRoomLeave(m: Message): Promise<boolean> {
let leaverContact: Contact | null, removerContact: Contact | null
if (leaver === this.userId) {
leaverContact = Contact.load(this.userId)
leaverContact.puppet = m.puppet
// not sure which is better
// removerContact = room.member({contactAlias: remover}) || room.member({name: remover})
removerContact = room.member(remover)
......@@ -361,8 +368,11 @@ async function checkRoomLeave(m: Message): Promise<boolean> {
log.error('PuppetWebFirer', 'fireRoomLeave() bot is removed from the room, but remover %s not found, event `room-leave` & `leave` will not be fired', remover)
return false
}
} else {
removerContact = Contact.load(this.userId)
removerContact.puppet = m.puppet
// not sure which is better
// leaverContact = room.member({contactAlias: remover}) || room.member({name: leaver})
leaverContact = room.member(remover)
......@@ -418,6 +428,7 @@ async function checkRoomTopic(m: Message): Promise<boolean> {
let changerContact: Contact | null
if (/^You$/.test(changer) || /^你$/.test(changer)) {
changerContact = Contact.load(this.userId)
changerContact.puppet = m.puppet
} else {
changerContact = room.member(changer)
}
......
......@@ -59,6 +59,7 @@ test('PuppetWebFriendRequest.confirm smoke testing', async t => {
`
const rawObj = JSON.parse(rawMessageData)
const m = new Message(rawObj)
m.puppet = this.puppet
t.true(/^You have added (.+) as your WeChat contact. Start chatting!$/.test(m.content()), 'should match confirm message')
......
......@@ -63,10 +63,8 @@ export class PuppetWebFriendRequest extends FriendRequest {
this.info = info
const contact = Contact.load(info.UserName)
if (!contact) {
log.warn('PuppetWebFriendRequest', 'receive() no contact found for "%s"', info.UserName)
throw new Error('no contact')
}
contact.puppet = this.puppet
this.contact = contact
this.hello = info.Content
this.ticket = info.Ticket
......
......@@ -22,8 +22,9 @@ import {
} from 'watchdog'
import {
ThrottleQueue,
} from 'rx-queue'
} from 'rx-queue'
import cloneClass from '../clone-class'
import {
config,
log,
......@@ -39,15 +40,14 @@ import {
Puppet,
PuppetOptions,
ScanData,
} from '../puppet'
import Room from '../room'
import Misc from '../misc'
} from '../puppet'
import Room from '../room'
import Misc from '../misc'
import {
Bridge,
Cookie,
} from './bridge'
import Event from './event'
} from './bridge'
import Event from './event'
import {
MediaData,
......@@ -794,7 +794,11 @@ export class PuppetWeb extends Puppet {
public async contactFind(filterFunc: string): Promise<Contact[]> {
try {
const idList = await this.bridge.contactFind(filterFunc)
return idList.map(id => Contact.load(id))
return idList.map(id => {
const c = Contact.load(id)
c.puppet = this
return c
})
} catch (e) {
log.warn('PuppetWeb', 'contactFind(%s) rejected: %s', filterFunc, e.message)
Raven.captureException(e)
......@@ -805,7 +809,11 @@ export class PuppetWeb extends Puppet {
public async roomFind(filterFunc: string): Promise<Room[]> {
try {
const idList = await this.bridge.roomFind(filterFunc)
return idList.map(id => Room.load(id))
return idList.map(id => {
const r = Room.load(id)
r.puppet = this
return r
})
} catch (e) {
log.warn('PuppetWeb', 'roomFind(%s) rejected: %s', filterFunc, e.message)
Raven.captureException(e)
......@@ -864,7 +872,9 @@ export class PuppetWeb extends Puppet {
if (!roomId) {
throw new Error('PuppetWeb.roomCreate() roomId "' + roomId + '" not found')
}
return Room.load(roomId)
const r = Room.load(roomId)
r.puppet = this
return r
} catch (e) {
log.warn('PuppetWeb', 'roomCreate(%s, %s) rejected: %s', contactIdList.join(','), topic, e.message)
......@@ -912,9 +922,14 @@ export class PuppetWeb extends Puppet {
log.verbose('PuppetWeb', 'readyStable()')
let counter = -1
// tslint:disable-next-line:variable-name
const MyContact = cloneClass(Contact)
MyContact.puppet = this
async function stable(done: Function): Promise<void> {
log.silly('PuppetWeb', 'readyStable() stable() counter=%d', counter)
const contactList = await Contact.findAll()
const contactList = await MyContact.findAll()
if (counter === contactList.length) {
log.verbose('PuppetWeb', 'readyStable() stable() READY counter=%d', counter)
return done()
......
......@@ -119,6 +119,7 @@ export class Room extends PuppetAccessory implements Sayable {
private async readyAllMembers(memberList: RoomRawMember[]): Promise<void> {
for (const member of memberList) {
const contact = Contact.load(member.UserName)
contact.puppet = this.puppet
await contact.ready()
}
return
......@@ -230,6 +231,7 @@ export class Room extends PuppetAccessory implements Sayable {
let m
if (typeof textOrMedia === 'string') {
m = new Message()
m.puppet = this.puppet
const replyToList: Contact[] = [].concat(replyTo as any || [])
......@@ -330,7 +332,11 @@ export class Room extends PuppetAccessory implements Sayable {
}
const memberList = (rawObj.MemberList || [])
.map(m => Contact.load(m.UserName))
.map(m => {
const c = Contact.load(m.UserName)
c.puppet = this.puppet
return c
})
const nameMap = this.parseMap('name', rawObj.MemberList)
const roomAliasMap = this.parseMap('roomAlias', rawObj.MemberList)
......@@ -357,6 +363,8 @@ export class Room extends PuppetAccessory implements Sayable {
memberList.forEach(member => {
let tmpName: string
const contact = Contact.load(member.UserName)
contact.puppet = this.puppet
switch (parseContent) {
case 'name':
tmpName = contact.name()
......@@ -716,7 +724,11 @@ export class Room extends PuppetAccessory implements Sayable {
log.silly('Room', 'memberAll() check %s from %s: %s', filterValue, filterKey, JSON.stringify(filterMap))
if (idList.length) {
return idList.map(id => Contact.load(id))
return idList.map(id => {
const c = Contact.load(id)
c.puppet = this.puppet
return c
})
} else {
return []
}
......@@ -926,7 +938,9 @@ export class Room extends PuppetAccessory implements Sayable {
}
if (this.rawObj.ChatRoomOwner) {
return Contact.load(this.rawObj.ChatRoomOwner)
const c = Contact.load(this.rawObj.ChatRoomOwner)
c.puppet = this.puppet
return c
}
log.info('Room', 'owner() is limited by Tencent API, sometimes work sometimes not')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册