@amityco/ts-sdk 6.4.2 → 6.4.3-0dfdabe.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.
- package/dist/cache/api/ingestInCache.d.ts +2 -1
- package/dist/cache/api/ingestInCache.d.ts.map +1 -1
- package/dist/commentRepository/events/onCommentReactionAdded.d.ts.map +1 -1
- package/dist/commentRepository/events/onCommentReactionRemoved.d.ts.map +1 -1
- package/dist/index.cjs.js +39 -41
- package/dist/index.esm.js +39 -41
- package/dist/index.umd.js +1 -1
- package/dist/postRepository/events/onPostReactionAdded.d.ts.map +1 -1
- package/dist/postRepository/events/onPostReactionRemoved.d.ts.map +1 -1
- package/dist/reactionRepository/utils/index.d.ts +1 -1
- package/dist/reactionRepository/utils/index.d.ts.map +1 -1
- package/dist/reactionRepository/utils/prepareReactionPayloadFormEvent.d.ts +3 -0
- package/dist/reactionRepository/utils/prepareReactionPayloadFormEvent.d.ts.map +1 -0
- package/dist/utils/tests/dummy/post.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/cache/api/ingestInCache.ts +4 -1
- package/src/channelRepsitory/channelMembership/observers/tests/getMembers.test.ts +7 -1
- package/src/channelRepsitory/channelMembership/observers/tests/searchMembers.test.ts +7 -1
- package/src/commentRepository/events/onCommentReactionAdded.ts +3 -4
- package/src/commentRepository/events/onCommentReactionRemoved.ts +3 -4
- package/src/postRepository/events/onPostReactionAdded.ts +3 -4
- package/src/postRepository/events/onPostReactionRemoved.ts +3 -4
- package/src/postRepository/events/tests/onPostReactionAdded.test.ts +1 -1
- package/src/reactionRepository/utils/index.ts +1 -1
- package/src/reactionRepository/utils/prepareReactionPayloadFormEvent.ts +62 -0
- package/src/utils/tests/dummy/post.ts +1 -0
- package/dist/reactionRepository/utils/preparePayloadForCache.d.ts +0 -3
- package/dist/reactionRepository/utils/preparePayloadForCache.d.ts.map +0 -1
- package/src/reactionRepository/utils/preparePayloadForCache.ts +0 -53
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"onPostReactionAdded.d.ts","sourceRoot":"","sources":["../../../src/postRepository/events/onPostReactionAdded.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,mBAAmB,aAAc,MAAM,QAAQ,CAAC,MAAM,IAAI,CAAC,KAAG,MAAM,
|
|
1
|
+
{"version":3,"file":"onPostReactionAdded.d.ts","sourceRoot":"","sources":["../../../src/postRepository/events/onPostReactionAdded.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,mBAAmB,aAAc,MAAM,QAAQ,CAAC,MAAM,IAAI,CAAC,KAAG,MAAM,YAmBhF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"onPostReactionRemoved.d.ts","sourceRoot":"","sources":["../../../src/postRepository/events/onPostReactionRemoved.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,qBAAqB,aAAc,MAAM,QAAQ,CAAC,MAAM,IAAI,CAAC,KAAG,MAAM,
|
|
1
|
+
{"version":3,"file":"onPostReactionRemoved.d.ts","sourceRoot":"","sources":["../../../src/postRepository/events/onPostReactionRemoved.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,qBAAqB,aAAc,MAAM,QAAQ,CAAC,MAAM,IAAI,CAAC,KAAG,MAAM,YAmBlF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/reactionRepository/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/reactionRepository/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,mCAAmC,CAAC;AAClD,cAAc,iCAAiC,CAAC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/** @hidden */
|
|
2
|
+
export declare const prepareReactionPayloadFormEvent: <T extends "post.addReaction" | "post.removeReaction" | "comment.addReaction" | "comment.removeReaction", P extends Amity.Events[T]>(event: T, payload: P) => P;
|
|
3
|
+
//# sourceMappingURL=prepareReactionPayloadFormEvent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prepareReactionPayloadFormEvent.d.ts","sourceRoot":"","sources":["../../../src/reactionRepository/utils/prepareReactionPayloadFormEvent.ts"],"names":[],"mappings":"AA8BA,cAAc;AACd,eAAO,MAAM,+BAA+B,iKA8B3C,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"post.d.ts","sourceRoot":"","sources":["../../../../src/utils/tests/dummy/post.ts"],"names":[],"mappings":"AAIA,wBAAgB,YAAY,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,IAAI,
|
|
1
|
+
{"version":3,"file":"post.d.ts","sourceRoot":"","sources":["../../../../src/utils/tests/dummy/post.ts"],"names":[],"mappings":"AAIA,wBAAgB,YAAY,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,IAAI,CA0BrE;AAED,eAAO,MAAM,MAAM,EAAE,KAAK,CAAC,IAAqB,CAAC;AAEjD,eAAO,MAAM,MAAM,EAAE,KAAK,CAAC,IAG1B,CAAC;AAEF,eAAO,MAAM,MAAM,EAAE,KAAK,CAAC,IAG1B,CAAC;AAEF,eAAO,MAAM,MAAM,EAAE,KAAK,CAAC,IAO1B,CAAC;AAEF,eAAO,MAAM,MAAM,EAAE,KAAK,CAAC,IAQ1B,CAAC;AAEF,eAAO,MAAM,MAAM,EAAE,KAAK,CAAC,IAI1B,CAAC;AAEF,eAAO,MAAM,KAAK;;;;;;CAMjB,CAAC;AAEF,eAAO,MAAM,gBAAgB;;;;;;;;;;CAU5B,CAAC;AAIF,eAAO,MAAM,iBAAiB;;;;;CAS7B,CAAC;AAEF,eAAO,MAAM,sBAAsB;;;;;CASlC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { PAYLOAD2MODEL, getResolver } from '~/core/model';
|
|
2
2
|
|
|
3
|
+
import { pushToCache } from './pushToCache';
|
|
3
4
|
import { upsertInCache } from './upsertInCache';
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -7,6 +8,7 @@ import { upsertInCache } from './upsertInCache';
|
|
|
7
8
|
*
|
|
8
9
|
* @param payload a "backend v3" payload object
|
|
9
10
|
* @param options caching options like cachedAt or offline
|
|
11
|
+
* @param replace If TRUE it will overwrite the current cache with payload, if FALSE it will merge the payload into cache.
|
|
10
12
|
*
|
|
11
13
|
* @category Cache
|
|
12
14
|
* @hidden
|
|
@@ -14,6 +16,7 @@ import { upsertInCache } from './upsertInCache';
|
|
|
14
16
|
export const ingestInCache = (
|
|
15
17
|
payload: Record<keyof typeof PAYLOAD2MODEL, Amity.Model[]> = {},
|
|
16
18
|
options?: Amity.CacheOptions,
|
|
19
|
+
replace = true,
|
|
17
20
|
) => {
|
|
18
21
|
Object.entries(payload).forEach(([key, models]) => {
|
|
19
22
|
const type = PAYLOAD2MODEL[key];
|
|
@@ -23,7 +26,7 @@ export const ingestInCache = (
|
|
|
23
26
|
if (!resolver) return;
|
|
24
27
|
|
|
25
28
|
models.forEach(model => {
|
|
26
|
-
upsertInCache([type, 'get', resolver(model)], model, options);
|
|
29
|
+
(replace ? pushToCache : upsertInCache)([type, 'get', resolver(model)], model, options);
|
|
27
30
|
});
|
|
28
31
|
});
|
|
29
32
|
};
|
|
@@ -98,7 +98,13 @@ describe('getMembers', () => {
|
|
|
98
98
|
|
|
99
99
|
test.each(events)('%s', async (test, event, channelUser, user, expected) => {
|
|
100
100
|
const callback = jest.fn();
|
|
101
|
-
|
|
101
|
+
|
|
102
|
+
client.http.get = jest.fn().mockImplementation(url => {
|
|
103
|
+
if (url === `/api/v4/channels/${encodeURIComponent(channelId)}/users`)
|
|
104
|
+
return Promise.resolve(channelUserQueryResponse);
|
|
105
|
+
|
|
106
|
+
return undefined;
|
|
107
|
+
});
|
|
102
108
|
|
|
103
109
|
getMembers({ channelId }, callback);
|
|
104
110
|
await pause();
|
|
@@ -129,7 +129,13 @@ describe('searchMembers', () => {
|
|
|
129
129
|
|
|
130
130
|
test.each(events)('%s', async (test, event, channelUser, user, expected) => {
|
|
131
131
|
const callback = jest.fn();
|
|
132
|
-
|
|
132
|
+
|
|
133
|
+
client.http.get = jest.fn().mockImplementation(url => {
|
|
134
|
+
if (url === `/api/v4/channels/${encodeURIComponent(channelId)}/users`)
|
|
135
|
+
return Promise.resolve(channelUserQueryResponse);
|
|
136
|
+
|
|
137
|
+
return undefined;
|
|
138
|
+
});
|
|
133
139
|
|
|
134
140
|
searchMembers({ channelId }, callback);
|
|
135
141
|
await pause();
|
|
@@ -2,7 +2,7 @@ import { getActiveClient } from '~/client/api';
|
|
|
2
2
|
import { pullFromCache } from '~/cache/api';
|
|
3
3
|
import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
4
4
|
import { createEventSubscriber } from '~/core/events';
|
|
5
|
-
import {
|
|
5
|
+
import { prepareReactionPayloadFormEvent } from '~/reactionRepository/utils';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* ```js
|
|
@@ -28,9 +28,8 @@ export const onCommentReactionAdded = (
|
|
|
28
28
|
if (!client.cache) {
|
|
29
29
|
callback(payload.comments[0]);
|
|
30
30
|
} else {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const { reactor, ...commentPayload } = payload;
|
|
31
|
+
const processed = prepareReactionPayloadFormEvent('comment.addReaction', payload);
|
|
32
|
+
const { reactor, ...commentPayload } = processed;
|
|
34
33
|
|
|
35
34
|
ingestInCache(commentPayload as Amity.CommentPayload);
|
|
36
35
|
|
|
@@ -2,7 +2,7 @@ import { getActiveClient } from '~/client/api';
|
|
|
2
2
|
import { pullFromCache } from '~/cache/api';
|
|
3
3
|
import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
4
4
|
import { createEventSubscriber } from '~/core/events';
|
|
5
|
-
import {
|
|
5
|
+
import { prepareReactionPayloadFormEvent } from '~/reactionRepository/utils';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* ```js
|
|
@@ -28,9 +28,8 @@ export const onCommentReactionRemoved = (
|
|
|
28
28
|
if (!client.cache) {
|
|
29
29
|
callback(payload.comments[0]);
|
|
30
30
|
} else {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const { reactor, ...commentPayload } = payload;
|
|
31
|
+
const processed = prepareReactionPayloadFormEvent('comment.removeReaction', payload);
|
|
32
|
+
const { reactor, ...commentPayload } = processed;
|
|
34
33
|
|
|
35
34
|
ingestInCache(commentPayload as Amity.CommentPayload);
|
|
36
35
|
|
|
@@ -2,7 +2,7 @@ import { getActiveClient } from '~/client/api';
|
|
|
2
2
|
import { pullFromCache } from '~/cache/api';
|
|
3
3
|
import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
4
4
|
import { createEventSubscriber } from '~/core/events';
|
|
5
|
-
import {
|
|
5
|
+
import { prepareReactionPayloadFormEvent } from '~/reactionRepository/utils';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* ```js
|
|
@@ -26,9 +26,8 @@ export const onPostReactionAdded = (callback: Amity.Listener<Amity.Post>): Amity
|
|
|
26
26
|
if (!client.cache) {
|
|
27
27
|
callback(payload.posts[0]);
|
|
28
28
|
} else {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const { reactor, ...postPayload } = payload;
|
|
29
|
+
const processed = prepareReactionPayloadFormEvent('post.addReaction', payload);
|
|
30
|
+
const { reactor, ...postPayload } = processed;
|
|
32
31
|
|
|
33
32
|
ingestInCache(postPayload as Amity.ProcessedPostPayload);
|
|
34
33
|
|
|
@@ -2,7 +2,7 @@ import { getActiveClient } from '~/client/api';
|
|
|
2
2
|
import { pullFromCache } from '~/cache/api';
|
|
3
3
|
import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
4
4
|
import { createEventSubscriber } from '~/core/events';
|
|
5
|
-
import {
|
|
5
|
+
import { prepareReactionPayloadFormEvent } from '~/reactionRepository/utils';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* ```js
|
|
@@ -26,9 +26,8 @@ export const onPostReactionRemoved = (callback: Amity.Listener<Amity.Post>): Ami
|
|
|
26
26
|
if (!client.cache) {
|
|
27
27
|
callback(payload.posts[0]);
|
|
28
28
|
} else {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const { reactor, ...postPayload } = payload;
|
|
29
|
+
const processed = prepareReactionPayloadFormEvent('post.removeReaction', payload);
|
|
30
|
+
const { reactor, ...postPayload } = processed;
|
|
32
31
|
|
|
33
32
|
ingestInCache(postPayload as Amity.ProcessedPostPayload);
|
|
34
33
|
|
|
@@ -25,7 +25,7 @@ describe('onPostReactionAdded', () => {
|
|
|
25
25
|
unsub();
|
|
26
26
|
|
|
27
27
|
expect(callback).toHaveBeenCalled();
|
|
28
|
-
expect(callback).toHaveBeenCalledWith(post11);
|
|
28
|
+
expect(callback).toHaveBeenCalledWith({ ...post11, myReactions: [reaction11.reactionName] });
|
|
29
29
|
});
|
|
30
30
|
|
|
31
31
|
test('it should got nothing if we did unsubscribe before got event', () => {
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { pullFromCache } from '~/cache/api';
|
|
2
|
+
import { getActiveClient } from '~/client/api';
|
|
3
|
+
|
|
4
|
+
const getMyReactionsInCache = <T extends Amity.PostPayload | Amity.CommentPayload>(
|
|
5
|
+
payload: T,
|
|
6
|
+
eventPrefix: string,
|
|
7
|
+
) => {
|
|
8
|
+
let id: string;
|
|
9
|
+
|
|
10
|
+
if (eventPrefix === 'post') {
|
|
11
|
+
id = (<Amity.PostPayload>payload).posts[0].postId;
|
|
12
|
+
} else if (eventPrefix === 'comment') {
|
|
13
|
+
id = (<Amity.PostPayload>payload).comments[0].commentId;
|
|
14
|
+
} else {
|
|
15
|
+
throw new Error(`Unknown event type`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return pullFromCache<Amity.Comment>(['comment', 'get', id])?.data?.myReactions || [];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const rebuildPayload = <T extends Amity.PostPayload | Amity.CommentPayload, K extends keyof T>(
|
|
22
|
+
payload: T,
|
|
23
|
+
payloadKey: K,
|
|
24
|
+
myReactions: Amity.Reactable['myReactions'],
|
|
25
|
+
) => {
|
|
26
|
+
const [model]: any = payload[payloadKey];
|
|
27
|
+
|
|
28
|
+
return { ...payload, [payloadKey]: [{ ...model, myReactions }] };
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** @hidden */
|
|
32
|
+
export const prepareReactionPayloadFormEvent = <
|
|
33
|
+
T extends
|
|
34
|
+
| 'post.addReaction'
|
|
35
|
+
| 'post.removeReaction'
|
|
36
|
+
| 'comment.addReaction'
|
|
37
|
+
| 'comment.removeReaction',
|
|
38
|
+
P extends Amity.Events[T],
|
|
39
|
+
>(
|
|
40
|
+
event: T,
|
|
41
|
+
payload: P,
|
|
42
|
+
): P => {
|
|
43
|
+
if (getActiveClient().userId !== payload.reactor.userId) {
|
|
44
|
+
return payload;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const eventPrefix = event.split('.')[0]; // 'post' | 'comment'
|
|
48
|
+
const eventSuffix = event.split('.')[1]; // 'addReaction' | 'removeReaction'
|
|
49
|
+
|
|
50
|
+
let myReactions: Amity.Reactable['myReactions'];
|
|
51
|
+
const cachedMyReactions = getMyReactionsInCache(payload, eventPrefix);
|
|
52
|
+
|
|
53
|
+
if (eventSuffix === 'addReaction') {
|
|
54
|
+
myReactions = [...cachedMyReactions, payload.reactor.reactionName];
|
|
55
|
+
} else if (eventSuffix === 'removeReaction') {
|
|
56
|
+
myReactions = cachedMyReactions.filter(reaction => reaction !== payload.reactor.reactionName);
|
|
57
|
+
} else {
|
|
58
|
+
throw new Error(`Unknown event type`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return rebuildPayload(payload, <keyof P>`${eventPrefix}s`, myReactions);
|
|
62
|
+
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"preparePayloadForCache.d.ts","sourceRoot":"","sources":["../../../src/reactionRepository/utils/preparePayloadForCache.ts"],"names":[],"mappings":"AAGA,cAAc;AACd,eAAO,MAAM,sBAAsB,uJAgDlC,CAAC"}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { pullFromCache } from '~/cache/api';
|
|
2
|
-
import { getActiveClient } from '~/client/api';
|
|
3
|
-
|
|
4
|
-
/** @hidden */
|
|
5
|
-
export const preparePayloadForCache = <
|
|
6
|
-
T extends
|
|
7
|
-
| 'post.addReaction'
|
|
8
|
-
| 'post.removeReaction'
|
|
9
|
-
| 'comment.addReaction'
|
|
10
|
-
| 'comment.removeReaction',
|
|
11
|
-
>(
|
|
12
|
-
event: T,
|
|
13
|
-
payload: Amity.Events[T],
|
|
14
|
-
) => {
|
|
15
|
-
const client = getActiveClient();
|
|
16
|
-
|
|
17
|
-
if (client.userId !== payload.reactor.userId) {
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
let model: Amity.Reactable;
|
|
22
|
-
let cached: Amity.Reactable | undefined;
|
|
23
|
-
|
|
24
|
-
if (event === 'post.addReaction' || event === 'post.removeReaction') {
|
|
25
|
-
const post = (payload as Amity.Events['post.addReaction']).posts[0];
|
|
26
|
-
cached = pullFromCache<Amity.Post>(['post', 'get', post.postId])?.data;
|
|
27
|
-
model = post;
|
|
28
|
-
} else {
|
|
29
|
-
const comment = (payload as Amity.Events['comment.addReaction']).comments[0];
|
|
30
|
-
cached = pullFromCache<Amity.Comment>(['comment', 'get', comment.commentId])?.data;
|
|
31
|
-
model = comment;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (!cached || !cached.myReactions) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (event === 'post.addReaction' || event === 'comment.addReaction') {
|
|
39
|
-
if (cached.myReactions.includes(payload.reactor.reactionName)) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
Object.assign(model, { myReactions: [...cached.myReactions, payload.reactor.reactionName] });
|
|
44
|
-
} else {
|
|
45
|
-
if (!cached.myReactions.includes(payload.reactor.reactionName)) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
Object.assign(model, {
|
|
50
|
-
myReactions: cached.myReactions.filter(x => x !== payload.reactor.reactionName),
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
};
|