@amityco/ts-sdk 0.0.1-beta.26 → 0.0.1-beta.27
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/@types/core/events.d.ts +7 -7
- package/dist/@types/core/events.d.ts.map +1 -1
- package/dist/@types/core/payload.d.ts +3 -5
- package/dist/@types/core/payload.d.ts.map +1 -1
- package/dist/@types/domains/follow.d.ts +2 -1
- package/dist/@types/domains/follow.d.ts.map +1 -1
- package/dist/@types/domains/reaction.d.ts +10 -0
- package/dist/@types/domains/reaction.d.ts.map +1 -1
- package/dist/channel/api/banChannelMembers.d.ts +16 -0
- package/dist/channel/api/banChannelMembers.d.ts.map +1 -0
- package/dist/channel/api/index.d.ts +2 -0
- package/dist/channel/api/index.d.ts.map +1 -1
- package/dist/channel/api/unbanChannelMembers.d.ts +16 -0
- package/dist/channel/api/unbanChannelMembers.d.ts.map +1 -0
- package/dist/comment/observers/liveComments.d.ts.map +1 -1
- package/dist/follow/api/follow.d.ts.map +1 -1
- package/dist/follow/api/getFollowInfo.d.ts.map +1 -1
- package/dist/follow/api/queryFollowers.d.ts +4 -4
- package/dist/follow/api/queryFollowers.d.ts.map +1 -1
- package/dist/follow/api/queryFollowings.d.ts +4 -4
- package/dist/follow/api/queryFollowings.d.ts.map +1 -1
- package/dist/follow/api/utils.d.ts +4 -4
- package/dist/follow/api/utils.d.ts.map +1 -1
- package/dist/follow/events/utils.d.ts.map +1 -1
- package/dist/index.cjs.js +345 -19
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +340 -20
- package/dist/index.umd.js +4 -4
- package/dist/reaction/api/queryReactions.d.ts +1 -6
- package/dist/reaction/api/queryReactions.d.ts.map +1 -1
- package/dist/reaction/events/index.d.ts +3 -0
- package/dist/reaction/events/index.d.ts.map +1 -1
- package/dist/reaction/events/onReactorAdded.d.ts +19 -0
- package/dist/reaction/events/onReactorAdded.d.ts.map +1 -0
- package/dist/reaction/events/onReactorRemoved.d.ts +19 -0
- package/dist/reaction/events/onReactorRemoved.d.ts.map +1 -0
- package/dist/reaction/observers/index.d.ts +2 -0
- package/dist/reaction/observers/index.d.ts.map +1 -0
- package/dist/reaction/observers/liveReactions.d.ts +22 -0
- package/dist/reaction/observers/liveReactions.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/@types/core/events.ts +7 -7
- package/src/@types/core/payload.ts +5 -6
- package/src/@types/domains/follow.ts +3 -1
- package/src/@types/domains/reaction.ts +17 -0
- package/src/channel/api/banChannelMembers.ts +41 -0
- package/src/channel/api/index.ts +3 -0
- package/src/channel/api/unbanChannelMembers.ts +41 -0
- package/src/comment/observers/liveComments.ts +4 -1
- package/src/follow/api/acceptFollower.ts +1 -1
- package/src/follow/api/declineFollower.ts +1 -1
- package/src/follow/api/follow.ts +1 -4
- package/src/follow/api/getFollowInfo.ts +8 -5
- package/src/follow/api/queryFollowers.ts +5 -4
- package/src/follow/api/queryFollowings.ts +5 -4
- package/src/follow/api/unfollow.ts +1 -1
- package/src/follow/api/utils.ts +10 -10
- package/src/follow/events/utils.ts +9 -6
- package/src/index.ts +1 -0
- package/src/reaction/api/queryReactions.ts +6 -6
- package/src/reaction/events/index.ts +4 -0
- package/src/reaction/events/onReactorAdded.ts +80 -0
- package/src/reaction/events/onReactorRemoved.ts +85 -0
- package/src/reaction/observers/index.ts +1 -0
- package/src/reaction/observers/liveReactions.ts +142 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { getActiveClient } from '~/client/api';
|
|
2
|
+
import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
3
|
+
import { createEventSubscriber } from '~/core/events';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* ```js
|
|
7
|
+
* import { onReactorAdded } from '@amityco/ts-sdk'
|
|
8
|
+
* const dispose = onReactorAdded('post', postId, reactor => {
|
|
9
|
+
* // ...
|
|
10
|
+
* })
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* Fired when an {@link Amity.Reactor} has been added
|
|
14
|
+
*
|
|
15
|
+
* @param {@link Amity.ReactableType} referenceType
|
|
16
|
+
* @param {string} referenceId
|
|
17
|
+
* @param callback The function to call when the event was fired
|
|
18
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
19
|
+
*
|
|
20
|
+
* @category Events
|
|
21
|
+
* */
|
|
22
|
+
export const onReactorAdded = (
|
|
23
|
+
referenceType: Amity.ReactableType,
|
|
24
|
+
referenceId: Amity.Reaction['referenceId'],
|
|
25
|
+
callback: Amity.Listener<Amity.Reactor>,
|
|
26
|
+
): Amity.Unsubscriber => {
|
|
27
|
+
const client = getActiveClient();
|
|
28
|
+
|
|
29
|
+
const callbackWrapper = (
|
|
30
|
+
referenceType_: Amity.ReactableType,
|
|
31
|
+
referenceId_: Amity.Reaction['referenceId'],
|
|
32
|
+
reaction: Amity.Reactor,
|
|
33
|
+
) => {
|
|
34
|
+
if (referenceType_ === referenceType && referenceId_ === referenceId) {
|
|
35
|
+
callback(reaction);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
if (referenceType === 'message') {
|
|
40
|
+
const filter = (payload: Amity.MessagePayload) => {
|
|
41
|
+
if (!payload.messages[0].latestReaction) return;
|
|
42
|
+
|
|
43
|
+
const { eventName, referenceId, referenceType, userDisplayName, ...reactor } =
|
|
44
|
+
payload.messages[0].latestReaction;
|
|
45
|
+
|
|
46
|
+
if (eventName !== 'add') return;
|
|
47
|
+
|
|
48
|
+
ingestInCache(payload);
|
|
49
|
+
ingestInCache({ reactions: [reactor as Amity.Reactor] });
|
|
50
|
+
|
|
51
|
+
callbackWrapper('message', payload.messages[0].messageId, reactor);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
return createEventSubscriber(client, 'reaction/onReactorAdded', 'v3.message.didUpdate', filter);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (referenceType === 'post') {
|
|
58
|
+
const filter = (payload: Amity.PostPayload & { reactor: Amity.Reactor }) => {
|
|
59
|
+
const { reactor, ...rest } = payload;
|
|
60
|
+
|
|
61
|
+
ingestInCache(rest as Amity.PostPayload);
|
|
62
|
+
ingestInCache({ reactions: [reactor] });
|
|
63
|
+
|
|
64
|
+
callbackWrapper('post', payload.posts[0].postId, reactor);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return createEventSubscriber(client, 'post.addReaction', 'post.addReaction', filter);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const filter = (payload: Amity.CommentPayload & { reactor: Amity.Reactor }) => {
|
|
71
|
+
const { reactor, ...rest } = payload;
|
|
72
|
+
|
|
73
|
+
ingestInCache(rest as Amity.CommentPayload);
|
|
74
|
+
ingestInCache({ reactions: [reactor] });
|
|
75
|
+
|
|
76
|
+
callbackWrapper('comment', payload.comments[0].commentId, reactor);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
return createEventSubscriber(client, 'comment.addReaction', 'comment.addReaction', filter);
|
|
80
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { getActiveClient } from '~/client/api';
|
|
2
|
+
import { ingestInCache } from '~/cache/api/ingestInCache';
|
|
3
|
+
import { createEventSubscriber } from '~/core/events';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* ```js
|
|
7
|
+
* import { onReactorRemoved } from '@amityco/ts-sdk'
|
|
8
|
+
* const dispose = onReactorRemoved('post', postId, reactor => {
|
|
9
|
+
* // ...
|
|
10
|
+
* })
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* Fired when an {@link Amity.Reactor} has been removed
|
|
14
|
+
*
|
|
15
|
+
* @param {@link Amity.ReactableType} referenceType
|
|
16
|
+
* @param {string} referenceId
|
|
17
|
+
* @param callback The function to call when the event was fired
|
|
18
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
19
|
+
*
|
|
20
|
+
* @category Events
|
|
21
|
+
* */
|
|
22
|
+
export const onReactorRemoved = (
|
|
23
|
+
referenceType: Amity.ReactableType,
|
|
24
|
+
referenceId: Amity.Reaction['referenceId'],
|
|
25
|
+
callback: Amity.Listener<Amity.Reactor>,
|
|
26
|
+
): Amity.Unsubscriber => {
|
|
27
|
+
const client = getActiveClient();
|
|
28
|
+
|
|
29
|
+
const callbackWrapper = (
|
|
30
|
+
referenceType_: Amity.ReactableType,
|
|
31
|
+
referenceId_: Amity.Reaction['referenceId'],
|
|
32
|
+
reaction: Amity.Reactor,
|
|
33
|
+
) => {
|
|
34
|
+
if (referenceType_ === referenceType && referenceId_ === referenceId) {
|
|
35
|
+
callback(reaction);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
if (referenceType === 'message') {
|
|
40
|
+
const filter = (payload: Amity.MessagePayload) => {
|
|
41
|
+
if (!payload.messages[0].latestReaction) return;
|
|
42
|
+
|
|
43
|
+
const { eventName, referenceId, referenceType, ...reactor } =
|
|
44
|
+
payload.messages[0].latestReaction;
|
|
45
|
+
|
|
46
|
+
if (eventName !== 'remove') return;
|
|
47
|
+
|
|
48
|
+
ingestInCache(payload);
|
|
49
|
+
ingestInCache({ reactions: [reactor as Amity.Reactor] });
|
|
50
|
+
|
|
51
|
+
callbackWrapper('message', payload.messages[0].messageId, reactor);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
return createEventSubscriber(
|
|
55
|
+
client,
|
|
56
|
+
'reaction/onReactorRemoved',
|
|
57
|
+
'v3.message.didUpdate',
|
|
58
|
+
filter,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (referenceType === 'post') {
|
|
63
|
+
const filter = (payload: Amity.PostPayload & { reactor: Amity.Reactor }) => {
|
|
64
|
+
const { reactor, ...rest } = payload;
|
|
65
|
+
|
|
66
|
+
ingestInCache(rest as Amity.PostPayload);
|
|
67
|
+
ingestInCache({ reactions: [reactor] });
|
|
68
|
+
|
|
69
|
+
callbackWrapper('post', payload.posts[0].postId, reactor);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
return createEventSubscriber(client, 'post.removeReaction', 'post.removeReaction', filter);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const filter = (payload: Amity.CommentPayload & { reactor: Amity.Reactor }) => {
|
|
76
|
+
const { reactor, ...rest } = payload;
|
|
77
|
+
|
|
78
|
+
ingestInCache(rest as Amity.CommentPayload);
|
|
79
|
+
ingestInCache({ reactions: [reactor] });
|
|
80
|
+
|
|
81
|
+
callbackWrapper('comment', payload.comments[0].commentId, reactor);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
return createEventSubscriber(client, 'comment.removeReaction', 'comment.removeReaction', filter);
|
|
85
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './liveReactions';
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/* eslint-disable no-use-before-define */
|
|
2
|
+
import { getResolver } from '~/core/model';
|
|
3
|
+
import { getActiveClient } from '~/client/api';
|
|
4
|
+
import { pushToCache, pullFromCache } from '~/cache/api';
|
|
5
|
+
import { createQuery, runQuery, queryOptions } from '~/core/query';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
COLLECTION_DEFAULT_CACHING_POLICY,
|
|
9
|
+
COLLECTION_DEFAULT_PAGINATION_LIMIT,
|
|
10
|
+
} from '~/utils/constants';
|
|
11
|
+
|
|
12
|
+
import { onReactorAdded, onReactorRemoved } from '../events';
|
|
13
|
+
import { queryReactions } from '../api';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* ```js
|
|
17
|
+
* import { liveReactions } from '@amityco/ts-sdk'
|
|
18
|
+
*
|
|
19
|
+
* let reactions = []
|
|
20
|
+
* const unsub = liveReactions({
|
|
21
|
+
* referenceId: Amity.Reaction['referenceId'],
|
|
22
|
+
* referenceType: Amity.Reaction['referenceType'],
|
|
23
|
+
* }, response => merge(reactions, response.data))
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* Observe all mutations on a list of {@link Amity.Reaction} for a given target object
|
|
27
|
+
*
|
|
28
|
+
* @param referenceType the type of the target
|
|
29
|
+
* @param referenceId the ID of the target
|
|
30
|
+
* @param callback the function to call when new data are available
|
|
31
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the messages
|
|
32
|
+
*
|
|
33
|
+
* @category Reactions Live Collection
|
|
34
|
+
*/
|
|
35
|
+
export const liveReactions = (
|
|
36
|
+
params: Amity.ReactionLiveCollection,
|
|
37
|
+
callback: Amity.LiveCollectionCallback<Amity.Reactor>,
|
|
38
|
+
config?: Amity.LiveCollectionConfig,
|
|
39
|
+
): Amity.Unsubscriber => {
|
|
40
|
+
const { log, cache } = getActiveClient();
|
|
41
|
+
|
|
42
|
+
if (!cache) {
|
|
43
|
+
console.log('For using Live Collection feature you need to enable Cache!');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const timestamp = Date.now();
|
|
47
|
+
log(`liveReactions(tmpid: ${timestamp}) > listen`);
|
|
48
|
+
|
|
49
|
+
const { limit: queryLimit, ...queryParams } = params;
|
|
50
|
+
|
|
51
|
+
const limit = queryLimit ?? COLLECTION_DEFAULT_PAGINATION_LIMIT;
|
|
52
|
+
const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config ?? {};
|
|
53
|
+
|
|
54
|
+
const disposers: Amity.Unsubscriber[] = [];
|
|
55
|
+
const cacheKey = [
|
|
56
|
+
'reaction',
|
|
57
|
+
'collection',
|
|
58
|
+
{ referenceId: params.referenceId, referenceType: params.referenceType },
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
const responder = (data: Amity.ReactionLiveCollectionCache) => {
|
|
62
|
+
const reactions: Amity.Reactor[] =
|
|
63
|
+
data.data
|
|
64
|
+
.map(reactorId => pullFromCache<Amity.Reactor>(['reaction', 'get', reactorId])!)
|
|
65
|
+
.filter(Boolean)
|
|
66
|
+
.map(({ data }) => data) ?? [];
|
|
67
|
+
|
|
68
|
+
callback({
|
|
69
|
+
onNextPage: onFetch,
|
|
70
|
+
data: reactions,
|
|
71
|
+
hasNextPage: !!data.params?.page,
|
|
72
|
+
loading: data.loading,
|
|
73
|
+
error: data.error,
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const realtimeRouter = (reaction: Amity.Reactor, action: Amity.ReactionActionType) => {
|
|
78
|
+
const collection = pullFromCache<Amity.ReactionLiveCollectionCache>(cacheKey)?.data;
|
|
79
|
+
if (!collection) return;
|
|
80
|
+
|
|
81
|
+
if (action === 'onAdded') {
|
|
82
|
+
collection.data = [...new Set([reaction.reactionId, ...collection.data])];
|
|
83
|
+
} else if (action === 'onRemoved') {
|
|
84
|
+
collection.data = collection.data.filter(p => p !== reaction.reactionId);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
pushToCache(cacheKey, collection);
|
|
88
|
+
responder(collection);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const onFetch = () => {
|
|
92
|
+
const collection = pullFromCache<Amity.ReactionLiveCollectionCache>(cacheKey)?.data;
|
|
93
|
+
|
|
94
|
+
const reactions = collection?.data ?? [];
|
|
95
|
+
|
|
96
|
+
if (reactions.length > 0 && !collection?.params?.page) return;
|
|
97
|
+
|
|
98
|
+
const query = createQuery(queryReactions, {
|
|
99
|
+
...queryParams,
|
|
100
|
+
page: collection?.params?.page ?? { limit },
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
runQuery(
|
|
104
|
+
query,
|
|
105
|
+
({ data: result, error, loading, nextPage: page }) => {
|
|
106
|
+
const data = {
|
|
107
|
+
loading,
|
|
108
|
+
error,
|
|
109
|
+
params: { page },
|
|
110
|
+
data: reactions,
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
if (result) {
|
|
114
|
+
data.data = [
|
|
115
|
+
...new Set([...reactions, ...result[0].reactors.map(getResolver('reaction'))]),
|
|
116
|
+
];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
pushToCache(cacheKey, data);
|
|
120
|
+
|
|
121
|
+
responder(data);
|
|
122
|
+
},
|
|
123
|
+
queryOptions(policy),
|
|
124
|
+
);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
disposers.push(
|
|
128
|
+
onReactorAdded(queryParams.referenceType, queryParams.referenceId, reaction =>
|
|
129
|
+
realtimeRouter(reaction, 'onAdded'),
|
|
130
|
+
),
|
|
131
|
+
onReactorRemoved(queryParams.referenceType, queryParams.referenceId, reaction =>
|
|
132
|
+
realtimeRouter(reaction, 'onRemoved'),
|
|
133
|
+
),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
onFetch();
|
|
137
|
+
|
|
138
|
+
return () => {
|
|
139
|
+
log(`liveReactions(tmpid: ${timestamp}) > dispose`);
|
|
140
|
+
disposers.forEach(fn => fn());
|
|
141
|
+
};
|
|
142
|
+
};
|