@amityco/ts-sdk-react-native 7.0.1-91b5585.0 → 7.0.2-3cbb4fc.0

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 (39) hide show
  1. package/dist/@types/core/linkPreview.d.ts +12 -0
  2. package/dist/@types/core/linkPreview.d.ts.map +1 -0
  3. package/dist/@types/domains/client.d.ts +0 -1
  4. package/dist/@types/domains/client.d.ts.map +1 -1
  5. package/dist/client/api/createClient.d.ts.map +1 -1
  6. package/dist/client/api/fetchLinkPreview.d.ts +14 -0
  7. package/dist/client/api/fetchLinkPreview.d.ts.map +1 -0
  8. package/dist/client/api/index.d.ts +1 -0
  9. package/dist/client/api/index.d.ts.map +1 -1
  10. package/dist/core/model/index.d.ts +0 -1
  11. package/dist/core/model/index.d.ts.map +1 -1
  12. package/dist/index.cjs.js +48 -122
  13. package/dist/index.esm.js +48 -122
  14. package/dist/index.umd.js +3 -3
  15. package/dist/messageRepository/api/createMessage.d.ts.map +1 -1
  16. package/dist/messageRepository/utils/getMessageFromMainDB.d.ts.map +1 -1
  17. package/dist/messageRepository/utils/index.d.ts +0 -1
  18. package/dist/messageRepository/utils/index.d.ts.map +1 -1
  19. package/dist/messageRepository/utils/prepareMessagePayload.d.ts.map +1 -1
  20. package/dist/utils/event.d.ts.map +1 -1
  21. package/package.json +1 -1
  22. package/src/@types/core/linkPreview.ts +12 -0
  23. package/src/@types/domains/client.ts +0 -2
  24. package/src/client/api/createClient.ts +0 -26
  25. package/src/client/api/fetchLinkPreview.ts +30 -0
  26. package/src/client/api/index.ts +1 -0
  27. package/src/core/model/index.ts +0 -1
  28. package/src/messageRepository/api/createMessage.ts +0 -7
  29. package/src/messageRepository/observers/getMessages/MessageQueryStreamController.ts +4 -4
  30. package/src/messageRepository/utils/getMessageFromMainDB.ts +5 -9
  31. package/src/messageRepository/utils/index.ts +0 -1
  32. package/src/messageRepository/utils/prepareMessagePayload.ts +17 -22
  33. package/src/utils/event.ts +2 -19
  34. package/dist/core/model/localReferenceId.d.ts +0 -2
  35. package/dist/core/model/localReferenceId.d.ts.map +0 -1
  36. package/dist/messageRepository/utils/getMessageIdentifierIds.d.ts +0 -2
  37. package/dist/messageRepository/utils/getMessageIdentifierIds.d.ts.map +0 -1
  38. package/src/core/model/localReferenceId.ts +0 -12
  39. package/src/messageRepository/utils/getMessageIdentifierIds.ts +0 -37
@@ -1 +1 @@
1
- {"version":3,"file":"createMessage.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/api/createMessage.ts"],"names":[],"mappings":"AAmBA,aAAK,kBAAkB,CAAC,CAAC,SAAS,KAAK,CAAC,kBAAkB,IAAI,IAAI,CAChE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAChB,cAAc,GAAG,UAAU,GAAG,UAAU,GAAG,MAAM,GAAG,UAAU,GAAG,YAAY,CAC9E,GAAG;IACF,IAAI,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;IAChC,MAAM,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC9B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAwEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,aAAa,yEAEvB,QAAQ,MAAM,MAAM,CAAC,MAAM,OAAO,CAAC,CA2CrC,CAAC"}
1
+ {"version":3,"file":"createMessage.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/api/createMessage.ts"],"names":[],"mappings":"AAmBA,aAAK,kBAAkB,CAAC,CAAC,SAAS,KAAK,CAAC,kBAAkB,IAAI,IAAI,CAChE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAChB,cAAc,GAAG,UAAU,GAAG,UAAU,GAAG,MAAM,GAAG,UAAU,GAAG,YAAY,CAC9E,GAAG;IACF,IAAI,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;IAChC,MAAM,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC9B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAqEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,aAAa,yEAEvB,QAAQ,MAAM,MAAM,CAAC,MAAM,OAAO,CAAC,CAuCrC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"getMessageFromMainDB.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/utils/getMessageFromMainDB.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,oBAAoB,cAAe,MAAM,KAAG,MAAM,eAAe,GAAG,SAShF,CAAC"}
1
+ {"version":3,"file":"getMessageFromMainDB.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/utils/getMessageFromMainDB.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,oBAAoB,cAAe,MAAM,KAAG,MAAM,eAAe,GAAG,SAMhF,CAAC"}
@@ -1,3 +1,2 @@
1
1
  export * from './prepareMessagePayload';
