wechaty-puppet-padplus2 3.1.2

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.
Files changed (182) hide show
  1. package/README.md +50 -0
  2. package/dist/cjs/cleanup.cjs +1 -0
  3. package/dist/cjs/config.cjs +1 -0
  4. package/dist/cjs/mock/cache-manager.cjs +1 -0
  5. package/dist/cjs/mock/events/event-friendship.cjs +1 -0
  6. package/dist/cjs/mock/events/event-message.cjs +1 -0
  7. package/dist/cjs/mock/events/event-room-invite.cjs +1 -0
  8. package/dist/cjs/mock/events/event-room-join.cjs +1 -0
  9. package/dist/cjs/mock/events/event-room-leave.cjs +1 -0
  10. package/dist/cjs/mock/events/event-room-topic.cjs +1 -0
  11. package/dist/cjs/mock/events/event.cjs +1 -0
  12. package/dist/cjs/mock/events/mod.cjs +1 -0
  13. package/dist/cjs/mock/messages/message-appmsg.cjs +1 -0
  14. package/dist/cjs/mock/messages/message-emotion.cjs +1 -0
  15. package/dist/cjs/mock/messages/message-miniprogram.cjs +1 -0
  16. package/dist/cjs/mock/messages/message-sysmsg.cjs +1 -0
  17. package/dist/cjs/mock/messages/sysmsg/message-pat.cjs +1 -0
  18. package/dist/cjs/mock/messages/sysmsg/message-revokemsg.cjs +1 -0
  19. package/dist/cjs/mock/messages/sysmsg/message-sysmsgtemplate.cjs +1 -0
  20. package/dist/cjs/mock/messages/sysmsg/message-todo.cjs +1 -0
  21. package/dist/cjs/mock/schema-mapper/contact.cjs +1 -0
  22. package/dist/cjs/mock/schema-mapper/message/message-parser-appmsg.cjs +1 -0
  23. package/dist/cjs/mock/schema-mapper/message/message-parser-refermsg.cjs +1 -0
  24. package/dist/cjs/mock/schema-mapper/message/message-parser-room.cjs +1 -0
  25. package/dist/cjs/mock/schema-mapper/message/message-parser-single-chat.cjs +1 -0
  26. package/dist/cjs/mock/schema-mapper/message/message-parser-sysmsg.cjs +1 -0
  27. package/dist/cjs/mock/schema-mapper/message/message-parser-type.cjs +1 -0
  28. package/dist/cjs/mock/schema-mapper/message/message-parser.cjs +1 -0
  29. package/dist/cjs/mock/schema-mapper/message/mod.cjs +1 -0
  30. package/dist/cjs/mock/schema-mapper/message.cjs +1 -0
  31. package/dist/cjs/mock/schema-mapper/room.cjs +1 -0
  32. package/dist/cjs/mock/types.cjs +1 -0
  33. package/dist/cjs/mock/utils/cached-promise.cjs +1 -0
  34. package/dist/cjs/mock/utils/is-type.cjs +1 -0
  35. package/dist/cjs/mock/utils/regex.cjs +1 -0
  36. package/dist/cjs/mock/utils/runner.cjs +1 -0
  37. package/dist/cjs/mock/utils/xml-to-json.cjs +1 -0
  38. package/dist/cjs/mod.cjs +1 -0
  39. package/dist/cjs/package-json.cjs +1 -0
  40. package/dist/cjs/puppet-plus.cjs +1 -0
  41. package/dist/cjs/server/cache-manager.cjs +1 -0
  42. package/dist/cjs/server/index.cjs +1 -0
  43. package/dist/cjs/server/map/AppMessageLink.cjs +1 -0
  44. package/dist/cjs/server/map/Label.cjs +1 -0
  45. package/dist/cjs/server/map/RoomMemberList.cjs +1 -0
  46. package/dist/cjs/server/map/contact.cjs +1 -0
  47. package/dist/cjs/server/map/index.cjs +1 -0
  48. package/dist/cjs/server/map/message.cjs +1 -0
  49. package/dist/cjs/server/map/response.cjs +1 -0
  50. package/dist/cjs/server/service/api/index3.cjs +1 -0
  51. package/dist/cjs/server/service/config/index.cjs +1 -0
  52. package/dist/cjs/server/service/index.cjs +1 -0
  53. package/dist/cjs/server/service/request/index.cjs +1 -0
  54. package/dist/cjs/server/service/utils/polling.cjs +1 -0
  55. package/dist/cjs/server/utils/PromiseUtils.cjs +1 -0
  56. package/dist/cjs/server/utils/SerialExecutor.cjs +1 -0
  57. package/dist/cjs/server/utils/Utils.cjs +1 -0
  58. package/dist/cjs/server/utils/xml-to-json.cjs +1 -0
  59. package/dist/esm/cleanup.js +26 -0
  60. package/dist/esm/config.js +9 -0
  61. package/dist/esm/mock/cache-manager.js +182 -0
  62. package/dist/esm/mock/events/event-friendship.js +59 -0
  63. package/dist/esm/mock/events/event-message.js +3 -0
  64. package/dist/esm/mock/events/event-room-invite.js +33 -0
  65. package/dist/esm/mock/events/event-room-join.js +105 -0
  66. package/dist/esm/mock/events/event-room-leave.js +83 -0
  67. package/dist/esm/mock/events/event-room-topic.js +50 -0
  68. package/dist/esm/mock/events/event.js +38 -0
  69. package/dist/esm/mock/events/mod.js +20 -0
  70. package/dist/esm/mock/messages/message-appmsg.js +41 -0
  71. package/dist/esm/mock/messages/message-emotion.js +24 -0
  72. package/dist/esm/mock/messages/message-miniprogram.js +18 -0
  73. package/dist/esm/mock/messages/message-sysmsg.js +50 -0
  74. package/dist/esm/mock/messages/sysmsg/message-pat.js +10 -0
  75. package/dist/esm/mock/messages/sysmsg/message-revokemsg.js +20 -0
  76. package/dist/esm/mock/messages/sysmsg/message-sysmsgtemplate.js +45 -0
  77. package/dist/esm/mock/messages/sysmsg/message-todo.js +15 -0
  78. package/dist/esm/mock/schema-mapper/contact.js +22 -0
  79. package/dist/esm/mock/schema-mapper/message/message-parser-appmsg.js +64 -0
  80. package/dist/esm/mock/schema-mapper/message/message-parser-refermsg.js +46 -0
  81. package/dist/esm/mock/schema-mapper/message/message-parser-room.js +33 -0
  82. package/dist/esm/mock/schema-mapper/message/message-parser-single-chat.js +4 -0
  83. package/dist/esm/mock/schema-mapper/message/message-parser-sysmsg.js +33 -0
  84. package/dist/esm/mock/schema-mapper/message/message-parser-type.js +31 -0
  85. package/dist/esm/mock/schema-mapper/message/message-parser.js +20 -0
  86. package/dist/esm/mock/schema-mapper/message/mod.js +19 -0
  87. package/dist/esm/mock/schema-mapper/message.js +21 -0
  88. package/dist/esm/mock/schema-mapper/room.js +30 -0
  89. package/dist/esm/mock/types.js +13 -0
  90. package/dist/esm/mock/utils/cached-promise.js +12 -0
  91. package/dist/esm/mock/utils/is-type.js +21 -0
  92. package/dist/esm/mock/utils/regex.js +11 -0
  93. package/dist/esm/mock/utils/runner.js +11 -0
  94. package/dist/esm/mock/utils/xml-to-json.js +14 -0
  95. package/dist/esm/mod.js +5 -0
  96. package/dist/esm/package-json.js +5 -0
  97. package/dist/esm/puppet-plus.js +829 -0
  98. package/dist/esm/server/cache-manager.js +77 -0
  99. package/dist/esm/server/index.js +19 -0
  100. package/dist/esm/server/map/AppMessageLink.js +58 -0
  101. package/dist/esm/server/map/Label.js +30 -0
  102. package/dist/esm/server/map/RoomMemberList.js +48 -0
  103. package/dist/esm/server/map/contact.js +192 -0
  104. package/dist/esm/server/map/index.js +7 -0
  105. package/dist/esm/server/map/message.js +107 -0
  106. package/dist/esm/server/map/response.js +78 -0
  107. package/dist/esm/server/service/api/index3.js +960 -0
  108. package/dist/esm/server/service/config/index.js +7 -0
  109. package/dist/esm/server/service/index.js +30 -0
  110. package/dist/esm/server/service/request/index.js +47 -0
  111. package/dist/esm/server/service/utils/polling.js +24 -0
  112. package/dist/esm/server/utils/PromiseUtils.js +19 -0
  113. package/dist/esm/server/utils/SerialExecutor.js +41 -0
  114. package/dist/esm/server/utils/Utils.js +7 -0
  115. package/dist/esm/server/utils/xml-to-json.js +18 -0
  116. package/dist/types/cleanup.d.ts +4 -0
  117. package/dist/types/config.d.ts +9 -0
  118. package/dist/types/mock/cache-manager.d.ts +55 -0
  119. package/dist/types/mock/events/event-friendship.d.ts +5 -0
  120. package/dist/types/mock/events/event-message.d.ts +5 -0
  121. package/dist/types/mock/events/event-room-invite.d.ts +5 -0
  122. package/dist/types/mock/events/event-room-join.d.ts +5 -0
  123. package/dist/types/mock/events/event-room-leave.d.ts +7 -0
  124. package/dist/types/mock/events/event-room-topic.d.ts +5 -0
  125. package/dist/types/mock/events/event.d.ts +28 -0
  126. package/dist/types/mock/events/mod.d.ts +4 -0
  127. package/dist/types/mock/messages/message-appmsg.d.ts +57 -0
  128. package/dist/types/mock/messages/message-emotion.d.ts +12 -0
  129. package/dist/types/mock/messages/message-miniprogram.d.ts +3 -0
  130. package/dist/types/mock/messages/message-sysmsg.d.ts +18 -0
  131. package/dist/types/mock/messages/sysmsg/message-pat.d.ts +14 -0
  132. package/dist/types/mock/messages/sysmsg/message-revokemsg.d.ts +19 -0
  133. package/dist/types/mock/messages/sysmsg/message-sysmsgtemplate.d.ts +60 -0
  134. package/dist/types/mock/messages/sysmsg/message-todo.d.ts +31 -0
  135. package/dist/types/mock/schema-mapper/contact.d.ts +4 -0
  136. package/dist/types/mock/schema-mapper/message/message-parser-appmsg.d.ts +4 -0
  137. package/dist/types/mock/schema-mapper/message/message-parser-refermsg.d.ts +4 -0
  138. package/dist/types/mock/schema-mapper/message/message-parser-room.d.ts +4 -0
  139. package/dist/types/mock/schema-mapper/message/message-parser-single-chat.d.ts +4 -0
  140. package/dist/types/mock/schema-mapper/message/message-parser-sysmsg.d.ts +4 -0
  141. package/dist/types/mock/schema-mapper/message/message-parser-type.d.ts +4 -0
  142. package/dist/types/mock/schema-mapper/message/message-parser.d.ts +12 -0
  143. package/dist/types/mock/schema-mapper/message/mod.d.ts +4 -0
  144. package/dist/types/mock/schema-mapper/message.d.ts +3 -0
  145. package/dist/types/mock/schema-mapper/room.d.ts +5 -0
  146. package/dist/types/mock/types.d.ts +35 -0
  147. package/dist/types/mock/utils/cached-promise.d.ts +5 -0
  148. package/dist/types/mock/utils/is-type.d.ts +9 -0
  149. package/dist/types/mock/utils/regex.d.ts +3 -0
  150. package/dist/types/mock/utils/runner.d.ts +3 -0
  151. package/dist/types/mock/utils/xml-to-json.d.ts +2 -0
  152. package/dist/types/mod.d.ts +6 -0
  153. package/dist/types/package-json.d.ts +4 -0
  154. package/dist/types/puppet-plus.d.ts +102 -0
  155. package/dist/types/server/cache-manager.d.ts +22 -0
  156. package/dist/types/server/index.d.ts +15 -0
  157. package/dist/types/server/map/AppMessageLink.d.ts +31 -0
  158. package/dist/types/server/map/Label.d.ts +17 -0
  159. package/dist/types/server/map/RoomMemberList.d.ts +31 -0
  160. package/dist/types/server/map/contact.d.ts +113 -0
  161. package/dist/types/server/map/index.d.ts +13 -0
  162. package/dist/types/server/map/message.d.ts +68 -0
  163. package/dist/types/server/map/response.d.ts +45 -0
  164. package/dist/types/server/server.d.ts +6 -0
  165. package/dist/types/server/service/api/index.d.ts +78 -0
  166. package/dist/types/server/service/api/index2.d.ts +72 -0
  167. package/dist/types/server/service/api/index3.d.ts +78 -0
  168. package/dist/types/server/service/api/index_c.d.ts +71 -0
  169. package/dist/types/server/service/api/ws.d.ts +2 -0
  170. package/dist/types/server/service/config/index.d.ts +6 -0
  171. package/dist/types/server/service/index.d.ts +8 -0
  172. package/dist/types/server/service/request/index.d.ts +14 -0
  173. package/dist/types/server/service/request/type.d.ts +11 -0
  174. package/dist/types/server/service/utils/polling.d.ts +8 -0
  175. package/dist/types/server/utils/PromiseUtils.d.ts +13 -0
  176. package/dist/types/server/utils/SerialExecutor.d.ts +10 -0
  177. package/dist/types/server/utils/Utils.d.ts +3 -0
  178. package/dist/types/server/utils/diff.d.ts +35 -0
  179. package/dist/types/server/utils/polling.d.ts +7 -0
  180. package/dist/types/server/utils/token.d.ts +11 -0
  181. package/dist/types/server/utils/xml-to-json.d.ts +3 -0
  182. package/package.json +123 -0
