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

rename FriendRequest to Friendship (#1312)

上级 3b94b99c
......@@ -30,7 +30,7 @@ import {
// Contact,
log,
Wechaty,
FriendRequest,
Friendship,
} from '../src/'
const welcome = `
......@@ -93,7 +93,7 @@ bot
* when request is set, we can get verify message from `request.hello`,
* and accept this request by `request.accept()`
*/
case FriendRequest.Type.Receive:
case Friendship.Type.Receive:
if (request.hello() === 'ding') {
logMsg = 'accepted automatically because verify messsage is "ding"'
console.log('before accept')
......@@ -110,7 +110,7 @@ bot
* 2. Friend Ship Confirmed
*
*/
case FriendRequest.Type.Confirm:
case Friendship.Type.Confirm:
logMsg = 'friend ship confirmed with ' + request.contact().name()
break
}
......
......@@ -23,19 +23,19 @@
* when you are runing with Docker or NPM instead of Git Source.
*/
import {
FriendRequest,
Friendship,
Wechaty,
// Room,
} from '../../src/'
export async function onFriend(
this: Wechaty,
request: FriendRequest,
request: Friendship,
): Promise<void> {
try {
const contact = request.contact()
if (request.type() === FriendRequest.Type.Confirm) {
if (request.type() === Friendship.Type.Confirm) {
console.log('New friend ' + contact.name() + ' relationship confirmed!')
return
}
......
......@@ -38,8 +38,8 @@ import {
} from './misc'
import {
FriendRequestPayload,
FriendRequestType,
FriendshipPayload,
FriendshipType,
} from './puppet/'
/**
......@@ -51,18 +51,18 @@ import {
*
* [Examples/Friend-Bot]{@link https://github.com/Chatie/wechaty/blob/master/examples/friend-bot.ts}
*/
export class FriendRequest extends Accessory {
export class Friendship extends Accessory {
// tslint:disable-next-line:variable-name
public static Type = FriendRequestType
public static Type = FriendshipType
public static load<T extends typeof FriendRequest>(
public static load<T extends typeof Friendship>(
this : T,
id : string,
): T['prototype'] {
const newFriendRequest = new (this as any)(id)
const newFriendship = new (this as any)(id)
// newFriendRequest.payload = this.puppet.cacheFriendRequestPayload.get(id)
return newFriendRequest
return newFriendship
}
/**
......@@ -74,17 +74,17 @@ export class FriendRequest extends Accessory {
contact : Contact,
hello : string,
): Promise<void> {
log.verbose('FriendRequest', 'static send(%s, %s)',
log.verbose('Friendship', 'static send(%s, %s)',
contact.id,
hello,
)
await this.puppet.friendRequestSend(contact.id, hello)
await this.puppet.friendshipVerify(contact.id, hello)
}
// public static createConfirm(
// contactId: string,
// ): FriendRequestPayload {
// log.verbose('FriendRequest', 'createConfirm(%s)',
// log.verbose('Friendship', 'createConfirm(%s)',
// contactId,
// )
......@@ -101,7 +101,7 @@ export class FriendRequest extends Accessory {
// hello : string,
// ticket : string,
// ): FriendRequestPayload {
// log.verbose('FriendRequest', 'createReceive(%s, %s, %s)',
// log.verbose('Friendship', 'createReceive(%s, %s, %s)',
// contactId,
// hello,
// ticket,
......@@ -123,29 +123,29 @@ export class FriendRequest extends Accessory {
*
*/
protected get payload(): undefined | FriendRequestPayload {
protected get payload(): undefined | FriendshipPayload {
if (!this.id) {
return undefined
}
return this.puppet.friendRequestPayloadCache(this.id)
return this.puppet.friendshipPayloadCache(this.id)
}
constructor(
public id: string,
) {
super()
log.verbose('FriendRequest', 'constructor(id=%s)', id)
log.verbose('Friendship', 'constructor(id=%s)', id)
// tslint:disable-next-line:variable-name
const MyClass = instanceToClass(this, FriendRequest)
const MyClass = instanceToClass(this, Friendship)
if (MyClass === FriendRequest) {
throw new Error('FriendRequest class can not be instanciated directly! See: https://github.com/Chatie/wechaty/issues/1217')
if (MyClass === Friendship) {
throw new Error('Friendship class can not be instanciated directly! See: https://github.com/Chatie/wechaty/issues/1217')
}
if (!this.puppet) {
throw new Error('FriendRequest class can not be instanciated without a puppet!')
throw new Error('Friendship class can not be instanciated without a puppet!')
}
}
......@@ -154,8 +154,8 @@ export class FriendRequest extends Accessory {
return this.constructor.name
}
return [
'FriendRequest#',
FriendRequestType[this.payload.type],
'Friendship#',
FriendshipType[this.payload.type],
'<',
this.payload.contactId,
'>',
......@@ -167,14 +167,14 @@ export class FriendRequest extends Accessory {
}
/**
* no `noCache` support because FriendRequest has no rawPayload(yet)
* no `noCache` support because Friendship has no rawPayload(yet)
*/
public async ready(): Promise<void> {
if (this.payload) {
return
}
await this.puppet.friendRequestPayload(this.id)
await this.puppet.friendshipPayload(this.id)
if (!this.payload) {
throw new Error('no payload')
......@@ -182,35 +182,35 @@ export class FriendRequest extends Accessory {
}
public async accept(): Promise<void> {
log.verbose('FriendRequest', 'accept()')
log.verbose('Friendship', 'accept()')
if (!this.payload) {
throw new Error('no payload')
}
if (this.payload.type !== FriendRequest.Type.Receive) {
throw new Error('accept() need type to be FriendRequestType.Receive, but it got a ' + FriendRequest.Type[this.payload.type!])
if (this.payload.type !== Friendship.Type.Receive) {
throw new Error('accept() need type to be FriendshipType.Receive, but it got a ' + Friendship.Type[this.payload.type!])
}
log.silly('FriendRequest', 'accept() to %s', this.payload.contactId)
log.silly('Friendship', 'accept() to %s', this.payload.contactId)
await this.puppet.friendRequestAccept(this.id)
await this.puppet.friendshipAccept(this.id)
const contact = this.contact()
await Misc.retry(async (retry, attempt) => {
log.silly('FriendRequest', 'accept() retry() ready() attempt %d', attempt)
log.silly('Friendship', 'accept() retry() ready() attempt %d', attempt)
await contact.ready()
if (contact.isReady()) {
log.verbose('FriendRequest', 'accept() with contact %s ready()', contact.name())
log.verbose('Friendship', 'accept() with contact %s ready()', contact.name())
return
}
retry(new Error('FriendRequest.accept() content.ready() not ready'))
retry(new Error('Friendship.accept() content.ready() not ready'))
}).catch((e: Error) => {
log.warn('FriendRequest', 'accept() contact %s not ready because of %s', contact, e && e.message || e)
log.warn('Friendship', 'accept() contact %s not ready because of %s', contact, e && e.message || e)
})
}
......@@ -232,16 +232,16 @@ export class FriendRequest extends Accessory {
}
public async reject(): Promise<void> {
log.warn('FriendRequest', 'reject() not necessary, NOP.')
log.warn('Friendship', 'reject() not necessary, NOP.')
return
}
public type(): FriendRequestType {
public type(): FriendshipType {
return this.payload
? this.payload.type
: FriendRequestType.Unknown
: FriendshipType.Unknown
}
}
export default FriendRequest
export default Friendship
......@@ -22,7 +22,7 @@ export {
Contact,
} from './contact'
export {
FriendRequest,
Friendship,
} from './friendship'
export {
Message,
......
......@@ -30,7 +30,7 @@ import {
ContactType,
ContactPayload,
FriendRequestPayload,
FriendshipPayload,
RoomPayload,
RoomMemberPayload,
// RoomQueryFilter,
......@@ -402,27 +402,27 @@ export class PuppetMock extends Puppet {
/**
*
* FriendRequest
* Friendship
*
*/
public async friendRequestRawPayload(id: string) : Promise<any> {
public async friendshipRawPayload(id: string) : Promise<any> {
return {id} as any
}
public async friendRequestRawPayloadParser(rawPayload: any) : Promise<FriendRequestPayload> {
public async friendshipRawPayloadParser(rawPayload: any) : Promise<FriendshipPayload> {
return rawPayload
}
public async friendRequestSend(
public async friendshipVerify(
contactId : string,
hello : string,
): Promise<void> {
log.verbose('PuppetMock', 'friendRequestSend(%s, %s)', contactId, hello)
log.verbose('PuppetMock', 'friendshipVerify(%s, %s)', contactId, hello)
}
public async friendRequestAccept(
friendRequestId : string,
public async friendshipAccept(
friendshipId : string,
): Promise<void> {
log.verbose('PuppetMock', 'friendRequestAccept(%s)', friendRequestId)
log.verbose('PuppetMock', 'friendshipAccept(%s)', friendshipId)
}
public ding(data?: any): Promise<string> {
......
......@@ -437,7 +437,7 @@ export interface PadchatRoomMemberListPayload {
// sourcenickname: '' },
// brandlist: [ [Object] ] } }
export interface PadchatFriendRequestPayload {
export interface PadchatFriendshipPayload {
fromusername : string, // 'lizhuohuan'
encryptusername : string // v1_xxx@stranger'
content : string, // 'hello'
......
......@@ -42,8 +42,8 @@ import {
Receiver,
FriendRequestPayload,
FriendRequestPayloadReceive,
FriendshipPayload,
FriendshipPayloadReceive,
WATCHDOG_TIMEOUT,
} from '../puppet/'
......@@ -52,7 +52,7 @@ import {
contactRawPayloadParser,
fileBoxToQrcode,
// friendRequestEventMessageParser,
friendRequestRawPayloadParser,
friendshipRawPayloadParser,
messageRawPayloadParser,
roomJoinEventMessageParser,
roomLeaveEventMessageParser,
......@@ -102,8 +102,8 @@ export class PuppetPadchat extends Puppet {
protected [WATCHDOG_TIMEOUT] = 4 * 60
// private readonly cachePadchatContactPayload : LRU.Cache<string, PadchatContactRawPayload>
private readonly cachePadchatFriendRequestPayload : LRU.Cache<string, PadchatMessagePayload>
private readonly cachePadchatMessagePayload : LRU.Cache<string, PadchatMessagePayload>
private readonly cachePadchatFriendshipPayload : LRU.Cache<string, PadchatMessagePayload>
private readonly cachePadchatMessagePayload : LRU.Cache<string, PadchatMessagePayload>
// private readonly cachePadchatRoomPayload : LRU.Cache<string, PadchatRoomRawPayload>
public bridge?: PadchatManager
......@@ -123,7 +123,7 @@ export class PuppetPadchat extends Puppet {
}
// this.cachePadchatContactPayload = new LRU<string, PadchatContactRawPayload>(lruOptions)
this.cachePadchatFriendRequestPayload = new LRU<string, PadchatMessagePayload>(lruOptions)
this.cachePadchatFriendshipPayload = new LRU<string, PadchatMessagePayload>(lruOptions)
this.cachePadchatMessagePayload = new LRU<string, PadchatMessagePayload>(lruOptions)
// this.cachePadchatRoomPayload = new LRU<string, PadchatRoomRawPayload>(lruOptions)
}
......@@ -252,7 +252,7 @@ export class PuppetPadchat extends Puppet {
switch (rawPayload.sub_type) {
case PadchatMessageType.VerifyMsg:
this.cachePadchatFriendRequestPayload.set(
this.cachePadchatFriendshipPayload.set(
rawPayload.msg_id,
rawPayload,
)
......@@ -913,14 +913,14 @@ export class PuppetPadchat extends Puppet {
/**
*
* FriendRequest
* Friendship
*
*/
public async friendRequestSend(
public async friendshipVerify(
contactId : string,
hello : string,
): Promise<void> {
log.verbose('PuppetPadchat', 'friendRequestSend(%s, %s)', contactId, hello)
log.verbose('PuppetPadchat', 'friendshipVerify(%s, %s)', contactId, hello)
const rawPayload = await this.contactRawPayload(contactId)
......@@ -951,14 +951,14 @@ export class PuppetPadchat extends Puppet {
)
}
public async friendRequestAccept(
friendRequestId : string,
public async friendshipAccept(
friendshipId : string,
): Promise<void> {
log.verbose('PuppetPadchat', 'friendRequestAccept(%s)', friendRequestId)
log.verbose('PuppetPadchat', 'friendshipAccept(%s)', friendshipId)
const payload = await this.friendRequestPayload(friendRequestId) as FriendRequestPayloadReceive
const payload = await this.friendshipPayload(friendshipId) as FriendshipPayloadReceive
console.log('friendRequestAccept: ', payload)
console.log('friendshipAccept: ', payload)
if (!payload.ticket) {
throw new Error('no ticket')
......@@ -977,19 +977,19 @@ export class PuppetPadchat extends Puppet {
)
}
public async friendRequestRawPayloadParser(rawPayload: PadchatMessagePayload) : Promise<FriendRequestPayload> {
log.verbose('PuppetPadchat', 'friendRequestRawPayloadParser({id=%s})', rawPayload.msg_id)
public async friendshipRawPayloadParser(rawPayload: PadchatMessagePayload) : Promise<FriendshipPayload> {
log.verbose('PuppetPadchat', 'friendshipRawPayloadParser({id=%s})', rawPayload.msg_id)
const payload: FriendRequestPayload = await friendRequestRawPayloadParser(rawPayload)
const payload: FriendshipPayload = await friendshipRawPayloadParser(rawPayload)
return payload
}
public async friendRequestRawPayload(friendRequestId: string): Promise<PadchatMessagePayload> {
log.verbose('PuppetPadchat', 'friendRequestRawPayload(%s)', friendRequestId)
public async friendshipRawPayload(friendshipId: string): Promise<PadchatMessagePayload> {
log.verbose('PuppetPadchat', 'friendshipRawPayload(%s)', friendshipId)
const rawPayload = this.cachePadchatFriendRequestPayload.get(friendRequestId)
const rawPayload = this.cachePadchatFriendshipPayload.get(friendshipId)
if (!rawPayload) {
throw new Error('no rawPayload for id ' + friendRequestId)
throw new Error('no rawPayload for id ' + friendshipId)
}
return rawPayload
......
......@@ -6,17 +6,17 @@
import test from 'blue-tape'
import {
FriendRequestPayload,
FriendRequestType,
FriendshipPayload,
FriendshipType,
} from '../../puppet/'
import {
PadchatMessagePayload,
} from '../padchat-schemas'
import { friendRequestRawPayloadParser } from './friend-request-raw-payload-parser'
import { friendshipRawPayloadParser } from './friend-request-raw-payload-parser'
test('friendRequestRawPayloadParser()', async t => {
test('friendshipRawPayloadParser()', async t => {
const DATA = '%5B%7B%22content%22%3A%22%3Cmsg+fromusername%3D%5C%22lizhuohuan%5C%22+encryptusername%3D%5C%22v1_cf269def9b946093f9d131a5e733ba169351013c95e46a860cddecaf485c4b10%40stranger%5C%22+fromnickname%3D%5C%22%E6%9D%8E%E5%8D%93%E6%A1%93%5C%22+content%3D%5C%22xixixi%5C%22+fullpy%3D%5C%22lizhuohuan%5C%22+shortpy%3D%5C%22LZH%5C%22+imagestatus%3D%5C%223%5C%22+scene%3D%5C%226%5C%22+country%3D%5C%22CN%5C%22+province%3D%5C%22Beijing%5C%22+city%3D%5C%22Haidian%5C%22+sign%3D%5C%22PreAngel%E6%8A%95%E8%B5%84%E4%BA%BA%E3%80%82%E6%B0%B4%E6%9C%A8%E6%B8%85%E5%8D%8EBBS%E7%AB%99%E9%95%BF%E3%80%82%E6%8A%95%E8%B5%84%E4%BA%BA%E4%B8%AD%E6%9C%80%E4%BC%9A%E9%A3%9E%E7%9A%84AI%E7%A8%8B%E5%BA%8F%E5%91%98%E3%80%82%5C%22+percard%3D%5C%221%5C%22+sex%3D%5C%221%5C%22+alias%3D%5C%22%5C%22+weibo%3D%5C%22%5C%22+weibonickname%3D%5C%22%5C%22+albumflag%3D%5C%220%5C%22+albumstyle%3D%5C%220%5C%22+albumbgimgid%3D%5C%22913943270785024_913943270785024%5C%22+snsflag%3D%5C%22177%5C%22+snsbgimgid%3D%5C%22http%3A%2F%2Fshmmsns.qpic.cn%2Fmmsns%2FNoFChqEQomEyhyNjzExH3v78BHSVmIzHBIdOECg9jgcTpRNwThgXJicCsGicI6Kib4xLETc2PuKwhM%2F0%5C%22+snsbgobjectid%3D%5C%2212683064081608282338%5C%22+mhash%3D%5C%22d98b28f4cb1708bb584f3e66078e0a0d%5C%22+mfullhash%3D%5C%22d98b28f4cb1708bb584f3e66078e0a0d%5C%22+bigheadimgurl%3D%5C%22http%3A%2F%2Fwx.qlogo.cn%2Fmmhead%2Fver_1%2FciaaFRTCqfHIKLY0wBjv3h0LSPkCEEcJ0fo6kQkMxQLBiahJWFk7rS9G4VLU5n9OfAnXWlMaIV01oeTITYS0OHlg%2F0%5C%22+smallheadimgurl%3D%5C%22http%3A%2F%2Fwx.qlogo.cn%2Fmmhead%2Fver_1%2FciaaFRTCqfHIKLY0wBjv3h0LSPkCEEcJ0fo6kQkMxQLBiahJWFk7rS9G4VLU5n9OfAnXWlMaIV01oeTITYS0OHlg%2F96%5C%22+ticket%3D%5C%22v2_1a0d2cf325e64b6f74bed09e944529e7cc7a7580cb323475050664566dd0302d89b8e2ed95b596b459cf762d94a0ce606da39babbae0dc26b18a62e079bfc120%40stranger%5C%22+opcode%3D%5C%222%5C%22+googlecontact%3D%5C%22%5C%22+qrticket%3D%5C%22%5C%22+chatroomusername%3D%5C%22%5C%22+sourceusername%3D%5C%22%5C%22+sourcenickname%3D%5C%22%5C%22%3E%3Cbrandlist+count%3D%5C%220%5C%22+ver%3D%5C%22652101432%5C%22%3E%3C%2Fbrandlist%3E%3C%2Fmsg%3E%22%2C%22continue%22%3A1%2C%22description%22%3A%22%22%2C%22from_user%22%3A%22fmessage%22%2C%22msg_id%22%3A%222957327798149218888%22%2C%22msg_source%22%3A%22%22%2C%22msg_type%22%3A5%2C%22status%22%3A1%2C%22sub_type%22%3A37%2C%22timestamp%22%3A1528557626%2C%22to_user%22%3A%22wxid_5zj4i5htp9ih22%22%2C%22uin%22%3A1928023446%7D%5D%0A'
// https://stackoverflow.com/a/24417399/1123955
......@@ -31,13 +31,13 @@ test('friendRequestRawPayloadParser()', async t => {
stranger : 'v1_cf269def9b946093f9d131a5e733ba169351013c95e46a860cddecaf485c4b10@stranger',
ticket : 'v2_1a0d2cf325e64b6f74bed09e944529e7cc7a7580cb323475050664566dd0302d89b8e2ed95b596b459cf762d94a0ce606da39babbae0dc26b18a62e079bfc120@stranger',
type : FriendRequestType.Receive,
type : FriendshipType.Receive,
}
// console.log(PADCHAT_MESSAGE_PAYLOAD)
const friendRequestPayload: FriendRequestPayload = await friendRequestRawPayloadParser(PADCHAT_MESSAGE_PAYLOAD)
// console.log(friendRequestPayload)
const friendshipPayload: FriendshipPayload = await friendshipRawPayloadParser(PADCHAT_MESSAGE_PAYLOAD)
// console.log(friendshipPayload)
t.deepEqual(friendRequestPayload, EXPECTED_FRIEND_REQUEST_PAYLOAD, 'should parse friendRequestPayload right')
t.deepEqual(friendshipPayload, EXPECTED_FRIEND_REQUEST_PAYLOAD, 'should parse friendshipPayload right')
})
......@@ -2,24 +2,24 @@
import { toJson } from 'xml2json'
import {
FriendRequestPayload,
FriendRequestType,
FriendshipPayload,
FriendshipType,
} from '../../puppet/'
import {
PadchatMessagePayload,
PadchatFriendRequestPayload,
PadchatFriendshipPayload,
} from '../padchat-schemas'
export async function friendRequestRawPayloadParser(
export async function friendshipRawPayloadParser(
rawPayload: PadchatMessagePayload,
) : Promise<FriendRequestPayload> {
) : Promise<FriendshipPayload> {
let tryXmlText = rawPayload.content
tryXmlText = tryXmlText.replace(/\+/g, ' ')
interface XmlSchema {
msg?: PadchatFriendRequestPayload,
msg?: PadchatFriendshipPayload,
}
const obj: XmlSchema = JSON.parse(toJson(tryXmlText))
......@@ -27,8 +27,8 @@ export async function friendRequestRawPayloadParser(
if (!obj.msg) {
throw new Error('no msg found')
}
const padchatFriendRequestPayload: PadchatFriendRequestPayload = obj.msg
// const padchatFriendRequestPayload = await new Promise<PadchatFriendRequestPayload>((resolve, reject) => {
const padchatFriendshipPayload: PadchatFriendshipPayload = obj.msg
// const padchatFriendshipPayload = await new Promise<PadchatFriendshipPayload>((resolve, reject) => {
// parseString(tryXmlText, { explicitArray: false }, (err, obj: XmlSchema) => {
// if (err) { // HTML can not be parsed to JSON
// return reject(err)
......@@ -43,18 +43,18 @@ export async function friendRequestRawPayloadParser(
// return resolve(obj.msg.$)
// })
// })
// console.log(padchatFriendRequestPayload)
// console.log(padchatFriendshipPayload)
const friendRequestPayload: FriendRequestPayload = {
const friendshipPayload: FriendshipPayload = {
id : rawPayload.msg_id,
contactId : padchatFriendRequestPayload.fromusername,
hello : padchatFriendRequestPayload.content,
stranger : padchatFriendRequestPayload.encryptusername,
ticket : padchatFriendRequestPayload.ticket,
type : FriendRequestType.Receive,
contactId : padchatFriendshipPayload.fromusername,
hello : padchatFriendshipPayload.content,
stranger : padchatFriendshipPayload.encryptusername,
ticket : padchatFriendshipPayload.ticket,
type : FriendshipType.Receive,
}
return friendRequestPayload
return friendshipPayload
// switch (rawPayload.sub_type) {
// case PadchatMessageType.VerifyMsg:
......@@ -67,20 +67,20 @@ export async function friendRequestRawPayloadParser(
// throw new Error('no recommendInfo')
// }
// const payloadReceive: FriendRequestPayloadReceive = {
// const payloadReceive: FriendshipPayloadReceive = {
// id : rawPayload.MsgId,
// contactId : recommendInfo.UserName,
// hello : recommendInfo.Content,
// ticket : recommendInfo.Ticket,
// type : FriendRequestType.Receive,
// type : FriendshipType.Receive,
// }
// return payloadReceive
// case PadchatMessageType.Sys:
// const payloadConfirm: FriendRequestPayloadConfirm = {
// const payloadConfirm: FriendshipPayloadConfirm = {
// id : rawPayload.MsgId,
// contactId : rawPayload.FromUserName,
// type : FriendRequestType.Confirm,
// type : FriendshipType.Confirm,
// }
// return payloadConfirm
......
......@@ -77,10 +77,10 @@ import {
// ContactQueryFilter,
ContactType,
FriendRequestPayload,
FriendRequestPayloadReceive,
FriendRequestPayloadConfirm,
FriendRequestType,
FriendshipPayload,
FriendshipPayloadReceive,
FriendshipPayloadConfirm,
FriendshipType,
MessagePayload,
MessageType,
......@@ -493,7 +493,7 @@ export class PuppetPuppeteer extends Puppet {
/**
* Treat those Types as TEXT
*
* FriendRequest is a SYS message
* Friendship is a SYS message
* FIXME: should we use better message type at here???
*/
case WebMessageType.SYS:
......@@ -1128,11 +1128,11 @@ export class PuppetPuppeteer extends Puppet {
/**
*
* FriendRequest
* Friendship
*
*/
public async friendRequestRawPayload(id: string): Promise<WebMessageRawPayload> {
log.warn('PuppetPuppeteer', 'friendRequestRawPayload(%s)', id)
public async friendshipRawPayload(id: string): Promise<WebMessageRawPayload> {
log.warn('PuppetPuppeteer', 'friendshipRawPayload(%s)', id)
const rawPayload = await this.bridge.getMessage(id)
if (!rawPayload) {
......@@ -1141,8 +1141,8 @@ export class PuppetPuppeteer extends Puppet {
return rawPayload
}
public async friendRequestRawPayloadParser(rawPayload: WebMessageRawPayload): Promise<FriendRequestPayload> {
log.warn('PuppetPuppeteer', 'friendRequestRawPayloadParser(%s)', rawPayload)
public async friendshipRawPayloadParser(rawPayload: WebMessageRawPayload): Promise<FriendshipPayload> {
log.warn('PuppetPuppeteer', 'friendshipRawPayloadParser(%s)', rawPayload)
switch (rawPayload.MsgType) {
case WebMessageType.VERIFYMSG:
......@@ -1155,20 +1155,20 @@ export class PuppetPuppeteer extends Puppet {
throw new Error('no recommendInfo')
}
const payloadReceive: FriendRequestPayloadReceive = {
const payloadReceive: FriendshipPayloadReceive = {
id : rawPayload.MsgId,
contactId : recommendInfo.UserName,
hello : recommendInfo.Content,
ticket : recommendInfo.Ticket,
type : FriendRequestType.Receive,
type : FriendshipType.Receive,
}
return payloadReceive
case WebMessageType.SYS:
const payloadConfirm: FriendRequestPayloadConfirm = {
const payloadConfirm: FriendshipPayloadConfirm = {
id : rawPayload.MsgId,
contactId : rawPayload.FromUserName,
type : FriendRequestType.Confirm,
type : FriendshipType.Confirm,
}
return payloadConfirm
default:
......@@ -1176,7 +1176,7 @@ export class PuppetPuppeteer extends Puppet {
}
}
public async friendRequestSend(
public async friendshipVerify(
contactId : string,
hello : string,
): Promise<void> {
......@@ -1189,10 +1189,10 @@ export class PuppetPuppeteer extends Puppet {
}
}
public async friendRequestAccept(
friendRequestId : string,
public async friendshipAccept(
friendshipId : string,
): Promise<void> {
const payload = await this.friendRequestPayload(friendRequestId) as FriendRequestPayloadReceive
const payload = await this.friendshipPayload(friendshipId) as FriendshipPayloadReceive
try {
await this.bridge.verifyUserOk(payload.contactId, payload.ticket)
......
......@@ -34,8 +34,8 @@ import {
} from '../wechaty'
import {
FriendRequestPayload,
FriendRequestType,
FriendshipPayload,
FriendshipType,
} from '../puppet/'
import {
......@@ -63,7 +63,7 @@ class PuppetTest extends PuppetPuppeteer {
}
}
test('PuppetPuppeteerFriendRequest.receive smoke testing', async t => {
test('PuppetPuppeteerFriendship.receive smoke testing', async t => {
const puppet = new PuppetTest({ memory: new MemoryCard() })
const wechaty = new WechatyTest({ puppet })
wechaty.initPuppetAccessory(puppet)
......@@ -79,9 +79,9 @@ test('PuppetPuppeteerFriendRequest.receive smoke testing', async t => {
const hello = info.Content
const ticket = info.Ticket
const id = 'id'
const type = FriendRequestType.Receive
const type = FriendshipType.Receive
const payload: FriendRequestPayload = {
const payload: FriendshipPayload = {
id,
type,
contactId: contact.id,
......@@ -90,20 +90,20 @@ test('PuppetPuppeteerFriendRequest.receive smoke testing', async t => {
}
const sandbox = sinon.createSandbox()
sandbox.stub(puppet, 'friendRequestPayload').resolves(payload)
sandbox.stub(puppet, 'friendRequestPayloadCache').returns(payload)
sandbox.stub(puppet, 'friendshipPayload').resolves(payload)
sandbox.stub(puppet, 'friendshipPayloadCache').returns(payload)
const fr = wechaty.FriendRequest.load(id)
const fr = wechaty.Friendship.load(id)
await fr.ready()
t.is(fr.hello(), '我是群聊"Wechaty"的李卓桓.PreAngel', 'should has right request message')
t.true(fr.contact() instanceof Contact, 'should have a Contact instance')
t.is(fr.type(), wechaty.FriendRequest.Type.Receive, 'should be receive type')
t.is(fr.type(), wechaty.Friendship.Type.Receive, 'should be receive type')
sandbox.restore()
})
test('PuppetPuppeteerFriendRequest.confirm smoke testing', async t => {
test('PuppetPuppeteerFriendship.confirm smoke testing', async t => {
const puppet = new PuppetTest({ memory: new MemoryCard() })
const wechaty = new WechatyTest({ puppet })
......@@ -114,9 +114,9 @@ test('PuppetPuppeteerFriendRequest.confirm smoke testing', async t => {
{"MsgId":"3382012679535022763","FromUserName":"@04a0fa314d0d8d50dc54e2ec908744ebf46b87404d143fd9a6692182dd90bd49","ToUserName":"@f7321198e0349f1b38c9f2ef158f70eb","MsgType":10000,"Content":"You have added 李卓桓.PreAngel as your WeChat contact. Start chatting!","Status":4,"ImgStatus":1,"CreateTime":1475569920,"VoiceLength":0,"PlayLength":0,"FileName":"","FileSize":"","MediaId":"","Url":"","AppMsgType":0,"StatusNotifyCode":0,"StatusNotifyUserName":"","RecommendInfo":{"UserName":"","NickName":"","QQNum":0,"Province":"","City":"","Content":"","Signature":"","Alias":"","Scene":0,"VerifyFlag":0,"AttrStatus":0,"Sex":0,"Ticket":"","OpCode":0},"ForwardFlag":0,"AppInfo":{"AppID":"","Type":0},"HasProductId":0,"Ticket":"","ImgHeight":0,"ImgWidth":0,"SubMsgType":0,"NewMsgId":3382012679535022600,"MMPeerUserName":"@04a0fa314d0d8d50dc54e2ec908744ebf46b87404d143fd9a6692182dd90bd49","MMDigest":"You have added 李卓桓.PreAngel as your WeChat contact. Start chatting!","MMIsSend":false,"MMIsChatRoom":false,"LocalID":"3382012679535022763","ClientMsgId":"3382012679535022763","MMActualContent":"You have added 李卓桓.PreAngel as your WeChat contact. Start chatting!","MMActualSender":"@04a0fa314d0d8d50dc54e2ec908744ebf46b87404d143fd9a6692182dd90bd49","MMDigestTime":"16:32","MMDisplayTime":1475569920,"MMTime":"16:32"}
`)
const friendRequestPayload: FriendRequestPayload = {
const friendshipPayload: FriendshipPayload = {
id : 'id',
type : FriendRequestType.Confirm,
type : FriendshipType.Confirm,
contactId : 'xxx',
}
......@@ -127,19 +127,19 @@ test('PuppetPuppeteerFriendRequest.confirm smoke testing', async t => {
sandbox.stub(puppet, 'contactPayload') .resolves({})
sandbox.stub(puppet, 'contactPayloadCache') .returns({})
sandbox.stub(puppet, 'friendRequestPayload') .resolves(friendRequestPayload)
sandbox.stub(puppet, 'friendRequestPayloadCache') .returns(friendRequestPayload)
sandbox.stub(puppet, 'friendshipPayload') .resolves(friendshipPayload)
sandbox.stub(puppet, 'friendshipPayloadCache') .returns(friendshipPayload)
const msg = wechaty.Message.create(rawMessagePayload.MsgId)
await msg.ready()
t.true(/^You have added (.+) as your WeChat contact. Start chatting!$/.test(msg.text()), 'should match confirm message')
const fr = wechaty.FriendRequest.load('xx')
const fr = wechaty.Friendship.load('xx')
await fr.ready()
t.true(fr.contact() instanceof Contact, 'should have a Contact instance')
t.is(fr.type(), wechaty.FriendRequest.Type.Confirm, 'should be confirm type')
t.is(fr.type(), wechaty.Friendship.Type.Confirm, 'should be confirm type')
sandbox.restore()
})
......@@ -35,10 +35,10 @@ import {
ContactType,
ContactPayload,
FriendRequestPayload,
FriendRequestPayloadReceive,
FriendRequestPayloadConfirm,
FriendRequestType,
FriendshipPayload,
FriendshipPayloadReceive,
FriendshipPayloadConfirm,
FriendshipType,
RoomPayload,
RoomMemberPayload,
......@@ -878,29 +878,29 @@ export class PuppetWechat4u extends Puppet {
/**
*
* FriendRequest
* Friendship
*
*/
public async friendRequestSend(
public async friendshipVerify(
contactId : string,
hello : string,
): Promise<void> {
log.verbose('PuppetWechat4u', 'friendRequestSend(%s, %s)', contactId, hello)
log.verbose('PuppetWechat4u', 'friendshipVerify(%s, %s)', contactId, hello)
await this.wechat4u.addFriend(contactId, hello)
}
public async friendRequestAccept(
friendRequestId : string,
public async friendshipAccept(
friendshipId : string,
): Promise<void> {
log.verbose('PuppetWechat4u', 'friendRequestAccept(%s)', friendRequestId)
log.verbose('PuppetWechat4u', 'friendshipAccept(%s)', friendshipId)
const payload = await this.friendRequestPayload(friendRequestId) as FriendRequestPayloadReceive
const payload = await this.friendshipPayload(friendshipId) as FriendshipPayloadReceive
await this.wechat4u.verifyUser(payload.contactId, payload.ticket)
}
public async friendRequestRawPayload(id: string) : Promise<any> {
log.verbose('PuppetWechat4u', 'friendRequestRawPayload(%s)', id)
public async friendshipRawPayload(id: string) : Promise<any> {
log.verbose('PuppetWechat4u', 'friendshipRawPayload(%s)', id)
const rawPayload = this.cacheMessageRawPayload.get(id)
if (!rawPayload) {
......@@ -910,8 +910,8 @@ export class PuppetWechat4u extends Puppet {
return rawPayload
}
public async friendRequestRawPayloadParser(rawPayload: any) : Promise<FriendRequestPayload> {
log.verbose('PuppetWechat4u', 'friendRequestRawPayloadParser(%s)', rawPayload)
public async friendshipRawPayloadParser(rawPayload: any) : Promise<FriendshipPayload> {
log.verbose('PuppetWechat4u', 'friendshipRawPayloadParser(%s)', rawPayload)
switch (rawPayload.MsgType) {
case WebMessageType.VERIFYMSG:
......@@ -924,20 +924,20 @@ export class PuppetWechat4u extends Puppet {
throw new Error('no recommendInfo')
}
const payloadReceive: FriendRequestPayloadReceive = {
const payloadReceive: FriendshipPayloadReceive = {
id : rawPayload.MsgId,
contactId : recommendInfo.UserName,
hello : recommendInfo.Content,
ticket : recommendInfo.Ticket,
type : FriendRequestType.Receive,
type : FriendshipType.Receive,
}
return payloadReceive
case WebMessageType.SYS:
const payloadConfirm: FriendRequestPayloadConfirm = {
const payloadConfirm: FriendshipPayloadConfirm = {
id : rawPayload.MsgId,
contactId : rawPayload.FromUserName,
type : FriendRequestType.Confirm,
type : FriendshipType.Confirm,
}
return payloadConfirm
......
......@@ -18,7 +18,7 @@ import {
// ContactPayloadFilterFactory,
} from '../puppet/schemas/contact'
import {
FriendRequestPayload,
FriendshipPayload,
} from '../puppet/schemas/friendship'
import {
MessagePayload,
......@@ -65,14 +65,14 @@ class PuppetTest extends Puppet {
/**
*
* FriendRequest
* Friendship
*
*/
public async friendRequestRawPayload(id: string) : Promise<any> { return {id} as any }
public async friendRequestRawPayloadParser(rawPayload: any) : Promise<FriendRequestPayload> { return rawPayload }
public async friendshipRawPayload(id: string) : Promise<any> { return {id} as any }
public async friendshipRawPayloadParser(rawPayload: any) : Promise<FriendshipPayload> { return rawPayload }
public async friendRequestSend(contactId: string, hello?: string) : Promise<void> { return {contactId, hello} as any }
public async friendRequestAccept(friendRequestId: string) : Promise<void> { return {friendRequestId} as any }
public async friendshipVerify(contactId: string, hello?: string) : Promise<void> { return {contactId, hello} as any }
public async friendshipAccept(friendshipId: string) : Promise<void> { return {friendshipId} as any }
/**
*
......
......@@ -50,7 +50,7 @@ import {
ContactPayloadFilterFunction,
} from './schemas/contact'
import {
FriendRequestPayload,
FriendshipPayload,
} from './schemas/friendship'
import {
MessagePayload,
......@@ -85,7 +85,7 @@ let PUPPET_COUNTER = 0
export abstract class Puppet extends EventEmitter implements Sayable {
public readonly cacheContactPayload : LRU.Cache<string, ContactPayload>
public readonly cacheFriendRequestPayload : LRU.Cache<string, FriendRequestPayload>
public readonly cacheFriendshipPayload : LRU.Cache<string, FriendshipPayload>
public readonly cacheMessagePayload : LRU.Cache<string, MessagePayload>
public readonly cacheRoomPayload : LRU.Cache<string, RoomPayload>
public readonly cacheRoomMemberPayload : LRU.Cache<string, RoomMemberPayload>
......@@ -140,7 +140,7 @@ export abstract class Puppet extends EventEmitter implements Sayable {
}
this.cacheContactPayload = new LRU<string, ContactPayload>(lruOptions)
this.cacheFriendRequestPayload = new LRU<string, FriendRequestPayload>(lruOptions)
this.cacheFriendshipPayload = new LRU<string, FriendshipPayload>(lruOptions)
this.cacheMessagePayload = new LRU<string, MessagePayload>(lruOptions)
this.cacheRoomPayload = new LRU<string, RoomPayload>(lruOptions)
this.cacheRoomMemberPayload = new LRU<string, RoomMemberPayload>(lruOptions)
......@@ -507,47 +507,47 @@ export abstract class Puppet extends EventEmitter implements Sayable {
/**
*
* FriendRequest
* Friendship
*
*/
public abstract async friendRequestSend(contactId: string, hello?: string) : Promise<void>
public abstract async friendRequestAccept(friendRequestId: string) : Promise<void>
public abstract async friendRequestRawPayload(friendRequestId: string) : Promise<any>
public abstract async friendRequestRawPayloadParser(rawPayload: any) : Promise<FriendRequestPayload>
public friendRequestPayloadCache(friendRequestId: string): undefined | FriendRequestPayload {
// log.silly('Puppet', 'friendRequestPayloadCache(id=%s) @ %s', friendRequestId, this)
if (!friendRequestId) {
public abstract async friendshipVerify(contactId: string, hello?: string) : Promise<void>
public abstract async friendshipAccept(friendshipId: string) : Promise<void>
public abstract async friendshipRawPayload(friendshipId: string) : Promise<any>
public abstract async friendshipRawPayloadParser(rawPayload: any) : Promise<FriendshipPayload>
public friendshipPayloadCache(friendshipId: string): undefined | FriendshipPayload {
// log.silly('Puppet', 'friendshipPayloadCache(id=%s) @ %s', friendshipId, this)
if (!friendshipId) {
throw new Error('no id')
}
const cachedPayload = this.cacheFriendRequestPayload.get(friendRequestId)
const cachedPayload = this.cacheFriendshipPayload.get(friendshipId)
if (cachedPayload) {
// log.silly('Puppet', 'friendRequestPayload(%s) cache HIT', friendRequestId)
// log.silly('Puppet', 'friendshipPayloadCache(%s) cache HIT', friendshipId)
} else {
log.silly('Puppet', 'friendRequestPayload(%s) cache MISS', friendRequestId)
log.silly('Puppet', 'friendshipPayloadCache(%s) cache MISS', friendshipId)
}
return cachedPayload
}
public async friendRequestPayload(
friendRequestId: string,
public async friendshipPayload(
friendshipId: string,
noCache = false,
): Promise<FriendRequestPayload> {
log.verbose('Puppet', 'friendRequestPayload(id=%s, noCache=%s)', friendRequestId, noCache)
): Promise<FriendshipPayload> {
log.verbose('Puppet', 'friendshipPayload(id=%s, noCache=%s)', friendshipId, noCache)
if (!friendRequestId) {
if (!friendshipId) {
throw new Error('no id')
}
if (noCache) {
log.silly('Puppet', 'friendRequestPayload(%s) cache PURGE', friendRequestId)
log.silly('Puppet', 'friendshipPayload(%s) cache PURGE', friendshipId)
this.cacheFriendRequestPayload.del(friendRequestId)
this.cacheFriendshipPayload.del(friendshipId)
} else {
const cachedPayload = this.friendRequestPayloadCache(friendRequestId)
const cachedPayload = this.friendshipPayloadCache(friendshipId)
if (cachedPayload) {
return cachedPayload
......@@ -558,10 +558,10 @@ export abstract class Puppet extends EventEmitter implements Sayable {
/**
* Cache not found
*/
const rawPayload = await this.friendRequestRawPayload(friendRequestId)
const payload = await this.friendRequestRawPayloadParser(rawPayload)
const rawPayload = await this.friendshipRawPayload(friendshipId)
const payload = await this.friendshipRawPayloadParser(rawPayload)
this.cacheFriendRequestPayload.set(friendRequestId, payload)
this.cacheFriendshipPayload.set(friendshipId, payload)
return payload
}
......
export enum FriendRequestType {
export enum FriendshipType {
Unknown = 0,
Confirm,
Receive,
Verify,
}
export interface FriendRequestPayloadBase {
export interface FriendshipPayloadBase {
id : string,
contactId : string,
hello? : string,
}
export type FriendRequestPayloadConfirm = FriendRequestPayloadBase & {
type : FriendRequestType.Confirm,
export type FriendshipPayloadConfirm = FriendshipPayloadBase & {
type : FriendshipType.Confirm,
}
export type FriendRequestPayloadReceive = FriendRequestPayloadBase & {
export type FriendshipPayloadReceive = FriendshipPayloadBase & {
stranger? : string,
ticket : string,
type : FriendRequestType.Receive,
type : FriendshipType.Receive,
}
export type FriendRequestPayloadVerify = FriendRequestPayloadBase & {
type : FriendRequestType.Verify,
export type FriendshipPayloadVerify = FriendshipPayloadBase & {
type : FriendshipType.Verify,
}
export type FriendRequestPayload = FriendRequestPayloadConfirm
| FriendRequestPayloadReceive
| FriendRequestPayloadVerify
export type FriendshipPayload = FriendshipPayloadConfirm
| FriendshipPayloadReceive
| FriendshipPayloadVerify
......@@ -31,7 +31,7 @@ import {
import {
config,
Contact,
FriendRequest,
Friendship,
IoClient,
Message,
Room,
......@@ -48,7 +48,7 @@ import { MemoryCard } from 'memory-card'
test('Export of the Framework', async t => {
t.ok(Contact , 'should export Contact')
t.ok(FriendRequest , 'should export FriendREquest')
t.ok(Friendship , 'should export Friendship')
t.ok(IoClient , 'should export IoClient')
t.ok(Message , 'should export Message')
t.ok(Puppet , 'should export Puppet')
......
......@@ -62,7 +62,7 @@ import {
ContactSelf,
} from './contact'
import {
FriendRequest,
Friendship,
} from './friendship'
import {
Message,
......@@ -140,7 +140,7 @@ export class Wechaty extends Accessory implements Sayable {
// tslint:disable-next-line:variable-name
public readonly ContactSelf : typeof ContactSelf
// tslint:disable-next-line:variable-name
public readonly FriendRequest : typeof FriendRequest
public readonly Friendship : typeof Friendship
// tslint:disable-next-line:variable-name
public readonly Message : typeof Message
// tslint:disable-next-line:variable-name
......@@ -197,7 +197,7 @@ export class Wechaty extends Accessory implements Sayable {
// TODO: make Message & Room constructor private???
this.Contact = cloneClass(Contact)
this.ContactSelf = cloneClass(ContactSelf)
this.FriendRequest = cloneClass(FriendRequest)
this.Friendship = cloneClass(Friendship)
this.Message = cloneClass(Message)
this.Room = cloneClass(Room)
}
......@@ -246,7 +246,7 @@ export class Wechaty extends Accessory implements Sayable {
}
public emit(event: 'error' , error: Error) : boolean
public emit(event: 'friend' , request: FriendRequest) : boolean
public emit(event: 'friend' , request: Friendship) : boolean
public emit(event: 'heartbeat' , data: any) : boolean
public emit(event: 'logout' , user: ContactSelf) : boolean
public emit(event: 'login' , user: ContactSelf) : boolean
......@@ -269,7 +269,7 @@ export class Wechaty extends Accessory implements Sayable {
}
public on(event: 'error' , listener: string | ((this: Wechaty, error: Error) => void)) : this
public on(event: 'friend' , listener: string | ((this: Wechaty, request: FriendRequest) => void)) : this
public on(event: 'friend' , listener: string | ((this: Wechaty, request: Friendship) => void)) : this
public on(event: 'heartbeat' , listener: string | ((this: Wechaty, data: any) => void)) : this
public on(event: 'logout' , listener: string | ((this: Wechaty, user: ContactSelf) => void)) : this
public on(event: 'login' , listener: string | ((this: Wechaty, user: ContactSelf) => void)) : this
......@@ -318,7 +318,7 @@ export class Wechaty extends Accessory implements Sayable {
* <li>408 waits for scan</li>
* </ul>
* @property {Function} heartbeat -(this: Wechaty, data: any) => void
* @property {Function} friend -(this: Wechaty, request?: FriendRequest) => void
* @property {Function} friend -(this: Wechaty, request?: Friendship) => void
* @property {Function} message -(this: Wechaty, message: Message) => void
* @property {Function} room-join -(this: Wechaty, room: Room, inviteeList: Contact[], inviter: Contact) => void
* @property {Function} room-topic -(this: Wechaty, room: Room, newTopic: string, oldTopic: string, changer: Contact) => void
......@@ -354,8 +354,8 @@ export class Wechaty extends Accessory implements Sayable {
* })
*
* @example <caption>Event:friend </caption>
* bot.on('friend', (request: FriendRequest) => {
* if(request.type === FriendRequest.Type.RECEIVE){ // 1. receive new friend request from new contact
* bot.on('friend', (request: Friendship) => {
* if(request.type === Friendship.Type.RECEIVE){ // 1. receive new friend request from new contact
* const contact = request.contact()
* let result = await request.accept()
* if(result){
......@@ -363,7 +363,7 @@ export class Wechaty extends Accessory implements Sayable {
* } else{
* console.log(`Request from ${contact.name()} failed to accept!`)
* }
* } else if (request.type === FriendRequest.Type.CONFIRM) { // 2. confirm friend ship
* } else if (request.type === Friendship.Type.CONFIRM) { // 2. confirm friend ship
* console.log(`new friendship confirmed with ${contact.name()}`)
* }
* })
......@@ -556,7 +556,7 @@ export class Wechaty extends Accessory implements Sayable {
case 'friend':
puppet.removeAllListeners('friend')
puppet.on('friend', async requestId => {
const request = this.FriendRequest.load(requestId)
const request = this.Friendship.load(requestId)
await request.ready()
this.emit('friend', request)
request.contact().emit('friend', request)
......@@ -666,7 +666,7 @@ export class Wechaty extends Accessory implements Sayable {
*/
this.Contact.wechaty = this
this.ContactSelf.wechaty = this
this.FriendRequest.wechaty = this
this.Friendship.wechaty = this
this.Message.wechaty = this
this.Room.wechaty = this
......@@ -675,7 +675,7 @@ export class Wechaty extends Accessory implements Sayable {
*/
this.Contact.puppet = puppet
this.ContactSelf.puppet = puppet
this.FriendRequest.puppet = puppet
this.Friendship.puppet = puppet
this.Message.puppet = puppet
this.Room.puppet = puppet
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册