@juzi/wechaty-puppet-service 1.0.73 → 1.0.75
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/src/client/payload-store.d.ts +1 -1
- package/dist/cjs/src/client/payload-store.d.ts.map +1 -1
- package/dist/cjs/src/client/payload-store.js +7 -2
- package/dist/cjs/src/client/payload-store.js.map +1 -1
- package/dist/cjs/src/client/puppet-service.d.ts +4 -3
- package/dist/cjs/src/client/puppet-service.d.ts.map +1 -1
- package/dist/cjs/src/client/puppet-service.js +52 -31
- package/dist/cjs/src/client/puppet-service.js.map +1 -1
- package/dist/cjs/src/package-json.js +4 -4
- package/dist/cjs/src/server/puppet-implementation.d.ts.map +1 -1
- package/dist/cjs/src/server/puppet-implementation.js +21 -0
- package/dist/cjs/src/server/puppet-implementation.js.map +1 -1
- package/dist/cjs/src/utils/pb-payload-helper.d.ts +4 -0
- package/dist/cjs/src/utils/pb-payload-helper.d.ts.map +1 -1
- package/dist/cjs/src/utils/pb-payload-helper.js +126 -8
- package/dist/cjs/src/utils/pb-payload-helper.js.map +1 -1
- package/dist/esm/src/client/payload-store.d.ts +1 -1
- package/dist/esm/src/client/payload-store.d.ts.map +1 -1
- package/dist/esm/src/client/payload-store.js +7 -2
- package/dist/esm/src/client/payload-store.js.map +1 -1
- package/dist/esm/src/client/puppet-service.d.ts +4 -3
- package/dist/esm/src/client/puppet-service.d.ts.map +1 -1
- package/dist/esm/src/client/puppet-service.js +53 -32
- package/dist/esm/src/client/puppet-service.js.map +1 -1
- package/dist/esm/src/package-json.js +4 -4
- package/dist/esm/src/server/puppet-implementation.d.ts.map +1 -1
- package/dist/esm/src/server/puppet-implementation.js +22 -1
- package/dist/esm/src/server/puppet-implementation.js.map +1 -1
- package/dist/esm/src/utils/pb-payload-helper.d.ts +4 -0
- package/dist/esm/src/utils/pb-payload-helper.d.ts.map +1 -1
- package/dist/esm/src/utils/pb-payload-helper.js +121 -7
- package/dist/esm/src/utils/pb-payload-helper.js.map +1 -1
- package/package.json +4 -4
- package/src/client/payload-store.ts +7 -3
- package/src/client/puppet-service.ts +63 -34
- package/src/package-json.ts +4 -4
- package/src/server/puppet-implementation.ts +28 -1
- package/src/utils/pb-payload-helper.ts +134 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@juzi/wechaty-puppet-service",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.75",
|
|
4
4
|
"description": "Puppet Service for Wechaty",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"@chatie/eslint-config": "^1.0.4",
|
|
52
52
|
"@chatie/semver": "^0.4.7",
|
|
53
53
|
"@chatie/tsconfig": "^4.6.3",
|
|
54
|
-
"@juzi/wechaty-puppet": "^1.0.
|
|
54
|
+
"@juzi/wechaty-puppet": "^1.0.66",
|
|
55
55
|
"@juzi/wechaty-puppet-mock": "^1.0.1",
|
|
56
56
|
"@swc/core": "1.3.39",
|
|
57
57
|
"@types/google-protobuf": "^3.15.5",
|
|
@@ -68,10 +68,10 @@
|
|
|
68
68
|
"why-is-node-running": "^2.2.1"
|
|
69
69
|
},
|
|
70
70
|
"peerDependencies": {
|
|
71
|
-
"@juzi/wechaty-puppet": "^1.0.
|
|
71
|
+
"@juzi/wechaty-puppet": "^1.0.66"
|
|
72
72
|
},
|
|
73
73
|
"dependencies": {
|
|
74
|
-
"@juzi/wechaty-grpc": "^1.0.
|
|
74
|
+
"@juzi/wechaty-grpc": "^1.0.68",
|
|
75
75
|
"clone-class": "^1.1.1",
|
|
76
76
|
"ducks": "^1.0.2",
|
|
77
77
|
"file-box": "^1.5.5",
|
|
@@ -51,7 +51,7 @@ class PayloadStore {
|
|
|
51
51
|
public room? : FlashStore<string, PUPPET.payloads.Room>
|
|
52
52
|
public tag? : FlashStore<string, PUPPET.payloads.Tag>
|
|
53
53
|
public tagGroup? : FlashStore<string, PUPPET.payloads.TagGroup>
|
|
54
|
-
public miscellaneous
|
|
54
|
+
public miscellaneous?: FlashStore<string, string>
|
|
55
55
|
|
|
56
56
|
protected storeDir: string
|
|
57
57
|
protected accountId?: string
|
|
@@ -72,7 +72,11 @@ class PayloadStore {
|
|
|
72
72
|
fs.mkdirSync(this.storeDir, { recursive: true })
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
|
|
75
|
+
try {
|
|
76
|
+
this.miscellaneous = new FlashStore(path.join(this.storeDir, 'miscellaneous')) // account-free data
|
|
77
|
+
} catch (e) {
|
|
78
|
+
log.error('PayloadStore', 'constructor() store failed to init, will continue without it.')
|
|
79
|
+
}
|
|
76
80
|
}
|
|
77
81
|
|
|
78
82
|
/**
|
|
@@ -155,7 +159,7 @@ class PayloadStore {
|
|
|
155
159
|
throw new Error('Can not destroy() a start()-ed store. Call stop() to stop it first')
|
|
156
160
|
}
|
|
157
161
|
|
|
158
|
-
await this.miscellaneous
|
|
162
|
+
await this.miscellaneous?.close()
|
|
159
163
|
|
|
160
164
|
/**
|
|
161
165
|
* Huan(202108): `fs.rm` was introduced from Node.js v14.14
|
|
@@ -50,7 +50,7 @@ import { packageJson } from '../package-json.js'
|
|
|
50
50
|
|
|
51
51
|
import { GrpcManager } from './grpc-manager.js'
|
|
52
52
|
import { PayloadStore } from './payload-store.js'
|
|
53
|
-
import { OptionalBooleanUnwrapper, OptionalBooleanWrapper, callRecordPbToPayload, channelPayloadToPb, channelPbToPayload, postPayloadToPb, urlLinkPbToPayload } from '../utils/pb-payload-helper.js'
|
|
53
|
+
import { OptionalBooleanUnwrapper, OptionalBooleanWrapper, callRecordPbToPayload, channelPayloadToPb, channelPbToPayload, chatHistoryPbToPayload, postPayloadToPb, urlLinkPbToPayload } from '../utils/pb-payload-helper.js'
|
|
54
54
|
import type { MessageBroadcastTargets } from '@juzi/wechaty-puppet/dist/esm/src/schemas/message.js'
|
|
55
55
|
import { timeoutPromise } from 'gerror'
|
|
56
56
|
import { BooleanIndicator } from 'state-switch'
|
|
@@ -143,9 +143,9 @@ class PuppetService extends PUPPET.Puppet {
|
|
|
143
143
|
this._grpcManager = undefined
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
-
log.
|
|
146
|
+
log.info('PuppetService', 'start() instanciating GrpcManager ...')
|
|
147
147
|
const grpcManager = new GrpcManager(this.options)
|
|
148
|
-
log.
|
|
148
|
+
log.info('PuppetService', 'start() instanciating GrpcManager ... done')
|
|
149
149
|
|
|
150
150
|
/**
|
|
151
151
|
* Huan(202108): when we started the event stream,
|
|
@@ -153,35 +153,35 @@ class PuppetService extends PUPPET.Puppet {
|
|
|
153
153
|
*/
|
|
154
154
|
this._grpcManager = grpcManager
|
|
155
155
|
|
|
156
|
-
log.
|
|
156
|
+
log.info('PuppetService', 'start() setting up bridge grpc event stream ...')
|
|
157
157
|
this.bridgeGrpcEventStream(grpcManager)
|
|
158
|
-
log.
|
|
158
|
+
log.info('PuppetService', 'start() setting up bridge grpc event stream ... done')
|
|
159
159
|
|
|
160
|
-
log.
|
|
160
|
+
log.info('PuppetService', 'start() starting grpc manager...')
|
|
161
161
|
const { lastEventSeq } = await this.getEventData()
|
|
162
|
-
const accountId = await this._payloadStore.miscellaneous
|
|
162
|
+
const accountId = await this._payloadStore.miscellaneous?.get('accountId') || ''
|
|
163
163
|
await grpcManager.start(lastEventSeq, accountId)
|
|
164
|
-
log.
|
|
164
|
+
log.info('PuppetService', 'start() starting grpc manager... done')
|
|
165
165
|
|
|
166
|
-
log.
|
|
166
|
+
log.info('PuppetService', 'start healthCheck')
|
|
167
167
|
this.startHealthCheck()
|
|
168
168
|
|
|
169
|
-
log.
|
|
169
|
+
log.info('PuppetService', 'onStart() ... done')
|
|
170
170
|
}
|
|
171
171
|
|
|
172
172
|
override async onStop (): Promise<void> {
|
|
173
|
-
log.
|
|
173
|
+
log.info('PuppetService', 'onStop()')
|
|
174
174
|
|
|
175
175
|
if (this._grpcManager) {
|
|
176
|
-
log.
|
|
176
|
+
log.info('PuppetService', 'onStop() stopping grpc manager ...')
|
|
177
177
|
const grpcManager = this._grpcManager
|
|
178
178
|
this._grpcManager = undefined
|
|
179
179
|
await grpcManager.stop()
|
|
180
|
-
log.
|
|
180
|
+
log.info('PuppetService', 'onStop() stopping grpc manager ... done')
|
|
181
181
|
}
|
|
182
182
|
|
|
183
|
-
log.
|
|
184
|
-
log.
|
|
183
|
+
log.info('PuppetService', 'onStop() ... done')
|
|
184
|
+
log.info('PuppetService', 'stop healthCheck')
|
|
185
185
|
this.stopHealthCheck()
|
|
186
186
|
}
|
|
187
187
|
|
|
@@ -262,10 +262,12 @@ class PuppetService extends PUPPET.Puppet {
|
|
|
262
262
|
}
|
|
263
263
|
|
|
264
264
|
if (seq) {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
265
|
+
if (this._payloadStore.miscellaneous) {
|
|
266
|
+
const lastEventSeq = await this._payloadStore.miscellaneous.get('eventSeq')
|
|
267
|
+
if (!lastEventSeq || (seq > Number(lastEventSeq) || seq === 1)) {
|
|
268
|
+
await this._payloadStore.miscellaneous.set('eventSeq', String(seq))
|
|
269
|
+
await this._payloadStore.miscellaneous.set('eventTimestamp', timestamp)
|
|
270
|
+
}
|
|
269
271
|
}
|
|
270
272
|
}
|
|
271
273
|
|
|
@@ -289,12 +291,14 @@ class PuppetService extends PUPPET.Puppet {
|
|
|
289
291
|
return
|
|
290
292
|
}
|
|
291
293
|
const loginPayload = JSON.parse(payload) as PUPPET.payloads.EventLogin
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
294
|
+
if (this._payloadStore.miscellaneous) {
|
|
295
|
+
const accountId = await this._payloadStore.miscellaneous.get('accountId')
|
|
296
|
+
if (accountId !== loginPayload.contactId) {
|
|
297
|
+
await this._payloadStore.miscellaneous.delete('eventSeq')
|
|
298
|
+
}
|
|
299
|
+
await this._payloadStore.miscellaneous.set('accountId', loginPayload.contactId)
|
|
295
300
|
}
|
|
296
|
-
|
|
297
|
-
;(
|
|
301
|
+
(
|
|
298
302
|
async () => this.login(loginPayload.contactId)
|
|
299
303
|
)().catch(e =>
|
|
300
304
|
log.error('PuppetService', 'onGrpcStreamEvent() this.login() rejection %s',
|
|
@@ -306,7 +310,7 @@ class PuppetService extends PUPPET.Puppet {
|
|
|
306
310
|
case grpcPuppet.EventType.EVENT_TYPE_LOGOUT:
|
|
307
311
|
{
|
|
308
312
|
const logoutPayload = JSON.parse(payload) as PUPPET.payloads.EventLogout
|
|
309
|
-
await this._payloadStore.miscellaneous
|
|
313
|
+
await this._payloadStore.miscellaneous?.delete('accountId')
|
|
310
314
|
;(
|
|
311
315
|
async () => this.logout(logoutPayload.data)
|
|
312
316
|
)().catch(e =>
|
|
@@ -1017,6 +1021,24 @@ class PuppetService extends PUPPET.Puppet {
|
|
|
1017
1021
|
return payload
|
|
1018
1022
|
}
|
|
1019
1023
|
|
|
1024
|
+
override async messageChatHistory (
|
|
1025
|
+
messageId: string,
|
|
1026
|
+
): Promise<PUPPET.payloads.ChatHistory[]> {
|
|
1027
|
+
log.verbose('PuppetService', 'messageChatHistory(%s)', messageId)
|
|
1028
|
+
|
|
1029
|
+
const request = new grpcPuppet.MessageChatHistoryRequest()
|
|
1030
|
+
request.setId(messageId)
|
|
1031
|
+
|
|
1032
|
+
const response = await util.promisify(
|
|
1033
|
+
this.grpcManager.client.messageChatHistory
|
|
1034
|
+
.bind(this.grpcManager.client),
|
|
1035
|
+
)(request)
|
|
1036
|
+
|
|
1037
|
+
const payload = chatHistoryPbToPayload(this.FileBoxUuid, response.getChatHistoryListList()!)
|
|
1038
|
+
|
|
1039
|
+
return payload
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1020
1042
|
override async messageSendMiniProgram (
|
|
1021
1043
|
conversationId : string,
|
|
1022
1044
|
miniProgramPayload : PUPPET.payloads.MiniProgram,
|
|
@@ -1179,22 +1201,29 @@ class PuppetService extends PUPPET.Puppet {
|
|
|
1179
1201
|
|
|
1180
1202
|
override async messageForward (
|
|
1181
1203
|
conversationId: string,
|
|
1182
|
-
|
|
1204
|
+
messageIds: string | string[],
|
|
1183
1205
|
): Promise<string | void> {
|
|
1184
|
-
log.verbose('PuppetService', 'messageForward(%s, %s)', conversationId,
|
|
1206
|
+
log.verbose('PuppetService', 'messageForward(%s, %s)', conversationId, messageIds)
|
|
1185
1207
|
|
|
1186
1208
|
const request = new grpcPuppet.MessageForwardRequest()
|
|
1187
1209
|
request.setConversationId(conversationId)
|
|
1188
|
-
|
|
1210
|
+
if (Array.isArray(messageIds)) {
|
|
1211
|
+
request.setMessageIdsList(messageIds)
|
|
1212
|
+
if (messageIds.length === 1) {
|
|
1213
|
+
request.setMessageId(messageIds[0] as string)
|
|
1214
|
+
}
|
|
1215
|
+
} else {
|
|
1216
|
+
request.setMessageId(messageIds)
|
|
1217
|
+
}
|
|
1189
1218
|
|
|
1190
|
-
log.info('PuppetService', `messageForward(${conversationId}, ${
|
|
1219
|
+
log.info('PuppetService', `messageForward(${conversationId}, ${messageIds}) about to call grpc`)
|
|
1191
1220
|
const response = await util.promisify(
|
|
1192
1221
|
this.grpcManager.client.messageForward
|
|
1193
1222
|
.bind(this.grpcManager.client),
|
|
1194
1223
|
)(request)
|
|
1195
1224
|
|
|
1196
1225
|
const forwardedMessageId = response.getId()
|
|
1197
|
-
log.info('PuppetService', `messageForward(${conversationId}, ${
|
|
1226
|
+
log.info('PuppetService', `messageForward(${conversationId}, ${messageIds}) grpc called, messageId: ${forwardedMessageId}`)
|
|
1198
1227
|
|
|
1199
1228
|
if (forwardedMessageId) {
|
|
1200
1229
|
return forwardedMessageId
|
|
@@ -2649,7 +2678,7 @@ class PuppetService extends PUPPET.Puppet {
|
|
|
2649
2678
|
return contactIdsList
|
|
2650
2679
|
}
|
|
2651
2680
|
|
|
2652
|
-
healthCheckInterval?: NodeJS.
|
|
2681
|
+
healthCheckInterval?: NodeJS.Timeout
|
|
2653
2682
|
startHealthCheck () {
|
|
2654
2683
|
this.healthCheckInterval = setInterval(() => {
|
|
2655
2684
|
this.ding('healthCheck')
|
|
@@ -2686,7 +2715,7 @@ class PuppetService extends PUPPET.Puppet {
|
|
|
2686
2715
|
|
|
2687
2716
|
this.grpcManager.stopStream()
|
|
2688
2717
|
const { lastEventSeq } = await this.getEventData()
|
|
2689
|
-
const accountId = await this._payloadStore.miscellaneous
|
|
2718
|
+
const accountId = await this._payloadStore.miscellaneous?.get('accountId') || ''
|
|
2690
2719
|
|
|
2691
2720
|
const onLoginResolve = (resolve: () => void) => {
|
|
2692
2721
|
const onLogin = (event: grpcPuppet.EventResponse) => {
|
|
@@ -2771,8 +2800,8 @@ class PuppetService extends PUPPET.Puppet {
|
|
|
2771
2800
|
}
|
|
2772
2801
|
|
|
2773
2802
|
async getEventData () {
|
|
2774
|
-
const lastEventTimestamp = await this._payloadStore.miscellaneous
|
|
2775
|
-
let lastEventSeq = await this._payloadStore.miscellaneous
|
|
2803
|
+
const lastEventTimestamp = await this._payloadStore.miscellaneous?.get('eventTimestamp') || 0
|
|
2804
|
+
let lastEventSeq = await this._payloadStore.miscellaneous?.get('eventSeq')
|
|
2776
2805
|
if ((Date.now() - Number(lastEventTimestamp || 0)) > this.timeoutMilliseconds) {
|
|
2777
2806
|
log.warn(`last event was ${(Date.now() - Number(lastEventTimestamp || 0)) / 1000} seconds ago, will not request event cache`)
|
|
2778
2807
|
lastEventSeq = undefined
|
package/src/package-json.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import type { PackageJson } from 'type-fest'
|
|
5
5
|
export const packageJson: PackageJson = {
|
|
6
6
|
"name": "@juzi/wechaty-puppet-service",
|
|
7
|
-
"version": "1.0.
|
|
7
|
+
"version": "1.0.75",
|
|
8
8
|
"description": "Puppet Service for Wechaty",
|
|
9
9
|
"type": "module",
|
|
10
10
|
"exports": {
|
|
@@ -55,7 +55,7 @@ export const packageJson: PackageJson = {
|
|
|
55
55
|
"@chatie/eslint-config": "^1.0.4",
|
|
56
56
|
"@chatie/semver": "^0.4.7",
|
|
57
57
|
"@chatie/tsconfig": "^4.6.3",
|
|
58
|
-
"@juzi/wechaty-puppet": "^1.0.
|
|
58
|
+
"@juzi/wechaty-puppet": "^1.0.66",
|
|
59
59
|
"@juzi/wechaty-puppet-mock": "^1.0.1",
|
|
60
60
|
"@swc/core": "1.3.39",
|
|
61
61
|
"@types/google-protobuf": "^3.15.5",
|
|
@@ -72,10 +72,10 @@ export const packageJson: PackageJson = {
|
|
|
72
72
|
"why-is-node-running": "^2.2.1"
|
|
73
73
|
},
|
|
74
74
|
"peerDependencies": {
|
|
75
|
-
"@juzi/wechaty-puppet": "^1.0.
|
|
75
|
+
"@juzi/wechaty-puppet": "^1.0.66"
|
|
76
76
|
},
|
|
77
77
|
"dependencies": {
|
|
78
|
-
"@juzi/wechaty-grpc": "^1.0.
|
|
78
|
+
"@juzi/wechaty-grpc": "^1.0.68",
|
|
79
79
|
"clone-class": "^1.1.1",
|
|
80
80
|
"ducks": "^1.0.2",
|
|
81
81
|
"file-box": "^1.5.5",
|
|
@@ -43,7 +43,7 @@ import {
|
|
|
43
43
|
import { log } from '../config.js'
|
|
44
44
|
import { grpcError } from './grpc-error.js'
|
|
45
45
|
import { EventStreamManager } from './event-stream-manager.js'
|
|
46
|
-
import { OptionalBooleanUnwrapper, OptionalBooleanWrapper, callRecordPayloadToPb, channelPayloadToPb, postPbToPayload, urlLinkPayloadToPb } from '../utils/pb-payload-helper.js'
|
|
46
|
+
import { OptionalBooleanUnwrapper, OptionalBooleanWrapper, callRecordPayloadToPb, channelPayloadToPb, chatHistoryPayloadToPb, postPbToPayload, urlLinkPayloadToPb } from '../utils/pb-payload-helper.js'
|
|
47
47
|
|
|
48
48
|
function puppetImplementation (
|
|
49
49
|
puppet : PUPPET.impls.PuppetInterface,
|
|
@@ -701,8 +701,14 @@ function puppetImplementation (
|
|
|
701
701
|
try {
|
|
702
702
|
const conversationId = call.request.getConversationId()
|
|
703
703
|
const messageId = call.request.getMessageId()
|
|
704
|
+
const messageIds = call.request.getMessageIdsList()
|
|
704
705
|
|
|
705
706
|
const id = await puppet.messageForward(conversationId, messageId)
|
|
707
|
+
if (messageIds.length > 1) {
|
|
708
|
+
await puppet.messageForward(conversationId, messageIds)
|
|
709
|
+
} else {
|
|
710
|
+
await puppet.messageForward(conversationId, messageId || messageId[0] || '')
|
|
711
|
+
}
|
|
706
712
|
|
|
707
713
|
const response = new grpcPuppet.MessageForwardResponse()
|
|
708
714
|
if (id) {
|
|
@@ -843,6 +849,27 @@ function puppetImplementation (
|
|
|
843
849
|
}
|
|
844
850
|
},
|
|
845
851
|
|
|
852
|
+
messageChatHistory: async (call, callback) => {
|
|
853
|
+
log.verbose('PuppetServiceImpl', 'messageChatHistory()')
|
|
854
|
+
|
|
855
|
+
try {
|
|
856
|
+
const id = call.request.getId()
|
|
857
|
+
|
|
858
|
+
const payloadList = await puppet.messageChatHistory(id)
|
|
859
|
+
|
|
860
|
+
const response = new grpcPuppet.MessageChatHistoryResponse()
|
|
861
|
+
|
|
862
|
+
const pbChatHistoryPayloadList = await chatHistoryPayloadToPb(grpcPuppet, payloadList, serializeFileBox)
|
|
863
|
+
|
|
864
|
+
response.setChatHistoryListList(pbChatHistoryPayloadList)
|
|
865
|
+
|
|
866
|
+
return callback(null, response)
|
|
867
|
+
|
|
868
|
+
} catch (e) {
|
|
869
|
+
return grpcError('messageMiniProgram', e, callback)
|
|
870
|
+
}
|
|
871
|
+
},
|
|
872
|
+
|
|
846
873
|
messagePayload: async (call, callback) => {
|
|
847
874
|
log.verbose('PuppetServiceImpl', 'messagePayload()')
|
|
848
875
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable no-case-declarations */
|
|
1
2
|
import {
|
|
2
3
|
puppet,
|
|
3
4
|
} from '@juzi/wechaty-grpc'
|
|
@@ -39,6 +40,25 @@ export const urlLinkPayloadToPb = (grpcPuppet: grpcPuppet, urlLinkPayload: PUPPE
|
|
|
39
40
|
return pbUrlLinkPayload
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
export const locationPayloadToPb = (grpcPuppet: grpcPuppet, locationPayload: PUPPET.payloads.Location) => {
|
|
44
|
+
const pbLocationPayload = new grpcPuppet.LocationPayload()
|
|
45
|
+
pbLocationPayload.setLatitude(locationPayload.latitude)
|
|
46
|
+
pbLocationPayload.setLongitude(locationPayload.longitude)
|
|
47
|
+
pbLocationPayload.setAccuracy(locationPayload.accuracy)
|
|
48
|
+
pbLocationPayload.setAddress(locationPayload.address)
|
|
49
|
+
pbLocationPayload.setName(locationPayload.name)
|
|
50
|
+
return pbLocationPayload
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const locationPbToPayload = (locationPayloadPb: puppet.LocationPayload) => {
|
|
54
|
+
const _locationPayloadPb = locationPayloadPb.toObject()
|
|
55
|
+
const payload: PUPPET.payloads.Location = {
|
|
56
|
+
..._locationPayloadPb,
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return payload
|
|
60
|
+
}
|
|
61
|
+
|
|
42
62
|
export const urlLinkPbToPayload = (urlLinkPayloadPb: puppet.UrlLinkPayload) => {
|
|
43
63
|
const _urlLinkPayloadPb = urlLinkPayloadPb.toObject()
|
|
44
64
|
const urlLinkPayload: PUPPET.payloads.UrlLink = {
|
|
@@ -116,13 +136,8 @@ export const postPayloadToPb = async (grpcPuppet: grpcPuppet, payload: PUPPET.pa
|
|
|
116
136
|
if (payload.rootId) { pb.setRootId(payload.rootId) }
|
|
117
137
|
if (payload.parentId) { pb.setParentId(payload.parentId) }
|
|
118
138
|
if (payload.location) {
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
location.setAddress(payload.location.address)
|
|
122
|
-
location.setName(payload.location.name)
|
|
123
|
-
location.setLatitude(payload.location.latitude)
|
|
124
|
-
location.setLongitude(payload.location.longitude)
|
|
125
|
-
pb.setLocation(location)
|
|
139
|
+
const pbLocationPayload = locationPayloadToPb(grpcPuppet, payload.location)
|
|
140
|
+
pb.setLocation(pbLocationPayload)
|
|
126
141
|
}
|
|
127
142
|
pb.setVisibleListList(payload.visibleList || [])
|
|
128
143
|
return pb
|
|
@@ -243,3 +258,115 @@ export const callRecordPayloadToPb = (grpcPuppet: grpcPuppet, callRecordPayload:
|
|
|
243
258
|
|
|
244
259
|
return pbCallRecordPayload
|
|
245
260
|
}
|
|
261
|
+
|
|
262
|
+
export const chatHistoryPbToPayload = (FileBoxUuid: typeof FileBox, chatHistoryPb: puppet.ChatHistoryPayload[]) => {
|
|
263
|
+
const chatHistoryPayloadList: PUPPET.payloads.ChatHistory[] = []
|
|
264
|
+
for (const payload of chatHistoryPb) {
|
|
265
|
+
const jsonText = payload.getAvatar()
|
|
266
|
+
|
|
267
|
+
const chatHistoryContent = payload.getMessage()
|
|
268
|
+
|
|
269
|
+
if (!chatHistoryContent) {
|
|
270
|
+
continue
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
let message: any
|
|
274
|
+
switch (chatHistoryContent.getType()) {
|
|
275
|
+
case puppet.ChatHistoryContentType.TEXT:
|
|
276
|
+
message = chatHistoryContent.getText()!
|
|
277
|
+
break
|
|
278
|
+
case puppet.ChatHistoryContentType.FILE:
|
|
279
|
+
message = chatHistoryContent.getFileBox()!
|
|
280
|
+
break
|
|
281
|
+
case puppet.ChatHistoryContentType.LOCATION:
|
|
282
|
+
message = locationPbToPayload(chatHistoryContent.getLocation()!)
|
|
283
|
+
break
|
|
284
|
+
case puppet.ChatHistoryContentType.URL:
|
|
285
|
+
message = urlLinkPbToPayload(chatHistoryContent.getUrlLink()!)
|
|
286
|
+
break
|
|
287
|
+
case puppet.ChatHistoryContentType.MINI_PROGRAM:
|
|
288
|
+
message = miniProgramPbToPayload(chatHistoryContent.getMiniProgram()!)
|
|
289
|
+
break
|
|
290
|
+
case puppet.ChatHistoryContentType.CHANNEL:
|
|
291
|
+
message = channelPbToPayload(chatHistoryContent.getChannel()!)
|
|
292
|
+
break
|
|
293
|
+
case puppet.ChatHistoryContentType.CHAT_HISTORY:
|
|
294
|
+
message = chatHistoryPbToPayload(FileBoxUuid, chatHistoryContent.getChatHistoryList()!)
|
|
295
|
+
break
|
|
296
|
+
default:
|
|
297
|
+
throw new Error(`Invalid chat history content type: ${chatHistoryContent.getType()}`)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const chatHistoryPayload: PUPPET.payloads.ChatHistory = {
|
|
301
|
+
type: payload.getType(),
|
|
302
|
+
avatar: FileBoxUuid.fromJSON(jsonText),
|
|
303
|
+
senderName: payload.getSenderName(),
|
|
304
|
+
corpName: payload.getCorpName(),
|
|
305
|
+
time: payload.getTime(),
|
|
306
|
+
message,
|
|
307
|
+
}
|
|
308
|
+
chatHistoryPayloadList.push(chatHistoryPayload)
|
|
309
|
+
}
|
|
310
|
+
return chatHistoryPayloadList
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export const chatHistoryPayloadToPb = async (grpcPuppet: grpcPuppet, chatHistoryPayloadList: PUPPET.payloads.ChatHistory[], serializeFileBox: (fileBox: FileBoxInterface) => Promise<string>) => {
|
|
314
|
+
const list = []
|
|
315
|
+
for (const chatHistoryPayload of chatHistoryPayloadList) {
|
|
316
|
+
const pbChatHistoryPayload = new grpcPuppet.ChatHistoryPayload()
|
|
317
|
+
|
|
318
|
+
pbChatHistoryPayload.setType(chatHistoryPayload.type)
|
|
319
|
+
const avatarSerializedFileBox = await serializeFileBox(chatHistoryPayload.avatar)
|
|
320
|
+
pbChatHistoryPayload.setAvatar(avatarSerializedFileBox)
|
|
321
|
+
pbChatHistoryPayload.setSenderName(chatHistoryPayload.senderName)
|
|
322
|
+
pbChatHistoryPayload.setCorpName(chatHistoryPayload.corpName)
|
|
323
|
+
pbChatHistoryPayload.setTime(chatHistoryPayload.time)
|
|
324
|
+
|
|
325
|
+
const pbChatHistoryContent = new grpcPuppet.ChatHistoryContent()
|
|
326
|
+
switch (chatHistoryPayload.type) {
|
|
327
|
+
case PUPPET.types.Message.Text:
|
|
328
|
+
case PUPPET.types.Message.Contact:
|
|
329
|
+
pbChatHistoryContent.setType(grpcPuppet.ChatHistoryContentType.TEXT)
|
|
330
|
+
pbChatHistoryContent.setText(chatHistoryPayload.message)
|
|
331
|
+
break
|
|
332
|
+
case PUPPET.types.Message.Image:
|
|
333
|
+
case PUPPET.types.Message.Attachment:
|
|
334
|
+
case PUPPET.types.Message.Video:
|
|
335
|
+
pbChatHistoryContent.setType(grpcPuppet.ChatHistoryContentType.FILE)
|
|
336
|
+
pbChatHistoryContent.setFileBox(await serializeFileBox(chatHistoryPayload.message))
|
|
337
|
+
break
|
|
338
|
+
case PUPPET.types.Message.Location:
|
|
339
|
+
pbChatHistoryContent.setType(grpcPuppet.ChatHistoryContentType.LOCATION)
|
|
340
|
+
const pbLocationPayload = locationPayloadToPb(grpcPuppet, chatHistoryPayload.message)
|
|
341
|
+
pbChatHistoryContent.setLocation(pbLocationPayload)
|
|
342
|
+
break
|
|
343
|
+
case PUPPET.types.Message.Url:
|
|
344
|
+
pbChatHistoryContent.setType(grpcPuppet.ChatHistoryContentType.URL)
|
|
345
|
+
const pbUrlLinkPayload = urlLinkPayloadToPb(grpcPuppet, chatHistoryPayload.message)
|
|
346
|
+
pbChatHistoryContent.setUrlLink(pbUrlLinkPayload)
|
|
347
|
+
break
|
|
348
|
+
case PUPPET.types.Message.MiniProgram:
|
|
349
|
+
pbChatHistoryContent.setType(grpcPuppet.ChatHistoryContentType.MINI_PROGRAM)
|
|
350
|
+
const pbMiniProgramPayload = miniProgramPayloadToPb(grpcPuppet, chatHistoryPayload.message)
|
|
351
|
+
pbChatHistoryContent.setMiniProgram(pbMiniProgramPayload)
|
|
352
|
+
break
|
|
353
|
+
case PUPPET.types.Message.Channel:
|
|
354
|
+
pbChatHistoryContent.setType(grpcPuppet.ChatHistoryContentType.CHANNEL)
|
|
355
|
+
const pbChannelPayload = channelPayloadToPb(grpcPuppet, chatHistoryPayload.message)
|
|
356
|
+
pbChatHistoryContent.setChannel(pbChannelPayload)
|
|
357
|
+
break
|
|
358
|
+
case PUPPET.types.Message.ChatHistory:
|
|
359
|
+
pbChatHistoryContent.setType(grpcPuppet.ChatHistoryContentType.CHAT_HISTORY)
|
|
360
|
+
const pbChatHistoryPayload = await chatHistoryPayloadToPb(grpcPuppet, chatHistoryPayload.message, serializeFileBox)
|
|
361
|
+
pbChatHistoryContent.setChatHistoryList(pbChatHistoryPayload)
|
|
362
|
+
break
|
|
363
|
+
default:
|
|
364
|
+
throw new Error(`ChatHistoryContent unsupported type ${chatHistoryPayload.type}`)
|
|
365
|
+
}
|
|
366
|
+
pbChatHistoryPayload.setMessage(pbChatHistoryContent)
|
|
367
|
+
|
|
368
|
+
list.push(pbChatHistoryPayload)
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return list
|
|
372
|
+
}
|