@@ -0,0 +1,829 @@
1
+ import * as t from "fast-xml-parser";
2
+
3
+ import { genIdempotentId as e } from "./server/utils/Utils.js";
4
+
5
+ import { LocalClient as a } from "./server/index.js";
6
+
7
+ import { SerialExecutor as s } from "./server/utils/SerialExecutor.js";
8
+
9
+ import { ImageType as i } from "./server/map/index.js";
10
+
11
+ import { parseAppmsgMessagePayload as n } from "./mock/messages/message-appmsg.js";
12
+
13
+ import { parseMiniProgramMessagePayload as r } from "./mock/messages/message-miniprogram.js";
14
+
15
+ import { WechatMessageType as o } from "./mock/types.js";
16
+
17
+ import { parseEmotionMessagePayload as c, generateEmotionPayload as m } from "./mock/messages/message-emotion.js";
18
+
19
+ import * as g from "wechaty-puppet";
20
+
21
+ import { log as h } from "wechaty-puppet";
22
+
23
+ import { FileBox as l } from "file-box";
24
+
25
+ import "./mock/events/mod.js";
26
+
27
+ import { packageJson as p } from "./package-json.js";
28
+
29
+ import { CacheManager as u } from "./mock/cache-manager.js";
30
+
31
+ import { removeRunningPuppet as d, addRunningPuppet as w } from "./cleanup.js";
32
+
33
+ import { CachedPromiseFunc as f } from "./mock/utils/cached-promise.js";
34
+
35
+ import { padPlusToWechaty as y } from "./mock/schema-mapper/message.js";
36
+
37
+ import { isRoomId as _, isIMContactId as M } from "./mock/utils/is-type.js";
38
+
39
+ import { isRoomLeaveDebouncing as C } from "./mock/events/event-room-leave.js";
40
+
41
+ import { padPlusContactToWechaty as b } from "./mock/schema-mapper/contact.js";
42
+
43
+ import { padPlusRoomToWechaty as P, padPlusRoomMemberToWechaty as S, chatRoomMemberToContact as R } from "./mock/schema-mapper/room.js";
44
+
45
+ import v from "fs";
46
+
47
+ import k from "fluent-ffmpeg";
48
+
49
+ import I from "os";
50
+
51
+ import $ from "path";
52
+
53
+ import T from "ffmpeg-static";
54
+
55
+ import { parseEvent as j, EventType as L } from "./mock/events/event.js";
56
+
57
+ import { Message as U } from "./server/map/message.js";
58
+
59
+ import { Contact as x } from "./server/map/contact.js";
60
+
61
+ import { AppMessageLink as A } from "./server/map/AppMessageLink.js";
62
+
63
+ import { Label as N } from "./server/map/Label.js";
64
+
65
+ const B = "$search$-", E = "[PuppetPadPlus]", VERSION = p.version;
66
+
67
+ v.existsSync("/usr/bin/ffmpeg") ? (console.log("/usr/bin/ffmpeg 文件存在,使用系统默认的"),
68
+ k.setFfmpegPath("/usr/bin/ffmpeg")) : k.setFfmpegPath(T);
69
+
70
+ class PuppetPlus extends g.Puppet {
71
+ options;
72
+ static VERSION=VERSION;
73
+ _printVersion=!0;
74
+ _onPushSerialExecutor=new s;
75
+ selfInfo;
76
+ _cacheMgr;
77
+ _client;
78
+ get client() {
79
+ return this._client;
80
+ }
81
+ constructor(t = {}) {
82
+ super(t), this.options = t;
83
+ }
84
+ version() {
85
+ return VERSION;
86
+ }
87
+ async onStop() {
88
+ await this._stopClient(!1);
89
+ }
90
+ async _stopClient(t) {
91
+ this._client && this._client.removeAllListeners(), this._client = void 0, this.__currentUserId = void 0,
92
+ this._cacheMgr && (await this._cacheMgr.close(), this._cacheMgr = void 0), d(this),
93
+ t && setTimeout((() => {
94
+ this.wrapAsync(this._startClient());
95
+ }));
96
+ }
97
+ async onStart() {
98
+ await this._startClient();
99
+ }
100
+ async requestLoginQRCode() {
101
+ if (!this._client) {
102
+ throw new Error("puppet has not started");
103
+ }
104
+ if (!this._client.api?.requestLoginQRCode) {
105
+ throw new Error("requestLoginQRCode is unsupported by current Api implementation");
106
+ }
107
+ return this._client.api.requestLoginQRCode();
108
+ }
109
+ async login(t) {
110
+ this._cacheMgr || (this._cacheMgr = new u(t), await this._cacheMgr.init()), await super.login(t);
111
+ await this._cacheMgr.getContact(this.currentUserId) || await this._updateContactCache(this._client.selfContact.toObject());
112
+ }
113
+ ding(t) {
114
+ h.silly("PuppetXp", "ding(%s)", t || ""), setTimeout((() => this.emit("dong", {
115
+ data: t || ""
116
+ })), 1e3);
117
+ }
118
+ async contactSelfName(t) {
119
+ await this._client.api.updateSelfNickName(t), this._client.selfContact.setNickname(t);
120
+ const contact = await this.contactRawPayload(this._client.selfContact.getUsername());
121
+ contact.nickname = t, await this._updateContactCache(contact);
122
+ }
123
+ async contactSelfQRCode() {
124
+ const t = await this._client.api.getContactQRCode(this._client.selfContact.getUsername(), 1);
125
+ return l.fromBase64(`${t.Data.qrcode.buffer}`, `qr-${this.currentUserId}.jpg`).toQRCode();
126
+ }
127
+ async contactSelfSignature(t) {
128
+ await this._client.api.updateSelfSignature(t), this._client.selfContact.setSignature(t);
129
+ const contact = await this.contactRawPayload(this._client.selfContact.getUsername());
130
+ contact.signature = t, await this._updateContactCache(contact);
131
+ }
132
+ async contactAlias(t, e) {
133
+ const contact = await this.contactRawPayload(t);
134
+ if (!e) {
135
+ return contact.remark;
136
+ }
137
+ if (-1 !== contact.username.indexOf("@stranger")) {
138
+ if (await this._cacheMgr.setContactStrangerAlias(contact.username, e), t.startsWith(B)) {
139
+ const a = await (this._cacheMgr?.getContactSearch(t));
140
+ a && a.contact && (a.contact.remark = e, await this._cacheMgr.setContactSearch(t, a));
141
+ }
142
+ } else {
143
+ await this._client.api.updateContactRemark(contact.username, e), contact.remark = e,
144
+ await this._updateContactCache(contact);
145
+ }
146
+ }
147
+ async contactAvatar(t, e) {
148
+ if (e) {
149
+ throw new Error("set avatar is not unsupported");
150
+ }
151
+ const contact = await this.contactRawPayload(t);
152
+ return l.fromUrl(contact.avatar, {
153
+ name: `avatar-${t}.jpg`
154
+ });
155
+ }
156
+ async contactList() {
157
+ return this._cacheMgr.getContactIds();
158
+ }
159
+ contactCorporationRemark(t, e) {
160
+ throw new Error(`contactCorporationRemark(${t}, ${e}) called failed: Method not supported.`);
161
+ }
162
+ contactDescription(t, e) {
163
+ throw new Error(`contactDescription(${t}, ${e}) called failed: Method not supported.`);
164
+ }
165
+ contactPhone(t, e) {
166
+ throw new Error(`contactPhone(${t}, ${e}) called failed: Method not supported.`);
167
+ }
168
+ async conversationReadMark(t, e) {
169
+ h.verbose("PuppetService", "conversationRead(%s, %s)", t, e);
170
+ }
171
+ async messageContact(t) {
172
+ throw new Error("not implement");
173
+ }
174
+ async messageFile(e) {
175
+ const a = await this.messageRawPayload(e), message = await this.messageRawPayloadParser(a);
176
+ switch (message.type) {
177
+ case g.types.Message.Image:
178
+ return this._getMessageImageFileBox(e, a, g.types.Image.HD);
179
+
180
+ case g.types.Message.Audio:
181
+ {
182
+ let s;
183
+ s = a.binarypayload && a.binarypayload.length ? "string" == typeof a.binarypayload ? Buffer.from(a.binarypayload, "base64") : Buffer.from(a.binarypayload) : await this._client.api.getMessageVoice(message.id, message.text, a.fromusername);
184
+ const i = l.fromBase64(s, `message-${e}-audio.sil`), n = {
185
+ attrNodeName: "$",
186
+ attributeNamePrefix: "",
187
+ ignoreAttributes: !1
188
+ }, r = t.parse(a.content, n), o = parseInt(r.msg.voicemsg.$.voicelength, 10);
189
+ return i.metadata = {
190
+ voiceLength: o
191
+ }, i;
192
+ }
193
+
194
+ case g.types.Message.Video:
195
+ {
196
+ const t = await this._client.api.getMessageVideo(message.id, message.text, a.tousername);
197
+ return l.fromBase64(t, `message-${e}-video.mp4`);
198
+ }
199
+
200
+ case g.types.Message.Attachment:
201
+ {
202
+ const t = await n(a.content), e = await this._client.api.getMessageAttach(message.text, a.tousername);
203
+ return l.fromBase64(e, t.title);
204
+ }
205
+
206
+ case g.types.Message.Emoticon:
207
+ {
208
+ const t = await c(a), s = l.fromUrl(t.cdnurl, {
209
+ name: `message-${e}-emoticon.jpg`
210
+ });
211
+ return s.metadata = {
212
+ payload: t,
213
+ type: "emoticon"
214
+ }, s;
215
+ }
216
+
217
+ case g.types.Message.MiniProgram:
218
+ try {
219
+ const t = await this._client.api.getMessageMiniProgramThumb(a.content, a.tousername);
220
+ return l.fromBase64(t, `message-${e}-miniprogram-thumb.jpg`);
221
+ } catch (t) {
222
+ return h.warn(`fail to get thumb for miniprogram message:${e}, payload: ${JSON.stringify(a)}`),
223
+ l.fromBuffer(Buffer.from(new ArrayBuffer(0)), `message-${e}-url-thumb.jpg`);
224
+ }
225
+
226
+ case g.types.Message.Url:
227
+ {
228
+ const t = await n(a.content);
229
+ if (t.thumburl) {
230
+ return l.fromUrl(t.thumburl);
231
+ }
232
+ try {
233
+ const t = await this._client.api.getMessageAttachThumb(a.content, a.tousername);
234
+ return l.fromBase64(t, `message-${e}-url-thumb.jpg`);
235
+ } catch (a) {
236
+ return h.warn(`fail to get thumb for url message:${e}, payload: ${JSON.stringify(t)}`),
237
+ l.fromBuffer(Buffer.from(new ArrayBuffer(0)), `message-${e}-url-thumb.jpg`);
238
+ }
239
+ }
240
+
241
+ default:
242
+ throw new Error(`Can not get file for message: ${e}`);
243
+ }
244
+ }
245
+ async messageImage(t, e) {
246
+ const a = await this.messageRawPayload(t);
247
+ return this._getMessageImageFileBox(t, a, e);
248
+ }
249
+ async messageMiniProgram(t) {
250
+ const e = await this.messageRawPayload(t);
251
+ if ((await this.messageRawPayloadParser(e)).type !== g.types.Message.MiniProgram) {
252
+ throw new Error("message is not mini program, can not get MiniProgramPayload");
253
+ }
254
+ return r(e);
255
+ }
256
+ async messageUrl(t) {
257
+ const e = await this.messageRawPayload(t);
258
+ if ((await this.messageRawPayloadParser(e)).type !== g.types.Message.Url) {
259
+ throw new Error("Can not get url from non url payload");
260
+ }
261
+ const a = await n(e.content);
262
+ return {
263
+ description: a.des,
264
+ thumbnailUrl: a.thumburl,
265
+ title: a.title,
266
+ url: a.url
267
+ };
268
+ }
269
+ async messageSendText(t, e, a) {
270
+ const s = await this.client.api.send_text(t, e, a || []), i = _(t) ? `${this._client.selfContact.getNickname()}: ${e}` : e;
271
+ return await this._onSendMessage((new U).setType(o.Text).setFromusername(this.currentUserId).setTousername(t).setContent(e).setPushcontent(i), s.getMsgid(), s.getMessagerevokeinfo()),
272
+ s.getMsgid();
273
+ }
274
+ async messageSendFile(t, e) {
275
+ const a = e.metadata;
276
+ if ("emoticon" === a.type) {
277
+ const e = a.payload, s = await this._client.api.sendMessageEmoji(t, e.md5, e.len), i = _(t) ? `${this._client.selfContact.getNickname()}: [动画表情]` : "[动画表情]", n = m(e);
278
+ return await this._onSendMessage((new U).setType(o.Emoticon).setFromusername(this.currentUserId).setTousername(t).setContent(n).setPushcontent(i), s.getMsgid(), s.getMessagerevokeinfo()),
279
+ s.getMsgid();
280
+ }
281
+ if (e.mediaType.startsWith("image/")) {
282
+ const a = await e.toBase64(), s = await this._client.api.sendImageMessage(t, a), i = _(t) ? `${this._client.selfContact.getNickname()}: [图片]` : "[图片]";
283
+ return await this._onSendMessage((new U).setType(o.Text).setFromusername(this.currentUserId).setTousername(t).setContent(i).setPushcontent(i), s.getMsgid(), s.getMessagerevokeinfo()),
284
+ s.getMsgid();
285
+ }
286
+ if ("audio/silk" === e.mediaType) {
287
+ const a = await e.toBase64(), s = await this._client.api.sendVoiceMessage(t, a, e.metadata.voiceLength), i = _(t) ? `${this._client.selfContact.getNickname()}: [语音]` : "[语音]";
288
+ return await this._onSendMessage((new U).setType(o.Text).setFromusername(this.currentUserId).setTousername(t).setContent(i).setPushcontent(i), s.getMsgid(), s.getMessagerevokeinfo()),
289
+ s.getMsgid();
290
+ }
291
+ if (e.mediaType.startsWith("video/")) {
292
+ const a = await e.toBase64(), s = await async function(t) {
293
+ const e = await t.toBuffer(), a = $.join(I.tmpdir(), `temp_video_${Date.now()}.mp4`), s = $.join(I.tmpdir(), `temp_image_${Date.now()}.jpg`);
294
+ try {
295
+ v.writeFileSync(a, e), await new Promise(((t, e) => {
296
+ k(a).outputOptions("-frames:v 1").output(s).on("end", t).on("error", e).run();
297
+ }));
298
+ return v.readFileSync(s).toString("base64");
299
+ } finally {
300
+ v.existsSync(a) && v.unlinkSync(a), v.existsSync(s) && v.unlinkSync(s);
301
+ }
302
+ }(e), i = await this._client.api.sendVideoMessage(t, a, s), n = _(t) ? `${this._client.selfContact.getNickname()}: [视频]` : "[视频]";
303
+ return await this._onSendMessage((new U).setType(o.Text).setFromusername(this.currentUserId).setTousername(t).setContent(n).setPushcontent(n), i.getMsgid(), i.getMessagerevokeinfo()),
304
+ i.getMsgid();
305
+ }
306
+ {
307
+ const a = await e.toBase64(), s = await this._client.api.sendFileMessage(`data:${e.mediaType};base64,${a}`), i = _(t) ? `${this._client.selfContact.getNickname()}: [文件]` : "[文件]";
308
+ return await this._onSendMessage((new U).setType(o.Text).setFromusername(this.currentUserId).setTousername(t).setContent(i).setPushcontent(i), s.getMsgid(), s.getMessagerevokeinfo()),
309
+ s.getMsgid();
310
+ }
311
+ }
312
+ async messageSendContact(t, e) {
313
+ const a = await this.contactRawPayload(e), contact = (new x).setUsername(a.username).setNickname(a.nickname).setAvatar(a.avatar).setGender(a.gender).setSignature(a.signature).setAlias(a.alias).setLabel(a.label).setRemark(a.remark).setCity(a.city).setProvince(a.province).setCountry(a.country).setContactaddscene(a.contactaddscene).setStranger(a.stranger), s = await this._client.api.sendContactCardMessage(t, contact), i = (_(t) ? `${this._client.selfContact.getNickname()}: ` : "") + "向你推荐了" + contact.getNickname();
314
+ return await this._onSendMessage((new U).setType(o.Text).setFromusername(this.currentUserId).setTousername(t).setContent(i).setPushcontent(i), s.getMsgid(), s.getMessagerevokeinfo()),
315
+ s.getMsgid();
316
+ }
317
+ async messageSendUrl(t, a) {
318
+ const s = new A;
319
+ s.setTitle(a.title).setUrl(a.url), a.description && s.setDescription(a.description),
320
+ a.thumbnailUrl && s.setThumburl(a.thumbnailUrl);
321
+ const i = await this._client.api.sendMessageLink(e(), t, s), n = _(t) ? `${this._client.selfContact.getNickname()}: [链接] ${a.title}` : `[链接] ${a.title}`;
322
+ return await this._onSendMessage((new U).setType(o.App).setFromusername(this.currentUserId).setTousername(t).setContent(i.getMsgcontent()).setPushcontent(n), i.getMsgid(), i.getMessagerevokeinfo()),
323
+ i.getMsgid();
324
+ }
325
+ async messageSendMiniProgram(t, e) {
326
+ const a = await this._client.api.sendMessageMiniProgram(t, e), s = _(t) ? `${this._client.selfContact.getNickname()}: [小程序] ${e.title}` : `[小程序] ${e.title}`;
327
+ return await this._onSendMessage((new U).setType(o.App).setFromusername(this.currentUserId).setTousername(t).setContent(a.getMsgcontent()).setPushcontent(s), a.getMsgid(), a.getMessagerevokeinfo()),
328
+ a.getMsgid();
329
+ }
330
+ async messageRecall(t) {
331
+ const message = await this._cacheMgr.getMessage(t), e = await this._cacheMgr.getMessageRevokeInfo(t);
332
+ return await this._client.api.revokeMessage(message.tousername, e.clientmsgid, e.newclientmsgid),
333
+ !0;
334
+ }
335
+ async forwardMessagewenjian(t, e) {
336
+ await this._client.api.forwardMessagewenjian(t, e);
337
+ }
338
+ async messageForward(t, e) {
339
+ const a = await this.messageRawPayload(e), message = await this.messageRawPayloadParser(a);
340
+ let s;
341
+ switch (message.type) {
342
+ case g.types.Message.Text:
343
+ s = await this.messageSendText(t, message.text);
344
+ break;
345
+
346
+ case g.types.Message.Image:
347
+ {
348
+ const a = await this.messageImage(e, g.types.Image.HD);
349
+ s = await this.messageSendFile(t, a);
350
+ break;
351
+ }
352
+
353
+ case g.types.Message.Audio:
354
+ {
355
+ const a = await this.messageFile(e);
356
+ s = await this.messageSendFile(t, a);
357
+ break;
358
+ }
359
+
360
+ case g.types.Message.Video:
361
+ {
362
+ const a = await this.messageFile(e);
363
+ s = await this.messageSendFile(t, a);
364
+ break;
365
+ }
366
+
367
+ case g.types.Message.Attachment:
368
+ {
369
+ const e = await this._client.api.forwardMessagewenjian(t, a.content);
370
+ s = e.getMsgid();
371
+ let i = a.pushcontent;
372
+ i && -1 !== i.indexOf(":") && (i = i.split(":")[1]), _(t) && (i = `${this._client.selfContact.getNickname()}:${i}`),
373
+ await this._onSendMessage((new U).setType(o.App).setFromusername(this.currentUserId).setTousername(t).setContent(e.getMsgcontent()).setPushcontent(i), e.getMsgid(), e.getMessagerevokeinfo());
374
+ break;
375
+ }
376
+
377
+ case g.types.Message.MiniProgram:
378
+ case g.types.Message.Url:
379
+ {
380
+ const e = await this._client.api.forwardMessage(t, a.content);
381
+ s = e.getMsgid();
382
+ let i = a.pushcontent;
383
+ i && -1 !== i.indexOf(":") && (i = i.split(":")[1]), _(t) && (i = `${this._client.selfContact.getNickname()}:${i}`),
384
+ await this._onSendMessage((new U).setType(o.App).setFromusername(this.currentUserId).setTousername(t).setContent(e.getMsgcontent()).setPushcontent(i), e.getMsgid(), e.getMessagerevokeinfo());
385
+ break;
386
+ }
387
+
388
+ case g.types.Message.Emoticon:
389
+ {
390
+ const a = await this.messageFile(e);
391
+ s = await this.messageSendFile(t, a);
392
+ break;
393
+ }
394
+
395
+ default:
396
+ throw new Error(`Message forwarding is unsupported for messageId:${e}, type:${message.type}`);
397
+ }
398
+ return s;
399
+ }
400
+ async roomAdd(t, e) {
401
+ await this._client.api.inviteChatroomMembers(t, e);
402
+ }
403
+ async roomAvatar(t) {
404
+ const e = await this.roomRawPayload(t);
405
+ return e.avatar ? l.fromUrl(e.avatar) : l.fromBuffer(Buffer.from(new ArrayBuffer(0)), `room-${e.username}-avatar.jpg`);
406
+ }
407
+ async roomQRCode(t) {
408
+ const e = await this._client.api.getChatRoomQrCode(t);
409
+ return l.fromBase64(`${e.Data.qrcode.buffer}`, `qr-${this.currentUserId}.jpg`).toQRCode();
410
+ }
411
+ async logout() {
412
+ this.isLoggedIn && (await this._client.api.logout(), this.emit("logout", {
413
+ contactId: this.currentUserId,
414
+ data: "logout by self"
415
+ }));
416
+ }
417
+ async roomCreate(t, e) {
418
+ return (await this._client.api.createChatRoom(t, e)).getRoomid();
419
+ }
420
+ async roomDel(t, e) {
421
+ await this._client.api.deleteChatRoomMember(t, e);
422
+ }
423
+ async roomList() {
424
+ return this._cacheMgr.getRoomIds();
425
+ }
426
+ async roomQuit(t) {
427
+ await this._client.api.quitChatRoom(t);
428
+ }
429
+ async roomTopic(t, e) {
430
+ await this._client.api.setChatRoomName(t, e || "");
431
+ }
432
+ async roomAnnounce(t, e) {
433
+ if (void 0 === e) {
434
+ return this._client.api.getChatRoomAnnouncement(t);
435
+ }
436
+ await this._client.api.setChatRoomAnnouncement(t, e);
437
+ }
438
+ async roomMemberList(t) {
439
+ const e = await this._getRoomMemberList(t);
440
+ return Object.values(e).map((t => t.username));
441
+ }
442
+ async roomInvitationAccept(t) {
443
+ const e = await this.roomInvitationRawPayload(t);
444
+ await this._client.api.acceptChatRoomInvitation(e.inviterId, e.invitation);
445
+ }
446
+ async friendshipAccept(t) {
447
+ const friendship = await this.friendshipRawPayload(t), e = friendship.contactId;
448
+ M(e) && await this._refreshContact(e, friendship.ticket), await this._client.api.acceptUser(e, friendship.ticket, friendship.stranger, friendship.scene);
449
+ }
450
+ async isFriend(t) {
451
+ const e = await this._client.api.GetFriendRelation(t);
452
+ return 0 === e.Data?.FriendRelation;
453
+ }
454
+ async friendshipAdd(t, e) {
455
+ let a;
456
+ e && (a = "string" == typeof e ? e : e.hello);
457
+ const s = await this._client.api.getChatContact(t);
458
+ await this._client.api.addContact(s.getUsername(), s.getAntispamticket(), a);
459
+ }
460
+ async friendshipSearchPhone(t) {
461
+ return this._friendshipSearch(t);
462
+ }
463
+ async friendshipSearchWeixin(t) {
464
+ return this._friendshipSearch(t);
465
+ }
466
+ async _friendshipSearch(t) {
467
+ if (await this._cacheMgr.getContactSearch(t)) {
468
+ return t;
469
+ }
470
+ const e = await this._client.api.getChatContact(t), a = `${B}${t}`;
471
+ return await this._cacheMgr.setContactSearch(a, e.toObject()), a;
472
+ }
473
+ async _findRoomIdForUserName(t) {
474
+ const e = [], a = await (this._cacheMgr?.getRoomIds()) || [];
475
+ for (const s of a) {
476
+ const a = await (this._cacheMgr?.getRoomMember(s));
477
+ if (!a) {
478
+ continue;
479
+ }
480
+ -1 !== Object.keys(a).indexOf(t) && e.push(s);
481
+ }
482
+ return e;
483
+ }
484
+ async contactDelete(t) {
485
+ (await this._refreshContact(t)).getStranger() ? h.warn(`can not delete contact which is not a friend:: ${t}`) : (await this._client.api.deleteContact(t),
486
+ await this._refreshContact(t));
487
+ }
488
+ async tagContactAdd(t, e) {
489
+ const a = await this._findTagWithName(t, !0), contact = await this.contactRawPayload(e), s = contact.label.split(",").filter((t => t)).map((t => parseInt(t, 10)));
490
+ -1 === s.indexOf(a.getId()) ? (s.push(a.getId()), await this._client.api.setContactLabel(e, s),
491
+ contact.label = s.join(","), await this._updateContactCache(contact)) : h.warn(`contact: ${e} has already assigned tag: ${t}`);
492
+ }
493
+ async tagContactRemove(t, e) {
494
+ const a = await this._findTagWithName(t);
495
+ if (!a) {
496
+ throw new Error(`can not find tag with name: ${t}`);
497
+ }
498
+ const contact = await this.contactRawPayload(e), s = contact.label.split(",").filter((t => t)).map((t => parseInt(t, 10))), i = s.indexOf(a.getId());
499
+ -1 !== i ? (s.splice(i, 1), await this._client.api.setContactLabel(e, s), contact.label = s.join(","),
500
+ await this._updateContactCache(contact)) : h.warn(E, `contact: ${e} has no tag: ${t}`);
501
+ }
502
+ async tagContactDelete(t) {
503
+ const e = await this._findTagWithName(t, !1);
504
+ if (!e) {
505
+ throw new Error(`tag:${t} doesn't exist`);
506
+ }
507
+ await this._client.api.removeLabel(e.getId()), await this._getTagList(!0);
508
+ }
509
+ async tagContactList(t) {
510
+ if (t) {
511
+ const contact = await this.contactRawPayload(t);
512
+ if (!contact.label || !contact.label.length) {
513
+ return [];
514
+ }
515
+ const e = contact.label.split(",").filter((t => t)).map((t => parseInt(t, 10))), {labelList: a, fromCache: s} = await this._getTagList();
516
+ let i = a.filter((t => -1 !== e.indexOf(t.getId())));
517
+ if (i.length === e.length || !s) {
518
+ return i.map((t => t.getName()));
519
+ }
520
+ return i = (await this._getTagList(!0)).labelList.filter((t => -1 !== e.indexOf(t.getId()))),
521
+ i.map((t => t.getName()));
522
+ }
523
+ {
524
+ const {labelList: t} = await this._getTagList(!0);
525
+ return t.map((t => t.getName()));
526
+ }
527
+ }
528
+ async contactRawPayload(t) {
529
+ if (t.startsWith(B)) {
530
+ return (await (this._cacheMgr?.getContactSearch(t))).contact;
531
+ }
532
+ let e = await this._cacheMgr.getContact(t);
533
+ return e || (e = await f(`contactRawPayload-${t}`, (async () => (await this._refreshContact(t)).toObject()))),
534
+ e;
535
+ }
536
+ async contactRawPayloadParser(t) {
537
+ return b(t);
538
+ }
539
+ async messageRawPayload(t) {
540
+ const e = await this._cacheMgr.getMessage(t);
541
+ if (!e) {
542
+ throw new Error(`can not find message in cache for messageId: ${t}`);
543
+ }
544
+ return e;
545
+ }
546
+ async messageRawPayloadParser(t) {
547
+ return y(this, t);
548
+ }
549
+ async roomRawPayload(t) {
550
+ let e = await this._cacheMgr.getRoom(t);
551
+ if (!e) {
552
+ e = (await this._refreshContact(t)).toObject();
553
+ }
554
+ return e;
555
+ }
556
+ async roomRawPayloadParser(t) {
557
+ return P(t);
558
+ }
559
+ async roomMemberRawPayload(t, e) {
560
+ return (await this._getRoomMemberList(t))[e];
561
+ }
562
+ async roomMemberRawPayloadParser(t) {
563
+ return S(t);
564
+ }
565
+ async roomInvitationRawPayload(t) {
566
+ const e = await this._cacheMgr.getRoomInvitation(t);
567
+ if (!e) {
568
+ throw new Error(`Can not find room invitation for id: ${t}`);
569
+ }
570
+ return e;
571
+ }
572
+ async roomInvitationRawPayloadParser(t) {
573
+ return t;
574
+ }
575
+ async friendshipRawPayload(t) {
576
+ const e = await this._cacheMgr.getFriendshipRawPayload(t);
577
+ if (!e) {
578
+ throw new Error(`Can not find friendship for id: ${t}`);
579
+ }
580
+ return e;
581
+ }
582
+ async friendshipRawPayloadParser(t) {
583
+ return t;
584
+ }
585
+ async _onPushContact(contact, t) {
586
+ if (h.silly(E, `on push contact: ${JSON.stringify(contact.toObject())}`), await this._updateContactCache(contact.toObject(), t),
587
+ contact.getEncryptusername()) {
588
+ const t = await this._cacheMgr.getContactStrangerAlias(contact.getEncryptusername());
589
+ t && (await this.contactAlias(contact.getUsername(), t), await this._cacheMgr.deleteContactStrangerAlias(contact.getEncryptusername()));
590
+ }
591
+ }
592
+ async _onPushMessage(message) {
593
+ const t = message.getId();
594
+ if (h.silly(E, `on push original message: ${JSON.stringify("")}`), await this._cacheMgr.hasMessage(t)) {
595
+ return;
596
+ }
597
+ const e = message.toObject();
598
+ await this._cacheMgr.setMessage(message.getId(), e);
599
+ const a = await j(this, e);
600
+ switch (a.type) {
601
+ case L.Message:
602
+ this.emit("message", {
603
+ messageId: t
604
+ });
605
+ break;
606
+
607
+ case L.Friendship:
608
+ {
609
+ const friendship = a.payload;
610
+ await this._cacheMgr.setFriendshipRawPayload(t, friendship), this.emit("friendship", {
611
+ friendshipId: t
612
+ });
613
+ break;
614
+ }
615
+
616
+ case L.RoomInvite:
617
+ {
618
+ const e = a.payload;
619
+ await this._cacheMgr.setRoomInvitation(t, e), this.emit("room-invite", {
620
+ roomInvitationId: t
621
+ });
622
+ break;
623
+ }
624
+
625
+ case L.RoomJoin:
626
+ {
627
+ const t = a.payload;
628
+ this.emit("room-join", t), await this._updateRoomMember(t.roomId);
629
+ break;
630
+ }
631
+
632
+ case L.RoomLeave:
633
+ {
634
+ const t = a.payload;
635
+ this.emit("room-leave", t), await this._updateRoomMember(t.roomId);
636
+ break;
637
+ }
638
+
639
+ case L.RoomTopic:
640
+ {
641
+ const t = a.payload;
642
+ this.emit("room-topic", t);
643
+ break;
644
+ }
645
+ }
646
+ }
647
+ async _updateContactCache(contact, t) {
648
+ if (contact.username) {
649
+ if (_(contact.username)) {
650
+ const e = await this._cacheMgr.getRoom(contact.username);
651
+ if (e && !t && (contact.avatar || (contact.avatar = e.avatar), contact.chatroommemberList.length < e.chatroommemberList.length && Math.abs(e.chatroommemberList.length - contact.chatroommemberList.length) < 3)) {
652
+ const t = new Set(contact.chatroommemberList.map((t => t.username))), a = e.chatroommemberList.filter((e => !t.has(e.username))).map((t => t.username)).filter((t => !C(contact.username, t)));
653
+ a.length && a.forEach((t => {
654
+ const e = {
655
+ removeeIdList: [ t ],
656
+ removerId: t,
657
+ roomId: contact.username,
658
+ timestamp: Math.floor(Date.now() / 1e3)
659
+ };
660
+ this.emit("room-leave", e);
661
+ }));
662
+ }
663
+ const a = contact.username;
664
+ await this._cacheMgr.setRoom(a, contact), await this.dirtyPayload(g.types.Payload.Room, a),
665
+ await this._updateRoomMember(a);
666
+ } else {
667
+ await this._cacheMgr.setContact(contact.username, contact), await this.dirtyPayload(g.types.Payload.Contact, contact.username);
668
+ }
669
+ } else {
670
+ h.warn(E, `username is required for contact: ${JSON.stringify(contact)}`);
671
+ }
672
+ }
673
+ async _updateRoomMember(t, e) {
674
+ e ? await this._cacheMgr.setRoomMember(t, e) : await this._cacheMgr.deleteRoomMember(t),
675
+ await this.dirtyPayload(g.types.Payload.RoomMember, t);
676
+ }
677
+ async _refreshContact(t, e) {
678
+ const contact = await this._client.api.getContact(t, e);
679
+ return contact.getUsername() || contact.setUsername(t), await this._updateContactCache(contact.toObject()),
680
+ contact;
681
+ }
682
+ async _getMessageImageFileBox(t, e, a) {
683
+ if ((await this.messageRawPayloadParser(e)).type !== g.types.Message.Image) {
684
+ throw new Error(`message ${t} is not image type message`);
685
+ }
686
+ if (a === g.types.Image.Thumbnail && e.binarypayload && e.binarypayload.length) {
687
+ const a = Buffer.from(e.binarypayload);
688
+ return l.fromBuffer(a, `message-${t}-image-thumb.jpg`);
689
+ }
690
+ let s;
691
+ s = a === g.types.Image.Thumbnail ? i.THUMB : (g.types.Image.HD, i.HD);
692
+ try {
693
+ const a = await this._client.api.getMessageImage(e.content, e.tousername, s);
694
+ let n;
695
+ return n = a.imageType === i.THUMB ? "thumb" : a.imageType === i.HD ? "hd" : "normal",
696
+ a.imageData ? l.fromBase64(a.imageData, `message-${t}-image-${n}.jpg`) : l.fromBuffer(Buffer.from(new ArrayBuffer(0)), `message-${t}-image-${(new Date).getTime()}.jpg`);
697
+ } catch (e) {
698
+ return l.fromBuffer(Buffer.from(new ArrayBuffer(0)), `message-${t}-image-${(new Date).getTime()}.jpg`);
699
+ }
700
+ }
701
+ async _onSendMessage(t, e, a) {
702
+ t.setId(e), t.setCreatetime(a.getCreatetime()), await this._cacheMgr.setMessage(e, t.toObject()),
703
+ await this._cacheMgr.setMessageRevokeInfo(e, a.toObject()), setImmediate((() => {
704
+ this.emit("message", {
705
+ messageId: e
706
+ });
707
+ }));
708
+ }
709
+ async _findTagWithName(t, e) {
710
+ let a = (await this._getTagList()).labelList, s = a.find((e => e.getName() === t));
711
+ if (s || (a = (await this._getTagList(!0)).labelList, s = a.find((e => e.getName() === t))),
712
+ !s && e) {
713
+ const e = await this._client.api.addLabel(t);
714
+ s = (new N).setId(e).setName(t), await this._getTagList(!0);
715
+ }
716
+ return s || null;
717
+ }
718
+ async _getTagList(t) {
719
+ let e = this._cacheMgr.getLabelList(), a = !0;
720
+ return e && !t || (e = await this._client.api.getLabelList(), this._cacheMgr?.setLabelList(e),
721
+ a = !1), {
722
+ fromCache: a,
723
+ labelList: e
724
+ };
725
+ }
726
+ async _getRoomMemberList(t, e) {
727
+ if (!this._cacheMgr) {
728
+ return {};
729
+ }
730
+ let a = await this._cacheMgr.getRoomMember(t);
731
+ if (!a || e) {
732
+ const e = await this._client.api.getChatRoomMembers(t), s = {};
733
+ for (const t of e) {
734
+ const contact = R(t);
735
+ await this._cacheMgr.hasContact(contact.getUsername()) || await this._cacheMgr.setContact(contact.getUsername(), contact.toObject()),
736
+ s[t.getUsername()] = t.toObject();
737
+ }
738
+ a = s, await this._updateRoomMember(t, s);
739
+ }
740
+ return a;
741
+ }
742
+ async _startClient() {
743
+ w(this), await this._setupClient();
744
+ const t = {
745
+ [g.types.ScanStatus.Unknown]: "Unknown",
746
+ [g.types.ScanStatus.Cancel]: "Cancel",
747
+ [g.types.ScanStatus.Waiting]: "Waiting",
748
+ [g.types.ScanStatus.Scanned]: "Scanned",
749
+ [g.types.ScanStatus.Confirmed]: "Confirmed",
750
+ [g.types.ScanStatus.Timeout]: "Timeout"
751
+ }, e = async e => {
752
+ let a, s = g.types.ScanStatus.Unknown;
753
+ switch (e.status) {
754
+ case 1:
755
+ a = e.imageUrl, s = g.types.ScanStatus.Waiting;
756
+ break;
757
+
758
+ case 2:
759
+ s = g.types.ScanStatus.Scanned;
760
+ break;
761
+
762
+ case 3:
763
+ s = g.types.ScanStatus.Confirmed;
764
+ break;
765
+
766
+ case 4:
767
+ s = g.types.ScanStatus.Cancel;
768
+ break;
769
+
770
+ case 5:
771
+ s = g.types.ScanStatus.Timeout;
772
+ }
773
+ h.silly(E, `scan event, status: ${t[s]}${a ? ", with qrcode: " + a : ""}`), this.emit("scan", {
774
+ qrcode: a,
775
+ status: s
776
+ });
777
+ };
778
+ this._client.api.login("", {
779
+ onLoginStart: t => {
780
+ h.info(E, "start login with type");
781
+ },
782
+ onLoginSuccess: async t => {
783
+ this.wrapAsync(this._onPushSerialExecutor.execute((async () => {
784
+ const t = this._client.selfContact.getUsername();
785
+ h.silly(E, `login success: ${t}`), await this.login(this._client.selfContact.getUsername()),
786
+ h.silly(E, "login success: DONE");
787
+ })));
788
+ },
789
+ onOneClickEvent: e,
790
+ onQrCodeEvent: e,
791
+ onSync: async t => {
792
+ this.wrapAsync(this._onPushSerialExecutor.execute((async () => {
793
+ for (const contact of t.getContactList()) {
794
+ await this._onPushContact(contact, !0);
795
+ }
796
+ for (const message of t.getMessageList()) {}
797
+ })));
798
+ }
799
+ }).then((() => (this.wrapAsync(this._onPushSerialExecutor.execute((async () => {
800
+ h.silly(E, "on ready"), this.emit("ready", {
801
+ data: "ready"
802
+ });
803
+ }))), null))).catch((async error => {
804
+ h.error(`start client failed: ${error.stack}`);
805
+ }));
806
+ }
807
+ async _setupClient() {
808
+ this._client = new a(this.options), this._client.on("kickout", this.wrapAsync((async () => {
809
+ this.currentUserId && this.emit("logout", {
810
+ contactId: this.currentUserId,
811
+ data: "退出登录"
812
+ }), this.__currentUserId = void 0;
813
+ }))), this._client.on("message", (async t => {
814
+ await this._onPushSerialExecutor.execute((async () => {
815
+ for (const message of t) {
816
+ await this._onPushMessage(message);
817
+ }
818
+ }));
819
+ })), this._client.on("contact", this.wrapAsync((async t => {
820
+ await this._onPushSerialExecutor.execute((async () => {
821
+ for (const contact of t) {
822
+ await this._onPushContact(contact);
823
+ }
824
+ }));
825
+ }))), this._printVersion && (this._printVersion = !1, h.info(`\n ============================================================\n Welcome to Wechaty Padplus puppet plus!\n - puppet-Padplus version: ${VERSION}\n ============================================================\n `));
826
+ }
827
+ }
828
+
829
+ export { PuppetPlus, PuppetPlus as default };