2
- export * from './getMessageIdentifierIds';
3
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"prepareMessagePayload.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/utils/prepareMessagePayload.ts"],"names":[],"mappings":"AAiCA,wBAAgB,cAAc,CAC5B,OAAO,EAAE,KAAK,CAAC,UAAU,EACzB,QAAQ,CAAC,EAAE,KAAK,CAAC,eAAe,EAAE,EAClC,KAAK,CAAC,EAAE,MAAM,KAAK,CAAC,iBAAiB,GACpC,KAAK,CAAC,eAAe,CAqEvB;AAWD,eAAO,MAAM,qBAAqB,YACvB,MAAM,cAAc,UACrB,MAAM,MAAM,iBAAiB,KACpC,QAAQ,MAAM,uBAAuB,CAyCvC,CAAC;AAEF,aAAK,gBAAgB,GAAG,IAAI,CAC1B,KAAK,CAAC,aAAa,EAIjB,MAAM,GACN,QAAQ,GACR,cAAc,GACd,MAAM,GACN,gBAAgB,GAChB,iBAAiB,GACjB,UAAU,GACV,eAAe,GACf,eAAe,CAClB,GAAG;IACF,WAAW,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,eAAe,CAAC,CAAC;IACnD,WAAW,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,eAAe,CAAC,CAAC;IACnD,SAAS,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IACvC,aAAa,EAAE,KAAK,CAAC,aAAa,CAAC,cAAc,CAAC,CAAC;IACnD,QAAQ,CAAC,EAAE,KAAK,CAAC,kBAAkB,CAAC;IACpC,OAAO,EAAE;QACP,MAAM,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QACvC,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,MAAM,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;KACrC,CAAC;CACH,CAAC;AAEF,wBAAgB,aAAa,CAAC,EAC5B,YAAY,EACZ,UAAU,EACV,QAAQ,EACR,IAAI,EACJ,GAAG,IAAI,EACR,EAAE,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAc9C;AAED,wBAAgB,kBAAkB,CAAC,EACjC,MAAM,EACN,YAAY,EACZ,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,KAAK,EACL,IAAI,EACJ,GAAG,IAAI,EACR,EAAE,KAAK,CAAC,sBAAsB,GAAG,gBAAgB,CAyBjD"}
1
+ {"version":3,"file":"prepareMessagePayload.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/utils/prepareMessagePayload.ts"],"names":[],"mappings":"AA4BA,wBAAgB,cAAc,CAC5B,OAAO,EAAE,KAAK,CAAC,UAAU,EACzB,QAAQ,CAAC,EAAE,KAAK,CAAC,eAAe,EAAE,EAClC,KAAK,CAAC,EAAE,MAAM,KAAK,CAAC,iBAAiB,GACpC,KAAK,CAAC,eAAe,CAqEvB;AAWD,eAAO,MAAM,qBAAqB,YACvB,MAAM,cAAc,UACrB,MAAM,MAAM,iBAAiB,KACpC,QAAQ,MAAM,uBAAuB,CAyCvC,CAAC;AAEF,aAAK,gBAAgB,GAAG,IAAI,CAC1B,KAAK,CAAC,aAAa,EAIjB,MAAM,GACN,QAAQ,GACR,cAAc,GACd,MAAM,GACN,gBAAgB,GAChB,iBAAiB,GACjB,UAAU,GACV,eAAe,GACf,eAAe,CAClB,GAAG;IACF,WAAW,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,eAAe,CAAC,CAAC;IACnD,WAAW,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,eAAe,CAAC,CAAC;IACnD,SAAS,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IACvC,aAAa,EAAE,KAAK,CAAC,aAAa,CAAC,cAAc,CAAC,CAAC;IACnD,QAAQ,CAAC,EAAE,KAAK,CAAC,kBAAkB,CAAC;IACpC,OAAO,EAAE;QACP,MAAM,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QACvC,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,MAAM,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;KACrC,CAAC;CACH,CAAC;AAEF,wBAAgB,aAAa,CAAC,EAC5B,YAAY,EACZ,UAAU,EACV,QAAQ,EACR,IAAI,EACJ,GAAG,IAAI,EACR,EAAE,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAc9C;AAED,wBAAgB,kBAAkB,CAAC,EACjC,MAAM,EACN,YAAY,EACZ,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,KAAK,EACL,IAAI,EACJ,GAAG,IAAI,EACR,EAAE,KAAK,CAAC,sBAAsB,GAAG,gBAAgB,CAyBjD"}
@@ -1 +1 @@
1
- {"version":3,"file":"event.d.ts","sourceRoot":"","sources":["../../src/utils/event.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,mBAAmB,qMAM6B,MAAM,YAAY,iJA+BzE,CAAC"}
1
+ {"version":3,"file":"event.d.ts","sourceRoot":"","sources":["../../src/utils/event.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,mBAAmB,qMAM6B,MAAM,YAAY,iJAgBzE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amityco/ts-sdk-react-native",
3
- "version": "7.0.1-91b5585.0",
3
+ "version": "7.0.2-3cbb4fc.0",
4
4
  "license": "CC-BY-ND-4.0",
5
5
  "author": "amity.co <developers@amity.co> (https://amity.co)",
6
6
  "description": "Amity Social Cloud Typescript SDK",
@@ -0,0 +1,12 @@
1
+ export {};
2
+
3
+ declare global {
4
+ namespace Amity {
5
+ type LinkPreview = {
6
+ title: string | null;
7
+ description: string | null;
8
+ image: string | null;
9
+ video: string | null;
10
+ };
11
+ }
12
+ }
@@ -81,8 +81,6 @@ declare global {
81
81
  getMarkerSyncConsistentMode: () => boolean;
82
82
 
83
83
  prefixDeviceIdKey?: string;
84
-
85
- objectSyncMap: Map<string, string | undefined>;
86
84
  };
87
85
 
88
86
  type Device = {
@@ -88,8 +88,6 @@ export const createClient = (
88
88
 
89
89
  const isUnreadCountEnabled = false;
90
90
 
91
- const objectSyncMap = new Map<string, string>();
92
-
93
91
  const client = {
94
92
  version: `${VERSION}`,
95
93
  apiKey,
@@ -135,30 +133,6 @@ export const createClient = (
135
133
  * This is allow user to have multiple SDK client and Mqtt client within the same app.
136
134
  */
137
135
  prefixDeviceIdKey,
138
-
139
- /*
140
- * The objectSyncMap is used to keep the map between local referenceId and server id in two ways mapping.
141
- *
142
- * For objects created locally (optimistic creation), the SDK stores them with their local ID as the cache key, which is used throughout the entire SDK session (in-memory) for syncing updates.
143
- * If the payload or response only contains the remote ID of the same object, the SDK will look up the corresponding local ID to retrieve the object through ObjectSyncMap.
144
- *
145
- * This is useful for syncing updates to the object, as the SDK can find the local ID from the server ID and update the object in the cache correctly.
146
- *
147
- * Example:
148
- * In the case of message optimistically creation. The referenceId is created locally.
149
- *
150
- * 1. When creating a message optimistically, a local referenceId is created. The objectSyncMap will have the following structure.
151
- * {
152
- * "LOCAL_uuid": undefined,
153
- * }
154
- *
155
- * 2. After the message was created on the server and SDK received the response from server, the objectSyncMap will be updated to
156
- * {
157
- * "LOCAL_uuid": "server_message_id",
158
- * "server_message_id": "LOCAL_uuid"
159
- * }
160
- */
161
- objectSyncMap,
162
136
  };
163
137
 
164
138
  try {
@@ -0,0 +1,30 @@
1
+ import { getActiveClient } from './activeClient';
2
+
3
+ /**
4
+ * ```js
5
+ * import { fetchLinkPreview } from '@amityco/ts-sdk'
6
+ * const { title, description, imageUrl } = fetchLinkPreview('https://www.example.com/')
7
+ * ```
8
+ *
9
+ *
10
+ * @param url the url to fetch link preview
11
+ * @returns A {@link Amity.LinkPreview} instance
12
+ *
13
+ * @category Client API
14
+ * */
15
+
16
+ export const fetchLinkPreview = async (url: string): Promise<Amity.LinkPreview> => {
17
+ const client = getActiveClient();
18
+
19
+ let fetchUrl = url;
20
+
21
+ if (!/^https?:\/\//i.test(url)) {
22
+ fetchUrl = `https://${url}`;
23
+ }
24
+
25
+ const { data } = await client.http.get<Amity.LinkPreview>(
26
+ `/api/v1/link-preview?url=${encodeURIComponent(fetchUrl)}`,
27
+ );
28
+
29
+ return data;
30
+ };
@@ -23,3 +23,4 @@ export * from './registerPushNotification';
23
23
  export * from './unregisterPushNotification';
24
24
 
25
25
  export * from './onRTEConnectionStateChange';
26
+ export * from './fetchLinkPreview';
@@ -1,6 +1,5 @@
1
1
  export * from './idResolvers';
2
2
  export * from './identifyModel';
3
- export * from './localReferenceId';
4
3
 
5
4
  /**
6
5
  * A map of v3 response keys to a store name.
@@ -30,7 +30,6 @@ const createMessageOptimistic = <T extends Amity.MessageContentType>(
30
30
  bundle: createMessageParam<T>,
31
31
  ): Amity.InternalMessage | undefined => {
32
32
  const client = getActiveClient();
33
- const { objectSyncMap } = client;
34
33
 
35
34
  if (!client.cache) return;
36
35
 
@@ -90,8 +89,6 @@ const createMessageOptimistic = <T extends Amity.MessageContentType>(
90
89
 
91
90
  pushToCache(['message', 'get', message.messageId], message, { cachedAt });
92
91
 
93
- objectSyncMap.set(message.messageId, undefined);
94
-
95
92
  fireEvent('local.message.created', { messages: [message] });
96
93
 
97
94
  return message;
@@ -123,7 +120,6 @@ export const createMessage = async <T extends Amity.MessageContentType>(
123
120
  ): Promise<Amity.Cached<Amity.Message>> => {
124
121
  const client = getActiveClient();
125
122
 
126
- const { objectSyncMap } = client;
127
123
  client.log('message/createMessage', bundle);
128
124
 
129
125
  const optimisticData = createMessageOptimistic<T>(bundle);
@@ -137,9 +133,6 @@ export const createMessage = async <T extends Amity.MessageContentType>(
137
133
  referenceId,
138
134
  });
139
135
 
140
- objectSyncMap.set(referenceId, payload.messages[0].messageId);
141
- objectSyncMap.set(payload.messages[0].messageId, referenceId);
142
-
143
136
  const data = await prepareMessagePayload(payload);
144
137
  const { messages } = data;
145
138
 
@@ -4,7 +4,7 @@ import { pullFromCache, pushToCache } from '~/cache/api';
4
4
  import { ingestInCache } from '~/cache/api/ingestInCache';
5
5
  import { getActiveClient } from '~/client';
6
6
  import { MessagePaginationController } from './MessagePaginationController';
7
- import { getMessageIdentifierIds } from '~/messageRepository/utils';
7
+ import { getResolver } from '~/core/model';
8
8
 
9
9
  export class MessageQueryStreamController extends QueryStreamController<
10
10
  Amity.MessagePayload,
@@ -50,7 +50,7 @@ export class MessageQueryStreamController extends QueryStreamController<
50
50
  ) {
51
51
  if (refresh) {
52
52
  pushToCache(this.cacheKey, {
53
- data: getMessageIdentifierIds(response.messages),
53
+ data: response.messages.map(getResolver('message')),
54
54
  query: this.query,
55
55
  });
56
56
  } else {
@@ -62,8 +62,8 @@ export class MessageQueryStreamController extends QueryStreamController<
62
62
  ...collection,
63
63
  data:
64
64
  direction === 'next'
65
- ? [...new Set([...messages, ...getMessageIdentifierIds(response.messages)])]
66
- : [...new Set([...getMessageIdentifierIds(response.messages), ...messages])],
65
+ ? [...new Set([...messages, ...response.messages.map(getResolver('message'))])]
66
+ : [...new Set([...response.messages.map(getResolver('message')), ...messages])],
67
67
  });
68
68
  }
69
69
  }
@@ -1,14 +1,10 @@
1
1
  import { pullFromCache } from '~/cache/api/pullFromCache';
2
- import { getActiveClient } from '~/client/api/activeClient';
3
- import { isLocalId } from '~/core/model';
2
+ import { queryCache } from '~/cache/api/queryCache';
4
3
 
5
4
  export const getMessageFromMainDB = (messageId: string): Amity.InternalMessage | undefined => {
6
- const client = getActiveClient();
7
- const { objectSyncMap } = client;
5
+ const message = pullFromCache<Amity.InternalMessage>(['message', 'get', messageId])?.data;
6
+ if (message) return message;
8
7
 
9
- const resolvedMessageId = isLocalId(messageId)
10
- ? messageId
11
- : objectSyncMap.get(messageId) ?? messageId;
12
-
13
- return pullFromCache<Amity.InternalMessage>(['message', 'get', resolvedMessageId])?.data;
8
+ const messages = queryCache<Amity.InternalMessage>(['message', 'get']);
9
+ return messages?.find(({ data }) => data.messageId === messageId)?.data;
14
10
  };
@@ -1,2 +1 @@
1
1
  export * from './prepareMessagePayload';
2
- export * from './getMessageIdentifierIds';
@@ -1,4 +1,4 @@
1
- import { pullFromCache } from '~/cache/api';
1
+ import { pullFromCache, queryCache } from '~/cache/api';
2
2
  import { ingestInCache } from '~/cache/api/ingestInCache';
3
3
  import { getMessageMarkers } from '~/marker/api/getMessageMarkers';
4
4
  import { prepareMessagePayloadForCache } from '~/reactionRepository/utils/prepareMessagePayloadForCache';
@@ -8,27 +8,22 @@ import { MessageContentType } from '~/@types';
8
8
  import { convertFromRaw as convertSubChannelFromRaw } from '~/subChannelRepository/utils/convertSubChannelFromRaw';
9
9
  import { updateSubChannelCache } from '~/subChannelRepository/utils/updateSubChannelCache';
10
10
  import { COLLECTION_DEFAULT_PAGINATION_LIMIT } from '~/utils/constants';
11
- import { getActiveClient } from '~/client/api/activeClient';
12
- import { isLocalId } from '~/core/model';
13
11
 
14
- const addLocalReferenceId = (payload: Amity.RawMessage) => {
15
- const client = getActiveClient();
16
- const { objectSyncMap } = client;
12
+ const mergePayloadWithLocal = (payload: Amity.RawMessage) => {
13
+ const localMessage = queryCache<Amity.InternalMessage>(['message', 'get'])?.find(
14
+ ({ data }) => data.messageId === payload.messageId,
15
+ )?.data;
17
16
 
18
- return {
19
- ...payload,
20
- /* NOTE: This logic is used to get local referenceId for each message.
21
- *
22
- * if messageId is a local reference id, use it as referenceId else get referenceId from objectSyncMap.
23
- * if find referenceId in objectSyncMap, this means this message is a local created message. The referenceId will be local reference id.
24
- * if cannot find referenceId in objectSyncMap, referenceId will be undefined.
25
- *
26
- * The referenceId is undefined means this message is not a local created message (optimistic creation message).
27
- */
28
- referenceId: isLocalId(payload.messageId)
29
- ? payload.messageId
30
- : objectSyncMap.get(payload.messageId),
31
- };
17
+ if (localMessage) {
18
+ return {
19
+ ...localMessage,
20
+ ...payload,
21
+ // NOTE: referenceId is missing in the some payload event. If we have local message data with referenceId, use it instead.
22
+ referenceId: localMessage.referenceId ?? payload.referenceId,
23
+ };
24
+ }
25
+
26
+ return payload;
32
27
  };
33
28
 
34
29
  export function convertFromRaw(
@@ -36,7 +31,7 @@ export function convertFromRaw(
36
31
  reactors?: Amity.InternalReactor[],
37
32
  event?: keyof Amity.MqttMessageEvents,
38
33
  ): Amity.InternalMessage {
39
- const messageWithReferenceId = addLocalReferenceId(message);
34
+ const mergeMessage = mergePayloadWithLocal(message);
40
35
 
41
36
  const {
42
37
  channelPublicId,
@@ -52,7 +47,7 @@ export function convertFromRaw(
52
47
  messageId,
53
48
  creatorId,
54
49
  ...rest
55
- } = messageWithReferenceId;
50
+ } = mergeMessage;
56
51
 
57
52
  let cache: Amity.CacheEntry<Amity.Message> | undefined;
58
53
 
@@ -1,6 +1,4 @@
1
1
  import { pullFromCache } from '~/cache/api/pullFromCache';
2
- import { getActiveClient } from '~/client/api/activeClient';
3
- import { isLocalId } from '~/core/model';
4
2
 
5
3
  export const convertEventPayload =
6
4
  <
@@ -18,23 +16,8 @@ export const convertEventPayload =
18
16
  return sourceModel;
19
17
  }
20
18
 
21
- const client = getActiveClient();
22
- const { objectSyncMap } = client;
23
-
24
- /*
25
- * NOTE: For objects created locally (optimistic creation), the SDK stores them with their local ID as the cache key, which is used throughout the entire SDK session (in-memory) for syncing updates.
26
- * If the payload or response only contains the remote ID of the same object, the SDK will look up the corresponding local ID to retrieve the object.
27
- */
28
- const resolvedId = isLocalId(sourceModel[sourceModelProp] as unknown as string)
29
- ? sourceModel[sourceModelProp]
30
- : objectSyncMap.get(sourceModel[sourceModelProp] as unknown as string) ??
31
- sourceModel[sourceModelProp];
32
-
33
- const model = pullFromCache<DestinationModel>([
34
- destinationDomain,
35
- 'get',
36
- `${resolvedId}`,
37
- ])?.data;
19
+ const cacheKey = [destinationDomain, 'get', `${sourceModel[sourceModelProp]}`];
20
+ const model = pullFromCache<DestinationModel>(cacheKey)?.data;
38
21
 
39
22
  if (!model) return;
40
23
 
@@ -1,2 +0,0 @@
1
- export declare const isLocalId: (id: string) => boolean;
2
- //# sourceMappingURL=localReferenceId.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"localReferenceId.d.ts","sourceRoot":"","sources":["../../../src/core/model/localReferenceId.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,SAAS,OAAQ,MAAM,YAEnC,CAAC"}
@@ -1,2 +0,0 @@
1
- export declare function getMessageIdentifierIds(messages: Amity.RawMessage[]): string[];
2
- //# sourceMappingURL=getMessageIdentifierIds.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getMessageIdentifierIds.d.ts","sourceRoot":"","sources":["../../../src/messageRepository/utils/getMessageIdentifierIds.ts"],"names":[],"mappings":"AAoBA,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,KAAK,CAAC,UAAU,EAAE,YAgBnE"}
@@ -1,12 +0,0 @@
1
- /*
2
- * To check if an id is a local referenceId, the id must start with 'LOCAL_'.
3
- *
4
- * Example: LOCAL_155f5158-281a-4a9d-a445-9243138d2041
5
- *
6
- * @param id as string
7
- * @returns true if the id is a local referenceId, false otherwise
8
- */
9
-
10
- export const isLocalId = (id: string) => {
11
- return id.startsWith('LOCAL_');
12
- };
@@ -1,37 +0,0 @@
1
- import { getActiveClient } from '~/client/api/activeClient';
2
- import { isLocalId } from '~/core/model';
3
-
4
- /*
5
- * To get message identifier ids used as a cache key for the each message.
6
- * These ids are used to store in the query stream and use to get messages from cache.
7
- *
8
- * The query stream data keeps the message identifier ids as follows:
9
- * ['server_message_id_1', 'server_message_id_2', 'LOCAL_message_id_1', 'LOCAL_message_id_2']
10
- *
11
- * Example use case: Message created locally and then created on the server.
12
- *
13
- * 1. Message created locally has `LOCAL_message_id_2` as referenceId.
14
- * 2. The `message.created` event will be fired from server with message payload that has no the referenceId.
15
- * 3. SDK use this function to find indentifer id for each message in the payload. If the message is created locally, the identifier id will be the local reference id.
16
- * 4. The SDK use this identifier ids to check if the message is already in the query stream before appending it.
17
- *
18
- * @param The raw message payload from server response
19
- * @returns The identifier ids of the messages
20
- */
21
- export function getMessageIdentifierIds(messages: Amity.RawMessage[]) {
22
- const client = getActiveClient();
23
- const { objectSyncMap } = client;
24
-
25
- return messages
26
- .map(message => {
27
- /* NOTE: This logic is used to get identifier id for each message.
28
- *
29
- * if messageId is a local id, use it as identifier id else get identifier id from objectSyncMap.
30
- * if find referenceId in objectSyncMap, this means this message is a local created message. The identifier id will be local message id.
31
- * if cannot find identifier id in objectSyncMap, this means this message is not a local created message. The identifier id will be server message id.
32
- */
33
- if (isLocalId(message.messageId)) return message.messageId;
34
- return objectSyncMap.get(message.messageId) ?? message.messageId;
35
- })
36
- .filter(Boolean);
37
- }