Skip to content
This repository has been archived by the owner on Jun 1, 2020. It is now read-only.

Commit

Permalink
Merge pull request #107 from lijiarui/dev
Browse files Browse the repository at this point in the history
Add implementation for room-invite event
  • Loading branch information
windmemory committed Jul 24, 2018
2 parents 143d830 + 606b714 commit ce7cfd1
Show file tree
Hide file tree
Showing 11 changed files with 397 additions and 15 deletions.
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechaty-puppet-padchat",
"version": "0.8.6",
"version": "0.9.0",
"description": "Padchat Puppet for Wechaty",
"directories": {
"example": "examples"
Expand Down Expand Up @@ -69,8 +69,8 @@
"tslint": "^5.10.0",
"tslint-config-standard": "^7.1.0",
"typescript": "^2.9.2",
"wechaty": "^0.17.133",
"wechaty-puppet": "^0.8.5"
"wechaty": "^0.19.107",
"wechaty-puppet": "^0.9.19"
},
"git": {
"scripts": {
Expand All @@ -79,20 +79,24 @@
},
"peerDependencies": {
"file-box": "^0.8.22",
"wechaty-puppet": "^0.8.2"
"wechaty-puppet": "^0.9.19"
},
"homepage": "https://github.com/lijiarui/wechaty-puppet-padchat#readme",
"dependencies": {
"array-flatten": "^2.1.1",
"axios": "^0.18.0",
"express": "^4.16.3",
"flash-store": "^0.5.9",
"fs-extra": "^6.0.1",
"jimp": "^0.2.28",
"json-rpc-peer": "^0.15.3",
"jsqr": "^1.1.0",
"lru-cache": "^4.1.3",
"promise-retry": "^1.1.1",
"qr-image": "^3.2.0",
"read-pkg-up": "^4.0.0",
"request": "^2.87.0",
"request-promise": "^4.2.2",
"rx-queue": "^0.4.26",
"rxjs": "^6.2.1",
"ws": "^5.2.1",
Expand Down
3 changes: 2 additions & 1 deletion scripts/npm-pack-testing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ npm install --production \
@types/node \
@types/normalize-package-data \
@types/xml2json \
clone-class \
brolog \
file-box \
fs-extra \
Expand All @@ -27,7 +28,7 @@ npm install --production \
rx-queue \
state-switch \
typescript \
wechaty-puppet \
wechaty-puppet@next \
watchdog \
qr-image \
xml2json \
Expand Down
50 changes: 50 additions & 0 deletions src/padchat-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
PadchatContactPayload,
PadchatContinue,

PadchatRoomInvitationPayload,
PadchatRoomInviteEvent,
PadchatRoomMemberPayload,
PadchatRoomPayload,
} from './padchat-schemas'
Expand Down Expand Up @@ -70,6 +72,7 @@ export class PadchatManager extends PadchatRpc {
[contactId: string]: PadchatRoomMemberPayload,
}>
private cacheRoomRawPayload? : FlashStoreSync<string, PadchatRoomPayload>
private cacheRoomInvitationRawPayload? : FlashStoreSync<string, PadchatRoomInvitationPayload>

private readonly state : StateSwitch
private readonly delayQueueExecutor : DelayQueueExector
Expand Down Expand Up @@ -106,6 +109,7 @@ export class PadchatManager extends PadchatRpc {
if ( this.cacheContactRawPayload
|| this.cacheRoomMemberRawPayload
|| this.cacheRoomRawPayload
|| this.cacheRoomInvitationRawPayload
) {
throw new Error('cache exists')
}
Expand All @@ -130,11 +134,13 @@ export class PadchatManager extends PadchatRpc {
this.cacheContactRawPayload = new FlashStoreSync(path.join(baseDir, 'contact-raw-payload'))
this.cacheRoomMemberRawPayload = new FlashStoreSync(path.join(baseDir, 'room-member-raw-payload'))
this.cacheRoomRawPayload = new FlashStoreSync(path.join(baseDir, 'room-raw-payload'))
this.cacheRoomInvitationRawPayload = new FlashStoreSync(path.join(baseDir, 'room-invitation-raw-payload'))

await Promise.all([
this.cacheContactRawPayload.ready(),
this.cacheRoomMemberRawPayload.ready(),
this.cacheRoomRawPayload.ready(),
this.cacheRoomInvitationRawPayload.ready(),
])

const roomMemberTotalNum = [...this.cacheRoomMemberRawPayload.values()].reduce(
Expand All @@ -158,18 +164,21 @@ export class PadchatManager extends PadchatRpc {
if ( this.cacheContactRawPayload
&& this.cacheRoomMemberRawPayload
&& this.cacheRoomRawPayload
&& this.cacheRoomInvitationRawPayload
) {
log.silly('PuppetPadchatManager', 'releaseCache() closing caches ...')

await Promise.all([
this.cacheContactRawPayload.close(),
this.cacheRoomMemberRawPayload.close(),
this.cacheRoomRawPayload.close(),
this.cacheRoomInvitationRawPayload.close(),
])

this.cacheContactRawPayload = undefined
this.cacheRoomMemberRawPayload = undefined
this.cacheRoomRawPayload = undefined
this.cacheRoomInvitationRawPayload = undefined

log.silly('PuppetPadchatManager', 'releaseCache() cache closed.')
} else {
Expand Down Expand Up @@ -987,6 +996,47 @@ export class PadchatManager extends PadchatRpc {
return rawPayload
}

public async roomInvitationRawPayload (roomInvitationId: string): Promise<PadchatRoomInvitationPayload> {
log.verbose('PuppetPadchatManager', 'roomInvitationRawPayload(%s)', roomInvitationId)
if (!this.cacheRoomInvitationRawPayload) {
throw new Error('no cache')
}

const payload = await this.cacheRoomInvitationRawPayload.get(roomInvitationId)

if (payload) {
return payload
} else {
throw new Error(`can not get invitation with invitation id: ${roomInvitationId}`)
}
}

public async roomInvitationRawPayloadDirty (roomInvitationId: string): Promise<void> {
log.verbose('PuppetPadchatManager', 'roomInvitationRawPayloadDirty(%s)', roomInvitationId)
if (!this.cacheRoomInvitationRawPayload) {
throw new Error('no cache')
}

await this.cacheRoomInvitationRawPayload.delete(roomInvitationId)
}

public async saveRoomInvitationRawPayload (roomInvitation: PadchatRoomInviteEvent): Promise<void> {
log.verbose('PuppetPadchatManager', 'saveRoomInvitationRawPayload(%s)', JSON.stringify(roomInvitation))
const { msgId, roomName, url, fromUser, timestamp } = roomInvitation

if (!this.cacheRoomInvitationRawPayload) {
throw new Error('no cache')
}

this.cacheRoomInvitationRawPayload.set(msgId, {
fromUser,
id: msgId,
roomName,
timestamp,
url,
})
}

public ding (data?: string): void {
log.verbose('PuppetPadchatManager', 'ding(%s)', data || '')
// TODO: healthy check
Expand Down
3 changes: 2 additions & 1 deletion src/padchat-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
PadchatPayload,
PadchatPayloadType,

PadchatRequestTokenPayload,
PadchatRoomMemberListPayload,
PadchatRoomMemberPayload,
PadchatRoomPayload,
Expand Down Expand Up @@ -1096,7 +1097,7 @@ export class PadchatRpc extends EventEmitter {
// 公众号中获取url访问token, 给下面的函数使用[WXRequestUrl]
// user 公众号用户名
// url 访问的链接
public async WXGetRequestToken (id: string, url: string): Promise<any> {
public async WXGetRequestToken (id: string, url: string): Promise<PadchatRequestTokenPayload> {
const result = await this.rpcCall('WXGetRequestToken', id, url)
log.silly('PadchatRpc', 'WXGetRequestToken , stranger,result: %s', JSON.stringify(result))
if (!result || result.status !== 0) {
Expand Down
52 changes: 51 additions & 1 deletion src/padchat-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
import {
ContactGender,
FriendshipType,
} from 'wechaty-puppet'

// 1 when use WXSyncContact, 0 when use WXGetContact
Expand Down Expand Up @@ -438,9 +439,58 @@ export interface PadchatRoomMemberListPayload {
// sourcenickname: '' },
// brandlist: [ [Object] ] } }

export interface PadchatRoomInvitationPayload {
id: string,
fromUser: string,
roomName: string,
timestamp: number,
url: string,
}

export interface PadchatFriendshipPayload {
fromusername : string, // 'lizhuohuan'
encryptusername : string // v1_xxx@stranger'
encryptusername : string, // v1_xxx@stranger'
content : string, // 'hello'
ticket : string, // 'v2_1a0d2cf325e64b6f74bed09e944529e7cc7a7580cb323475050664566dd0302d89b8e2ed95b596b459cf762d94a0ce606da39babbae0dc26b18a62e079bfc120@stranger',
}

export interface PadchatRequestTokenPayload {
full_url : string,
info : string,
message : string,
share_url : string,
status : number,
}

export interface PadchatRoomInviteEvent {
fromUser: string,
msgId: string,
roomName: string,
timestamp: number,
url: string,
}

export interface FriendshipPayloadBase {
id : string,

contactId : string,
hello? : string,
}

export type FriendshipPayloadConfirm = FriendshipPayloadBase & {
type : FriendshipType.Confirm,
}

export type FriendshipPayloadReceive = FriendshipPayloadBase & {
stranger? : string,
ticket : string,
type : FriendshipType.Receive,
}

export type FriendshipPayloadVerify = FriendshipPayloadBase & {
type : FriendshipType.Verify,
}

export type FriendshipPayload = FriendshipPayloadConfirm
| FriendshipPayloadReceive
| FriendshipPayloadVerify

0 comments on commit ce7cfd1

Please sign in to comment.