@amityco/ts-sdk 0.0.1-beta.22 → 0.0.1-beta.25

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 (81) hide show
  1. package/dist/@types/core/events.d.ts +5 -1
  2. package/dist/@types/core/events.d.ts.map +1 -1
  3. package/dist/@types/domains/channel.d.ts +1 -2
  4. package/dist/@types/domains/channel.d.ts.map +1 -1
  5. package/dist/@types/domains/reaction.d.ts +1 -1
  6. package/dist/@types/domains/reaction.d.ts.map +1 -1
  7. package/dist/channel/api/queryChannelMembers.d.ts +2 -1
  8. package/dist/channel/api/queryChannelMembers.d.ts.map +1 -1
  9. package/dist/channel/api/updateChannel.d.ts +1 -1
  10. package/dist/channel/api/updateChannel.d.ts.map +1 -1
  11. package/dist/client/api/connectClient.d.ts +3 -1
  12. package/dist/client/api/connectClient.d.ts.map +1 -1
  13. package/dist/comment/api/queryComments.d.ts +1 -0
  14. package/dist/comment/api/queryComments.d.ts.map +1 -1
  15. package/dist/core/events.d.ts +3 -3
  16. package/dist/core/events.d.ts.map +1 -1
  17. package/dist/core/model/identifyModel.d.ts +1 -0
  18. package/dist/core/model/identifyModel.d.ts.map +1 -1
  19. package/dist/core/query/query.d.ts.map +1 -1
  20. package/dist/index.cjs.js +381 -67
  21. package/dist/index.d.ts +2 -0
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.esm.js +376 -68
  24. package/dist/index.umd.js +5 -5
  25. package/dist/message/api/createMessage.d.ts.map +1 -1
  26. package/dist/message/events/onMessageUpdated.d.ts.map +1 -1
  27. package/dist/poll/api/closePoll.d.ts.map +1 -1
  28. package/dist/poll/api/deletePoll.d.ts.map +1 -1
  29. package/dist/poll/api/votePoll.d.ts.map +1 -1
  30. package/dist/poll/events/index.d.ts +3 -0
  31. package/dist/poll/events/index.d.ts.map +1 -0
  32. package/dist/poll/events/onPollDeleted.d.ts +17 -0
  33. package/dist/poll/events/onPollDeleted.d.ts.map +1 -0
  34. package/dist/poll/events/onPollUpdated.d.ts +17 -0
  35. package/dist/poll/events/onPollUpdated.d.ts.map +1 -0
  36. package/dist/poll/observers/index.d.ts +2 -0
  37. package/dist/poll/observers/index.d.ts.map +1 -0
  38. package/dist/poll/observers/observePoll.d.ts +18 -0
  39. package/dist/poll/observers/observePoll.d.ts.map +1 -0
  40. package/dist/post/api/approvePost.d.ts +17 -0
  41. package/dist/post/api/approvePost.d.ts.map +1 -0
  42. package/dist/post/api/declinePost.d.ts +17 -0
  43. package/dist/post/api/declinePost.d.ts.map +1 -0
  44. package/dist/post/api/index.d.ts +2 -0
  45. package/dist/post/api/index.d.ts.map +1 -1
  46. package/dist/reaction/events/onReactionAdded.d.ts.map +1 -1
  47. package/dist/reaction/utils/index.d.ts +1 -0
  48. package/dist/reaction/utils/index.d.ts.map +1 -1
  49. package/dist/reaction/utils/prepareMessagePayloadForCache.d.ts +3 -0
  50. package/dist/reaction/utils/prepareMessagePayloadForCache.d.ts.map +1 -0
  51. package/dist/report/api/createReport.d.ts.map +1 -1
  52. package/dist/report/api/deleteReport.d.ts.map +1 -1
  53. package/package.json +1 -1
  54. package/src/@types/core/events.ts +6 -1
  55. package/src/@types/domains/channel.ts +1 -2
  56. package/src/@types/domains/reaction.ts +1 -1
  57. package/src/channel/api/queryChannelMembers.ts +4 -4
  58. package/src/channel/api/updateChannel.ts +1 -1
  59. package/src/client/api/connectClient.ts +12 -7
  60. package/src/comment/api/queryComments.ts +6 -3
  61. package/src/core/model/identifyModel.ts +18 -0
  62. package/src/core/query/query.ts +19 -1
  63. package/src/index.ts +2 -0
  64. package/src/message/api/createMessage.ts +22 -6
  65. package/src/message/events/onMessageUpdated.ts +9 -1
  66. package/src/poll/api/closePoll.ts +3 -0
  67. package/src/poll/api/deletePoll.ts +3 -0
  68. package/src/poll/api/votePoll.ts +3 -0
  69. package/src/poll/events/index.ts +2 -0
  70. package/src/poll/events/onPollDeleted.ts +31 -0
  71. package/src/poll/events/onPollUpdated.ts +31 -0
  72. package/src/poll/observers/index.ts +1 -0
  73. package/src/poll/observers/observePoll.ts +67 -0
  74. package/src/post/api/approvePost.ts +48 -0
  75. package/src/post/api/declinePost.ts +48 -0
  76. package/src/post/api/index.ts +3 -0
  77. package/src/reaction/events/onReactionAdded.ts +4 -0
  78. package/src/reaction/utils/index.ts +1 -0
  79. package/src/reaction/utils/prepareMessagePayloadForCache.ts +40 -0
  80. package/src/report/api/createReport.ts +7 -1
  81. package/src/report/api/deleteReport.ts +7 -1
