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

Follow the puppet@0.19 abstract design

上级 fb47be47
...@@ -97,7 +97,7 @@ ...@@ -97,7 +97,7 @@
"read-pkg-up": "^7.0.0", "read-pkg-up": "^7.0.0",
"state-switch": "^0.6.2", "state-switch": "^0.6.2",
"watchdog": "^0.8.1", "watchdog": "^0.8.1",
"wechaty-puppet": "^0.19.5", "wechaty-puppet": "^0.19.6",
"ws": "^7.0.0" "ws": "^7.0.0"
}, },
"devDependencies": { "devDependencies": {
......
...@@ -121,7 +121,7 @@ export class ContactSelf extends Contact { ...@@ -121,7 +121,7 @@ export class ContactSelf extends Contact {
throw new Error('only can get qrcode for the login userself') throw new Error('only can get qrcode for the login userself')
} }
const qrcodeValue = await this.puppet.contactSelfQrcode() const qrcodeValue = await this.puppet.contactSelfQRCode()
return guardQrCodeValue(qrcodeValue) return guardQrCodeValue(qrcodeValue)
} }
......
...@@ -397,37 +397,42 @@ export class Contact extends Accessory implements Sayable { ...@@ -397,37 +397,42 @@ export class Contact extends Accessory implements Sayable {
/** /**
* 1. Text * 1. Text
*/ */
msgId = await this.puppet.messageSendText({ msgId = await this.puppet.messageSendText(
contactId: this.id, this.id,
}, something) something,
)
} else if (something instanceof Contact) { } else if (something instanceof Contact) {
/** /**
* 2. Contact * 2. Contact
*/ */
msgId = await this.puppet.messageSendContact({ msgId = await this.puppet.messageSendContact(
contactId: this.id, this.id,
}, something.id) something.id,
)
} else if (something instanceof FileBox) { } else if (something instanceof FileBox) {
/** /**
* 3. File * 3. File
*/ */
msgId = await this.puppet.messageSendFile({ msgId = await this.puppet.messageSendFile(
contactId: this.id, this.id,
}, something) something,
)
} else if (something instanceof UrlLink) { } else if (something instanceof UrlLink) {
/** /**
* 4. Link Message * 4. Link Message
*/ */
msgId = await this.puppet.messageSendUrl({ msgId = await this.puppet.messageSendUrl(
contactId : this.id, this.id,
}, something.payload) something.payload,
)
} else if (something instanceof MiniProgram) { } else if (something instanceof MiniProgram) {
/** /**
* 5. Mini Program * 5. Mini Program
*/ */
msgId = await this.puppet.messageSendMiniProgram({ msgId = await this.puppet.messageSendMiniProgram(
contactId : this.id, this.id,
}, something.payload) something.payload,
)
} else { } else {
throw new Error('unsupported arg: ' + something) throw new Error('unsupported arg: ' + something)
} }
......
...@@ -518,47 +518,61 @@ export class Message extends Accessory implements Sayable { ...@@ -518,47 +518,61 @@ export class Message extends Accessory implements Sayable {
const from = this.from() const from = this.from()
// const to = this.to() // const to = this.to()
const room = this.room() const room = this.room()
let conversationId: string
if (room) {
conversationId = room.id
} else if (from) {
conversationId = from.id
} else {
throw new Error('neither room nor from?')
}
let msgId: void | string let msgId: void | string
if (typeof textOrContactOrFileOrUrlOrMini === 'string') { if (typeof textOrContactOrFileOrUrlOrMini === 'string') {
/** /**
* Text Message * Text Message
*/ */
msgId = await this.puppet.messageSendText({ // msgId = await this.puppet.messageSendText({
contactId : (from && from.id) || undefined, // contactId : (from && from.id) || undefined,
roomId : (room && room.id) || undefined, // roomId : (room && room.id) || undefined,
}, textOrContactOrFileOrUrlOrMini) // }, textOrContactOrFileOrUrlOrMini)
msgId = await this.puppet.messageSendText(
conversationId,
textOrContactOrFileOrUrlOrMini,
)
} else if (textOrContactOrFileOrUrlOrMini instanceof Contact) { } else if (textOrContactOrFileOrUrlOrMini instanceof Contact) {
/** /**
* Contact Card * Contact Card
*/ */
msgId = await this.puppet.messageSendContact({ msgId = await this.puppet.messageSendContact(
contactId : (from && from.id) || undefined, conversationId,
roomId : (room && room.id) || undefined, textOrContactOrFileOrUrlOrMini.id,
}, textOrContactOrFileOrUrlOrMini.id) )
} else if (textOrContactOrFileOrUrlOrMini instanceof FileBox) { } else if (textOrContactOrFileOrUrlOrMini instanceof FileBox) {
/** /**
* File Message * File Message
*/ */
msgId = await this.puppet.messageSendFile({ msgId = await this.puppet.messageSendFile(
contactId : (from && from.id) || undefined, conversationId,
roomId : (room && room.id) || undefined, textOrContactOrFileOrUrlOrMini,
}, textOrContactOrFileOrUrlOrMini) )
} else if (textOrContactOrFileOrUrlOrMini instanceof UrlLink) { } else if (textOrContactOrFileOrUrlOrMini instanceof UrlLink) {
/** /**
* Link Message * Link Message
*/ */
msgId = await this.puppet.messageSendUrl({ msgId = await this.puppet.messageSendUrl(
contactId : (from && from.id) || undefined, conversationId,
roomId : (room && room.id) || undefined, textOrContactOrFileOrUrlOrMini.payload,
}, textOrContactOrFileOrUrlOrMini.payload) )
} else if (textOrContactOrFileOrUrlOrMini instanceof MiniProgram) { } else if (textOrContactOrFileOrUrlOrMini instanceof MiniProgram) {
/** /**
* MiniProgram * MiniProgram
*/ */
msgId = await this.puppet.messageSendMiniProgram({ msgId = await this.puppet.messageSendMiniProgram(
contactId : (from && from.id) || undefined, conversationId,
roomId : (room && room.id) || undefined, textOrContactOrFileOrUrlOrMini.payload,
}, textOrContactOrFileOrUrlOrMini.payload) )
} else { } else {
throw new Error('unknown msg: ' + textOrContactOrFileOrUrlOrMini) throw new Error('unknown msg: ' + textOrContactOrFileOrUrlOrMini)
} }
...@@ -884,21 +898,12 @@ export class Message extends Accessory implements Sayable { ...@@ -884,21 +898,12 @@ export class Message extends Accessory implements Sayable {
public async forward (to: Room | Contact): Promise<void> { public async forward (to: Room | Contact): Promise<void> {
log.verbose('Message', 'forward(%s)', to) log.verbose('Message', 'forward(%s)', to)
let roomId // let roomId
let contactId // let contactId
if (to instanceof Room) {
roomId = to.id
} else if (to instanceof Contact) {
contactId = to.id
}
try { try {
await this.puppet.messageForward( await this.puppet.messageForward(
{ to.id,
contactId,
roomId,
},
this.id, this.id,
) )
} catch (e) { } catch (e) {
......
...@@ -92,7 +92,7 @@ export class RoomInvitation extends Accessory implements Acceptable { ...@@ -92,7 +92,7 @@ export class RoomInvitation extends Accessory implements Acceptable {
'RoomInvitation#', 'RoomInvitation#',
this.id, this.id,
'<', '<',
payload.roomTopic, payload.topic,
',', ',',
payload.inviterId, payload.inviterId,
'>', '>',
...@@ -177,7 +177,7 @@ export class RoomInvitation extends Accessory implements Acceptable { ...@@ -177,7 +177,7 @@ export class RoomInvitation extends Accessory implements Acceptable {
const payload = await this.puppet.roomInvitationPayload(this.id) const payload = await this.puppet.roomInvitationPayload(this.id)
// roomTopic deprecated. use topic instead: // roomTopic deprecated. use topic instead:
return payload.topic || payload.roomTopic || '' return payload.topic || payload.topic || ''
} }
/** /**
...@@ -194,7 +194,7 @@ export class RoomInvitation extends Accessory implements Acceptable { ...@@ -194,7 +194,7 @@ export class RoomInvitation extends Accessory implements Acceptable {
const payload = await this.puppet.roomInvitationPayload(this.id) const payload = await this.puppet.roomInvitationPayload(this.id)
// roomMemberCount deprecated. use memberCount instead: // roomMemberCount deprecated. use memberCount instead:
return payload.memberCount || payload.roomMemberCount || 0 return payload.memberCount || payload.memberCount || 0
} }
/** /**
...@@ -216,7 +216,7 @@ export class RoomInvitation extends Accessory implements Acceptable { ...@@ -216,7 +216,7 @@ export class RoomInvitation extends Accessory implements Acceptable {
const payload = await this.puppet.roomInvitationPayload(this.id) const payload = await this.puppet.roomInvitationPayload(this.id)
// roomMemberIdList deprecated. use memberIdList isntead. // roomMemberIdList deprecated. use memberIdList isntead.
const contactIdList = payload.memberIdList || payload.roomMemberIdList || [] const contactIdList = payload.memberIdList || payload.memberIdList || []
const contactList = contactIdList.map( const contactList = contactIdList.map(
id => this.wechaty.Contact.load(id), id => this.wechaty.Contact.load(id),
......
...@@ -113,7 +113,8 @@ test('say()', async () => { ...@@ -113,7 +113,8 @@ test('say()', async () => {
await room.say`To be ${contact1} or not to be ${contact2}` await room.say`To be ${contact1} or not to be ${contact2}`
t.deepEqual(callback.getCall(0).args, [ t.deepEqual(callback.getCall(0).args, [
{ contactId: EXPECTED_CONTACT_1_ID, roomId: EXPECTED_ROOM_ID }, // { contactId: EXPECTED_CONTACT_1_ID, roomId: EXPECTED_ROOM_ID },
EXPECTED_ROOM_ID,
'To be @little1 or not to be @big2', 'To be @little1 or not to be @big2',
[EXPECTED_CONTACT_1_ID, EXPECTED_CONTACT_2_ID], [EXPECTED_CONTACT_1_ID, EXPECTED_CONTACT_2_ID],
], 'Tagged Template say should be matched') ], 'Tagged Template say should be matched')
...@@ -124,7 +125,8 @@ test('say()', async () => { ...@@ -124,7 +125,8 @@ test('say()', async () => {
await room.say('Yo', contact1) await room.say('Yo', contact1)
t.deepEqual(callback.getCall(0).args, [ t.deepEqual(callback.getCall(0).args, [
{ contactId: EXPECTED_CONTACT_1_ID, roomId: EXPECTED_ROOM_ID }, // { contactId: EXPECTED_CONTACT_1_ID, roomId: EXPECTED_ROOM_ID },
EXPECTED_ROOM_ID,
'@little1 Yo', '@little1 Yo',
[EXPECTED_CONTACT_1_ID], [EXPECTED_CONTACT_1_ID],
], 'Single mention should work with old ways') ], 'Single mention should work with old ways')
...@@ -135,7 +137,8 @@ test('say()', async () => { ...@@ -135,7 +137,8 @@ test('say()', async () => {
await room.say('hey buddies, let\'s party', contact1, contact2) await room.say('hey buddies, let\'s party', contact1, contact2)
t.deepEqual(callback.getCall(0).args, [ t.deepEqual(callback.getCall(0).args, [
{ contactId: EXPECTED_CONTACT_1_ID, roomId: EXPECTED_ROOM_ID }, // { contactId: EXPECTED_CONTACT_1_ID, roomId: EXPECTED_ROOM_ID },
EXPECTED_ROOM_ID,
'@little1 @big2 hey buddies, let\'s party', '@little1 @big2 hey buddies, let\'s party',
[EXPECTED_CONTACT_1_ID, EXPECTED_CONTACT_2_ID], [EXPECTED_CONTACT_1_ID, EXPECTED_CONTACT_2_ID],
], 'Multiple mention should work with new way') ], 'Multiple mention should work with new way')
......
...@@ -526,12 +526,12 @@ export class Room extends Accessory implements Sayable { ...@@ -526,12 +526,12 @@ export class Room extends Accessory implements Sayable {
} else { } else {
text = something text = something
} }
const receiver = { // const receiver = {
contactId : (mentionList.length && mentionList[0].id) || undefined, // contactId : (mentionList.length && mentionList[0].id) || undefined,
roomId : this.id, // roomId : this.id,
} // }
msgId = await this.puppet.messageSendText( msgId = await this.puppet.messageSendText(
receiver, this.id,
text, text,
mentionList.map(c => c.id), mentionList.map(c => c.id),
) )
...@@ -539,30 +539,34 @@ export class Room extends Accessory implements Sayable { ...@@ -539,30 +539,34 @@ export class Room extends Accessory implements Sayable {
/** /**
* 2. File Message * 2. File Message
*/ */
msgId = await this.puppet.messageSendFile({ msgId = await this.puppet.messageSendFile(
roomId: this.id, this.id,
}, something) something,
)
} else if (something instanceof Contact) { } else if (something instanceof Contact) {
/** /**
* 3. Contact Card * 3. Contact Card
*/ */
msgId = await this.puppet.messageSendContact({ msgId = await this.puppet.messageSendContact(
roomId: this.id, this.id,
}, something.id) something.id,
)
} else if (something instanceof UrlLink) { } else if (something instanceof UrlLink) {
/** /**
* 4. Link Message * 4. Link Message
*/ */
msgId = await this.puppet.messageSendUrl({ msgId = await this.puppet.messageSendUrl(
contactId : this.id, this.id,
}, something.payload) something.payload,
)
} else if (something instanceof MiniProgram) { } else if (something instanceof MiniProgram) {
/** /**
* 5. Mini Program * 5. Mini Program
*/ */
msgId = await this.puppet.messageSendMiniProgram({ msgId = await this.puppet.messageSendMiniProgram(
contactId : this.id, this.id,
}, something.payload) something.payload,
)
} else { } else {
throw new Error('arg unsupported: ' + something) throw new Error('arg unsupported: ' + something)
} }
...@@ -579,16 +583,16 @@ export class Room extends Accessory implements Sayable { ...@@ -579,16 +583,16 @@ export class Room extends Accessory implements Sayable {
...varList: unknown[] ...varList: unknown[]
) { ) {
const mentionList: Contact[] = varList.filter(v => v instanceof Contact) as any const mentionList: Contact[] = varList.filter(v => v instanceof Contact) as any
const receiver = { // const receiver = {
contactId : (mentionList.length && mentionList[0].id) || undefined, // contactId : (mentionList.length && mentionList[0].id) || undefined,
roomId : this.id, // roomId : this.id,
} // }
if (varList.length === 0) { if (varList.length === 0) {
/** /**
* No mention in the string * No mention in the string
*/ */
return this.puppet.messageSendText( return this.puppet.messageSendText(
receiver, this.id,
textList[0], textList[0],
) )
// TODO(huan) 20191222 it seems the following code will not happen, // TODO(huan) 20191222 it seems the following code will not happen,
...@@ -630,7 +634,7 @@ export class Room extends Accessory implements Sayable { ...@@ -630,7 +634,7 @@ export class Room extends Accessory implements Sayable {
finalText += textList[i] finalText += textList[i]
return this.puppet.messageSendText( return this.puppet.messageSendText(
receiver, this.id,
finalText, finalText,
mentionList.map(c => c.id), mentionList.map(c => c.id),
) )
...@@ -956,7 +960,7 @@ export class Room extends Accessory implements Sayable { ...@@ -956,7 +960,7 @@ export class Room extends Accessory implements Sayable {
*/ */
public async qrcode (): Promise<string> { public async qrcode (): Promise<string> {
log.verbose('Room', 'qrcode()') log.verbose('Room', 'qrcode()')
const qrcodeValue = await this.puppet.roomQrcode(this.id) const qrcodeValue = await this.puppet.roomQRCode(this.id)
return guardQrCodeValue(qrcodeValue) return guardQrCodeValue(qrcodeValue)
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册