1 |
|
2 | import d from 'debug';
|
3 | import uuid from 'uuid/v4';
|
4 | import IMClient from './im-client';
|
5 | import { RECONNECT, RECONNECT_ERROR } from './events/core';
|
6 | import { Conversation } from './conversations';
|
7 | import { MessageQueryDirection } from './conversations/conversation-base';
|
8 | import Message, { MessageStatus } from './messages/message';
|
9 | import BinaryMessage from './messages/binary-message';
|
10 | import TextMessage from './messages/text-message';
|
11 | import TypedMessage from './messages/typed-message';
|
12 | import RecalledMessage from './messages/recalled-message';
|
13 | import MessageParser from './message-parser';
|
14 | import { trim, internal, ensureArray, finalize } from './utils';
|
15 |
|
16 | const debug = d('LC:IMPlugin');
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | const MessagePriority = {
|
24 |
|
25 | HIGH: 1,
|
26 |
|
27 | NORMAL: 2,
|
28 |
|
29 | LOW: 3,
|
30 | };
|
31 | Object.freeze(MessagePriority);
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | const defineConversationProperty = (
|
49 | prop,
|
50 | descriptor = {
|
51 | get() {
|
52 | return this.get(prop);
|
53 | },
|
54 | set(value) {
|
55 | this.set(prop, value);
|
56 | },
|
57 | }
|
58 | ) => {
|
59 | Object.defineProperty(Conversation.prototype, prop, descriptor);
|
60 | };
|
61 |
|
62 | export {
|
63 | |
64 |
|
65 |
|
66 | Message,
|
67 | |
68 |
|
69 |
|
70 | BinaryMessage,
|
71 | |
72 |
|
73 |
|
74 | TypedMessage,
|
75 | |
76 |
|
77 |
|
78 | TextMessage,
|
79 | |
80 |
|
81 |
|
82 | RecalledMessage,
|
83 | MessagePriority,
|
84 | MessageStatus,
|
85 | MessageQueryDirection,
|
86 | defineConversationProperty,
|
87 | };
|
88 |
|
89 | export {
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | messageType,
|
104 | |
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | messageField,
|
127 | IE10Compatible,
|
128 | } from './messages/helpers';
|
129 |
|
130 | export { ConversationMemberRole } from './conversation-member-info';
|
131 | export {
|
132 | |
133 |
|
134 |
|
135 | Conversation,
|
136 | |
137 |
|
138 |
|
139 | ChatRoom,
|
140 | |
141 |
|
142 |
|
143 | ServiceConversation,
|
144 | |
145 |
|
146 |
|
147 | TemporaryConversation,
|
148 | } from './conversations';
|
149 |
|
150 | const onRealtimeCreate = realtime => {
|
151 |
|
152 | const deviceId = uuid();
|
153 | realtime._IMClients = {};
|
154 | realtime._IMClientsCreationCount = 0;
|
155 | const messageParser = new MessageParser(realtime._plugins);
|
156 | realtime._messageParser = messageParser;
|
157 |
|
158 | const signAVUser = async user =>
|
159 | realtime._request({
|
160 | method: 'POST',
|
161 | path: '/rtm/sign',
|
162 | data: {
|
163 | session_token: user.getSessionToken(),
|
164 | },
|
165 | });
|
166 |
|
167 | |
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | const register = messageClass =>
|
179 | ensureArray(messageClass).map(messageParser.register.bind(messageParser));
|
180 | register(ensureArray(realtime._plugins.messageClasses));
|
181 | |
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | const createIMClient = async (
|
196 | identity,
|
197 | { tag, isReconnect, ...clientOptions } = {},
|
198 | lagecyTag
|
199 | ) => {
|
200 | let id;
|
201 | const buildinOptions = {};
|
202 | if (identity) {
|
203 | if (typeof identity === 'string') {
|
204 | id = identity;
|
205 | } else if (identity.id && identity.getSessionToken) {
|
206 | ({ id } = identity);
|
207 | const sessionToken = identity.getSessionToken();
|
208 | if (!sessionToken) {
|
209 | throw new Error('User must be authenticated');
|
210 | }
|
211 | buildinOptions.signatureFactory = signAVUser;
|
212 | } else {
|
213 | throw new TypeError('Identity must be a String or an AV.User');
|
214 | }
|
215 | if (realtime._IMClients[id] !== undefined) {
|
216 | return realtime._IMClients[id];
|
217 | }
|
218 | }
|
219 | if (lagecyTag) {
|
220 | console.warn(
|
221 | 'DEPRECATION createIMClient tag param: Use options.tag instead.'
|
222 | );
|
223 | }
|
224 | const _tag = tag || lagecyTag;
|
225 | const promise = realtime
|
226 | ._open()
|
227 | .then(connection => {
|
228 | const client = new IMClient(
|
229 | id,
|
230 | { ...buildinOptions, ...clientOptions },
|
231 | {
|
232 | _connection: connection,
|
233 | _request: realtime._request.bind(realtime),
|
234 | _messageParser: messageParser,
|
235 | _plugins: realtime._plugins,
|
236 | _identity: identity,
|
237 | }
|
238 | );
|
239 | connection.on(RECONNECT, () =>
|
240 | client
|
241 | ._open(realtime._options.appId, _tag, deviceId, true)
|
242 | |
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | |
249 |
|
250 |
|
251 |
|
252 |
|
253 | .then(
|
254 | () => client.emit(RECONNECT),
|
255 | error => client.emit(RECONNECT_ERROR, error)
|
256 | )
|
257 | );
|
258 | internal(client)._eventemitter.on(
|
259 | 'beforeclose',
|
260 | () => {
|
261 | delete realtime._IMClients[client.id];
|
262 | if (realtime._firstIMClient === client) {
|
263 | delete realtime._firstIMClient;
|
264 | }
|
265 | },
|
266 | realtime
|
267 | );
|
268 | internal(client)._eventemitter.on(
|
269 | 'close',
|
270 | () => {
|
271 | realtime._deregister(client);
|
272 | },
|
273 | realtime
|
274 | );
|
275 | return client
|
276 | ._open(realtime._options.appId, _tag, deviceId, isReconnect)
|
277 | .then(() => {
|
278 | realtime._IMClients[client.id] = client;
|
279 | realtime._IMClientsCreationCount += 1;
|
280 | if (realtime._IMClientsCreationCount === 1) {
|
281 | client._omitPeerId(true);
|
282 | realtime._firstIMClient = client;
|
283 | } else if (
|
284 | realtime._IMClientsCreationCount > 1 &&
|
285 | realtime._firstIMClient
|
286 | ) {
|
287 | realtime._firstIMClient._omitPeerId(false);
|
288 | }
|
289 | realtime._register(client);
|
290 | return client;
|
291 | })
|
292 | .catch(error => {
|
293 | delete realtime._IMClients[client.id];
|
294 | throw error;
|
295 | });
|
296 | })
|
297 | .then(
|
298 | ...finalize(() => {
|
299 | realtime._deregisterPending(promise);
|
300 | })
|
301 | );
|
302 | if (identity) {
|
303 | realtime._IMClients[id] = promise;
|
304 | }
|
305 | realtime._registerPending(promise);
|
306 | return promise;
|
307 | };
|
308 | Object.assign(realtime, {
|
309 | register,
|
310 | createIMClient,
|
311 | });
|
312 |
|
313 | };
|
314 |
|
315 | const beforeCommandDispatch = (command, realtime) => {
|
316 | const isIMCommand = command.service === null || command.service === 2;
|
317 | if (!isIMCommand) return true;
|
318 | const targetClient = command.peerId
|
319 | ? realtime._IMClients[command.peerId]
|
320 | : realtime._firstIMClient;
|
321 | if (targetClient) {
|
322 | Promise.resolve(targetClient)
|
323 | .then(client => client._dispatchCommand(command))
|
324 | .catch(debug);
|
325 | } else {
|
326 | debug(
|
327 | '[WARN] Unexpected message received without any live client match: %O',
|
328 | trim(command)
|
329 | );
|
330 | }
|
331 | return false;
|
332 | };
|
333 |
|
334 | export const IMPlugin = {
|
335 | name: 'leancloud-realtime-plugin-im',
|
336 | onRealtimeCreate,
|
337 | beforeCommandDispatch,
|
338 | messageClasses: [Message, BinaryMessage, RecalledMessage, TextMessage],
|
339 | };
|