@@ -0,0 +1,67 @@
1
+ import { getActiveClient } from '~/client/api';
2
+
3
+ import { createQuery, runQuery, queryOptions } from '~/core/query';
4
+
5
+ import { getPoll } from '../api';
6
+
7
+ import { onPollUpdated, onPollDeleted } from '../events';
8
+
9
+ /**
10
+ * ```js
11
+ * import { observePoll } from '@amityco/ts-sdk'
12
+ *
13
+ * let poll = {}
14
+ * const dispose = observePoll(pollId, updated => poll = updated)
15
+ * ```
16
+ *
17
+ * Observe all mutation on a given {@link Amity.Poll}
18
+ *
19
+ * @param pollId the ID of the poll to observe
20
+ * @param callback the function to call when new data are available
21
+ * @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the poll
22
+ *
23
+ * @category Poll Observer
24
+ */
25
+ export const observePoll = <Events extends ['onFetch', 'onUpdate', 'onDelete']>(
26
+ pollId: string,
27
+ callback: Amity.ObjectListener<Amity.Snapshot<Amity.Poll | undefined>, Events>,
28
+ policy: Amity.QueryPolicy = 'cache_then_server',
29
+ ): Amity.Unsubscriber => {
30
+ const { log } = getActiveClient();
31
+
32
+ const timestamp = Date.now();
33
+ log(`observePoll(tmpid: ${timestamp}) > listen`);
34
+
35
+ // wrapper function to make sure
36
+ const router = (result: Amity.Snapshot<Amity.Poll | undefined>, action: Events[number]) => {
37
+ if (callback instanceof Function) return callback(result);
38
+
39
+ if (action !== 'onFetch') callback.onEvent?.(action, result);
40
+
41
+ callback[action]?.(result);
42
+ };
43
+
44
+ const realtimeRouter = (result: Amity.Snapshot<Amity.Poll>, action: Events[number]) => {
45
+ if (result.data?.pollId !== pollId) return;
46
+
47
+ router(result, action);
48
+ };
49
+
50
+ const disposers: Amity.Unsubscriber[] = [];
51
+
52
+ disposers.push(
53
+ onPollUpdated(data => realtimeRouter({ data, loading: false, origin: 'event' }, 'onUpdate')),
54
+ onPollDeleted(data => realtimeRouter({ data, loading: false, origin: 'event' }, 'onDelete')),
55
+ );
56
+
57
+ runQuery(
58
+ createQuery(getPoll, pollId),
59
+ result => (result.data || result.error) && router(result, 'onFetch'),
60
+ queryOptions(policy),
61
+ );
62
+
63
+ return () => {
64
+ log(`observePoll(tmpid: ${timestamp}) > dispose`);
65
+ disposers.forEach(fn => fn());
66
+ };
67
+ };
@@ -0,0 +1,48 @@
1
+ import { getActiveClient } from '~/client/api';
2
+
3
+ import { ingestInCache } from '~/cache/api/ingestInCache';
4
+ import { fireEvent } from '~/core/events';
5
+
6
+ /**
7
+ * ```js
8
+ * import { approvePost } from '@amityco/ts-sdk'
9
+ *
10
+ * const { data: post } = await approvePost('postId')
11
+ * ```
12
+ *
13
+ * Approves a {@link Amity.Post}
14
+ *
15
+ * @param postId The {@link Amity.Post} ID to be approved
16
+ * @return A {@link Amity.Post} that was approved
17
+ *
18
+ * @category Post API
19
+ * @async
20
+ */
21
+ export const approvePost = async (
22
+ postId: Amity.Post['postId'],
23
+ ): Promise<Amity.Cached<Amity.Post>> => {
24
+ const client = getActiveClient();
25
+ client.log('post/approvePost', postId);
26
+
27
+ const { data: approved } = await client.http.post<Amity.PostPayload>(
28
+ `/api/v3/posts/${encodeURIComponent(postId)}/approve`,
29
+ );
30
+
31
+ const cachedAt = client.cache && Date.now();
32
+ if (client.cache) ingestInCache(approved, { cachedAt });
33
+
34
+ // fire virtual event for community update
35
+ if (approved.posts[0].targetType === 'community') {
36
+ const community = approved.communities?.[0];
37
+
38
+ // @ts-ignore as communities is optional currently in PostPayload
39
+ fireEvent('community.updated', { communities: [community] });
40
+ }
41
+
42
+ fireEvent('post.approved', { posts: [approved.posts[0]] });
43
+
44
+ return {
45
+ data: approved.posts.find(post => post.postId === postId)!,
46
+ cachedAt,
47
+ };
48
+ };
@@ -0,0 +1,48 @@
1
+ import { getActiveClient } from '~/client/api';
2
+
3
+ import { ingestInCache } from '~/cache/api/ingestInCache';
4
+ import { fireEvent } from '~/core/events';
5
+
6
+ /**
7
+ * ```js
8
+ * import { declinePost } from '@amityco/ts-sdk'
9
+ *
10
+ * const {data: post} = await declinePost('postId')
11
+ * ```
12
+ *
13
+ * Declines a {@link Amity.Post}
14
+ *
15
+ * @param postId The {@link Amity.Post} ID to be declined
16
+ * @return A {@link Amity.Post} that was declined
17
+ *
18
+ * @category Post API
19
+ * @async
20
+ */
21
+ export const declinePost = async (
22
+ postId: Amity.Post['postId'],
23
+ ): Promise<Amity.Cached<Amity.Post>> => {
24
+ const client = getActiveClient();
25
+ client.log('post/declinePost', postId);
26
+
27
+ const { data: declined } = await client.http.post<Amity.PostPayload>(
28
+ `/api/v3/posts/${encodeURIComponent(postId)}/decline`,
29
+ );
30
+
31
+ const cachedAt = client.cache && Date.now();
32
+ if (client.cache) ingestInCache(declined, { cachedAt });
33
+
34
+ // fire virtual event
35
+ if (declined.posts[0].targetType === 'community') {
36
+ const community = declined.communities?.[0];
37
+
38
+ // @ts-ignore as communities is optional currently in PostPayload
39
+ fireEvent('community.updated', { communities: [community] });
40
+ }
41
+
42
+ fireEvent('post.declined', { posts: [declined.posts[0]] });
43
+
44
+ return {
45
+ data: declined.posts.find(post => post.postId === postId)!,
46
+ cachedAt,
47
+ };
48
+ };
@@ -5,3 +5,6 @@ export * from './getPost';
5
5
  export * from './createPost';
