@amityco/ts-sdk 0.0.1-02f0fa3.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/LICENSE +390 -0
- package/README.md +137 -0
- package/dist/@types/core/cache.d.ts +21 -0
- package/dist/@types/core/cache.d.ts.map +1 -0
- package/dist/@types/core/errors.d.ts +32 -0
- package/dist/@types/core/errors.d.ts.map +1 -0
- package/dist/@types/core/events.d.ts +46 -0
- package/dist/@types/core/events.d.ts.map +1 -0
- package/dist/@types/core/model.d.ts +41 -0
- package/dist/@types/core/model.d.ts.map +1 -0
- package/dist/@types/core/payload.d.ts +88 -0
- package/dist/@types/core/payload.d.ts.map +1 -0
- package/dist/@types/core/permissions.d.ts +71 -0
- package/dist/@types/core/permissions.d.ts.map +1 -0
- package/dist/@types/core/query.d.ts +72 -0
- package/dist/@types/core/query.d.ts.map +1 -0
- package/dist/@types/core/store.d.ts +46 -0
- package/dist/@types/core/store.d.ts.map +1 -0
- package/dist/@types/core/transport.d.ts +27 -0
- package/dist/@types/core/transport.d.ts.map +1 -0
- package/dist/@types/domains/category.d.ts +11 -0
- package/dist/@types/domains/category.d.ts.map +1 -0
- package/dist/@types/domains/channel.d.ts +26 -0
- package/dist/@types/domains/channel.d.ts.map +1 -0
- package/dist/@types/domains/client.d.ts +31 -0
- package/dist/@types/domains/client.d.ts.map +1 -0
- package/dist/@types/domains/comment.d.ts +17 -0
- package/dist/@types/domains/comment.d.ts.map +1 -0
- package/dist/@types/domains/community.d.ts +24 -0
- package/dist/@types/domains/community.d.ts.map +1 -0
- package/dist/@types/domains/content.d.ts +26 -0
- package/dist/@types/domains/content.d.ts.map +1 -0
- package/dist/@types/domains/feed.d.ts +14 -0
- package/dist/@types/domains/feed.d.ts.map +1 -0
- package/dist/@types/domains/file.d.ts +29 -0
- package/dist/@types/domains/file.d.ts.map +1 -0
- package/dist/@types/domains/group.d.ts +26 -0
- package/dist/@types/domains/group.d.ts.map +1 -0
- package/dist/@types/domains/message.d.ts +16 -0
- package/dist/@types/domains/message.d.ts.map +1 -0
- package/dist/@types/domains/partials.d.ts +42 -0
- package/dist/@types/domains/partials.d.ts.map +1 -0
- package/dist/@types/domains/post.d.ts +22 -0
- package/dist/@types/domains/post.d.ts.map +1 -0
- package/dist/@types/domains/reaction.d.ts +24 -0
- package/dist/@types/domains/reaction.d.ts.map +1 -0
- package/dist/@types/domains/stream.d.ts +47 -0
- package/dist/@types/domains/stream.d.ts.map +1 -0
- package/dist/@types/domains/user.d.ts +12 -0
- package/dist/@types/domains/user.d.ts.map +1 -0
- package/dist/@types/global.d.ts +13 -0
- package/dist/@types/global.d.ts.map +1 -0
- package/dist/@types/index.d.ts +23 -0
- package/dist/@types/index.d.ts.map +1 -0
- package/dist/category/api/getCategory.d.ts +16 -0
- package/dist/category/api/getCategory.d.ts.map +1 -0
- package/dist/category/api/index.d.ts +3 -0
- package/dist/category/api/index.d.ts.map +1 -0
- package/dist/category/api/queryCategories.d.ts +23 -0
- package/dist/category/api/queryCategories.d.ts.map +1 -0
- package/dist/channel/api/addChannelMembers.d.ts +17 -0
- package/dist/channel/api/addChannelMembers.d.ts.map +1 -0
- package/dist/channel/api/addChannelMembersRole.d.ts +18 -0
- package/dist/channel/api/addChannelMembersRole.d.ts.map +1 -0
- package/dist/channel/api/createChannel.d.ts +19 -0
- package/dist/channel/api/createChannel.d.ts.map +1 -0
- package/dist/channel/api/deleteChannel.d.ts +16 -0
- package/dist/channel/api/deleteChannel.d.ts.map +1 -0
- package/dist/channel/api/getChannel.d.ts +32 -0
- package/dist/channel/api/getChannel.d.ts.map +1 -0
- package/dist/channel/api/getChannels.d.ts +32 -0
- package/dist/channel/api/getChannels.d.ts.map +1 -0
- package/dist/channel/api/index.d.ts +14 -0
- package/dist/channel/api/index.d.ts.map +1 -0
- package/dist/channel/api/joinChannel.d.ts +16 -0
- package/dist/channel/api/joinChannel.d.ts.map +1 -0
- package/dist/channel/api/leaveChannel.d.ts +16 -0
- package/dist/channel/api/leaveChannel.d.ts.map +1 -0
- package/dist/channel/api/queryChannelMembers.d.ts +42 -0
- package/dist/channel/api/queryChannelMembers.d.ts.map +1 -0
- package/dist/channel/api/queryChannels.d.ts +43 -0
- package/dist/channel/api/queryChannels.d.ts.map +1 -0
- package/dist/channel/api/removeChannelMembers.d.ts +17 -0
- package/dist/channel/api/removeChannelMembers.d.ts.map +1 -0
- package/dist/channel/api/removeChannelMembersRole.d.ts +18 -0
- package/dist/channel/api/removeChannelMembersRole.d.ts.map +1 -0
- package/dist/channel/api/updateChannel.d.ts +17 -0
- package/dist/channel/api/updateChannel.d.ts.map +1 -0
- package/dist/channel/events/index.d.ts +7 -0
- package/dist/channel/events/index.d.ts.map +1 -0
- package/dist/channel/events/onChannelCreated.d.ts +17 -0
- package/dist/channel/events/onChannelCreated.d.ts.map +1 -0
- package/dist/channel/events/onChannelDeleted.d.ts +17 -0
- package/dist/channel/events/onChannelDeleted.d.ts.map +1 -0
- package/dist/channel/events/onChannelJoined.d.ts +17 -0
- package/dist/channel/events/onChannelJoined.d.ts.map +1 -0
- package/dist/channel/events/onChannelLeft.d.ts +17 -0
- package/dist/channel/events/onChannelLeft.d.ts.map +1 -0
- package/dist/channel/events/onChannelUpdated.d.ts +17 -0
- package/dist/channel/events/onChannelUpdated.d.ts.map +1 -0
- package/dist/channel/events/onMemberAdded.d.ts +17 -0
- package/dist/channel/events/onMemberAdded.d.ts.map +1 -0
- package/dist/channel/events/onMemberRemoved.d.ts +17 -0
- package/dist/channel/events/onMemberRemoved.d.ts.map +1 -0
- package/dist/channel/observers/index.d.ts +2 -0
- package/dist/channel/observers/index.d.ts.map +1 -0
- package/dist/channel/observers/observeChannel.d.ts +20 -0
- package/dist/channel/observers/observeChannel.d.ts.map +1 -0
- package/dist/client/api/activeClient.d.ts +17 -0
- package/dist/client/api/activeClient.d.ts.map +1 -0
- package/dist/client/api/clearCache.d.ts +15 -0
- package/dist/client/api/clearCache.d.ts.map +1 -0
- package/dist/client/api/connectClient.d.ts +26 -0
- package/dist/client/api/connectClient.d.ts.map +1 -0
- package/dist/client/api/createClient.d.ts +17 -0
- package/dist/client/api/createClient.d.ts.map +1 -0
- package/dist/client/api/disconnectClient.d.ts +16 -0
- package/dist/client/api/disconnectClient.d.ts.map +1 -0
- package/dist/client/api/enableCache.d.ts +15 -0
- package/dist/client/api/enableCache.d.ts.map +1 -0
- package/dist/client/api/getToken.d.ts +19 -0
- package/dist/client/api/getToken.d.ts.map +1 -0
- package/dist/client/api/index.d.ts +8 -0
- package/dist/client/api/index.d.ts.map +1 -0
- package/dist/client/api/isConnected.d.ts +14 -0
- package/dist/client/api/isConnected.d.ts.map +1 -0
- package/dist/client/events/index.d.ts +2 -0
- package/dist/client/events/index.d.ts.map +1 -0
- package/dist/client/events/onClientDisconnected.d.ts +17 -0
- package/dist/client/events/onClientDisconnected.d.ts.map +1 -0
- package/dist/comment/api/createComment.d.ts +16 -0
- package/dist/comment/api/createComment.d.ts.map +1 -0
- package/dist/comment/api/deleteComment.d.ts +16 -0
- package/dist/comment/api/deleteComment.d.ts.map +1 -0
- package/dist/comment/api/getComment.d.ts +32 -0
- package/dist/comment/api/getComment.d.ts.map +1 -0
- package/dist/comment/api/getComments.d.ts +32 -0
- package/dist/comment/api/getComments.d.ts.map +1 -0
- package/dist/comment/api/index.d.ts +7 -0
- package/dist/comment/api/index.d.ts.map +1 -0
- package/dist/comment/api/queryComments.d.ts +53 -0
- package/dist/comment/api/queryComments.d.ts.map +1 -0
- package/dist/comment/api/updateComment.d.ts +19 -0
- package/dist/comment/api/updateComment.d.ts.map +1 -0
- package/dist/comment/events/index.d.ts +4 -0
- package/dist/comment/events/index.d.ts.map +1 -0
- package/dist/comment/events/onCommentCreated.d.ts +17 -0
- package/dist/comment/events/onCommentCreated.d.ts.map +1 -0
- package/dist/comment/events/onCommentDeleted.d.ts +17 -0
- package/dist/comment/events/onCommentDeleted.d.ts.map +1 -0
- package/dist/comment/events/onCommentUpdated.d.ts +17 -0
- package/dist/comment/events/onCommentUpdated.d.ts.map +1 -0
- package/dist/comment/observers/index.d.ts +3 -0
- package/dist/comment/observers/index.d.ts.map +1 -0
- package/dist/comment/observers/observeComment.d.ts +20 -0
- package/dist/comment/observers/observeComment.d.ts.map +1 -0
- package/dist/comment/observers/observeComments.d.ts +22 -0
- package/dist/comment/observers/observeComments.d.ts.map +1 -0
- package/dist/community/api/addCommunityMembers.d.ts +17 -0
- package/dist/community/api/addCommunityMembers.d.ts.map +1 -0
- package/dist/community/api/addCommunityMembersRole.d.ts +18 -0
- package/dist/community/api/addCommunityMembersRole.d.ts.map +1 -0
- package/dist/community/api/createCommunity.d.ts +20 -0
- package/dist/community/api/createCommunity.d.ts.map +1 -0
- package/dist/community/api/deleteCommunity.d.ts +16 -0
- package/dist/community/api/deleteCommunity.d.ts.map +1 -0
- package/dist/community/api/getCommunities.d.ts +32 -0
- package/dist/community/api/getCommunities.d.ts.map +1 -0
- package/dist/community/api/getCommunity.d.ts +32 -0
- package/dist/community/api/getCommunity.d.ts.map +1 -0
- package/dist/community/api/index.d.ts +14 -0
- package/dist/community/api/index.d.ts.map +1 -0
- package/dist/community/api/joinCommunity.d.ts +16 -0
- package/dist/community/api/joinCommunity.d.ts.map +1 -0
- package/dist/community/api/leaveCommunity.d.ts +16 -0
- package/dist/community/api/leaveCommunity.d.ts.map +1 -0
- package/dist/community/api/queryCommunities.d.ts +46 -0
- package/dist/community/api/queryCommunities.d.ts.map +1 -0
- package/dist/community/api/queryCommunityMembers.d.ts +42 -0
- package/dist/community/api/queryCommunityMembers.d.ts.map +1 -0
- package/dist/community/api/removeCommunityMembers.d.ts +17 -0
- package/dist/community/api/removeCommunityMembers.d.ts.map +1 -0
- package/dist/community/api/removeCommunityMembersRole.d.ts +18 -0
- package/dist/community/api/removeCommunityMembersRole.d.ts.map +1 -0
- package/dist/community/api/updateCommunity.d.ts +17 -0
- package/dist/community/api/updateCommunity.d.ts.map +1 -0
- package/dist/community/events/index.d.ts +6 -0
- package/dist/community/events/index.d.ts.map +1 -0
- package/dist/community/events/onCommunityCreated.d.ts +17 -0
- package/dist/community/events/onCommunityCreated.d.ts.map +1 -0
- package/dist/community/events/onCommunityDeleted.d.ts +17 -0
- package/dist/community/events/onCommunityDeleted.d.ts.map +1 -0
- package/dist/community/events/onCommunityJoined.d.ts +17 -0
- package/dist/community/events/onCommunityJoined.d.ts.map +1 -0
- package/dist/community/events/onCommunityLeft.d.ts +17 -0
- package/dist/community/events/onCommunityLeft.d.ts.map +1 -0
- package/dist/community/events/onCommunityUpdated.d.ts +17 -0
- package/dist/community/events/onCommunityUpdated.d.ts.map +1 -0
- package/dist/community/observers/index.d.ts +2 -0
- package/dist/community/observers/index.d.ts.map +1 -0
- package/dist/community/observers/observeCommunity.d.ts +20 -0
- package/dist/community/observers/observeCommunity.d.ts.map +1 -0
- package/dist/core/cache/drop.d.ts +11 -0
- package/dist/core/cache/drop.d.ts.map +1 -0
- package/dist/core/cache/index.d.ts +19 -0
- package/dist/core/cache/index.d.ts.map +1 -0
- package/dist/core/cache/ingest.d.ts +10 -0
- package/dist/core/cache/ingest.d.ts.map +1 -0
- package/dist/core/cache/merge.d.ts +10 -0
- package/dist/core/cache/merge.d.ts.map +1 -0
- package/dist/core/cache/mutate.d.ts +10 -0
- package/dist/core/cache/mutate.d.ts.map +1 -0
- package/dist/core/cache/pull.d.ts +11 -0
- package/dist/core/cache/pull.d.ts.map +1 -0
- package/dist/core/cache/push.d.ts +10 -0
- package/dist/core/cache/push.d.ts.map +1 -0
- package/dist/core/cache/sync.d.ts +10 -0
- package/dist/core/cache/sync.d.ts.map +1 -0
- package/dist/core/debug.d.ts +15 -0
- package/dist/core/debug.d.ts.map +1 -0
- package/dist/core/device.d.ts +7 -0
- package/dist/core/device.d.ts.map +1 -0
- package/dist/core/errors.d.ts +59 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/events.d.ts +33 -0
- package/dist/core/events.d.ts.map +1 -0
- package/dist/core/model/idResolvers.d.ts +20 -0
- package/dist/core/model/idResolvers.d.ts.map +1 -0
- package/dist/core/model/identifyModel.d.ts +8 -0
- package/dist/core/model/identifyModel.d.ts.map +1 -0
- package/dist/core/model/index.d.ts +4 -0
- package/dist/core/model/index.d.ts.map +1 -0
- package/dist/core/model/toRecord.d.ts +10 -0
- package/dist/core/model/toRecord.d.ts.map +1 -0
- package/dist/core/query/decorate.d.ts +23 -0
- package/dist/core/query/decorate.d.ts.map +1 -0
- package/dist/core/query/filtering.d.ts +12 -0
- package/dist/core/query/filtering.d.ts.map +1 -0
- package/dist/core/query/index.d.ts +6 -0
- package/dist/core/query/index.d.ts.map +1 -0
- package/dist/core/query/paging.d.ts +58 -0
- package/dist/core/query/paging.d.ts.map +1 -0
- package/dist/core/query/query.d.ts +121 -0
- package/dist/core/query/query.d.ts.map +1 -0
- package/dist/core/query/sorting.d.ts +39 -0
- package/dist/core/query/sorting.d.ts.map +1 -0
- package/dist/core/stores/index.d.ts +3 -0
- package/dist/core/stores/index.d.ts.map +1 -0
- package/dist/core/stores/memory.d.ts +30 -0
- package/dist/core/stores/memory.d.ts.map +1 -0
- package/dist/core/stores/persistent.d.ts +13 -0
- package/dist/core/stores/persistent.d.ts.map +1 -0
- package/dist/core/transports.d.ts +49 -0
- package/dist/core/transports.d.ts.map +1 -0
- package/dist/core/uuid.d.ts +2 -0
- package/dist/core/uuid.d.ts.map +1 -0
- package/dist/feed/api/index.d.ts +2 -0
- package/dist/feed/api/index.d.ts.map +1 -0
- package/dist/feed/api/queryGlobalFeed.d.ts +23 -0
- package/dist/feed/api/queryGlobalFeed.d.ts.map +1 -0
- package/dist/file/api/createFile.d.ts +16 -0
- package/dist/file/api/createFile.d.ts.map +1 -0
- package/dist/file/api/deleteFile.d.ts +16 -0
- package/dist/file/api/deleteFile.d.ts.map +1 -0
- package/dist/file/api/fileUrlWithSize.d.ts +9 -0
- package/dist/file/api/fileUrlWithSize.d.ts.map +1 -0
- package/dist/file/api/getFile.d.ts +32 -0
- package/dist/file/api/getFile.d.ts.map +1 -0
- package/dist/file/api/index.d.ts +5 -0
- package/dist/file/api/index.d.ts.map +1 -0
- package/dist/file/observers/index.d.ts +2 -0
- package/dist/file/observers/index.d.ts.map +1 -0
- package/dist/file/observers/observeFile.d.ts +20 -0
- package/dist/file/observers/observeFile.d.ts.map +1 -0
- package/dist/index.cjs.js +4884 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.esm.js +4734 -0
- package/dist/index.umd.js +4880 -0
- package/dist/message/api/createMessage.d.ts +38 -0
- package/dist/message/api/createMessage.d.ts.map +1 -0
- package/dist/message/api/deleteMessage.d.ts +32 -0
- package/dist/message/api/deleteMessage.d.ts.map +1 -0
- package/dist/message/api/getMessage.d.ts +32 -0
- package/dist/message/api/getMessage.d.ts.map +1 -0
- package/dist/message/api/getMessages.d.ts +32 -0
- package/dist/message/api/getMessages.d.ts.map +1 -0
- package/dist/message/api/index.d.ts +7 -0
- package/dist/message/api/index.d.ts.map +1 -0
- package/dist/message/api/queryMessages.d.ts +55 -0
- package/dist/message/api/queryMessages.d.ts.map +1 -0
- package/dist/message/api/updateMessage.d.ts +38 -0
- package/dist/message/api/updateMessage.d.ts.map +1 -0
- package/dist/message/events/index.d.ts +4 -0
- package/dist/message/events/index.d.ts.map +1 -0
- package/dist/message/events/onMessageCreated.d.ts +17 -0
- package/dist/message/events/onMessageCreated.d.ts.map +1 -0
- package/dist/message/events/onMessageDeleted.d.ts +17 -0
- package/dist/message/events/onMessageDeleted.d.ts.map +1 -0
- package/dist/message/events/onMessageUpdated.d.ts +17 -0
- package/dist/message/events/onMessageUpdated.d.ts.map +1 -0
- package/dist/message/observers/index.d.ts +3 -0
- package/dist/message/observers/index.d.ts.map +1 -0
- package/dist/message/observers/observeMessage.d.ts +22 -0
- package/dist/message/observers/observeMessage.d.ts.map +1 -0
- package/dist/message/observers/observeMessages.d.ts +19 -0
- package/dist/message/observers/observeMessages.d.ts.map +1 -0
- package/dist/post/api/createPost.d.ts +21 -0
- package/dist/post/api/createPost.d.ts.map +1 -0
- package/dist/post/api/deletePost.d.ts +16 -0
- package/dist/post/api/deletePost.d.ts.map +1 -0
- package/dist/post/api/getPost.d.ts +32 -0
- package/dist/post/api/getPost.d.ts.map +1 -0
- package/dist/post/api/getPosts.d.ts +32 -0
- package/dist/post/api/getPosts.d.ts.map +1 -0
- package/dist/post/api/index.d.ts +7 -0
- package/dist/post/api/index.d.ts.map +1 -0
- package/dist/post/api/queryPosts.d.ts +45 -0
- package/dist/post/api/queryPosts.d.ts.map +1 -0
- package/dist/post/api/updatePost.d.ts +19 -0
- package/dist/post/api/updatePost.d.ts.map +1 -0
- package/dist/post/events/index.d.ts +4 -0
- package/dist/post/events/index.d.ts.map +1 -0
- package/dist/post/events/onPostCreated.d.ts +17 -0
- package/dist/post/events/onPostCreated.d.ts.map +1 -0
- package/dist/post/events/onPostDeleted.d.ts +17 -0
- package/dist/post/events/onPostDeleted.d.ts.map +1 -0
- package/dist/post/events/onPostUpdated.d.ts +17 -0
- package/dist/post/events/onPostUpdated.d.ts.map +1 -0
- package/dist/post/observers/index.d.ts +3 -0
- package/dist/post/observers/index.d.ts.map +1 -0
- package/dist/post/observers/observePost.d.ts +20 -0
- package/dist/post/observers/observePost.d.ts.map +1 -0
- package/dist/post/observers/observePosts.d.ts +25 -0
- package/dist/post/observers/observePosts.d.ts.map +1 -0
- package/dist/reaction/api/addReaction.d.ts +36 -0
- package/dist/reaction/api/addReaction.d.ts.map +1 -0
- package/dist/reaction/api/index.d.ts +3 -0
- package/dist/reaction/api/index.d.ts.map +1 -0
- package/dist/reaction/api/removeReaction.d.ts +36 -0
- package/dist/reaction/api/removeReaction.d.ts.map +1 -0
- package/dist/reaction/events/index.d.ts +2 -0
- package/dist/reaction/events/index.d.ts.map +1 -0
- package/dist/reaction/events/onReactionAdded.d.ts +17 -0
- package/dist/reaction/events/onReactionAdded.d.ts.map +1 -0
- package/dist/reaction/events/onReactionRemoved.d.ts +17 -0
- package/dist/reaction/events/onReactionRemoved.d.ts.map +1 -0
- package/dist/reaction/utils/dispatchReactable.d.ts +3 -0
- package/dist/reaction/utils/dispatchReactable.d.ts.map +1 -0
- package/dist/reaction/utils/fetchReference.d.ts +3 -0
- package/dist/reaction/utils/fetchReference.d.ts.map +1 -0
- package/dist/reaction/utils/index.d.ts +3 -0
- package/dist/reaction/utils/index.d.ts.map +1 -0
- package/dist/report/api/createReport.d.ts +16 -0
- package/dist/report/api/createReport.d.ts.map +1 -0
- package/dist/report/api/deleteReport.d.ts +16 -0
- package/dist/report/api/deleteReport.d.ts.map +1 -0
- package/dist/report/api/index.d.ts +4 -0
- package/dist/report/api/index.d.ts.map +1 -0
- package/dist/report/api/isReportedByMe.d.ts +16 -0
- package/dist/report/api/isReportedByMe.d.ts.map +1 -0
- package/dist/report/constants/referenceTypes.d.ts +24 -0
- package/dist/report/constants/referenceTypes.d.ts.map +1 -0
- package/dist/role/api/getRole.d.ts +32 -0
- package/dist/role/api/getRole.d.ts.map +1 -0
- package/dist/role/api/index.d.ts +3 -0
- package/dist/role/api/index.d.ts.map +1 -0
- package/dist/role/api/queryRoles.d.ts +42 -0
- package/dist/role/api/queryRoles.d.ts.map +1 -0
- package/dist/stream/api/createStream.d.ts +18 -0
- package/dist/stream/api/createStream.d.ts.map +1 -0
- package/dist/stream/api/deleteStream.d.ts +16 -0
- package/dist/stream/api/deleteStream.d.ts.map +1 -0
- package/dist/stream/api/getStream.d.ts +12 -0
- package/dist/stream/api/getStream.d.ts.map +1 -0
- package/dist/stream/api/index.d.ts +6 -0
- package/dist/stream/api/index.d.ts.map +1 -0
- package/dist/stream/api/queryStreams.d.ts +23 -0
- package/dist/stream/api/queryStreams.d.ts.map +1 -0
- package/dist/stream/api/updateStream.d.ts +17 -0
- package/dist/stream/api/updateStream.d.ts.map +1 -0
- package/dist/stream/events/index.d.ts +4 -0
- package/dist/stream/events/index.d.ts.map +1 -0
- package/dist/stream/events/onStreamRecorded.d.ts +17 -0
- package/dist/stream/events/onStreamRecorded.d.ts.map +1 -0
- package/dist/stream/events/onStreamStarted.d.ts +17 -0
- package/dist/stream/events/onStreamStarted.d.ts.map +1 -0
- package/dist/stream/events/onStreamStopped.d.ts +17 -0
- package/dist/stream/events/onStreamStopped.d.ts.map +1 -0
- package/dist/user/api/getUser.d.ts +32 -0
- package/dist/user/api/getUser.d.ts.map +1 -0
- package/dist/user/api/getUsers.d.ts +32 -0
- package/dist/user/api/getUsers.d.ts.map +1 -0
- package/dist/user/api/index.d.ts +5 -0
- package/dist/user/api/index.d.ts.map +1 -0
- package/dist/user/api/queryUsers.d.ts +43 -0
- package/dist/user/api/queryUsers.d.ts.map +1 -0
- package/dist/user/api/updateUser.d.ts +17 -0
- package/dist/user/api/updateUser.d.ts.map +1 -0
- package/dist/user/events/index.d.ts +2 -0
- package/dist/user/events/index.d.ts.map +1 -0
- package/dist/user/events/onUserUpdated.d.ts +17 -0
- package/dist/user/events/onUserUpdated.d.ts.map +1 -0
- package/dist/user/observers/index.d.ts +2 -0
- package/dist/user/observers/index.d.ts.map +1 -0
- package/dist/user/observers/observeUser.d.ts +19 -0
- package/dist/user/observers/observeUser.d.ts.map +1 -0
- package/package.json +59 -0
- package/rollup.config.js +41 -0
- package/src/@types/core/cache.ts +22 -0
- package/src/@types/core/errors.ts +34 -0
- package/src/@types/core/events.ts +60 -0
- package/src/@types/core/model.ts +51 -0
- package/src/@types/core/payload.ts +102 -0
- package/src/@types/core/permissions.ts +76 -0
- package/src/@types/core/query.ts +95 -0
- package/src/@types/core/store.ts +52 -0
- package/src/@types/core/transport.ts +32 -0
- package/src/@types/domains/category.ts +15 -0
- package/src/@types/domains/channel.ts +38 -0
- package/src/@types/domains/client.ts +36 -0
- package/src/@types/domains/comment.ts +26 -0
- package/src/@types/domains/community.ts +35 -0
- package/src/@types/domains/content.ts +35 -0
- package/src/@types/domains/feed.ts +16 -0
- package/src/@types/domains/file.ts +41 -0
- package/src/@types/domains/group.ts +38 -0
- package/src/@types/domains/message.ts +27 -0
- package/src/@types/domains/partials.ts +52 -0
- package/src/@types/domains/post.ts +31 -0
- package/src/@types/domains/reaction.ts +30 -0
- package/src/@types/domains/stream.ts +57 -0
- package/src/@types/domains/user.ts +18 -0
- package/src/@types/global.ts +25 -0
- package/src/@types/index.ts +31 -0
- package/src/category/api/getCategory.ts +35 -0
- package/src/category/api/index.ts +2 -0
- package/src/category/api/queryCategories.ts +49 -0
- package/src/channel/api/addChannelMembers.ts +45 -0
- package/src/channel/api/addChannelMembersRole.ts +43 -0
- package/src/channel/api/createChannel.ts +37 -0
- package/src/channel/api/deleteChannel.ts +34 -0
- package/src/channel/api/getChannel.ts +59 -0
- package/src/channel/api/getChannels.ts +63 -0
- package/src/channel/api/index.ts +18 -0
- package/src/channel/api/joinChannel.ts +38 -0
- package/src/channel/api/leaveChannel.ts +38 -0
- package/src/channel/api/queryChannelMembers.ts +79 -0
- package/src/channel/api/queryChannels.ts +89 -0
- package/src/channel/api/removeChannelMembers.ts +45 -0
- package/src/channel/api/removeChannelMembersRole.ts +43 -0
- package/src/channel/api/updateChannel.ts +38 -0
- package/src/channel/events/index.ts +6 -0
- package/src/channel/events/onChannelCreated.ts +37 -0
- package/src/channel/events/onChannelDeleted.ts +37 -0
- package/src/channel/events/onChannelJoined.ts +37 -0
- package/src/channel/events/onChannelLeft.ts +37 -0
- package/src/channel/events/onChannelUpdated.ts +37 -0
- package/src/channel/events/onMemberAdded.ts +37 -0
- package/src/channel/events/onMemberRemoved.ts +37 -0
- package/src/channel/observers/index.ts +1 -0
- package/src/channel/observers/observeChannel.ts +80 -0
- package/src/client/api/activeClient.ts +33 -0
- package/src/client/api/clearCache.ts +23 -0
- package/src/client/api/connectClient.ts +63 -0
- package/src/client/api/createClient.ts +56 -0
- package/src/client/api/disconnectClient.ts +31 -0
- package/src/client/api/enableCache.ts +35 -0
- package/src/client/api/getToken.ts +35 -0
- package/src/client/api/index.ts +7 -0
- package/src/client/api/isConnected.ts +24 -0
- package/src/client/events/index.ts +1 -0
- package/src/client/events/onClientDisconnected.ts +29 -0
- package/src/comment/api/createComment.ts +43 -0
- package/src/comment/api/deleteComment.ts +40 -0
- package/src/comment/api/getComment.ts +59 -0
- package/src/comment/api/getComments.ts +63 -0
- package/src/comment/api/index.ts +7 -0
- package/src/comment/api/queryComments.ts +169 -0
- package/src/comment/api/updateComment.ts +41 -0
- package/src/comment/events/index.ts +3 -0
- package/src/comment/events/onCommentCreated.ts +39 -0
- package/src/comment/events/onCommentDeleted.ts +39 -0
- package/src/comment/events/onCommentUpdated.ts +39 -0
- package/src/comment/observers/index.ts +2 -0
- package/src/comment/observers/observeComment.ts +73 -0
- package/src/comment/observers/observeComments.ts +61 -0
- package/src/community/api/addCommunityMembers.ts +46 -0
- package/src/community/api/addCommunityMembersRole.ts +43 -0
- package/src/community/api/createCommunity.ts +43 -0
- package/src/community/api/deleteCommunity.ts +35 -0
- package/src/community/api/getCommunities.ts +63 -0
- package/src/community/api/getCommunity.ts +58 -0
- package/src/community/api/index.ts +17 -0
- package/src/community/api/joinCommunity.ts +41 -0
- package/src/community/api/leaveCommunity.ts +41 -0
- package/src/community/api/queryCommunities.ts +92 -0
- package/src/community/api/queryCommunityMembers.ts +78 -0
- package/src/community/api/removeCommunityMembers.ts +46 -0
- package/src/community/api/removeCommunityMembersRole.ts +43 -0
- package/src/community/api/updateCommunity.ts +40 -0
- package/src/community/events/index.ts +6 -0
- package/src/community/events/onCommunityCreated.ts +37 -0
- package/src/community/events/onCommunityDeleted.ts +37 -0
- package/src/community/events/onCommunityJoined.ts +37 -0
- package/src/community/events/onCommunityLeft.ts +37 -0
- package/src/community/events/onCommunityUpdated.ts +37 -0
- package/src/community/observers/index.ts +1 -0
- package/src/community/observers/observeCommunity.ts +74 -0
- package/src/core/cache/drop.ts +24 -0
- package/src/core/cache/index.ts +24 -0
- package/src/core/cache/ingest.ts +24 -0
- package/src/core/cache/merge.ts +19 -0
- package/src/core/cache/mutate.ts +25 -0
- package/src/core/cache/pull.ts +31 -0
- package/src/core/cache/push.ts +41 -0
- package/src/core/cache/sync.ts +41 -0
- package/src/core/debug.ts +26 -0
- package/src/core/device.ts +38 -0
- package/src/core/errors.ts +85 -0
- package/src/core/events.ts +93 -0
- package/src/core/model/idResolvers.ts +66 -0
- package/src/core/model/identifyModel.ts +32 -0
- package/src/core/model/index.ts +3 -0
- package/src/core/model/toRecord.ts +11 -0
- package/src/core/query/decorate.ts +64 -0
- package/src/core/query/filtering.ts +17 -0
- package/src/core/query/index.ts +5 -0
- package/src/core/query/paging.ts +166 -0
- package/src/core/query/query.ts +300 -0
- package/src/core/query/sorting.ts +50 -0
- package/src/core/stores/index.ts +2 -0
- package/src/core/stores/memory.ts +59 -0
- package/src/core/stores/persistent.ts +63 -0
- package/src/core/transports.ts +157 -0
- package/src/core/uuid.ts +2 -0
- package/src/feed/api/index.ts +1 -0
- package/src/feed/api/queryGlobalFeed.ts +50 -0
- package/src/file/api/createFile.ts +51 -0
- package/src/file/api/deleteFile.ts +35 -0
- package/src/file/api/fileUrlWithSize.ts +13 -0
- package/src/file/api/getFile.ts +60 -0
- package/src/file/api/index.ts +4 -0
- package/src/file/observers/index.ts +1 -0
- package/src/file/observers/observeFile.ts +57 -0
- package/src/index.ts +55 -0
- package/src/message/api/createMessage.ts +90 -0
- package/src/message/api/deleteMessage.ts +75 -0
- package/src/message/api/getMessage.ts +60 -0
- package/src/message/api/getMessages.ts +63 -0
- package/src/message/api/index.ts +6 -0
- package/src/message/api/queryMessages.ts +145 -0
- package/src/message/api/updateMessage.ts +79 -0
- package/src/message/events/index.ts +3 -0
- package/src/message/events/onMessageCreated.ts +39 -0
- package/src/message/events/onMessageDeleted.ts +39 -0
- package/src/message/events/onMessageUpdated.ts +39 -0
- package/src/message/observers/index.ts +2 -0
- package/src/message/observers/observeMessage.ts +76 -0
- package/src/message/observers/observeMessages.ts +60 -0
- package/src/post/api/createPost.ts +45 -0
- package/src/post/api/deletePost.ts +43 -0
- package/src/post/api/getPost.ts +60 -0
- package/src/post/api/getPosts.ts +63 -0
- package/src/post/api/index.ts +7 -0
- package/src/post/api/queryPosts.ts +86 -0
- package/src/post/api/updatePost.ts +42 -0
- package/src/post/events/index.ts +3 -0
- package/src/post/events/onPostCreated.ts +39 -0
- package/src/post/events/onPostDeleted.ts +39 -0
- package/src/post/events/onPostUpdated.ts +39 -0
- package/src/post/observers/index.ts +2 -0
- package/src/post/observers/observePost.ts +72 -0
- package/src/post/observers/observePosts.ts +66 -0
- package/src/reaction/api/addReaction.ts +84 -0
- package/src/reaction/api/index.ts +2 -0
- package/src/reaction/api/removeReaction.ts +84 -0
- package/src/reaction/events/index.ts +1 -0
- package/src/reaction/events/onReactionAdded.ts +43 -0
- package/src/reaction/events/onReactionRemoved.ts +41 -0
- package/src/reaction/utils/dispatchReactable.ts +19 -0
- package/src/reaction/utils/fetchReference.ts +18 -0
- package/src/reaction/utils/index.ts +2 -0
- package/src/report/api/createReport.ts +40 -0
- package/src/report/api/deleteReport.ts +40 -0
- package/src/report/api/index.ts +4 -0
- package/src/report/api/isReportedByMe.ts +37 -0
- package/src/report/constants/referenceTypes.ts +22 -0
- package/src/role/api/getRole.ts +53 -0
- package/src/role/api/index.ts +2 -0
- package/src/role/api/queryRoles.ts +79 -0
- package/src/stream/api/createStream.ts +33 -0
- package/src/stream/api/deleteStream.ts +38 -0
- package/src/stream/api/getStream.ts +29 -0
- package/src/stream/api/index.ts +7 -0
- package/src/stream/api/queryStreams.ts +44 -0
- package/src/stream/api/updateStream.ts +36 -0
- package/src/stream/events/index.ts +3 -0
- package/src/stream/events/onStreamRecorded.ts +39 -0
- package/src/stream/events/onStreamStarted.ts +39 -0
- package/src/stream/events/onStreamStopped.ts +39 -0
- package/src/user/api/getUser.ts +59 -0
- package/src/user/api/getUsers.ts +60 -0
- package/src/user/api/index.ts +5 -0
- package/src/user/api/queryUsers.ts +88 -0
- package/src/user/api/updateUser.ts +44 -0
- package/src/user/events/index.ts +1 -0
- package/src/user/events/onUserUpdated.ts +39 -0
- package/src/user/observers/index.ts +1 -0
- package/src/user/observers/observeUser.ts +64 -0
- package/tsconfig.json +28 -0
|
@@ -0,0 +1,4734 @@
|
|
|
1
|
+
import { encode, decode } from 'js-base64';
|
|
2
|
+
import nativeIdGenerator from 'react-native-uuid';
|
|
3
|
+
import debug from 'debug';
|
|
4
|
+
import axios from 'axios';
|
|
5
|
+
import HttpAgent, { HttpsAgent } from 'agentkeepalive';
|
|
6
|
+
import io from 'socket.io-client';
|
|
7
|
+
import mitt from 'mitt';
|
|
8
|
+
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Generic ASC error
|
|
12
|
+
* @category Errors
|
|
13
|
+
*/
|
|
14
|
+
class ASCError extends Error {
|
|
15
|
+
/**
|
|
16
|
+
* @param message A custom error message
|
|
17
|
+
* @param code A normalized error code
|
|
18
|
+
* @param level A normalized failure level descriptor
|
|
19
|
+
*/
|
|
20
|
+
constructor(message, code, level) {
|
|
21
|
+
super(`Amity SDK (${code}): ${message}`);
|
|
22
|
+
this.code = code;
|
|
23
|
+
this.level = level;
|
|
24
|
+
this.type = 'ASC';
|
|
25
|
+
this.timestamp = Date.now();
|
|
26
|
+
if (Error.captureStackTrace)
|
|
27
|
+
Error.captureStackTrace(this, ASCError);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* API level error
|
|
32
|
+
* @category Errors
|
|
33
|
+
*/
|
|
34
|
+
class ASCApiError extends ASCError {
|
|
35
|
+
/**
|
|
36
|
+
* @param code A normalized error code
|
|
37
|
+
* @param level A normalized failure level descriptor
|
|
38
|
+
*/
|
|
39
|
+
constructor(message, code, level) {
|
|
40
|
+
super(message, code, level);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Unexpected error
|
|
45
|
+
* @category Errors
|
|
46
|
+
*/
|
|
47
|
+
class ASCUnknownError extends ASCError {
|
|
48
|
+
/**
|
|
49
|
+
* @param code A normalized error code
|
|
50
|
+
* @param level A normalized failure level descriptor
|
|
51
|
+
*/
|
|
52
|
+
constructor(code = 800000 /* UNKNOWN_ERROR */, level = "fatal" /* FATAL */) {
|
|
53
|
+
super('Unexpected error', code, level);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Network related error
|
|
58
|
+
* @category Errors
|
|
59
|
+
*/
|
|
60
|
+
class ASCConnectionError extends ASCError {
|
|
61
|
+
/**
|
|
62
|
+
* @param message A custom error message
|
|
63
|
+
*/
|
|
64
|
+
constructor(message = 'SDK client is having connection issues') {
|
|
65
|
+
super(message, 800210 /* CONNECTION_ERROR */, "error" /* ERROR */);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let activeClient = null;
|
|
70
|
+
/**
|
|
71
|
+
* Get the active client
|
|
72
|
+
*
|
|
73
|
+
* @returns the active client instance
|
|
74
|
+
*
|
|
75
|
+
* @hidden
|
|
76
|
+
*/
|
|
77
|
+
const getActiveClient = () => {
|
|
78
|
+
if (!activeClient) {
|
|
79
|
+
throw new ASCError('There is no active client', 800000 /* UNKNOWN_ERROR */, "fatal" /* FATAL */);
|
|
80
|
+
}
|
|
81
|
+
return activeClient;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Sets the active client
|
|
85
|
+
*
|
|
86
|
+
* @param client the client to assume as currently active client
|
|
87
|
+
*
|
|
88
|
+
* @hidden
|
|
89
|
+
*/
|
|
90
|
+
const setActiveClient = (client) => {
|
|
91
|
+
activeClient = client;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Pull objects from the cache layer
|
|
96
|
+
*
|
|
97
|
+
* @param name The name of the store to query
|
|
98
|
+
* @param keys A collection of unique keys for the store
|
|
99
|
+
* @returns The collection of objects matching the keys. Keys which don't match object will be omitted
|
|
100
|
+
*
|
|
101
|
+
* @category Cache
|
|
102
|
+
*/
|
|
103
|
+
const pullFromCache = (name, ...keys) => {
|
|
104
|
+
var _a;
|
|
105
|
+
const { cache } = getActiveClient();
|
|
106
|
+
const store = cache === null || cache === void 0 ? void 0 : cache[name];
|
|
107
|
+
if (!cache || !store)
|
|
108
|
+
return;
|
|
109
|
+
const state = (_a = store.read()) !== null && _a !== void 0 ? _a : {};
|
|
110
|
+
// return all
|
|
111
|
+
if (!(keys === null || keys === void 0 ? void 0 : keys.length))
|
|
112
|
+
return Object.values(state);
|
|
113
|
+
// return some
|
|
114
|
+
return keys
|
|
115
|
+
.map(key => {
|
|
116
|
+
const localId = cache.lookup[`${name}#${key}`];
|
|
117
|
+
return state[localId];
|
|
118
|
+
})
|
|
119
|
+
.filter(Boolean);
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Dictionary of resolver functions for each model
|
|
124
|
+
* @hidden
|
|
125
|
+
*/
|
|
126
|
+
const idResolvers = {
|
|
127
|
+
user: ({ userId }) => userId,
|
|
128
|
+
file: ({ fileId }) => fileId,
|
|
129
|
+
role: ({ roleId }) => roleId,
|
|
130
|
+
channel: ({ channelId }) => channelId,
|
|
131
|
+
channelUsers: ({ channelId, userId }) => `${channelId}#${userId}`,
|
|
132
|
+
message: ({ messageId }) => messageId,
|
|
133
|
+
community: ({ communityId }) => communityId,
|
|
134
|
+
communityUsers: ({ communityId, userId }) => `${communityId}#${userId}`,
|
|
135
|
+
post: ({ postId }) => postId,
|
|
136
|
+
comment: ({ commentId }) => commentId,
|
|
137
|
+
stream: ({ streamId }) => streamId,
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Retrieve the id resolver matching a domain name
|
|
141
|
+
*
|
|
142
|
+
* @param name the domain name for the resolve
|
|
143
|
+
* @returns an idResolver for the given domain name
|
|
144
|
+
*/
|
|
145
|
+
const getResolver = (name) => idResolvers[name];
|
|
146
|
+
/**
|
|
147
|
+
* A map of v3 response keys to a store name.
|
|
148
|
+
* @hidden
|
|
149
|
+
*/
|
|
150
|
+
const storeInterests = {
|
|
151
|
+
users: 'user',
|
|
152
|
+
files: 'file',
|
|
153
|
+
roles: 'role',
|
|
154
|
+
channels: 'channel',
|
|
155
|
+
channelUsers: 'channelUsers',
|
|
156
|
+
messages: 'message',
|
|
157
|
+
communities: 'community',
|
|
158
|
+
communityUsers: 'communityUsers',
|
|
159
|
+
posts: 'post',
|
|
160
|
+
postChildren: 'post',
|
|
161
|
+
comments: 'comment',
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Pull objects from the cache layer
|
|
166
|
+
*
|
|
167
|
+
* @param name The name of the store to query
|
|
168
|
+
* @param models A collection of models t store
|
|
169
|
+
*
|
|
170
|
+
* @category Cache
|
|
171
|
+
*/
|
|
172
|
+
const pushToCache = (name, ...models) => {
|
|
173
|
+
const { cache } = getActiveClient();
|
|
174
|
+
const store = cache === null || cache === void 0 ? void 0 : cache[name];
|
|
175
|
+
if (!cache || !store)
|
|
176
|
+
return;
|
|
177
|
+
const filtered = models.filter(Boolean);
|
|
178
|
+
if (!filtered.length)
|
|
179
|
+
return;
|
|
180
|
+
const synced = Object.fromEntries(filtered.map(model => {
|
|
181
|
+
var _a;
|
|
182
|
+
var _b, _c;
|
|
183
|
+
const modelId = getResolver(name)(model);
|
|
184
|
+
const localId = ((_a = (_b = cache.lookup)[_c = `${name}#${modelId}`]) !== null && _a !== void 0 ? _a : (_b[_c] = model.localId));
|
|
185
|
+
// WARNING: shallow immutable
|
|
186
|
+
return [localId, Object.assign(Object.assign({
|
|
187
|
+
// @ts-ignore: syncedAt is before "...model" so it can be manually overridden
|
|
188
|
+
syncedAt: Date.now() }, model), { localId })];
|
|
189
|
+
}));
|
|
190
|
+
store.write(synced);
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Pull objects from the cache layer
|
|
195
|
+
*
|
|
196
|
+
* @param name The name of the store to query
|
|
197
|
+
* @param keys A collection of unique keys for the store
|
|
198
|
+
* @returns The collection of objects matching the keys. Keys which don't match object will be omitted
|
|
199
|
+
*
|
|
200
|
+
* @category Cache
|
|
201
|
+
*/
|
|
202
|
+
const dropFromCache = (name, partial) => {
|
|
203
|
+
const { cache } = getActiveClient();
|
|
204
|
+
const store = cache === null || cache === void 0 ? void 0 : cache[name];
|
|
205
|
+
if (!cache || !store)
|
|
206
|
+
return [];
|
|
207
|
+
const modelId = getResolver(name)(partial);
|
|
208
|
+
const localId = cache.lookup[`${name}#${modelId}`];
|
|
209
|
+
delete cache.lookup[`${name}#${modelId}`];
|
|
210
|
+
store.erase(localId);
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
/** hidden */
|
|
214
|
+
/** @hidden */
|
|
215
|
+
const CRITERIAS = {
|
|
216
|
+
'file': ['fileId', 'attributes'],
|
|
217
|
+
'user': ['userId', 'avatarFileId'],
|
|
218
|
+
'role': ['roleId'],
|
|
219
|
+
'channel': ['channelId', 'rateLimit'],
|
|
220
|
+
'channelUsers': ['channelId', 'userId', 'membership'],
|
|
221
|
+
'message': ['messageId'],
|
|
222
|
+
'community': ['communityId', 'onlyAdminCanPost'],
|
|
223
|
+
'communityUsers': ['userId', 'communityId', 'communityMembership'],
|
|
224
|
+
'post': ['postId', 'feedId'],
|
|
225
|
+
'comment': ['commentId', 'referenceId'],
|
|
226
|
+
'stream': ['streamId'],
|
|
227
|
+
};
|
|
228
|
+
/** @hidden */
|
|
229
|
+
const hasKeys = (object, needles) => {
|
|
230
|
+
const haystack = Object.keys(object);
|
|
231
|
+
return needles.every(needle => haystack.includes(needle));
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* Finds the name of the store for a given unknown model
|
|
235
|
+
*
|
|
236
|
+
* @param model one of the {@link Amity.Model} to identify
|
|
237
|
+
* @returns the name of the corresponding store
|
|
238
|
+
*/
|
|
239
|
+
const identifyModel = (model) => {
|
|
240
|
+
var _a;
|
|
241
|
+
const criterias = Object.entries(CRITERIAS);
|
|
242
|
+
return (_a = criterias.find(([_, keys]) => hasKeys(model, keys))) === null || _a === void 0 ? void 0 : _a[0];
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Sync-up local ids of objects in the cache
|
|
247
|
+
*
|
|
248
|
+
* @param original The object holding the localId to keep
|
|
249
|
+
* @param replacement The object holding the localId to discard
|
|
250
|
+
*
|
|
251
|
+
* @category Cache
|
|
252
|
+
*/
|
|
253
|
+
const syncInCache = (original, replacement) => {
|
|
254
|
+
var _a;
|
|
255
|
+
const { cache, log } = getActiveClient();
|
|
256
|
+
const name = (_a = identifyModel(original)) !== null && _a !== void 0 ? _a : identifyModel(replacement);
|
|
257
|
+
if (!name) {
|
|
258
|
+
log('syncInCache', 'could not identify the models', { original, replacement });
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const store = cache === null || cache === void 0 ? void 0 : cache[name];
|
|
262
|
+
if (!cache || !store)
|
|
263
|
+
return;
|
|
264
|
+
// remove the object from cache (it's cleaner this way)
|
|
265
|
+
dropFromCache(name, original);
|
|
266
|
+
dropFromCache(name, replacement);
|
|
267
|
+
// glue the localIds together
|
|
268
|
+
replacement.localId = original.localId;
|
|
269
|
+
// put back the replacement after (again, it's cleaner this way)
|
|
270
|
+
pushToCache(name, replacement);
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Ingest a payload (v3) into the cache
|
|
275
|
+
*
|
|
276
|
+
* @param payload a "backend v3" payload object
|
|
277
|
+
* @param order the order of the keys to process (in case of need)
|
|
278
|
+
*
|
|
279
|
+
* @category Cache
|
|
280
|
+
*/
|
|
281
|
+
const ingestInCache = (payload = {}, order = Object.keys(payload)) => {
|
|
282
|
+
order
|
|
283
|
+
.forEach(key => {
|
|
284
|
+
const name = storeInterests[key];
|
|
285
|
+
if (!name)
|
|
286
|
+
return;
|
|
287
|
+
pushToCache(name, ...payload[key]);
|
|
288
|
+
});
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Apply partial mutation to objects in the cache
|
|
293
|
+
*
|
|
294
|
+
* @param name The name of the store to query
|
|
295
|
+
* @param mutation A partial view of the object to update
|
|
296
|
+
*
|
|
297
|
+
* @category Cache
|
|
298
|
+
*/
|
|
299
|
+
const mutateCache = (name, key, mutator) => {
|
|
300
|
+
var _a;
|
|
301
|
+
const [model] = (_a = pullFromCache(name, key)) !== null && _a !== void 0 ? _a : [];
|
|
302
|
+
if (!model)
|
|
303
|
+
return;
|
|
304
|
+
const mutation = mutator(model);
|
|
305
|
+
mutation && pushToCache(name, Object.assign(Object.assign({}, model), mutation));
|
|
306
|
+
return mutation;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Apply partial mutation to objects in the cache
|
|
311
|
+
*
|
|
312
|
+
* @param name The name of the store to query
|
|
313
|
+
* @param mutation A partial view of the object to update
|
|
314
|
+
*
|
|
315
|
+
* @category Cache
|
|
316
|
+
*/
|
|
317
|
+
const mergeInCache = (name, mutation) => {
|
|
318
|
+
const key = getResolver(name)(mutation);
|
|
319
|
+
mutateCache(name, key, model => (Object.assign(Object.assign({}, model), mutation)));
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Creates a cache instance. The cache instance will handle stores
|
|
324
|
+
* given to it as input, and will be used as the first source of cached data
|
|
325
|
+
* in the SDK.
|
|
326
|
+
*
|
|
327
|
+
* @param stores A partial collection of stores
|
|
328
|
+
* @returns A cache instance
|
|
329
|
+
*
|
|
330
|
+
* @category Cache
|
|
331
|
+
*/
|
|
332
|
+
const createCache = (stores) => {
|
|
333
|
+
const lookup = {};
|
|
334
|
+
return Object.assign({ lookup }, stores);
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
// @ts-ignore
|
|
338
|
+
/**
|
|
339
|
+
* Type guard to find if the a given decoded backend token uses "skip/limit" pagination style
|
|
340
|
+
*
|
|
341
|
+
* @param json any json object as extracted from the backend
|
|
342
|
+
* @returns success boolean if the token has either "skip" or "limit" props
|
|
343
|
+
*
|
|
344
|
+
* @hidden
|
|
345
|
+
*/
|
|
346
|
+
const isSkipLimit = (json) => (['skip', 'limit'].some(prop => prop in json));
|
|
347
|
+
/**
|
|
348
|
+
* Type guard to find if the a given decoded backend token uses "after/before" pagination style
|
|
349
|
+
*
|
|
350
|
+
* @param json any json object as extracted from the backend
|
|
351
|
+
* @returns success boolean if the token has either "after" or "before" props
|
|
352
|
+
*
|
|
353
|
+
* @hidden
|
|
354
|
+
*/
|
|
355
|
+
const isAfterBefore = (json) => (['after', 'before', 'first', 'last'].some(prop => prop in json));
|
|
356
|
+
/**
|
|
357
|
+
* Type guard to find if the a given object is wrapped around Amity.Paged<>
|
|
358
|
+
*
|
|
359
|
+
* @param payload any object as passed from query functions
|
|
360
|
+
* @returns success boolean if the object is an array shaped as [Record<string, Amity.Syncable>, Amity.Pages]
|
|
361
|
+
*
|
|
362
|
+
* @hidden
|
|
363
|
+
*/
|
|
364
|
+
const isPaged = (payload) => {
|
|
365
|
+
return ((payload === null || payload === void 0 ? void 0 : payload.hasOwnProperty('data'))
|
|
366
|
+
&& (payload === null || payload === void 0 ? void 0 : payload.hasOwnProperty('nextPage'))
|
|
367
|
+
&& (payload === null || payload === void 0 ? void 0 : payload.hasOwnProperty('prevPage')));
|
|
368
|
+
};
|
|
369
|
+
/**
|
|
370
|
+
* Converts a paging object into a b64 string token
|
|
371
|
+
*
|
|
372
|
+
* @param paging the sdk-friendly paging object
|
|
373
|
+
* @param style the style of token to produce
|
|
374
|
+
* @returns a backend's b64 encoded token
|
|
375
|
+
*
|
|
376
|
+
* @hidden
|
|
377
|
+
*/
|
|
378
|
+
const toToken = (paging, style) => {
|
|
379
|
+
var _a;
|
|
380
|
+
if (!paging || !Object.keys(paging).length)
|
|
381
|
+
return;
|
|
382
|
+
let payload = {};
|
|
383
|
+
// TODO: refactor this clean
|
|
384
|
+
if (style === 'skiplimit') {
|
|
385
|
+
payload = {
|
|
386
|
+
skip: (_a = paging.after) !== null && _a !== void 0 ? _a : 0,
|
|
387
|
+
limit: paging.limit,
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
else if (style === 'afterbefore') {
|
|
391
|
+
/*
|
|
392
|
+
Caution: this testing style is only valid because
|
|
393
|
+
our backend expects nothing else than a number as
|
|
394
|
+
"before" or "after" value. if that would change,
|
|
395
|
+
we'd need to move toward a more simple: `!paging.before`
|
|
396
|
+
*/
|
|
397
|
+
if (!isNaN(paging === null || paging === void 0 ? void 0 : paging.before)) {
|
|
398
|
+
payload = {
|
|
399
|
+
before: paging.before,
|
|
400
|
+
last: paging.limit,
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
else if (!isNaN(paging === null || paging === void 0 ? void 0 : paging.after)) {
|
|
404
|
+
payload = {
|
|
405
|
+
after: paging.after,
|
|
406
|
+
first: paging.limit,
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
// avoid sending {} as it seems backend flips when we do
|
|
411
|
+
if (!Object.keys(payload).length)
|
|
412
|
+
return;
|
|
413
|
+
// don't try catch, let it throw
|
|
414
|
+
return encode(JSON.stringify(payload));
|
|
415
|
+
};
|
|
416
|
+
/**
|
|
417
|
+
* Converts a b64 string token into a paging object
|
|
418
|
+
*
|
|
419
|
+
* @param token the backend's b64 encoded token
|
|
420
|
+
* @returns a sdk-friendly paging object
|
|
421
|
+
*
|
|
422
|
+
* @hidden
|
|
423
|
+
*/
|
|
424
|
+
const toPage = (token) => {
|
|
425
|
+
if (!token)
|
|
426
|
+
return null;
|
|
427
|
+
// don't try catch, let it throw
|
|
428
|
+
const json = JSON.parse(decode(token));
|
|
429
|
+
if (isSkipLimit(json)) {
|
|
430
|
+
return {
|
|
431
|
+
after: json.skip,
|
|
432
|
+
limit: json.limit,
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
else if (isAfterBefore(json)) {
|
|
436
|
+
if ('before' in json) {
|
|
437
|
+
return {
|
|
438
|
+
before: json.before,
|
|
439
|
+
limit: json.last,
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
else if ('after' in json) {
|
|
443
|
+
return {
|
|
444
|
+
after: json.after,
|
|
445
|
+
limit: json.first,
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return null;
|
|
450
|
+
};
|
|
451
|
+
/**
|
|
452
|
+
* Slices a collection according to its paging
|
|
453
|
+
*
|
|
454
|
+
* @param type the type of collection (for unique id identification with after/before)
|
|
455
|
+
* @param collection the collection to slice
|
|
456
|
+
* @param paging the paging object
|
|
457
|
+
* @returns the sliced collection according to the paging definition
|
|
458
|
+
*
|
|
459
|
+
* @hidden
|
|
460
|
+
*/
|
|
461
|
+
const sliceByPaging = (type, collection, page) => {
|
|
462
|
+
console.log('slice by paging', page);
|
|
463
|
+
if (!page)
|
|
464
|
+
return collection.slice();
|
|
465
|
+
let start = 0;
|
|
466
|
+
let end = undefined;
|
|
467
|
+
// next page behavior
|
|
468
|
+
if (page.after) {
|
|
469
|
+
start = page.after;
|
|
470
|
+
end = start + page.limit;
|
|
471
|
+
// prev page behavior
|
|
472
|
+
}
|
|
473
|
+
else if (page.before) {
|
|
474
|
+
end = page.before + 1;
|
|
475
|
+
start = end > page.limit ? end - page.limit : 0;
|
|
476
|
+
}
|
|
477
|
+
const paged = collection.slice(start, end);
|
|
478
|
+
return paged.length === page.limit ? paged : undefined;
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Type guard to check and cast that a given async function is has the ".locally" property
|
|
483
|
+
*
|
|
484
|
+
* @param func any SDK APi function
|
|
485
|
+
* @returns success boolean if the function has a 'locally' twin
|
|
486
|
+
*
|
|
487
|
+
* @hidden
|
|
488
|
+
*/
|
|
489
|
+
const isFetcher = (func) => ('locally' in func);
|
|
490
|
+
/**
|
|
491
|
+
* Type guard to check and cast that a given async function is has the ".optimistically" property
|
|
492
|
+
*
|
|
493
|
+
* @param func any SDK APi function
|
|
494
|
+
* @returns success boolean if the function has an 'optimistically' twin
|
|
495
|
+
*
|
|
496
|
+
* @hidden
|
|
497
|
+
*/
|
|
498
|
+
const isMutator = (func) => ('optimistically' in func);
|
|
499
|
+
/**
|
|
500
|
+
* Type guard to check and cast that a given async function is has
|
|
501
|
+
* the ".locally" or ".optimistically" property
|
|
502
|
+
*
|
|
503
|
+
* @param func any SDK APi function
|
|
504
|
+
* @returns success boolean if the function has an offline twin
|
|
505
|
+
*
|
|
506
|
+
* @hidden
|
|
507
|
+
*/
|
|
508
|
+
const isOffline = (func) => isFetcher(func) || isMutator(func);
|
|
509
|
+
/**
|
|
510
|
+
* Type guard to check and cast that a given object has the "syncedAt" property
|
|
511
|
+
*
|
|
512
|
+
* @param model any object to check on
|
|
513
|
+
* @returns success boolean if the object has property "syncedAt"
|
|
514
|
+
*
|
|
515
|
+
* @hidden
|
|
516
|
+
*/
|
|
517
|
+
const isSyncable = (model) => (model === null || model === void 0 ? void 0 : model.hasOwnProperty('syncedAt'));
|
|
518
|
+
/**
|
|
519
|
+
* Checks if a model is considered local (syncedAt === -1)
|
|
520
|
+
*
|
|
521
|
+
* @param model any syncable object to check
|
|
522
|
+
* @returns success boolean if the object is marked as local
|
|
523
|
+
*
|
|
524
|
+
* @hidden
|
|
525
|
+
*/
|
|
526
|
+
const isLocal = (model) => {
|
|
527
|
+
return isSyncable(model) && (model === null || model === void 0 ? void 0 : model.syncedAt) === -1;
|
|
528
|
+
};
|
|
529
|
+
/**
|
|
530
|
+
* Checks if a model is considered fresh
|
|
531
|
+
*
|
|
532
|
+
* @param model any syncable object to check
|
|
533
|
+
* @param lifeSpan the supposedly duration for which the object is considered synced
|
|
534
|
+
* @returns success boolean if the object is below the given lifespan
|
|
535
|
+
*
|
|
536
|
+
* @hidden
|
|
537
|
+
*/
|
|
538
|
+
const isFresh = (model, lifeSpan = 0) => {
|
|
539
|
+
var _a;
|
|
540
|
+
return Date.now() - ((_a = model === null || model === void 0 ? void 0 : model.syncedAt) !== null && _a !== void 0 ? _a : 0) <= lifeSpan;
|
|
541
|
+
};
|
|
542
|
+
/**
|
|
543
|
+
* Checks if a model is considered synced
|
|
544
|
+
*
|
|
545
|
+
* @param model any syncable object to check
|
|
546
|
+
* @param lifeSpan the supposedly duration for which the object is considered synced
|
|
547
|
+
* @returns success boolean if the object is local or below the given lifespan
|
|
548
|
+
*
|
|
549
|
+
* @hidden
|
|
550
|
+
*/
|
|
551
|
+
const isSynced = (model, lifeSpan = 0) => {
|
|
552
|
+
return isLocal(model) || isFresh(model, lifeSpan);
|
|
553
|
+
};
|
|
554
|
+
/**
|
|
555
|
+
* ```js
|
|
556
|
+
* import { createQuery, getUser } from '@amityco/ts-sdk'
|
|
557
|
+
* const query = createQuery(getUser, 'foobar')
|
|
558
|
+
* ```
|
|
559
|
+
*
|
|
560
|
+
* Creates a wrapper for the API call you wish to call.
|
|
561
|
+
* This wrapper is necessary to create for optimistically calls
|
|
562
|
+
*
|
|
563
|
+
*
|
|
564
|
+
* @param fn A compatible API function from the ts sdk
|
|
565
|
+
* @param args The arguments to pass to the function passed as `fn`
|
|
566
|
+
* @returns A wrapper containing both the function and its future arguments
|
|
567
|
+
*
|
|
568
|
+
* @category Query
|
|
569
|
+
*/
|
|
570
|
+
const createQuery = (func, ...args) => ({ func, args });
|
|
571
|
+
/**
|
|
572
|
+
* ```js
|
|
573
|
+
* import { queryOptions } from '@amityco/ts-sdk'
|
|
574
|
+
* const options = queryOptions('no_fetch', lifeSpan)
|
|
575
|
+
* ```
|
|
576
|
+
*
|
|
577
|
+
* Creates a query options object based on the query policy passed
|
|
578
|
+
*
|
|
579
|
+
* @param policy The policy to apply to a query
|
|
580
|
+
* @returns A properly set query options object
|
|
581
|
+
*
|
|
582
|
+
* @category Query
|
|
583
|
+
*/
|
|
584
|
+
const queryOptions = (policy, lifeSpan = 2 * 60 * 1000) => {
|
|
585
|
+
if (policy === 'cache_only')
|
|
586
|
+
return { lifeSpan: Infinity };
|
|
587
|
+
else if (policy === 'server_only')
|
|
588
|
+
return { lifeSpan: 0 };
|
|
589
|
+
return { lifeSpan };
|
|
590
|
+
};
|
|
591
|
+
/**
|
|
592
|
+
* Checks if an unknown shaped payload is considered empty or not.
|
|
593
|
+
* Since the payload can be wrapped around [] or {} (query or get many),
|
|
594
|
+
* we need a smarter definition of what's considered "empty".
|
|
595
|
+
*
|
|
596
|
+
* @param local the unknown object to check for emptiness
|
|
597
|
+
* @returns true if the mixed-shape "local" content is considered empty
|
|
598
|
+
*
|
|
599
|
+
* @hidden
|
|
600
|
+
*/
|
|
601
|
+
const isEmpty = (local) => {
|
|
602
|
+
// if it's supposed to be a single object, it'd be undefined
|
|
603
|
+
if (!local)
|
|
604
|
+
return true;
|
|
605
|
+
// it's a paged query, the 1st cell of the array would be empty
|
|
606
|
+
else if (isPaged(local) && isEmpty(local.data)) {
|
|
607
|
+
return true;
|
|
608
|
+
}
|
|
609
|
+
// if it's a item collection, it'd have no keys
|
|
610
|
+
else if (!Object.keys(local).length)
|
|
611
|
+
return true;
|
|
612
|
+
return false;
|
|
613
|
+
};
|
|
614
|
+
/**
|
|
615
|
+
* Checks if an unknown shaped payload is considered synced or not.
|
|
616
|
+
* The object will first be identified by 3 types of shapes:
|
|
617
|
+
*
|
|
618
|
+
* 1. Amity.Paged<Amity.Syncable>
|
|
619
|
+
* 2. Amity.Syncable
|
|
620
|
+
* 3. Record<string, Amity.Syncable>
|
|
621
|
+
*
|
|
622
|
+
* then the "Syncable" component will be extracted and evaluated against
|
|
623
|
+
* the query options' lifeSpan value.
|
|
624
|
+
*
|
|
625
|
+
* @param local the unknown object to check for freshness
|
|
626
|
+
* @param options query options (for lifeSpan checking)
|
|
627
|
+
* @returns true if the mixed-shape "local" content is considered fresh
|
|
628
|
+
*
|
|
629
|
+
* @hidden
|
|
630
|
+
*/
|
|
631
|
+
const shouldAbort = (local, options) => {
|
|
632
|
+
// it's a paged query
|
|
633
|
+
if (isPaged(local) && shouldAbort(local.data, options)) {
|
|
634
|
+
return true;
|
|
635
|
+
}
|
|
636
|
+
// if it's object let's try to see if it's Amity.Syncable first
|
|
637
|
+
else if (isSynced(local, options.lifeSpan)) {
|
|
638
|
+
return true;
|
|
639
|
+
}
|
|
640
|
+
// otherwise consider as Record<string, Amity.Syncable>
|
|
641
|
+
else if (Object.values(local).length > 0 &&
|
|
642
|
+
Object.values(local).every(item => isSynced(item, options.lifeSpan))) {
|
|
643
|
+
return true;
|
|
644
|
+
}
|
|
645
|
+
return false;
|
|
646
|
+
};
|
|
647
|
+
/**
|
|
648
|
+
* ```js
|
|
649
|
+
* import { createQuery, getUser, runQuery } from '@amityco/ts-sdk'
|
|
650
|
+
* const query = createQuery(getUser, client, 'foobar')
|
|
651
|
+
* runQuery(query, user => console.log(user))
|
|
652
|
+
* ```
|
|
653
|
+
*
|
|
654
|
+
* Calls an API function wrapped around a Amity.Query, and executes the callback whenever
|
|
655
|
+
* a value is available. The value can be picked either from the local cache and/or
|
|
656
|
+
* from the server afterwards depending on the query options passed
|
|
657
|
+
*
|
|
658
|
+
* @param query A query object wrapping the call to be made
|
|
659
|
+
* @param callback A function to execute when a value is available
|
|
660
|
+
* @param options the query options
|
|
661
|
+
*
|
|
662
|
+
* @category Query
|
|
663
|
+
*/
|
|
664
|
+
const runQuery = ({ func, args }, callback, options = queryOptions('cache_then_server')) => {
|
|
665
|
+
if (!isOffline(func) || options.lifeSpan === 0) {
|
|
666
|
+
callback === null || callback === void 0 ? void 0 : callback(wrap(undefined, {
|
|
667
|
+
origin: 'server',
|
|
668
|
+
loading: true
|
|
669
|
+
}));
|
|
670
|
+
func(...args)
|
|
671
|
+
.then(fresh => {
|
|
672
|
+
callback === null || callback === void 0 ? void 0 : callback(wrap(fresh, {
|
|
673
|
+
origin: 'server',
|
|
674
|
+
loading: false,
|
|
675
|
+
}));
|
|
676
|
+
})
|
|
677
|
+
.catch(error => {
|
|
678
|
+
callback === null || callback === void 0 ? void 0 : callback(wrap(undefined, {
|
|
679
|
+
error,
|
|
680
|
+
loading: false,
|
|
681
|
+
}));
|
|
682
|
+
});
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
const local = isMutator(func)
|
|
686
|
+
? func.optimistically(...args)
|
|
687
|
+
: func.locally(...args);
|
|
688
|
+
if (!isEmpty(local)) {
|
|
689
|
+
callback === null || callback === void 0 ? void 0 : callback(wrap(local, { origin: 'local' }));
|
|
690
|
+
}
|
|
691
|
+
if (local && isFetcher(func) && shouldAbort(local, options))
|
|
692
|
+
return;
|
|
693
|
+
callback === null || callback === void 0 ? void 0 : callback(wrap(local, {
|
|
694
|
+
origin: 'local',
|
|
695
|
+
loading: true,
|
|
696
|
+
}));
|
|
697
|
+
func(...args)
|
|
698
|
+
.then(fresh => {
|
|
699
|
+
// @ts-ignore i know.
|
|
700
|
+
isLocal(local) && syncInCache(local, fresh);
|
|
701
|
+
callback === null || callback === void 0 ? void 0 : callback(wrap(fresh, {
|
|
702
|
+
origin: 'server',
|
|
703
|
+
loading: false
|
|
704
|
+
}));
|
|
705
|
+
})
|
|
706
|
+
.catch(error => {
|
|
707
|
+
callback === null || callback === void 0 ? void 0 : callback(wrap(local, {
|
|
708
|
+
error,
|
|
709
|
+
loading: false,
|
|
710
|
+
}));
|
|
711
|
+
});
|
|
712
|
+
};
|
|
713
|
+
/**
|
|
714
|
+
*
|
|
715
|
+
* @param data
|
|
716
|
+
* @param meta
|
|
717
|
+
* @returns
|
|
718
|
+
*/
|
|
719
|
+
const wrap = (data, meta = {}) => {
|
|
720
|
+
if (isPaged(data))
|
|
721
|
+
return Object.assign(Object.assign({}, meta), data);
|
|
722
|
+
return Object.assign(Object.assign({}, meta), { data });
|
|
723
|
+
};
|
|
724
|
+
|
|
725
|
+
const uuid = () => nativeIdGenerator.v4().toString();
|
|
726
|
+
|
|
727
|
+
/**
|
|
728
|
+
* Creates a pre-configured logger instance
|
|
729
|
+
*
|
|
730
|
+
* @param session The name of the debug session
|
|
731
|
+
* @returns A pre-configured logger function
|
|
732
|
+
*
|
|
733
|
+
* @category Debug
|
|
734
|
+
* @hidden
|
|
735
|
+
*/
|
|
736
|
+
const createLogger = (session) => {
|
|
737
|
+
const active = (`${session === null || session === void 0 ? void 0 : session.trim()}`.length > 0
|
|
738
|
+
&& process.env.NODE_ENV !== 'production');
|
|
739
|
+
const logger = debug(session);
|
|
740
|
+
const print = (topic, ...args) => {
|
|
741
|
+
active && logger(topic, args);
|
|
742
|
+
};
|
|
743
|
+
print.__instance__ = logger;
|
|
744
|
+
return print;
|
|
745
|
+
};
|
|
746
|
+
|
|
747
|
+
/**
|
|
748
|
+
* Axios param serializer (for querystring)
|
|
749
|
+
* @hidden
|
|
750
|
+
*/
|
|
751
|
+
const stringify = (params, prefix) => {
|
|
752
|
+
return Object.entries(params)
|
|
753
|
+
.filter(([, value]) => value !== undefined)
|
|
754
|
+
.map(([name, value]) => {
|
|
755
|
+
const key = prefix ? `${prefix}[${name}]` : name;
|
|
756
|
+
if (Array.isArray(value)) {
|
|
757
|
+
return value.map(item => `${key}[]=${item}`).join('&');
|
|
758
|
+
}
|
|
759
|
+
else if (typeof value !== 'object') {
|
|
760
|
+
return `${key}=${value}`;
|
|
761
|
+
}
|
|
762
|
+
return stringify(value, prefix !== null && prefix !== void 0 ? prefix : name);
|
|
763
|
+
})
|
|
764
|
+
.join('&');
|
|
765
|
+
};
|
|
766
|
+
/**
|
|
767
|
+
* Creates a pre-configured axios instance
|
|
768
|
+
*
|
|
769
|
+
* @param endpoint The ASC rest api server's URL
|
|
770
|
+
* @returns A pre-configured axios instance
|
|
771
|
+
*
|
|
772
|
+
* @category Transport
|
|
773
|
+
* @hidden
|
|
774
|
+
*/
|
|
775
|
+
const createHttpTransport = (endpoint = 'https://api.amity.co') => {
|
|
776
|
+
const options = {
|
|
777
|
+
maxSockets: 100,
|
|
778
|
+
maxFreeSockets: 10,
|
|
779
|
+
timeout: 60000,
|
|
780
|
+
freeSocketTimeout: 30000,
|
|
781
|
+
};
|
|
782
|
+
const instance = axios.create({
|
|
783
|
+
baseURL: endpoint,
|
|
784
|
+
httpAgent: new HttpAgent(options),
|
|
785
|
+
httpsAgent: new HttpsAgent(options),
|
|
786
|
+
paramsSerializer: stringify,
|
|
787
|
+
});
|
|
788
|
+
instance.interceptors.response.use((response) => response, (error) => {
|
|
789
|
+
var _a;
|
|
790
|
+
const { response } = error;
|
|
791
|
+
console.error((_a = response === null || response === void 0 ? void 0 : response.data) !== null && _a !== void 0 ? _a : error);
|
|
792
|
+
if (response)
|
|
793
|
+
unwrapPayload(response.data);
|
|
794
|
+
});
|
|
795
|
+
return instance;
|
|
796
|
+
};
|
|
797
|
+
/**
|
|
798
|
+
* Creates a pre-configured socket.io instance
|
|
799
|
+
*
|
|
800
|
+
* @param endpoint The socket.io server's URL
|
|
801
|
+
* @returns A pre-configured (non-connected) socket.io client instance
|
|
802
|
+
*
|
|
803
|
+
* @category Transport
|
|
804
|
+
* @hidden
|
|
805
|
+
*/
|
|
806
|
+
const createWebsocketTransport = (endpoint = 'https://api.ekomedia.technology') => {
|
|
807
|
+
const socket = io(endpoint, {
|
|
808
|
+
autoConnect: false,
|
|
809
|
+
reconnectionDelay: 2000,
|
|
810
|
+
transports: ['websocket'],
|
|
811
|
+
});
|
|
812
|
+
socket.on('disconnect', (reason) => {
|
|
813
|
+
if (reason === 'io server disconnect')
|
|
814
|
+
socket.connect();
|
|
815
|
+
});
|
|
816
|
+
['error', 'connect_error', 'connect_timeout', 'reconnect_error', 'reconnect_failed'].forEach(event => {
|
|
817
|
+
socket.on(event, (error) => {
|
|
818
|
+
throw new ASCConnectionError(`socket.io(${event}): ${error.message}`);
|
|
819
|
+
});
|
|
820
|
+
});
|
|
821
|
+
return socket;
|
|
822
|
+
};
|
|
823
|
+
/**
|
|
824
|
+
* Unwraps v3 server payload (for error handling and type extraction)
|
|
825
|
+
*
|
|
826
|
+
* @param response A typical "v3" response (related to backend's service strategy)
|
|
827
|
+
*
|
|
828
|
+
* @returns The data returned by the backend
|
|
829
|
+
* @throws An error related to backend's rejection
|
|
830
|
+
*
|
|
831
|
+
* @category Transport
|
|
832
|
+
* @hidden
|
|
833
|
+
*/
|
|
834
|
+
const unwrapPayload = (response) => {
|
|
835
|
+
if (response.status === 'success') {
|
|
836
|
+
return response.data;
|
|
837
|
+
}
|
|
838
|
+
else if (response.status === 'fail') {
|
|
839
|
+
throw new ASCUnknownError(response.code);
|
|
840
|
+
}
|
|
841
|
+
else if (response.status === 'error') {
|
|
842
|
+
throw new ASCApiError(response.message, response.code, "error" /* ERROR */);
|
|
843
|
+
}
|
|
844
|
+
// unjust, but helps force uncast "T | undefined" to allow deconstruction without warnings
|
|
845
|
+
return undefined;
|
|
846
|
+
};
|
|
847
|
+
/**
|
|
848
|
+
* Promisify a websocket event emission - resulting in a synchronous http-like XHR (ws legacy)
|
|
849
|
+
*
|
|
850
|
+
* @param client The current client for which to send the event with
|
|
851
|
+
* @param event The websocket event name
|
|
852
|
+
* @param data The event's payload
|
|
853
|
+
*
|
|
854
|
+
* @returns The data returned by the backend
|
|
855
|
+
* @throws An error related to backend's rejection
|
|
856
|
+
*
|
|
857
|
+
* @category Transport
|
|
858
|
+
* @async
|
|
859
|
+
* @hidden
|
|
860
|
+
*/
|
|
861
|
+
const synchronousWSCall = async (client, event, data) => {
|
|
862
|
+
const { ws } = client;
|
|
863
|
+
const value = await new Promise((resolve, reject) => {
|
|
864
|
+
ws.emit(event, data, (response) => {
|
|
865
|
+
try {
|
|
866
|
+
resolve(unwrapPayload(response));
|
|
867
|
+
}
|
|
868
|
+
catch (error) {
|
|
869
|
+
reject(error);
|
|
870
|
+
}
|
|
871
|
+
});
|
|
872
|
+
});
|
|
873
|
+
return value;
|
|
874
|
+
};
|
|
875
|
+
|
|
876
|
+
// need to sync up the list
|
|
877
|
+
const EVENTS = [
|
|
878
|
+
'disconnected',
|
|
879
|
+
'v3.user.didUpdate',
|
|
880
|
+
'v3.channel.didUpdate',
|
|
881
|
+
'v3.channel.didUpdate',
|
|
882
|
+
'v3.channel.didJoin',
|
|
883
|
+
'v3.channel.didLeave',
|
|
884
|
+
'v3.channel.didAddUsers',
|
|
885
|
+
'v3.channel.didRemoveUsers',
|
|
886
|
+
'v3.channel.didBan',
|
|
887
|
+
'v3.channel.didUnban',
|
|
888
|
+
'v3.message.didCreate',
|
|
889
|
+
'v3.message.didUpdate',
|
|
890
|
+
'v3.message.didDelete',
|
|
891
|
+
];
|
|
892
|
+
/** @hidden */
|
|
893
|
+
const createEventEmitter = () => {
|
|
894
|
+
return mitt();
|
|
895
|
+
};
|
|
896
|
+
/**
|
|
897
|
+
* Wraps the websocket events into the client's event emitter for
|
|
898
|
+
* abstraction of transport.
|
|
899
|
+
*
|
|
900
|
+
* @param ws A websocket connection to listen from
|
|
901
|
+
* @param emitter An event emitter to wire the events to
|
|
902
|
+
*
|
|
903
|
+
* @category Transport
|
|
904
|
+
* @hidden
|
|
905
|
+
*/
|
|
906
|
+
const proxyWebsocketEvents = (ws, emitter) => {
|
|
907
|
+
EVENTS.forEach(event => {
|
|
908
|
+
ws.on(event, (param) => emitter.emit(event, param));
|
|
909
|
+
});
|
|
910
|
+
};
|
|
911
|
+
/**
|
|
912
|
+
* Standardize the subscription of SSE through web sockets
|
|
913
|
+
*
|
|
914
|
+
* @param client The current client for which to subscribe the event to
|
|
915
|
+
* @param namespace A unique name for the logger
|
|
916
|
+
* @param event The websocket event name
|
|
917
|
+
* @param fn A wrapper for the callback.
|
|
918
|
+
* @returns A dispose function to unsubscribe to the event
|
|
919
|
+
*
|
|
920
|
+
* @category Transport
|
|
921
|
+
* @hidden
|
|
922
|
+
*/
|
|
923
|
+
const createEventSubscriber = (client, namespace, event, fn) => {
|
|
924
|
+
const { log, emitter } = client;
|
|
925
|
+
const timestamp = Date.now();
|
|
926
|
+
log(`${namespace}(tmpid: ${timestamp}) > listen`);
|
|
927
|
+
const handler = (...payload) => {
|
|
928
|
+
log(`${namespace}(tmpid: ${timestamp}) > trigger`, payload);
|
|
929
|
+
fn(...payload);
|
|
930
|
+
};
|
|
931
|
+
emitter.on(event, handler);
|
|
932
|
+
return () => {
|
|
933
|
+
log(`${namespace}(tmpid: ${timestamp}) > dispose`);
|
|
934
|
+
emitter.off(event, handler);
|
|
935
|
+
};
|
|
936
|
+
};
|
|
937
|
+
/**
|
|
938
|
+
* Wrapper around dispatch event
|
|
939
|
+
*
|
|
940
|
+
* @hidden
|
|
941
|
+
*/
|
|
942
|
+
const fireEvent = (event, payload) => {
|
|
943
|
+
const { emitter } = getActiveClient();
|
|
944
|
+
setTimeout(() => { emitter.emit(event, payload); }, 0);
|
|
945
|
+
};
|
|
946
|
+
|
|
947
|
+
/**
|
|
948
|
+
* ```js
|
|
949
|
+
* import { createClient } from '@amityco/ts-sdk'
|
|
950
|
+
* const client = createClient(apiKey, 'https://asc.server/', 'myClient')
|
|
951
|
+
* ```
|
|
952
|
+
*
|
|
953
|
+
* Creates a new {@link Amity.Client} instance
|
|
954
|
+
*
|
|
955
|
+
* @param apiKey for the {@link Amity.Client} instance
|
|
956
|
+
* @param apiEndpoint endpoint to connect to
|
|
957
|
+
* @param debugSession session's identifier for the client's logger instance
|
|
958
|
+
* @returns A {@link Amity.Client} instance
|
|
959
|
+
*
|
|
960
|
+
* @category Client API
|
|
961
|
+
**/
|
|
962
|
+
const createClient = (apiKey, apiEndpoint = 'https://api.amity.co', debugSession = 'amity') => {
|
|
963
|
+
const log = createLogger(debugSession);
|
|
964
|
+
log('client/api/createClient', {
|
|
965
|
+
apiKey: apiKey.replace(/.{5}$/g, 'xxxxx'),
|
|
966
|
+
apiEndpoint,
|
|
967
|
+
});
|
|
968
|
+
const http = createHttpTransport(apiEndpoint);
|
|
969
|
+
const ws = createWebsocketTransport(apiEndpoint);
|
|
970
|
+
const emitter = createEventEmitter();
|
|
971
|
+
const client = {
|
|
972
|
+
apiKey, log, http, ws, emitter,
|
|
973
|
+
use: () => setActiveClient(client),
|
|
974
|
+
};
|
|
975
|
+
try {
|
|
976
|
+
getActiveClient();
|
|
977
|
+
}
|
|
978
|
+
catch (_a) {
|
|
979
|
+
setActiveClient(client);
|
|
980
|
+
}
|
|
981
|
+
return client;
|
|
982
|
+
};
|
|
983
|
+
|
|
984
|
+
// TODO: find a way to uniquely identify the device
|
|
985
|
+
// import { getStorage } from './storage'
|
|
986
|
+
/** @hidden */
|
|
987
|
+
const VERSION = '11.0.0';
|
|
988
|
+
/** @hidden */
|
|
989
|
+
const getDeviceId = () => {
|
|
990
|
+
// here maybe put some auto detection of the device id based on if it's react or not?
|
|
991
|
+
// anyhow we let consumer branch out if they want, but it doesn't mean we shouldn't try to be nice
|
|
992
|
+
// and do things automatically
|
|
993
|
+
// https://www.npmjs.com/package/react-native-device-info#getuniqueid
|
|
994
|
+
// how to make sure that the react native package will not be included when build is for web
|
|
995
|
+
return `ascWebSdk#${Math.random().toString(16)}`;
|
|
996
|
+
};
|
|
997
|
+
/** @hidden */
|
|
998
|
+
const getDeviceModel = () => {
|
|
999
|
+
var _a, _b;
|
|
1000
|
+
if ((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node)
|
|
1001
|
+
return process.versions.node;
|
|
1002
|
+
if (navigator)
|
|
1003
|
+
return `${navigator.product.toLowerCase()}#${(_b = navigator.userAgent) !== null && _b !== void 0 ? _b : 'unknown_agent'}`;
|
|
1004
|
+
return 'unknown_model';
|
|
1005
|
+
};
|
|
1006
|
+
/** @hidden */
|
|
1007
|
+
const getDeviceInfo = () => {
|
|
1008
|
+
const model = getDeviceModel();
|
|
1009
|
+
return {
|
|
1010
|
+
version: VERSION,
|
|
1011
|
+
// TODO: replace with: kind: process.versions ? 'node' : 'web', when backend has fixed this
|
|
1012
|
+
kind: 'web',
|
|
1013
|
+
model,
|
|
1014
|
+
};
|
|
1015
|
+
};
|
|
1016
|
+
|
|
1017
|
+
/**
|
|
1018
|
+
* Retrieves a pair of {@link Amity.Tokens} necessary for connection
|
|
1019
|
+
*
|
|
1020
|
+
* @param userId The userId to use to issue a token
|
|
1021
|
+
* @param displayName The user's displayName
|
|
1022
|
+
* @param deviceId The user's device Id (can be manually set for native users)
|
|
1023
|
+
* @param authToken The authentication token - necessary when network option is set to secure
|
|
1024
|
+
* @return The accessToken for the given userId
|
|
1025
|
+
*
|
|
1026
|
+
* @category Client API
|
|
1027
|
+
* @hidden
|
|
1028
|
+
*/
|
|
1029
|
+
const getToken = async (params) => {
|
|
1030
|
+
const client = getActiveClient();
|
|
1031
|
+
client.log('client/api/getToken', params);
|
|
1032
|
+
const deviceInfo = await getDeviceInfo();
|
|
1033
|
+
const { data } = await client.http.post('/v1/device', Object.assign(Object.assign({}, params), { deviceInfo }), {
|
|
1034
|
+
headers: { 'X-API-Key': client.apiKey }
|
|
1035
|
+
});
|
|
1036
|
+
return data;
|
|
1037
|
+
};
|
|
1038
|
+
|
|
1039
|
+
/**
|
|
1040
|
+
* ```js
|
|
1041
|
+
* import { connectClient } from '@amityco/ts-sdk/client/api'
|
|
1042
|
+
* const success = await connectClient({
|
|
1043
|
+
* userId: 'XYZ123456789',
|
|
1044
|
+
* })
|
|
1045
|
+
* ```
|
|
1046
|
+
*
|
|
1047
|
+
* Connects an {@link Amity.Client} instance to ASC servers
|
|
1048
|
+
*
|
|
1049
|
+
* @param userId the user ID for the current session
|
|
1050
|
+
* @param displayName the user's displayName for the current session
|
|
1051
|
+
* @param deviceId Manual override of the user's device id (for device management)
|
|
1052
|
+
* @param authToken The authentication token - necessary when network option is set to secure
|
|
1053
|
+
* @returns a success boolean if connected
|
|
1054
|
+
*
|
|
1055
|
+
* @category Client API
|
|
1056
|
+
* @async
|
|
1057
|
+
*/
|
|
1058
|
+
const connectClient = async (params) => {
|
|
1059
|
+
var _a, _b;
|
|
1060
|
+
const client = getActiveClient();
|
|
1061
|
+
client.log('client/api/connectClient', Object.assign({ apiKey: client.apiKey }, params));
|
|
1062
|
+
// default values
|
|
1063
|
+
(_a = params.displayName) !== null && _a !== void 0 ? _a : (params.displayName = params.userId);
|
|
1064
|
+
(_b = params.deviceId) !== null && _b !== void 0 ? _b : (params.deviceId = getDeviceId());
|
|
1065
|
+
// @ts-ignore: in that particular case, typing can be
|
|
1066
|
+
// ignored since we enforce default values just before.
|
|
1067
|
+
const { accessToken } = await getToken(params);
|
|
1068
|
+
// manually setup the token for http transport
|
|
1069
|
+
client.http.defaults.headers.common['Authorization'] = `Bearer ${accessToken}`;
|
|
1070
|
+
// manually setup the token for ws transport
|
|
1071
|
+
client.ws.io.opts.query = { token: accessToken };
|
|
1072
|
+
// wire websocket events to our event emitter
|
|
1073
|
+
proxyWebsocketEvents(client.ws, client.emitter);
|
|
1074
|
+
// TODO: in phase 2, this should not be necessary. we should have WS
|
|
1075
|
+
// to be connected "on demand" rather than "by default"
|
|
1076
|
+
await new Promise(resolve => {
|
|
1077
|
+
client.ws.once('connect', resolve);
|
|
1078
|
+
client.ws.open();
|
|
1079
|
+
});
|
|
1080
|
+
client.userId = params.userId;
|
|
1081
|
+
return client.ws.connected;
|
|
1082
|
+
};
|
|
1083
|
+
|
|
1084
|
+
/**
|
|
1085
|
+
* ```js
|
|
1086
|
+
* import { disconnectClient } from '@amityco/ts-sdk'
|
|
1087
|
+
* const success = await disconnectClient()
|
|
1088
|
+
* ```
|
|
1089
|
+
*
|
|
1090
|
+
* Disconnects an {@link Amity.Client} instance from ASC servers
|
|
1091
|
+
*
|
|
1092
|
+
* @param client The {@link Amity.Client} instance to disconnect
|
|
1093
|
+
* @returns a success boolean if disconnected
|
|
1094
|
+
*
|
|
1095
|
+
* @category Client API
|
|
1096
|
+
* @async
|
|
1097
|
+
*/
|
|
1098
|
+
const disconnectClient = async () => {
|
|
1099
|
+
const client = getActiveClient();
|
|
1100
|
+
client.log('client/api/disconnectClient');
|
|
1101
|
+
await new Promise(resolve => {
|
|
1102
|
+
client.ws.once('disconnect', resolve);
|
|
1103
|
+
client.ws.disconnect();
|
|
1104
|
+
});
|
|
1105
|
+
client.emitter.all.clear();
|
|
1106
|
+
client.ws.removeAllListeners();
|
|
1107
|
+
client.userId = undefined;
|
|
1108
|
+
return !client.ws.connected;
|
|
1109
|
+
};
|
|
1110
|
+
|
|
1111
|
+
/**
|
|
1112
|
+
* ```js
|
|
1113
|
+
* import { isConnected } from '@amityco/ts-sdk'
|
|
1114
|
+
* const connected = isConnected()
|
|
1115
|
+
* ```
|
|
1116
|
+
*
|
|
1117
|
+
* Checks if a {@link Amity.Client} instance is connected to ASC servers
|
|
1118
|
+
*
|
|
1119
|
+
* @returns a success boolean if connected
|
|
1120
|
+
*
|
|
1121
|
+
* @category Client API
|
|
1122
|
+
*/
|
|
1123
|
+
const isConnected = () => {
|
|
1124
|
+
var _a, _b;
|
|
1125
|
+
const client = getActiveClient();
|
|
1126
|
+
client.log('client/api/isConnected', client);
|
|
1127
|
+
return !!(client.userId
|
|
1128
|
+
&& ((_b = (_a = client.http.defaults.headers.common) === null || _a === void 0 ? void 0 : _a['Authorization']) === null || _b === void 0 ? void 0 : _b.length)
|
|
1129
|
+
&& client.ws.connected);
|
|
1130
|
+
};
|
|
1131
|
+
|
|
1132
|
+
/**
|
|
1133
|
+
* Minimal store implementation.
|
|
1134
|
+
*
|
|
1135
|
+
* Beware, the state is mutable! it is your responsibility
|
|
1136
|
+
* handle the state's mutations correctly. We do so in
|
|
1137
|
+
* order for users with mutable/observable stacks to be able
|
|
1138
|
+
* to decorate the state with their favorite libraries.
|
|
1139
|
+
*
|
|
1140
|
+
* @param defaultValue the hydration state value of the store
|
|
1141
|
+
* @returns A store instance with an "in memory" storage strategy
|
|
1142
|
+
*
|
|
1143
|
+
* @category Storage
|
|
1144
|
+
* @hidden
|
|
1145
|
+
*/
|
|
1146
|
+
const memoryStore = (defaultValue = {}) => {
|
|
1147
|
+
const state = defaultValue;
|
|
1148
|
+
/* default read operation is just to return the state */
|
|
1149
|
+
const read = () => state;
|
|
1150
|
+
/* default write operation is just to mutate the state */
|
|
1151
|
+
const write = (payload) => {
|
|
1152
|
+
Object.assign(state, payload);
|
|
1153
|
+
};
|
|
1154
|
+
/* default write operation is just to mutate the state */
|
|
1155
|
+
const erase = (...keys) => {
|
|
1156
|
+
if (!keys.length)
|
|
1157
|
+
keys = Object.keys(state);
|
|
1158
|
+
keys.forEach(key => delete state[key]);
|
|
1159
|
+
};
|
|
1160
|
+
return { read, write, erase };
|
|
1161
|
+
};
|
|
1162
|
+
/**
|
|
1163
|
+
* ```js
|
|
1164
|
+
* import { memoryStores } from '@amityco/core/cache/store/volatile'
|
|
1165
|
+
* const stores = imemoryStores('user', 'file')
|
|
1166
|
+
* ```
|
|
1167
|
+
*
|
|
1168
|
+
* Builds a collection of in memory stores based on the available store names
|
|
1169
|
+
*
|
|
1170
|
+
* @param names collection of store names
|
|
1171
|
+
* @returns a dictionary of { storeName: storeInstance } according to the given names
|
|
1172
|
+
*
|
|
1173
|
+
* @category Storage
|
|
1174
|
+
*/
|
|
1175
|
+
const memoryStores = (...names) => {
|
|
1176
|
+
return names.reduce((acc, name) => {
|
|
1177
|
+
const store = memoryStore();
|
|
1178
|
+
return Object.assign(Object.assign({}, acc), { [name]: store });
|
|
1179
|
+
}, {});
|
|
1180
|
+
};
|
|
1181
|
+
|
|
1182
|
+
const DEFAULT_DEBOUNCE_TIME = 250;
|
|
1183
|
+
/**
|
|
1184
|
+
* Persistent store implementation.
|
|
1185
|
+
*
|
|
1186
|
+
* @param persistentKey the name of the store in persistent storage
|
|
1187
|
+
* @param defaultValue the hydration state value of the store
|
|
1188
|
+
* @param debounceTime a delay to avoid spamming the persistent storage while writing,
|
|
1189
|
+
* @returns A store instance with an "persistent" storage strategy
|
|
1190
|
+
*
|
|
1191
|
+
* @category Storage
|
|
1192
|
+
* @hidden
|
|
1193
|
+
*/
|
|
1194
|
+
const persistentStore = async (persistentKey, defaultValue = {}, debounceTime = DEFAULT_DEBOUNCE_TIME) => {
|
|
1195
|
+
let initialValue = defaultValue;
|
|
1196
|
+
const serializedBackup = await AsyncStorage.getItem(persistentKey);
|
|
1197
|
+
if (serializedBackup) {
|
|
1198
|
+
try {
|
|
1199
|
+
initialValue = JSON.parse(serializedBackup);
|
|
1200
|
+
}
|
|
1201
|
+
catch (err) { }
|
|
1202
|
+
}
|
|
1203
|
+
// create a store
|
|
1204
|
+
const store = memoryStore(initialValue);
|
|
1205
|
+
let timer;
|
|
1206
|
+
let job = null;
|
|
1207
|
+
const backup = () => {
|
|
1208
|
+
if (job) {
|
|
1209
|
+
job.then(() => backup());
|
|
1210
|
+
return;
|
|
1211
|
+
}
|
|
1212
|
+
clearTimeout(timer);
|
|
1213
|
+
setTimeout(async () => {
|
|
1214
|
+
job = Promise.resolve()
|
|
1215
|
+
.then(() => AsyncStorage.setItem(persistentKey, JSON.stringify(store.read())))
|
|
1216
|
+
.then(() => job = null);
|
|
1217
|
+
}, debounceTime);
|
|
1218
|
+
};
|
|
1219
|
+
const write = (payload) => {
|
|
1220
|
+
store.write(payload);
|
|
1221
|
+
backup();
|
|
1222
|
+
};
|
|
1223
|
+
const erase = (...keys) => {
|
|
1224
|
+
store.erase(...keys);
|
|
1225
|
+
backup();
|
|
1226
|
+
};
|
|
1227
|
+
return { read: store.read, write, erase };
|
|
1228
|
+
};
|
|
1229
|
+
|
|
1230
|
+
/**
|
|
1231
|
+
* ```js
|
|
1232
|
+
* import { enableCache } from '@amityco/ts-sdk'
|
|
1233
|
+
* enableCache()
|
|
1234
|
+
* ```
|
|
1235
|
+
*
|
|
1236
|
+
* Adds a new {@link Amity.Cache} object to an {@link Amity.Client} instance
|
|
1237
|
+
*
|
|
1238
|
+
* @param stores a partial dictionary of stores to be used for caching
|
|
1239
|
+
*
|
|
1240
|
+
* @category Client API
|
|
1241
|
+
*/
|
|
1242
|
+
const enableCache = (stores = memoryStores('user', 'file', 'channel', 'message', 'community', 'post', 'comment', 'communityUsers', 'channelUsers')) => {
|
|
1243
|
+
const client = getActiveClient();
|
|
1244
|
+
client.log('client/api/enableCache');
|
|
1245
|
+
client.cache = createCache(stores);
|
|
1246
|
+
};
|
|
1247
|
+
|
|
1248
|
+
/**
|
|
1249
|
+
* ```js
|
|
1250
|
+
* import { clearCache } from '@amityco/ts-sdk'
|
|
1251
|
+
* clearCache()
|
|
1252
|
+
* ```
|
|
1253
|
+
*
|
|
1254
|
+
* Resets the state of the given {@link Amity.Store} names within
|
|
1255
|
+
* the {@link Amity.Cache} object of a {@link Amity.Client} instance
|
|
1256
|
+
*
|
|
1257
|
+
* @param stores an array of the stores names to be wiped
|
|
1258
|
+
*
|
|
1259
|
+
* @category Client API
|
|
1260
|
+
*/
|
|
1261
|
+
const clearCache = (stores = ['user', 'file', 'channel', 'message', 'community', 'post', 'comment']) => {
|
|
1262
|
+
const client = getActiveClient();
|
|
1263
|
+
client.log('client/api/enableCache');
|
|
1264
|
+
stores.forEach(store => { var _a, _b; return (_b = (_a = client.cache) === null || _a === void 0 ? void 0 : _a[store]) === null || _b === void 0 ? void 0 : _b.erase(); });
|
|
1265
|
+
};
|
|
1266
|
+
|
|
1267
|
+
/**
|
|
1268
|
+
* converts an array looking collection to an object collection, grouped by localId
|
|
1269
|
+
*
|
|
1270
|
+
* @param array array of {@link Amity.Syncable}
|
|
1271
|
+
* @returns array converted to object, grouped by localId
|
|
1272
|
+
*
|
|
1273
|
+
* @hidden
|
|
1274
|
+
*/
|
|
1275
|
+
const toRecord = (array) => (array.reduce((acc, item) => (Object.assign(Object.assign({}, acc), { [item.localId]: item })), {}));
|
|
1276
|
+
|
|
1277
|
+
/**
|
|
1278
|
+
* Set the model's syncedAt property and localId
|
|
1279
|
+
*
|
|
1280
|
+
* @param model the model to decorate
|
|
1281
|
+
* @param timestamp the given timestamp
|
|
1282
|
+
*/
|
|
1283
|
+
const decoratePayload = (payload, timestamp = Date.now(), keyMapping = storeInterests) => {
|
|
1284
|
+
Object.fromEntries(Object.entries(payload)
|
|
1285
|
+
.map(([key, models]) => ([
|
|
1286
|
+
key, decorateModels(models, timestamp, keyMapping[key])
|
|
1287
|
+
])));
|
|
1288
|
+
};
|
|
1289
|
+
/**
|
|
1290
|
+
* Set the model's syncedAt property and localId
|
|
1291
|
+
*
|
|
1292
|
+
* @param model the model to decorate
|
|
1293
|
+
* @param timestamp the given timestamp
|
|
1294
|
+
*/
|
|
1295
|
+
const decorateModels = (models, timestamp = Date.now(), type) => {
|
|
1296
|
+
models.forEach(model => decorateModel(model, timestamp, type));
|
|
1297
|
+
};
|
|
1298
|
+
/**
|
|
1299
|
+
* Set the model's syncedAt property and localId
|
|
1300
|
+
*
|
|
1301
|
+
* @param model the model to decorate
|
|
1302
|
+
* @param syncedAt the given timestamp
|
|
1303
|
+
* @param type An opportunity to manually pass the type. If undefined, it will be smartly computed
|
|
1304
|
+
*/
|
|
1305
|
+
const decorateModel = (model, timestamp = Date.now(), type = identifyModel(model)) => {
|
|
1306
|
+
var _a, _b;
|
|
1307
|
+
(_a = model.syncedAt) !== null && _a !== void 0 ? _a : (model.syncedAt = timestamp);
|
|
1308
|
+
const { cache } = getActiveClient();
|
|
1309
|
+
if (type) {
|
|
1310
|
+
const modelId = getResolver(type)(model);
|
|
1311
|
+
model.localId = (_b = cache === null || cache === void 0 ? void 0 : cache.lookup[`${type}#${modelId}`]) !== null && _b !== void 0 ? _b : uuid();
|
|
1312
|
+
}
|
|
1313
|
+
};
|
|
1314
|
+
|
|
1315
|
+
// Note:
|
|
1316
|
+
// this file should contain a suite of filtering utilities to help the
|
|
1317
|
+
// local version of the query functions.
|
|
1318
|
+
/**
|
|
1319
|
+
* Filter a given collection with strict equality against a param
|
|
1320
|
+
*
|
|
1321
|
+
* @param collection the collection to filter
|
|
1322
|
+
* @param key the key of the collection's items to challenge
|
|
1323
|
+
* @param value the expected value
|
|
1324
|
+
* @returns a filtered collection with items only matching the criteria
|
|
1325
|
+
*
|
|
1326
|
+
* @hidden
|
|
1327
|
+
*/
|
|
1328
|
+
const filterByPropEquality = (collection, key, value) => (value !== undefined ? collection.filter(item => item[key] === value) : collection);
|
|
1329
|
+
|
|
1330
|
+
// Note:
|
|
1331
|
+
// this file should contain a suite of sorting utilities to help the
|
|
1332
|
+
// local version of the query functions.
|
|
1333
|
+
/**
|
|
1334
|
+
* Alphabetic sorting of objects having a displayName
|
|
1335
|
+
*/
|
|
1336
|
+
const sortByDisplayName = ({ displayName: a }, { displayName: b }) => {
|
|
1337
|
+
if (a === b)
|
|
1338
|
+
return 0;
|
|
1339
|
+
return a < b ? -1 : 1;
|
|
1340
|
+
};
|
|
1341
|
+
/**
|
|
1342
|
+
* Sorting a collection by their apparition order (oldest first)
|
|
1343
|
+
*/
|
|
1344
|
+
const sortByChannelSegment = ({ channelSegment: a }, { channelSegment: b }) => a - b;
|
|
1345
|
+
/**
|
|
1346
|
+
* Sorting a collection by their apparition order (oldest first)
|
|
1347
|
+
*/
|
|
1348
|
+
const sortBySegmentNumber = ({ segmentNumber: a }, { segmentNumber: b }) => a - b;
|
|
1349
|
+
/**
|
|
1350
|
+
* Sorting a collection by its oldest items
|
|
1351
|
+
*/
|
|
1352
|
+
const sortByFirstCreated = ({ createdAt: a }, { createdAt: b }) => new Date(a).valueOf() - new Date(b).valueOf();
|
|
1353
|
+
/**
|
|
1354
|
+
* Sorting a collection by its newest items
|
|
1355
|
+
*/
|
|
1356
|
+
const sortByLastCreated = ({ createdAt: a }, { createdAt: b }) => new Date(b).valueOf() - new Date(a).valueOf();
|
|
1357
|
+
/**
|
|
1358
|
+
* Sorting a collection by the items with most recent activity
|
|
1359
|
+
*/
|
|
1360
|
+
const sortByLastActivity = ({ lastActivity: a }, { lastActivity: b }) => new Date(b).valueOf() - new Date(a).valueOf();
|
|
1361
|
+
|
|
1362
|
+
/**
|
|
1363
|
+
* ```js
|
|
1364
|
+
* import { onClientDisconnected } from '@amityco/ts-sdk'
|
|
1365
|
+
* const dispose = onClientDisconnected(reason => {
|
|
1366
|
+
* // ...
|
|
1367
|
+
* })
|
|
1368
|
+
* ```
|
|
1369
|
+
*
|
|
1370
|
+
* Fired when any {@link Amity.Client} have been disconnected
|
|
1371
|
+
*
|
|
1372
|
+
* @param callback The function to call when the event was fired
|
|
1373
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1374
|
+
*
|
|
1375
|
+
* @category Client Events
|
|
1376
|
+
*/
|
|
1377
|
+
const onClientDisconnected = (callback) => {
|
|
1378
|
+
return createEventSubscriber(getActiveClient(), 'client/onClientDisconnected', 'disconnected', () => callback());
|
|
1379
|
+
};
|
|
1380
|
+
|
|
1381
|
+
/**
|
|
1382
|
+
* ```js
|
|
1383
|
+
* import { getUser } from '@amityco/ts-sdk'
|
|
1384
|
+
* const user = await getUser('foobar')
|
|
1385
|
+
* ```
|
|
1386
|
+
*
|
|
1387
|
+
* Fetches a {@link Amity.User} object
|
|
1388
|
+
*
|
|
1389
|
+
* @param userId the ID of the {@link Amity.User} to fetch
|
|
1390
|
+
* @returns the associated {@link Amity.User} object
|
|
1391
|
+
*
|
|
1392
|
+
* @category User API
|
|
1393
|
+
* @async
|
|
1394
|
+
*/
|
|
1395
|
+
const getUser = async (userId) => {
|
|
1396
|
+
const client = getActiveClient();
|
|
1397
|
+
client.log('user/getUser', userId);
|
|
1398
|
+
// API-FIX: endpoint should not be /list, parameters should be querystring.
|
|
1399
|
+
const { data } = await client.http.get(`/api/v3/users/${userId}`);
|
|
1400
|
+
decoratePayload(data);
|
|
1401
|
+
ingestInCache(data);
|
|
1402
|
+
const { users } = data;
|
|
1403
|
+
return users.find(user => user.userId === userId);
|
|
1404
|
+
};
|
|
1405
|
+
/**
|
|
1406
|
+
* ```js
|
|
1407
|
+
* import { getUser } from '@amityco/ts-sdk'
|
|
1408
|
+
* const user = getUser.locally('foobar')
|
|
1409
|
+
* ```
|
|
1410
|
+
*
|
|
1411
|
+
* Fetches a {@link Amity.User} object from cache
|
|
1412
|
+
*
|
|
1413
|
+
* @param userId the ID of the {@link Amity.User} to fetch
|
|
1414
|
+
* @returns the associated {@link Amity.User} object
|
|
1415
|
+
*
|
|
1416
|
+
* @category User API
|
|
1417
|
+
*/
|
|
1418
|
+
getUser.locally = (userId) => {
|
|
1419
|
+
var _a;
|
|
1420
|
+
const client = getActiveClient();
|
|
1421
|
+
client.log('user/getUser.locally', userId);
|
|
1422
|
+
const [user] = (_a = pullFromCache('user', userId)) !== null && _a !== void 0 ? _a : [];
|
|
1423
|
+
return user;
|
|
1424
|
+
};
|
|
1425
|
+
|
|
1426
|
+
/**
|
|
1427
|
+
* ```js
|
|
1428
|
+
* import { getUsers } from '@amityco/ts-sdk'
|
|
1429
|
+
* const users = await getUsers(['foo', 'bar'])
|
|
1430
|
+
* ```
|
|
1431
|
+
*
|
|
1432
|
+
* Fetches a collection of {@link Amity.User} objects
|
|
1433
|
+
*
|
|
1434
|
+
* @param userIds the IDs of the {@link Amity.User} to fetch
|
|
1435
|
+
* @returns the associated collection of {@link Amity.User} objects
|
|
1436
|
+
*
|
|
1437
|
+
* @category User API
|
|
1438
|
+
* @async
|
|
1439
|
+
*/
|
|
1440
|
+
const getUsers = async (userIds) => {
|
|
1441
|
+
const client = getActiveClient();
|
|
1442
|
+
client.log('user/getUsers', userIds);
|
|
1443
|
+
// API-FIX: endpoint should not be /list, parameters should be querystring.
|
|
1444
|
+
const { data } = await client.http.get(`/api/v3/users/list`, { params: { userIds } });
|
|
1445
|
+
decoratePayload(data);
|
|
1446
|
+
ingestInCache(data);
|
|
1447
|
+
const { users } = data;
|
|
1448
|
+
return toRecord(users);
|
|
1449
|
+
};
|
|
1450
|
+
/**
|
|
1451
|
+
* ```js
|
|
1452
|
+
* import { getUsers } from '@amityco/ts-sdk'
|
|
1453
|
+
* const users = getUsers.locally!(['foo', 'bar'])
|
|
1454
|
+
* ```
|
|
1455
|
+
*
|
|
1456
|
+
* Fetches a collection of {@link Amity.User} objects from cache
|
|
1457
|
+
*
|
|
1458
|
+
* @param userIds the IDs of the {@link Amity.User} to fetch
|
|
1459
|
+
* @returns the associated collection of {@link Amity.User} objects
|
|
1460
|
+
*
|
|
1461
|
+
* @category User API
|
|
1462
|
+
*/
|
|
1463
|
+
getUsers.locally = (userIds) => {
|
|
1464
|
+
var _a;
|
|
1465
|
+
const cached = (_a = pullFromCache('user', ...userIds)) !== null && _a !== void 0 ? _a : [];
|
|
1466
|
+
if ((cached === null || cached === void 0 ? void 0 : cached.length) === userIds.length)
|
|
1467
|
+
return toRecord(cached);
|
|
1468
|
+
};
|
|
1469
|
+
|
|
1470
|
+
/*! *****************************************************************************
|
|
1471
|
+
Copyright (c) Microsoft Corporation.
|
|
1472
|
+
|
|
1473
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
1474
|
+
purpose with or without fee is hereby granted.
|
|
1475
|
+
|
|
1476
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
1477
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
1478
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
1479
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
1480
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
1481
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
1482
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
1483
|
+
***************************************************************************** */
|
|
1484
|
+
|
|
1485
|
+
function __rest(s, e) {
|
|
1486
|
+
var t = {};
|
|
1487
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
1488
|
+
t[p] = s[p];
|
|
1489
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
1490
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
1491
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
1492
|
+
t[p[i]] = s[p[i]];
|
|
1493
|
+
}
|
|
1494
|
+
return t;
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
/**
|
|
1498
|
+
* ```js
|
|
1499
|
+
* import { queryUsers } from '@amityco/ts-sdk'
|
|
1500
|
+
* const { users, previous, next } = await queryUsers({ keyword: 'foo' })
|
|
1501
|
+
* ```
|
|
1502
|
+
*
|
|
1503
|
+
* Queries a paginable list of {@link Amity.User} objects
|
|
1504
|
+
* Search is performed by displayName such as `.startsWith(search)`
|
|
1505
|
+
*
|
|
1506
|
+
* @param query The query parameters
|
|
1507
|
+
* @returns A page of {@link Amity.User} objects
|
|
1508
|
+
*
|
|
1509
|
+
* @category User API
|
|
1510
|
+
* @async
|
|
1511
|
+
*/
|
|
1512
|
+
const queryUsers = async (query) => {
|
|
1513
|
+
const client = getActiveClient();
|
|
1514
|
+
client.log('user/queryUsers', query);
|
|
1515
|
+
const _a = query !== null && query !== void 0 ? query : {}, { page, displayName, filter = 'all', sortBy = 'displayName' } = _a, params = __rest(_a, ["page", "displayName", "filter", "sortBy"]);
|
|
1516
|
+
// API-FIX: parameters should be querystring.
|
|
1517
|
+
const { data } = await client.http.get(`/api/v3/users`, { params: Object.assign(Object.assign({}, params), { keyword: displayName, filter,
|
|
1518
|
+
sortBy, options: {
|
|
1519
|
+
token: toToken(page, 'skiplimit'),
|
|
1520
|
+
} }) });
|
|
1521
|
+
// unpacking
|
|
1522
|
+
const { paging } = data, response = __rest(data, ["paging"]);
|
|
1523
|
+
decoratePayload(response);
|
|
1524
|
+
ingestInCache(response);
|
|
1525
|
+
const prevPage = toPage(paging.previous);
|
|
1526
|
+
const nextPage = toPage(paging.next);
|
|
1527
|
+
const { users } = response;
|
|
1528
|
+
return { data: toRecord(users), prevPage, nextPage };
|
|
1529
|
+
};
|
|
1530
|
+
/**
|
|
1531
|
+
* ```js
|
|
1532
|
+
* import { queryUsers } from '@amityco/ts-sdk'
|
|
1533
|
+
* const users = queryUsers.locally({ keyword: 'foo' })
|
|
1534
|
+
* ```
|
|
1535
|
+
*
|
|
1536
|
+
* Queries a paginable list of {@link Amity.User} objects from cache
|
|
1537
|
+
* Search is performed by displayName such as `.startsWith(search)`
|
|
1538
|
+
*
|
|
1539
|
+
* @param query The query parameters
|
|
1540
|
+
* @returns A page of {@link Amity.User} objects
|
|
1541
|
+
*
|
|
1542
|
+
* @category User API
|
|
1543
|
+
*/
|
|
1544
|
+
queryUsers.locally = (query) => {
|
|
1545
|
+
const client = getActiveClient();
|
|
1546
|
+
client.log('user/queryUsers.locally', query);
|
|
1547
|
+
// TODO
|
|
1548
|
+
};
|
|
1549
|
+
|
|
1550
|
+
/**
|
|
1551
|
+
* ```js
|
|
1552
|
+
* import { updateUser } from '@amityco/ts-sdk'
|
|
1553
|
+
* const updated = await updateUser(userId, { displayName: 'foobar' })
|
|
1554
|
+
* ```
|
|
1555
|
+
*
|
|
1556
|
+
* Updates an {@link Amity.User}
|
|
1557
|
+
*
|
|
1558
|
+
* @param userId The ID of the {@link Amity.User} to update
|
|
1559
|
+
* @param patch The patch data to apply
|
|
1560
|
+
* @returns the updated {@link Amity.User} object
|
|
1561
|
+
*
|
|
1562
|
+
* @category User API
|
|
1563
|
+
* @async
|
|
1564
|
+
*/
|
|
1565
|
+
const updateUser = async (userId, patch) => {
|
|
1566
|
+
const client = getActiveClient();
|
|
1567
|
+
client.log('user/updateUser', userId, patch);
|
|
1568
|
+
// API-FIX: Endpoint is malformed, userId should be a url param.
|
|
1569
|
+
/*
|
|
1570
|
+
const { data } = await client.http.put<Amity.Response<UserPayload>>(
|
|
1571
|
+
`/api/v3/users/${userId}`, patch,
|
|
1572
|
+
)
|
|
1573
|
+
*/
|
|
1574
|
+
const { data } = await client.http.put(`/api/v3/users/`, Object.assign(Object.assign({ userId }, patch), { createNewUserWhenNotFound: false }));
|
|
1575
|
+
decoratePayload(data);
|
|
1576
|
+
ingestInCache(data);
|
|
1577
|
+
// no need for event, fired by server
|
|
1578
|
+
return data.users.find(user => user.userId === userId);
|
|
1579
|
+
};
|
|
1580
|
+
|
|
1581
|
+
/**
|
|
1582
|
+
* ```js
|
|
1583
|
+
* import { onUserUpdated } from '@amityco/ts-sdk'
|
|
1584
|
+
* const dispose = onUserUpdated(user => {
|
|
1585
|
+
* // ...
|
|
1586
|
+
* })
|
|
1587
|
+
* ```
|
|
1588
|
+
*
|
|
1589
|
+
* Fired when any {@link Amity.User} have been updated
|
|
1590
|
+
*
|
|
1591
|
+
* @param callback The function to call when the event was fired
|
|
1592
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
1593
|
+
*
|
|
1594
|
+
* @category User Events
|
|
1595
|
+
*/
|
|
1596
|
+
const onUserUpdated = (callback) => {
|
|
1597
|
+
const client = getActiveClient();
|
|
1598
|
+
const filter = (payload) => {
|
|
1599
|
+
decoratePayload(payload);
|
|
1600
|
+
ingestInCache(payload);
|
|
1601
|
+
callback(payload.users[0]);
|
|
1602
|
+
};
|
|
1603
|
+
return createEventSubscriber(client, 'user/onUserUpdate', 'v3.user.didUpdate', filter);
|
|
1604
|
+
};
|
|
1605
|
+
|
|
1606
|
+
/**
|
|
1607
|
+
* ```js
|
|
1608
|
+
* import { observeUser } from '@amityco/ts-sdk'
|
|
1609
|
+
*
|
|
1610
|
+
* let user = {}
|
|
1611
|
+
* const dispose = observeUser(userId, updated => user = updated)
|
|
1612
|
+
* ```
|
|
1613
|
+
*
|
|
1614
|
+
* @param userId the ID of the user to observe
|
|
1615
|
+
* @param callback the function to call when new data are available
|
|
1616
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the user
|
|
1617
|
+
*
|
|
1618
|
+
* Observe all mutation on a given {@link Amity.User}
|
|
1619
|
+
* @category User Observer
|
|
1620
|
+
*/
|
|
1621
|
+
const observeUser = (userId, callback) => {
|
|
1622
|
+
const { log } = getActiveClient();
|
|
1623
|
+
const timestamp = Date.now();
|
|
1624
|
+
log(`observeUser(tmpid: ${timestamp}) > listen`);
|
|
1625
|
+
// wrapper function to make sure
|
|
1626
|
+
const router = (result, action) => {
|
|
1627
|
+
var _a, _b, _c;
|
|
1628
|
+
// filter function
|
|
1629
|
+
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.userId) !== userId)
|
|
1630
|
+
return;
|
|
1631
|
+
if (callback instanceof Function)
|
|
1632
|
+
return callback(result);
|
|
1633
|
+
if (action !== 'onFetch')
|
|
1634
|
+
(_b = callback.onEvent) === null || _b === void 0 ? void 0 : _b.call(callback, action, result);
|
|
1635
|
+
(_c = callback[action]) === null || _c === void 0 ? void 0 : _c.call(callback, result);
|
|
1636
|
+
};
|
|
1637
|
+
const disposers = [];
|
|
1638
|
+
disposers.push(onUserUpdated(data => router({ data }, 'onUpdate')));
|
|
1639
|
+
runQuery(createQuery(getUser, userId), (result) => result.data && router(result, 'onFetch'));
|
|
1640
|
+
return () => {
|
|
1641
|
+
log(`observeUser(tmpid: ${timestamp}) > dispose`);
|
|
1642
|
+
disposers.forEach(fn => fn());
|
|
1643
|
+
};
|
|
1644
|
+
};
|
|
1645
|
+
|
|
1646
|
+
/**
|
|
1647
|
+
* ```js
|
|
1648
|
+
* import { getFile } from '@amityco/ts-sdk'
|
|
1649
|
+
* const file = await getFile('foo')
|
|
1650
|
+
* ```
|
|
1651
|
+
*
|
|
1652
|
+
* Fetches a {@link Amity.File} object
|
|
1653
|
+
*
|
|
1654
|
+
* @param filelId the ID of the {@link Amity.File} to fetch
|
|
1655
|
+
* @returns the associated {@link Amity.File} object
|
|
1656
|
+
*
|
|
1657
|
+
* @category File API
|
|
1658
|
+
* @async
|
|
1659
|
+
*/
|
|
1660
|
+
const getFile = async (fileId) => {
|
|
1661
|
+
const client = getActiveClient();
|
|
1662
|
+
client.log('file/getFile', fileId);
|
|
1663
|
+
const { data } = await client.http.get(`/api/v3/files/${fileId}`);
|
|
1664
|
+
// API-FIX: backend bad serializer issue. it should be:
|
|
1665
|
+
// const { files } = data
|
|
1666
|
+
decorateModel(data);
|
|
1667
|
+
ingestInCache({ files: [data] });
|
|
1668
|
+
return data;
|
|
1669
|
+
};
|
|
1670
|
+
/**
|
|
1671
|
+
* ```js
|
|
1672
|
+
* import { getFile } from '@amityco/ts-sdk'
|
|
1673
|
+
* const file = getFile.locally('foo')
|
|
1674
|
+
* ```
|
|
1675
|
+
*
|
|
1676
|
+
* Fetches a {@link Amity.File} object from cache
|
|
1677
|
+
*
|
|
1678
|
+
* @param fileId the ID of the {@link Amity.File} to fetch
|
|
1679
|
+
* @returns the associated {@link Amity.File} object
|
|
1680
|
+
*
|
|
1681
|
+
* @category File API
|
|
1682
|
+
*/
|
|
1683
|
+
getFile.locally = (fileId) => {
|
|
1684
|
+
var _a;
|
|
1685
|
+
const client = getActiveClient();
|
|
1686
|
+
client.log('file/getFile.locally', fileId);
|
|
1687
|
+
const [file] = (_a = pullFromCache('file', fileId)) !== null && _a !== void 0 ? _a : [];
|
|
1688
|
+
return file;
|
|
1689
|
+
};
|
|
1690
|
+
|
|
1691
|
+
/**
|
|
1692
|
+
* ```js
|
|
1693
|
+
* import { createFile } from '@amityco/ts-sdk'
|
|
1694
|
+
* const created = await createFile(formData)
|
|
1695
|
+
* ```
|
|
1696
|
+
*
|
|
1697
|
+
* Creates an {@link Amity.File}
|
|
1698
|
+
*
|
|
1699
|
+
* @param formData The data necessary to create a new {@link Amity.File}
|
|
1700
|
+
* @returns The newly created {@link Amity.File}
|
|
1701
|
+
*
|
|
1702
|
+
* @category File API
|
|
1703
|
+
* @async
|
|
1704
|
+
*/
|
|
1705
|
+
const createFile = async (formData, onProgress) => {
|
|
1706
|
+
const client = getActiveClient();
|
|
1707
|
+
client.log('file/createFile', formData);
|
|
1708
|
+
if (!formData.getAll('file').length)
|
|
1709
|
+
throw new Error('The formData object must have a `file` key.');
|
|
1710
|
+
const headers = 'getHeaders' in formData
|
|
1711
|
+
? formData.getHeaders()
|
|
1712
|
+
: { 'content-type': 'multipart/form-data' };
|
|
1713
|
+
const { data } = await client.http.post('/api/v3/files', formData, {
|
|
1714
|
+
headers, onUploadProgress({ loaded, total }) {
|
|
1715
|
+
onProgress && onProgress(Math.round((loaded * 100) / total));
|
|
1716
|
+
}
|
|
1717
|
+
});
|
|
1718
|
+
// API-FIX: payload should be serialized properly
|
|
1719
|
+
// const { files } = data
|
|
1720
|
+
decorateModels(data);
|
|
1721
|
+
ingestInCache({ files: data });
|
|
1722
|
+
return data[0];
|
|
1723
|
+
};
|
|
1724
|
+
// TODO: consider doing local creation with URL.createObjectURL()
|
|
1725
|
+
|
|
1726
|
+
/**
|
|
1727
|
+
* ```js
|
|
1728
|
+
* import { deleteFile } from '@amityco/ts-sdk'
|
|
1729
|
+
* const success = await deleteFile('foo')
|
|
1730
|
+
* ```
|
|
1731
|
+
*
|
|
1732
|
+
* Deletes a {@link Amity.File}
|
|
1733
|
+
*
|
|
1734
|
+
* @param fileId The {@link Amity.File} ID to delete
|
|
1735
|
+
* @return A success boolean if the {@link Amity.File} was deleted
|
|
1736
|
+
*
|
|
1737
|
+
* @category File API
|
|
1738
|
+
* @async
|
|
1739
|
+
*/
|
|
1740
|
+
const deleteFile = async (fileId) => {
|
|
1741
|
+
const client = getActiveClient();
|
|
1742
|
+
client.log('file/deleteFile', fileId);
|
|
1743
|
+
const { data } = await client.http.delete(`/api/v3/files/${fileId}`);
|
|
1744
|
+
const { success } = data;
|
|
1745
|
+
if (success)
|
|
1746
|
+
mergeInCache('file', { fileId, isDeleted: true });
|
|
1747
|
+
return success;
|
|
1748
|
+
};
|
|
1749
|
+
|
|
1750
|
+
/**
|
|
1751
|
+
* Allow to query a file url with a selected size
|
|
1752
|
+
*
|
|
1753
|
+
* @param fileUrl the file Url to decorate
|
|
1754
|
+
* @param size one of the selected sizes
|
|
1755
|
+
* @returns the computed url for the selected size
|
|
1756
|
+
*/
|
|
1757
|
+
const fileUrlWithSize = (fileUrl, size) => {
|
|
1758
|
+
return `${fileUrl}?size=${size}`;
|
|
1759
|
+
};
|
|
1760
|
+
|
|
1761
|
+
/**
|
|
1762
|
+
* ```js
|
|
1763
|
+
* import { observeFile } from '@amityco/ts-sdk'
|
|
1764
|
+
*
|
|
1765
|
+
* let file = {}
|
|
1766
|
+
* const dispose = observeFile(fileId, updated => file = updated)
|
|
1767
|
+
* ```
|
|
1768
|
+
*
|
|
1769
|
+
* Observe all mutation on a given {@link Amity.File}
|
|
1770
|
+
*
|
|
1771
|
+
* @param fileId the ID of the file to observe
|
|
1772
|
+
* @param callback the function to call when new data are available
|
|
1773
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the file
|
|
1774
|
+
*
|
|
1775
|
+
* @category File Observer
|
|
1776
|
+
*/
|
|
1777
|
+
const observeFile = (fileId, callback) => {
|
|
1778
|
+
const { log } = getActiveClient();
|
|
1779
|
+
const timestamp = Date.now();
|
|
1780
|
+
log(`observeFile(tmpid: ${timestamp}) > listen`);
|
|
1781
|
+
// wrapper function to make sure
|
|
1782
|
+
const router = (result, action) => {
|
|
1783
|
+
var _a, _b, _c;
|
|
1784
|
+
// filter function
|
|
1785
|
+
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.fileId) !== fileId)
|
|
1786
|
+
return;
|
|
1787
|
+
if (callback instanceof Function)
|
|
1788
|
+
return callback(result);
|
|
1789
|
+
if (action !== 'onFetch')
|
|
1790
|
+
(_b = callback.onEvent) === null || _b === void 0 ? void 0 : _b.call(callback, action, result);
|
|
1791
|
+
(_c = callback[action]) === null || _c === void 0 ? void 0 : _c.call(callback, result);
|
|
1792
|
+
};
|
|
1793
|
+
runQuery(createQuery(getFile, fileId), (result) => result.data && router(result, 'onFetch'));
|
|
1794
|
+
return () => {
|
|
1795
|
+
log(`observeFile(tmpid: ${timestamp}) > dispose`);
|
|
1796
|
+
};
|
|
1797
|
+
};
|
|
1798
|
+
|
|
1799
|
+
/**
|
|
1800
|
+
* ```js
|
|
1801
|
+
* import { queryRoles } from '@amityco/ts-sdk'
|
|
1802
|
+
* const roles = await queryRoles()
|
|
1803
|
+
* ```
|
|
1804
|
+
*
|
|
1805
|
+
* Queries a paginable list of {@link Amity.Role} objects
|
|
1806
|
+
* Search is performed by displayName such as `.startsWith(search)`
|
|
1807
|
+
*
|
|
1808
|
+
* @param query The query parameters
|
|
1809
|
+
* @returns A page of {@link Amity.Role} objects
|
|
1810
|
+
*
|
|
1811
|
+
* @category Role API
|
|
1812
|
+
* @async
|
|
1813
|
+
*/
|
|
1814
|
+
const queryRoles = async (query) => {
|
|
1815
|
+
const client = getActiveClient();
|
|
1816
|
+
client.log('role/queryRoles', query);
|
|
1817
|
+
const _a = query !== null && query !== void 0 ? query : {}, { page, displayName, sortBy } = _a, params = __rest(_a, ["page", "displayName", "sortBy"]);
|
|
1818
|
+
// API-FIX: parameters should be querystring.
|
|
1819
|
+
const { data } = await client.http.get(`/api/v3/roles`, {
|
|
1820
|
+
params: Object.assign(Object.assign({}, params), { keyword: displayName, sortBy, options: {
|
|
1821
|
+
token: toToken(page, 'skiplimit'),
|
|
1822
|
+
} }),
|
|
1823
|
+
});
|
|
1824
|
+
// unpacking
|
|
1825
|
+
const { paging } = data, response = __rest(data, ["paging"]);
|
|
1826
|
+
decoratePayload(response);
|
|
1827
|
+
ingestInCache(response);
|
|
1828
|
+
const prevPage = toPage(paging.previous);
|
|
1829
|
+
const nextPage = toPage(paging.next);
|
|
1830
|
+
const { roles } = response;
|
|
1831
|
+
return { data: toRecord(roles), prevPage, nextPage };
|
|
1832
|
+
};
|
|
1833
|
+
/**
|
|
1834
|
+
* ```js
|
|
1835
|
+
* import { queryRoles } from '@amityco/ts-sdk'
|
|
1836
|
+
* const roles = queryRoles.locally({ keyword: 'foo' })
|
|
1837
|
+
* ```
|
|
1838
|
+
*
|
|
1839
|
+
* Queries a paginable list of {@link Amity.Role} objects from cache
|
|
1840
|
+
* Search is performed by displayName such as `.startsWith(search)`
|
|
1841
|
+
*
|
|
1842
|
+
* @param query The query parameters
|
|
1843
|
+
* @returns A page of {@link Amity.Role} objects
|
|
1844
|
+
*
|
|
1845
|
+
* @category Role API
|
|
1846
|
+
*/
|
|
1847
|
+
queryRoles.locally = (query) => {
|
|
1848
|
+
const client = getActiveClient();
|
|
1849
|
+
client.log('role/queryRoles.locally', query);
|
|
1850
|
+
// TODO
|
|
1851
|
+
};
|
|
1852
|
+
|
|
1853
|
+
/**
|
|
1854
|
+
* ```js
|
|
1855
|
+
* import { getRole } from '@amityco/ts-sdk'
|
|
1856
|
+
* const role = await getRole('foobar')
|
|
1857
|
+
* ```
|
|
1858
|
+
*
|
|
1859
|
+
* Fetches a {@link Amity.Role} object
|
|
1860
|
+
*
|
|
1861
|
+
* @param roleId the ID of the {@link Amity.Role} to fetch
|
|
1862
|
+
* @returns the associated {@link Amity.Role} object
|
|
1863
|
+
*
|
|
1864
|
+
* @category Role API
|
|
1865
|
+
* @async
|
|
1866
|
+
*/
|
|
1867
|
+
const getRole = async (roleId) => {
|
|
1868
|
+
const client = getActiveClient();
|
|
1869
|
+
client.log('role/getRole', roleId);
|
|
1870
|
+
const { data } = await client.http.get(`/api/v3/roles/${roleId}`);
|
|
1871
|
+
decorateModels(data);
|
|
1872
|
+
ingestInCache({ roles: data });
|
|
1873
|
+
return data.find(role => role.roleId === roleId);
|
|
1874
|
+
};
|
|
1875
|
+
/**
|
|
1876
|
+
* ```js
|
|
1877
|
+
* import { getRole } from '@amityco/ts-sdk'
|
|
1878
|
+
* const role = getRole.locally('foobar')
|
|
1879
|
+
* ```
|
|
1880
|
+
*
|
|
1881
|
+
* Fetches a {@link Amity.Role} object from cache
|
|
1882
|
+
*
|
|
1883
|
+
* @param roleId the ID of the {@link Amity.Role} to fetch
|
|
1884
|
+
* @returns the associated {@link Amity.Role} object
|
|
1885
|
+
*
|
|
1886
|
+
* @category Role API
|
|
1887
|
+
*/
|
|
1888
|
+
getRole.locally = (roleId) => {
|
|
1889
|
+
var _a;
|
|
1890
|
+
const client = getActiveClient();
|
|
1891
|
+
client.log('role/getRole.locally', roleId);
|
|
1892
|
+
const [role] = (_a = pullFromCache('role', roleId)) !== null && _a !== void 0 ? _a : [];
|
|
1893
|
+
return role;
|
|
1894
|
+
};
|
|
1895
|
+
|
|
1896
|
+
/** @hidden */
|
|
1897
|
+
const dispatchReactable = (referenceType, model) => {
|
|
1898
|
+
if (referenceType === 'message')
|
|
1899
|
+
// @ts-ignore: refactor later
|
|
1900
|
+
fireEvent('v3.message.didUpdate', { messages: [model] });
|
|
1901
|
+
else if (referenceType === 'post')
|
|
1902
|
+
// @ts-ignore: refactor later
|
|
1903
|
+
fireEvent('v3.post.didUpdate', { posts: [model] });
|
|
1904
|
+
else if (referenceType === 'comment')
|
|
1905
|
+
// @ts-ignore: refactor later
|
|
1906
|
+
fireEvent('v3.comment.didUpdate', { comments: [model] });
|
|
1907
|
+
};
|
|
1908
|
+
|
|
1909
|
+
/**
|
|
1910
|
+
* ```js
|
|
1911
|
+
* import { getMessage } from '@amityco/ts-sdk'
|
|
1912
|
+
* const message = await getMessage('foobar')
|
|
1913
|
+
* ```
|
|
1914
|
+
*
|
|
1915
|
+
* Fetches a {@link Amity.Message} object
|
|
1916
|
+
*
|
|
1917
|
+
* @param messageId the ID of the {@link Amity.Message} to fetch
|
|
1918
|
+
* @returns the associated {@link Amity.Message} object
|
|
1919
|
+
*
|
|
1920
|
+
* @category Message API
|
|
1921
|
+
* @async
|
|
1922
|
+
*/
|
|
1923
|
+
const getMessage = async (messageId) => {
|
|
1924
|
+
const client = getActiveClient();
|
|
1925
|
+
client.log('message/getMessage', messageId);
|
|
1926
|
+
// API-FIX: endpoint should not be /list, parameters should be querystring.
|
|
1927
|
+
const { data } = await client.http.get(`/api/v3/messages/${messageId}`);
|
|
1928
|
+
decoratePayload(data);
|
|
1929
|
+
ingestInCache(data);
|
|
1930
|
+
const { messages } = data;
|
|
1931
|
+
return messages.find(message => message.messageId === messageId);
|
|
1932
|
+
};
|
|
1933
|
+
/**
|
|
1934
|
+
* ```js
|
|
1935
|
+
* import { getMessage } from '@amityco/ts-sdk'
|
|
1936
|
+
* const message = getMessage.locally('foobar')
|
|
1937
|
+
* ```
|
|
1938
|
+
*
|
|
1939
|
+
* Fetches a {@link Amity.Message} object
|
|
1940
|
+
*
|
|
1941
|
+
* @param messageId the ID of the {@link Amity.Message} to fetch
|
|
1942
|
+
* @returns the associated {@link Amity.Message} object
|
|
1943
|
+
*
|
|
1944
|
+
* @category Message API
|
|
1945
|
+
*/
|
|
1946
|
+
getMessage.locally = (messageId) => {
|
|
1947
|
+
var _a;
|
|
1948
|
+
const client = getActiveClient();
|
|
1949
|
+
client.log('message/getMessage.locally', messageId);
|
|
1950
|
+
const [message] = (_a = pullFromCache('message', messageId)) !== null && _a !== void 0 ? _a : [];
|
|
1951
|
+
return message;
|
|
1952
|
+
};
|
|
1953
|
+
|
|
1954
|
+
/**
|
|
1955
|
+
* ```js
|
|
1956
|
+
* import { getMessages } from '@amityco/ts-sdk'
|
|
1957
|
+
* const messages = await getMessages(['foo', 'bar'])
|
|
1958
|
+
* ```
|
|
1959
|
+
*
|
|
1960
|
+
* Fetches a collection of {@link Amity.Message} objects
|
|
1961
|
+
*
|
|
1962
|
+
* @param messageIds the IDs of the {@link Amity.Message} to fetch
|
|
1963
|
+
* @returns the associated collection of {@link Amity.Message} objects
|
|
1964
|
+
*
|
|
1965
|
+
* @category Message API
|
|
1966
|
+
* @async
|
|
1967
|
+
*/
|
|
1968
|
+
const getMessages = async (messageIds) => {
|
|
1969
|
+
const client = getActiveClient();
|
|
1970
|
+
client.log('message/getMessages', messageIds);
|
|
1971
|
+
// API-FIX: endpoint should not be /list, parameters should be querystring.
|
|
1972
|
+
const { data } = await client.http.get(`/api/v3/messages/list`, { params: { messageIds } });
|
|
1973
|
+
decoratePayload(data);
|
|
1974
|
+
ingestInCache(data);
|
|
1975
|
+
const { messages } = data;
|
|
1976
|
+
return toRecord(messages);
|
|
1977
|
+
};
|
|
1978
|
+
/**
|
|
1979
|
+
* ```js
|
|
1980
|
+
* import { getMessages } from '@amityco/ts-sdk'
|
|
1981
|
+
* const messages = getMessages.locally(['foo', 'bar']) ?? []
|
|
1982
|
+
* ```
|
|
1983
|
+
*
|
|
1984
|
+
* Fetches a collection of {@link Amity.Message} objects from cache
|
|
1985
|
+
*
|
|
1986
|
+
* @param messageIds the IDs of the {@link Amity.Message} to fetch
|
|
1987
|
+
* @returns the associated collection of {@link Amity.Message} objects
|
|
1988
|
+
*
|
|
1989
|
+
* @category Message API
|
|
1990
|
+
*/
|
|
1991
|
+
getMessages.locally = (messageIds) => {
|
|
1992
|
+
var _a;
|
|
1993
|
+
const client = getActiveClient();
|
|
1994
|
+
client.log('message/getMessages.locally', messageIds);
|
|
1995
|
+
const cached = (_a = pullFromCache('message', ...messageIds)) !== null && _a !== void 0 ? _a : [];
|
|
1996
|
+
if ((cached === null || cached === void 0 ? void 0 : cached.length) === messageIds.length)
|
|
1997
|
+
return toRecord(cached);
|
|
1998
|
+
};
|
|
1999
|
+
|
|
2000
|
+
/**
|
|
2001
|
+
* ```js
|
|
2002
|
+
* import { queryMessages } from '@amityco/ts-sdk'
|
|
2003
|
+
* const messages = await queryMessages({ channelId })
|
|
2004
|
+
* ```
|
|
2005
|
+
*
|
|
2006
|
+
* Queries a paginable list of {@link Amity.Message} objects
|
|
2007
|
+
*
|
|
2008
|
+
* @param query The query parameters
|
|
2009
|
+
* @returns A page of {@link Amity.Message} objects
|
|
2010
|
+
*
|
|
2011
|
+
* @category Message API
|
|
2012
|
+
* @async
|
|
2013
|
+
*/
|
|
2014
|
+
const queryMessages = async (query) => {
|
|
2015
|
+
const client = getActiveClient();
|
|
2016
|
+
client.log('message/queryMessages', query);
|
|
2017
|
+
const _a = query !== null && query !== void 0 ? query : {}, { page } = _a, params = __rest(_a, ["page"]);
|
|
2018
|
+
// API-FIX: parameters should be querystring. (1)
|
|
2019
|
+
// API-FIX: backend should answer Amity.Response (2)
|
|
2020
|
+
// API-FIX: pagination should not be indexed on channelSegment (3)
|
|
2021
|
+
// const { data } = await client.http.get<Amity.Response<Amity.Paged<Amity.MessagePayload>>>(
|
|
2022
|
+
const { data } = await client.http.get(`/api/v3/messages`, { params: Object.assign(Object.assign({}, params), { options: {
|
|
2023
|
+
token: toToken(page, 'afterbefore'),
|
|
2024
|
+
} }), });
|
|
2025
|
+
// API-FIX: backend should answer Amity.Response (2)
|
|
2026
|
+
// const { paging, messages } = unwrapPayload(data)
|
|
2027
|
+
const { paging } = data, response = __rest(data, ["paging"]);
|
|
2028
|
+
decoratePayload(response);
|
|
2029
|
+
ingestInCache(response);
|
|
2030
|
+
const { messages } = response;
|
|
2031
|
+
const nextPage = toPage(paging.next);
|
|
2032
|
+
const prevPage = toPage(paging.previous);
|
|
2033
|
+
return { data: toRecord(messages), nextPage, prevPage };
|
|
2034
|
+
};
|
|
2035
|
+
/**
|
|
2036
|
+
* ```js
|
|
2037
|
+
* import { queryMessages } from '@amityco/ts-sdk'
|
|
2038
|
+
* const messages = queryMessages.locally!({ channelId })
|
|
2039
|
+
* ```
|
|
2040
|
+
*
|
|
2041
|
+
* Queries a paginable list of {@link Amity.Message} objects from cache
|
|
2042
|
+
*
|
|
2043
|
+
* @param query The query parameters
|
|
2044
|
+
* @returns A page of {@link Amity.Message} objects
|
|
2045
|
+
*
|
|
2046
|
+
* @category Message API
|
|
2047
|
+
*/
|
|
2048
|
+
queryMessages.locally = (query) => {
|
|
2049
|
+
var _a, _b, _c, _d, _e;
|
|
2050
|
+
const client = getActiveClient();
|
|
2051
|
+
client.log('message/queryMessages.locally', query);
|
|
2052
|
+
const { channelId, page } = query;
|
|
2053
|
+
const [channel] = (_a = pullFromCache('channel', channelId)) !== null && _a !== void 0 ? _a : [];
|
|
2054
|
+
let messages = (_b = pullFromCache('message')) !== null && _b !== void 0 ? _b : [];
|
|
2055
|
+
// filter
|
|
2056
|
+
messages = filterByPropEquality(messages, 'channelId', channelId);
|
|
2057
|
+
// API-FIX filter by isDeleted doesn't work in backend... srsly wtf... how do they test things???
|
|
2058
|
+
// messages = filterByPropEquality(messages, 'isDeleted', query?.isDeleted)
|
|
2059
|
+
// sort
|
|
2060
|
+
messages = messages.sort(sortByChannelSegment);
|
|
2061
|
+
// paging
|
|
2062
|
+
if (page) {
|
|
2063
|
+
// slice
|
|
2064
|
+
// API-FIX we can't reuse proper page because the message page is done
|
|
2065
|
+
// by channelSegment which is sadly unique to the entire platform.
|
|
2066
|
+
// this part is copied and adapted from sliceByPaging function
|
|
2067
|
+
let start = 0;
|
|
2068
|
+
let end = undefined;
|
|
2069
|
+
// next page behavior
|
|
2070
|
+
if ('after' in page) {
|
|
2071
|
+
start = page.after !== 0
|
|
2072
|
+
? messages.findIndex(message => message.channelSegment === (page === null || page === void 0 ? void 0 : page.after)) + 1
|
|
2073
|
+
: 0;
|
|
2074
|
+
end = start + page.limit;
|
|
2075
|
+
// prev page behavior
|
|
2076
|
+
}
|
|
2077
|
+
else if ('before' in page) {
|
|
2078
|
+
end = page.before !== 0
|
|
2079
|
+
? messages.findIndex(message => message.channelSegment === (page === null || page === void 0 ? void 0 : page.before)) + 1
|
|
2080
|
+
: messages.length - 1;
|
|
2081
|
+
start = end > page.limit ? end - page.limit : 0;
|
|
2082
|
+
}
|
|
2083
|
+
const paged = messages.slice(start, end);
|
|
2084
|
+
messages = paged.length === page.limit ? paged : [];
|
|
2085
|
+
}
|
|
2086
|
+
const prevPage = ((_c = messages[0]) === null || _c === void 0 ? void 0 : _c.channelSegment) > 0
|
|
2087
|
+
? { before: messages[0].channelSegment, limit: page === null || page === void 0 ? void 0 : page.limit }
|
|
2088
|
+
: undefined;
|
|
2089
|
+
const nextPage = ((_d = messages[messages.length - 1]) === null || _d === void 0 ? void 0 : _d.channelSegment) < ((_e = channel === null || channel === void 0 ? void 0 : channel.messageCount) !== null && _e !== void 0 ? _e : 0)
|
|
2090
|
+
? { after: messages[messages.length - 1].channelSegment, limit: page === null || page === void 0 ? void 0 : page.limit }
|
|
2091
|
+
: undefined;
|
|
2092
|
+
return { data: toRecord(messages), nextPage, prevPage };
|
|
2093
|
+
};
|
|
2094
|
+
|
|
2095
|
+
/**
|
|
2096
|
+
* ```js
|
|
2097
|
+
* import { createMessage } from '@amityco/ts-sdk'
|
|
2098
|
+
* const created = await createMessage({
|
|
2099
|
+
* channelId: 'foobar',
|
|
2100
|
+
* data: { text: 'hello world' }
|
|
2101
|
+
* })
|
|
2102
|
+
* ```
|
|
2103
|
+
*
|
|
2104
|
+
* Creates an {@link Amity.Message}
|
|
2105
|
+
*
|
|
2106
|
+
* @param bundle The data necessary to create a new {@link Amity.Message}
|
|
2107
|
+
* @returns The newly created {@link Amity.Message}
|
|
2108
|
+
*
|
|
2109
|
+
* @category Message API
|
|
2110
|
+
* @async
|
|
2111
|
+
*/
|
|
2112
|
+
const createMessage = async (bundle) => {
|
|
2113
|
+
const client = getActiveClient();
|
|
2114
|
+
client.log('user/createMessage', bundle);
|
|
2115
|
+
const { data } = await client.http.post('/api/v3/messages', bundle);
|
|
2116
|
+
decoratePayload(data);
|
|
2117
|
+
ingestInCache(data);
|
|
2118
|
+
// no need for event, fired by server
|
|
2119
|
+
const { messages } = data;
|
|
2120
|
+
return messages[0];
|
|
2121
|
+
};
|
|
2122
|
+
/**
|
|
2123
|
+
* ```js
|
|
2124
|
+
* import { createMessage } from '@amityco/ts-sdk'
|
|
2125
|
+
* const created = createMessage.locally({
|
|
2126
|
+
* channelId: 'foobar',
|
|
2127
|
+
* data: { text: 'hello world' }
|
|
2128
|
+
* })
|
|
2129
|
+
* ```
|
|
2130
|
+
*
|
|
2131
|
+
* Creates an {@link Amity.Message} in cache
|
|
2132
|
+
*
|
|
2133
|
+
* @param bundle The data necessary to create a new {@link Amity.Message}
|
|
2134
|
+
* @returns The newly created {@link Amity.Message}
|
|
2135
|
+
*
|
|
2136
|
+
* @category Message API
|
|
2137
|
+
*/
|
|
2138
|
+
createMessage.optimistically = (bundle) => {
|
|
2139
|
+
var _a, _b, _c;
|
|
2140
|
+
const client = getActiveClient();
|
|
2141
|
+
client.log('user/createMessage.locally', bundle);
|
|
2142
|
+
// should we do this?
|
|
2143
|
+
if (!client.cache)
|
|
2144
|
+
return;
|
|
2145
|
+
const [channel] = (_a = pullFromCache('channel', bundle.channelId)) !== null && _a !== void 0 ? _a : [];
|
|
2146
|
+
pushToCache('channel', Object.assign(Object.assign({}, channel), { messageCount: ((_b = channel.messageCount) !== null && _b !== void 0 ? _b : 0) + 1 }));
|
|
2147
|
+
const message = Object.assign(Object.assign({ localId: uuid(), messageId: Math.random().toString(16), userId: client.userId, channelSegment: ((_c = channel === null || channel === void 0 ? void 0 : channel.messageCount) !== null && _c !== void 0 ? _c : 0) + 1, createdAt: new Date().toISOString() }, bundle), { syncedAt: -1 });
|
|
2148
|
+
pushToCache('message', message);
|
|
2149
|
+
fireEvent('v3.message.didCreate', { messages: [message] });
|
|
2150
|
+
return message;
|
|
2151
|
+
};
|
|
2152
|
+
|
|
2153
|
+
/**
|
|
2154
|
+
* ```js
|
|
2155
|
+
* import { updateMessage } from '@amityco/ts-sdk'
|
|
2156
|
+
* const updated = await updateMessage(messageId, {
|
|
2157
|
+
* data: { text: 'hello world' }
|
|
2158
|
+
* })
|
|
2159
|
+
* ```
|
|
2160
|
+
*
|
|
2161
|
+
* Updates an {@link Amity.Message}
|
|
2162
|
+
*
|
|
2163
|
+
* @param messageId The ID of the {@link Amity.Message} to edit
|
|
2164
|
+
* @param patch The patch data to apply
|
|
2165
|
+
* @returns the updated {@link Amity.Message} object
|
|
2166
|
+
*
|
|
2167
|
+
* @category Message API
|
|
2168
|
+
* @async
|
|
2169
|
+
*/
|
|
2170
|
+
const updateMessage = async (messageId, patch) => {
|
|
2171
|
+
const client = getActiveClient();
|
|
2172
|
+
client.log('user/updateMessage', patch);
|
|
2173
|
+
const { data } = await client.http.put(`/api/v3/messages/${messageId}`, patch);
|
|
2174
|
+
decoratePayload(data);
|
|
2175
|
+
ingestInCache(data);
|
|
2176
|
+
// no need for event, fired by server
|
|
2177
|
+
return data.messages.find(message => message.messageId === messageId);
|
|
2178
|
+
};
|
|
2179
|
+
/**
|
|
2180
|
+
* ```js
|
|
2181
|
+
* import { updateMessage } from '@amityco/ts-sdk'
|
|
2182
|
+
* const updated = updateMessage.locally('foobar', {
|
|
2183
|
+
* data: { text: 'hello world' }
|
|
2184
|
+
* })
|
|
2185
|
+
* ```
|
|
2186
|
+
*
|
|
2187
|
+
* Updates an {@link Amity.Message} in cache
|
|
2188
|
+
*
|
|
2189
|
+
* @param messageId The ID of the {@link Amity.Message} to edit
|
|
2190
|
+
* @param patch The patch data to apply
|
|
2191
|
+
* @returns the updated {@link Amity.Message} object
|
|
2192
|
+
*
|
|
2193
|
+
* @category Message API
|
|
2194
|
+
*/
|
|
2195
|
+
updateMessage.optimistically = (messageId, patch) => {
|
|
2196
|
+
var _a;
|
|
2197
|
+
const client = getActiveClient();
|
|
2198
|
+
client.log('user/updateMessage', patch);
|
|
2199
|
+
const [message] = (_a = pullFromCache('message', messageId)) !== null && _a !== void 0 ? _a : [];
|
|
2200
|
+
if (!message)
|
|
2201
|
+
return;
|
|
2202
|
+
const updated = Object.assign(Object.assign(Object.assign({}, message), patch), { updatedAt: new Date().toISOString() });
|
|
2203
|
+
pushToCache('message', updated);
|
|
2204
|
+
fireEvent('v3.message.didUpdate', { messages: [updated] });
|
|
2205
|
+
return updated;
|
|
2206
|
+
};
|
|
2207
|
+
|
|
2208
|
+
/**
|
|
2209
|
+
* ```js
|
|
2210
|
+
* import { deleteMessage } from '@amityco/ts-sdk'
|
|
2211
|
+
* const success = await deleteMessage('foobar')
|
|
2212
|
+
* ```
|
|
2213
|
+
*
|
|
2214
|
+
* Delete a {@link Amity.Message}
|
|
2215
|
+
*
|
|
2216
|
+
* @param The ID of the {@link Amity.Message} to delete
|
|
2217
|
+
* @return A success boolean if the {@link Amity.Message} was deleted
|
|
2218
|
+
*
|
|
2219
|
+
* @category Message API
|
|
2220
|
+
* @async
|
|
2221
|
+
*/
|
|
2222
|
+
const deleteMessage = async (messageId) => {
|
|
2223
|
+
const client = getActiveClient();
|
|
2224
|
+
client.log('message/deleteMessage', messageId);
|
|
2225
|
+
// API-FIX: This endpoint has not been implemented yet.
|
|
2226
|
+
await client.http.delete(`/api/v3/messages/${messageId}`);
|
|
2227
|
+
const deleted = await getMessage(messageId);
|
|
2228
|
+
// no need for event, fired by server
|
|
2229
|
+
return deleted;
|
|
2230
|
+
};
|
|
2231
|
+
/**
|
|
2232
|
+
* ```js
|
|
2233
|
+
* import { deleteMessage } from '@amityco/ts-sdk'
|
|
2234
|
+
* const success = deleteMessage.locally('foobar')
|
|
2235
|
+
* ```
|
|
2236
|
+
*
|
|
2237
|
+
* Deletes a {@link Amity.Message}
|
|
2238
|
+
*
|
|
2239
|
+
* @param messageId The {@link Amity.Message} ID to delete
|
|
2240
|
+
* @return A success boolean if the {@link Amity.Message} was deleted
|
|
2241
|
+
*
|
|
2242
|
+
* @category Message API
|
|
2243
|
+
*/
|
|
2244
|
+
deleteMessage.optimistically = (messageId) => {
|
|
2245
|
+
var _a;
|
|
2246
|
+
const client = getActiveClient();
|
|
2247
|
+
client.log('message/deleteMessage', messageId);
|
|
2248
|
+
const [message] = (_a = pullFromCache('message', messageId)) !== null && _a !== void 0 ? _a : [];
|
|
2249
|
+
if (!message)
|
|
2250
|
+
return;
|
|
2251
|
+
const deleted = Object.assign(Object.assign({}, message), { isDeleted: true, updatedAt: new Date().toISOString() });
|
|
2252
|
+
pushToCache('message', deleted);
|
|
2253
|
+
fireEvent('v3.message.didDelete', { messages: [deleted] });
|
|
2254
|
+
return deleted;
|
|
2255
|
+
};
|
|
2256
|
+
|
|
2257
|
+
/**
|
|
2258
|
+
* ```js
|
|
2259
|
+
* import { queryPosts } from '@amityco/ts-sdk'
|
|
2260
|
+
* const posts = await queryPosts({ targetId, targetType })
|
|
2261
|
+
* ```
|
|
2262
|
+
*
|
|
2263
|
+
* Queries a paginable list of {@link Amity.Post} objects
|
|
2264
|
+
*
|
|
2265
|
+
* @param query The query parameters
|
|
2266
|
+
* @returns posts
|
|
2267
|
+
*
|
|
2268
|
+
* @category Post API
|
|
2269
|
+
* @async
|
|
2270
|
+
*/
|
|
2271
|
+
const queryPosts = async (query) => {
|
|
2272
|
+
const client = getActiveClient();
|
|
2273
|
+
client.log('post/queryPosts', query);
|
|
2274
|
+
const _a = query !== null && query !== void 0 ? query : {}, { page } = _a, params = __rest(_a, ["page"]);
|
|
2275
|
+
// API-FIX: parameters should be querystring. (1)
|
|
2276
|
+
// API-FIX: backend should answer Amity.Response (2)
|
|
2277
|
+
// const { data } = await client.http.get<Amity.Response<Amity.PagedResponse<Amity.PostPayload>>>(
|
|
2278
|
+
const { data } = await client.http.get(`/api/v3/posts`, { params: Object.assign(Object.assign({}, params), { options: {
|
|
2279
|
+
token: toToken(page, 'skiplimit'),
|
|
2280
|
+
} }), });
|
|
2281
|
+
// API-FIX: backend should answer Amity.Response (2)
|
|
2282
|
+
// const { paging, posts } = unwrapPayload(data)
|
|
2283
|
+
const { paging } = data, response = __rest(data, ["paging"]);
|
|
2284
|
+
decoratePayload(response);
|
|
2285
|
+
ingestInCache(response);
|
|
2286
|
+
const { posts } = response;
|
|
2287
|
+
const nextPage = toPage(paging.next);
|
|
2288
|
+
const prevPage = toPage(paging.previous);
|
|
2289
|
+
return { data: toRecord(posts), nextPage, prevPage };
|
|
2290
|
+
};
|
|
2291
|
+
/**
|
|
2292
|
+
* ```js
|
|
2293
|
+
* import { queryPosts } from '@amityco/ts-sdk'
|
|
2294
|
+
* const posts = queryPosts.locally({ targetId, targetType })
|
|
2295
|
+
* ```
|
|
2296
|
+
*
|
|
2297
|
+
* Queries a paginable list of {@link Amity.Post} objects from cache
|
|
2298
|
+
*
|
|
2299
|
+
* @param query The query parameters
|
|
2300
|
+
* @returns posts
|
|
2301
|
+
*
|
|
2302
|
+
* @category Post API
|
|
2303
|
+
*/
|
|
2304
|
+
queryPosts.locally = (query) => {
|
|
2305
|
+
const client = getActiveClient();
|
|
2306
|
+
client.log('post/queryPosts.locally', query);
|
|
2307
|
+
// TODO
|
|
2308
|
+
};
|
|
2309
|
+
|
|
2310
|
+
/**
|
|
2311
|
+
* ```js
|
|
2312
|
+
* import { getPosts } from '@amityco/ts-sdk'
|
|
2313
|
+
* const posts = await getPosts(['foo', 'bar'])
|
|
2314
|
+
* ```
|
|
2315
|
+
*
|
|
2316
|
+
* Fetches a collection of {@link Amity.Post} objects
|
|
2317
|
+
*
|
|
2318
|
+
* @param postIds the IDs of the {@link Amity.Post} to fetch
|
|
2319
|
+
* @returns the associated collection of {@link Amity.Post} objects
|
|
2320
|
+
*
|
|
2321
|
+
* @category Post API
|
|
2322
|
+
* @async
|
|
2323
|
+
*/
|
|
2324
|
+
const getPosts = async (postIds) => {
|
|
2325
|
+
const client = getActiveClient();
|
|
2326
|
+
client.log('post/getPosts', postIds);
|
|
2327
|
+
// API-FIX: endpoint should not be /list, parameters should be querystring.
|
|
2328
|
+
const { data } = await client.http.get(`/api/v3/posts/list`, { params: { postIds } });
|
|
2329
|
+
decoratePayload(data);
|
|
2330
|
+
ingestInCache(data);
|
|
2331
|
+
const { posts } = data;
|
|
2332
|
+
return posts;
|
|
2333
|
+
};
|
|
2334
|
+
/**
|
|
2335
|
+
* ```js
|
|
2336
|
+
* import { getPosts } from '@amityco/ts-sdk'
|
|
2337
|
+
* const posts = getPosts.locally(['foo', 'bar'])
|
|
2338
|
+
* ```
|
|
2339
|
+
*
|
|
2340
|
+
* Fetches a collection of {@link Amity.Post} objects from cache
|
|
2341
|
+
*
|
|
2342
|
+
* @param postIds the IDs of the {@link Amity.Post} to fetch
|
|
2343
|
+
* @returns the associated collection of {@link Amity.Post} objects
|
|
2344
|
+
*
|
|
2345
|
+
* @category Post API
|
|
2346
|
+
*/
|
|
2347
|
+
getPosts.locally = (postIds) => {
|
|
2348
|
+
var _a;
|
|
2349
|
+
const client = getActiveClient();
|
|
2350
|
+
client.log('post/getPosts.locally', postIds);
|
|
2351
|
+
const cached = (_a = pullFromCache('post', ...postIds)) !== null && _a !== void 0 ? _a : [];
|
|
2352
|
+
if ((cached === null || cached === void 0 ? void 0 : cached.length) === postIds.length)
|
|
2353
|
+
return toRecord(cached);
|
|
2354
|
+
};
|
|
2355
|
+
|
|
2356
|
+
/**
|
|
2357
|
+
* ```js
|
|
2358
|
+
* import { getPost } from '@amityco/ts-sdk'
|
|
2359
|
+
* const post = await getPost('foobar')
|
|
2360
|
+
* ```
|
|
2361
|
+
*
|
|
2362
|
+
* Fetches a {@link Amity.Post} object
|
|
2363
|
+
*
|
|
2364
|
+
* @param postId the ID of the {@link Amity.Post} to fetch
|
|
2365
|
+
* @returns the associated {@link Amity.Post} object
|
|
2366
|
+
*
|
|
2367
|
+
* @category Post API
|
|
2368
|
+
* @async
|
|
2369
|
+
*/
|
|
2370
|
+
const getPost = async (postId) => {
|
|
2371
|
+
const client = getActiveClient();
|
|
2372
|
+
client.log('post/getPost', postId);
|
|
2373
|
+
// API-FIX: endpoint should not be /list, parameters should be querystring.
|
|
2374
|
+
const { data } = await client.http.get(`/api/v3/posts/${postId}`);
|
|
2375
|
+
decoratePayload(data);
|
|
2376
|
+
ingestInCache(data);
|
|
2377
|
+
const { posts } = data;
|
|
2378
|
+
return posts.find(post => post.postId === postId);
|
|
2379
|
+
};
|
|
2380
|
+
/**
|
|
2381
|
+
* ```js
|
|
2382
|
+
* import { getPost } from '@amityco/ts-sdk'
|
|
2383
|
+
* const post = getPost.locally('foobar')
|
|
2384
|
+
* ```
|
|
2385
|
+
*
|
|
2386
|
+
* Fetches a {@link Amity.Post} object
|
|
2387
|
+
*
|
|
2388
|
+
* @param postId the ID of the {@link Amity.Post} to fetch
|
|
2389
|
+
* @returns the associated {@link Amity.Post} object
|
|
2390
|
+
*
|
|
2391
|
+
* @category Post API
|
|
2392
|
+
*/
|
|
2393
|
+
getPost.locally = (postId) => {
|
|
2394
|
+
var _a;
|
|
2395
|
+
const client = getActiveClient();
|
|
2396
|
+
client.log('post/getPost.locally', postId);
|
|
2397
|
+
const [post] = (_a = pullFromCache('post', postId)) !== null && _a !== void 0 ? _a : [];
|
|
2398
|
+
return post;
|
|
2399
|
+
};
|
|
2400
|
+
|
|
2401
|
+
/**
|
|
2402
|
+
* ```js
|
|
2403
|
+
* import { createPost } from '@amityco/ts-sdk'
|
|
2404
|
+
* const newPost = await createPost(bundle)
|
|
2405
|
+
* ```
|
|
2406
|
+
*
|
|
2407
|
+
* Creates an {@link Amity.Post}
|
|
2408
|
+
*
|
|
2409
|
+
* @param bundle The data necessary to create a new {@link Amity.Post}
|
|
2410
|
+
* @returns The newly created {@link Amity.Post}
|
|
2411
|
+
*
|
|
2412
|
+
* @category Post API
|
|
2413
|
+
* @async
|
|
2414
|
+
*/
|
|
2415
|
+
const createPost = async (bundle) => {
|
|
2416
|
+
const client = getActiveClient();
|
|
2417
|
+
client.log('post/createPost', bundle);
|
|
2418
|
+
// API-FIX: backend rejects that we pass the dataType manually T_T
|
|
2419
|
+
if (!bundle.dataType || ['text', 'image', 'file'].includes(bundle.dataType))
|
|
2420
|
+
delete bundle.dataType;
|
|
2421
|
+
const { data } = await client.http.post('/api/v3/posts', bundle);
|
|
2422
|
+
decoratePayload(data);
|
|
2423
|
+
ingestInCache(data);
|
|
2424
|
+
fireEvent('v3.community.didUpdate', { communities: data.communities });
|
|
2425
|
+
fireEvent('v3.post.didCreate', data);
|
|
2426
|
+
const { posts } = data;
|
|
2427
|
+
return posts[0];
|
|
2428
|
+
};
|
|
2429
|
+
|
|
2430
|
+
/**
|
|
2431
|
+
* ```js
|
|
2432
|
+
* import { updatePost } from '@amityco/ts-sdk'
|
|
2433
|
+
* const updated = await updatePost(postId, {
|
|
2434
|
+
* data: { text: 'hello world' }
|
|
2435
|
+
* })
|
|
2436
|
+
* ```
|
|
2437
|
+
*
|
|
2438
|
+
* Updates an {@link Amity.Post}
|
|
2439
|
+
*
|
|
2440
|
+
* @param postId The ID of the {@link Amity.Post} to edit
|
|
2441
|
+
* @param patch The patch data to apply
|
|
2442
|
+
* @returns the updated {@link Amity.Post} object
|
|
2443
|
+
*
|
|
2444
|
+
* @category Post API
|
|
2445
|
+
* @async
|
|
2446
|
+
*/
|
|
2447
|
+
const updatePost = async (postId, patch) => {
|
|
2448
|
+
const client = getActiveClient();
|
|
2449
|
+
client.log('user/updatePost', patch);
|
|
2450
|
+
const { data } = await client.http.put(`/api/v3/posts/${postId}`, patch);
|
|
2451
|
+
decoratePayload(data);
|
|
2452
|
+
ingestInCache(data);
|
|
2453
|
+
fireEvent('v3.post.didUpdate', data);
|
|
2454
|
+
const { posts } = data;
|
|
2455
|
+
return posts.find(post => post.postId === postId);
|
|
2456
|
+
};
|
|
2457
|
+
|
|
2458
|
+
/**
|
|
2459
|
+
* ```js
|
|
2460
|
+
* import { queryCommunities } from '@amityco/ts-sdk'
|
|
2461
|
+
* const communities = await queryCommunities()
|
|
2462
|
+
* ```
|
|
2463
|
+
*
|
|
2464
|
+
* Queries a paginable list of {@link Amity.Community} objects
|
|
2465
|
+
* Search is performed by displayName such as `.startsWith(search)`
|
|
2466
|
+
*
|
|
2467
|
+
* @param query The query parameters
|
|
2468
|
+
* @returns A page of {@link Amity.Community} objects
|
|
2469
|
+
*
|
|
2470
|
+
* @category Community API
|
|
2471
|
+
* @async
|
|
2472
|
+
*/
|
|
2473
|
+
const queryCommunities = async (query) => {
|
|
2474
|
+
const client = getActiveClient();
|
|
2475
|
+
client.log('channel/queryCommunities', query);
|
|
2476
|
+
// safe decapsulation
|
|
2477
|
+
const _a = query !== null && query !== void 0 ? query : {}, { page, displayName, membership } = _a, params = __rest(_a, ["page", "displayName", "membership"]);
|
|
2478
|
+
// API-FIX: parameters should be querystring.
|
|
2479
|
+
// API-FIX: backend doesnt answer Amity.Response
|
|
2480
|
+
// const { data } = await client.http.get<Amity.Response<Amity.PagedResponse<CommunityPayload>>>(
|
|
2481
|
+
const { data } = await client.http.get(`/api/v3/communities`, { params: Object.assign(Object.assign({}, params), { keyword: displayName, filter: membership, options: {
|
|
2482
|
+
token: toToken(page, 'skiplimit')
|
|
2483
|
+
} }) });
|
|
2484
|
+
// API-FIX: backend to response Amity.Response: const { paging, communities } = unwrapPayload(data)
|
|
2485
|
+
const { paging } = data, response = __rest(data, ["paging"]);
|
|
2486
|
+
decoratePayload(response);
|
|
2487
|
+
ingestInCache(response);
|
|
2488
|
+
const prevPage = toPage(paging === null || paging === void 0 ? void 0 : paging.previous);
|
|
2489
|
+
const nextPage = toPage(paging === null || paging === void 0 ? void 0 : paging.next);
|
|
2490
|
+
const { communities } = response;
|
|
2491
|
+
return { data: toRecord(communities), prevPage, nextPage };
|
|
2492
|
+
};
|
|
2493
|
+
/**
|
|
2494
|
+
* ```js
|
|
2495
|
+
* import { queryCommunities } from '@amityco/ts-sdk'
|
|
2496
|
+
* const communities = queryCommunities.locally()
|
|
2497
|
+
* ```
|
|
2498
|
+
*
|
|
2499
|
+
* Queries a paginable list of {@link Amity.Community} objects from cache
|
|
2500
|
+
*
|
|
2501
|
+
* @param query The query parameters
|
|
2502
|
+
* @returns communities
|
|
2503
|
+
*
|
|
2504
|
+
* @category Community API
|
|
2505
|
+
*/
|
|
2506
|
+
queryCommunities.locally = (query) => {
|
|
2507
|
+
const client = getActiveClient();
|
|
2508
|
+
client.log('community/queryCommunities.locally', query);
|
|
2509
|
+
// TODO
|
|
2510
|
+
};
|
|
2511
|
+
|
|
2512
|
+
/**
|
|
2513
|
+
* ```js
|
|
2514
|
+
* import { getCommunities } from '@amityco/ts-sdk'
|
|
2515
|
+
* const communities = await getCommunities(['foo', 'bar'])
|
|
2516
|
+
* ```
|
|
2517
|
+
*
|
|
2518
|
+
* Fetches a collection of {@link Amity.Community} objects
|
|
2519
|
+
*
|
|
2520
|
+
* @param communityIds the IDs of the {@link Amity.Community} to fetch
|
|
2521
|
+
* @returns the associated collection of {@link Amity.Community} objects
|
|
2522
|
+
*
|
|
2523
|
+
* @category Community API
|
|
2524
|
+
* @async
|
|
2525
|
+
*/
|
|
2526
|
+
const getCommunities = async (communityIds) => {
|
|
2527
|
+
const client = getActiveClient();
|
|
2528
|
+
client.log('community/getCommunities', communityIds);
|
|
2529
|
+
// API-FIX: endpoint should not be /list, parameters should be querystring.
|
|
2530
|
+
const { data } = await client.http.get(`/api/v3/communities/list`, { params: { communityIds } });
|
|
2531
|
+
decoratePayload(data);
|
|
2532
|
+
ingestInCache(data);
|
|
2533
|
+
const { communities } = data;
|
|
2534
|
+
return toRecord(communities);
|
|
2535
|
+
};
|
|
2536
|
+
/**
|
|
2537
|
+
* ```js
|
|
2538
|
+
* import { getCommunities } from '@amityco/ts-sdk'
|
|
2539
|
+
* const communities = getCommunities.locally(['foo', 'bar']) ?? []
|
|
2540
|
+
* ```
|
|
2541
|
+
*
|
|
2542
|
+
* Fetches a collection of {@link Amity.Community} objects from cache
|
|
2543
|
+
*
|
|
2544
|
+
* @param communityIds the IDs of the {@link Amity.Community} to fetch
|
|
2545
|
+
* @returns the associated collection of {@link Amity.Community} objects
|
|
2546
|
+
*
|
|
2547
|
+
* @category Community API
|
|
2548
|
+
*/
|
|
2549
|
+
getCommunities.locally = (communityIds) => {
|
|
2550
|
+
var _a;
|
|
2551
|
+
const client = getActiveClient();
|
|
2552
|
+
client.log('community/getCommunities.locally', communityIds);
|
|
2553
|
+
const cached = (_a = pullFromCache('community', ...communityIds)) !== null && _a !== void 0 ? _a : [];
|
|
2554
|
+
if ((cached === null || cached === void 0 ? void 0 : cached.length) === communityIds.length)
|
|
2555
|
+
return toRecord(cached);
|
|
2556
|
+
};
|
|
2557
|
+
|
|
2558
|
+
/**
|
|
2559
|
+
* ```js
|
|
2560
|
+
* import { getCommunity } from '@amityco/ts-sdk'
|
|
2561
|
+
* const community = await getCommunity('foobar')
|
|
2562
|
+
* ```
|
|
2563
|
+
*
|
|
2564
|
+
* Fetches a {@link Amity.Community} object
|
|
2565
|
+
*
|
|
2566
|
+
* @param communityId the ID of the {@link Amity.Community} to fetch
|
|
2567
|
+
* @returns the associated {@link Amity.Community} object
|
|
2568
|
+
*
|
|
2569
|
+
* @category Community API
|
|
2570
|
+
* @async
|
|
2571
|
+
*/
|
|
2572
|
+
const getCommunity = async (communityId) => {
|
|
2573
|
+
const client = getActiveClient();
|
|
2574
|
+
client.log('community/getCommunity', communityId);
|
|
2575
|
+
// API-FIX: endpoint should not be /list, parameters should be querystring.
|
|
2576
|
+
const { data } = await client.http.get(`/api/v3/communities/${communityId}`);
|
|
2577
|
+
decoratePayload(data);
|
|
2578
|
+
ingestInCache(data);
|
|
2579
|
+
const { communities } = data;
|
|
2580
|
+
return communities.find(community => community.communityId === communityId);
|
|
2581
|
+
};
|
|
2582
|
+
/**
|
|
2583
|
+
* ```js
|
|
2584
|
+
* import { getCommunity } from '@amityco/ts-sdk'
|
|
2585
|
+
* const community = getCommunity.locally('foobar')
|
|
2586
|
+
* ```
|
|
2587
|
+
*
|
|
2588
|
+
* Fetches a {@link Amity.Community} object from cache
|
|
2589
|
+
*
|
|
2590
|
+
* @param communityId the ID of the {@link Amity.Community} to fetch
|
|
2591
|
+
* @returns the associated {@link Amity.Community} object
|
|
2592
|
+
*
|
|
2593
|
+
* @category Community API
|
|
2594
|
+
*/
|
|
2595
|
+
getCommunity.locally = (communityId) => {
|
|
2596
|
+
var _a;
|
|
2597
|
+
const client = getActiveClient();
|
|
2598
|
+
client.log('community/getCommunity.locally', communityId);
|
|
2599
|
+
const [community] = (_a = pullFromCache('community', communityId)) !== null && _a !== void 0 ? _a : [];
|
|
2600
|
+
return community;
|
|
2601
|
+
};
|
|
2602
|
+
|
|
2603
|
+
/**
|
|
2604
|
+
* ```js
|
|
2605
|
+
* import { createCommunity } from '@amityco/ts-sdk'
|
|
2606
|
+
* const created = await createCommunity({ communityId: 'foobar', displayName: 'foobar' })
|
|
2607
|
+
* ```
|
|
2608
|
+
*
|
|
2609
|
+
* Creates an {@link Amity.Community}
|
|
2610
|
+
*
|
|
2611
|
+
* @param bundle The data necessary to create a new {@link Amity.Community}
|
|
2612
|
+
* @returns The newly created {@link Amity.Community}
|
|
2613
|
+
*
|
|
2614
|
+
* @category Community API
|
|
2615
|
+
* @async
|
|
2616
|
+
*/
|
|
2617
|
+
const createCommunity = async (bundle) => {
|
|
2618
|
+
const client = getActiveClient();
|
|
2619
|
+
client.log('user/createCommunity', bundle);
|
|
2620
|
+
const { data } = await client.http.post('/api/v3/communities/', bundle);
|
|
2621
|
+
decoratePayload(data);
|
|
2622
|
+
ingestInCache(data);
|
|
2623
|
+
fireEvent('v3.community.didCreate', data);
|
|
2624
|
+
const { communities } = data;
|
|
2625
|
+
return communities[0];
|
|
2626
|
+
};
|
|
2627
|
+
|
|
2628
|
+
/**
|
|
2629
|
+
* ```js
|
|
2630
|
+
* import { updateCommunity } from '@amityco/ts-sdk'
|
|
2631
|
+
* const updated = await updateCommunity(communityId, { displayName: 'foobar' })
|
|
2632
|
+
* ```
|
|
2633
|
+
*
|
|
2634
|
+
* Updates an {@link Amity.Community}
|
|
2635
|
+
*
|
|
2636
|
+
* @param communityId The ID of the {@link Amity.Community} to edit
|
|
2637
|
+
* @param patch The patch data to apply
|
|
2638
|
+
* @returns the updated {@link Amity.Community} object
|
|
2639
|
+
*
|
|
2640
|
+
* @category Community API
|
|
2641
|
+
* @async
|
|
2642
|
+
*/
|
|
2643
|
+
const updateCommunity = async (communityId, patch) => {
|
|
2644
|
+
const client = getActiveClient();
|
|
2645
|
+
client.log('community/updateCommunity', communityId, patch);
|
|
2646
|
+
const { data } = await client.http.put(`/api/v3/communities/${communityId}`, patch);
|
|
2647
|
+
decoratePayload(data);
|
|
2648
|
+
ingestInCache(data);
|
|
2649
|
+
fireEvent('v3.community.didUpdate', data);
|
|
2650
|
+
const { communities } = data;
|
|
2651
|
+
return communities.find(community => community.communityId === communityId);
|
|
2652
|
+
};
|
|
2653
|
+
|
|
2654
|
+
/**
|
|
2655
|
+
* ```js
|
|
2656
|
+
* import { deleteCommunity } from '@amityco/ts-sdk'
|
|
2657
|
+
* const success = await deleteCommunity('foobar')
|
|
2658
|
+
* ```
|
|
2659
|
+
*
|
|
2660
|
+
* Deletes a {@link Amity.Community}
|
|
2661
|
+
*
|
|
2662
|
+
* @param communityId The {@link Amity.Community} ID to delete
|
|
2663
|
+
* @return A success boolean if the {@link Amity.Community} was deleted
|
|
2664
|
+
*
|
|
2665
|
+
* @category Community API
|
|
2666
|
+
* @async
|
|
2667
|
+
*/
|
|
2668
|
+
const deleteCommunity = async (communityId) => {
|
|
2669
|
+
const client = getActiveClient();
|
|
2670
|
+
client.log('community/deleteCommunity', communityId);
|
|
2671
|
+
// API-FIX: This endpoint has not been implemented yet.
|
|
2672
|
+
await client.http.delete(`/api/v3/communities/${communityId}`);
|
|
2673
|
+
const deleted = await getCommunity(communityId);
|
|
2674
|
+
fireEvent('v3.community.didDelete', { communities: [deleted] });
|
|
2675
|
+
return deleted;
|
|
2676
|
+
};
|
|
2677
|
+
|
|
2678
|
+
/**
|
|
2679
|
+
* ```js
|
|
2680
|
+
* import { joinCommunity } from '@amityco/ts-sdk'
|
|
2681
|
+
* const isJoined = await joinCommunity('foobar')
|
|
2682
|
+
* ```
|
|
2683
|
+
*
|
|
2684
|
+
* Joins a {@link Amity.Community} object
|
|
2685
|
+
*
|
|
2686
|
+
* @param communityId the {@link Amity.Community} to join
|
|
2687
|
+
* @returns A success boolean if the {@link Amity.Community} was joined
|
|
2688
|
+
*
|
|
2689
|
+
* @category Community API
|
|
2690
|
+
* @async
|
|
2691
|
+
*/
|
|
2692
|
+
const joinCommunity = async (communityId) => {
|
|
2693
|
+
const client = getActiveClient();
|
|
2694
|
+
client.log('community/joinCommunity', communityId);
|
|
2695
|
+
const { data } = await client.http.post(`/api/v3/communities/${communityId}/join`);
|
|
2696
|
+
decoratePayload(data);
|
|
2697
|
+
ingestInCache(data);
|
|
2698
|
+
fireEvent('v3.community.didJoin', {
|
|
2699
|
+
communities: data.communities
|
|
2700
|
+
});
|
|
2701
|
+
const { communityUsers } = data;
|
|
2702
|
+
return !!communityUsers.find(communityUser => (communityUser.communityId === communityId
|
|
2703
|
+
&& communityUser.communityMembership === 'member'));
|
|
2704
|
+
};
|
|
2705
|
+
|
|
2706
|
+
/**
|
|
2707
|
+
* ```js
|
|
2708
|
+
* import { leaveCommunity } from '@amityco/ts-sdk'
|
|
2709
|
+
* const isLeft = await leaveCommunity('foobar')
|
|
2710
|
+
* ```
|
|
2711
|
+
*
|
|
2712
|
+
* Leaves a {@link Amity.Community} object
|
|
2713
|
+
*
|
|
2714
|
+
* @param communityId the {@link Amity.Community} to leave
|
|
2715
|
+
* @returns A success boolean if {@link Amity.Community} was left
|
|
2716
|
+
*
|
|
2717
|
+
* @category Community API
|
|
2718
|
+
* @async
|
|
2719
|
+
*/
|
|
2720
|
+
const leaveCommunity = async (communityId) => {
|
|
2721
|
+
const client = getActiveClient();
|
|
2722
|
+
client.log('community/leaveCommunity', communityId);
|
|
2723
|
+
const { data } = await client.http.delete(`/api/v3/communities/${communityId}/leave`);
|
|
2724
|
+
decoratePayload(data);
|
|
2725
|
+
ingestInCache(data);
|
|
2726
|
+
fireEvent('v3.community.didJoin', {
|
|
2727
|
+
communities: data.communities,
|
|
2728
|
+
});
|
|
2729
|
+
const { communityUsers } = data;
|
|
2730
|
+
return !!communityUsers.find(communityUser => (communityUser.communityId === communityId
|
|
2731
|
+
&& communityUser.communityMembership !== 'member'));
|
|
2732
|
+
};
|
|
2733
|
+
|
|
2734
|
+
/**
|
|
2735
|
+
* ```js
|
|
2736
|
+
* import { queryCommunityMembers } from '@amityco/ts-sdk'
|
|
2737
|
+
* const communityMembers = await queryCommunityMembers({ communityId: 'foo' })
|
|
2738
|
+
* ```
|
|
2739
|
+
*
|
|
2740
|
+
* Queries a paginable list of {@link Amity.CommunityUser} objects
|
|
2741
|
+
*
|
|
2742
|
+
* @param query The query parameters
|
|
2743
|
+
* @returns A page of {@link Amity.CommunityUser} objects
|
|
2744
|
+
*
|
|
2745
|
+
* @category Community API
|
|
2746
|
+
* @async
|
|
2747
|
+
**/
|
|
2748
|
+
const queryCommunityMembers = async (query) => {
|
|
2749
|
+
const client = getActiveClient();
|
|
2750
|
+
client.log('community/queryCommunityMembers', query);
|
|
2751
|
+
const _a = query !== null && query !== void 0 ? query : {}, { page, communityId } = _a, params = __rest(_a, ["page", "communityId"]);
|
|
2752
|
+
const { data } = await client.http.get(`/api/v3/communities/${communityId}/users`, {
|
|
2753
|
+
params: Object.assign(Object.assign({}, params), { options: {
|
|
2754
|
+
token: toToken(page, 'skiplimit'),
|
|
2755
|
+
} }),
|
|
2756
|
+
});
|
|
2757
|
+
const { paging } = data, response = __rest(data, ["paging"]);
|
|
2758
|
+
decoratePayload(response);
|
|
2759
|
+
ingestInCache(response);
|
|
2760
|
+
const { communityUsers } = response;
|
|
2761
|
+
const nextPage = toPage(paging.next);
|
|
2762
|
+
const prevPage = toPage(paging.previous);
|
|
2763
|
+
return { data: toRecord(communityUsers), nextPage, prevPage };
|
|
2764
|
+
};
|
|
2765
|
+
/**
|
|
2766
|
+
* ```js
|
|
2767
|
+
* import { queryCommunityMembers } from '@amityco/ts-sdk'
|
|
2768
|
+
* const communityMembers = await queryCommunityMembers(query)
|
|
2769
|
+
* ```
|
|
2770
|
+
*
|
|
2771
|
+
* Queries a paginable list of {@link Amity.Post} objects from cache
|
|
2772
|
+
*
|
|
2773
|
+
* @param query The query parameters
|
|
2774
|
+
* @returns posts
|
|
2775
|
+
*
|
|
2776
|
+
* @category Post API
|
|
2777
|
+
*/
|
|
2778
|
+
queryCommunityMembers.locally = (query) => {
|
|
2779
|
+
const client = getActiveClient();
|
|
2780
|
+
client.log('community/queryCommunityMembers', query);
|
|
2781
|
+
// TODO
|
|
2782
|
+
};
|
|
2783
|
+
|
|
2784
|
+
/**
|
|
2785
|
+
* ```js
|
|
2786
|
+
* import { addCommunityMembers } from '@amityco/ts-sdk'
|
|
2787
|
+
* const updated = await addCommunityMembers(communityId, ['foo', 'bar'])
|
|
2788
|
+
* ```
|
|
2789
|
+
*
|
|
2790
|
+
* Adds a list of {@link Amity.User} to a {@link Amity.Community} to add users to
|
|
2791
|
+
*
|
|
2792
|
+
* @param communityId The ID of the {@link Amity.Community} to perform
|
|
2793
|
+
* @param userIds The list of IDs {@link Amity.User} to add
|
|
2794
|
+
* @returns A success boolean if the {@link Amity.User} were added to the {@link Amity.Community}
|
|
2795
|
+
*
|
|
2796
|
+
* @category Community API
|
|
2797
|
+
* @async
|
|
2798
|
+
*/
|
|
2799
|
+
const addCommunityMembers = async (communityId, userIds) => {
|
|
2800
|
+
const client = getActiveClient();
|
|
2801
|
+
client.log('community/addCommunityMembers', communityId, userIds);
|
|
2802
|
+
const { data } = await client.http.post(`/api/v3/communities/${communityId}/users`, { communityId, userIds });
|
|
2803
|
+
decoratePayload(data);
|
|
2804
|
+
ingestInCache(data);
|
|
2805
|
+
fireEvent('v3.community.didAddUsers', {
|
|
2806
|
+
communities: data.communities,
|
|
2807
|
+
});
|
|
2808
|
+
const { communityUsers } = data;
|
|
2809
|
+
return !!communityUsers.find(communityUser => communityUser.communityId === communityId &&
|
|
2810
|
+
communityUser.communityMembership === 'member');
|
|
2811
|
+
};
|
|
2812
|
+
|
|
2813
|
+
/**
|
|
2814
|
+
* ```js
|
|
2815
|
+
* import { removeCommunityMembers } from '@amityco/ts-sdk'
|
|
2816
|
+
* const updated = await removeCommunityMembers(communityId, ['foo', 'bar'])
|
|
2817
|
+
* ```
|
|
2818
|
+
*
|
|
2819
|
+
* Removes a list of {@link Amity.User} from a {@link Amity.Channel}
|
|
2820
|
+
*
|
|
2821
|
+
* @param communityId The ID of the {@link Amity.Community} to edit
|
|
2822
|
+
* @param userIds The list of IDs {@link Amity.User} to remove
|
|
2823
|
+
* @returns A success boolean if the list of {@link Amity.User} were removed from the {@link Amity.Community}
|
|
2824
|
+
*
|
|
2825
|
+
* @category Community API
|
|
2826
|
+
* @async
|
|
2827
|
+
*/
|
|
2828
|
+
const removeCommunityMembers = async (communityId, userIds) => {
|
|
2829
|
+
const client = getActiveClient();
|
|
2830
|
+
client.log('community/removeCommunityMembers', communityId, userIds);
|
|
2831
|
+
const { data } = await client.http.delete(`/api/v3/communities/${communityId}/users`, { data: { communityId, userIds } });
|
|
2832
|
+
decoratePayload(data);
|
|
2833
|
+
ingestInCache(data);
|
|
2834
|
+
fireEvent('v3.community.didRemoveUsers', {
|
|
2835
|
+
communities: data.communities,
|
|
2836
|
+
});
|
|
2837
|
+
const { communityUsers } = data;
|
|
2838
|
+
return !!communityUsers.find(communityUser => communityUser.communityId === communityId &&
|
|
2839
|
+
communityUser.communityMembership !== 'member');
|
|
2840
|
+
};
|
|
2841
|
+
|
|
2842
|
+
/**
|
|
2843
|
+
* ```js
|
|
2844
|
+
* import { addCommunityMembersRole } from '@amityco/ts-sdk'
|
|
2845
|
+
* const updated = await addCommunityMembersRole(communityId, 'foo', ['bar'])
|
|
2846
|
+
* ```
|
|
2847
|
+
*
|
|
2848
|
+
* Adds an {@link Amity.Role} to a list of {@link Amity.User} on a {@link Amity.Community}
|
|
2849
|
+
*
|
|
2850
|
+
* @param communityId The ID of the {@link Amity.Community} to perform
|
|
2851
|
+
* @param roleId The ID of the {@link Amity.Role} to apply
|
|
2852
|
+
* @param userId Array of IDs of the {@link Amity.User} to perform
|
|
2853
|
+
* @returns A success boolean if the {@link Amity.Role} were added to list of {@link Amity.User} in the {@link Amity.Community}
|
|
2854
|
+
*
|
|
2855
|
+
* @category Community API
|
|
2856
|
+
* @async
|
|
2857
|
+
*/
|
|
2858
|
+
const addCommunityMembersRole = async (communityId, roleId, userIds) => {
|
|
2859
|
+
const client = getActiveClient();
|
|
2860
|
+
client.log('community/addCommunityMembersRole', communityId, roleId, userIds);
|
|
2861
|
+
const { data } = await client.http.post(`/api/v3/communities/${communityId}/users/roles`, { communityId, role: roleId, userIds });
|
|
2862
|
+
decoratePayload(data);
|
|
2863
|
+
ingestInCache(data);
|
|
2864
|
+
const { communityUsers } = data;
|
|
2865
|
+
return !!communityUsers.find(communityUser => communityUser.communityId === communityId && communityUser.roles.includes(roleId));
|
|
2866
|
+
};
|
|
2867
|
+
|
|
2868
|
+
/**
|
|
2869
|
+
* ```js
|
|
2870
|
+
* import { removeCommunityMembersRole } from '@amityco/ts-sdk'
|
|
2871
|
+
* const updated = await removeCommunityMembersRole(communityId, 'foo', ['bar'])
|
|
2872
|
+
* ```
|
|
2873
|
+
*
|
|
2874
|
+
* Removes an {@link Amity.Role} from a list of {@link Amity.User} on a {@link Amity.Community}
|
|
2875
|
+
*
|
|
2876
|
+
* @param communityId The ID of the {@link Amity.Community} to perform
|
|
2877
|
+
* @param role The ID of the {@link Amity.Role} to apply
|
|
2878
|
+
* @param userId Array of IDs of the {@link Amity.User} to perform
|
|
2879
|
+
* @returns A success boolean if the {@link Amity.Role} were removed from list of {@link Amity.User} in the {@link Amity.Community}
|
|
2880
|
+
*
|
|
2881
|
+
* @category Community API
|
|
2882
|
+
* @async
|
|
2883
|
+
*/
|
|
2884
|
+
const removeCommunityMembersRole = async (communityId, roleId, userIds) => {
|
|
2885
|
+
const client = getActiveClient();
|
|
2886
|
+
client.log('community/removeCommunityMembersRole', communityId, roleId, userIds);
|
|
2887
|
+
const { data } = await client.http.delete(`/api/v3/communities/${communityId}/users/roles`, { data: { communityId, role: roleId, userIds } });
|
|
2888
|
+
decoratePayload(data);
|
|
2889
|
+
ingestInCache(data);
|
|
2890
|
+
const { communityUsers } = data;
|
|
2891
|
+
return !!communityUsers.find(communityUser => communityUser.communityId === communityId && !communityUser.roles.includes(roleId));
|
|
2892
|
+
};
|
|
2893
|
+
|
|
2894
|
+
/**
|
|
2895
|
+
* ```js
|
|
2896
|
+
* import { deletePost } from '@amityco/ts-sdk'
|
|
2897
|
+
* const success = await deletePost('foobar')
|
|
2898
|
+
* ```
|
|
2899
|
+
*
|
|
2900
|
+
* Deletes a {@link Amity.Post}
|
|
2901
|
+
*
|
|
2902
|
+
* @param postId The {@link Amity.Post} ID to delete
|
|
2903
|
+
* @return A success boolean if the {@link Amity.Post} was deleted
|
|
2904
|
+
*
|
|
2905
|
+
* @category Post API
|
|
2906
|
+
* @async
|
|
2907
|
+
*/
|
|
2908
|
+
const deletePost = async (postId) => {
|
|
2909
|
+
const client = getActiveClient();
|
|
2910
|
+
client.log('post/deletePost', postId);
|
|
2911
|
+
// API-FIX: This endpoint has not been implemented yet.
|
|
2912
|
+
await client.http.delete(`/api/v3/posts/${postId}`);
|
|
2913
|
+
// API-FIX: as for now we have no choice but to refetch
|
|
2914
|
+
const deleted = await getPost(postId);
|
|
2915
|
+
if (deleted.targetType === 'community') {
|
|
2916
|
+
const community = await getCommunity(deleted.targetId);
|
|
2917
|
+
fireEvent('v3.community.didUpdate', { communities: [community] });
|
|
2918
|
+
}
|
|
2919
|
+
fireEvent('v3.post.didDelete', { posts: [deleted] });
|
|
2920
|
+
return deleted;
|
|
2921
|
+
};
|
|
2922
|
+
|
|
2923
|
+
/**
|
|
2924
|
+
* ```js
|
|
2925
|
+
* import { queryComments } from '@amityco/ts-sdk'
|
|
2926
|
+
* const comments = await queryComments({ postId })
|
|
2927
|
+
* ```
|
|
2928
|
+
*
|
|
2929
|
+
* Queries a paginable list of {@link Amity.Comment} objects
|
|
2930
|
+
*
|
|
2931
|
+
* @param query The query parameters
|
|
2932
|
+
* @returns A page of {@link Amity.Comment} objects
|
|
2933
|
+
*
|
|
2934
|
+
* @category Comment API
|
|
2935
|
+
* @async
|
|
2936
|
+
*/
|
|
2937
|
+
const queryComments = async (query) => {
|
|
2938
|
+
const client = getActiveClient();
|
|
2939
|
+
client.log('comment/queryComments', query);
|
|
2940
|
+
const { page } = query, params = __rest(query, ["page"]);
|
|
2941
|
+
query.parentId !== undefined;
|
|
2942
|
+
// API-FIX: parameters should be querystring. (1)
|
|
2943
|
+
// API-FIX: backend should answer Amity.Response (2)
|
|
2944
|
+
// const { data } = await client.http.get<Amity.Response<Amity.PagedResponse<Amity.CommentPayload>>>(
|
|
2945
|
+
const { data } = await client.http.get(`/api/v3/comments`, {
|
|
2946
|
+
params: Object.assign(Object.assign({}, params), {
|
|
2947
|
+
// filterByParentId, API-FIX: backend does not support this boolean LOL. what the hell seriously.
|
|
2948
|
+
options: {
|
|
2949
|
+
token: toToken(page, 'afterbefore'),
|
|
2950
|
+
} }),
|
|
2951
|
+
});
|
|
2952
|
+
// API-FIX: backend should answer Amity.Response (2)
|
|
2953
|
+
// const { paging, comments } = unwrapPayload(data)
|
|
2954
|
+
const { paging } = data, response = __rest(data, ["paging"]);
|
|
2955
|
+
decoratePayload(response);
|
|
2956
|
+
ingestInCache(response);
|
|
2957
|
+
const prevPage = toPage(paging.previous);
|
|
2958
|
+
const nextPage = toPage(paging.next);
|
|
2959
|
+
const { comments } = response;
|
|
2960
|
+
return { data: toRecord(comments), prevPage, nextPage };
|
|
2961
|
+
};
|
|
2962
|
+
/**
|
|
2963
|
+
* ```js
|
|
2964
|
+
* import { queryComments } from '@amityco/ts-sdk'
|
|
2965
|
+
* const comments = queryComments.locally({ postId })
|
|
2966
|
+
* ```
|
|
2967
|
+
*
|
|
2968
|
+
* Queries a paginable list of {@link Amity.Comment} objects from cache
|
|
2969
|
+
*
|
|
2970
|
+
* @param query The query parameters
|
|
2971
|
+
* @returns comments
|
|
2972
|
+
*
|
|
2973
|
+
* @category Comment API
|
|
2974
|
+
*/
|
|
2975
|
+
queryComments.locally = (query) => {
|
|
2976
|
+
var _a;
|
|
2977
|
+
const client = getActiveClient();
|
|
2978
|
+
client.log('post/queryComments.locally', query);
|
|
2979
|
+
const { referenceId, referenceType, parentId, isDeleted, page = { before: 0, limit: 5 }, } = query;
|
|
2980
|
+
if (isNaN(page.after) && isNaN(page.before))
|
|
2981
|
+
page.before = 0;
|
|
2982
|
+
if (isNaN(page.limit))
|
|
2983
|
+
page.limit = 5;
|
|
2984
|
+
let comments = (_a = pullFromCache('comment')) !== null && _a !== void 0 ? _a : [];
|
|
2985
|
+
// no cache
|
|
2986
|
+
if (!comments.length)
|
|
2987
|
+
return;
|
|
2988
|
+
// filter
|
|
2989
|
+
comments = filterByPropEquality(comments, 'referenceId', referenceId);
|
|
2990
|
+
comments = filterByPropEquality(comments, 'referenceType', referenceType);
|
|
2991
|
+
comments = filterByPropEquality(comments, 'parentId', parentId);
|
|
2992
|
+
comments = filterByPropEquality(comments, 'isDeleted', isDeleted);
|
|
2993
|
+
// no interesting cache
|
|
2994
|
+
if (!comments.length)
|
|
2995
|
+
return;
|
|
2996
|
+
// sort
|
|
2997
|
+
comments = comments.sort(sortBySegmentNumber);
|
|
2998
|
+
// paging
|
|
2999
|
+
// API-FIX we can't reuse proper page because the message page is done
|
|
3000
|
+
// by segmentNumber which is sadly unique to the entire platform.
|
|
3001
|
+
// this part is copied and adapted from sliceByPaging function
|
|
3002
|
+
const afterIndex = !isNaN(page.after)
|
|
3003
|
+
? comments.findIndex(({ segmentNumber }) => segmentNumber === page.after + 1)
|
|
3004
|
+
: 0;
|
|
3005
|
+
const beforeIndex = !isNaN(page.before) && page.before !== 0
|
|
3006
|
+
? comments.findIndex(({ segmentNumber }) => segmentNumber === page.before - 1) + 1
|
|
3007
|
+
: comments.length;
|
|
3008
|
+
if (afterIndex < 0 || beforeIndex < 0)
|
|
3009
|
+
return;
|
|
3010
|
+
let slice = comments.slice(afterIndex !== -1 ? afterIndex : 0, beforeIndex !== -1 ? beforeIndex : undefined);
|
|
3011
|
+
// nothing left after slicing
|
|
3012
|
+
if (!slice.length)
|
|
3013
|
+
return;
|
|
3014
|
+
if (!isNaN(page.after) && !isNaN(page.before)) ;
|
|
3015
|
+
else if (!isNaN(page.after)) {
|
|
3016
|
+
slice = slice.slice(0, page.limit);
|
|
3017
|
+
}
|
|
3018
|
+
else if (!isNaN(page.before)) {
|
|
3019
|
+
slice = slice.slice(-page.limit);
|
|
3020
|
+
}
|
|
3021
|
+
for (let i = 0; i < slice.length - 1; i++)
|
|
3022
|
+
if (slice[i].segmentNumber !== slice[i + 1].segmentNumber - 1)
|
|
3023
|
+
return;
|
|
3024
|
+
const prevPage = slice[0].segmentNumber > 1
|
|
3025
|
+
? { before: slice[0].segmentNumber, limit: page.limit }
|
|
3026
|
+
: undefined;
|
|
3027
|
+
const nextPage = {
|
|
3028
|
+
after: slice[slice.length - 1].segmentNumber,
|
|
3029
|
+
limit: page.limit,
|
|
3030
|
+
};
|
|
3031
|
+
return { data: toRecord(slice), nextPage, prevPage };
|
|
3032
|
+
};
|
|
3033
|
+
|
|
3034
|
+
/**
|
|
3035
|
+
* ```js
|
|
3036
|
+
* import { getComments } from '@amityco/ts-sdk'
|
|
3037
|
+
* const comments = await getComments(['foo', 'bar'])
|
|
3038
|
+
* ```
|
|
3039
|
+
*
|
|
3040
|
+
* Fetches a collection of {@link Amity.Comment} objects
|
|
3041
|
+
*
|
|
3042
|
+
* @param commentIds the IDs of the {@link Amity.Comment} to fetch
|
|
3043
|
+
* @returns the associated collection of {@link Amity.Comment} objects
|
|
3044
|
+
*
|
|
3045
|
+
* @category Comment API
|
|
3046
|
+
* @async
|
|
3047
|
+
*/
|
|
3048
|
+
const getComments = async (commentIds) => {
|
|
3049
|
+
const client = getActiveClient();
|
|
3050
|
+
client.log('comment/getComments', commentIds);
|
|
3051
|
+
// API-FIX: endpoint should not be /list, parameters should be querystring.
|
|
3052
|
+
const { data } = await client.http.get(`/api/v3/comments/list`, { params: { commentIds } });
|
|
3053
|
+
decoratePayload(data);
|
|
3054
|
+
ingestInCache(data);
|
|
3055
|
+
const { comments } = data;
|
|
3056
|
+
return toRecord(comments);
|
|
3057
|
+
};
|
|
3058
|
+
/**
|
|
3059
|
+
* ```js
|
|
3060
|
+
* import { getComments } from '@amityco/ts-sdk'
|
|
3061
|
+
* const comments = getComments.locally(['foo', 'bar'])
|
|
3062
|
+
* ```
|
|
3063
|
+
*
|
|
3064
|
+
* Fetches a collection of {@link Amity.Comment} objects from cache
|
|
3065
|
+
*
|
|
3066
|
+
* @param commentIds the IDs of the {@link Amity.Comment} to fetch
|
|
3067
|
+
* @returns the associated collection of {@link Amity.Comment} objects
|
|
3068
|
+
*
|
|
3069
|
+
* @category Comment API
|
|
3070
|
+
*/
|
|
3071
|
+
getComments.locally = (commentIds) => {
|
|
3072
|
+
var _a;
|
|
3073
|
+
const client = getActiveClient();
|
|
3074
|
+
client.log('comment/getComments.locally', commentIds);
|
|
3075
|
+
const cached = (_a = pullFromCache('comment', ...commentIds)) !== null && _a !== void 0 ? _a : [];
|
|
3076
|
+
if ((cached === null || cached === void 0 ? void 0 : cached.length) === commentIds.length)
|
|
3077
|
+
return toRecord(cached);
|
|
3078
|
+
};
|
|
3079
|
+
|
|
3080
|
+
/**
|
|
3081
|
+
* ```js
|
|
3082
|
+
* import { getComment } from '@amityco/ts-sdk'
|
|
3083
|
+
* const comment = await getComment('foobar')
|
|
3084
|
+
* ```
|
|
3085
|
+
*
|
|
3086
|
+
* Fetches a {@link Amity.Comment} object
|
|
3087
|
+
*
|
|
3088
|
+
* @param commentId the ID of the {@link Amity.Comment} to fetch
|
|
3089
|
+
* @returns the associated {@link Amity.Comment} object
|
|
3090
|
+
*
|
|
3091
|
+
* @category Comment API
|
|
3092
|
+
* @async
|
|
3093
|
+
*/
|
|
3094
|
+
const getComment = async (commentId) => {
|
|
3095
|
+
const client = getActiveClient();
|
|
3096
|
+
client.log('comment/getComment', commentId);
|
|
3097
|
+
// API-FIX: endpoint should not be /list, parameters should be querystring.
|
|
3098
|
+
const { data } = await client.http.get(`/api/v3/comments/${commentId}`);
|
|
3099
|
+
decoratePayload(data);
|
|
3100
|
+
ingestInCache(data);
|
|
3101
|
+
const { comments } = data;
|
|
3102
|
+
return comments.find(comment => comment.commentId === commentId);
|
|
3103
|
+
};
|
|
3104
|
+
/**
|
|
3105
|
+
* ```js
|
|
3106
|
+
* import { getComment } from '@amityco/ts-sdk'
|
|
3107
|
+
* const comment = getComment.locally('foobar')
|
|
3108
|
+
* ```
|
|
3109
|
+
*
|
|
3110
|
+
* Fetches a {@link Amity.Comment} object
|
|
3111
|
+
*
|
|
3112
|
+
* @param commentId the ID of the {@link Amity.Comment} to fetch
|
|
3113
|
+
* @returns the associated {@link Amity.Comment} object
|
|
3114
|
+
*
|
|
3115
|
+
* @category Comment API
|
|
3116
|
+
*/
|
|
3117
|
+
getComment.locally = (commentId) => {
|
|
3118
|
+
var _a;
|
|
3119
|
+
const client = getActiveClient();
|
|
3120
|
+
client.log('comment/getComment.locally', commentId);
|
|
3121
|
+
const [comment] = (_a = pullFromCache('comment', commentId)) !== null && _a !== void 0 ? _a : [];
|
|
3122
|
+
return comment;
|
|
3123
|
+
};
|
|
3124
|
+
|
|
3125
|
+
/**
|
|
3126
|
+
* ```js
|
|
3127
|
+
* import { createComment } from '@amityco/ts-sdk'
|
|
3128
|
+
* const newComment = await createComment(bundle)
|
|
3129
|
+
* ```
|
|
3130
|
+
*
|
|
3131
|
+
* Creates an {@link Amity.Comment}
|
|
3132
|
+
*
|
|
3133
|
+
* @param bundle The data necessary to create a new {@link Amity.Comment}
|
|
3134
|
+
* @returns The newly created {@link Amity.Comment}
|
|
3135
|
+
*
|
|
3136
|
+
* @category Comment API
|
|
3137
|
+
* @async
|
|
3138
|
+
*/
|
|
3139
|
+
const createComment = async (bundle) => {
|
|
3140
|
+
const client = getActiveClient();
|
|
3141
|
+
client.log('comment/createComment', bundle);
|
|
3142
|
+
const { data } = await client.http.post('/api/v3/comments', bundle);
|
|
3143
|
+
decoratePayload(data);
|
|
3144
|
+
ingestInCache(data);
|
|
3145
|
+
const post = await getPost(bundle.referenceId);
|
|
3146
|
+
fireEvent('v3.post.didUpdate', { posts: [post] });
|
|
3147
|
+
fireEvent('v3.comment.didCreate', data);
|
|
3148
|
+
const { comments } = data;
|
|
3149
|
+
return comments[0];
|
|
3150
|
+
};
|
|
3151
|
+
|
|
3152
|
+
/**
|
|
3153
|
+
* ```js
|
|
3154
|
+
* import { updateComment } from '@amityco/ts-sdk'
|
|
3155
|
+
* const updated = await updateComment(commentId, {
|
|
3156
|
+
* data: { text: 'hello world' }
|
|
3157
|
+
* })
|
|
3158
|
+
* ```
|
|
3159
|
+
*
|
|
3160
|
+
* Updates an {@link Amity.Comment}
|
|
3161
|
+
*
|
|
3162
|
+
* @param commentId The ID of the {@link Amity.Comment} to edit
|
|
3163
|
+
* @param patch The patch data to apply
|
|
3164
|
+
* @returns the updated {@link Amity.Comment} object
|
|
3165
|
+
*
|
|
3166
|
+
* @category Comment API
|
|
3167
|
+
* @async
|
|
3168
|
+
*/
|
|
3169
|
+
const updateComment = async (commentId, patch) => {
|
|
3170
|
+
const client = getActiveClient();
|
|
3171
|
+
client.log('user/updateComment', patch);
|
|
3172
|
+
const { data } = await client.http.put(`/api/v3/comments/${commentId}`, patch);
|
|
3173
|
+
decoratePayload(data);
|
|
3174
|
+
ingestInCache(data);
|
|
3175
|
+
fireEvent('v3.comment.didUpdate', data);
|
|
3176
|
+
const { comments } = data;
|
|
3177
|
+
return comments.find(comment => comment.commentId === commentId);
|
|
3178
|
+
};
|
|
3179
|
+
|
|
3180
|
+
/**
|
|
3181
|
+
* ```js
|
|
3182
|
+
* import { deleteComment } from '@amityco/ts-sdk'
|
|
3183
|
+
* const success = await deleteComment('foobar')
|
|
3184
|
+
* ```
|
|
3185
|
+
*
|
|
3186
|
+
* Deletes a {@link Amity.Comment}
|
|
3187
|
+
*
|
|
3188
|
+
* @param commentId The {@link Amity.Comment} ID to delete
|
|
3189
|
+
* @return A success boolean if the {@link Amity.Comment} was deleted
|
|
3190
|
+
*
|
|
3191
|
+
* @category Comment API
|
|
3192
|
+
* @async
|
|
3193
|
+
*/
|
|
3194
|
+
const deleteComment = async (commentId) => {
|
|
3195
|
+
const client = getActiveClient();
|
|
3196
|
+
client.log('comment/deleteComment', commentId);
|
|
3197
|
+
// API-FIX: This endpoint has not been implemented yet.
|
|
3198
|
+
await client.http.delete(`/api/v3/comments/${commentId}`);
|
|
3199
|
+
const deleted = await getComment(commentId);
|
|
3200
|
+
const post = await getPost(deleted.referenceId);
|
|
3201
|
+
fireEvent('v3.post.didUpdate', { posts: [post] });
|
|
3202
|
+
fireEvent('v3.comment.didDelete', { comments: [deleted] });
|
|
3203
|
+
return deleted;
|
|
3204
|
+
};
|
|
3205
|
+
|
|
3206
|
+
/** @hidden */
|
|
3207
|
+
const fetchReference = async (referenceType, referenceId) => {
|
|
3208
|
+
if (referenceType === 'message')
|
|
3209
|
+
return getMessage(referenceId);
|
|
3210
|
+
else if (referenceType === 'post')
|
|
3211
|
+
return getPost(referenceId);
|
|
3212
|
+
else if (referenceType === 'comment')
|
|
3213
|
+
return getComment(referenceId);
|
|
3214
|
+
throw new Error(`Incorrect referenceType: ${referenceType}`);
|
|
3215
|
+
};
|
|
3216
|
+
|
|
3217
|
+
/**
|
|
3218
|
+
* ```js
|
|
3219
|
+
* import { addReaction } from '@amityco/ts-sdk'
|
|
3220
|
+
* const success = await addReaction('post', postId, 'like')
|
|
3221
|
+
* ```
|
|
3222
|
+
*
|
|
3223
|
+
* Creates an {@link Amity.Reaction}
|
|
3224
|
+
*
|
|
3225
|
+
* @param referenceType The type of thing to add a {@link Amity.Reaction} to, such as a post or a comment.
|
|
3226
|
+
* @param referenceId The ID of the thing to add a new {@link Amity.Reaction} to.
|
|
3227
|
+
* @param reactionName Reaction name, such as a `like` or `love`.
|
|
3228
|
+
* @returns The added result.
|
|
3229
|
+
*
|
|
3230
|
+
* @category Reaction API
|
|
3231
|
+
* @async
|
|
3232
|
+
**/
|
|
3233
|
+
const addReaction = async (referenceType, referenceId, reactionName) => {
|
|
3234
|
+
var _a, _b;
|
|
3235
|
+
const client = getActiveClient();
|
|
3236
|
+
client.log('reaction/createReaction', {
|
|
3237
|
+
referenceId,
|
|
3238
|
+
referenceType,
|
|
3239
|
+
reactionName,
|
|
3240
|
+
});
|
|
3241
|
+
await client.http.post('/api/v2/reactions', { referenceId, referenceType, reactionName });
|
|
3242
|
+
const model = await fetchReference(referenceType, referenceId);
|
|
3243
|
+
dispatchReactable(referenceType, model);
|
|
3244
|
+
return (_b = (_a = model.myReactions) === null || _a === void 0 ? void 0 : _a.includes(reactionName)) !== null && _b !== void 0 ? _b : false;
|
|
3245
|
+
};
|
|
3246
|
+
/**
|
|
3247
|
+
* ```js
|
|
3248
|
+
* import { addReaction } from '@amityco/ts-sdk'
|
|
3249
|
+
* const success = addReaction.optimistically('post', postId, 'like')
|
|
3250
|
+
* ```
|
|
3251
|
+
*
|
|
3252
|
+
* Creates an {@link Amity.Reaction} optimistically
|
|
3253
|
+
*
|
|
3254
|
+
* @param referenceType The type of thing to add a {@link Amity.Reaction} to, such as a post or a comment.
|
|
3255
|
+
* @param referenceId The ID of the thing to add a new {@link Amity.Reaction} to.
|
|
3256
|
+
* @param reactionName Reaction name, such as a `like` or `love`.
|
|
3257
|
+
* @returns The added result.
|
|
3258
|
+
*
|
|
3259
|
+
* @category Reaction API
|
|
3260
|
+
**/
|
|
3261
|
+
addReaction.optimistically = (referenceType, referenceId, reactionName) => {
|
|
3262
|
+
var _a, _b;
|
|
3263
|
+
const model = mutateCache(referenceType, referenceId, model => {
|
|
3264
|
+
var _a, _b, _c;
|
|
3265
|
+
if ((_a = model.myReactions) === null || _a === void 0 ? void 0 : _a.includes(reactionName))
|
|
3266
|
+
return;
|
|
3267
|
+
return Object.assign(Object.assign({}, model), { syncedAt: -1, reactionsCount: model.reactionsCount + 1, myReactions: [...((_b = model.myReactions) !== null && _b !== void 0 ? _b : []), reactionName], reactions: Object.assign(Object.assign({}, model.reactions), { [reactionName]: ((_c = model.reactions[reactionName]) !== null && _c !== void 0 ? _c : 0) + 1 }) });
|
|
3268
|
+
});
|
|
3269
|
+
model && dispatchReactable(referenceType, model);
|
|
3270
|
+
return (_b = (_a = model === null || model === void 0 ? void 0 : model.myReactions) === null || _a === void 0 ? void 0 : _a.includes(reactionName)) !== null && _b !== void 0 ? _b : false;
|
|
3271
|
+
};
|
|
3272
|
+
|
|
3273
|
+
/**
|
|
3274
|
+
* ```js
|
|
3275
|
+
* import { deleteReaction } from '@amityco/ts-sdk'
|
|
3276
|
+
* const success = await deleteReaction('post', 'foobar', 'like')
|
|
3277
|
+
* ```
|
|
3278
|
+
*
|
|
3279
|
+
* Removes a {@link Amity.Reaction} from a {@link Amity.Reactable} object
|
|
3280
|
+
*
|
|
3281
|
+
* @param referenceType The type of thing to add a {@link Amity.Reaction} to, such as a post or a comment.
|
|
3282
|
+
* @param referenceId The ID of the thing to add a new {@link Amity.Reaction} to.
|
|
3283
|
+
* @param reactionName Reaction name, such as a `like` or `love`.
|
|
3284
|
+
* @returns The removed result.
|
|
3285
|
+
*
|
|
3286
|
+
* @category Reaction API
|
|
3287
|
+
* @async
|
|
3288
|
+
**/
|
|
3289
|
+
const removeReaction = async (referenceType, referenceId, reactionName) => {
|
|
3290
|
+
var _a, _b;
|
|
3291
|
+
const client = getActiveClient();
|
|
3292
|
+
client.log('reaction/removeReaction', {
|
|
3293
|
+
referenceId,
|
|
3294
|
+
referenceType,
|
|
3295
|
+
reactionName,
|
|
3296
|
+
});
|
|
3297
|
+
await client.http.delete(`/api/v2/reactions`, { data: { referenceId, referenceType, reactionName } });
|
|
3298
|
+
const model = await fetchReference(referenceType, referenceId);
|
|
3299
|
+
dispatchReactable(referenceType, model);
|
|
3300
|
+
return (_b = !((_a = model.myReactions) === null || _a === void 0 ? void 0 : _a.includes(reactionName))) !== null && _b !== void 0 ? _b : false;
|
|
3301
|
+
};
|
|
3302
|
+
/**
|
|
3303
|
+
* ```js
|
|
3304
|
+
* import { removeReaction } from '@amityco/ts-sdk'
|
|
3305
|
+
* const success = removeReaction.optimistically('post', postId, 'like')
|
|
3306
|
+
* ```
|
|
3307
|
+
*
|
|
3308
|
+
* Removes a {@link Amity.Reaction} from a {@link Amity.Reactable} object optimistically
|
|
3309
|
+
*
|
|
3310
|
+
* @param referenceType The type of thing to add a {@link Amity.Reaction} to, such as a post or a comment.
|
|
3311
|
+
* @param referenceId The ID of the thing to add a new {@link Amity.Reaction} to.
|
|
3312
|
+
* @param reactionName Reaction name, such as a `like` or `love`.
|
|
3313
|
+
* @returns The added result.
|
|
3314
|
+
*
|
|
3315
|
+
* @category Reaction API
|
|
3316
|
+
**/
|
|
3317
|
+
removeReaction.optimistically = (referenceType, referenceId, reactionName) => {
|
|
3318
|
+
var _a, _b;
|
|
3319
|
+
const model = mutateCache(referenceType, referenceId, model => {
|
|
3320
|
+
var _a, _b, _c;
|
|
3321
|
+
if (!((_a = model.myReactions) === null || _a === void 0 ? void 0 : _a.includes(reactionName)))
|
|
3322
|
+
return;
|
|
3323
|
+
return Object.assign(Object.assign({}, model), { syncedAt: -1, reactionsCount: Math.max(0, model.reactionsCount - 1), myReactions: ((_b = model.myReactions) !== null && _b !== void 0 ? _b : []).filter(item => item !== reactionName), reactions: Object.assign(Object.assign({}, model.reactions), { [reactionName]: Math.max(0, ((_c = model.reactions[reactionName]) !== null && _c !== void 0 ? _c : 0) - 1) }) });
|
|
3324
|
+
});
|
|
3325
|
+
model && dispatchReactable(referenceType, model);
|
|
3326
|
+
return (_b = !((_a = model === null || model === void 0 ? void 0 : model.myReactions) === null || _a === void 0 ? void 0 : _a.includes(reactionName))) !== null && _b !== void 0 ? _b : false;
|
|
3327
|
+
};
|
|
3328
|
+
|
|
3329
|
+
/**
|
|
3330
|
+
* ```js
|
|
3331
|
+
* import { onReactionRemoved } from '@amityco/ts-sdk'
|
|
3332
|
+
* const dispose = onReactionRemoved(message => {
|
|
3333
|
+
* // ...
|
|
3334
|
+
* })
|
|
3335
|
+
* ```
|
|
3336
|
+
*
|
|
3337
|
+
* Fired when an {@link Amity.Reaction} has been removed
|
|
3338
|
+
*
|
|
3339
|
+
* @param callback The function to call when the event was fired
|
|
3340
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3341
|
+
*
|
|
3342
|
+
* @category Message Events
|
|
3343
|
+
**/
|
|
3344
|
+
const onReactionRemoved = (referenceType, referenceId, callback) => {
|
|
3345
|
+
const client = getActiveClient();
|
|
3346
|
+
const filter = (payload) => {
|
|
3347
|
+
// TODO: lol
|
|
3348
|
+
decoratePayload(payload);
|
|
3349
|
+
ingestInCache(payload);
|
|
3350
|
+
};
|
|
3351
|
+
return createEventSubscriber(client, 'reaction/onReactionRemoved', 'v3.message.didUpdate', filter);
|
|
3352
|
+
};
|
|
3353
|
+
|
|
3354
|
+
var REFERENCE_TYPES = {
|
|
3355
|
+
post: {
|
|
3356
|
+
domainName: 'posts',
|
|
3357
|
+
eventName: 'v3/post.isflagbyme',
|
|
3358
|
+
primaryKey: 'postId',
|
|
3359
|
+
},
|
|
3360
|
+
comment: {
|
|
3361
|
+
domainName: 'comments',
|
|
3362
|
+
eventName: 'v3/comment.isflagbyme',
|
|
3363
|
+
primaryKey: 'commentId',
|
|
3364
|
+
},
|
|
3365
|
+
message: {
|
|
3366
|
+
domainName: 'messages',
|
|
3367
|
+
eventName: 'v3/message.isFlaggedByMe',
|
|
3368
|
+
primaryKey: 'messageId',
|
|
3369
|
+
},
|
|
3370
|
+
user: {
|
|
3371
|
+
domainName: 'users',
|
|
3372
|
+
eventName: 'user.isFlagByMe',
|
|
3373
|
+
primaryKey: 'userId',
|
|
3374
|
+
},
|
|
3375
|
+
};
|
|
3376
|
+
|
|
3377
|
+
/**
|
|
3378
|
+
* ```js
|
|
3379
|
+
* import { createReport } from '@amityco/ts-sdk'
|
|
3380
|
+
* const flagged = await createReport('post', postId)
|
|
3381
|
+
* ```
|
|
3382
|
+
*
|
|
3383
|
+
* @param referenceType The type of thing to add a report to, such as a post or a comment.
|
|
3384
|
+
* @param referenceId The ID of the thing to add a new report to.
|
|
3385
|
+
* @returns the created report result
|
|
3386
|
+
*
|
|
3387
|
+
* @category Report API
|
|
3388
|
+
* @async
|
|
3389
|
+
**/
|
|
3390
|
+
const createReport = async (referenceType, referenceId) => {
|
|
3391
|
+
const client = getActiveClient();
|
|
3392
|
+
client.log('report/createReport', { referenceType, referenceId });
|
|
3393
|
+
const { domainName } = REFERENCE_TYPES[referenceType];
|
|
3394
|
+
const { data } = await client.http.post(`/api/v3/${domainName}/${referenceId}/flag`);
|
|
3395
|
+
decoratePayload(data);
|
|
3396
|
+
ingestInCache(data);
|
|
3397
|
+
fireEvent(`v3.${referenceType}.didUpdate`, data);
|
|
3398
|
+
return !!data;
|
|
3399
|
+
};
|
|
3400
|
+
|
|
3401
|
+
/**
|
|
3402
|
+
* ```js
|
|
3403
|
+
* import { deleteReport } from '@amityco/ts-sdk'
|
|
3404
|
+
* const unflagged = await deleteReport('post', postId)
|
|
3405
|
+
* ```
|
|
3406
|
+
*
|
|
3407
|
+
* @param referenceType The type of thing to delete a report to, such as a post or a comment.
|
|
3408
|
+
* @param referenceId The ID of the thing to delete a report to.
|
|
3409
|
+
* @returns the deleted report result
|
|
3410
|
+
*
|
|
3411
|
+
* @category Report API
|
|
3412
|
+
* @async
|
|
3413
|
+
**/
|
|
3414
|
+
const deleteReport = async (referenceType, referenceId) => {
|
|
3415
|
+
const client = getActiveClient();
|
|
3416
|
+
client.log('report/deleteReport', { referenceType, referenceId });
|
|
3417
|
+
const { domainName } = REFERENCE_TYPES[referenceType];
|
|
3418
|
+
const { data } = await client.http.delete(`/api/v3/${domainName}/${referenceId}/unflag`);
|
|
3419
|
+
decoratePayload(data);
|
|
3420
|
+
ingestInCache(data);
|
|
3421
|
+
fireEvent(`v3.${referenceType}.didUpdate`, data);
|
|
3422
|
+
return !!data;
|
|
3423
|
+
};
|
|
3424
|
+
|
|
3425
|
+
/**
|
|
3426
|
+
* ```js
|
|
3427
|
+
* import { isReportedByMe } from '@amityco/ts-sdk'
|
|
3428
|
+
* const isReported = await isReportedByMe('post', postId)
|
|
3429
|
+
* ```
|
|
3430
|
+
*
|
|
3431
|
+
* @param referenceType The type of thing to check a report to, such as a post or a comment.
|
|
3432
|
+
* @param referenceId The ID of the thing to check a report to.
|
|
3433
|
+
* @returns `true` if the report is created by me, `false` if doesn't.
|
|
3434
|
+
*
|
|
3435
|
+
* @category Report API
|
|
3436
|
+
* @async
|
|
3437
|
+
**/
|
|
3438
|
+
const isReportedByMe = async (referenceType, referenceId) => {
|
|
3439
|
+
var _a, _b;
|
|
3440
|
+
const client = getActiveClient();
|
|
3441
|
+
client.log('report/isReportedByMe', { referenceType, referenceId });
|
|
3442
|
+
const { eventName, primaryKey } = REFERENCE_TYPES[referenceType];
|
|
3443
|
+
const { result, isFlagByMe } = (_a = (await synchronousWSCall(client, eventName, {
|
|
3444
|
+
[primaryKey]: referenceId,
|
|
3445
|
+
}))) !== null && _a !== void 0 ? _a : {};
|
|
3446
|
+
return (_b = result !== null && result !== void 0 ? result : isFlagByMe) !== null && _b !== void 0 ? _b : false;
|
|
3447
|
+
};
|
|
3448
|
+
|
|
3449
|
+
/**
|
|
3450
|
+
* ```js
|
|
3451
|
+
* import { queryChannels } from '@amityco/ts-sdk'
|
|
3452
|
+
* const channels = await queryChannels()
|
|
3453
|
+
* ```
|
|
3454
|
+
*
|
|
3455
|
+
* Queries a paginable list of {@link Amity.Channel} objects
|
|
3456
|
+
* Search is performed by displayName such as `.startsWith(search)`
|
|
3457
|
+
*
|
|
3458
|
+
* @param query The query parameters
|
|
3459
|
+
* @returns A page of {@link Amity.Channel} objects
|
|
3460
|
+
*
|
|
3461
|
+
* @category Channel API
|
|
3462
|
+
* @async
|
|
3463
|
+
*/
|
|
3464
|
+
const queryChannels = async (query) => {
|
|
3465
|
+
const client = getActiveClient();
|
|
3466
|
+
client.log('channel/queryChannels', query);
|
|
3467
|
+
// safe decapsulation
|
|
3468
|
+
const _a = query !== null && query !== void 0 ? query : {}, { page, displayName, membership } = _a, params = __rest(_a, ["page", "displayName", "membership"]);
|
|
3469
|
+
// API-FIX: parameters should be querystring.
|
|
3470
|
+
// API-FIX: backend doesnt answer Amity.Response
|
|
3471
|
+
// const { data } = await client.http.get<Amity.Response<Amity.Paged<ChannelPayload>>>(
|
|
3472
|
+
const { data } = await client.http.get(`/api/v3/channels`, { params: Object.assign(Object.assign({}, params), { keyword: displayName, filter: membership, options: {
|
|
3473
|
+
token: toToken(page, 'skiplimit'),
|
|
3474
|
+
} }) });
|
|
3475
|
+
const { paging } = data, response = __rest(data, ["paging"]);
|
|
3476
|
+
decoratePayload(response);
|
|
3477
|
+
ingestInCache(response);
|
|
3478
|
+
const prevPage = toPage(paging === null || paging === void 0 ? void 0 : paging.previous);
|
|
3479
|
+
const nextPage = toPage(paging === null || paging === void 0 ? void 0 : paging.next);
|
|
3480
|
+
const { channels } = response;
|
|
3481
|
+
return { data: toRecord(channels), prevPage, nextPage };
|
|
3482
|
+
};
|
|
3483
|
+
/**
|
|
3484
|
+
* ```js
|
|
3485
|
+
* import { queryChannels } from '@amityco/ts-sdk'
|
|
3486
|
+
* const channels = queryChannels.locally()
|
|
3487
|
+
* ```
|
|
3488
|
+
*
|
|
3489
|
+
* Queries a paginable list of {@link Amity.Channel} objects from cache
|
|
3490
|
+
*
|
|
3491
|
+
* @param query The query parameters
|
|
3492
|
+
* @returns channels
|
|
3493
|
+
*
|
|
3494
|
+
* @category Channel API
|
|
3495
|
+
*/
|
|
3496
|
+
queryChannels.locally = (query) => {
|
|
3497
|
+
const client = getActiveClient();
|
|
3498
|
+
client.log('channel/queryChannels.locally', query);
|
|
3499
|
+
// TODO
|
|
3500
|
+
};
|
|
3501
|
+
|
|
3502
|
+
/**
|
|
3503
|
+
* ```js
|
|
3504
|
+
* import { getChannels } from '@amityco/ts-sdk'
|
|
3505
|
+
* const channels = await getChannels(['foo', 'bar'])
|
|
3506
|
+
* ```
|
|
3507
|
+
*
|
|
3508
|
+
* Fetches a collection of {@link Amity.Channel} objects
|
|
3509
|
+
*
|
|
3510
|
+
* @param channelIds the IDs of the {@link Amity.Channel} to fetch
|
|
3511
|
+
* @returns the associated collection of {@link Amity.Channel} objects
|
|
3512
|
+
*
|
|
3513
|
+
* @category Channel API
|
|
3514
|
+
* @async
|
|
3515
|
+
*/
|
|
3516
|
+
const getChannels = async (channelIds) => {
|
|
3517
|
+
const client = getActiveClient();
|
|
3518
|
+
client.log('channel/getChannels', channelIds);
|
|
3519
|
+
// API-FIX: endpoint should not be /list, parameters should be querystring.
|
|
3520
|
+
const { data } = await client.http.get(`/api/v3/channels/list`, { params: { channelIds } });
|
|
3521
|
+
decoratePayload(data);
|
|
3522
|
+
ingestInCache(data);
|
|
3523
|
+
const { channels } = data;
|
|
3524
|
+
return toRecord(channels);
|
|
3525
|
+
};
|
|
3526
|
+
/**
|
|
3527
|
+
* ```js
|
|
3528
|
+
* import { getChannels } from '@amityco/ts-sdk'
|
|
3529
|
+
* const channels = getChannels.locally(['foo', 'bar']) ?? []
|
|
3530
|
+
* ```
|
|
3531
|
+
*
|
|
3532
|
+
* Fetches a collection of {@link Amity.Channel} objects from cache
|
|
3533
|
+
*
|
|
3534
|
+
* @param channelIds the IDs of the {@link Amity.Channel} to fetch
|
|
3535
|
+
* @returns the associated collection of {@link Amity.Channel} objects
|
|
3536
|
+
*
|
|
3537
|
+
* @category Channel API
|
|
3538
|
+
*/
|
|
3539
|
+
getChannels.locally = (channelIds) => {
|
|
3540
|
+
var _a;
|
|
3541
|
+
const client = getActiveClient();
|
|
3542
|
+
client.log('channel/getChannels.locally', channelIds);
|
|
3543
|
+
const cached = (_a = pullFromCache('channel', ...channelIds)) !== null && _a !== void 0 ? _a : [];
|
|
3544
|
+
if ((cached === null || cached === void 0 ? void 0 : cached.length) === channelIds.length)
|
|
3545
|
+
return toRecord(cached);
|
|
3546
|
+
};
|
|
3547
|
+
|
|
3548
|
+
/**
|
|
3549
|
+
* ```js
|
|
3550
|
+
* import { getChannel } from '@amityco/ts-sdk'
|
|
3551
|
+
* const channel = await getChannel('foobar')
|
|
3552
|
+
* ```
|
|
3553
|
+
*
|
|
3554
|
+
* Fetches a {@link Amity.Channel} object
|
|
3555
|
+
*
|
|
3556
|
+
* @param channelId the ID of the {@link Amity.Channel} to fetch
|
|
3557
|
+
* @returns the associated {@link Amity.Channel} object
|
|
3558
|
+
*
|
|
3559
|
+
* @category Channel API
|
|
3560
|
+
* @async
|
|
3561
|
+
*/
|
|
3562
|
+
const getChannel = async (channelId) => {
|
|
3563
|
+
const client = getActiveClient();
|
|
3564
|
+
client.log('channel/getChannel', channelId);
|
|
3565
|
+
// API-FIX: endpoint should not be /list, parameters should be querystring.
|
|
3566
|
+
const { data } = await client.http.get(`/api/v3/channels/${channelId}`);
|
|
3567
|
+
decoratePayload(data);
|
|
3568
|
+
ingestInCache(data);
|
|
3569
|
+
const { channels } = data;
|
|
3570
|
+
return channels.find(channel => channel.channelId === channelId);
|
|
3571
|
+
};
|
|
3572
|
+
/**
|
|
3573
|
+
* ```js
|
|
3574
|
+
* import { getChannel } from '@amityco/ts-sdk'
|
|
3575
|
+
* const channel = getChannel.locally('foobar')
|
|
3576
|
+
* ```
|
|
3577
|
+
*
|
|
3578
|
+
* Fetches a {@link Amity.Channel} object from cache
|
|
3579
|
+
*
|
|
3580
|
+
* @param channelId the ID of the {@link Amity.Channel} to fetch
|
|
3581
|
+
* @returns the associated {@link Amity.Channel} object
|
|
3582
|
+
*
|
|
3583
|
+
* @category Channel API
|
|
3584
|
+
*/
|
|
3585
|
+
getChannel.locally = (channelId) => {
|
|
3586
|
+
var _a;
|
|
3587
|
+
const client = getActiveClient();
|
|
3588
|
+
client.log('channel/getChannel.locally', channelId);
|
|
3589
|
+
const [channel] = (_a = pullFromCache('channel', channelId)) !== null && _a !== void 0 ? _a : [];
|
|
3590
|
+
return channel;
|
|
3591
|
+
};
|
|
3592
|
+
|
|
3593
|
+
/**
|
|
3594
|
+
* ```js
|
|
3595
|
+
* import { createChannel } from '@amityco/ts-sdk'
|
|
3596
|
+
* const created = await createChannel({ channelId: 'foobar', displayName: 'foobar' })
|
|
3597
|
+
* ```
|
|
3598
|
+
*
|
|
3599
|
+
* Creates an {@link Amity.Channel}
|
|
3600
|
+
*
|
|
3601
|
+
* @param bundle The data necessary to create a new {@link Amity.Channel}
|
|
3602
|
+
* @returns The newly created {@link Amity.Channel}
|
|
3603
|
+
*
|
|
3604
|
+
* @category Channel API
|
|
3605
|
+
* @async
|
|
3606
|
+
*/
|
|
3607
|
+
const createChannel = async (bundle) => {
|
|
3608
|
+
const client = getActiveClient();
|
|
3609
|
+
client.log('user/createChannel', bundle);
|
|
3610
|
+
const { data } = await client.http.post('/api/v3/channels', bundle);
|
|
3611
|
+
decoratePayload(data);
|
|
3612
|
+
ingestInCache(data);
|
|
3613
|
+
// no need for event, fired by server
|
|
3614
|
+
const { channels } = data;
|
|
3615
|
+
return channels[0];
|
|
3616
|
+
};
|
|
3617
|
+
|
|
3618
|
+
/**
|
|
3619
|
+
* ```js
|
|
3620
|
+
* import { updateChannel } from '@amityco/ts-sdk'
|
|
3621
|
+
* const updated = await updateChannel(channelId, { displayName: 'foobar' })
|
|
3622
|
+
* ```
|
|
3623
|
+
*
|
|
3624
|
+
* Updates an {@link Amity.Channel}
|
|
3625
|
+
*
|
|
3626
|
+
* @param channelId The ID of the {@link Amity.Channel} to edit
|
|
3627
|
+
* @param patch The patch data to apply
|
|
3628
|
+
* @returns the updated {@link Amity.Channel} object
|
|
3629
|
+
*
|
|
3630
|
+
* @category Channel API
|
|
3631
|
+
* @async
|
|
3632
|
+
*/
|
|
3633
|
+
const updateChannel = async (channelId, patch) => {
|
|
3634
|
+
const client = getActiveClient();
|
|
3635
|
+
client.log('channel/updateChannel', channelId, patch);
|
|
3636
|
+
const { data } = await client.http.put(`/api/v3/channels/${channelId}`, patch);
|
|
3637
|
+
decoratePayload(data);
|
|
3638
|
+
ingestInCache(data);
|
|
3639
|
+
// no need for event, fired by server
|
|
3640
|
+
const { channels } = data;
|
|
3641
|
+
return channels.find(channel => channel.channelId === channelId);
|
|
3642
|
+
};
|
|
3643
|
+
|
|
3644
|
+
/**
|
|
3645
|
+
* ```js
|
|
3646
|
+
* import { deleteChannel } from '@amityco/ts-sdk'
|
|
3647
|
+
* const success = await deleteChannel('foobar')
|
|
3648
|
+
* ```
|
|
3649
|
+
*
|
|
3650
|
+
* Deletes a {@link Amity.Channel}
|
|
3651
|
+
*
|
|
3652
|
+
* @param channelId The {@link Amity.Channel} ID to delete
|
|
3653
|
+
* @return A success boolean if the {@link Amity.Channel} was deleted
|
|
3654
|
+
*
|
|
3655
|
+
* @category Channel API
|
|
3656
|
+
* @async
|
|
3657
|
+
*/
|
|
3658
|
+
const deleteChannel = async (channelId) => {
|
|
3659
|
+
const client = getActiveClient();
|
|
3660
|
+
client.log('channel/deleteChannel', channelId);
|
|
3661
|
+
// API-FIX: This endpoint has not been implemented yet.
|
|
3662
|
+
await client.http.delete(`/api/v3/channels/${channelId}`);
|
|
3663
|
+
const deleted = await getChannel(channelId);
|
|
3664
|
+
// no need for event, fired by server
|
|
3665
|
+
return deleted;
|
|
3666
|
+
};
|
|
3667
|
+
|
|
3668
|
+
/**
|
|
3669
|
+
* ```js
|
|
3670
|
+
* import { joinChannel } from '@amityco/ts-sdk'
|
|
3671
|
+
* const isJoined = await joinChannel('foobar')
|
|
3672
|
+
* ```
|
|
3673
|
+
*
|
|
3674
|
+
* Joins a {@link Amity.Channel} object
|
|
3675
|
+
*
|
|
3676
|
+
* @param channelId the {@link Amity.Channel} to join
|
|
3677
|
+
* @returns A success boolean if the {@link Amity.Channel} was joined
|
|
3678
|
+
*
|
|
3679
|
+
* @category Channel API
|
|
3680
|
+
* @async
|
|
3681
|
+
*/
|
|
3682
|
+
const joinChannel = async (channelId) => {
|
|
3683
|
+
const client = getActiveClient();
|
|
3684
|
+
client.log('channel/joinChannel', channelId);
|
|
3685
|
+
const { data } = await client.http.post(`/api/v3/channels/${channelId}/join`);
|
|
3686
|
+
decoratePayload(data);
|
|
3687
|
+
ingestInCache(data);
|
|
3688
|
+
// no need for event, fired by server
|
|
3689
|
+
const { channelUsers } = data;
|
|
3690
|
+
return !!channelUsers.find(channelUser => (channelUser.channelId === channelId
|
|
3691
|
+
&& channelUser.membership === 'member'));
|
|
3692
|
+
};
|
|
3693
|
+
|
|
3694
|
+
/**
|
|
3695
|
+
* ```js
|
|
3696
|
+
* import { leaveChannel } from '@amityco/ts-sdk'
|
|
3697
|
+
* const isLeft = await leaveChannel('foobar')
|
|
3698
|
+
* ```
|
|
3699
|
+
*
|
|
3700
|
+
* Leave a {@link Amity.Channel} object
|
|
3701
|
+
*
|
|
3702
|
+
* @param channelId the {@link Amity.Channel} to leave
|
|
3703
|
+
* @returns A success boolean if the {@link Amity.Channel} was left
|
|
3704
|
+
*
|
|
3705
|
+
* @category Channel API
|
|
3706
|
+
* @async
|
|
3707
|
+
*/
|
|
3708
|
+
const leaveChannel = async (channelId) => {
|
|
3709
|
+
const client = getActiveClient();
|
|
3710
|
+
client.log('channel/leaveChannel', channelId);
|
|
3711
|
+
const { data } = await client.http.delete(`/api/v3/channels/${channelId}/leave`);
|
|
3712
|
+
decoratePayload(data);
|
|
3713
|
+
ingestInCache(data);
|
|
3714
|
+
// no need for event, fired by server
|
|
3715
|
+
const { channelUsers } = data;
|
|
3716
|
+
return !!channelUsers.find(channelUser => (channelUser.channelId === channelId
|
|
3717
|
+
&& channelUser.membership !== 'member'));
|
|
3718
|
+
};
|
|
3719
|
+
|
|
3720
|
+
/**
|
|
3721
|
+
* ```js
|
|
3722
|
+
* import { queryChannelMembers } from '@amityco/ts-sdk'
|
|
3723
|
+
* const channelMembers = await queryChannelMembers({ channelId: 'foo' })
|
|
3724
|
+
* ```
|
|
3725
|
+
*
|
|
3726
|
+
* Queries a paginable list of {@link Amity.ChannelUser} objects
|
|
3727
|
+
*
|
|
3728
|
+
* @param query The query parameters
|
|
3729
|
+
* @returns A page of {@link Amity.ChannelUser} objects
|
|
3730
|
+
*
|
|
3731
|
+
* @category Channel API
|
|
3732
|
+
* @async
|
|
3733
|
+
**/
|
|
3734
|
+
const queryChannelMembers = async (query) => {
|
|
3735
|
+
const client = getActiveClient();
|
|
3736
|
+
client.log('channel/queryChannelMembers', query);
|
|
3737
|
+
const _a = query !== null && query !== void 0 ? query : {}, { page, channelId, membership } = _a, params = __rest(_a, ["page", "channelId", "membership"]);
|
|
3738
|
+
const { data } = await client.http.get(`/api/v3/channels/${channelId}/users`, {
|
|
3739
|
+
params: Object.assign(Object.assign({}, params), { filter: membership, options: {
|
|
3740
|
+
token: toToken(page, 'skiplimit'),
|
|
3741
|
+
} }),
|
|
3742
|
+
});
|
|
3743
|
+
const { paging } = data, response = __rest(data, ["paging"]);
|
|
3744
|
+
decoratePayload(response);
|
|
3745
|
+
ingestInCache(response);
|
|
3746
|
+
const { channelUsers } = response;
|
|
3747
|
+
const nextPage = toPage(paging.next);
|
|
3748
|
+
const prevPage = toPage(paging.previous);
|
|
3749
|
+
return { data: toRecord(channelUsers), nextPage, prevPage };
|
|
3750
|
+
};
|
|
3751
|
+
/**
|
|
3752
|
+
* ```js
|
|
3753
|
+
* import { queryChannelMembers } from '@amityco/ts-sdk'
|
|
3754
|
+
* const channelMembers = await queryChannelMembers(query)
|
|
3755
|
+
* ```
|
|
3756
|
+
*
|
|
3757
|
+
* Queries a paginable list of {@link Amity.Post} objects from cache
|
|
3758
|
+
*
|
|
3759
|
+
* @param query The query parameters
|
|
3760
|
+
* @returns posts
|
|
3761
|
+
*
|
|
3762
|
+
* @category Post API
|
|
3763
|
+
*/
|
|
3764
|
+
queryChannelMembers.locally = (query) => {
|
|
3765
|
+
const client = getActiveClient();
|
|
3766
|
+
client.log('channel/queryChannelMembers', query);
|
|
3767
|
+
// TODO
|
|
3768
|
+
};
|
|
3769
|
+
|
|
3770
|
+
/**
|
|
3771
|
+
* ```js
|
|
3772
|
+
* import { addChannelMembers } from '@amityco/ts-sdk'
|
|
3773
|
+
* const updated = await addChannelMembers(channelId, ['foo', 'bar'])
|
|
3774
|
+
* ```
|
|
3775
|
+
*
|
|
3776
|
+
* Adds a list of {@link Amity.User} to a {@link Amity.Channel}
|
|
3777
|
+
*
|
|
3778
|
+
* @param channelId The ID of the {@link Amity.Channel} to perform
|
|
3779
|
+
* @param userIds The list of IDs {@link Amity.User} to add
|
|
3780
|
+
* @returns A success boolean if the {@link Amity.User} were added to the {@link Amity.Channel}
|
|
3781
|
+
*
|
|
3782
|
+
* @category Channel API
|
|
3783
|
+
* @async
|
|
3784
|
+
*/
|
|
3785
|
+
const addChannelMembers = async (channelId, userIds) => {
|
|
3786
|
+
const client = getActiveClient();
|
|
3787
|
+
client.log('channel/addChannelMembers', channelId, userIds);
|
|
3788
|
+
const { data } = await client.http.post(`/api/v3/channels/${channelId}/users`, { channelId, userIds });
|
|
3789
|
+
decoratePayload(data);
|
|
3790
|
+
ingestInCache(data);
|
|
3791
|
+
fireEvent('v3.channel.didAddUsers', {
|
|
3792
|
+
channels: data.channels,
|
|
3793
|
+
});
|
|
3794
|
+
const { channelUsers } = data;
|
|
3795
|
+
return !!channelUsers.find(channelUser => channelUser.channelId === channelId && channelUser.membership === 'member');
|
|
3796
|
+
};
|
|
3797
|
+
|
|
3798
|
+
/**
|
|
3799
|
+
* ```js
|
|
3800
|
+
* import { removeChannelMembers } from '@amityco/ts-sdk'
|
|
3801
|
+
* const updated = await removeChannelMembers(channelId, ['foo', 'bar'])
|
|
3802
|
+
* ```
|
|
3803
|
+
*
|
|
3804
|
+
* Removes a list of {@link Amity.User} from a {@link Amity.Channel}
|
|
3805
|
+
*
|
|
3806
|
+
* @param channelId The ID of the {@link Amity.Channel} to perform
|
|
3807
|
+
* @param userIds The list of IDs {@link Amity.User} to remove
|
|
3808
|
+
* @returns A success boolean if the list of {@link Amity.User} were removed from the {@link Amity.Channel}
|
|
3809
|
+
*
|
|
3810
|
+
* @category Channel API
|
|
3811
|
+
* @async
|
|
3812
|
+
*/
|
|
3813
|
+
const removeChannelMembers = async (channelId, userIds) => {
|
|
3814
|
+
const client = getActiveClient();
|
|
3815
|
+
client.log('channel/removeChannelMembers', channelId, userIds);
|
|
3816
|
+
const { data } = await client.http.delete(`/api/v3/channels/${channelId}/users`, { data: { channelId, userIds } });
|
|
3817
|
+
decoratePayload(data);
|
|
3818
|
+
ingestInCache(data);
|
|
3819
|
+
fireEvent('v3.channel.didRemoveUsers', {
|
|
3820
|
+
channels: data.channels,
|
|
3821
|
+
});
|
|
3822
|
+
const { channelUsers } = data;
|
|
3823
|
+
return !!channelUsers.find(channelUser => channelUser.channelId === channelId && channelUser.membership !== 'member');
|
|
3824
|
+
};
|
|
3825
|
+
|
|
3826
|
+
/**
|
|
3827
|
+
* ```js
|
|
3828
|
+
* import { addChannelMembersRole } from '@amityco/ts-sdk'
|
|
3829
|
+
* const updated = await addChannelMembersRole(channelId, 'foo', ['bar'])
|
|
3830
|
+
* ```
|
|
3831
|
+
*
|
|
3832
|
+
* Adds an {@link Amity.Role} to a list of {@link Amity.User} on a {@link Amity.Channel}
|
|
3833
|
+
*
|
|
3834
|
+
* @param channelId The ID of the {@link Amity.Channel} to perform
|
|
3835
|
+
* @param roleId The ID of the {@link Amity.Role} to apply
|
|
3836
|
+
* @param userId Array of IDs of the {@link Amity.User} to perform
|
|
3837
|
+
* @returns A success boolean if the {@link Amity.Role} were added to list of {@link Amity.User} in the {@link Amity.Channel}
|
|
3838
|
+
*
|
|
3839
|
+
* @category Channel API
|
|
3840
|
+
* @async
|
|
3841
|
+
*/
|
|
3842
|
+
const addChannelMembersRole = async (channelId, roleId, userIds) => {
|
|
3843
|
+
const client = getActiveClient();
|
|
3844
|
+
client.log('channel/addChannelMembersRole', channelId, roleId, userIds);
|
|
3845
|
+
const { data } = await client.http.post(`/api/v3/channels/${channelId}/users/roles`, { channelId, role: roleId, userIds });
|
|
3846
|
+
decoratePayload(data);
|
|
3847
|
+
ingestInCache(data);
|
|
3848
|
+
const { channelUsers } = data;
|
|
3849
|
+
return !!channelUsers.find(channelUser => channelUser.channelId === channelId && channelUser.roles.includes(roleId));
|
|
3850
|
+
};
|
|
3851
|
+
|
|
3852
|
+
/**
|
|
3853
|
+
* ```js
|
|
3854
|
+
* import { removeChannelMembersRole } from '@amityco/ts-sdk'
|
|
3855
|
+
* const updated = await removeChannelMembersRole(channelId, 'foo', ['bar'])
|
|
3856
|
+
* ```
|
|
3857
|
+
*
|
|
3858
|
+
* Removes an {@link Amity.Role} from a list of {@link Amity.User} on a {@link Amity.Channel}
|
|
3859
|
+
*
|
|
3860
|
+
* @param channelId The ID of the {@link Amity.Channel} to perform
|
|
3861
|
+
* @param roleId The ID of the {@link Amity.Role} to apply
|
|
3862
|
+
* @param userId Array of IDs of the {@link Amity.User} to perform
|
|
3863
|
+
* @returns A success boolean if the {@link Amity.Role} were removed from list of {@link Amity.User} in the {@link Amity.Channel}
|
|
3864
|
+
*
|
|
3865
|
+
* @category Channel API
|
|
3866
|
+
* @async
|
|
3867
|
+
*/
|
|
3868
|
+
const removeChannelMembersRole = async (channelId, roleId, userIds) => {
|
|
3869
|
+
const client = getActiveClient();
|
|
3870
|
+
client.log('channel/removeChannelMembersRole', channelId, roleId, userIds);
|
|
3871
|
+
const { data } = await client.http.delete(`/api/v3/channels/${channelId}/users/roles`, { data: { channelId, role: roleId, userIds } });
|
|
3872
|
+
decoratePayload(data);
|
|
3873
|
+
ingestInCache(data);
|
|
3874
|
+
const { channelUsers } = data;
|
|
3875
|
+
return !!channelUsers.find(channelUser => channelUser.channelId === channelId && !channelUser.roles.includes(roleId));
|
|
3876
|
+
};
|
|
3877
|
+
|
|
3878
|
+
/**
|
|
3879
|
+
* ```js
|
|
3880
|
+
* import { onChannelUpdated } from '@amityco/ts-sdk'
|
|
3881
|
+
* const dispose = onChannelUpdated(channel => {
|
|
3882
|
+
* // ...
|
|
3883
|
+
* })
|
|
3884
|
+
* ```
|
|
3885
|
+
*
|
|
3886
|
+
* Fired when any {@link Amity.Channel} have been updated
|
|
3887
|
+
*
|
|
3888
|
+
* @param callback The function to call when the event was fired
|
|
3889
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3890
|
+
*
|
|
3891
|
+
* @category Channel Events
|
|
3892
|
+
*/
|
|
3893
|
+
const onChannelUpdated = (callback) => {
|
|
3894
|
+
const client = getActiveClient();
|
|
3895
|
+
const filter = (payload) => {
|
|
3896
|
+
decoratePayload(payload);
|
|
3897
|
+
ingestInCache(payload);
|
|
3898
|
+
callback(payload.channels[0]);
|
|
3899
|
+
};
|
|
3900
|
+
return createEventSubscriber(client, 'onChannelUpdated', 'v3.channel.didUpdate', filter);
|
|
3901
|
+
};
|
|
3902
|
+
|
|
3903
|
+
/**
|
|
3904
|
+
* ```js
|
|
3905
|
+
* import { onChannelDeleted } from '@amityco/ts-sdk'
|
|
3906
|
+
* const dispose = onChannelDeleted(channel => {
|
|
3907
|
+
* // ...
|
|
3908
|
+
* })
|
|
3909
|
+
* ```
|
|
3910
|
+
*
|
|
3911
|
+
* Fired when any {@link Amity.Channel} have been deleted
|
|
3912
|
+
*
|
|
3913
|
+
* @param callback The function to call when the event was fired
|
|
3914
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3915
|
+
*
|
|
3916
|
+
* @category Channel Events
|
|
3917
|
+
*/
|
|
3918
|
+
const onChannelDeleted = (callback) => {
|
|
3919
|
+
const client = getActiveClient();
|
|
3920
|
+
const filter = (payload) => {
|
|
3921
|
+
decoratePayload(payload);
|
|
3922
|
+
ingestInCache(payload);
|
|
3923
|
+
callback(payload.channels[0]);
|
|
3924
|
+
};
|
|
3925
|
+
return createEventSubscriber(client, 'onChannelDeleted', 'v3.channel.didDelete', filter);
|
|
3926
|
+
};
|
|
3927
|
+
|
|
3928
|
+
/**
|
|
3929
|
+
* ```js
|
|
3930
|
+
* import { onChannelJoined } from '@amityco/ts-sdk'
|
|
3931
|
+
* const dispose = onChannelJoined(channel => {
|
|
3932
|
+
* // ...
|
|
3933
|
+
* })
|
|
3934
|
+
* ```
|
|
3935
|
+
*
|
|
3936
|
+
* Fired when any {@link Amity.Channel} was joined
|
|
3937
|
+
*
|
|
3938
|
+
* @param callback The function to call when the event was fired
|
|
3939
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3940
|
+
*
|
|
3941
|
+
* @category Channel Events
|
|
3942
|
+
*/
|
|
3943
|
+
const onChannelJoined = (callback) => {
|
|
3944
|
+
const client = getActiveClient();
|
|
3945
|
+
const filter = (payload) => {
|
|
3946
|
+
decoratePayload(payload);
|
|
3947
|
+
ingestInCache(payload);
|
|
3948
|
+
callback(payload.channels[0]);
|
|
3949
|
+
};
|
|
3950
|
+
return createEventSubscriber(client, 'onChannelJoined', 'v3.channel.didJoin', filter);
|
|
3951
|
+
};
|
|
3952
|
+
|
|
3953
|
+
/**
|
|
3954
|
+
* ```js
|
|
3955
|
+
* import { onChannelLeft } from '@amityco/ts-sdk'
|
|
3956
|
+
* const dispose = onChannelLeft(channel => {
|
|
3957
|
+
* // ...
|
|
3958
|
+
* })
|
|
3959
|
+
* ```
|
|
3960
|
+
*
|
|
3961
|
+
* Fired when any {@link Amity.Channel} was left
|
|
3962
|
+
*
|
|
3963
|
+
* @param callback The function to call when the event was fired
|
|
3964
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3965
|
+
*
|
|
3966
|
+
* @category Channel Events
|
|
3967
|
+
*/
|
|
3968
|
+
const onChannelLeft = (callback) => {
|
|
3969
|
+
const client = getActiveClient();
|
|
3970
|
+
const filter = (payload) => {
|
|
3971
|
+
decoratePayload(payload);
|
|
3972
|
+
ingestInCache(payload);
|
|
3973
|
+
callback(payload.channels[0]);
|
|
3974
|
+
};
|
|
3975
|
+
return createEventSubscriber(client, 'onChannelLeft', 'v3.channel.didLeave', filter);
|
|
3976
|
+
};
|
|
3977
|
+
|
|
3978
|
+
/**
|
|
3979
|
+
* ```js
|
|
3980
|
+
* import { onMemberAdded } from '@amityco/ts-sdk'
|
|
3981
|
+
* const dispose = onMemberAdded(channel => {
|
|
3982
|
+
* // ...
|
|
3983
|
+
* })
|
|
3984
|
+
* ```
|
|
3985
|
+
*
|
|
3986
|
+
* Fired when any {@link Amity.User} has been added to any {@link Amity.Channel}
|
|
3987
|
+
*
|
|
3988
|
+
* @param callback The function to call when the event was fired
|
|
3989
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
3990
|
+
*
|
|
3991
|
+
* @category Channel Events
|
|
3992
|
+
*/
|
|
3993
|
+
const onMemberAdded = (callback) => {
|
|
3994
|
+
const client = getActiveClient();
|
|
3995
|
+
const filter = (payload) => {
|
|
3996
|
+
decoratePayload(payload);
|
|
3997
|
+
ingestInCache(payload);
|
|
3998
|
+
callback(payload.channels[0]);
|
|
3999
|
+
};
|
|
4000
|
+
return createEventSubscriber(client, 'onMemberAdded', 'v3.channel.didAddUsers', filter);
|
|
4001
|
+
};
|
|
4002
|
+
|
|
4003
|
+
/**
|
|
4004
|
+
* ```js
|
|
4005
|
+
* import { onMemberRemoved } from '@amityco/ts-sdk'
|
|
4006
|
+
* const dispose = onMemberRemoved(channel => {
|
|
4007
|
+
* // ...
|
|
4008
|
+
* })
|
|
4009
|
+
* ```
|
|
4010
|
+
*
|
|
4011
|
+
* Fired when any {@link Amity.User} has been removed from any {@link Amity.Channel}
|
|
4012
|
+
*
|
|
4013
|
+
* @param callback The function to call when the event was fired
|
|
4014
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4015
|
+
*
|
|
4016
|
+
* @category Channel Events
|
|
4017
|
+
*/
|
|
4018
|
+
const onMemberRemoved = (callback) => {
|
|
4019
|
+
const client = getActiveClient();
|
|
4020
|
+
const filter = (payload) => {
|
|
4021
|
+
decoratePayload(payload);
|
|
4022
|
+
ingestInCache(payload);
|
|
4023
|
+
callback(payload.channels[0]);
|
|
4024
|
+
};
|
|
4025
|
+
return createEventSubscriber(client, 'onMemberRemoved', 'v3.channel.didRemoveUsers', filter);
|
|
4026
|
+
};
|
|
4027
|
+
|
|
4028
|
+
/**
|
|
4029
|
+
* ```js
|
|
4030
|
+
* import { observeChannel } from '@amityco/ts-sdk'
|
|
4031
|
+
*
|
|
4032
|
+
* let channel = {}
|
|
4033
|
+
* const dispose = observeChannel(channelId, ({ data }) => channel = data)
|
|
4034
|
+
* ```
|
|
4035
|
+
*
|
|
4036
|
+
* Observe all mutation on a given {@link Amity.Channel}
|
|
4037
|
+
*
|
|
4038
|
+
* @param channelId the ID of the channel to observe
|
|
4039
|
+
* @param callback the function to call when new data are available
|
|
4040
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the channel
|
|
4041
|
+
*
|
|
4042
|
+
* @category Channel Observer
|
|
4043
|
+
*/
|
|
4044
|
+
const observeChannel = (channelId, callback) => {
|
|
4045
|
+
const { log } = getActiveClient();
|
|
4046
|
+
const timestamp = Date.now();
|
|
4047
|
+
log(`observeChannel(tmpid: ${timestamp}) > listen`);
|
|
4048
|
+
// wrapper function to make sure
|
|
4049
|
+
const router = (result, action) => {
|
|
4050
|
+
var _a, _b, _c;
|
|
4051
|
+
// filter function
|
|
4052
|
+
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.channelId) !== channelId)
|
|
4053
|
+
return;
|
|
4054
|
+
if (callback instanceof Function)
|
|
4055
|
+
return callback(result);
|
|
4056
|
+
if (action !== 'onFetch')
|
|
4057
|
+
(_b = callback.onEvent) === null || _b === void 0 ? void 0 : _b.call(callback, action, result);
|
|
4058
|
+
(_c = callback[action]) === null || _c === void 0 ? void 0 : _c.call(callback, result);
|
|
4059
|
+
};
|
|
4060
|
+
const disposers = [];
|
|
4061
|
+
disposers.push(onChannelUpdated(data => router({ data }, 'onUpdate')), onChannelDeleted(data => router({ data }, 'onDelete')), onChannelJoined(data => router({ data }, 'onJoin')), onChannelLeft(data => router({ data }, 'onLeft')), onMemberAdded(data => router({ data }, 'onMemberAdded')), onMemberRemoved(data => router({ data }, 'onMemberRemoved')));
|
|
4062
|
+
runQuery(createQuery(getChannel, channelId), result => result.data && router(result, 'onFetch'));
|
|
4063
|
+
return () => {
|
|
4064
|
+
log(`observeChannel(tmpid: ${timestamp}) > dispose`);
|
|
4065
|
+
disposers.forEach(fn => fn());
|
|
4066
|
+
};
|
|
4067
|
+
};
|
|
4068
|
+
|
|
4069
|
+
/**
|
|
4070
|
+
* ```js
|
|
4071
|
+
* import { onMessageCreated } from '@amityco/ts-sdk'
|
|
4072
|
+
* const dispose = onMessageCreated(message => {
|
|
4073
|
+
* // ...
|
|
4074
|
+
* })
|
|
4075
|
+
* ```
|
|
4076
|
+
*
|
|
4077
|
+
* Fired when an {@link Amity.Message} has been created
|
|
4078
|
+
*
|
|
4079
|
+
* @param callback The function to call when the event was fired
|
|
4080
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4081
|
+
*
|
|
4082
|
+
* @category Message Events
|
|
4083
|
+
*/
|
|
4084
|
+
const onMessageCreated = (callback) => {
|
|
4085
|
+
const client = getActiveClient();
|
|
4086
|
+
const filter = (payload) => {
|
|
4087
|
+
decoratePayload(payload);
|
|
4088
|
+
ingestInCache(payload);
|
|
4089
|
+
callback(payload.messages[0]);
|
|
4090
|
+
};
|
|
4091
|
+
return createEventSubscriber(client, 'message/onMessageCreated', 'v3.message.didCreate', filter);
|
|
4092
|
+
};
|
|
4093
|
+
|
|
4094
|
+
/**
|
|
4095
|
+
* ```js
|
|
4096
|
+
* import { onMessageUpdated } from '@amityco/ts-sdk'
|
|
4097
|
+
* const dispose = onMessageUpdated(message => {
|
|
4098
|
+
* // ...
|
|
4099
|
+
* })
|
|
4100
|
+
* ```
|
|
4101
|
+
*
|
|
4102
|
+
* Fired when an {@link Amity.Message} has been updated
|
|
4103
|
+
*
|
|
4104
|
+
* @param callback The function to call when the event was fired
|
|
4105
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4106
|
+
*
|
|
4107
|
+
* @category Message Events
|
|
4108
|
+
*/
|
|
4109
|
+
const onMessageUpdated = (callback) => {
|
|
4110
|
+
const client = getActiveClient();
|
|
4111
|
+
const filter = (payload) => {
|
|
4112
|
+
decoratePayload(payload);
|
|
4113
|
+
ingestInCache(payload);
|
|
4114
|
+
callback(payload.messages[0]);
|
|
4115
|
+
};
|
|
4116
|
+
return createEventSubscriber(client, 'message/onMessageUpdated', 'v3.message.didUpdate', filter);
|
|
4117
|
+
};
|
|
4118
|
+
|
|
4119
|
+
/**
|
|
4120
|
+
* ```js
|
|
4121
|
+
* import { onMessageDeleted } from '@amityco/ts-sdk'
|
|
4122
|
+
* const dispose = onMessageDeleted(message => {
|
|
4123
|
+
* // ...
|
|
4124
|
+
* })
|
|
4125
|
+
* ```
|
|
4126
|
+
*
|
|
4127
|
+
* Fired when an {@link Amity.Message} was deleted
|
|
4128
|
+
*
|
|
4129
|
+
* @param callback The function to call when the event was fired
|
|
4130
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4131
|
+
*
|
|
4132
|
+
* @category Message Events
|
|
4133
|
+
*/
|
|
4134
|
+
const onMessageDeleted = (callback) => {
|
|
4135
|
+
const client = getActiveClient();
|
|
4136
|
+
const filter = (payload) => {
|
|
4137
|
+
decoratePayload(payload);
|
|
4138
|
+
ingestInCache(payload);
|
|
4139
|
+
callback(payload.messages[0]);
|
|
4140
|
+
};
|
|
4141
|
+
return createEventSubscriber(client, 'message/onMessageDeleted', 'v3.message.didDelete', filter);
|
|
4142
|
+
};
|
|
4143
|
+
|
|
4144
|
+
/**
|
|
4145
|
+
* ```js
|
|
4146
|
+
* import { observeMessage } from '@amityco/ts-sdk'
|
|
4147
|
+
*
|
|
4148
|
+
* let message = {}
|
|
4149
|
+
* const unsub = observeMessage(messageId, updated => message = updated)
|
|
4150
|
+
* ```
|
|
4151
|
+
*
|
|
4152
|
+
* Observe all mutations on a {@link Amity.Message} given a single messageId
|
|
4153
|
+
*
|
|
4154
|
+
* @param client the client to observe the message with
|
|
4155
|
+
* @param messageId the ID of the message to observe
|
|
4156
|
+
* @param callback the function to call when new data are available
|
|
4157
|
+
* @param policy a query policy option for this observer
|
|
4158
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the message
|
|
4159
|
+
*
|
|
4160
|
+
* @category Message Observer
|
|
4161
|
+
*/
|
|
4162
|
+
const observeMessage = (messageId, callback, policy = 'cache_then_server') => {
|
|
4163
|
+
const { log } = getActiveClient();
|
|
4164
|
+
const timestamp = Date.now();
|
|
4165
|
+
log(`observeMessage(tmpid: ${timestamp}) > listen`);
|
|
4166
|
+
// wrapper function to make sure
|
|
4167
|
+
const router = (result, action) => {
|
|
4168
|
+
var _a, _b, _c;
|
|
4169
|
+
// filter function
|
|
4170
|
+
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.messageId) !== messageId)
|
|
4171
|
+
return;
|
|
4172
|
+
if (callback instanceof Function)
|
|
4173
|
+
return callback(result);
|
|
4174
|
+
if (action !== 'onFetch')
|
|
4175
|
+
(_b = callback.onEvent) === null || _b === void 0 ? void 0 : _b.call(callback, action, result);
|
|
4176
|
+
(_c = callback[action]) === null || _c === void 0 ? void 0 : _c.call(callback, result);
|
|
4177
|
+
};
|
|
4178
|
+
const disposers = [];
|
|
4179
|
+
disposers.push(onMessageUpdated(message => router(message, 'onUpdate')), onMessageDeleted(message => router(message, 'onDelete')));
|
|
4180
|
+
if (policy !== 'no_fetch') {
|
|
4181
|
+
runQuery(createQuery(getMessage, messageId), (result) => result.data && router(result, 'onFetch'), queryOptions(policy));
|
|
4182
|
+
}
|
|
4183
|
+
return () => {
|
|
4184
|
+
log(`observeMessages(tmpid: ${timestamp}) > dispose`);
|
|
4185
|
+
disposers.forEach(fn => fn());
|
|
4186
|
+
};
|
|
4187
|
+
};
|
|
4188
|
+
|
|
4189
|
+
/**
|
|
4190
|
+
* ```js
|
|
4191
|
+
* import { observeMessages } from '@amityco/ts-sdk'
|
|
4192
|
+
*
|
|
4193
|
+
* let messages = []
|
|
4194
|
+
* const unsub = observeMessages(channelId, message => merge(messages, message))
|
|
4195
|
+
* ```
|
|
4196
|
+
*
|
|
4197
|
+
* Observe all mutations on a list of {@link Amity.Message} for a given {@link Amity.Channel} object
|
|
4198
|
+
*
|
|
4199
|
+
* @param client the client to observe the message with
|
|
4200
|
+
* @param channelId the ID of the channel where to observe the messages
|
|
4201
|
+
* @param callback the function to call when new data are available
|
|
4202
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the messages
|
|
4203
|
+
*
|
|
4204
|
+
* @category Message Observer
|
|
4205
|
+
*/
|
|
4206
|
+
const observeMessages = (channelId, callback) => {
|
|
4207
|
+
const { log } = getActiveClient();
|
|
4208
|
+
const timestamp = Date.now();
|
|
4209
|
+
log(`observeMessages(tmpid: ${timestamp}) > listen`);
|
|
4210
|
+
const disposers = [];
|
|
4211
|
+
const router = (message, action) => {
|
|
4212
|
+
var _a, _b;
|
|
4213
|
+
if (message.channelId !== channelId)
|
|
4214
|
+
return;
|
|
4215
|
+
if (callback instanceof Function)
|
|
4216
|
+
return callback(message);
|
|
4217
|
+
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, message);
|
|
4218
|
+
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, message);
|
|
4219
|
+
};
|
|
4220
|
+
disposers.push(onMessageCreated(message => router(message, 'onCreate')), onMessageUpdated(message => router(message, 'onUpdate')), onMessageDeleted(message => router(message, 'onDelete')));
|
|
4221
|
+
return () => {
|
|
4222
|
+
log(`observeMessages(tmpid: ${timestamp}) > dispose`);
|
|
4223
|
+
disposers.forEach(fn => fn());
|
|
4224
|
+
};
|
|
4225
|
+
};
|
|
4226
|
+
|
|
4227
|
+
/**
|
|
4228
|
+
* ```js
|
|
4229
|
+
* import { onCommunityCreated } from '@amityco/ts-sdk'
|
|
4230
|
+
* const dispose = onCommunityCreated(channel => {
|
|
4231
|
+
* // ...
|
|
4232
|
+
* })
|
|
4233
|
+
* ```
|
|
4234
|
+
*
|
|
4235
|
+
* Fired when any {@link Amity.Community} have been created
|
|
4236
|
+
*
|
|
4237
|
+
* @param callback The function to call when the event was fired
|
|
4238
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4239
|
+
*
|
|
4240
|
+
* @category Community Events
|
|
4241
|
+
*/
|
|
4242
|
+
const onCommunityCreated = (callback) => {
|
|
4243
|
+
const client = getActiveClient();
|
|
4244
|
+
const filter = (payload) => {
|
|
4245
|
+
decoratePayload(payload);
|
|
4246
|
+
ingestInCache(payload);
|
|
4247
|
+
callback(payload.communities[0]);
|
|
4248
|
+
};
|
|
4249
|
+
return createEventSubscriber(client, 'onCommunityCreated', 'v3.community.didCreate', filter);
|
|
4250
|
+
};
|
|
4251
|
+
|
|
4252
|
+
/**
|
|
4253
|
+
* ```js
|
|
4254
|
+
* import { onCommunityUpdated } from '@amityco/ts-sdk'
|
|
4255
|
+
* const dispose = onCommunityUpdated(channel => {
|
|
4256
|
+
* // ...
|
|
4257
|
+
* })
|
|
4258
|
+
* ```
|
|
4259
|
+
*
|
|
4260
|
+
* Fired when any {@link Amity.Community} have been updated
|
|
4261
|
+
*
|
|
4262
|
+
* @param callback The function to call when the event was fired
|
|
4263
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4264
|
+
*
|
|
4265
|
+
* @category Community Events
|
|
4266
|
+
*/
|
|
4267
|
+
const onCommunityUpdated = (callback) => {
|
|
4268
|
+
const client = getActiveClient();
|
|
4269
|
+
const filter = (payload) => {
|
|
4270
|
+
decoratePayload(payload);
|
|
4271
|
+
ingestInCache(payload);
|
|
4272
|
+
callback(payload.communities[0]);
|
|
4273
|
+
};
|
|
4274
|
+
return createEventSubscriber(client, 'onCommunityUpdated', 'v3.community.didUpdate', filter);
|
|
4275
|
+
};
|
|
4276
|
+
|
|
4277
|
+
/**
|
|
4278
|
+
* ```js
|
|
4279
|
+
* import { onCommunityDeleted } from '@amityco/ts-sdk'
|
|
4280
|
+
* const dispose = onCommunityDeleted(channel => {
|
|
4281
|
+
* // ...
|
|
4282
|
+
* })
|
|
4283
|
+
* ```
|
|
4284
|
+
*
|
|
4285
|
+
* Fired when any {@link Amity.Community} have been deleted
|
|
4286
|
+
*
|
|
4287
|
+
* @param callback The function to call when the event was fired
|
|
4288
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4289
|
+
*
|
|
4290
|
+
* @category Community Events
|
|
4291
|
+
*/
|
|
4292
|
+
const onCommunityDeleted = (callback) => {
|
|
4293
|
+
const client = getActiveClient();
|
|
4294
|
+
const filter = (payload) => {
|
|
4295
|
+
decoratePayload(payload);
|
|
4296
|
+
ingestInCache(payload);
|
|
4297
|
+
callback(payload.communities[0]);
|
|
4298
|
+
};
|
|
4299
|
+
return createEventSubscriber(client, 'onCommunityDeleted', 'v3.community.didDelete', filter);
|
|
4300
|
+
};
|
|
4301
|
+
|
|
4302
|
+
/**
|
|
4303
|
+
* ```js
|
|
4304
|
+
* import { onCommunityJoined } from '@amityco/ts-sdk'
|
|
4305
|
+
* const dispose = onCommunityJoined(channel => {
|
|
4306
|
+
* // ...
|
|
4307
|
+
* })
|
|
4308
|
+
* ```
|
|
4309
|
+
*
|
|
4310
|
+
* Fired when any {@link Amity.Community} was joined
|
|
4311
|
+
*
|
|
4312
|
+
* @param callback The function to call when the event was fired
|
|
4313
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4314
|
+
*
|
|
4315
|
+
* @category Community Events
|
|
4316
|
+
*/
|
|
4317
|
+
const onCommunityJoined = (callback) => {
|
|
4318
|
+
const client = getActiveClient();
|
|
4319
|
+
const filter = (payload) => {
|
|
4320
|
+
decoratePayload(payload);
|
|
4321
|
+
ingestInCache(payload);
|
|
4322
|
+
callback(payload.communities[0]);
|
|
4323
|
+
};
|
|
4324
|
+
return createEventSubscriber(client, 'onCommunityJoined', 'v3.community.didJoin', filter);
|
|
4325
|
+
};
|
|
4326
|
+
|
|
4327
|
+
/**
|
|
4328
|
+
* ```js
|
|
4329
|
+
* import { onCommunityLeft } from '@amityco/ts-sdk'
|
|
4330
|
+
* const dispose = onCommunityLeft(channel => {
|
|
4331
|
+
* // ...
|
|
4332
|
+
* })
|
|
4333
|
+
* ```
|
|
4334
|
+
*
|
|
4335
|
+
* Fired when any {@link Amity.Community} was left
|
|
4336
|
+
*
|
|
4337
|
+
* @param callback The function to call when the event was fired
|
|
4338
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4339
|
+
*
|
|
4340
|
+
* @category Community Events
|
|
4341
|
+
*/
|
|
4342
|
+
const onCommunityLeft = (callback) => {
|
|
4343
|
+
const client = getActiveClient();
|
|
4344
|
+
const filter = (payload) => {
|
|
4345
|
+
decoratePayload(payload);
|
|
4346
|
+
ingestInCache(payload);
|
|
4347
|
+
callback(payload.communities[0]);
|
|
4348
|
+
};
|
|
4349
|
+
return createEventSubscriber(client, 'onCommunityLeft', 'v3.community.didLeave', filter);
|
|
4350
|
+
};
|
|
4351
|
+
|
|
4352
|
+
/**
|
|
4353
|
+
* ```js
|
|
4354
|
+
* import { observeCommunity } from '@amityco/ts-sdk'
|
|
4355
|
+
*
|
|
4356
|
+
* let community = {}
|
|
4357
|
+
* const dispose = observeCommunity(communityId, updated => community = updated)
|
|
4358
|
+
* ```
|
|
4359
|
+
*
|
|
4360
|
+
* Observe all mutation on a given {@link Amity.Community}
|
|
4361
|
+
*
|
|
4362
|
+
* @param communityId the ID of the community to observe
|
|
4363
|
+
* @param callback the function to call when new data are available
|
|
4364
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the community
|
|
4365
|
+
*
|
|
4366
|
+
* @category Community Observer
|
|
4367
|
+
*/
|
|
4368
|
+
const observeCommunity = (communityId, callback) => {
|
|
4369
|
+
const { log } = getActiveClient();
|
|
4370
|
+
const timestamp = Date.now();
|
|
4371
|
+
log(`observeCommunity(tmpid: ${timestamp}) > listen`);
|
|
4372
|
+
// wrapper function to make sure
|
|
4373
|
+
const router = (result, action) => {
|
|
4374
|
+
var _a, _b, _c;
|
|
4375
|
+
// filter function
|
|
4376
|
+
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.communityId) !== communityId)
|
|
4377
|
+
return;
|
|
4378
|
+
if (callback instanceof Function)
|
|
4379
|
+
return callback(result);
|
|
4380
|
+
if (action !== 'onFetch')
|
|
4381
|
+
(_b = callback.onEvent) === null || _b === void 0 ? void 0 : _b.call(callback, action, result);
|
|
4382
|
+
(_c = callback[action]) === null || _c === void 0 ? void 0 : _c.call(callback, result);
|
|
4383
|
+
};
|
|
4384
|
+
const disposers = [];
|
|
4385
|
+
disposers.push(onCommunityUpdated(data => router({ data }, 'onUpdate')), onCommunityDeleted(data => router({ data }, 'onDelete')), onCommunityJoined(data => router({ data }, 'onJoin')), onCommunityLeft(data => router({ data }, 'onLeft')));
|
|
4386
|
+
runQuery(createQuery(getCommunity, communityId), result => result.data && router(result, 'onFetch'));
|
|
4387
|
+
return () => {
|
|
4388
|
+
log(`observeCommunity(tmpid: ${timestamp}) > dispose`);
|
|
4389
|
+
disposers.forEach(fn => fn());
|
|
4390
|
+
};
|
|
4391
|
+
};
|
|
4392
|
+
|
|
4393
|
+
/**
|
|
4394
|
+
* ```js
|
|
4395
|
+
* import { queryGlobalFeed } from '@amityco/ts-sdk'
|
|
4396
|
+
* const posts = await queryGlobalFeed()
|
|
4397
|
+
* ```
|
|
4398
|
+
*
|
|
4399
|
+
* Queries a paginable list of {@link Amity.Post} objects
|
|
4400
|
+
*
|
|
4401
|
+
* @param query The query parameters
|
|
4402
|
+
* @returns A page of {@link Amity.Post} objects
|
|
4403
|
+
*
|
|
4404
|
+
* @category Feed API
|
|
4405
|
+
* @async
|
|
4406
|
+
**/
|
|
4407
|
+
const queryGlobalFeed = async (query) => {
|
|
4408
|
+
const client = getActiveClient();
|
|
4409
|
+
client.log('feed/queryGlobalFeed', query);
|
|
4410
|
+
const _a = query !== null && query !== void 0 ? query : {}, { page } = _a, params = __rest(_a, ["page"]);
|
|
4411
|
+
const { data } = await client.http.get(`/api/v4/me/global-feeds`, { params: Object.assign(Object.assign({}, params), { options: {
|
|
4412
|
+
token: toToken(page, 'skiplimit')
|
|
4413
|
+
} }) });
|
|
4414
|
+
const { paging } = data, response = __rest(data, ["paging"]);
|
|
4415
|
+
decoratePayload(response);
|
|
4416
|
+
ingestInCache(response);
|
|
4417
|
+
const { posts } = response;
|
|
4418
|
+
const nextPage = toPage(paging.next);
|
|
4419
|
+
const prevPage = toPage(paging.previous);
|
|
4420
|
+
return { data: toRecord(posts), nextPage, prevPage };
|
|
4421
|
+
};
|
|
4422
|
+
|
|
4423
|
+
/**
|
|
4424
|
+
* ```js
|
|
4425
|
+
* import { onPostCreated } from '@amityco/ts-sdk'
|
|
4426
|
+
* const dispose = onPostCreated(post => {
|
|
4427
|
+
* // ...
|
|
4428
|
+
* })
|
|
4429
|
+
* ```
|
|
4430
|
+
*
|
|
4431
|
+
* Fired when an {@link Amity.Post} has been created
|
|
4432
|
+
*
|
|
4433
|
+
* @param callback The function to call when the event was fired
|
|
4434
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4435
|
+
*
|
|
4436
|
+
* @category Post Events
|
|
4437
|
+
*/
|
|
4438
|
+
const onPostCreated = (callback) => {
|
|
4439
|
+
const client = getActiveClient();
|
|
4440
|
+
const filter = (payload) => {
|
|
4441
|
+
decoratePayload(payload);
|
|
4442
|
+
ingestInCache(payload);
|
|
4443
|
+
callback(payload.posts[0]);
|
|
4444
|
+
};
|
|
4445
|
+
return createEventSubscriber(client, 'message/onPostCreated', 'v3.post.didCreate', filter);
|
|
4446
|
+
};
|
|
4447
|
+
|
|
4448
|
+
/**
|
|
4449
|
+
* ```js
|
|
4450
|
+
* import { onPostUpdated } from '@amityco/ts-sdk'
|
|
4451
|
+
* const dispose = onPostUpdated(post => {
|
|
4452
|
+
* // ...
|
|
4453
|
+
* })
|
|
4454
|
+
* ```
|
|
4455
|
+
*
|
|
4456
|
+
* Fired when an {@link Amity.Post} has been updated
|
|
4457
|
+
*
|
|
4458
|
+
* @param callback The function to call when the event was fired
|
|
4459
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4460
|
+
*
|
|
4461
|
+
* @category Post Events
|
|
4462
|
+
*/
|
|
4463
|
+
const onPostUpdated = (callback) => {
|
|
4464
|
+
const client = getActiveClient();
|
|
4465
|
+
const filter = (payload) => {
|
|
4466
|
+
decoratePayload(payload);
|
|
4467
|
+
ingestInCache(payload);
|
|
4468
|
+
callback(payload.posts[0]);
|
|
4469
|
+
};
|
|
4470
|
+
return createEventSubscriber(client, 'message/onPostUpdated', 'v3.post.didUpdate', filter);
|
|
4471
|
+
};
|
|
4472
|
+
|
|
4473
|
+
/**
|
|
4474
|
+
* ```js
|
|
4475
|
+
* import { onPostDeleted } from '@amityco/ts-sdk'
|
|
4476
|
+
* const dispose = onPostDeleted(post => {
|
|
4477
|
+
* // ...
|
|
4478
|
+
* })
|
|
4479
|
+
* ```
|
|
4480
|
+
*
|
|
4481
|
+
* Fired when an {@link Amity.Post} has been deleted
|
|
4482
|
+
*
|
|
4483
|
+
* @param callback The function to call when the event was fired
|
|
4484
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4485
|
+
*
|
|
4486
|
+
* @category Post Events
|
|
4487
|
+
*/
|
|
4488
|
+
const onPostDeleted = (callback) => {
|
|
4489
|
+
const client = getActiveClient();
|
|
4490
|
+
const filter = (payload) => {
|
|
4491
|
+
decoratePayload(payload);
|
|
4492
|
+
ingestInCache(payload);
|
|
4493
|
+
callback(payload.posts[0]);
|
|
4494
|
+
};
|
|
4495
|
+
return createEventSubscriber(client, 'message/onPostDeleted', 'v3.post.didDelete', filter);
|
|
4496
|
+
};
|
|
4497
|
+
|
|
4498
|
+
/**
|
|
4499
|
+
* ```js
|
|
4500
|
+
* import { observePosts } from '@amityco/ts-sdk'
|
|
4501
|
+
*
|
|
4502
|
+
* let posts = []
|
|
4503
|
+
* const unsub = observePosts({
|
|
4504
|
+
* targetType: Amity.PostTargetType,
|
|
4505
|
+
* targetId: Amity.Post['targetId'],
|
|
4506
|
+
* }, post => merge(posts, post))
|
|
4507
|
+
* ```
|
|
4508
|
+
*
|
|
4509
|
+
* Observe all mutations on a list of {@link Amity.Post} for a given target object
|
|
4510
|
+
*
|
|
4511
|
+
* @param targetType the type of the target
|
|
4512
|
+
* @param targetId the ID of the target
|
|
4513
|
+
* @param callback the function to call when new data are available
|
|
4514
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the messages
|
|
4515
|
+
*
|
|
4516
|
+
* @category Post Observer
|
|
4517
|
+
*/
|
|
4518
|
+
const observePosts = (params, callback) => {
|
|
4519
|
+
const { log } = getActiveClient();
|
|
4520
|
+
const timestamp = Date.now();
|
|
4521
|
+
log(`observePosts(tmpid: ${timestamp}) > listen`);
|
|
4522
|
+
const disposers = [];
|
|
4523
|
+
const router = (post, action) => {
|
|
4524
|
+
var _a, _b;
|
|
4525
|
+
if (params.targetId !== post.targetId
|
|
4526
|
+
|| params.targetType !== post.targetType)
|
|
4527
|
+
return;
|
|
4528
|
+
if (callback instanceof Function)
|
|
4529
|
+
return callback(post);
|
|
4530
|
+
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, post);
|
|
4531
|
+
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, post);
|
|
4532
|
+
};
|
|
4533
|
+
disposers.push(onPostCreated(post => router(post, 'onCreate')), onPostUpdated(post => router(post, 'onUpdate')), onPostDeleted(post => router(post, 'onDelete')));
|
|
4534
|
+
return () => {
|
|
4535
|
+
log(`observePosts(tmpid: ${timestamp}) > dispose`);
|
|
4536
|
+
disposers.forEach(fn => fn());
|
|
4537
|
+
};
|
|
4538
|
+
};
|
|
4539
|
+
|
|
4540
|
+
/**
|
|
4541
|
+
* ```js
|
|
4542
|
+
* import { observePost } from '@amityco/ts-sdk'
|
|
4543
|
+
*
|
|
4544
|
+
* let post = {}
|
|
4545
|
+
* const dispose = observePost(postId, updated => post = updated)
|
|
4546
|
+
* ```
|
|
4547
|
+
*
|
|
4548
|
+
* Observe all mutation on a given {@link Amity.Post}
|
|
4549
|
+
*
|
|
4550
|
+
* @param postId the ID of the post to observe
|
|
4551
|
+
* @param callback the function to call when new data are available
|
|
4552
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the post
|
|
4553
|
+
*
|
|
4554
|
+
* @category Post Observer
|
|
4555
|
+
*/
|
|
4556
|
+
const observePost = (postId, callback, policy = 'cache_then_server') => {
|
|
4557
|
+
const { log } = getActiveClient();
|
|
4558
|
+
const timestamp = Date.now();
|
|
4559
|
+
log(`observePost(tmpid: ${timestamp}) > listen`);
|
|
4560
|
+
// wrapper function to make sure
|
|
4561
|
+
const router = (result, action) => {
|
|
4562
|
+
var _a, _b, _c;
|
|
4563
|
+
// filter function
|
|
4564
|
+
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.postId) !== postId)
|
|
4565
|
+
return;
|
|
4566
|
+
if (callback instanceof Function)
|
|
4567
|
+
return callback(result);
|
|
4568
|
+
if (action !== 'onFetch')
|
|
4569
|
+
(_b = callback.onEvent) === null || _b === void 0 ? void 0 : _b.call(callback, action, result);
|
|
4570
|
+
(_c = callback[action]) === null || _c === void 0 ? void 0 : _c.call(callback, result);
|
|
4571
|
+
};
|
|
4572
|
+
const disposers = [];
|
|
4573
|
+
disposers.push(onPostUpdated(data => router({ data }, 'onUpdate')), onPostDeleted(data => router({ data }, 'onDelete')));
|
|
4574
|
+
runQuery(createQuery(getPost, postId), (result) => result.data && router(result, 'onFetch'), queryOptions(policy));
|
|
4575
|
+
return () => {
|
|
4576
|
+
log(`observePosts(tmpid: ${timestamp}) > dispose`);
|
|
4577
|
+
disposers.forEach(fn => fn());
|
|
4578
|
+
};
|
|
4579
|
+
};
|
|
4580
|
+
|
|
4581
|
+
/**
|
|
4582
|
+
* ```js
|
|
4583
|
+
* import { onCommentCreated } from '@amityco/ts-sdk'
|
|
4584
|
+
* const dispose = onCommentCreated(comment => {
|
|
4585
|
+
* // ...
|
|
4586
|
+
* })
|
|
4587
|
+
* ```
|
|
4588
|
+
*
|
|
4589
|
+
* Fired when an {@link Amity.Comment} has been created
|
|
4590
|
+
*
|
|
4591
|
+
* @param callback The function to call when the event was fired
|
|
4592
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4593
|
+
*
|
|
4594
|
+
* @category Comment Events
|
|
4595
|
+
*/
|
|
4596
|
+
const onCommentCreated = (callback) => {
|
|
4597
|
+
const client = getActiveClient();
|
|
4598
|
+
const filter = (payload) => {
|
|
4599
|
+
decoratePayload(payload);
|
|
4600
|
+
ingestInCache(payload);
|
|
4601
|
+
callback(payload.comments[0]);
|
|
4602
|
+
};
|
|
4603
|
+
return createEventSubscriber(client, 'message/onCommentCreated', 'v3.comment.didCreate', filter);
|
|
4604
|
+
};
|
|
4605
|
+
|
|
4606
|
+
/**
|
|
4607
|
+
* ```js
|
|
4608
|
+
* import { onCommentUpdated } from '@amityco/ts-sdk'
|
|
4609
|
+
* const dispose = onCommentUpdated(comment => {
|
|
4610
|
+
* // ...
|
|
4611
|
+
* })
|
|
4612
|
+
* ```
|
|
4613
|
+
*
|
|
4614
|
+
* Fired when an {@link Amity.Comment} has been updated
|
|
4615
|
+
*
|
|
4616
|
+
* @param callback The function to call when the event was fired
|
|
4617
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4618
|
+
*
|
|
4619
|
+
* @category Comment Events
|
|
4620
|
+
*/
|
|
4621
|
+
const onCommentUpdated = (callback) => {
|
|
4622
|
+
const client = getActiveClient();
|
|
4623
|
+
const filter = (payload) => {
|
|
4624
|
+
decoratePayload(payload);
|
|
4625
|
+
ingestInCache(payload);
|
|
4626
|
+
callback(payload.comments[0]);
|
|
4627
|
+
};
|
|
4628
|
+
return createEventSubscriber(client, 'message/onCommentUpdated', 'v3.comment.didUpdate', filter);
|
|
4629
|
+
};
|
|
4630
|
+
|
|
4631
|
+
/**
|
|
4632
|
+
* ```js
|
|
4633
|
+
* import { onCommentDeleted } from '@amityco/ts-sdk'
|
|
4634
|
+
* const dispose = onCommentDeleted(comment => {
|
|
4635
|
+
* // ...
|
|
4636
|
+
* })
|
|
4637
|
+
* ```
|
|
4638
|
+
*
|
|
4639
|
+
* Fired when an {@link Amity.Comment} has been deleted
|
|
4640
|
+
*
|
|
4641
|
+
* @param callback The function to call when the event was fired
|
|
4642
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
4643
|
+
*
|
|
4644
|
+
* @category Comment Events
|
|
4645
|
+
*/
|
|
4646
|
+
const onCommentDeleted = (callback) => {
|
|
4647
|
+
const client = getActiveClient();
|
|
4648
|
+
const filter = (payload) => {
|
|
4649
|
+
decoratePayload(payload);
|
|
4650
|
+
ingestInCache(payload);
|
|
4651
|
+
callback(payload.comments[0]);
|
|
4652
|
+
};
|
|
4653
|
+
return createEventSubscriber(client, 'message/onCommentDeleted', 'v3.comment.didDelete', filter);
|
|
4654
|
+
};
|
|
4655
|
+
|
|
4656
|
+
/**
|
|
4657
|
+
* ```js
|
|
4658
|
+
* import { observeComments } from '@amityco/ts-sdk'
|
|
4659
|
+
*
|
|
4660
|
+
* let comments = []
|
|
4661
|
+
* const unsub = observeComments(postId, comment => merge(comments, comment))
|
|
4662
|
+
* ```
|
|
4663
|
+
*
|
|
4664
|
+
* Observe all mutations on a list of {@link Amity.Comment} for a given {@link Amity.Post} object
|
|
4665
|
+
*
|
|
4666
|
+
* @param postId the ID of the post where to observe the comments
|
|
4667
|
+
* @param callback the function to call when new data are available
|
|
4668
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the comments
|
|
4669
|
+
*
|
|
4670
|
+
* @category Comment Observer
|
|
4671
|
+
*/
|
|
4672
|
+
const observeComments = (postId, callback) => {
|
|
4673
|
+
const { log } = getActiveClient();
|
|
4674
|
+
const timestamp = Date.now();
|
|
4675
|
+
log(`observeComments(tmpid: ${timestamp}) > listen`);
|
|
4676
|
+
const disposers = [];
|
|
4677
|
+
const router = (comment, action) => {
|
|
4678
|
+
var _a, _b;
|
|
4679
|
+
if (comment.referenceId !== postId)
|
|
4680
|
+
return;
|
|
4681
|
+
if (callback instanceof Function)
|
|
4682
|
+
return callback(comment);
|
|
4683
|
+
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, comment);
|
|
4684
|
+
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, comment);
|
|
4685
|
+
};
|
|
4686
|
+
disposers.push(onCommentCreated(comment => router(comment, 'onCreate')), onCommentUpdated(comment => router(comment, 'onUpdate')), onCommentDeleted(comment => router(comment, 'onDelete')));
|
|
4687
|
+
return () => {
|
|
4688
|
+
log(`observeComments(tmpid: ${timestamp}) > dispose`);
|
|
4689
|
+
disposers.forEach(fn => fn());
|
|
4690
|
+
};
|
|
4691
|
+
};
|
|
4692
|
+
|
|
4693
|
+
/**
|
|
4694
|
+
* ```js
|
|
4695
|
+
* import { observeComment } from '@amityco/ts-sdk'
|
|
4696
|
+
*
|
|
4697
|
+
* let comment = {}
|
|
4698
|
+
* const dispose = observeComment(commentId, ({ data }) => comment = data)
|
|
4699
|
+
* ```
|
|
4700
|
+
*
|
|
4701
|
+
* Observe all mutation on a given {@link Amity.Comment}
|
|
4702
|
+
*
|
|
4703
|
+
* @param commentId the ID of the comment to observe
|
|
4704
|
+
* @param callback the function to call when new data are available
|
|
4705
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the comment
|
|
4706
|
+
*
|
|
4707
|
+
* @category Comment Observer
|
|
4708
|
+
*/
|
|
4709
|
+
const observeComment = (commentId, callback, policy = 'cache_then_server') => {
|
|
4710
|
+
const { log } = getActiveClient();
|
|
4711
|
+
const timestamp = Date.now();
|
|
4712
|
+
log(`observeComment(tmpid: ${timestamp}) > listen`);
|
|
4713
|
+
// wrapper function to make sure
|
|
4714
|
+
const router = (result, action) => {
|
|
4715
|
+
var _a, _b, _c;
|
|
4716
|
+
// filter function
|
|
4717
|
+
if (((_a = result.data) === null || _a === void 0 ? void 0 : _a.commentId) !== commentId)
|
|
4718
|
+
return;
|
|
4719
|
+
if (callback instanceof Function)
|
|
4720
|
+
return callback(result);
|
|
4721
|
+
if (action !== 'onFetch')
|
|
4722
|
+
(_b = callback.onEvent) === null || _b === void 0 ? void 0 : _b.call(callback, action, result);
|
|
4723
|
+
(_c = callback[action]) === null || _c === void 0 ? void 0 : _c.call(callback, result);
|
|
4724
|
+
};
|
|
4725
|
+
const disposers = [];
|
|
4726
|
+
disposers.push(onCommentUpdated(data => router({ data }, 'onUpdate')), onCommentDeleted(data => router({ data }, 'onDelete')));
|
|
4727
|
+
runQuery(createQuery(getComment, commentId), result => result.data && router(result, 'onFetch'), queryOptions(policy));
|
|
4728
|
+
return () => {
|
|
4729
|
+
log(`observeComment(tmpid: ${timestamp}) > dispose`);
|
|
4730
|
+
disposers.forEach(fn => fn());
|
|
4731
|
+
};
|
|
4732
|
+
};
|
|
4733
|
+
|
|
4734
|
+
export { addChannelMembers, addChannelMembersRole, addCommunityMembers, addCommunityMembersRole, addReaction, clearCache, connectClient, createCache, createChannel, createClient, createComment, createCommunity, createFile, createMessage, createPost, createQuery, createReport, decorateModel, decorateModels, decoratePayload, deleteChannel, deleteComment, deleteCommunity, deleteFile, deleteMessage, deletePost, deleteReport, disconnectClient, dropFromCache, enableCache, fileUrlWithSize, filterByPropEquality, getActiveClient, getChannel, getChannels, getComment, getComments, getCommunities, getCommunity, getFile, getMessage, getMessages, getPost, getPosts, getRole, getUser, getUsers, ingestInCache, isAfterBefore, isConnected, isFetcher, isFresh, isLocal, isMutator, isOffline, isPaged, isReportedByMe, isSkipLimit, isSyncable, isSynced, joinChannel, joinCommunity, leaveChannel, leaveCommunity, memoryStore, memoryStores, mergeInCache, mutateCache, observeChannel, observeComment, observeComments, observeCommunity, observeFile, observeMessage, observeMessages, observePost, observePosts, observeUser, onChannelDeleted, onChannelJoined, onChannelLeft, onChannelUpdated, onClientDisconnected, onCommentCreated, onCommentDeleted, onCommentUpdated, onCommunityCreated, onCommunityDeleted, onCommunityJoined, onCommunityLeft, onCommunityUpdated, onMemberAdded, onMemberRemoved, onMessageCreated, onMessageDeleted, onMessageUpdated, onPostCreated, onPostDeleted, onPostUpdated, onReactionRemoved, onUserUpdated, persistentStore, pullFromCache, pushToCache, queryChannelMembers, queryChannels, queryComments, queryCommunities, queryCommunityMembers, queryGlobalFeed, queryMessages, queryOptions, queryPosts, queryRoles, queryUsers, removeChannelMembers, removeChannelMembersRole, removeCommunityMembers, removeCommunityMembersRole, removeReaction, runQuery, sliceByPaging, sortByChannelSegment, sortByDisplayName, sortByFirstCreated, sortByLastActivity, sortByLastCreated, sortBySegmentNumber, syncInCache, toPage, toToken, updateChannel, updateComment, updateCommunity, updateMessage, updatePost, updateUser };
|