6
6
  export * from './updatePost';
7
7
  export * from './deletePost';
8
+
9
+ export * from './approvePost';
10
+ export * from './declinePost';
@@ -34,6 +34,10 @@ export const onReactionAdded = (
34
34
  }
35
35
  };
36
36
 
37
+ /*
38
+ * Important: Want to point out that reference type will never be 'message'
39
+ * for onReactionAdded
40
+ */
37
41
  if (referenceType === 'message') {
38
42
  const client = getActiveClient();
39
43
 
@@ -1,3 +1,4 @@
1
1
  export * from './dispatchReactable';
2
2
  export * from './fetchReference';
3
3
  export * from './preparePayloadForCache';
4
+ export * from './prepareMessagePayloadForCache';
@@ -0,0 +1,40 @@
1
+ import { pullFromCache } from '~/cache/api';
2
+ import { getActiveClient } from '~/client/api';
3
+
4
+ /** @hidden */
5
+ /*
6
+ * @param message payload from http request without myReactions
7
+ * add myReactions to http response if the event was a reaction event
8
+ */
9
+ export const prepareMessagePayloadForCache = (payload: Amity.Message) => {
10
+ const client = getActiveClient();
11
+
12
+ const cached = pullFromCache<Amity.Message>(['message', 'get', payload.messageId]);
13
+ // '[]' in cases where the new reaction is the first one
14
+ const myReactions = cached?.data.myReactions || [];
15
+
16
+ // add myReactions to the payload
17
+ Object.assign(payload, { myReactions });
18
+
19
+ // check if there are any updates to the reactions
20
+ const latestReaction = payload?.latestReaction;
21
+ const isLatestReactionMine = latestReaction && latestReaction.userId === client.userId;
22
+
23
+ if (!isLatestReactionMine) {
24
+ return;
25
+ }
26
+
27
+ // new reaction added
28
+ if (latestReaction.eventName === 'add' && !myReactions.includes(latestReaction.reactionName)) {
29
+ Object.assign(payload, {
30
+ myReactions: [...myReactions, latestReaction.reactionName],
31
+ });
32
+ }
33
+
34
+ // existing reaction removed
35
+ if (latestReaction.eventName === 'remove' && myReactions.includes(latestReaction.reactionName)) {
36
+ Object.assign(payload, {
37
+ myReactions: myReactions.filter(x => x !== latestReaction.reactionName),
38
+ });
39
+ }
40
+ };
@@ -29,7 +29,13 @@ export const createReport = async (
29
29
 
30
30
  const { data } = await client.http.post<
31
31
  Amity.PostPayload | Amity.CommentPayload | Amity.MessagePayload | Amity.UserPayload
32
- >(`/api/v3/${domainName}/${encodeURIComponent(referenceId)}/flag`);
32
+ >(
33
+ `/api/${
34
+ referenceType === 'user'
35
+ ? `v4/me/flags/${encodeURIComponent(referenceId)}`
36
+ : `v3/${domainName}/${encodeURIComponent(referenceId)}/flag`
37
+ }`,
38
+ );
33
39
 
34
40
  if (client.cache) ingestInCache(data);
35
41
 
@@ -29,7 +29,13 @@ export const deleteReport = async (
29
29
 
30
30
  const { data } = await client.http.delete<
31
31
  Amity.PostPayload | Amity.CommentPayload | Amity.MessagePayload | Amity.UserPayload
32
- >(`/api/v3/${domainName}/${encodeURIComponent(referenceId)}/unflag`);
32
+ >(
33
+ `/api/${
34
+ referenceType === 'user'
35
+ ? `v4/me/flags/${encodeURIComponent(referenceId)}`
36
+ : `v3/${domainName}/${encodeURIComponent(referenceId)}/unflag`
37
+ }`,
38
+ );
33
39
 
34
40
  if (client.cache) ingestInCache(data);
35
41