@amityco/ts-sdk 0.0.1-beta.33 → 0.0.1-beta.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/@types/core/errors.d.ts +1 -0
- package/dist/@types/core/errors.d.ts.map +1 -1
- package/dist/@types/core/events.d.ts +51 -22
- package/dist/@types/core/events.d.ts.map +1 -1
- package/dist/@types/core/model.d.ts +2 -0
- package/dist/@types/core/model.d.ts.map +1 -1
- package/dist/@types/core/payload.d.ts +15 -5
- package/dist/@types/core/payload.d.ts.map +1 -1
- package/dist/@types/domains/channel.d.ts +12 -7
- package/dist/@types/domains/channel.d.ts.map +1 -1
- package/dist/@types/domains/client.d.ts +40 -1
- package/dist/@types/domains/client.d.ts.map +1 -1
- package/dist/@types/domains/content.d.ts +2 -3
- package/dist/@types/domains/content.d.ts.map +1 -1
- package/dist/@types/domains/group.d.ts +6 -2
- package/dist/@types/domains/group.d.ts.map +1 -1
- package/dist/@types/domains/message.d.ts +37 -17
- package/dist/@types/domains/message.d.ts.map +1 -1
- package/dist/@types/domains/post.d.ts +2 -2
- package/dist/@types/domains/post.d.ts.map +1 -1
- package/dist/@types/domains/subChannel.d.ts +39 -0
- package/dist/@types/domains/subChannel.d.ts.map +1 -0
- package/dist/@types/global.d.ts +7 -0
- package/dist/@types/global.d.ts.map +1 -1
- package/dist/@types/index.d.ts +1 -0
- package/dist/@types/index.d.ts.map +1 -1
- package/dist/cache/api/tests/dropFromCache.test.d.ts +2 -0
- package/dist/cache/api/tests/dropFromCache.test.d.ts.map +1 -0
- package/dist/cache/api/tests/enableCache.test.d.ts +2 -0
- package/dist/cache/api/tests/enableCache.test.d.ts.map +1 -0
- package/dist/cache/api/tests/pullFromCache.test.d.ts +2 -0
- package/dist/cache/api/tests/pullFromCache.test.d.ts.map +1 -0
- package/dist/cache/api/tests/upsertInCache.test.d.ts +2 -0
- package/dist/cache/api/tests/upsertInCache.test.d.ts.map +1 -0
- package/dist/cache/utils/tests/encodeKey.test.d.ts +2 -0
- package/dist/cache/utils/tests/encodeKey.test.d.ts.map +1 -0
- package/dist/cache/utils/tests/partialMatch.test.d.ts +2 -0
- package/dist/cache/utils/tests/partialMatch.test.d.ts.map +1 -0
- package/dist/channel/api/banChannelMembers.d.ts.map +1 -1
- package/dist/channel/api/createChannel.d.ts +1 -1
- package/dist/channel/api/index.d.ts +2 -0
- package/dist/channel/api/index.d.ts.map +1 -1
- package/dist/channel/api/queryChannelMembers.d.ts.map +1 -1
- package/dist/channel/api/removeChannelMembers.d.ts.map +1 -1
- package/dist/channel/api/startReading.d.ts +17 -0
- package/dist/channel/api/startReading.d.ts.map +1 -0
- package/dist/channel/api/stopReading.d.ts +16 -0
- package/dist/channel/api/stopReading.d.ts.map +1 -0
- package/dist/channel/api/tests/createChannel.test.d.ts +2 -0
- package/dist/channel/api/tests/createChannel.test.d.ts.map +1 -0
- package/dist/channel/api/tests/joinChannel.test.d.ts +2 -0
- package/dist/channel/api/tests/joinChannel.test.d.ts.map +1 -0
- package/dist/channel/api/tests/removeChannelMembers.test.d.ts +2 -0
- package/dist/channel/api/tests/removeChannelMembers.test.d.ts.map +1 -0
- package/dist/channel/api/tests/startReading.test.d.ts +2 -0
- package/dist/channel/api/tests/startReading.test.d.ts.map +1 -0
- package/dist/channel/api/tests/stopReading.test.d.ts +2 -0
- package/dist/channel/api/tests/stopReading.test.d.ts.map +1 -0
- package/dist/channel/api/unbanChannelMembers.d.ts.map +1 -1
- package/dist/channel/events/index.d.ts +1 -0
- package/dist/channel/events/index.d.ts.map +1 -1
- package/dist/channel/events/onChannelJoined.d.ts +2 -2
- package/dist/channel/events/onChannelJoined.d.ts.map +1 -1
- package/dist/channel/events/onChannelLeft.d.ts +2 -2
- package/dist/channel/events/onChannelLeft.d.ts.map +1 -1
- package/dist/channel/events/onChannelMemberAdded.d.ts.map +1 -1
- package/dist/channel/events/onChannelMemberBanned.d.ts.map +1 -1
- package/dist/channel/events/onChannelMemberRemoved.d.ts.map +1 -1
- package/dist/channel/events/onChannelMemberUnbanned.d.ts.map +1 -1
- package/dist/channel/events/onChannelMuted.d.ts +17 -0
- package/dist/channel/events/onChannelMuted.d.ts.map +1 -0
- package/dist/channel/events/onChannelUpdated.d.ts +1 -1
- package/dist/channel/events/onChannelUpdated.d.ts.map +1 -1
- package/dist/channel/events/tests/onChannelCreated.test.d.ts +2 -0
- package/dist/channel/events/tests/onChannelCreated.test.d.ts.map +1 -0
- package/dist/channel/events/tests/onChannelJoined.test.d.ts +2 -0
- package/dist/channel/events/tests/onChannelJoined.test.d.ts.map +1 -0
- package/dist/channel/events/tests/onChannelMemberAdded.test.d.ts +2 -0
- package/dist/channel/events/tests/onChannelMemberAdded.test.d.ts.map +1 -0
- package/dist/channel/events/tests/onChannelMemberRemoved.test.d.ts +2 -0
- package/dist/channel/events/tests/onChannelMemberRemoved.test.d.ts.map +1 -0
- package/dist/channel/events/tests/onChannelUpdated.test.d.ts +2 -0
- package/dist/channel/events/tests/onChannelUpdated.test.d.ts.map +1 -0
- package/dist/channel/observers/index.d.ts +1 -0
- package/dist/channel/observers/index.d.ts.map +1 -1
- package/dist/channel/observers/liveChannel.d.ts +21 -0
- package/dist/channel/observers/liveChannel.d.ts.map +1 -0
- package/dist/channel/observers/liveChannelMembers.d.ts.map +1 -1
- package/dist/channel/observers/liveChannels.d.ts.map +1 -1
- package/dist/channel/observers/observeChannel.d.ts +1 -1
- package/dist/channel/observers/observeChannel.d.ts.map +1 -1
- package/dist/channel/observers/observeChannels.d.ts.map +1 -1
- package/dist/channel/utils/index.d.ts +2 -1
- package/dist/channel/utils/index.d.ts.map +1 -1
- package/dist/client/api/accessTokenExpiryWatcher.d.ts +4 -0
- package/dist/client/api/accessTokenExpiryWatcher.d.ts.map +1 -0
- package/dist/client/api/activeUser.d.ts +1 -1
- package/dist/client/api/activeUser.d.ts.map +1 -1
- package/dist/client/api/connectClient.d.ts +1 -1
- package/dist/client/api/connectClient.d.ts.map +1 -1
- package/dist/client/api/createClient.d.ts +1 -1
- package/dist/client/api/createClient.d.ts.map +1 -1
- package/dist/client/api/disconnectClient.d.ts +0 -1
- package/dist/client/api/disconnectClient.d.ts.map +1 -1
- package/dist/client/api/getChatSettings.d.ts +15 -0
- package/dist/client/api/getChatSettings.d.ts.map +1 -0
- package/dist/client/api/getSocialSettings.d.ts +15 -0
- package/dist/client/api/getSocialSettings.d.ts.map +1 -0
- package/dist/client/api/getToken.d.ts +4 -4
- package/dist/client/api/index.d.ts +1 -0
- package/dist/client/api/index.d.ts.map +1 -1
- package/dist/client/api/renewal.d.ts +2 -0
- package/dist/client/api/renewal.d.ts.map +1 -0
- package/dist/client/api/setSessionState.d.ts +12 -0
- package/dist/client/api/setSessionState.d.ts.map +1 -0
- package/dist/client/api/terminateClient.d.ts +9 -0
- package/dist/client/api/terminateClient.d.ts.map +1 -0
- package/dist/client/api/tests/accessTokenExpiryWatcher.test.d.ts +2 -0
- package/dist/client/api/tests/accessTokenExpiryWatcher.test.d.ts.map +1 -0
- package/dist/client/api/tests/activeClient.test.d.ts +2 -0
- package/dist/client/api/tests/activeClient.test.d.ts.map +1 -0
- package/dist/client/api/tests/activeUser.test.d.ts +2 -0
- package/dist/client/api/tests/activeUser.test.d.ts.map +1 -0
- package/dist/client/api/tests/connectClient.test.d.ts +2 -0
- package/dist/client/api/tests/connectClient.test.d.ts.map +1 -0
- package/dist/client/api/tests/createClient.test.d.ts +2 -0
- package/dist/client/api/tests/createClient.test.d.ts.map +1 -0
- package/dist/client/api/tests/disconnectClient.test.d.ts +2 -0
- package/dist/client/api/tests/disconnectClient.test.d.ts.map +1 -0
- package/dist/client/api/tests/getToken.test.d.ts +2 -0
- package/dist/client/api/tests/getToken.test.d.ts.map +1 -0
- package/dist/client/api/tests/renewal.test.d.ts +2 -0
- package/dist/client/api/tests/renewal.test.d.ts.map +1 -0
- package/dist/client/api/tests/setSessionState.test.d.ts +2 -0
- package/dist/client/api/tests/setSessionState.test.d.ts.map +1 -0
- package/dist/client/api/tests/terminateClient.test.d.ts +2 -0
- package/dist/client/api/tests/terminateClient.test.d.ts.map +1 -0
- package/dist/client/events/index.d.ts +1 -0
- package/dist/client/events/index.d.ts.map +1 -1
- package/dist/client/events/onClientBanned.d.ts +1 -1
- package/dist/client/events/onClientBanned.d.ts.map +1 -1
- package/dist/client/events/onSessionStateChange.d.ts +17 -0
- package/dist/client/events/onSessionStateChange.d.ts.map +1 -0
- package/dist/client/events/onTokenExpired.d.ts +17 -0
- package/dist/client/events/onTokenExpired.d.ts.map +1 -0
- package/dist/client/events/onTokenTerminated.d.ts +11 -0
- package/dist/client/events/onTokenTerminated.d.ts.map +1 -0
- package/dist/client/events/tests/onSessionStateChange.test.d.ts +2 -0
- package/dist/client/events/tests/onSessionStateChange.test.d.ts.map +1 -0
- package/dist/client/events/tests/onTokenTerminated.test.d.ts +2 -0
- package/dist/client/events/tests/onTokenTerminated.test.d.ts.map +1 -0
- package/dist/client/tests/connectClient.test.d.ts +2 -0
- package/dist/client/tests/connectClient.test.d.ts.map +1 -0
- package/dist/client/utils/setClientToken.d.ts +19 -0
- package/dist/client/utils/setClientToken.d.ts.map +1 -0
- package/dist/comment/api/createComment.d.ts +1 -1
- package/dist/comment/api/createComment.d.ts.map +1 -1
- package/dist/comment/api/tests/createComment.test.d.ts +2 -0
- package/dist/comment/api/tests/createComment.test.d.ts.map +1 -0
- package/dist/comment/api/tests/getComments.test.d.ts +2 -0
- package/dist/comment/api/tests/getComments.test.d.ts.map +1 -0
- package/dist/comment/events/tests/onCommentCreated.test.d.ts +2 -0
- package/dist/comment/events/tests/onCommentCreated.test.d.ts.map +1 -0
- package/dist/comment/events/tests/onCommentDeleted.test.d.ts +2 -0
- package/dist/comment/events/tests/onCommentDeleted.test.d.ts.map +1 -0
- package/dist/comment/events/utils.d.ts +1 -1
- package/dist/comment/events/utils.d.ts.map +1 -1
- package/dist/community/api/queryCommunities.d.ts.map +1 -1
- package/dist/community/api/queryCommunityMembers.d.ts +6 -1
- package/dist/community/api/queryCommunityMembers.d.ts.map +1 -1
- package/dist/community/api/test/addCommunityMember.test.d.ts +2 -0
- package/dist/community/api/test/addCommunityMember.test.d.ts.map +1 -0
- package/dist/community/api/test/addCommunityMemberRole.test.d.ts +2 -0
- package/dist/community/api/test/addCommunityMemberRole.test.d.ts.map +1 -0
- package/dist/community/api/test/createCommunity.test.d.ts +2 -0
- package/dist/community/api/test/createCommunity.test.d.ts.map +1 -0
- package/dist/community/api/test/deleteCommunity.test.d.ts +2 -0
- package/dist/community/api/test/deleteCommunity.test.d.ts.map +1 -0
- package/dist/community/api/test/getCommunities.test.d.ts +2 -0
- package/dist/community/api/test/getCommunities.test.d.ts.map +1 -0
- package/dist/community/api/test/getCommunity.test.d.ts +2 -0
- package/dist/community/api/test/getCommunity.test.d.ts.map +1 -0
- package/dist/community/api/test/getRecommendedCommunities.test.d.ts +2 -0
- package/dist/community/api/test/getRecommendedCommunities.test.d.ts.map +1 -0
- package/dist/community/api/test/getTopTrendingCommunities.test.d.ts +2 -0
- package/dist/community/api/test/getTopTrendingCommunities.test.d.ts.map +1 -0
- package/dist/community/api/test/joinCommunity.test.d.ts +2 -0
- package/dist/community/api/test/joinCommunity.test.d.ts.map +1 -0
- package/dist/community/api/test/leaveCommunity.test.d.ts +2 -0
- package/dist/community/api/test/leaveCommunity.test.d.ts.map +1 -0
- package/dist/community/api/test/queryCommunity.test.d.ts +2 -0
- package/dist/community/api/test/queryCommunity.test.d.ts.map +1 -0
- package/dist/community/api/test/queryCommunityMembers.test.d.ts +2 -0
- package/dist/community/api/test/queryCommunityMembers.test.d.ts.map +1 -0
- package/dist/community/api/test/removeCommunityMemberRole.test.d.ts +2 -0
- package/dist/community/api/test/removeCommunityMemberRole.test.d.ts.map +1 -0
- package/dist/community/api/test/removeCommunityMembers.test.d.ts +2 -0
- package/dist/community/api/test/removeCommunityMembers.test.d.ts.map +1 -0
- package/dist/community/api/test/updateCommunity.test.d.ts +2 -0
- package/dist/community/api/test/updateCommunity.test.d.ts.map +1 -0
- package/dist/community/events/test/onCommunityCreated.test.d.ts +2 -0
- package/dist/community/events/test/onCommunityCreated.test.d.ts.map +1 -0
- package/dist/community/events/test/onCommunityDeleted.test.d.ts +2 -0
- package/dist/community/events/test/onCommunityDeleted.test.d.ts.map +1 -0
- package/dist/community/events/test/onCommunityJoined.test.d.ts +2 -0
- package/dist/community/events/test/onCommunityJoined.test.d.ts.map +1 -0
- package/dist/community/events/test/onCommunityLeft.test.d.ts +2 -0
- package/dist/community/events/test/onCommunityLeft.test.d.ts.map +1 -0
- package/dist/community/events/test/onCommunityUpdated.test.d.ts +2 -0
- package/dist/community/events/test/onCommunityUpdated.test.d.ts.map +1 -0
- package/dist/community/events/test/onCommunityUserAdded.test.d.ts +2 -0
- package/dist/community/events/test/onCommunityUserAdded.test.d.ts.map +1 -0
- package/dist/community/events/test/onCommunityUserBanned.test.d.ts +2 -0
- package/dist/community/events/test/onCommunityUserBanned.test.d.ts.map +1 -0
- package/dist/community/events/test/onCommunityUserRemoved.test.d.ts +2 -0
- package/dist/community/events/test/onCommunityUserRemoved.test.d.ts.map +1 -0
- package/dist/community/events/test/onCommunityUserUnbaned.test.d.ts +2 -0
- package/dist/community/events/test/onCommunityUserUnbaned.test.d.ts.map +1 -0
- package/dist/community/events/test/utils.test.d.ts +2 -0
- package/dist/community/events/test/utils.test.d.ts.map +1 -0
- package/dist/community/events/utils.d.ts +2 -2
- package/dist/community/events/utils.d.ts.map +1 -1
- package/dist/core/events.d.ts +3 -3
- package/dist/core/events.d.ts.map +1 -1
- package/dist/core/model/idResolvers.d.ts.map +1 -1
- package/dist/core/model/identifyModel.d.ts.map +1 -1
- package/dist/core/model/index.d.ts +5 -0
- package/dist/core/model/index.d.ts.map +1 -1
- package/dist/core/query/filtering.d.ts.map +1 -1
- package/dist/core/query/query.d.ts.map +1 -1
- package/dist/core/subscription.d.ts +26 -1
- package/dist/core/subscription.d.ts.map +1 -1
- package/dist/core/tests/subscription.test.d.ts +2 -0
- package/dist/core/tests/subscription.test.d.ts.map +1 -0
- package/dist/core/transports/http.d.ts.map +1 -1
- package/dist/feed/api/queryGlobalFeed.d.ts +1 -1
- package/dist/feed/api/queryGlobalFeed.d.ts.map +1 -1
- package/dist/feed/api/test/queryGlobalFeed.test.d.ts +2 -0
- package/dist/feed/api/test/queryGlobalFeed.test.d.ts.map +1 -0
- package/dist/follow/events/utils.d.ts +1 -1
- package/dist/follow/events/utils.d.ts.map +1 -1
- package/dist/group/utils/index.d.ts +2 -0
- package/dist/group/utils/index.d.ts.map +1 -0
- package/dist/group/utils/test/withUsers.test.d.ts +2 -0
- package/dist/group/utils/test/withUsers.test.d.ts.map +1 -0
- package/dist/group/utils/withUser.d.ts +38 -0
- package/dist/group/utils/withUser.d.ts.map +1 -0
- package/dist/index.cjs.js +2451 -870
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +2424 -869
- package/dist/index.umd.js +4 -4
- package/dist/message/api/createMessage.d.ts +15 -9
- package/dist/message/api/createMessage.d.ts.map +1 -1
- package/dist/message/api/deleteMessage.d.ts +2 -2
- package/dist/message/api/getMessage.d.ts.map +1 -1
- package/dist/message/api/getMessages.d.ts.map +1 -1
- package/dist/message/api/queryMessages.d.ts.map +1 -1
- package/dist/message/api/updateMessage.d.ts +1 -1
- package/dist/message/api/updateMessage.d.ts.map +1 -1
- package/dist/message/events/index.d.ts +5 -1
- package/dist/message/events/index.d.ts.map +1 -1
- package/dist/message/events/onMessageCreated.d.ts.map +1 -1
- package/dist/message/events/onMessageDeleted.d.ts.map +1 -1
- package/dist/message/events/onMessageFlagCleared.d.ts +17 -0
- package/dist/message/events/onMessageFlagCleared.d.ts.map +1 -0
- package/dist/message/events/onMessageFlagged.d.ts +17 -0
- package/dist/message/events/onMessageFlagged.d.ts.map +1 -0
- package/dist/message/events/onMessageReactionAdded.d.ts +18 -0
- package/dist/message/events/onMessageReactionAdded.d.ts.map +1 -0
- package/dist/message/events/onMessageReactionRemoved.d.ts +18 -0
- package/dist/message/events/onMessageReactionRemoved.d.ts.map +1 -0
- package/dist/message/events/onMessageUnflagged.d.ts +17 -0
- package/dist/message/events/onMessageUnflagged.d.ts.map +1 -0
- package/dist/message/events/onMessageUpdated.d.ts.map +1 -1
- package/dist/message/observers/liveMessage.d.ts +7 -4
- package/dist/message/observers/liveMessage.d.ts.map +1 -1
- package/dist/message/observers/liveMessages.d.ts +7 -6
- package/dist/message/observers/liveMessages.d.ts.map +1 -1
- package/dist/message/observers/observeMessage.d.ts +1 -0
- package/dist/message/observers/observeMessage.d.ts.map +1 -1
- package/dist/message/observers/observeMessages.d.ts +5 -4
- package/dist/message/observers/observeMessages.d.ts.map +1 -1
- package/dist/message/tests/events.test.d.ts +2 -0
- package/dist/message/tests/events.test.d.ts.map +1 -0
- package/dist/message/utils/index.d.ts +18 -0
- package/dist/message/utils/index.d.ts.map +1 -0
- package/dist/post/api/queryPosts.d.ts.map +1 -1
- package/dist/post/api/tests/queryPosts.test.d.ts +2 -0
- package/dist/post/api/tests/queryPosts.test.d.ts.map +1 -0
- package/dist/post/events/tests/onPostApproved.test.d.ts +2 -0
- package/dist/post/events/tests/onPostApproved.test.d.ts.map +1 -0
- package/dist/post/events/tests/onPostCreated.test.d.ts +2 -0
- package/dist/post/events/tests/onPostCreated.test.d.ts.map +1 -0
- package/dist/post/events/tests/onPostDeclined.test.d.ts +2 -0
- package/dist/post/events/tests/onPostDeclined.test.d.ts.map +1 -0
- package/dist/post/events/tests/onPostDeleted.test.d.ts +2 -0
- package/dist/post/events/tests/onPostDeleted.test.d.ts.map +1 -0
- package/dist/post/events/tests/onPostFlagged.test.d.ts +2 -0
- package/dist/post/events/tests/onPostFlagged.test.d.ts.map +1 -0
- package/dist/post/events/tests/onPostReactionAdded.test.d.ts +2 -0
- package/dist/post/events/tests/onPostReactionAdded.test.d.ts.map +1 -0
- package/dist/post/events/tests/onPostReactionRemoved.test.d.ts +2 -0
- package/dist/post/events/tests/onPostReactionRemoved.test.d.ts.map +1 -0
- package/dist/post/events/tests/onPostUnflagged.test.d.ts +2 -0
- package/dist/post/events/tests/onPostUnflagged.test.d.ts.map +1 -0
- package/dist/post/events/tests/onPostUpdated.test.d.ts +2 -0
- package/dist/post/events/tests/onPostUpdated.test.d.ts.map +1 -0
- package/dist/post/events/utils.d.ts +1 -1
- package/dist/post/events/utils.d.ts.map +1 -1
- package/dist/post/observers/livePosts.d.ts.map +1 -1
- package/dist/post/observers/tests/livePosts.test.d.ts +2 -0
- package/dist/post/observers/tests/livePosts.test.d.ts.map +1 -0
- package/dist/reaction/api/addReaction.d.ts.map +1 -1
- package/dist/reaction/api/removeReaction.d.ts.map +1 -1
- package/dist/reaction/events/onReactionAdded.d.ts.map +1 -1
- package/dist/reaction/events/onReactionRemoved.d.ts.map +1 -1
- package/dist/reaction/events/onReactorAdded.d.ts.map +1 -1
- package/dist/reaction/events/onReactorRemoved.d.ts.map +1 -1
- package/dist/report/api/createReport.d.ts.map +1 -1
- package/dist/report/api/deleteReport.d.ts.map +1 -1
- package/dist/report/api/isReportedByMe.d.ts.map +1 -1
- package/dist/stream/api/tests/createStream.test.d.ts +2 -0
- package/dist/stream/api/tests/createStream.test.d.ts.map +1 -0
- package/dist/stream/api/tests/deleteStream.test.d.ts +2 -0
- package/dist/stream/api/tests/deleteStream.test.d.ts.map +1 -0
- package/dist/stream/api/tests/getStream.test.d.ts +2 -0
- package/dist/stream/api/tests/getStream.test.d.ts.map +1 -0
- package/dist/subChannel/api/createSubChannel.d.ts +16 -0
- package/dist/subChannel/api/createSubChannel.d.ts.map +1 -0
- package/dist/subChannel/api/deleteSubChannel.d.ts +16 -0
- package/dist/subChannel/api/deleteSubChannel.d.ts.map +1 -0
- package/dist/subChannel/api/getSubChannel.d.ts +32 -0
- package/dist/subChannel/api/getSubChannel.d.ts.map +1 -0
- package/dist/subChannel/api/getSubChannels.d.ts +32 -0
- package/dist/subChannel/api/getSubChannels.d.ts.map +1 -0
- package/dist/subChannel/api/index.d.ts +7 -0
- package/dist/subChannel/api/index.d.ts.map +1 -0
- package/dist/subChannel/api/querySubChannels.d.ts +32 -0
- package/dist/subChannel/api/querySubChannels.d.ts.map +1 -0
- package/dist/subChannel/api/tests/createSubChannel.test.d.ts +2 -0
- package/dist/subChannel/api/tests/createSubChannel.test.d.ts.map +1 -0
- package/dist/subChannel/api/updateSubChannel.d.ts +17 -0
- package/dist/subChannel/api/updateSubChannel.d.ts.map +1 -0
- package/dist/subChannel/events/index.d.ts +4 -0
- package/dist/subChannel/events/index.d.ts.map +1 -0
- package/dist/subChannel/events/onSubChannelCreated.d.ts +17 -0
- package/dist/subChannel/events/onSubChannelCreated.d.ts.map +1 -0
- package/dist/subChannel/events/onSubChannelDeleted.d.ts +17 -0
- package/dist/subChannel/events/onSubChannelDeleted.d.ts.map +1 -0
- package/dist/subChannel/events/onSubChannelFetched.d.ts +3 -0
- package/dist/subChannel/events/onSubChannelFetched.d.ts.map +1 -0
- package/dist/subChannel/events/onSubChannelUpdated.d.ts +17 -0
- package/dist/subChannel/events/onSubChannelUpdated.d.ts.map +1 -0
- package/dist/subChannel/observers/index.d.ts +3 -0
- package/dist/subChannel/observers/index.d.ts.map +1 -0
- package/dist/subChannel/observers/liveSubChannel.d.ts +21 -0
- package/dist/subChannel/observers/liveSubChannel.d.ts.map +1 -0
- package/dist/subChannel/observers/liveSubChannels.d.ts +21 -0
- package/dist/subChannel/observers/liveSubChannels.d.ts.map +1 -0
- package/dist/subChannel/observers/tests/liveSubChannel.test.d.ts +2 -0
- package/dist/subChannel/observers/tests/liveSubChannel.test.d.ts.map +1 -0
- package/dist/subChannel/observers/tests/liveSubChannels.test.d.ts +2 -0
- package/dist/subChannel/observers/tests/liveSubChannels.test.d.ts.map +1 -0
- package/dist/subChannel/utils/index.d.ts +8 -0
- package/dist/subChannel/utils/index.d.ts.map +1 -0
- package/dist/user/events/index.d.ts +0 -1
- package/dist/user/events/index.d.ts.map +1 -1
- package/dist/user/events/utils.d.ts +1 -1
- package/dist/user/events/utils.d.ts.map +1 -1
- package/dist/user/observers/liveUser.d.ts +7 -4
- package/dist/user/observers/liveUser.d.ts.map +1 -1
- package/dist/utils/constants.d.ts +2 -0
- package/dist/utils/constants.d.ts.map +1 -1
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/tests/client.d.ts.map +1 -1
- package/dist/utils/tests/dummy/channel.d.ts +47 -22
- package/dist/utils/tests/dummy/channel.d.ts.map +1 -1
- package/dist/utils/tests/dummy/community.d.ts +33 -1
- package/dist/utils/tests/dummy/community.d.ts.map +1 -1
- package/dist/utils/tests/dummy/index.d.ts +5 -1
- package/dist/utils/tests/dummy/index.d.ts.map +1 -1
- package/dist/utils/tests/dummy/member.d.ts +2 -0
- package/dist/utils/tests/dummy/member.d.ts.map +1 -0
- package/dist/utils/tests/dummy/message.d.ts +2 -1
- package/dist/utils/tests/dummy/message.d.ts.map +1 -1
- package/dist/utils/tests/dummy/post.d.ts +15 -0
- package/dist/utils/tests/dummy/post.d.ts.map +1 -1
- package/dist/utils/tests/dummy/stream.d.ts +83 -0
- package/dist/utils/tests/dummy/stream.d.ts.map +1 -0
- package/dist/utils/tests/dummy/subChannel.d.ts +23 -0
- package/dist/utils/tests/dummy/subChannel.d.ts.map +1 -0
- package/dist/utils/tests/dummy/token.d.ts +11 -0
- package/dist/utils/tests/dummy/token.d.ts.map +1 -0
- package/dist/utils/tests/dummy/user.d.ts +1 -0
- package/dist/utils/tests/dummy/user.d.ts.map +1 -1
- package/dist/utils/tests/utils.d.ts +1 -0
- package/dist/utils/tests/utils.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/@types/core/errors.ts +1 -0
- package/src/@types/core/events.ts +62 -28
- package/src/@types/core/model.ts +2 -0
- package/src/@types/core/payload.ts +21 -5
- package/src/@types/domains/channel.ts +23 -6
- package/src/@types/domains/client.ts +66 -1
- package/src/@types/domains/content.ts +4 -3
- package/src/@types/domains/group.ts +6 -2
- package/src/@types/domains/message.ts +59 -26
- package/src/@types/domains/post.ts +2 -2
- package/src/@types/domains/subChannel.ts +48 -0
- package/src/@types/global.ts +14 -0
- package/src/@types/index.ts +1 -0
- package/src/cache/api/tests/dropFromCache.test.ts +59 -0
- package/src/cache/api/tests/enableCache.test.ts +15 -0
- package/src/cache/api/tests/pullFromCache.test.ts +42 -0
- package/src/cache/api/tests/upsertInCache.test.ts +60 -0
- package/src/cache/utils/tests/encodeKey.test.ts +19 -0
- package/src/cache/utils/tests/partialMatch.test.ts +13 -0
- package/src/channel/api/addChannelMembers.ts +2 -2
- package/src/channel/api/banChannelMembers.ts +5 -3
- package/src/channel/api/createChannel.ts +1 -1
- package/src/channel/api/getChannels.ts +2 -2
- package/src/channel/api/index.ts +3 -0
- package/src/channel/api/queryChannelMembers.ts +15 -10
- package/src/channel/api/queryChannels.ts +1 -1
- package/src/channel/api/removeChannelMembers.ts +9 -1
- package/src/channel/api/startReading.ts +53 -0
- package/src/channel/api/stopReading.ts +49 -0
- package/src/channel/api/tests/addChannelMembers.test.ts +36 -8
- package/src/channel/api/tests/createChannel.test.ts +53 -0
- package/src/channel/api/tests/getChannel.test.ts +3 -2
- package/src/channel/api/tests/joinChannel.test.ts +52 -0
- package/src/channel/api/tests/queryChannelMembers.test.ts +27 -11
- package/src/channel/api/tests/queryChannels.test.ts +2 -2
- package/src/channel/api/tests/removeChannelMembers.test.ts +95 -0
- package/src/channel/api/tests/startReading.test.ts +25 -0
- package/src/channel/api/tests/stopReading.test.ts +25 -0
- package/src/channel/api/unbanChannelMembers.ts +6 -3
- package/src/channel/events/index.ts +1 -0
- package/src/channel/events/onChannelCreated.ts +1 -1
- package/src/channel/events/onChannelDeleted.ts +1 -1
- package/src/channel/events/onChannelJoined.ts +12 -7
- package/src/channel/events/onChannelLeft.ts +12 -7
- package/src/channel/events/onChannelMemberAdded.ts +8 -6
- package/src/channel/events/onChannelMemberBanned.ts +7 -4
- package/src/channel/events/onChannelMemberRemoved.ts +7 -9
- package/src/channel/events/onChannelMemberUnbanned.ts +7 -4
- package/src/channel/events/onChannelMuted.ts +34 -0
- package/src/channel/events/onChannelUpdated.ts +10 -1
- package/src/channel/events/tests/onChannelCreated.test.ts +55 -0
- package/src/channel/events/tests/onChannelJoined.test.ts +56 -0
- package/src/channel/events/tests/onChannelMemberAdded.test.ts +56 -0
- package/src/channel/events/tests/onChannelMemberBanned.test.ts +13 -12
- package/src/channel/events/tests/onChannelMemberRemoved.test.ts +56 -0
- package/src/channel/events/tests/onChannelMemberUnbanned.test.ts +13 -12
- package/src/channel/events/tests/onChannelUpdated.test.ts +55 -0
- package/src/channel/observers/index.ts +1 -0
- package/src/channel/observers/liveChannel.ts +108 -0
- package/src/channel/observers/liveChannelMembers.ts +40 -34
- package/src/channel/observers/liveChannels.ts +25 -14
- package/src/channel/observers/observeChannel.ts +2 -0
- package/src/channel/observers/observeChannels.ts +2 -0
- package/src/channel/observers/tests/liveChannelMembers.test.ts +107 -95
- package/src/channel/observers/tests/liveChannels.test.ts +385 -126
- package/src/channel/utils/index.ts +54 -1
- package/src/channel/utils/tests/prepareChannelPayload.test.ts +22 -22
- package/src/client/api/accessTokenExpiryWatcher.ts +83 -0
- package/src/client/api/activeUser.ts +1 -1
- package/src/client/api/connectClient.ts +114 -26
- package/src/client/api/createClient.ts +10 -2
- package/src/client/api/disconnectClient.ts +26 -2
- package/src/client/api/getChatSettings.ts +22 -0
- package/src/client/api/getSocialSettings.ts +24 -0
- package/src/client/api/getToken.ts +6 -6
- package/src/client/api/index.ts +2 -0
- package/src/client/api/renewal.ts +74 -0
- package/src/client/api/setSessionState.ts +75 -0
- package/src/client/api/terminateClient.ts +20 -0
- package/src/client/api/tests/accessTokenExpiryWatcher.test.ts +129 -0
- package/src/client/api/tests/activeClient.test.ts +20 -0
- package/src/client/api/tests/activeUser.test.ts +20 -0
- package/src/client/api/tests/connectClient.test.ts +84 -0
- package/src/client/api/tests/createClient.test.ts +48 -0
- package/src/client/api/tests/disconnectClient.test.ts +78 -0
- package/src/client/api/tests/getToken.test.ts +36 -0
- package/src/client/api/tests/renewal.test.ts +67 -0
- package/src/client/api/tests/setSessionState.test.ts +62 -0
- package/src/client/api/tests/terminateClient.test.ts +29 -0
- package/src/client/events/index.ts +2 -0
- package/src/client/events/onClientBanned.ts +12 -5
- package/src/client/events/onSessionStateChange.ts +31 -0
- package/src/client/events/onTokenExpired.ts +26 -0
- package/src/client/events/onTokenTerminated.ts +20 -0
- package/src/client/events/tests/onSessionStateChange.test.ts +37 -0
- package/src/client/events/tests/onTokenTerminated.test.ts +37 -0
- package/src/client/tests/connectClient.test.ts +40 -0
- package/src/client/utils/setClientToken.ts +39 -0
- package/src/comment/api/createComment.ts +3 -3
- package/src/comment/api/tests/createComment.test.ts +49 -0
- package/src/comment/api/tests/getComments.test.ts +36 -0
- package/src/comment/events/tests/onCommentCreated.test.ts +27 -0
- package/src/comment/events/tests/onCommentDeleted.test.ts +27 -0
- package/src/comment/events/utils.ts +1 -1
- package/src/comment/observers/liveComments.ts +8 -8
- package/src/community/api/queryCommunities.ts +3 -2
- package/src/community/api/queryCommunityMembers.ts +59 -14
- package/src/community/api/test/addCommunityMember.test.ts +71 -0
- package/src/community/api/test/addCommunityMemberRole.test.ts +67 -0
- package/src/community/api/test/createCommunity.test.ts +61 -0
- package/src/community/api/test/deleteCommunity.test.ts +74 -0
- package/src/community/api/test/getCommunities.test.ts +73 -0
- package/src/community/api/test/getCommunity.test.ts +69 -0
- package/src/community/api/test/getRecommendedCommunities.test.ts +102 -0
- package/src/community/api/test/getTopTrendingCommunities.test.ts +102 -0
- package/src/community/api/test/joinCommunity.test.ts +70 -0
- package/src/community/api/test/leaveCommunity.test.ts +78 -0
- package/src/community/api/test/queryCommunity.test.ts +108 -0
- package/src/community/api/test/queryCommunityMembers.test.ts +122 -0
- package/src/community/api/test/removeCommunityMemberRole.test.ts +67 -0
- package/src/community/api/test/removeCommunityMembers.test.ts +75 -0
- package/src/community/api/test/updateCommunity.test.ts +65 -0
- package/src/community/events/test/onCommunityCreated.test.ts +46 -0
- package/src/community/events/test/onCommunityDeleted.test.ts +35 -0
- package/src/community/events/test/onCommunityJoined.test.ts +50 -0
- package/src/community/events/test/onCommunityLeft.test.ts +50 -0
- package/src/community/events/test/onCommunityUpdated.test.ts +49 -0
- package/src/community/events/test/onCommunityUserAdded.test.ts +50 -0
- package/src/community/events/test/onCommunityUserBanned.test.ts +50 -0
- package/src/community/events/test/onCommunityUserRemoved.test.ts +50 -0
- package/src/community/events/test/onCommunityUserUnbaned.test.ts +50 -0
- package/src/community/events/test/utils.test.ts +158 -0
- package/src/community/events/utils.ts +18 -10
- package/src/core/events.ts +11 -20
- package/src/core/model/idResolvers.ts +1 -0
- package/src/core/model/identifyModel.ts +1 -0
- package/src/core/model/index.ts +15 -0
- package/src/core/query/filtering.ts +2 -9
- package/src/core/query/query.ts +2 -34
- package/src/core/subscription.ts +50 -10
- package/src/core/tests/query/query/runQuery.test.ts +15 -38
- package/src/core/tests/subscription.test.ts +15 -0
- package/src/core/transports/http.ts +36 -1
- package/src/core/transports/mqtt.ts +1 -1
- package/src/external/api/createUserToken.ts +2 -2
- package/src/feed/api/queryGlobalFeed.ts +1 -1
- package/src/feed/api/test/queryGlobalFeed.test.ts +138 -0
- package/src/follow/events/utils.ts +1 -1
- package/src/follow/observers/liveFollowers.ts +8 -8
- package/src/follow/observers/liveFollowings.ts +8 -8
- package/src/group/utils/index.ts +1 -0
- package/src/group/utils/test/withUsers.test.ts +103 -0
- package/src/group/utils/withUser.ts +92 -0
- package/src/index.ts +4 -0
- package/src/message/api/createMessage.ts +60 -29
- package/src/message/api/deleteMessage.ts +6 -6
- package/src/message/api/getMessage.ts +5 -4
- package/src/message/api/getMessages.ts +7 -5
- package/src/message/api/queryMessages.ts +13 -25
- package/src/message/api/tests/createMessage.test.ts +129 -37
- package/src/message/api/tests/deleteMessage.test.ts +18 -12
- package/src/message/api/tests/getMessage.test.ts +6 -4
- package/src/message/api/tests/getMessages.test.ts +7 -8
- package/src/message/api/tests/queryMessages.test.ts +20 -23
- package/src/message/api/tests/updateMessage.test.ts +13 -12
- package/src/message/api/updateMessage.ts +12 -9
- package/src/message/events/index.ts +7 -1
- package/src/message/events/onMessageCreated.ts +50 -3
- package/src/message/events/onMessageDeleted.ts +14 -2
- package/src/message/events/onMessageFetched.ts +1 -1
- package/src/message/events/onMessageFlagCleared.ts +34 -0
- package/src/message/events/onMessageFlagged.ts +33 -0
- package/src/message/events/onMessageReactionAdded.ts +35 -0
- package/src/message/events/onMessageReactionRemoved.ts +40 -0
- package/src/message/events/onMessageUnflagged.ts +32 -0
- package/src/message/events/onMessageUpdated.ts +14 -10
- package/src/message/observers/liveMessage.ts +42 -23
- package/src/message/observers/liveMessages.ts +30 -16
- package/src/message/observers/observeMessage.ts +1 -0
- package/src/message/observers/observeMessages.ts +6 -5
- package/src/message/tests/events.test.ts +53 -0
- package/src/message/tests/observers/liveMessage.test.ts +46 -49
- package/src/message/tests/observers/liveMessages.test.ts +45 -37
- package/src/message/utils/index.ts +129 -0
- package/src/post/api/queryPosts.ts +9 -0
- package/src/post/api/tests/queryPosts.test.ts +149 -0
- package/src/post/events/tests/onPostApproved.test.ts +33 -0
- package/src/post/events/tests/onPostCreated.test.ts +33 -0
- package/src/post/events/tests/onPostDeclined.test.ts +33 -0
- package/src/post/events/tests/onPostDeleted.test.ts +33 -0
- package/src/post/events/tests/onPostFlagged.test.ts +33 -0
- package/src/post/events/tests/onPostReactionAdded.test.ts +40 -0
- package/src/post/events/tests/onPostReactionRemoved.test.ts +40 -0
- package/src/post/events/tests/onPostUnflagged.test.ts +33 -0
- package/src/post/events/tests/onPostUpdated.test.ts +33 -0
- package/src/post/events/utils.ts +1 -1
- package/src/post/observers/livePosts.ts +32 -12
- package/src/post/observers/tests/livePosts.test.ts +302 -0
- package/src/reaction/api/addReaction.ts +1 -0
- package/src/reaction/api/removeReaction.ts +6 -1
- package/src/reaction/events/onReactionAdded.ts +4 -7
- package/src/reaction/events/onReactionRemoved.ts +4 -7
- package/src/reaction/events/onReactorAdded.ts +4 -2
- package/src/reaction/events/onReactorRemoved.ts +4 -7
- package/src/reaction/observers/liveReactions.ts +6 -6
- package/src/reaction/utils/dispatchReactable.ts +1 -1
- package/src/report/api/createReport.ts +28 -18
- package/src/report/api/deleteReport.ts +28 -18
- package/src/report/api/isReportedByMe.ts +9 -2
- package/src/stream/api/tests/createStream.test.ts +30 -0
- package/src/stream/api/tests/deleteStream.test.ts +48 -0
- package/src/stream/api/tests/getStream.test.ts +23 -0
- package/src/subChannel/api/createSubChannel.ts +41 -0
- package/src/subChannel/api/deleteSubChannel.ts +53 -0
- package/src/subChannel/api/getSubChannel.ts +85 -0
- package/src/subChannel/api/getSubChannels.ts +77 -0
- package/src/subChannel/api/index.ts +8 -0
- package/src/subChannel/api/querySubChannels.ts +115 -0
- package/src/subChannel/api/tests/createSubChannel.test.ts +103 -0
- package/src/subChannel/api/updateSubChannel.ts +43 -0
- package/src/subChannel/events/index.ts +3 -0
- package/src/subChannel/events/onSubChannelCreated.ts +32 -0
- package/src/subChannel/events/onSubChannelDeleted.ts +44 -0
- package/src/subChannel/events/onSubChannelFetched.ts +12 -0
- package/src/subChannel/events/onSubChannelUpdated.ts +32 -0
- package/src/subChannel/observers/index.ts +2 -0
- package/src/subChannel/observers/liveSubChannel.ts +84 -0
- package/src/subChannel/observers/liveSubChannels.ts +137 -0
- package/src/subChannel/observers/tests/liveSubChannel.test.ts +212 -0
- package/src/subChannel/observers/tests/liveSubChannels.test.ts +206 -0
- package/src/subChannel/utils/index.ts +50 -0
- package/src/user/events/index.ts +0 -1
- package/src/user/events/utils.ts +1 -1
- package/src/user/observers/liveUser.ts +29 -23
- package/src/user/tests/observers/liveUser.test.ts +5 -9
- package/src/utils/constants.ts +2 -0
- package/src/utils/index.ts +52 -0
- package/src/utils/tests/client.ts +18 -2
- package/src/utils/tests/dummy/channel.ts +123 -117
- package/src/utils/tests/dummy/community.ts +67 -3
- package/src/utils/tests/dummy/index.ts +6 -1
- package/src/utils/tests/dummy/member.ts +18 -0
- package/src/utils/tests/dummy/message.ts +37 -16
- package/src/utils/tests/dummy/post.ts +34 -0
- package/src/utils/tests/dummy/stream.ts +86 -0
- package/src/utils/tests/dummy/subChannel.ts +51 -0
- package/src/utils/tests/dummy/token.ts +12 -0
- package/src/utils/tests/dummy/user.ts +2 -0
- package/src/utils/tests/utils.ts +2 -0
- package/dist/channel/utils/prepareChannelPayload.d.ts +0 -3
- package/dist/channel/utils/prepareChannelPayload.d.ts.map +0 -1
- package/src/channel/utils/prepareChannelPayload.ts +0 -31
package/dist/index.esm.js
CHANGED
|
@@ -91,6 +91,78 @@ const isProduction = ((_a$1 = safeProcess.env) === null || _a$1 === void 0 ? voi
|
|
|
91
91
|
var _a, _b;
|
|
92
92
|
const VERSION$1 = (_b = (_a = safeProcess.env) === null || _a === void 0 ? void 0 : _a.VERSION) !== null && _b !== void 0 ? _b : '__dev__';
|
|
93
93
|
|
|
94
|
+
// cache constants
|
|
95
|
+
const CACHE_KEY_GET = 'get';
|
|
96
|
+
const CACHE_KEY_TOMBSTONE = 'dead';
|
|
97
|
+
const CACHE_LIFESPAN = 1 * 60 * 1000; // 1 minute
|
|
98
|
+
const CACHE_LIFESPAN_TOMBSTONE = 3 * 60 * 1000; // 3 minutes
|
|
99
|
+
/**
|
|
100
|
+
* Shallow-clones an object and sort its keys
|
|
101
|
+
*
|
|
102
|
+
* @param source a plain object to clone
|
|
103
|
+
* @returns a clone of source, with keys sorted with default javascript sorting
|
|
104
|
+
*
|
|
105
|
+
* @category Cache
|
|
106
|
+
* @hidden
|
|
107
|
+
*/
|
|
108
|
+
const normalize = (source) => Object.keys(source)
|
|
109
|
+
.sort()
|
|
110
|
+
.reduce((acc, key) => (Object.assign(Object.assign({}, acc), { [key]: source[key] })), {});
|
|
111
|
+
/**
|
|
112
|
+
* Encodes a given {@link Amity.CacheKey} to a plain string
|
|
113
|
+
*
|
|
114
|
+
* @param key the key to encode
|
|
115
|
+
* @returns an encoded string
|
|
116
|
+
*
|
|
117
|
+
* @category Cache
|
|
118
|
+
* @hidden
|
|
119
|
+
*/
|
|
120
|
+
const encodeKey = (key) => JSON.stringify(key, (_, val) => (typeof val === 'object' ? normalize(val) : val));
|
|
121
|
+
/**
|
|
122
|
+
* Decodes a string back into a {@link Amity.CacheKey}
|
|
123
|
+
|
|
124
|
+
*
|
|
125
|
+
* @param key the string key to decode
|
|
126
|
+
* @returns a plain Amity.CacheKey object.
|
|
127
|
+
*
|
|
128
|
+
* @category Cache
|
|
129
|
+
* @hidden
|
|
130
|
+
*/
|
|
131
|
+
const decodeKey = (key) => JSON.parse(key);
|
|
132
|
+
/**
|
|
133
|
+
* Performs a recursive partial deep equal check on two objects.
|
|
134
|
+
*
|
|
135
|
+
* @param predicate the reference object containing the partial information
|
|
136
|
+
* @param candidate the object to check against
|
|
137
|
+
* @returns true if the candidate contains all the values of the predicate
|
|
138
|
+
*
|
|
139
|
+
* @category Cache
|
|
140
|
+
*/
|
|
141
|
+
const partialMatch = (predicate, candidate) => {
|
|
142
|
+
if (predicate === candidate)
|
|
143
|
+
return true;
|
|
144
|
+
// if one or the other is nullish, there can't be equality
|
|
145
|
+
if (!predicate || !candidate)
|
|
146
|
+
return false;
|
|
147
|
+
// we only perform recursive check on objects
|
|
148
|
+
if (typeof predicate !== 'object') {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
// recursively call for partial match based on the predicate
|
|
152
|
+
return Object.keys(predicate).every(key => partialMatch(predicate[key], candidate[key]));
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* checks if passed error code is included in
|
|
156
|
+
* Tombstone errors list
|
|
157
|
+
*
|
|
158
|
+
* @param errorCode as {@link Amity.ServerError}
|
|
159
|
+
* @returns success boolean if the given errorCode
|
|
160
|
+
* is included in Tombstone errors list
|
|
161
|
+
*
|
|
162
|
+
* @category Cache
|
|
163
|
+
*/
|
|
164
|
+
const checkIfShouldGoesToTombstone = (errorCode) => [400400 /* Amity.ServerError.ITEM_NOT_FOUND */, 400300 /* Amity.ServerError.FORBIDDEN */].includes(errorCode);
|
|
165
|
+
|
|
94
166
|
/**
|
|
95
167
|
* Type guard to find if the a given decoded backend token uses "skip/limit" pagination style
|
|
96
168
|
*
|
|
@@ -240,12 +312,180 @@ const toPageRaw = (token) => {
|
|
|
240
312
|
return undefined;
|
|
241
313
|
};
|
|
242
314
|
|
|
315
|
+
/* eslint-disable no-use-before-define */
|
|
316
|
+
/**
|
|
317
|
+
* Type guard to check and cast that a given async function is has the ".locally" property
|
|
318
|
+
*
|
|
319
|
+
* @param func any SDK APi function
|
|
320
|
+
* @returns success boolean if the function has a 'locally' twin
|
|
321
|
+
*
|
|
322
|
+
* @hidden
|
|
323
|
+
*/
|
|
324
|
+
const isFetcher = (func) => 'locally' in func;
|
|
325
|
+
/**
|
|
326
|
+
* Type guard to check and cast that a given async function is has the ".optimistically" property
|
|
327
|
+
*
|
|
328
|
+
* @param func any SDK APi function
|
|
329
|
+
* @returns success boolean if the function has an 'optimistically' twin
|
|
330
|
+
*
|
|
331
|
+
* @hidden
|
|
332
|
+
*/
|
|
333
|
+
const isMutator = (func) => 'optimistically' in func;
|
|
334
|
+
/**
|
|
335
|
+
* Type guard to check and cast that a given async function is has
|
|
336
|
+
* the ".locally" or ".optimistically" property
|
|
337
|
+
*
|
|
338
|
+
* @param func any SDK APi function
|
|
339
|
+
* @returns success boolean if the function has an offline twin
|
|
340
|
+
*
|
|
341
|
+
* @hidden
|
|
342
|
+
*/
|
|
343
|
+
const isOffline = (func) => isFetcher(func) || isMutator(func);
|
|
344
|
+
/**
|
|
345
|
+
* Type guard to check and cast that a given object has the "cachedAt" property
|
|
346
|
+
*
|
|
347
|
+
* @param model any object to check on
|
|
348
|
+
* @returns success boolean if the object has property "cachedAt"
|
|
349
|
+
*
|
|
350
|
+
* @hidden
|
|
351
|
+
*/
|
|
352
|
+
const isCachable = (model) => model === null || model === void 0 ? void 0 : model.hasOwnProperty('cachedAt');
|
|
353
|
+
/**
|
|
354
|
+
* Checks if a model is considered local (cachedAt === -1)
|
|
355
|
+
*
|
|
356
|
+
* @param model any cachable object to check
|
|
357
|
+
* @returns success boolean if the object is marked as local
|
|
358
|
+
*
|
|
359
|
+
* @hidden
|
|
360
|
+
*/
|
|
361
|
+
const isLocal = (model) => {
|
|
362
|
+
return isCachable(model) && (model === null || model === void 0 ? void 0 : model.cachedAt) === -1;
|
|
363
|
+
};
|
|
364
|
+
/**
|
|
365
|
+
* Checks if a model is considered fresh
|
|
366
|
+
*
|
|
367
|
+
* @param model any cachable object to check
|
|
368
|
+
* @param lifeSpan the supposedly duration for which the object is considered synced
|
|
369
|
+
* @returns success boolean if the object is below the given lifespan
|
|
370
|
+
*
|
|
371
|
+
* @hidden
|
|
372
|
+
*/
|
|
373
|
+
const isFresh = (model, lifeSpan = CACHE_LIFESPAN) => {
|
|
374
|
+
var _a;
|
|
375
|
+
return Date.now() - ((_a = model === null || model === void 0 ? void 0 : model.cachedAt) !== null && _a !== void 0 ? _a : 0) <= lifeSpan;
|
|
376
|
+
};
|
|
377
|
+
/**
|
|
378
|
+
* ```js
|
|
379
|
+
* import { createQuery, getUser } from '@amityco/ts-sdk'
|
|
380
|
+
* const query = createQuery(getUser, 'foobar')
|
|
381
|
+
* ```
|
|
382
|
+
*
|
|
383
|
+
* Creates a wrapper for the API call you wish to call.
|
|
384
|
+
* This wrapper is necessary to create for optimistically calls
|
|
385
|
+
*
|
|
386
|
+
*
|
|
387
|
+
* @param func A compatible API function from the ts sdk
|
|
388
|
+
* @param args The arguments to pass to the function passed as `fn`
|
|
389
|
+
* @returns A wrapper containing both the function and its future arguments
|
|
390
|
+
*
|
|
391
|
+
* @category Query
|
|
392
|
+
*/
|
|
393
|
+
const createQuery = (func, ...args) => ({ func, args });
|
|
394
|
+
/**
|
|
395
|
+
* ```js
|
|
396
|
+
* import { queryOptions } from '@amityco/ts-sdk'
|
|
397
|
+
* const options = queryOptions('no_fetch', lifeSpan)
|
|
398
|
+
* ```
|
|
399
|
+
*
|
|
400
|
+
* Creates a query options object based on the query policy passed
|
|
401
|
+
*
|
|
402
|
+
* @param policy The policy to apply to a query
|
|
403
|
+
* @returns A properly set query options object
|
|
404
|
+
*
|
|
405
|
+
* @category Query
|
|
406
|
+
*/
|
|
407
|
+
const queryOptions = (policy, lifeSpan = CACHE_LIFESPAN) => {
|
|
408
|
+
if (policy === 'cache_only')
|
|
409
|
+
return { lifeSpan: Infinity };
|
|
410
|
+
return { lifeSpan: lifeSpan < CACHE_LIFESPAN ? CACHE_LIFESPAN : lifeSpan };
|
|
411
|
+
};
|
|
412
|
+
/**
|
|
413
|
+
* ```js
|
|
414
|
+
* import { createQuery, getUser, runQuery } from '@amityco/ts-sdk'
|
|
415
|
+
* const query = createQuery(getUser, client, 'foobar')
|
|
416
|
+
* runQuery(query, user => console.log(user))
|
|
417
|
+
* ```
|
|
418
|
+
*
|
|
419
|
+
* Calls an API function wrapped around a Amity.Query, and executes the callback whenever
|
|
420
|
+
* a value is available. The value can be picked either from the local cache and/or
|
|
421
|
+
* from the server afterwards depending on the query options passed
|
|
422
|
+
*
|
|
423
|
+
* @param query A query object wrapping the call to be made
|
|
424
|
+
* @param callback A function to execute when a value is available
|
|
425
|
+
* @param options the query options
|
|
426
|
+
*
|
|
427
|
+
* @category Query
|
|
428
|
+
*/
|
|
429
|
+
const runQuery = ({ func, args }, callback, options = queryOptions('cache_then_server')) => {
|
|
430
|
+
let local;
|
|
431
|
+
const { lifeSpan } = queryOptions('cache_then_server', options.lifeSpan);
|
|
432
|
+
// offline first
|
|
433
|
+
if (isOffline(func)) {
|
|
434
|
+
try {
|
|
435
|
+
local = isMutator(func) ? func.optimistically(...args) : func.locally(...args);
|
|
436
|
+
}
|
|
437
|
+
catch (error) {
|
|
438
|
+
callback === null || callback === void 0 ? void 0 : callback(createSnapshot(undefined, {
|
|
439
|
+
origin: 'local',
|
|
440
|
+
loading: false,
|
|
441
|
+
error,
|
|
442
|
+
}));
|
|
443
|
+
}
|
|
444
|
+
const shouldAbort = isCachable(local) && isFresh(local, lifeSpan);
|
|
445
|
+
callback === null || callback === void 0 ? void 0 : callback(createSnapshot(local, {
|
|
446
|
+
origin: 'local',
|
|
447
|
+
loading: !(isFetcher(func) && shouldAbort),
|
|
448
|
+
}));
|
|
449
|
+
if (shouldAbort)
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
callback === null || callback === void 0 ? void 0 : callback(createSnapshot(undefined, {
|
|
454
|
+
origin: 'local',
|
|
455
|
+
loading: true,
|
|
456
|
+
}));
|
|
457
|
+
}
|
|
458
|
+
func(...args)
|
|
459
|
+
.then(fresh => {
|
|
460
|
+
// @ts-ignore i know.
|
|
461
|
+
// isLocal(local) && syncInCache(local, fresh)
|
|
462
|
+
callback === null || callback === void 0 ? void 0 : callback(createSnapshot(fresh, {
|
|
463
|
+
origin: 'server',
|
|
464
|
+
loading: false,
|
|
465
|
+
}));
|
|
466
|
+
})
|
|
467
|
+
.catch(error => {
|
|
468
|
+
callback === null || callback === void 0 ? void 0 : callback(createSnapshot(undefined, {
|
|
469
|
+
origin: 'server',
|
|
470
|
+
loading: false,
|
|
471
|
+
error,
|
|
472
|
+
}));
|
|
473
|
+
});
|
|
474
|
+
};
|
|
475
|
+
// eslint-disable-next-line no-redeclare
|
|
476
|
+
function createSnapshot(data, options) {
|
|
477
|
+
if (isPaged(data) || isCachable(data))
|
|
478
|
+
return Object.assign(Object.assign({}, options), data);
|
|
479
|
+
return Object.assign(Object.assign({}, options), { data });
|
|
480
|
+
}
|
|
481
|
+
|
|
243
482
|
/** @hidden */
|
|
244
483
|
const idResolvers = {
|
|
245
484
|
user: ({ userId }) => userId,
|
|
246
485
|
file: ({ fileId }) => fileId,
|
|
247
486
|
role: ({ roleId }) => roleId,
|
|
248
487
|
channel: ({ channelId }) => channelId,
|
|
488
|
+
subChannel: ({ subChannelId }) => subChannelId,
|
|
249
489
|
channelUsers: ({ channelId, userId }) => `${channelId}#${userId}`,
|
|
250
490
|
message: ({ messageId }) => messageId,
|
|
251
491
|
community: ({ communityId }) => communityId,
|
|
@@ -282,6 +522,7 @@ const CRITERIAS = {
|
|
|
282
522
|
user: ['userId', 'avatarFileId'],
|
|
283
523
|
role: ['roleId'],
|
|
284
524
|
channel: ['channelId', 'rateLimit'],
|
|
525
|
+
subChannel: ['subChannelId'],
|
|
285
526
|
channelUsers: ['channelId', 'userId', 'membership'],
|
|
286
527
|
message: ['messageId'],
|
|
287
528
|
community: ['communityId', 'postSetting'],
|
|
@@ -340,6 +581,7 @@ const PAYLOAD2MODEL = {
|
|
|
340
581
|
files: 'file',
|
|
341
582
|
roles: 'role',
|
|
342
583
|
channels: 'channel',
|
|
584
|
+
messageFeeds: 'subChannel',
|
|
343
585
|
channelUsers: 'channelUsers',
|
|
344
586
|
messages: 'message',
|
|
345
587
|
communities: 'community',
|
|
@@ -353,277 +595,17 @@ const PAYLOAD2MODEL = {
|
|
|
353
595
|
videoStreamings: 'stream',
|
|
354
596
|
follows: 'follow',
|
|
355
597
|
followCounts: 'followCount',
|
|
356
|
-
};
|
|
598
|
+
};
|
|
599
|
+
/** hidden */
|
|
600
|
+
const hasUpdates = (prevData, nextData) => {
|
|
601
|
+
return new Date(prevData.updatedAt) < new Date(nextData.updatedAt);
|
|
602
|
+
};
|
|
603
|
+
/** hidden */
|
|
604
|
+
function getFutureDate(date = new Date().toISOString()) {
|
|
605
|
+
return new Date(new Date(date).getTime() + 1).toISOString();
|
|
606
|
+
}
|
|
357
607
|
|
|
358
|
-
|
|
359
|
-
const CACHE_KEY_GET = 'get';
|
|
360
|
-
const CACHE_KEY_TOMBSTONE = 'dead';
|
|
361
|
-
const CACHE_LIFESPAN = 1 * 60 * 1000; // 1 minute
|
|
362
|
-
const CACHE_LIFESPAN_TOMBSTONE = 3 * 60 * 1000; // 3 minutes
|
|
363
|
-
/**
|
|
364
|
-
* Shallow-clones an object and sort its keys
|
|
365
|
-
*
|
|
366
|
-
* @param source a plain object to clone
|
|
367
|
-
* @returns a clone of source, with keys sorted with default javascript sorting
|
|
368
|
-
*
|
|
369
|
-
* @category Cache
|
|
370
|
-
* @hidden
|
|
371
|
-
*/
|
|
372
|
-
const normalize = (source) => Object.keys(source)
|
|
373
|
-
.sort()
|
|
374
|
-
.reduce((acc, key) => (Object.assign(Object.assign({}, acc), { [key]: source[key] })), {});
|
|
375
|
-
/**
|
|
376
|
-
* Encodes a given {@link Amity.CacheKey} to a plain string
|
|
377
|
-
*
|
|
378
|
-
* @param key the key to encode
|
|
379
|
-
* @returns an encoded string
|
|
380
|
-
*
|
|
381
|
-
* @category Cache
|
|
382
|
-
* @hidden
|
|
383
|
-
*/
|
|
384
|
-
const encodeKey = (key) => JSON.stringify(key, (_, val) => (typeof val === 'object' ? normalize(val) : val));
|
|
385
|
-
/**
|
|
386
|
-
* Decodes a string back into a {@link Amity.CacheKey}
|
|
387
|
-
|
|
388
|
-
*
|
|
389
|
-
* @param key the string key to decode
|
|
390
|
-
* @returns a plain Amity.CacheKey object.
|
|
391
|
-
*
|
|
392
|
-
* @category Cache
|
|
393
|
-
* @hidden
|
|
394
|
-
*/
|
|
395
|
-
const decodeKey = (key) => JSON.parse(key);
|
|
396
|
-
/**
|
|
397
|
-
* Performs a recursive partial deep equal check on two objects.
|
|
398
|
-
*
|
|
399
|
-
* @param predicate the reference object containing the partial information
|
|
400
|
-
* @param candidate the object to check against
|
|
401
|
-
* @returns true if the candidate contains all the values of the predicate
|
|
402
|
-
*
|
|
403
|
-
* @category Cache
|
|
404
|
-
*/
|
|
405
|
-
const partialMatch = (predicate, candidate) => {
|
|
406
|
-
if (predicate === candidate)
|
|
407
|
-
return true;
|
|
408
|
-
// if one or the other is nullish, there can't be equality
|
|
409
|
-
if (!predicate || !candidate)
|
|
410
|
-
return false;
|
|
411
|
-
// we only perform recursive check on objects
|
|
412
|
-
if (typeof predicate !== 'object') {
|
|
413
|
-
return false;
|
|
414
|
-
}
|
|
415
|
-
// recursively call for partial match based on the predicate
|
|
416
|
-
return Object.keys(predicate).every(key => partialMatch(predicate[key], candidate[key]));
|
|
417
|
-
};
|
|
418
|
-
/**
|
|
419
|
-
* checks if passed error code is included in
|
|
420
|
-
* Tombstone errors list
|
|
421
|
-
*
|
|
422
|
-
* @param errorCode as {@link Amity.ServerError}
|
|
423
|
-
* @returns success boolean if the given errorCode
|
|
424
|
-
* is included in Tombstone errors list
|
|
425
|
-
*
|
|
426
|
-
* @category Cache
|
|
427
|
-
*/
|
|
428
|
-
const checkIfShouldGoesToTombstone = (errorCode) => [400400 /* Amity.ServerError.ITEM_NOT_FOUND */, 400300 /* Amity.ServerError.FORBIDDEN */].includes(errorCode);
|
|
429
|
-
|
|
430
|
-
/* eslint-disable no-use-before-define */
|
|
431
|
-
/**
|
|
432
|
-
* Type guard to check and cast that a given async function is has the ".locally" property
|
|
433
|
-
*
|
|
434
|
-
* @param func any SDK APi function
|
|
435
|
-
* @returns success boolean if the function has a 'locally' twin
|
|
436
|
-
*
|
|
437
|
-
* @hidden
|
|
438
|
-
*/
|
|
439
|
-
const isFetcher = (func) => 'locally' in func;
|
|
440
|
-
/**
|
|
441
|
-
* Type guard to check and cast that a given async function is has the ".optimistically" property
|
|
442
|
-
*
|
|
443
|
-
* @param func any SDK APi function
|
|
444
|
-
* @returns success boolean if the function has an 'optimistically' twin
|
|
445
|
-
*
|
|
446
|
-
* @hidden
|
|
447
|
-
*/
|
|
448
|
-
const isMutator = (func) => 'optimistically' in func;
|
|
449
|
-
/**
|
|
450
|
-
* Type guard to check and cast that a given async function is has
|
|
451
|
-
* the ".locally" or ".optimistically" property
|
|
452
|
-
*
|
|
453
|
-
* @param func any SDK APi function
|
|
454
|
-
* @returns success boolean if the function has an offline twin
|
|
455
|
-
*
|
|
456
|
-
* @hidden
|
|
457
|
-
*/
|
|
458
|
-
const isOffline = (func) => isFetcher(func) || isMutator(func);
|
|
459
|
-
/**
|
|
460
|
-
* Type guard to check and cast that a given object has the "cachedAt" property
|
|
461
|
-
*
|
|
462
|
-
* @param model any object to check on
|
|
463
|
-
* @returns success boolean if the object has property "cachedAt"
|
|
464
|
-
*
|
|
465
|
-
* @hidden
|
|
466
|
-
*/
|
|
467
|
-
const isCachable = (model) => model === null || model === void 0 ? void 0 : model.hasOwnProperty('cachedAt');
|
|
468
|
-
/**
|
|
469
|
-
* Checks if a model is considered local (cachedAt === -1)
|
|
470
|
-
*
|
|
471
|
-
* @param model any cachable object to check
|
|
472
|
-
* @returns success boolean if the object is marked as local
|
|
473
|
-
*
|
|
474
|
-
* @hidden
|
|
475
|
-
*/
|
|
476
|
-
const isLocal = (model) => {
|
|
477
|
-
return isCachable(model) && (model === null || model === void 0 ? void 0 : model.cachedAt) === -1;
|
|
478
|
-
};
|
|
479
|
-
/**
|
|
480
|
-
* Checks if a model is considered fresh
|
|
481
|
-
*
|
|
482
|
-
* @param model any cachable object to check
|
|
483
|
-
* @param lifeSpan the supposedly duration for which the object is considered synced
|
|
484
|
-
* @returns success boolean if the object is below the given lifespan
|
|
485
|
-
*
|
|
486
|
-
* @hidden
|
|
487
|
-
*/
|
|
488
|
-
const isFresh = (model, lifeSpan = CACHE_LIFESPAN) => {
|
|
489
|
-
var _a;
|
|
490
|
-
return Date.now() - ((_a = model === null || model === void 0 ? void 0 : model.cachedAt) !== null && _a !== void 0 ? _a : 0) <= lifeSpan;
|
|
491
|
-
};
|
|
492
|
-
/**
|
|
493
|
-
* ```js
|
|
494
|
-
* import { createQuery, getUser } from '@amityco/ts-sdk'
|
|
495
|
-
* const query = createQuery(getUser, 'foobar')
|
|
496
|
-
* ```
|
|
497
|
-
*
|
|
498
|
-
* Creates a wrapper for the API call you wish to call.
|
|
499
|
-
* This wrapper is necessary to create for optimistically calls
|
|
500
|
-
*
|
|
501
|
-
*
|
|
502
|
-
* @param func A compatible API function from the ts sdk
|
|
503
|
-
* @param args The arguments to pass to the function passed as `fn`
|
|
504
|
-
* @returns A wrapper containing both the function and its future arguments
|
|
505
|
-
*
|
|
506
|
-
* @category Query
|
|
507
|
-
*/
|
|
508
|
-
const createQuery = (func, ...args) => ({ func, args });
|
|
509
|
-
/**
|
|
510
|
-
* ```js
|
|
511
|
-
* import { queryOptions } from '@amityco/ts-sdk'
|
|
512
|
-
* const options = queryOptions('no_fetch', lifeSpan)
|
|
513
|
-
* ```
|
|
514
|
-
*
|
|
515
|
-
* Creates a query options object based on the query policy passed
|
|
516
|
-
*
|
|
517
|
-
* @param policy The policy to apply to a query
|
|
518
|
-
* @returns A properly set query options object
|
|
519
|
-
*
|
|
520
|
-
* @category Query
|
|
521
|
-
*/
|
|
522
|
-
const queryOptions = (policy, lifeSpan = CACHE_LIFESPAN) => {
|
|
523
|
-
if (policy === 'cache_only')
|
|
524
|
-
return { lifeSpan: Infinity };
|
|
525
|
-
return { lifeSpan: lifeSpan < CACHE_LIFESPAN ? CACHE_LIFESPAN : lifeSpan };
|
|
526
|
-
};
|
|
527
|
-
/**
|
|
528
|
-
* ```js
|
|
529
|
-
* import { createQuery, getUser, runQuery } from '@amityco/ts-sdk'
|
|
530
|
-
* const query = createQuery(getUser, client, 'foobar')
|
|
531
|
-
* runQuery(query, user => console.log(user))
|
|
532
|
-
* ```
|
|
533
|
-
*
|
|
534
|
-
* Calls an API function wrapped around a Amity.Query, and executes the callback whenever
|
|
535
|
-
* a value is available. The value can be picked either from the local cache and/or
|
|
536
|
-
* from the server afterwards depending on the query options passed
|
|
537
|
-
*
|
|
538
|
-
* @param query A query object wrapping the call to be made
|
|
539
|
-
* @param callback A function to execute when a value is available
|
|
540
|
-
* @param options the query options
|
|
541
|
-
*
|
|
542
|
-
* @category Query
|
|
543
|
-
*/
|
|
544
|
-
const runQuery = ({ func, args }, callback, options = queryOptions('cache_then_server')) => {
|
|
545
|
-
let local;
|
|
546
|
-
const { lifeSpan } = queryOptions('cache_then_server', options.lifeSpan);
|
|
547
|
-
// offline first
|
|
548
|
-
if (isOffline(func)) {
|
|
549
|
-
try {
|
|
550
|
-
local = isMutator(func) ? func.optimistically(...args) : func.locally(...args);
|
|
551
|
-
}
|
|
552
|
-
catch (error) {
|
|
553
|
-
callback === null || callback === void 0 ? void 0 : callback(createSnapshot(undefined, {
|
|
554
|
-
origin: 'local',
|
|
555
|
-
loading: false,
|
|
556
|
-
error,
|
|
557
|
-
}));
|
|
558
|
-
}
|
|
559
|
-
const shouldAbort = isCachable(local) && isFresh(local, lifeSpan);
|
|
560
|
-
callback === null || callback === void 0 ? void 0 : callback(createSnapshot(local, {
|
|
561
|
-
origin: 'local',
|
|
562
|
-
loading: !(isFetcher(func) && shouldAbort),
|
|
563
|
-
}));
|
|
564
|
-
if (shouldAbort)
|
|
565
|
-
return;
|
|
566
|
-
}
|
|
567
|
-
else {
|
|
568
|
-
callback === null || callback === void 0 ? void 0 : callback(createSnapshot(undefined, {
|
|
569
|
-
origin: 'local',
|
|
570
|
-
loading: true,
|
|
571
|
-
}));
|
|
572
|
-
}
|
|
573
|
-
let optimisticArgs = args;
|
|
574
|
-
/* get args for optimistic method calls
|
|
575
|
-
*
|
|
576
|
-
* On optimistic updates, the modal key for the update needs to be passed to
|
|
577
|
-
* the server so as to avoid duplicate modal creation
|
|
578
|
-
*
|
|
579
|
-
* ex: on creating a new message pass the modalKey is messageId, pass this
|
|
580
|
-
* id onto the server so that a duplicate with seperate messageId is not
|
|
581
|
-
* created.
|
|
582
|
-
*/
|
|
583
|
-
if (isMutator(func)) {
|
|
584
|
-
// @ts-ignore
|
|
585
|
-
const modelKey = local && local.data && identifyModelKey(local.data);
|
|
586
|
-
if (modelKey) {
|
|
587
|
-
/*
|
|
588
|
-
* modal key is only required for create as update and delete queries pass
|
|
589
|
-
* the modal key in the param itself
|
|
590
|
-
* For update & delete the first argument is always an id which is of type
|
|
591
|
-
* string, whereas for create the first and only argument is the param object
|
|
592
|
-
*
|
|
593
|
-
* When creating an AmityObject optimistically we need to pass the modal key
|
|
594
|
-
* on to the server. ex: for create message modal key will be 'messageId`
|
|
595
|
-
*/
|
|
596
|
-
if (typeof args[0] !== 'string') {
|
|
597
|
-
// @ts-ignore
|
|
598
|
-
optimisticArgs = [Object.assign(Object.assign({}, args[0]), { [modelKey]: local.data[modelKey] })];
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
func(...optimisticArgs)
|
|
603
|
-
.then(fresh => {
|
|
604
|
-
// @ts-ignore i know.
|
|
605
|
-
// isLocal(local) && syncInCache(local, fresh)
|
|
606
|
-
callback === null || callback === void 0 ? void 0 : callback(createSnapshot(fresh, {
|
|
607
|
-
origin: 'server',
|
|
608
|
-
loading: false,
|
|
609
|
-
}));
|
|
610
|
-
})
|
|
611
|
-
.catch(error => {
|
|
612
|
-
callback === null || callback === void 0 ? void 0 : callback(createSnapshot(undefined, {
|
|
613
|
-
origin: 'server',
|
|
614
|
-
loading: false,
|
|
615
|
-
error,
|
|
616
|
-
}));
|
|
617
|
-
});
|
|
618
|
-
};
|
|
619
|
-
// eslint-disable-next-line no-redeclare
|
|
620
|
-
function createSnapshot(data, options) {
|
|
621
|
-
if (isPaged(data) || isCachable(data))
|
|
622
|
-
return Object.assign(Object.assign({}, options), data);
|
|
623
|
-
return Object.assign(Object.assign({}, options), { data });
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
/* eslint-disable max-classes-per-file */
|
|
608
|
+
/* eslint-disable max-classes-per-file */
|
|
627
609
|
/**
|
|
628
610
|
* Generic ASC error
|
|
629
611
|
* @category Errors
|
|
@@ -1079,16 +1061,10 @@ const filterByChannelMembership = (collection, membership, userId) => {
|
|
|
1079
1061
|
'get',
|
|
1080
1062
|
channelUserCacheKey,
|
|
1081
1063
|
])) === null || _a === void 0 ? void 0 : _a.data;
|
|
1082
|
-
|
|
1083
|
-
if (membership === 'member' && channelUser) {
|
|
1084
|
-
return true;
|
|
1085
|
-
}
|
|
1086
|
-
// if channel user by user id not found, user is not a member of that
|
|
1087
|
-
// channel
|
|
1088
|
-
if (membership === 'notMember' && !channelUser) {
|
|
1064
|
+
if (membership === 'member' && channelUser && channelUser.membership !== 'none') {
|
|
1089
1065
|
return true;
|
|
1090
1066
|
}
|
|
1091
|
-
return
|
|
1067
|
+
return membership === 'notMember' && (!channelUser || channelUser.membership === 'none');
|
|
1092
1068
|
});
|
|
1093
1069
|
};
|
|
1094
1070
|
|
|
@@ -1149,19 +1125,9 @@ const WS_EVENTS = [
|
|
|
1149
1125
|
'connect_error',
|
|
1150
1126
|
'reconnect_error',
|
|
1151
1127
|
'reconnect_failed',
|
|
1152
|
-
'
|
|
1153
|
-
|
|
1154
|
-
'
|
|
1155
|
-
'v3.channel.didJoin',
|
|
1156
|
-
'v3.channel.didLeave',
|
|
1157
|
-
'v3.channel.didAddUsers',
|
|
1158
|
-
'v3.channel.didRemoveUsers',
|
|
1159
|
-
'v3.channel.didBan',
|
|
1160
|
-
'v3.channel.didUnban',
|
|
1161
|
-
'v3.message.didFetch',
|
|
1162
|
-
'v3.message.didCreate',
|
|
1163
|
-
'v3.message.didUpdate',
|
|
1164
|
-
'v3.message.didDelete',
|
|
1128
|
+
'sessionStateChange',
|
|
1129
|
+
// for internal use by accessTokenExpiryWatcher
|
|
1130
|
+
'tokenExpired',
|
|
1165
1131
|
];
|
|
1166
1132
|
const MQTT_EVENTS = [
|
|
1167
1133
|
'connect',
|
|
@@ -1171,6 +1137,7 @@ const MQTT_EVENTS = [
|
|
|
1171
1137
|
'close',
|
|
1172
1138
|
'end',
|
|
1173
1139
|
'reconnect',
|
|
1140
|
+
'user.didGlobalBan',
|
|
1174
1141
|
];
|
|
1175
1142
|
/** @hidden */
|
|
1176
1143
|
const createEventEmitter = () => {
|
|
@@ -1188,12 +1155,16 @@ const createEventEmitter = () => {
|
|
|
1188
1155
|
*/
|
|
1189
1156
|
const proxyWebsocketEvents = (ws, emitter) => {
|
|
1190
1157
|
WS_EVENTS.forEach(event => {
|
|
1191
|
-
ws.on(event, (param) =>
|
|
1158
|
+
ws.on(event, (param) => {
|
|
1159
|
+
emitter.emit(event, param);
|
|
1160
|
+
});
|
|
1192
1161
|
});
|
|
1193
1162
|
};
|
|
1194
1163
|
const proxyMqttEvents = (mqttClient, emitter) => {
|
|
1195
1164
|
MQTT_EVENTS.forEach(event => {
|
|
1196
|
-
mqttClient.on(event, (...params) =>
|
|
1165
|
+
mqttClient.on(event, (...params) => {
|
|
1166
|
+
emitter.emit(event, params.length === 1 ? params[0] : params);
|
|
1167
|
+
});
|
|
1197
1168
|
});
|
|
1198
1169
|
// @ts-ignore
|
|
1199
1170
|
mqttClient.on('message', (topic, payload) => {
|
|
@@ -1277,6 +1248,7 @@ const getCommunityUserTopic = (path, level) => {
|
|
|
1277
1248
|
return path;
|
|
1278
1249
|
}
|
|
1279
1250
|
};
|
|
1251
|
+
const getNetworkId = (user) => user.path.split('/user/')[0];
|
|
1280
1252
|
const getCommunityTopic = ({ path }, level = SubscriptionLevels.COMMUNITY) => getCommunityUserTopic(path, level);
|
|
1281
1253
|
const getUserTopic = ({ path }, level = SubscriptionLevels.USER) => getCommunityUserTopic(level === SubscriptionLevels.USER ? path : path.replace(/^(\w*)/, '$1/social'), level);
|
|
1282
1254
|
const getPostTopic = ({ path }, level = SubscriptionLevels.POST) => {
|
|
@@ -1292,26 +1264,58 @@ const getCommentTopic = ({ path }) => {
|
|
|
1292
1264
|
};
|
|
1293
1265
|
const getMyFollowersTopic = () => {
|
|
1294
1266
|
const user = getActiveUser();
|
|
1295
|
-
|
|
1296
|
-
return `${networkId}/membership/${user._id}/+/+`;
|
|
1267
|
+
return `${getNetworkId(user)}/membership/${user._id}/+/+`;
|
|
1297
1268
|
};
|
|
1298
1269
|
const getMyFollowingsTopic = () => {
|
|
1299
1270
|
const user = getActiveUser();
|
|
1300
|
-
|
|
1301
|
-
return `${networkId}/membership/+/${user._id}/+`;
|
|
1271
|
+
return `${getNetworkId(user)}/membership/+/${user._id}/+`;
|
|
1302
1272
|
};
|
|
1303
|
-
|
|
1273
|
+
const getChannelTopic = (channel) => `${channel.path}/#`;
|
|
1274
|
+
const getSubChannelTopic = (subChannel) => `${subChannel.path}/#`;
|
|
1275
|
+
const getMessageTopic = (message) => message.path;
|
|
1276
|
+
/**
|
|
1277
|
+
* @hidden
|
|
1278
|
+
*
|
|
1279
|
+
* Create a topic to subscribe to network level events like 'user is deleted from the network'
|
|
1280
|
+
*/
|
|
1304
1281
|
const getNetworkTopic = () => {
|
|
1305
|
-
|
|
1306
|
-
const [networkId] = user.path.split('/user/');
|
|
1307
|
-
return networkId;
|
|
1282
|
+
return getNetworkId(getActiveUser());
|
|
1308
1283
|
};
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1284
|
+
/**
|
|
1285
|
+
* @hidden
|
|
1286
|
+
*
|
|
1287
|
+
* Create a topic to subscribe to channel events for 'conversation', 'community' channel
|
|
1288
|
+
*/
|
|
1289
|
+
const getSmartFeedChannelTopic = () => {
|
|
1313
1290
|
const user = getActiveUser();
|
|
1314
|
-
|
|
1291
|
+
return `${getNetworkId(user)}/smartfeed/${user._id}/channels`;
|
|
1292
|
+
};
|
|
1293
|
+
/**
|
|
1294
|
+
* @hidden
|
|
1295
|
+
*
|
|
1296
|
+
* Create a topic to subscribe to sub channel events for 'conversation', 'community' channel
|
|
1297
|
+
*/
|
|
1298
|
+
const getSmartFeedSubChannelTopic = () => {
|
|
1299
|
+
const user = getActiveUser();
|
|
1300
|
+
return `${getNetworkId(user)}/smartfeed/${user._id}/messagefeeds`;
|
|
1301
|
+
};
|
|
1302
|
+
/**
|
|
1303
|
+
* @hidden
|
|
1304
|
+
*
|
|
1305
|
+
* Create a topic to subscribe to message events for 'conversation', 'community' channel
|
|
1306
|
+
*/
|
|
1307
|
+
const getSmartFeedMessageTopic = () => {
|
|
1308
|
+
const user = getActiveUser();
|
|
1309
|
+
return `${getNetworkId(user)}/smartfeed/${user._id}/messages`;
|
|
1310
|
+
};
|
|
1311
|
+
let mqttAccessToken;
|
|
1312
|
+
let mqttUserId;
|
|
1313
|
+
function subscribeTopic(topic, callback) {
|
|
1314
|
+
var _a;
|
|
1315
|
+
const { mqtt, emitter, token } = getActiveClient();
|
|
1316
|
+
const accessToken = (_a = token === null || token === void 0 ? void 0 : token.accessToken) !== null && _a !== void 0 ? _a : '';
|
|
1317
|
+
const user = getActiveUser();
|
|
1318
|
+
if (mqttAccessToken !== accessToken || mqttUserId !== user._id) {
|
|
1315
1319
|
mqttAccessToken = accessToken;
|
|
1316
1320
|
mqttUserId = user._id;
|
|
1317
1321
|
mqtt.connect({ accessToken: mqttAccessToken, userId: mqttUserId });
|
|
@@ -1407,12 +1411,32 @@ const createHttpTransport = (endpoint) => {
|
|
|
1407
1411
|
httpsAgent: new HttpsAgent(options),
|
|
1408
1412
|
paramsSerializer: stringify,
|
|
1409
1413
|
});
|
|
1414
|
+
instance.interceptors.request.use(config => {
|
|
1415
|
+
var _a;
|
|
1416
|
+
// check global.ts for the extension of AxiosRequestConfig
|
|
1417
|
+
const tokenExpiry = (_a = config.metadata) === null || _a === void 0 ? void 0 : _a.tokenExpiry;
|
|
1418
|
+
if (tokenExpiry) {
|
|
1419
|
+
if (Date.now() >= Date.parse(tokenExpiry)) {
|
|
1420
|
+
return Object.assign(Object.assign({}, config), { cancelToken: new axios.CancelToken(cancel => cancel('Token Expired')) });
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
return config;
|
|
1424
|
+
});
|
|
1410
1425
|
instance.interceptors.response.use(response => response, error => {
|
|
1411
1426
|
var _a;
|
|
1412
1427
|
const { response } = error;
|
|
1413
1428
|
// eslint-disable-next-line no-console
|
|
1414
1429
|
console.error((_a = response === null || response === void 0 ? void 0 : response.data) !== null && _a !== void 0 ? _a : error);
|
|
1415
|
-
|
|
1430
|
+
// handle unauthorized request
|
|
1431
|
+
if (response.data.code === 400100 /* Amity.ServerError.UNAUTHORIZED */) {
|
|
1432
|
+
fireEvent('tokenTerminated', "terminated" /* Amity.SessionStates.TERMINATED */);
|
|
1433
|
+
}
|
|
1434
|
+
// handle request cancellation
|
|
1435
|
+
if (axios.isCancel(error)) {
|
|
1436
|
+
fireEvent('tokenExpired', "tokenExpired" /* Amity.SessionStates.TOKEN_EXPIRED */);
|
|
1437
|
+
throw new ASCError('Token expired', 800403 /* Amity.ClientError.TOKEN_EXPIRED */, "fatal" /* Amity.ErrorLevel.FATAL */);
|
|
1438
|
+
}
|
|
1439
|
+
if (response === null || response === void 0 ? void 0 : response.data)
|
|
1416
1440
|
unwrapPayload(response.data);
|
|
1417
1441
|
});
|
|
1418
1442
|
return instance;
|
|
@@ -20147,7 +20171,7 @@ var MqttError;
|
|
|
20147
20171
|
MqttError[MqttError["NOT_AUTHORIZED"] = 5] = "NOT_AUTHORIZED";
|
|
20148
20172
|
})(MqttError || (MqttError = {}));
|
|
20149
20173
|
function getMqttOptions(params) {
|
|
20150
|
-
return Object.assign({ clean: false, clientId: `mqttjs_ + ${Math.random().toString(16).
|
|
20174
|
+
return Object.assign({ clean: false, clientId: `mqttjs_ + ${Math.random().toString(16).substring(2, 10)}`, protocolId: 'MQTT', protocolVersion: 4, reconnectPeriod: 0, will: {
|
|
20151
20175
|
topic: 'WillMsg',
|
|
20152
20176
|
payload: 'Connection Closed abnormally..!',
|
|
20153
20177
|
qos: 0,
|
|
@@ -20278,54 +20302,153 @@ const getFeedSettings = async () => {
|
|
|
20278
20302
|
}));
|
|
20279
20303
|
};
|
|
20280
20304
|
|
|
20281
|
-
const
|
|
20305
|
+
const COLLECTION_DEFAULT_PAGINATION_LIMIT = 5;
|
|
20306
|
+
const COLLECTION_DEFAULT_CACHING_POLICY = 'cache_then_server';
|
|
20307
|
+
const ENABLE_CACHE_MESSAGE = 'For using Live Collection feature you need to enable Cache!';
|
|
20308
|
+
const LIVE_OBJECT_ENABLE_CACHE_MESSAGE = 'For using Live Object feature you need to enable Cache!';
|
|
20309
|
+
const UNSYNCED_OBJECT_CACHED_AT_MESSAGE = 'Observing unsynced object is not supported by Live Object.';
|
|
20310
|
+
const UNSYNCED_OBJECT_CACHED_AT_VALUE = -5;
|
|
20311
|
+
const MINUTE = 60 * 1000;
|
|
20312
|
+
const ACCESS_TOKEN_WATCHER_INTERVAL = 10 * MINUTE;
|
|
20313
|
+
|
|
20314
|
+
// TODO: find a way to uniquely identify the device
|
|
20315
|
+
/** @hidden */
|
|
20316
|
+
const VERSION = '11.0.0';
|
|
20317
|
+
/** @hidden */
|
|
20318
|
+
const getDeviceId = () => {
|
|
20319
|
+
// here maybe put some auto detection of the device id based on if it's react or not?
|
|
20320
|
+
// anyhow we let consumer branch out if they want, but it doesn't mean we shouldn't try to be nice
|
|
20321
|
+
// and do things automatically
|
|
20322
|
+
// https://www.npmjs.com/package/react-native-device-info#getuniqueid
|
|
20323
|
+
// how to make sure that the react native package will not be included when build is for web
|
|
20324
|
+
return `ascWebSdk#${Math.random().toString(16)}`;
|
|
20325
|
+
};
|
|
20326
|
+
/** @hidden */
|
|
20327
|
+
const getDeviceModel = () => {
|
|
20328
|
+
var _a, _b;
|
|
20329
|
+
if ((_a = safeProcess.versions) === null || _a === void 0 ? void 0 : _a.node)
|
|
20330
|
+
return safeProcess.versions.node;
|
|
20331
|
+
if (navigator)
|
|
20332
|
+
return `${navigator.product.toLowerCase()}#${(_b = navigator.userAgent) !== null && _b !== void 0 ? _b : 'unknown_agent'}`;
|
|
20333
|
+
return 'unknown_model';
|
|
20334
|
+
};
|
|
20335
|
+
/** @hidden */
|
|
20336
|
+
const getDeviceInfo = () => {
|
|
20337
|
+
const model = getDeviceModel();
|
|
20338
|
+
return {
|
|
20339
|
+
version: VERSION,
|
|
20340
|
+
// TODO: replace with: kind: process.versions ? 'node' : 'web', when backend has fixed this
|
|
20341
|
+
kind: 'web',
|
|
20342
|
+
model,
|
|
20343
|
+
};
|
|
20344
|
+
};
|
|
20345
|
+
|
|
20346
|
+
/*
|
|
20347
|
+
* Session States cannot be set arbitarily.
|
|
20348
|
+
* These are the possible new states based on the current state
|
|
20349
|
+
* Check tech spec for details:
|
|
20350
|
+
* https://ekoapp.atlassian.net/wiki/spaces/UP/pages/2082537485/ASC+Core+-+Session+Management+3.0#Session-State
|
|
20351
|
+
*/
|
|
20352
|
+
const sessisonStateTransitions = {
|
|
20353
|
+
// notLoggedIn -> establishing
|
|
20354
|
+
["notLoggedIn" /* Amity.SessionStates.NOT_LOGGED_IN */]: ["establishing" /* Amity.SessionStates.ESTABLISHING */],
|
|
20355
|
+
// terminated -> establishing
|
|
20356
|
+
["terminated" /* Amity.SessionStates.TERMINATED */]: ["establishing" /* Amity.SessionStates.ESTABLISHING */],
|
|
20357
|
+
// establishing -> notLoggedIn, established
|
|
20358
|
+
["establishing" /* Amity.SessionStates.ESTABLISHING */]: [
|
|
20359
|
+
"notLoggedIn" /* Amity.SessionStates.NOT_LOGGED_IN */,
|
|
20360
|
+
"established" /* Amity.SessionStates.ESTABLISHED */,
|
|
20361
|
+
],
|
|
20362
|
+
/*
|
|
20363
|
+
* established -> notLoggedIn, terminated, tokenExpired, establishing (for
|
|
20364
|
+
* cases when logging in same user)
|
|
20365
|
+
*/
|
|
20366
|
+
["established" /* Amity.SessionStates.ESTABLISHED */]: [
|
|
20367
|
+
"notLoggedIn" /* Amity.SessionStates.NOT_LOGGED_IN */,
|
|
20368
|
+
"terminated" /* Amity.SessionStates.TERMINATED */,
|
|
20369
|
+
"tokenExpired" /* Amity.SessionStates.TOKEN_EXPIRED */,
|
|
20370
|
+
"establishing" /* Amity.SessionStates.ESTABLISHING */,
|
|
20371
|
+
],
|
|
20372
|
+
// tokenExpired -> establishing
|
|
20373
|
+
["tokenExpired" /* Amity.SessionStates.TOKEN_EXPIRED */]: ["establishing" /* Amity.SessionStates.ESTABLISHING */],
|
|
20374
|
+
};
|
|
20375
|
+
const isValidStateChange = (prevState, nextState) => {
|
|
20376
|
+
return sessisonStateTransitions[prevState].includes(nextState);
|
|
20377
|
+
};
|
|
20282
20378
|
/**
|
|
20283
|
-
*
|
|
20284
|
-
* import { createClient } from '@amityco/ts-sdk'
|
|
20285
|
-
* const client = createClient(apiKey, 'https://asc.server/', 'myClient')
|
|
20286
|
-
* ```
|
|
20379
|
+
* Checks if a {@link Amity.Client} instance is connected to ASC servers
|
|
20287
20380
|
*
|
|
20288
|
-
* Creates a new {@link Amity.Client} instance
|
|
20289
20381
|
*
|
|
20290
|
-
* @param
|
|
20291
|
-
* @
|
|
20292
|
-
*
|
|
20293
|
-
* @
|
|
20294
|
-
|
|
20382
|
+
* @param state the new session state
|
|
20383
|
+
* @returns a success boolean if connected
|
|
20384
|
+
*
|
|
20385
|
+
* @category private
|
|
20386
|
+
*/
|
|
20387
|
+
const setSessionState = (state) => {
|
|
20388
|
+
const client = getActiveClient();
|
|
20389
|
+
client.log('client/api/setSessionState', state);
|
|
20390
|
+
const { sessionState: prevState } = client;
|
|
20391
|
+
if (prevState === state)
|
|
20392
|
+
return false;
|
|
20393
|
+
// check if transition is valid
|
|
20394
|
+
if (!isValidStateChange(prevState, state)) {
|
|
20395
|
+
throw new ASCError(`Session state cannot change from ${prevState} to ${state}`, 800000 /* Amity.ClientError.UNKNOWN_ERROR */, "error" /* Amity.ErrorLevel.ERROR */);
|
|
20396
|
+
}
|
|
20397
|
+
client.sessionState = state;
|
|
20398
|
+
fireEvent('sessionStateChange', state);
|
|
20399
|
+
return true;
|
|
20400
|
+
};
|
|
20401
|
+
|
|
20402
|
+
/**
|
|
20403
|
+
* Retrieves a pair of {@link Amity.Tokens} necessary for connection
|
|
20404
|
+
*
|
|
20405
|
+
* @param params.userId The userId to use to issue a token
|
|
20406
|
+
* @param params.displayName The user's displayName
|
|
20407
|
+
* @param params.deviceId The user's device Id (can be manually set for native users)
|
|
20408
|
+
* @param params.authToken The authentication token - necessary when network option is set to secure
|
|
20409
|
+
* @return The accessToken for the given userId
|
|
20295
20410
|
*
|
|
20296
20411
|
* @category Client API
|
|
20297
|
-
*
|
|
20298
|
-
|
|
20299
|
-
|
|
20300
|
-
const
|
|
20301
|
-
log('client/api/
|
|
20302
|
-
|
|
20303
|
-
|
|
20412
|
+
* @hidden
|
|
20413
|
+
*/
|
|
20414
|
+
const getToken = async (params) => {
|
|
20415
|
+
const client = getActiveClient();
|
|
20416
|
+
client.log('client/api/getToken', params);
|
|
20417
|
+
const deviceInfo = getDeviceInfo();
|
|
20418
|
+
const { data } = await client.http.post('/api/v3/sessions', Object.assign(Object.assign({}, params), { deviceInfo }), {
|
|
20419
|
+
// @ts-ignore
|
|
20420
|
+
headers: { 'X-API-Key': client.apiKey },
|
|
20304
20421
|
});
|
|
20305
|
-
|
|
20306
|
-
|
|
20307
|
-
|
|
20308
|
-
|
|
20309
|
-
|
|
20310
|
-
|
|
20311
|
-
|
|
20312
|
-
|
|
20313
|
-
|
|
20314
|
-
|
|
20315
|
-
|
|
20316
|
-
|
|
20317
|
-
|
|
20318
|
-
|
|
20319
|
-
|
|
20320
|
-
|
|
20321
|
-
|
|
20322
|
-
|
|
20323
|
-
|
|
20324
|
-
|
|
20325
|
-
|
|
20326
|
-
|
|
20327
|
-
|
|
20328
|
-
|
|
20422
|
+
return data;
|
|
20423
|
+
};
|
|
20424
|
+
|
|
20425
|
+
/**
|
|
20426
|
+
* A util to set or refresh client token
|
|
20427
|
+
*
|
|
20428
|
+
* @param params.userId the user ID for the current session
|
|
20429
|
+
* @param params.displayName the user's displayName for the current session
|
|
20430
|
+
* @param params.deviceId Manual override of the user's device id (for device management)
|
|
20431
|
+
* @param params.authToken The authentication token - necessary when network option is set to secure
|
|
20432
|
+
* @returns token & user info
|
|
20433
|
+
*
|
|
20434
|
+
* @category private
|
|
20435
|
+
* @async
|
|
20436
|
+
*/
|
|
20437
|
+
const setClientToken = async (params) => {
|
|
20438
|
+
const client = getActiveClient();
|
|
20439
|
+
// begin establishing session
|
|
20440
|
+
setSessionState("establishing" /* Amity.SessionStates.ESTABLISHING */);
|
|
20441
|
+
// @ts-ignore: in that particular case, typing can be
|
|
20442
|
+
// ignored since we enforce default values just before.
|
|
20443
|
+
const { accessToken, users, expiresAt, issuedAt, refreshToken } = await getToken(params);
|
|
20444
|
+
// manually setup the token for http transport
|
|
20445
|
+
client.http.defaults.headers.common.Authorization = `Bearer ${accessToken}`;
|
|
20446
|
+
client.http.defaults.metadata = { tokenExpiry: expiresAt };
|
|
20447
|
+
// manually setup the token for ws transport
|
|
20448
|
+
client.ws.io.opts.query = { token: accessToken };
|
|
20449
|
+
client.token = { accessToken, issuedAt, expiresAt };
|
|
20450
|
+
setSessionState("established" /* Amity.SessionStates.ESTABLISHED */);
|
|
20451
|
+
return { accessToken, users, refreshToken };
|
|
20329
20452
|
};
|
|
20330
20453
|
|
|
20331
20454
|
/**
|
|
@@ -20336,7 +20459,6 @@ const createClient = (apiKey, apiRegion = API_REGIONS.SG, { debugSession = DEFAU
|
|
|
20336
20459
|
*
|
|
20337
20460
|
* Disconnects an {@link Amity.Client} instance from ASC servers
|
|
20338
20461
|
*
|
|
20339
|
-
* @param client The {@link Amity.Client} instance to disconnect
|
|
20340
20462
|
* @returns a success boolean if disconnected
|
|
20341
20463
|
*
|
|
20342
20464
|
* @category Client API
|
|
@@ -20355,42 +20477,30 @@ const disconnectClient = async () => {
|
|
|
20355
20477
|
client.ws.removeAllListeners();
|
|
20356
20478
|
client.mqtt.removeAllListeners();
|
|
20357
20479
|
client.userId = undefined;
|
|
20358
|
-
client.
|
|
20480
|
+
client.token = undefined;
|
|
20481
|
+
client.http.defaults.headers.common.Authorization = '';
|
|
20482
|
+
client.ws.io.opts.query = { token: '' };
|
|
20483
|
+
/*
|
|
20484
|
+
* for cases when session state is terminated (example on ban) or token expired,
|
|
20485
|
+
* the terminating block will set session state to terminated or for the or
|
|
20486
|
+
* in the case of expired token the same happens
|
|
20487
|
+
*
|
|
20488
|
+
* establishing state also ignored in cases where accessTokenExpiryWatcher
|
|
20489
|
+
* calls renewal. There is a possibility that renewal will be called before
|
|
20490
|
+
* disconnectClient finishes execution
|
|
20491
|
+
*/
|
|
20492
|
+
if (client.sessionState === "established" /* Amity.SessionStates.ESTABLISHED */)
|
|
20493
|
+
setSessionState("notLoggedIn" /* Amity.SessionStates.NOT_LOGGED_IN */);
|
|
20494
|
+
/*
|
|
20495
|
+
* Cache should be usable if tokenExpired
|
|
20496
|
+
* https://ekoapp.atlassian.net/wiki/spaces/UP/pages/2082537485/ASC+Core+-+Session+Management+3.0#SDK-usability-based-on-Session-State
|
|
20497
|
+
*/
|
|
20498
|
+
if (client.sessionState !== "tokenExpired" /* Amity.SessionStates.TOKEN_EXPIRED */ && client.cache) {
|
|
20499
|
+
client.cache = { data: {} };
|
|
20500
|
+
}
|
|
20359
20501
|
return !client.ws.connected;
|
|
20360
20502
|
};
|
|
20361
20503
|
|
|
20362
|
-
// TODO: find a way to uniquely identify the device
|
|
20363
|
-
/** @hidden */
|
|
20364
|
-
const VERSION = '11.0.0';
|
|
20365
|
-
/** @hidden */
|
|
20366
|
-
const getDeviceId = () => {
|
|
20367
|
-
// here maybe put some auto detection of the device id based on if it's react or not?
|
|
20368
|
-
// anyhow we let consumer branch out if they want, but it doesn't mean we shouldn't try to be nice
|
|
20369
|
-
// and do things automatically
|
|
20370
|
-
// https://www.npmjs.com/package/react-native-device-info#getuniqueid
|
|
20371
|
-
// how to make sure that the react native package will not be included when build is for web
|
|
20372
|
-
return `ascWebSdk#${Math.random().toString(16)}`;
|
|
20373
|
-
};
|
|
20374
|
-
/** @hidden */
|
|
20375
|
-
const getDeviceModel = () => {
|
|
20376
|
-
var _a, _b;
|
|
20377
|
-
if ((_a = safeProcess.versions) === null || _a === void 0 ? void 0 : _a.node)
|
|
20378
|
-
return safeProcess.versions.node;
|
|
20379
|
-
if (navigator)
|
|
20380
|
-
return `${navigator.product.toLowerCase()}#${(_b = navigator.userAgent) !== null && _b !== void 0 ? _b : 'unknown_agent'}`;
|
|
20381
|
-
return 'unknown_model';
|
|
20382
|
-
};
|
|
20383
|
-
/** @hidden */
|
|
20384
|
-
const getDeviceInfo = () => {
|
|
20385
|
-
const model = getDeviceModel();
|
|
20386
|
-
return {
|
|
20387
|
-
version: VERSION,
|
|
20388
|
-
// TODO: replace with: kind: process.versions ? 'node' : 'web', when backend has fixed this
|
|
20389
|
-
kind: 'web',
|
|
20390
|
-
model,
|
|
20391
|
-
};
|
|
20392
|
-
};
|
|
20393
|
-
|
|
20394
20504
|
/**
|
|
20395
20505
|
* Ingest a payload (v3) into the cache
|
|
20396
20506
|
*
|
|
@@ -20442,26 +20552,17 @@ const createUserEventSubscriber = (event, callback) => {
|
|
|
20442
20552
|
const onUserDeleted = (callback) => createUserEventSubscriber('user.deleted', callback);
|
|
20443
20553
|
|
|
20444
20554
|
/**
|
|
20445
|
-
*
|
|
20555
|
+
* Terminates {@link Amity.Client} instance
|
|
20446
20556
|
*
|
|
20447
|
-
* @param userId The userId to use to issue a token
|
|
20448
|
-
* @param displayName The user's displayName
|
|
20449
|
-
* @param deviceId The user's device Id (can be manually set for native users)
|
|
20450
|
-
* @param authToken The authentication token - necessary when network option is set to secure
|
|
20451
|
-
* @return The accessToken for the given userId
|
|
20452
20557
|
*
|
|
20453
|
-
*
|
|
20454
|
-
* @
|
|
20558
|
+
*
|
|
20559
|
+
* @category private
|
|
20455
20560
|
*/
|
|
20456
|
-
const
|
|
20561
|
+
const terminateClient = () => {
|
|
20457
20562
|
const client = getActiveClient();
|
|
20458
|
-
|
|
20459
|
-
|
|
20460
|
-
|
|
20461
|
-
// @ts-ignore
|
|
20462
|
-
headers: { 'X-API-Key': client.apiKey },
|
|
20463
|
-
});
|
|
20464
|
-
return data;
|
|
20563
|
+
setSessionState("terminated" /* Amity.SessionStates.TERMINATED */);
|
|
20564
|
+
client.sessionHandler = undefined;
|
|
20565
|
+
disconnectClient();
|
|
20465
20566
|
};
|
|
20466
20567
|
|
|
20467
20568
|
const EVENTS = [
|
|
@@ -20536,12 +20637,71 @@ const onClientDisconnected = (callback) => {
|
|
|
20536
20637
|
* @category Client Events
|
|
20537
20638
|
*/
|
|
20538
20639
|
const onClientBanned = (callback) => {
|
|
20539
|
-
|
|
20540
|
-
|
|
20541
|
-
|
|
20640
|
+
const client = getActiveClient();
|
|
20641
|
+
return createEventSubscriber(client, `client/onConnectionError(user.didGlobalBan)`, 'user.didGlobalBan', callback);
|
|
20642
|
+
};
|
|
20643
|
+
|
|
20644
|
+
/**
|
|
20645
|
+
* ```js
|
|
20646
|
+
* import { onSessionStateChange } from '@amityco/ts-sdk'
|
|
20647
|
+
* const dispose = onSessionStateChange((state: Amity.SessionStates) => {
|
|
20648
|
+
* // ...
|
|
20649
|
+
* })
|
|
20650
|
+
* ```
|
|
20651
|
+
*
|
|
20652
|
+
* Fired when any {@link Amity.Client} has a session state change
|
|
20653
|
+
*
|
|
20654
|
+
* @param callback The function to call when the event was fired
|
|
20655
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
20656
|
+
*
|
|
20657
|
+
* @category Client Events
|
|
20658
|
+
*/
|
|
20659
|
+
const onSessionStateChange = (callback) => {
|
|
20660
|
+
const client = getActiveClient();
|
|
20661
|
+
return createEventSubscriber(client, `client/onSessionStateChange`, 'sessionStateChange', callback);
|
|
20662
|
+
};
|
|
20663
|
+
|
|
20664
|
+
/**
|
|
20665
|
+
* ```js
|
|
20666
|
+
* import { onTokenExpired } from '@amityco/ts-sdk'
|
|
20667
|
+
* const dispose = onTokenExpired((state: Amity.SessionStates.TOKEN_EXPIRED) => {
|
|
20668
|
+
* // ...
|
|
20669
|
+
* })
|
|
20670
|
+
* ```
|
|
20671
|
+
*
|
|
20672
|
+
* Fired when any {@link Amity.Client} session state has expired
|
|
20673
|
+
*
|
|
20674
|
+
* @param callback The function to call when the event was fired
|
|
20675
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
20676
|
+
*
|
|
20677
|
+
* @category private
|
|
20678
|
+
*/
|
|
20679
|
+
const onTokenExpired = (callback) => {
|
|
20680
|
+
const client = getActiveClient();
|
|
20681
|
+
return createEventSubscriber(client, `client/onTokenExpired`, 'tokenExpired', callback);
|
|
20682
|
+
};
|
|
20683
|
+
|
|
20684
|
+
/**
|
|
20685
|
+
*
|
|
20686
|
+
* Fired when any {@link Amity.Client} session state has terminated
|
|
20687
|
+
*
|
|
20688
|
+
* @param callback The function to call when the event was fired
|
|
20689
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
20690
|
+
*
|
|
20691
|
+
* @category private
|
|
20692
|
+
*/
|
|
20693
|
+
const onTokenTerminated = (callback) => {
|
|
20694
|
+
const client = getActiveClient();
|
|
20695
|
+
return createEventSubscriber(client, `client/onTokenTerminated`, 'tokenTerminated', callback);
|
|
20542
20696
|
};
|
|
20543
20697
|
|
|
20544
20698
|
/* eslint-disable no-param-reassign */
|
|
20699
|
+
/*
|
|
20700
|
+
* declared earlier to accomodate case when logging in with a different user
|
|
20701
|
+
* than the one already connected, in which case the existing subscriptions need
|
|
20702
|
+
* to be cleared
|
|
20703
|
+
*/
|
|
20704
|
+
const subscriptions = [];
|
|
20545
20705
|
/**
|
|
20546
20706
|
* ```js
|
|
20547
20707
|
* import { connectClient } from '@amityco/ts-sdk/client/api'
|
|
@@ -20562,44 +20722,258 @@ const onClientBanned = (callback) => {
|
|
|
20562
20722
|
* @category Client API
|
|
20563
20723
|
* @async
|
|
20564
20724
|
*/
|
|
20565
|
-
const connectClient = async (params, config) => {
|
|
20566
|
-
var _a;
|
|
20725
|
+
const connectClient = async (params, sessionHandler, config) => {
|
|
20726
|
+
var _a, _b, _c;
|
|
20567
20727
|
const client = getActiveClient();
|
|
20568
|
-
|
|
20728
|
+
let unsubWatcher;
|
|
20729
|
+
client.log('client/api/connectClient', Object.assign({ apiKey: client.apiKey, sessionState: client.sessionState }, params));
|
|
20730
|
+
// if connecting to a different userId than the one that is connected currently
|
|
20731
|
+
if (client.userId && client.userId !== params.userId) {
|
|
20732
|
+
await disconnectClient();
|
|
20733
|
+
// Remove subscription to ban and delete
|
|
20734
|
+
subscriptions.forEach(fn => fn());
|
|
20735
|
+
}
|
|
20569
20736
|
// default values
|
|
20570
20737
|
(_a = params.deviceId) !== null && _a !== void 0 ? _a : (params.deviceId = getDeviceId());
|
|
20571
|
-
|
|
20572
|
-
|
|
20573
|
-
|
|
20574
|
-
|
|
20575
|
-
|
|
20576
|
-
|
|
20577
|
-
|
|
20578
|
-
|
|
20579
|
-
|
|
20580
|
-
|
|
20581
|
-
|
|
20582
|
-
|
|
20583
|
-
|
|
20584
|
-
client.
|
|
20585
|
-
client.
|
|
20586
|
-
|
|
20587
|
-
|
|
20588
|
-
|
|
20589
|
-
|
|
20590
|
-
|
|
20738
|
+
try {
|
|
20739
|
+
// @ts-ignore: in that particular case, typing can be
|
|
20740
|
+
// ignored since we enforce default values just before.
|
|
20741
|
+
const { users } = await setClientToken(params);
|
|
20742
|
+
// FIXME: events are duplicated if connectClient is called few times without disconnectClient
|
|
20743
|
+
// wire websocket events to our event emitter
|
|
20744
|
+
proxyWebsocketEvents(client.ws, client.emitter);
|
|
20745
|
+
// TODO: in phase 2, this should not be necessary. we should have WS
|
|
20746
|
+
// to be connected "on demand" rather than "by default"
|
|
20747
|
+
await new Promise(resolve => {
|
|
20748
|
+
client.ws.once('connect', resolve);
|
|
20749
|
+
client.ws.open();
|
|
20750
|
+
});
|
|
20751
|
+
client.userId = params.userId;
|
|
20752
|
+
client.sessionHandler = sessionHandler;
|
|
20753
|
+
/*
|
|
20754
|
+
* Cannot push to subscriptions as watcher needs to continue working even if
|
|
20755
|
+
* token expires
|
|
20756
|
+
*/
|
|
20757
|
+
unsubWatcher = client.accessTokenExpiryWatcher((_b = client.token) === null || _b === void 0 ? void 0 : _b.issuedAt, (_c = client.token) === null || _c === void 0 ? void 0 : _c.expiresAt, sessionHandler);
|
|
20758
|
+
setActiveUser(users[0]);
|
|
20759
|
+
}
|
|
20760
|
+
catch (error) {
|
|
20761
|
+
/*
|
|
20762
|
+
* if getting token failed session state reverts to initial state when app
|
|
20763
|
+
* is first launched
|
|
20764
|
+
*/
|
|
20765
|
+
setSessionState("notLoggedIn" /* Amity.SessionStates.NOT_LOGGED_IN */);
|
|
20766
|
+
// pass error down tree so the calling function handle it
|
|
20767
|
+
throw error;
|
|
20768
|
+
}
|
|
20769
|
+
if ((config === null || config === void 0 ? void 0 : config.disableRTE) !== true) {
|
|
20591
20770
|
subscribeTopic(getNetworkTopic());
|
|
20771
|
+
subscribeTopic(getSmartFeedChannelTopic());
|
|
20772
|
+
subscribeTopic(getSmartFeedSubChannelTopic());
|
|
20773
|
+
subscribeTopic(getSmartFeedMessageTopic());
|
|
20774
|
+
// subscribing to user topic is necessary to handle ban event
|
|
20775
|
+
subscribeTopic(getUserTopic(getActiveUser()));
|
|
20592
20776
|
}
|
|
20593
|
-
|
|
20594
|
-
|
|
20595
|
-
|
|
20777
|
+
subscriptions.push(
|
|
20778
|
+
// GLOBAL_BAN
|
|
20779
|
+
onClientBanned((_) => {
|
|
20780
|
+
terminateClient();
|
|
20781
|
+
subscriptions.forEach(fn => fn());
|
|
20782
|
+
unsubWatcher();
|
|
20783
|
+
}), onTokenTerminated(_ => {
|
|
20784
|
+
terminateClient();
|
|
20785
|
+
subscriptions.forEach(fn => fn());
|
|
20786
|
+
unsubWatcher();
|
|
20787
|
+
}), onUserDeleted((user) => {
|
|
20788
|
+
if (user.userId === client.userId) {
|
|
20789
|
+
terminateClient();
|
|
20596
20790
|
subscriptions.forEach(fn => fn());
|
|
20597
|
-
|
|
20598
|
-
|
|
20599
|
-
|
|
20791
|
+
unsubWatcher();
|
|
20792
|
+
}
|
|
20793
|
+
}), onTokenExpired(state => {
|
|
20794
|
+
setSessionState(state);
|
|
20795
|
+
disconnectClient();
|
|
20796
|
+
subscriptions.forEach(fn => fn());
|
|
20797
|
+
}), onUserDeleted(() => null));
|
|
20600
20798
|
return client.ws.connected;
|
|
20601
20799
|
};
|
|
20602
20800
|
|
|
20801
|
+
/*
|
|
20802
|
+
* Renewal defintion accepted by SessionHandler interface
|
|
20803
|
+
*
|
|
20804
|
+
* Tech Spec:
|
|
20805
|
+
* https://ekoapp.atlassian.net/wiki/spaces/UP/pages/2082537485/ASC+Core+-+Session+Management+3.0#Session-Handler
|
|
20806
|
+
*
|
|
20807
|
+
* @category private
|
|
20808
|
+
*/
|
|
20809
|
+
const renewal = () => {
|
|
20810
|
+
let tokenRenewed = false;
|
|
20811
|
+
let renewTimeoutId;
|
|
20812
|
+
const client = getActiveClient();
|
|
20813
|
+
client.log('initiating access token renewal');
|
|
20814
|
+
/*
|
|
20815
|
+
* Renews a token if it is hasn't been renewed before. Also marks token as
|
|
20816
|
+
* renewed once done
|
|
20817
|
+
* Per instance of Renewal, only one renewal is allowed
|
|
20818
|
+
*/
|
|
20819
|
+
const renewToken = async (authToken) => {
|
|
20820
|
+
const { userId, displayName } = getActiveUser();
|
|
20821
|
+
const deviceId = getDeviceId();
|
|
20822
|
+
const params = { userId, displayName, authToken, deviceId };
|
|
20823
|
+
if (client.sessionState === "tokenExpired" /* Amity.SessionStates.TOKEN_EXPIRED */) {
|
|
20824
|
+
// sessionHandler will never be empty when renewal is instantiatied
|
|
20825
|
+
await connectClient(params, client.sessionHandler);
|
|
20826
|
+
}
|
|
20827
|
+
else {
|
|
20828
|
+
// about to expire
|
|
20829
|
+
// @ts-ignore: in that particular case, typing can be
|
|
20830
|
+
// ignored since we enforce default values just before.
|
|
20831
|
+
await setClientToken(params);
|
|
20832
|
+
}
|
|
20833
|
+
tokenRenewed = true;
|
|
20834
|
+
if (renewTimeoutId)
|
|
20835
|
+
clearTimeout(renewTimeoutId);
|
|
20836
|
+
};
|
|
20837
|
+
return {
|
|
20838
|
+
renew: () => {
|
|
20839
|
+
if (tokenRenewed) {
|
|
20840
|
+
console.log("'renew' method can be called only once per renewal instance");
|
|
20841
|
+
return;
|
|
20842
|
+
}
|
|
20843
|
+
renewToken();
|
|
20844
|
+
},
|
|
20845
|
+
renewWithAuthToken: (authToken) => {
|
|
20846
|
+
if (tokenRenewed) {
|
|
20847
|
+
console.log("'renewWithAuthToken' method can be called only once per renewal instance");
|
|
20848
|
+
return;
|
|
20849
|
+
}
|
|
20850
|
+
renewToken(authToken);
|
|
20851
|
+
},
|
|
20852
|
+
unableToRetrieveAuthToken: () => {
|
|
20853
|
+
renewTimeoutId = setTimeout(() => {
|
|
20854
|
+
var _a;
|
|
20855
|
+
(_a = client.sessionHandler) === null || _a === void 0 ? void 0 : _a.sessionWillRenewAccessToken(renewal());
|
|
20856
|
+
}, ACCESS_TOKEN_WATCHER_INTERVAL);
|
|
20857
|
+
},
|
|
20858
|
+
};
|
|
20859
|
+
};
|
|
20860
|
+
|
|
20861
|
+
const ABOUT_TO_EXPIRE_THRESHOLD = 80 / 100;
|
|
20862
|
+
const COMPENSATED_DELAY = 5 * MINUTE;
|
|
20863
|
+
/*
|
|
20864
|
+
* a helper function to check if the token has expires
|
|
20865
|
+
*
|
|
20866
|
+
* @param token to be checked
|
|
20867
|
+
* @returns boolean indicating if token expires
|
|
20868
|
+
*
|
|
20869
|
+
* @category private
|
|
20870
|
+
*/
|
|
20871
|
+
const isExpired = (expiresAt) => Date.now() > Date.parse(expiresAt) - COMPENSATED_DELAY;
|
|
20872
|
+
/*
|
|
20873
|
+
* a helper function to check if the token is about to expire
|
|
20874
|
+
*
|
|
20875
|
+
* @param token to be checked
|
|
20876
|
+
* @returns boolean indicating if token is aboutToExpire
|
|
20877
|
+
*
|
|
20878
|
+
* @category private
|
|
20879
|
+
*/
|
|
20880
|
+
const isAboutToExpire = (expiresAt, issuedAt) => {
|
|
20881
|
+
const expires = Date.parse(expiresAt);
|
|
20882
|
+
const issued = Date.parse(issuedAt);
|
|
20883
|
+
const now = Date.now();
|
|
20884
|
+
const duration = expires - issued - COMPENSATED_DELAY;
|
|
20885
|
+
const aboutToExpireAt = issued + duration * ABOUT_TO_EXPIRE_THRESHOLD;
|
|
20886
|
+
return now > aboutToExpireAt && now < expires;
|
|
20887
|
+
};
|
|
20888
|
+
/*
|
|
20889
|
+
* Monitors time to expire of token and updates session state to aboutToExpire
|
|
20890
|
+
*
|
|
20891
|
+
* @returns intervalId to be cleared after trigger
|
|
20892
|
+
*
|
|
20893
|
+
* @category private
|
|
20894
|
+
*/
|
|
20895
|
+
const accessTokenExpiryWatcher = (expiresAt, issuedAt, sessionHandler) => {
|
|
20896
|
+
const interval = setInterval(() => {
|
|
20897
|
+
if (isExpired(expiresAt)) {
|
|
20898
|
+
/*
|
|
20899
|
+
* the event handler will take care of updating session state
|
|
20900
|
+
* Note, this will also clear the interval id, so this event will only be
|
|
20901
|
+
* fired once
|
|
20902
|
+
*/
|
|
20903
|
+
fireEvent('tokenExpired', "tokenExpired" /* Amity.SessionStates.TOKEN_EXPIRED */);
|
|
20904
|
+
/*
|
|
20905
|
+
* https://ekoapp.atlassian.net/wiki/spaces/UP/pages/2082537485/ASC+Core+-+Session+Management+3.0#Automatically-initiate-renewal-flow
|
|
20906
|
+
*
|
|
20907
|
+
* Why sechduled task?
|
|
20908
|
+
* Since fireEvent is scheduled, it will be called
|
|
20909
|
+
* after sessionHandler leading to an invalid state change from
|
|
20910
|
+
* establishing to tokenExpired
|
|
20911
|
+
*/
|
|
20912
|
+
scheduleTask(() => sessionHandler.sessionWillRenewAccessToken(renewal()));
|
|
20913
|
+
return;
|
|
20914
|
+
}
|
|
20915
|
+
if (isAboutToExpire(issuedAt, expiresAt)) {
|
|
20916
|
+
sessionHandler.sessionWillRenewAccessToken(renewal());
|
|
20917
|
+
}
|
|
20918
|
+
}, ACCESS_TOKEN_WATCHER_INTERVAL);
|
|
20919
|
+
return () => clearInterval(interval);
|
|
20920
|
+
};
|
|
20921
|
+
|
|
20922
|
+
const DEFAULT_DEBUG_SESSION = 'amity';
|
|
20923
|
+
/**
|
|
20924
|
+
* ```js
|
|
20925
|
+
* import { createClient } from '@amityco/ts-sdk'
|
|
20926
|
+
* const client = createClient(apiKey, 'https://asc.server/', 'myClient')
|
|
20927
|
+
* ```
|
|
20928
|
+
*
|
|
20929
|
+
* Creates a new {@link Amity.Client} instance
|
|
20930
|
+
*
|
|
20931
|
+
* @param apiKey for the {@link Amity.Client} instance
|
|
20932
|
+
* @param apiRegion endpoint to connect to
|
|
20933
|
+
* @param apiEndpoint custom endpoint in case you don't want to use a preset endpoint
|
|
20934
|
+
* @param param.debugSession session's identifier for the client's logger instance
|
|
20935
|
+
* @returns A {@link Amity.Client} instance
|
|
20936
|
+
*
|
|
20937
|
+
* @category Client API
|
|
20938
|
+
* */
|
|
20939
|
+
const createClient = (apiKey, apiRegion = API_REGIONS.SG, { debugSession = DEFAULT_DEBUG_SESSION, apiEndpoint, } = {}) => {
|
|
20940
|
+
var _a, _b;
|
|
20941
|
+
const log = createLogger(debugSession);
|
|
20942
|
+
log('client/api/createClient', {
|
|
20943
|
+
apiKey: apiKey.replace(/.{5}$/g, 'xxxxx'),
|
|
20944
|
+
apiRegion,
|
|
20945
|
+
});
|
|
20946
|
+
const httpEndpoint = (_a = apiEndpoint === null || apiEndpoint === void 0 ? void 0 : apiEndpoint.http) !== null && _a !== void 0 ? _a : computeUrl('http', apiRegion);
|
|
20947
|
+
const mqttEndpoint = (_b = apiEndpoint === null || apiEndpoint === void 0 ? void 0 : apiEndpoint.mqtt) !== null && _b !== void 0 ? _b : computeUrl('mqtt', apiRegion);
|
|
20948
|
+
const http = createHttpTransport(httpEndpoint);
|
|
20949
|
+
const ws = createWebsocketTransport(httpEndpoint);
|
|
20950
|
+
const mqtt = createMqttTransport(mqttEndpoint);
|
|
20951
|
+
const emitter = createEventEmitter();
|
|
20952
|
+
const sessionState = "notLoggedIn" /* Amity.SessionStates.NOT_LOGGED_IN */;
|
|
20953
|
+
const sessionHandler = undefined;
|
|
20954
|
+
const client = {
|
|
20955
|
+
version: `${VERSION$1}`,
|
|
20956
|
+
apiKey,
|
|
20957
|
+
log,
|
|
20958
|
+
http,
|
|
20959
|
+
ws,
|
|
20960
|
+
mqtt,
|
|
20961
|
+
emitter,
|
|
20962
|
+
accessTokenExpiryWatcher,
|
|
20963
|
+
use: () => setActiveClient(client),
|
|
20964
|
+
sessionState,
|
|
20965
|
+
sessionHandler,
|
|
20966
|
+
getFeedSettings,
|
|
20967
|
+
};
|
|
20968
|
+
try {
|
|
20969
|
+
return getActiveClient();
|
|
20970
|
+
}
|
|
20971
|
+
catch (_c) {
|
|
20972
|
+
setActiveClient(client);
|
|
20973
|
+
}
|
|
20974
|
+
return client;
|
|
20975
|
+
};
|
|
20976
|
+
|
|
20603
20977
|
/**
|
|
20604
20978
|
* ```js
|
|
20605
20979
|
* import { isConnected } from '@amityco/ts-sdk'
|
|
@@ -20941,23 +21315,6 @@ const updateUser = async (userId, patch) => {
|
|
|
20941
21315
|
};
|
|
20942
21316
|
};
|
|
20943
21317
|
|
|
20944
|
-
/**
|
|
20945
|
-
* ```js
|
|
20946
|
-
* import { onUserFetched } from '@amityco/ts-sdk'
|
|
20947
|
-
* const dispose = onUserFetched(user => {
|
|
20948
|
-
* // ...
|
|
20949
|
-
* })
|
|
20950
|
-
* ```
|
|
20951
|
-
*
|
|
20952
|
-
* Fired when a {@link Amity.User} has been fetched
|
|
20953
|
-
*
|
|
20954
|
-
* @param callback The function to call when the event was fired
|
|
20955
|
-
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
20956
|
-
*
|
|
20957
|
-
* @category User Events
|
|
20958
|
-
*/
|
|
20959
|
-
const onUserFetched = (callback) => createUserEventSubscriber('user.fetched', callback);
|
|
20960
|
-
|
|
20961
21318
|
/**
|
|
20962
21319
|
* ```js
|
|
20963
21320
|
* import { onUserUpdated } from '@amityco/ts-sdk'
|
|
@@ -21026,61 +21383,74 @@ const onUserUnflagged = (callback) => createUserEventSubscriber('user.unflagged'
|
|
|
21026
21383
|
*/
|
|
21027
21384
|
const onUserFlagCleared = (callback) => createUserEventSubscriber('user.flagCleared', callback);
|
|
21028
21385
|
|
|
21029
|
-
|
|
21030
|
-
|
|
21031
|
-
|
|
21032
|
-
const
|
|
21033
|
-
|
|
21034
|
-
|
|
21386
|
+
/**
|
|
21387
|
+
* ```js
|
|
21388
|
+
* import { onUserFetched } from '@amityco/ts-sdk'
|
|
21389
|
+
* const dispose = onUserFetched(user => {
|
|
21390
|
+
* // ...
|
|
21391
|
+
* })
|
|
21392
|
+
* ```
|
|
21393
|
+
*
|
|
21394
|
+
* Fired when a {@link Amity.User} has been fetched
|
|
21395
|
+
*
|
|
21396
|
+
* @param callback The function to call when the event was fired
|
|
21397
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
21398
|
+
*
|
|
21399
|
+
* @category User Events
|
|
21400
|
+
*/
|
|
21401
|
+
const onUserFetched = (callback) => createUserEventSubscriber('user.fetched', callback);
|
|
21035
21402
|
|
|
21036
21403
|
/**
|
|
21037
21404
|
* ```js
|
|
21038
|
-
* import { liveUser } from '@amityco/ts-sdk'
|
|
21405
|
+
* import { liveUser } from '@amityco/ts-sdk';
|
|
21039
21406
|
*
|
|
21040
|
-
* let
|
|
21041
|
-
*
|
|
21407
|
+
* let user;
|
|
21408
|
+
*
|
|
21409
|
+
* const unsubscribe = liveUser(userId, response => {
|
|
21410
|
+
* user = response.data;
|
|
21411
|
+
* });
|
|
21042
21412
|
* ```
|
|
21043
21413
|
*
|
|
21044
21414
|
* Observe all mutation on a given {@link Amity.User}
|
|
21045
21415
|
*
|
|
21046
21416
|
* @param userId the ID of the user to observe
|
|
21047
21417
|
* @param callback the function to call when new data are available
|
|
21048
|
-
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the
|
|
21418
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the user
|
|
21049
21419
|
*
|
|
21050
21420
|
* @category Message Live Object
|
|
21051
21421
|
*/
|
|
21052
21422
|
const liveUser = (userId, callback) => {
|
|
21053
21423
|
const { log, cache } = getActiveClient();
|
|
21054
|
-
|
|
21424
|
+
const timestamp = Date.now();
|
|
21425
|
+
log(`liveUser(tmpid: ${timestamp}) > listen`);
|
|
21055
21426
|
if (!cache) {
|
|
21056
21427
|
console.log(LIVE_OBJECT_ENABLE_CACHE_MESSAGE);
|
|
21057
21428
|
}
|
|
21058
|
-
|
|
21059
|
-
log(`liveUser(tmpid: ${timestamp}) > listen`);
|
|
21429
|
+
let user;
|
|
21060
21430
|
const disposers = [];
|
|
21061
21431
|
const dispatcher = (data) => {
|
|
21062
|
-
var _a;
|
|
21063
|
-
// we check updatedAt to see if the current snapshot is new so
|
|
21064
|
-
// we won't send old data back, only fresh new data
|
|
21065
|
-
if (user && ((_a = data.data) === null || _a === void 0 ? void 0 : _a.updatedAt) === (user === null || user === void 0 ? void 0 : user.updatedAt) && data.origin !== 'server')
|
|
21066
|
-
return;
|
|
21067
|
-
callback(data);
|
|
21068
21432
|
user = data.data;
|
|
21433
|
+
callback(data);
|
|
21069
21434
|
};
|
|
21070
|
-
const realtimeRouter = (
|
|
21071
|
-
|
|
21435
|
+
const realtimeRouter = (eventUser) => {
|
|
21436
|
+
if (userId !== eventUser.userId) {
|
|
21437
|
+
return;
|
|
21438
|
+
}
|
|
21439
|
+
if (user && !hasUpdates(user, eventUser)) {
|
|
21440
|
+
return;
|
|
21441
|
+
}
|
|
21442
|
+
dispatcher({ loading: false, data: eventUser, origin: 'event' });
|
|
21072
21443
|
};
|
|
21073
21444
|
const onFetch = () => {
|
|
21074
21445
|
const query = createQuery(getUser, userId, true);
|
|
21075
21446
|
runQuery(query, ({ loading, data, error, origin }) => {
|
|
21076
21447
|
dispatcher({ loading, data, origin, error });
|
|
21077
21448
|
if (error) {
|
|
21078
|
-
// Terminate observe functionality on error
|
|
21079
21449
|
disposers.forEach(fn => fn());
|
|
21080
21450
|
}
|
|
21081
21451
|
});
|
|
21082
21452
|
};
|
|
21083
|
-
disposers.push(onUserFetched(
|
|
21453
|
+
disposers.push(onUserFetched(realtimeRouter), onUserUpdated(realtimeRouter), onUserDeleted(realtimeRouter), onUserFlagged(realtimeRouter), onUserUnflagged(realtimeRouter), onUserFlagCleared(realtimeRouter));
|
|
21084
21454
|
onFetch();
|
|
21085
21455
|
return () => {
|
|
21086
21456
|
log(`liveUser(tmpid: ${timestamp}) > dispose`);
|
|
@@ -21494,7 +21864,7 @@ getRole.locally = (roleId) => {
|
|
|
21494
21864
|
const dispatchReactable = (referenceType, model) => {
|
|
21495
21865
|
if (referenceType === 'message')
|
|
21496
21866
|
// @ts-ignore: refactor later
|
|
21497
|
-
fireEvent('
|
|
21867
|
+
fireEvent('local.message.updated', { messages: [model] });
|
|
21498
21868
|
else if (referenceType === 'post')
|
|
21499
21869
|
// @ts-ignore: refactor later
|
|
21500
21870
|
fireEvent('post.updated', { posts: [model] });
|
|
@@ -21503,6 +21873,100 @@ const dispatchReactable = (referenceType, model) => {
|
|
|
21503
21873
|
fireEvent('comment.updated', { comments: [model] });
|
|
21504
21874
|
};
|
|
21505
21875
|
|
|
21876
|
+
/** @hidden */
|
|
21877
|
+
/*
|
|
21878
|
+
* @param message payload from http request without myReactions
|
|
21879
|
+
* add myReactions to http response if the event was a reaction event
|
|
21880
|
+
*/
|
|
21881
|
+
const prepareMessagePayloadForCache = (payload) => {
|
|
21882
|
+
const client = getActiveClient();
|
|
21883
|
+
const cached = pullFromCache(['message', 'get', payload.messageId]);
|
|
21884
|
+
// '[]' in cases where the new reaction is the first one
|
|
21885
|
+
const myReactions = (cached === null || cached === void 0 ? void 0 : cached.data.myReactions) || [];
|
|
21886
|
+
// add myReactions to the payload
|
|
21887
|
+
Object.assign(payload, { myReactions });
|
|
21888
|
+
// check if there are any updates to the reactions
|
|
21889
|
+
const latestReaction = payload === null || payload === void 0 ? void 0 : payload.latestReaction;
|
|
21890
|
+
const isLatestReactionMine = latestReaction && latestReaction.userId === client.userId;
|
|
21891
|
+
if (!isLatestReactionMine) {
|
|
21892
|
+
return;
|
|
21893
|
+
}
|
|
21894
|
+
// new reaction added
|
|
21895
|
+
if (latestReaction.eventName === 'add' && !myReactions.includes(latestReaction.reactionName)) {
|
|
21896
|
+
Object.assign(payload, {
|
|
21897
|
+
myReactions: [...myReactions, latestReaction.reactionName],
|
|
21898
|
+
});
|
|
21899
|
+
}
|
|
21900
|
+
// existing reaction removed
|
|
21901
|
+
if (latestReaction.eventName === 'remove' && myReactions.includes(latestReaction.reactionName)) {
|
|
21902
|
+
Object.assign(payload, {
|
|
21903
|
+
myReactions: myReactions.filter(x => x !== latestReaction.reactionName),
|
|
21904
|
+
});
|
|
21905
|
+
}
|
|
21906
|
+
};
|
|
21907
|
+
|
|
21908
|
+
function convertFromRaw$2(_a) {
|
|
21909
|
+
var { channelPublicId, childCount, creatorPublicId, mentionedUsers, messageFeedId, myReactions, reactionCount, reactions, referenceId, segment } = _a, rest = __rest(_a, ["channelPublicId", "childCount", "creatorPublicId", "mentionedUsers", "messageFeedId", "myReactions", "reactionCount", "reactions", "referenceId", "segment"]);
|
|
21910
|
+
let cache;
|
|
21911
|
+
if (referenceId) {
|
|
21912
|
+
try {
|
|
21913
|
+
cache = pullFromCache(['message', 'get', referenceId]);
|
|
21914
|
+
// eslint-disable-next-line no-empty
|
|
21915
|
+
}
|
|
21916
|
+
catch (_b) { }
|
|
21917
|
+
}
|
|
21918
|
+
if (!cache) {
|
|
21919
|
+
try {
|
|
21920
|
+
cache = pullFromCache(['message', 'get', rest.messageId]);
|
|
21921
|
+
// eslint-disable-next-line no-empty
|
|
21922
|
+
}
|
|
21923
|
+
catch (_c) { }
|
|
21924
|
+
}
|
|
21925
|
+
const out = Object.assign(Object.assign({}, rest), { channelId: channelPublicId, channelSegment: segment, childrenNumber: childCount, creatorId: creatorPublicId, reactions: reactions !== null && reactions !== void 0 ? reactions : {}, reactionsCount: reactionCount, subChannelId: messageFeedId, uniqueId: cache ? cache.data.uniqueId : rest.messageId });
|
|
21926
|
+
if (mentionedUsers) {
|
|
21927
|
+
out.mentionees = mentionedUsers.map(mention => {
|
|
21928
|
+
if (mention.type === 'channel') {
|
|
21929
|
+
return mention;
|
|
21930
|
+
}
|
|
21931
|
+
return { type: 'user', userIds: mention.userPublicIds };
|
|
21932
|
+
});
|
|
21933
|
+
}
|
|
21934
|
+
if (myReactions) {
|
|
21935
|
+
// http response
|
|
21936
|
+
out.myReactions = myReactions;
|
|
21937
|
+
}
|
|
21938
|
+
else {
|
|
21939
|
+
// mqtt event
|
|
21940
|
+
try {
|
|
21941
|
+
prepareMessagePayloadForCache(out);
|
|
21942
|
+
// eslint-disable-next-line no-empty
|
|
21943
|
+
}
|
|
21944
|
+
catch (_d) { }
|
|
21945
|
+
}
|
|
21946
|
+
return out;
|
|
21947
|
+
}
|
|
21948
|
+
const prepareMessagePayload = (payload) => {
|
|
21949
|
+
return Object.assign(Object.assign({}, payload), { messages: payload.messages.map(convertFromRaw$2) });
|
|
21950
|
+
};
|
|
21951
|
+
const convertResponseFromRaw = (response) => {
|
|
21952
|
+
return Object.assign(Object.assign({}, response), { data: Object.assign(Object.assign({}, response.data), { messages: response.data.messages.map(convertFromRaw$2) }) });
|
|
21953
|
+
};
|
|
21954
|
+
function convertParams(_a) {
|
|
21955
|
+
var { subChannelId, mentionees } = _a, rest = __rest(_a, ["subChannelId", "mentionees"]);
|
|
21956
|
+
return Object.assign({ messageFeedId: subChannelId, mentionedUsers: mentionees }, rest);
|
|
21957
|
+
}
|
|
21958
|
+
function convertQueryParams$1(_a) {
|
|
21959
|
+
var { page, sortBy, subChannelId, tags } = _a, rest = __rest(_a, ["page", "sortBy", "subChannelId", "tags"]);
|
|
21960
|
+
const out = Object.assign(Object.assign({}, rest), { messageFeedId: subChannelId, options: { token: toToken(page || { limit: 10 }, 'afterbeforeraw') } });
|
|
21961
|
+
if (sortBy !== undefined) {
|
|
21962
|
+
out.options.sortBy = sortBy;
|
|
21963
|
+
}
|
|
21964
|
+
if (tags) {
|
|
21965
|
+
out.includeTags = tags;
|
|
21966
|
+
}
|
|
21967
|
+
return out;
|
|
21968
|
+
}
|
|
21969
|
+
|
|
21506
21970
|
/**
|
|
21507
21971
|
* ```js
|
|
21508
21972
|
* import { getMessage } from '@amityco/ts-sdk'
|
|
@@ -21527,7 +21991,9 @@ const getMessage = async (messageId, isLive = false) => {
|
|
|
21527
21991
|
let data;
|
|
21528
21992
|
try {
|
|
21529
21993
|
// API-FIX: endpoint should not be /list, parameters should be querystring.
|
|
21530
|
-
const response = await client.http
|
|
21994
|
+
const response = await client.http
|
|
21995
|
+
.get(`/api/v5/messages/${encodeURIComponent(messageId)}`)
|
|
21996
|
+
.then(convertResponseFromRaw);
|
|
21531
21997
|
data = response.data;
|
|
21532
21998
|
}
|
|
21533
21999
|
catch (error) {
|
|
@@ -21540,7 +22006,7 @@ const getMessage = async (messageId, isLive = false) => {
|
|
|
21540
22006
|
if (client.cache)
|
|
21541
22007
|
ingestInCache(data, { cachedAt });
|
|
21542
22008
|
const { messages } = data;
|
|
21543
|
-
fireEvent('
|
|
22009
|
+
fireEvent('local.message.fetched', { messages });
|
|
21544
22010
|
return {
|
|
21545
22011
|
data: messages.find(message => message.messageId === messageId),
|
|
21546
22012
|
cachedAt,
|
|
@@ -21590,15 +22056,16 @@ getMessage.locally = (messageId) => {
|
|
|
21590
22056
|
const getMessages = async (messageIds) => {
|
|
21591
22057
|
const client = getActiveClient();
|
|
21592
22058
|
client.log('message/getMessages', messageIds);
|
|
21593
|
-
|
|
21594
|
-
|
|
21595
|
-
params: { messageIds },
|
|
21596
|
-
})
|
|
22059
|
+
const { data } = await client.http
|
|
22060
|
+
.get(`/api/v5/messages/list`, {
|
|
22061
|
+
params: { messageIds: messageIds.map(encodeURIComponent) },
|
|
22062
|
+
})
|
|
22063
|
+
.then(convertResponseFromRaw);
|
|
21597
22064
|
const cachedAt = client.cache && Date.now();
|
|
21598
22065
|
if (client.cache)
|
|
21599
22066
|
ingestInCache(data, { cachedAt });
|
|
21600
22067
|
const { messages } = data;
|
|
21601
|
-
fireEvent('
|
|
22068
|
+
fireEvent('local.message.fetched', { messages });
|
|
21602
22069
|
return {
|
|
21603
22070
|
data: messages,
|
|
21604
22071
|
cachedAt,
|
|
@@ -21653,31 +22120,25 @@ getMessages.locally = (messageIds) => {
|
|
|
21653
22120
|
const queryMessages = async (query) => {
|
|
21654
22121
|
const client = getActiveClient();
|
|
21655
22122
|
client.log('message/queryMessages', query);
|
|
21656
|
-
const
|
|
22123
|
+
const params = convertQueryParams$1(query);
|
|
21657
22124
|
// API-FIX: parameters should be querystring. (1)
|
|
21658
22125
|
// API-FIX: backend should answer Amity.Response (2)
|
|
21659
22126
|
// API-FIX: pagination should not be indexed on channelSegment (3)
|
|
21660
22127
|
// const { data } = await client.http.get<Amity.Response<Amity.Paged<Amity.MessagePayload>>>(
|
|
21661
|
-
const
|
|
21662
|
-
|
|
21663
|
-
|
|
21664
|
-
} }),
|
|
21665
|
-
});
|
|
22128
|
+
const response = await client.http
|
|
22129
|
+
.get(`/api/v5/messages`, { params })
|
|
22130
|
+
.then(convertResponseFromRaw);
|
|
21666
22131
|
// API-FIX: backend should answer Amity.Response (2)
|
|
21667
22132
|
// const { paging, messages } = unwrapPayload(data)
|
|
21668
|
-
const { paging } =
|
|
21669
|
-
const { messages } =
|
|
22133
|
+
const _a = response.data, { paging } = _a, data = __rest(_a, ["paging"]);
|
|
22134
|
+
const { messages } = data;
|
|
21670
22135
|
const cachedAt = client.cache && Date.now();
|
|
21671
22136
|
if (client.cache) {
|
|
21672
|
-
ingestInCache(
|
|
21673
|
-
const cacheKey = [
|
|
21674
|
-
'message',
|
|
21675
|
-
'query',
|
|
21676
|
-
Object.assign(Object.assign({}, params), { options: Object.assign({}, page) }),
|
|
21677
|
-
];
|
|
22137
|
+
ingestInCache(data, { cachedAt });
|
|
22138
|
+
const cacheKey = ['message', 'query', params];
|
|
21678
22139
|
pushToCache(cacheKey, { messages: messages.map(getResolver('message')), paging });
|
|
21679
22140
|
}
|
|
21680
|
-
fireEvent('
|
|
22141
|
+
fireEvent('local.message.fetched', { messages });
|
|
21681
22142
|
const nextPage = toPage(paging.next);
|
|
21682
22143
|
const prevPage = toPage(paging.previous);
|
|
21683
22144
|
return { data: messages, cachedAt, prevPage, nextPage };
|
|
@@ -21701,8 +22162,8 @@ queryMessages.locally = (query) => {
|
|
|
21701
22162
|
client.log('message/queryMessages.locally', query);
|
|
21702
22163
|
if (!client.cache)
|
|
21703
22164
|
return;
|
|
21704
|
-
const
|
|
21705
|
-
const queryKey = ['message', 'query',
|
|
22165
|
+
const params = convertQueryParams$1(query);
|
|
22166
|
+
const queryKey = ['message', 'query', params];
|
|
21706
22167
|
const { data, cachedAt } = (_a = pullFromCache(queryKey)) !== null && _a !== void 0 ? _a : {};
|
|
21707
22168
|
if (!(data === null || data === void 0 ? void 0 : data.messages.length))
|
|
21708
22169
|
return;
|
|
@@ -21719,13 +22180,20 @@ queryMessages.locally = (query) => {
|
|
|
21719
22180
|
|
|
21720
22181
|
const uuid = () => nativeIdGenerator.v4().toString();
|
|
21721
22182
|
|
|
22183
|
+
// FIXME: temp solution
|
|
22184
|
+
let uniqueId;
|
|
21722
22185
|
/**
|
|
21723
22186
|
* ```js
|
|
21724
|
-
* import { createMessage } from '@amityco/ts-sdk'
|
|
21725
|
-
*
|
|
21726
|
-
*
|
|
21727
|
-
*
|
|
21728
|
-
* }
|
|
22187
|
+
* import { createMessage, createQuery, runQuery } from '@amityco/ts-sdk'
|
|
22188
|
+
*
|
|
22189
|
+
* const query = createQuery(createMessage, {
|
|
22190
|
+
* subChannelId: 'foobar',
|
|
22191
|
+
* data: { text: 'hello world' },
|
|
22192
|
+
* });
|
|
22193
|
+
*
|
|
22194
|
+
* runQuery(query, ({ data: message, loading }) => {
|
|
22195
|
+
* console.log(message);
|
|
22196
|
+
* });
|
|
21729
22197
|
* ```
|
|
21730
22198
|
*
|
|
21731
22199
|
* Creates an {@link Amity.Message}
|
|
@@ -21738,13 +22206,19 @@ const uuid = () => nativeIdGenerator.v4().toString();
|
|
|
21738
22206
|
*/
|
|
21739
22207
|
const createMessage = async (bundle) => {
|
|
21740
22208
|
const client = getActiveClient();
|
|
21741
|
-
client.log('
|
|
21742
|
-
const
|
|
22209
|
+
client.log('message/createMessage', bundle);
|
|
22210
|
+
const referenceId = uniqueId;
|
|
22211
|
+
uniqueId = undefined;
|
|
22212
|
+
const { data } = await client.http
|
|
22213
|
+
.post('/api/v5/messages', Object.assign(Object.assign({}, convertParams(bundle)), { referenceId }))
|
|
22214
|
+
.then(convertResponseFromRaw);
|
|
21743
22215
|
const cachedAt = client.cache && Date.now();
|
|
21744
|
-
if (client.cache)
|
|
22216
|
+
if (client.cache) {
|
|
22217
|
+
dropFromCache(['message', 'get', referenceId]);
|
|
21745
22218
|
ingestInCache(data, { cachedAt });
|
|
22219
|
+
}
|
|
21746
22220
|
const { messages } = data;
|
|
21747
|
-
fireEvent('
|
|
22221
|
+
fireEvent('local.message.created', { messages });
|
|
21748
22222
|
return {
|
|
21749
22223
|
data: messages[0],
|
|
21750
22224
|
cachedAt,
|
|
@@ -21753,7 +22227,8 @@ const createMessage = async (bundle) => {
|
|
|
21753
22227
|
/**
|
|
21754
22228
|
* ```js
|
|
21755
22229
|
* import { createMessage } from '@amityco/ts-sdk'
|
|
21756
|
-
*
|
|
22230
|
+
*
|
|
22231
|
+
* const created = createMessage.optimistically({
|
|
21757
22232
|
* channelId: 'foobar',
|
|
21758
22233
|
* data: { text: 'hello world' }
|
|
21759
22234
|
* })
|
|
@@ -21778,17 +22253,26 @@ createMessage.optimistically = (bundle) => {
|
|
|
21778
22253
|
* Updated to handle client requirement to add messageId while uploading
|
|
21779
22254
|
* message with image. Temporary!
|
|
21780
22255
|
*/
|
|
21781
|
-
const
|
|
21782
|
-
|
|
21783
|
-
|
|
21784
|
-
|
|
21785
|
-
|
|
22256
|
+
const localId = `LOCAL_${uuid()}`;
|
|
22257
|
+
uniqueId = localId;
|
|
22258
|
+
const bundleWithMessageId = Object.assign({ messageId: localId, uniqueId: localId }, bundle);
|
|
22259
|
+
client.log('message/createMessage.optimistically', bundleWithMessageId);
|
|
22260
|
+
const subChannel = pullFromCache(['subChannel', 'get', bundle.subChannelId]);
|
|
22261
|
+
if (subChannel) {
|
|
22262
|
+
upsertInCache(['subChannel', 'get', bundle.subChannelId], Object.assign(Object.assign({}, subChannel.data), { messageCount: subChannel.data.messageCount + 1 }));
|
|
22263
|
+
if (subChannel.data.channelId === subChannel.data.subChannelId) {
|
|
22264
|
+
const channel = pullFromCache(['channel', 'get', subChannel.data.channelId]);
|
|
22265
|
+
if (channel === null || channel === void 0 ? void 0 : channel.data) {
|
|
22266
|
+
upsertInCache(['channel', 'get', subChannel.data.channelId], Object.assign(Object.assign({}, channel.data), { messageCount: ((_a = channel.data.messageCount) !== null && _a !== void 0 ? _a : 0) + 1 }));
|
|
22267
|
+
}
|
|
22268
|
+
}
|
|
22269
|
+
}
|
|
21786
22270
|
// as reused to update created and updated time, which should be the same
|
|
21787
22271
|
const createdTime = new Date().toISOString();
|
|
21788
|
-
const message = Object.assign({
|
|
22272
|
+
const message = Object.assign({ creatorId: client.userId, channelSegment: ((_b = subChannel === null || subChannel === void 0 ? void 0 : subChannel.data.messageCount) !== null && _b !== void 0 ? _b : 0) + 1, childrenNumber: 0, createdAt: createdTime, updatedAt: createdTime }, bundleWithMessageId);
|
|
21789
22273
|
const cachedAt = UNSYNCED_OBJECT_CACHED_AT_VALUE;
|
|
21790
22274
|
pushToCache(['message', 'get', message.messageId], message, { cachedAt });
|
|
21791
|
-
fireEvent('
|
|
22275
|
+
fireEvent('local.message.created', { messages: [message] });
|
|
21792
22276
|
return {
|
|
21793
22277
|
cachedAt,
|
|
21794
22278
|
data: message,
|
|
@@ -21814,13 +22298,15 @@ createMessage.optimistically = (bundle) => {
|
|
|
21814
22298
|
*/
|
|
21815
22299
|
const updateMessage = async (messageId, patch) => {
|
|
21816
22300
|
const client = getActiveClient();
|
|
21817
|
-
client.log('
|
|
21818
|
-
const { data } = await client.http
|
|
22301
|
+
client.log('message/updateMessage', patch);
|
|
22302
|
+
const { data } = await client.http
|
|
22303
|
+
.put(`/api/v5/messages/${encodeURIComponent(messageId)}`, convertParams(patch))
|
|
22304
|
+
.then(convertResponseFromRaw);
|
|
21819
22305
|
const cachedAt = client.cache && Date.now();
|
|
21820
22306
|
if (client.cache)
|
|
21821
22307
|
ingestInCache(data, { cachedAt });
|
|
21822
22308
|
const { messages } = data;
|
|
21823
|
-
fireEvent('
|
|
22309
|
+
fireEvent('local.message.updated', { messages });
|
|
21824
22310
|
return {
|
|
21825
22311
|
data: messages.find(message => message.messageId === messageId),
|
|
21826
22312
|
cachedAt,
|
|
@@ -21829,7 +22315,7 @@ const updateMessage = async (messageId, patch) => {
|
|
|
21829
22315
|
/**
|
|
21830
22316
|
* ```js
|
|
21831
22317
|
* import { updateMessage } from '@amityco/ts-sdk'
|
|
21832
|
-
* const updated = updateMessage.
|
|
22318
|
+
* const updated = updateMessage.optimistically('foobar', {
|
|
21833
22319
|
* data: { text: 'hello world' }
|
|
21834
22320
|
* })
|
|
21835
22321
|
* ```
|
|
@@ -21844,7 +22330,7 @@ const updateMessage = async (messageId, patch) => {
|
|
|
21844
22330
|
*/
|
|
21845
22331
|
updateMessage.optimistically = (messageId, patch) => {
|
|
21846
22332
|
const client = getActiveClient();
|
|
21847
|
-
client.log('
|
|
22333
|
+
client.log('message/updateMessage.optimistically', patch);
|
|
21848
22334
|
if (!client.cache)
|
|
21849
22335
|
return;
|
|
21850
22336
|
const message = pullFromCache(['message', 'get', messageId]);
|
|
@@ -21853,7 +22339,7 @@ updateMessage.optimistically = (messageId, patch) => {
|
|
|
21853
22339
|
const cachedAt = -1;
|
|
21854
22340
|
const updated = Object.assign(Object.assign(Object.assign({}, message.data), patch), { updatedAt: new Date().toISOString() });
|
|
21855
22341
|
upsertInCache(['message', 'get', messageId], updated, { cachedAt });
|
|
21856
|
-
fireEvent('
|
|
22342
|
+
fireEvent('local.message.updated', { messages: [updated] });
|
|
21857
22343
|
return {
|
|
21858
22344
|
data: updated,
|
|
21859
22345
|
cachedAt,
|
|
@@ -21868,7 +22354,7 @@ updateMessage.optimistically = (messageId, patch) => {
|
|
|
21868
22354
|
*
|
|
21869
22355
|
* Delete a {@link Amity.Message}
|
|
21870
22356
|
*
|
|
21871
|
-
* @param
|
|
22357
|
+
* @param messageId the ID of the {@link Amity.Message} to delete
|
|
21872
22358
|
* @return A success boolean if the {@link Amity.Message} was deleted
|
|
21873
22359
|
*
|
|
21874
22360
|
* @category Message API
|
|
@@ -21878,15 +22364,15 @@ const deleteMessage = async (messageId) => {
|
|
|
21878
22364
|
const client = getActiveClient();
|
|
21879
22365
|
client.log('message/deleteMessage', messageId);
|
|
21880
22366
|
// API-FIX: This endpoint has not been implemented yet.
|
|
21881
|
-
await client.http.delete(`/api/
|
|
22367
|
+
await client.http.delete(`/api/v5/messages/${encodeURIComponent(messageId)}`);
|
|
21882
22368
|
const deleted = await getMessage(messageId);
|
|
21883
|
-
fireEvent('
|
|
22369
|
+
fireEvent('local.message.deleted', { messages: [deleted.data] });
|
|
21884
22370
|
return deleted.data;
|
|
21885
22371
|
};
|
|
21886
22372
|
/**
|
|
21887
22373
|
* ```js
|
|
21888
22374
|
* import { deleteMessage } from '@amityco/ts-sdk'
|
|
21889
|
-
* const success = deleteMessage.
|
|
22375
|
+
* const success = deleteMessage.optimistically('foobar')
|
|
21890
22376
|
* ```
|
|
21891
22377
|
*
|
|
21892
22378
|
* Deletes a {@link Amity.Message}
|
|
@@ -21898,14 +22384,14 @@ const deleteMessage = async (messageId) => {
|
|
|
21898
22384
|
*/
|
|
21899
22385
|
deleteMessage.optimistically = (messageId) => {
|
|
21900
22386
|
const client = getActiveClient();
|
|
21901
|
-
client.log('message/deleteMessage', messageId);
|
|
22387
|
+
client.log('message/deleteMessage.optimistically', messageId);
|
|
21902
22388
|
const message = pullFromCache(['message', 'get', messageId]);
|
|
21903
22389
|
if (!message)
|
|
21904
22390
|
return;
|
|
21905
22391
|
const cachedAt = -1;
|
|
21906
22392
|
const deleted = Object.assign(Object.assign({}, message.data), { isDeleted: true, updatedAt: new Date().toISOString() });
|
|
21907
22393
|
upsertInCache(['message', 'get', messageId], deleted, { cachedAt });
|
|
21908
|
-
fireEvent('
|
|
22394
|
+
fireEvent('local.message.deleted', { messages: [deleted] });
|
|
21909
22395
|
return {
|
|
21910
22396
|
data: deleted,
|
|
21911
22397
|
cachedAt,
|
|
@@ -21930,11 +22416,20 @@ const queryPosts = async (query) => {
|
|
|
21930
22416
|
const client = getActiveClient();
|
|
21931
22417
|
client.log('post/queryPosts', query);
|
|
21932
22418
|
const { page = { limit: 10 } } = query, params = __rest(query, ["page"]);
|
|
22419
|
+
const { dataTypes, matchingOnlyParentPost } = params;
|
|
21933
22420
|
// API-FIX: parameters should be querystring. (1)
|
|
21934
22421
|
// API-FIX: backend should answer Amity.Response (2)
|
|
21935
22422
|
// const { data } = await client.http.get<Amity.Response<Amity.PagedResponse<Amity.PostPayload>>>(
|
|
21936
22423
|
const { data } = await client.http.get(`/api/v4/posts`, {
|
|
21937
|
-
params: Object.assign(Object.assign({}, params), {
|
|
22424
|
+
params: Object.assign(Object.assign({}, params), {
|
|
22425
|
+
/*
|
|
22426
|
+
* when creating post like image, file, video BE will create 2 posts
|
|
22427
|
+
* 1. parent post to store text with dataType=text
|
|
22428
|
+
* 2. child post to store dataTypes post data
|
|
22429
|
+
*
|
|
22430
|
+
* By default BE queries only parent post
|
|
22431
|
+
*/
|
|
22432
|
+
matchingOnlyParentPost: matchingOnlyParentPost !== null && matchingOnlyParentPost !== void 0 ? matchingOnlyParentPost : !(dataTypes === null || dataTypes === void 0 ? void 0 : dataTypes.length), options: {
|
|
21938
22433
|
token: toToken(page, 'afterbeforeraw'),
|
|
21939
22434
|
} }),
|
|
21940
22435
|
});
|
|
@@ -22232,12 +22727,13 @@ const queryCommunities = async (query) => {
|
|
|
22232
22727
|
const client = getActiveClient();
|
|
22233
22728
|
client.log('channel/queryCommunities', query);
|
|
22234
22729
|
// safe decapsulation
|
|
22235
|
-
const _a = query !== null && query !== void 0 ? query : {}, { page
|
|
22730
|
+
const _a = query !== null && query !== void 0 ? query : {}, { page } = _a, params = __rest(_a, ["page"]);
|
|
22731
|
+
const _b = params !== null && params !== void 0 ? params : {}, { displayName, membership } = _b, restParams = __rest(_b, ["displayName", "membership"]);
|
|
22236
22732
|
// API-FIX: parameters should be querystring.
|
|
22237
22733
|
// API-FIX: backend doesnt answer Amity.Response
|
|
22238
22734
|
// const { data } = await client.http.get<Amity.Response<Amity.PagedResponse<CommunityPayload>>>(
|
|
22239
22735
|
const { data } = await client.http.get(`/api/v3/communities`, {
|
|
22240
|
-
params: Object.assign(Object.assign({},
|
|
22736
|
+
params: Object.assign(Object.assign({}, restParams), { keyword: displayName, filter: membership, options: {
|
|
22241
22737
|
token: toToken(page, 'skiplimit'),
|
|
22242
22738
|
} }),
|
|
22243
22739
|
});
|
|
@@ -22533,10 +23029,62 @@ const leaveCommunity = async (communityId) => {
|
|
|
22533
23029
|
};
|
|
22534
23030
|
|
|
22535
23031
|
/**
|
|
22536
|
-
*
|
|
22537
|
-
*
|
|
22538
|
-
*
|
|
22539
|
-
*
|
|
23032
|
+
* Attach user object to membership model
|
|
23033
|
+
* - If cache is not enabled, set user to undefined ({..., user: undefined})
|
|
23034
|
+
* - If user object is not found in cache, set user to undefined
|
|
23035
|
+
* ({..., user: undefined})
|
|
23036
|
+
* - If a user is found in the cache, it attaches the user from the cache
|
|
23037
|
+
* ({..., user: { userId, displayName,... }})
|
|
23038
|
+
*
|
|
23039
|
+
* @param member The membership model object
|
|
23040
|
+
* @param userIdProp The user ID prop name of membership object
|
|
23041
|
+
* @param userProp The user prop name of destination object
|
|
23042
|
+
* @returns The membership model object that is already mapped to user
|
|
23043
|
+
* @hidden
|
|
23044
|
+
*/
|
|
23045
|
+
const withUser = (member, userIdProp = 'userId', userProp = 'user') => (Object.assign({ get [userProp]() {
|
|
23046
|
+
var _a;
|
|
23047
|
+
const client = getActiveClient();
|
|
23048
|
+
if (!client.cache)
|
|
23049
|
+
return undefined;
|
|
23050
|
+
const userCache = (_a = pullFromCache(['user', 'get', member[userIdProp]])) === null || _a === void 0 ? void 0 : _a.data;
|
|
23051
|
+
if (!userCache) {
|
|
23052
|
+
return undefined;
|
|
23053
|
+
}
|
|
23054
|
+
return userCache;
|
|
23055
|
+
} }, member));
|
|
23056
|
+
/**
|
|
23057
|
+
* Mapping membership model with user object
|
|
23058
|
+
*
|
|
23059
|
+
* @param members The membership model objects
|
|
23060
|
+
* @param userIdProp The user ID prop name of membership object
|
|
23061
|
+
* @param userProp The user prop name of destination object
|
|
23062
|
+
* @returns The membership model objects that is already
|
|
23063
|
+
* mapped to users
|
|
23064
|
+
* @hidden
|
|
23065
|
+
*/
|
|
23066
|
+
const withUsers = (members, userIdProp = 'userId', userProp = 'user') => members.map(member => withUser(member, userIdProp, userProp));
|
|
23067
|
+
/**
|
|
23068
|
+
* Transform members prop in any payload with attached users members
|
|
23069
|
+
*
|
|
23070
|
+
* @param payload The payload contain members prop
|
|
23071
|
+
* @param memberProp A name of members prop
|
|
23072
|
+
* @param userIdProp The user ID prop name of membership object
|
|
23073
|
+
* @param userProp The user prop name of destination object
|
|
23074
|
+
* @returns The transforming payload
|
|
23075
|
+
* @hidden
|
|
23076
|
+
*/
|
|
23077
|
+
const prepareMembershipPayload = (payload, memberProp, userIdProp = 'userId', userProp = 'user') => {
|
|
23078
|
+
const _a = payload, _b = memberProp, memberPayload = _a[_b], rest = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]);
|
|
23079
|
+
const members = withUsers(memberPayload, userIdProp, userProp);
|
|
23080
|
+
return Object.assign(Object.assign({}, rest), { [memberProp]: members });
|
|
23081
|
+
};
|
|
23082
|
+
|
|
23083
|
+
/**
|
|
23084
|
+
* ```js
|
|
23085
|
+
* import { queryCommunityMembers } from '@amityco/ts-sdk'
|
|
23086
|
+
* const communityMembers = await queryCommunityMembers({ communityId: 'foo' })
|
|
23087
|
+
* ```
|
|
22540
23088
|
*
|
|
22541
23089
|
* Queries a paginable list of {@link Amity.CommunityUser} objects
|
|
22542
23090
|
*
|
|
@@ -22549,17 +23097,27 @@ const leaveCommunity = async (communityId) => {
|
|
|
22549
23097
|
const queryCommunityMembers = async (query) => {
|
|
22550
23098
|
const client = getActiveClient();
|
|
22551
23099
|
client.log('community/queryCommunityMembers', query);
|
|
22552
|
-
const _a = query !== null && query !== void 0 ? query : {}, { page
|
|
22553
|
-
const { data } = await client.http.get(`/api/v3/communities/${communityId}/users`, {
|
|
23100
|
+
const _a = query !== null && query !== void 0 ? query : {}, { page } = _a, params = __rest(_a, ["page"]);
|
|
23101
|
+
const { data: queryPayload } = await client.http.get(`/api/v3/communities/${params.communityId}/users`, {
|
|
22554
23102
|
params: Object.assign(Object.assign({}, params), { options: {
|
|
22555
23103
|
token: toToken(page, 'skiplimit'),
|
|
22556
23104
|
} }),
|
|
22557
23105
|
});
|
|
22558
|
-
const { paging } =
|
|
22559
|
-
const
|
|
23106
|
+
const { paging } = queryPayload, payload = __rest(queryPayload, ["paging"]);
|
|
23107
|
+
const preparedPayload = prepareMembershipPayload(payload, 'communityUsers');
|
|
23108
|
+
const { communityUsers } = preparedPayload;
|
|
22560
23109
|
const cachedAt = client.cache && Date.now();
|
|
22561
23110
|
if (client.cache) {
|
|
22562
|
-
ingestInCache(
|
|
23111
|
+
ingestInCache(preparedPayload, { cachedAt });
|
|
23112
|
+
const cacheKey = [
|
|
23113
|
+
'communityUsers',
|
|
23114
|
+
'query',
|
|
23115
|
+
Object.assign(Object.assign({}, params), { options: Object.assign({}, page) }),
|
|
23116
|
+
];
|
|
23117
|
+
pushToCache(cacheKey, {
|
|
23118
|
+
communityUsers: communityUsers.map(({ communityId, userId }) => getResolver('communityUsers')({ communityId, userId })),
|
|
23119
|
+
paging,
|
|
23120
|
+
});
|
|
22563
23121
|
}
|
|
22564
23122
|
const nextPage = toPage(paging.next);
|
|
22565
23123
|
const prevPage = toPage(paging.previous);
|
|
@@ -22579,9 +23137,29 @@ const queryCommunityMembers = async (query) => {
|
|
|
22579
23137
|
* @category Post API
|
|
22580
23138
|
*/
|
|
22581
23139
|
queryCommunityMembers.locally = (query) => {
|
|
23140
|
+
var _a, _b;
|
|
22582
23141
|
const client = getActiveClient();
|
|
22583
23142
|
client.log('community/queryCommunityMembers', query);
|
|
22584
|
-
|
|
23143
|
+
if (!client.cache)
|
|
23144
|
+
return;
|
|
23145
|
+
const _c = query !== null && query !== void 0 ? query : {}, { page } = _c, params = __rest(_c, ["page"]);
|
|
23146
|
+
const cacheKey = [
|
|
23147
|
+
'communityUsers',
|
|
23148
|
+
'query',
|
|
23149
|
+
Object.assign(Object.assign({}, params), { options: Object.assign({}, page) }),
|
|
23150
|
+
];
|
|
23151
|
+
const { data, cachedAt } = (_a = pullFromCache(cacheKey)) !== null && _a !== void 0 ? _a : {};
|
|
23152
|
+
if (!(data === null || data === void 0 ? void 0 : data.communityUsers.length))
|
|
23153
|
+
return;
|
|
23154
|
+
const communityUsers = data.communityUsers
|
|
23155
|
+
.map(key => pullFromCache(['communityUsers', 'get', key]))
|
|
23156
|
+
.filter(Boolean)
|
|
23157
|
+
.map(({ data }) => data);
|
|
23158
|
+
const prevPage = toPage(data === null || data === void 0 ? void 0 : data.paging.previous);
|
|
23159
|
+
const nextPage = toPage(data === null || data === void 0 ? void 0 : data.paging.next);
|
|
23160
|
+
return communityUsers.length === ((_b = data === null || data === void 0 ? void 0 : data.communityUsers) === null || _b === void 0 ? void 0 : _b.length)
|
|
23161
|
+
? { data: communityUsers, cachedAt, prevPage, nextPage }
|
|
23162
|
+
: undefined;
|
|
22585
23163
|
};
|
|
22586
23164
|
|
|
22587
23165
|
/**
|
|
@@ -23290,38 +23868,6 @@ const preparePayloadForCache = (event, payload) => {
|
|
|
23290
23868
|
}
|
|
23291
23869
|
};
|
|
23292
23870
|
|
|
23293
|
-
/** @hidden */
|
|
23294
|
-
/*
|
|
23295
|
-
* @param message payload from http request without myReactions
|
|
23296
|
-
* add myReactions to http response if the event was a reaction event
|
|
23297
|
-
*/
|
|
23298
|
-
const prepareMessagePayloadForCache = (payload) => {
|
|
23299
|
-
const client = getActiveClient();
|
|
23300
|
-
const cached = pullFromCache(['message', 'get', payload.messageId]);
|
|
23301
|
-
// '[]' in cases where the new reaction is the first one
|
|
23302
|
-
const myReactions = (cached === null || cached === void 0 ? void 0 : cached.data.myReactions) || [];
|
|
23303
|
-
// add myReactions to the payload
|
|
23304
|
-
Object.assign(payload, { myReactions });
|
|
23305
|
-
// check if there are any updates to the reactions
|
|
23306
|
-
const latestReaction = payload === null || payload === void 0 ? void 0 : payload.latestReaction;
|
|
23307
|
-
const isLatestReactionMine = latestReaction && latestReaction.userId === client.userId;
|
|
23308
|
-
if (!isLatestReactionMine) {
|
|
23309
|
-
return;
|
|
23310
|
-
}
|
|
23311
|
-
// new reaction added
|
|
23312
|
-
if (latestReaction.eventName === 'add' && !myReactions.includes(latestReaction.reactionName)) {
|
|
23313
|
-
Object.assign(payload, {
|
|
23314
|
-
myReactions: [...myReactions, latestReaction.reactionName],
|
|
23315
|
-
});
|
|
23316
|
-
}
|
|
23317
|
-
// existing reaction removed
|
|
23318
|
-
if (latestReaction.eventName === 'remove' && myReactions.includes(latestReaction.reactionName)) {
|
|
23319
|
-
Object.assign(payload, {
|
|
23320
|
-
myReactions: myReactions.filter(x => x !== latestReaction.reactionName),
|
|
23321
|
-
});
|
|
23322
|
-
}
|
|
23323
|
-
};
|
|
23324
|
-
|
|
23325
23871
|
/**
|
|
23326
23872
|
* ```js
|
|
23327
23873
|
* import { addReaction } from '@amityco/ts-sdk'
|
|
@@ -23349,6 +23895,7 @@ const addReaction = async (referenceType, referenceId, reactionName) => {
|
|
|
23349
23895
|
referenceId,
|
|
23350
23896
|
referenceType,
|
|
23351
23897
|
reactionName,
|
|
23898
|
+
referenceVersion: referenceType === 'message' ? 5 : undefined,
|
|
23352
23899
|
});
|
|
23353
23900
|
if (client.cache) {
|
|
23354
23901
|
const model = pullFromCache([
|
|
@@ -23425,7 +23972,12 @@ const removeReaction = async (referenceType, referenceId, reactionName) => {
|
|
|
23425
23972
|
reactionName,
|
|
23426
23973
|
});
|
|
23427
23974
|
await client.http.delete(`/api/v2/reactions`, {
|
|
23428
|
-
data: {
|
|
23975
|
+
data: {
|
|
23976
|
+
referenceId,
|
|
23977
|
+
referenceType,
|
|
23978
|
+
reactionName,
|
|
23979
|
+
referenceVersion: referenceType === 'message' ? 5 : undefined,
|
|
23980
|
+
},
|
|
23429
23981
|
});
|
|
23430
23982
|
if (client.cache) {
|
|
23431
23983
|
const model = pullFromCache([
|
|
@@ -23938,8 +24490,9 @@ const onReactionAdded = (referenceType, referenceId, callback) => {
|
|
|
23938
24490
|
*/
|
|
23939
24491
|
if (referenceType === 'message') {
|
|
23940
24492
|
const client = getActiveClient();
|
|
23941
|
-
const filter = (
|
|
24493
|
+
const filter = (rawPayload) => {
|
|
23942
24494
|
var _a;
|
|
24495
|
+
const payload = prepareMessagePayload(rawPayload);
|
|
23943
24496
|
const cached = pullFromCache([
|
|
23944
24497
|
'message',
|
|
23945
24498
|
'get',
|
|
@@ -23951,7 +24504,7 @@ const onReactionAdded = (referenceType, referenceId, callback) => {
|
|
|
23951
24504
|
callbackWrapper(payload.messages[0]);
|
|
23952
24505
|
}
|
|
23953
24506
|
};
|
|
23954
|
-
return createEventSubscriber(client, 'reaction/onReactionAdded', '
|
|
24507
|
+
return createEventSubscriber(client, 'reaction/onReactionAdded', 'message.updated', filter);
|
|
23955
24508
|
}
|
|
23956
24509
|
if (referenceType === 'post') {
|
|
23957
24510
|
return onPostReactionAdded(callbackWrapper);
|
|
@@ -23984,8 +24537,9 @@ const onReactionRemoved = (referenceType, referenceId, callback) => {
|
|
|
23984
24537
|
};
|
|
23985
24538
|
if (referenceType === 'message') {
|
|
23986
24539
|
const client = getActiveClient();
|
|
23987
|
-
const filter = (
|
|
24540
|
+
const filter = (rawPayload) => {
|
|
23988
24541
|
var _a;
|
|
24542
|
+
const payload = prepareMessagePayload(rawPayload);
|
|
23989
24543
|
const cached = pullFromCache([
|
|
23990
24544
|
'message',
|
|
23991
24545
|
'get',
|
|
@@ -23997,7 +24551,7 @@ const onReactionRemoved = (referenceType, referenceId, callback) => {
|
|
|
23997
24551
|
callbackWrapper(payload.messages[0]);
|
|
23998
24552
|
}
|
|
23999
24553
|
};
|
|
24000
|
-
return createEventSubscriber(client, 'reaction/onReactionRemoved', '
|
|
24554
|
+
return createEventSubscriber(client, 'reaction/onReactionRemoved', 'message.updated', filter);
|
|
24001
24555
|
}
|
|
24002
24556
|
if (referenceType === 'post') {
|
|
24003
24557
|
return onPostReactionRemoved(callbackWrapper);
|
|
@@ -24030,7 +24584,8 @@ const onReactorAdded = (referenceType, referenceId, callback) => {
|
|
|
24030
24584
|
}
|
|
24031
24585
|
};
|
|
24032
24586
|
if (referenceType === 'message') {
|
|
24033
|
-
const filter = (
|
|
24587
|
+
const filter = (rawPayload) => {
|
|
24588
|
+
const payload = prepareMessagePayload(rawPayload);
|
|
24034
24589
|
if (!payload.messages[0].latestReaction)
|
|
24035
24590
|
return;
|
|
24036
24591
|
const _a = payload.messages[0].latestReaction, { eventName, referenceId, referenceType, userDisplayName } = _a, reactor = __rest(_a, ["eventName", "referenceId", "referenceType", "userDisplayName"]);
|
|
@@ -24040,7 +24595,7 @@ const onReactorAdded = (referenceType, referenceId, callback) => {
|
|
|
24040
24595
|
ingestInCache({ reactions: [reactor] });
|
|
24041
24596
|
callbackWrapper('message', payload.messages[0].messageId, reactor);
|
|
24042
24597
|
};
|
|
24043
|
-
return createEventSubscriber(client, 'reaction/onReactorAdded', '
|
|
24598
|
+
return createEventSubscriber(client, 'reaction/onReactorAdded', 'message.updated', filter);
|
|
24044
24599
|
}
|
|
24045
24600
|
if (referenceType === 'post') {
|
|
24046
24601
|
const filter = (payload) => {
|
|
@@ -24085,7 +24640,8 @@ const onReactorRemoved = (referenceType, referenceId, callback) => {
|
|
|
24085
24640
|
}
|
|
24086
24641
|
};
|
|
24087
24642
|
if (referenceType === 'message') {
|
|
24088
|
-
const filter = (
|
|
24643
|
+
const filter = (rawPayload) => {
|
|
24644
|
+
const payload = prepareMessagePayload(rawPayload);
|
|
24089
24645
|
if (!payload.messages[0].latestReaction)
|
|
24090
24646
|
return;
|
|
24091
24647
|
const _a = payload.messages[0].latestReaction, { eventName, referenceId, referenceType } = _a, reactor = __rest(_a, ["eventName", "referenceId", "referenceType"]);
|
|
@@ -24095,7 +24651,7 @@ const onReactorRemoved = (referenceType, referenceId, callback) => {
|
|
|
24095
24651
|
ingestInCache({ reactions: [reactor] });
|
|
24096
24652
|
callbackWrapper('message', payload.messages[0].messageId, reactor);
|
|
24097
24653
|
};
|
|
24098
|
-
return createEventSubscriber(client, 'reaction/onReactorRemoved', '
|
|
24654
|
+
return createEventSubscriber(client, 'reaction/onReactorRemoved', 'message.updated', filter);
|
|
24099
24655
|
}
|
|
24100
24656
|
if (referenceType === 'post') {
|
|
24101
24657
|
const filter = (payload) => {
|
|
@@ -24165,7 +24721,7 @@ const liveReactions = (params, callback, config) => {
|
|
|
24165
24721
|
error: data.error,
|
|
24166
24722
|
});
|
|
24167
24723
|
};
|
|
24168
|
-
const realtimeRouter = (
|
|
24724
|
+
const realtimeRouter = (action) => (reaction) => {
|
|
24169
24725
|
var _a;
|
|
24170
24726
|
const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
|
|
24171
24727
|
if (!collection)
|
|
@@ -24202,7 +24758,7 @@ const liveReactions = (params, callback, config) => {
|
|
|
24202
24758
|
responder(data);
|
|
24203
24759
|
}, queryOptions(policy));
|
|
24204
24760
|
};
|
|
24205
|
-
disposers.push(onReactorAdded(queryParams.referenceType, queryParams.referenceId,
|
|
24761
|
+
disposers.push(onReactorAdded(queryParams.referenceType, queryParams.referenceId, realtimeRouter('onAdded')), onReactorRemoved(queryParams.referenceType, queryParams.referenceId, realtimeRouter('onRemoved')));
|
|
24206
24762
|
onFetch();
|
|
24207
24763
|
return () => {
|
|
24208
24764
|
log(`liveReactions(tmpid: ${timestamp}) > dispose`);
|
|
@@ -24249,16 +24805,26 @@ var REFERENCE_TYPES = {
|
|
|
24249
24805
|
const createReport = async (referenceType, referenceId) => {
|
|
24250
24806
|
const client = getActiveClient();
|
|
24251
24807
|
client.log('report/createReport', { referenceType, referenceId });
|
|
24252
|
-
const
|
|
24253
|
-
|
|
24254
|
-
|
|
24255
|
-
|
|
24808
|
+
const getAPIUrl = () => {
|
|
24809
|
+
if (referenceType === 'user') {
|
|
24810
|
+
return `/api/v4/me/flags/${encodeURIComponent(referenceId)}`;
|
|
24811
|
+
}
|
|
24812
|
+
if (referenceType === 'message') {
|
|
24813
|
+
return `/api/v5/messages/${encodeURIComponent(referenceId)}/flags`;
|
|
24814
|
+
}
|
|
24815
|
+
const { domainName } = REFERENCE_TYPES[referenceType];
|
|
24816
|
+
return `/api/v3/${domainName}/${encodeURIComponent(referenceId)}/flag`;
|
|
24817
|
+
};
|
|
24818
|
+
const { data } = await client.http.post(getAPIUrl());
|
|
24256
24819
|
if (client.cache)
|
|
24257
|
-
ingestInCache(data);
|
|
24258
|
-
|
|
24259
|
-
|
|
24260
|
-
|
|
24261
|
-
|
|
24820
|
+
ingestInCache(referenceType === 'message' ? prepareMessagePayload(data) : data);
|
|
24821
|
+
if (referenceType === 'message') {
|
|
24822
|
+
fireEvent('message.flagged', data);
|
|
24823
|
+
}
|
|
24824
|
+
else {
|
|
24825
|
+
// @ts-ignore
|
|
24826
|
+
fireEvent(`${referenceType}.flagged`, data);
|
|
24827
|
+
}
|
|
24262
24828
|
return !!data;
|
|
24263
24829
|
};
|
|
24264
24830
|
|
|
@@ -24278,16 +24844,26 @@ const createReport = async (referenceType, referenceId) => {
|
|
|
24278
24844
|
const deleteReport = async (referenceType, referenceId) => {
|
|
24279
24845
|
const client = getActiveClient();
|
|
24280
24846
|
client.log('report/deleteReport', { referenceType, referenceId });
|
|
24281
|
-
const
|
|
24282
|
-
|
|
24283
|
-
|
|
24284
|
-
|
|
24847
|
+
const getAPIUrl = () => {
|
|
24848
|
+
if (referenceType === 'user') {
|
|
24849
|
+
return `/api/v4/me/flags/${encodeURIComponent(referenceId)}`;
|
|
24850
|
+
}
|
|
24851
|
+
if (referenceType === 'message') {
|
|
24852
|
+
return `/api/v5/messages/${encodeURIComponent(referenceId)}/flags`;
|
|
24853
|
+
}
|
|
24854
|
+
const { domainName } = REFERENCE_TYPES[referenceType];
|
|
24855
|
+
return `/api/v3/${domainName}/${encodeURIComponent(referenceId)}/unflag`;
|
|
24856
|
+
};
|
|
24857
|
+
const { data } = await client.http.delete(getAPIUrl());
|
|
24285
24858
|
if (client.cache)
|
|
24286
|
-
ingestInCache(data);
|
|
24287
|
-
|
|
24288
|
-
|
|
24289
|
-
|
|
24290
|
-
|
|
24859
|
+
ingestInCache(referenceType === 'message' ? prepareMessagePayload(data) : data);
|
|
24860
|
+
if (referenceType === 'message') {
|
|
24861
|
+
fireEvent('message.unflagged', data);
|
|
24862
|
+
}
|
|
24863
|
+
else {
|
|
24864
|
+
// @ts-ignore
|
|
24865
|
+
fireEvent(`${referenceType}.unflagged`, data);
|
|
24866
|
+
}
|
|
24291
24867
|
return !!data;
|
|
24292
24868
|
};
|
|
24293
24869
|
|
|
@@ -24309,26 +24885,45 @@ const isReportedByMe = async (referenceType, referenceId) => {
|
|
|
24309
24885
|
const client = getActiveClient();
|
|
24310
24886
|
client.log('report/isReportedByMe', { referenceType, referenceId });
|
|
24311
24887
|
const { eventName, primaryKey } = REFERENCE_TYPES[referenceType];
|
|
24888
|
+
if (referenceType === 'message') {
|
|
24889
|
+
const { data } = await client.http.get(`/api/v5/messages/${encodeURIComponent(referenceId)}/flags`);
|
|
24890
|
+
return data.result;
|
|
24891
|
+
}
|
|
24312
24892
|
const { result, isFlagByMe } = (_a = (await synchronousWSCall(client, eventName, {
|
|
24313
24893
|
[primaryKey]: referenceId,
|
|
24314
24894
|
}))) !== null && _a !== void 0 ? _a : {};
|
|
24315
24895
|
return (_b = result !== null && result !== void 0 ? result : isFlagByMe) !== null && _b !== void 0 ? _b : false;
|
|
24316
24896
|
};
|
|
24317
24897
|
|
|
24318
|
-
|
|
24898
|
+
function convertFromRaw$1(_a, params) {
|
|
24899
|
+
var { _id } = _a, rest = __rest(_a, ["_id"]);
|
|
24900
|
+
return Object.assign(Object.assign(Object.assign({}, rest), params), { defaultSubChannelId: _id });
|
|
24901
|
+
}
|
|
24902
|
+
function getSegmentParams(rawChannel) {
|
|
24903
|
+
let cached;
|
|
24904
|
+
try {
|
|
24905
|
+
cached = pullFromCache(['channel', 'get', rawChannel.channelId]);
|
|
24906
|
+
// eslint-disable-next-line no-empty
|
|
24907
|
+
}
|
|
24908
|
+
catch (_a) { }
|
|
24909
|
+
if (cached) {
|
|
24910
|
+
return {
|
|
24911
|
+
defaultSubChannelHasUnreadMention: cached.data.defaultSubChannelHasUnreadMention,
|
|
24912
|
+
defaultSubChannelUnreadCount: cached.data.defaultSubChannelUnreadCount,
|
|
24913
|
+
localReadToSegment: cached.data.localReadToSegment,
|
|
24914
|
+
localLastMentionSegment: cached.data.localLastMentionSegment,
|
|
24915
|
+
};
|
|
24916
|
+
}
|
|
24917
|
+
return {
|
|
24918
|
+
defaultSubChannelHasUnreadMention: false,
|
|
24919
|
+
defaultSubChannelUnreadCount: 0,
|
|
24920
|
+
// @ts-ignore
|
|
24921
|
+
localReadToSegment: rawChannel.messageCount > 0 ? rawChannel.messageCount - 1 : 0,
|
|
24922
|
+
localLastMentionSegment: 0,
|
|
24923
|
+
};
|
|
24924
|
+
}
|
|
24319
24925
|
const prepareChannelPayload = (payload) => {
|
|
24320
|
-
const
|
|
24321
|
-
const channels = payload.channels.map(channel => {
|
|
24322
|
-
const channelUser = payload.channelUsers.find(channelUser => channelUser.userId === client.userId && channel.channelId === channelUser.channelId);
|
|
24323
|
-
const hasMention = channelUser
|
|
24324
|
-
? channelUser.readToSegment < channelUser.lastMentionedSegment
|
|
24325
|
-
: false;
|
|
24326
|
-
const unreadCount = channelUser && channelUser.membership === 'member' && channel.messageCount
|
|
24327
|
-
? Math.max(0, channel.messageCount - channelUser.readToSegment)
|
|
24328
|
-
: 0;
|
|
24329
|
-
return Object.assign(Object.assign({}, channel), { hasMention,
|
|
24330
|
-
unreadCount });
|
|
24331
|
-
});
|
|
24926
|
+
const channels = payload.channels.map(channel => convertFromRaw$1(channel, getSegmentParams(channel)));
|
|
24332
24927
|
return Object.assign(Object.assign({}, payload), { channels });
|
|
24333
24928
|
};
|
|
24334
24929
|
|
|
@@ -24464,10 +25059,10 @@ getChannels.locally = (channelIds) => {
|
|
|
24464
25059
|
const cached = channelIds
|
|
24465
25060
|
.map(channelId => pullFromCache(['channel', 'get', channelId]))
|
|
24466
25061
|
.filter(Boolean);
|
|
24467
|
-
const channels = cached.map(({ data }) => data);
|
|
24468
|
-
const oldest = (_a = cached.sort((a, b) => (a.cachedAt < b.cachedAt ? -1 : 1))) === null || _a === void 0 ? void 0 : _a[0];
|
|
24469
25062
|
if ((cached === null || cached === void 0 ? void 0 : cached.length) < channelIds.length)
|
|
24470
25063
|
return;
|
|
25064
|
+
const channels = cached.map(({ data }) => data);
|
|
25065
|
+
const oldest = (_a = cached.sort((a, b) => (a.cachedAt < b.cachedAt ? -1 : 1))) === null || _a === void 0 ? void 0 : _a[0];
|
|
24471
25066
|
return {
|
|
24472
25067
|
data: channels,
|
|
24473
25068
|
cachedAt: oldest.cachedAt,
|
|
@@ -24542,7 +25137,7 @@ getChannel.locally = (channelId) => {
|
|
|
24542
25137
|
/**
|
|
24543
25138
|
* ```js
|
|
24544
25139
|
* import { createChannel } from '@amityco/ts-sdk'
|
|
24545
|
-
* const created = await createChannel({
|
|
25140
|
+
* const created = await createChannel({ displayName: 'foobar' })
|
|
24546
25141
|
* ```
|
|
24547
25142
|
*
|
|
24548
25143
|
* Creates an {@link Amity.Channel}
|
|
@@ -24694,18 +25289,19 @@ const leaveChannel = async (channelId) => {
|
|
|
24694
25289
|
const queryChannelMembers = async (query) => {
|
|
24695
25290
|
const client = getActiveClient();
|
|
24696
25291
|
client.log('channel/queryChannelMembers', query);
|
|
24697
|
-
const _a = query !== null && query !== void 0 ? query : {}, { page
|
|
25292
|
+
const _a = query !== null && query !== void 0 ? query : {}, { page } = _a, params = __rest(_a, ["page"]);
|
|
24698
25293
|
const { data: queryPayload } = await client.http.get(`/api/v4/channels/${encodeURIComponent(params.channelId)}/users`, {
|
|
24699
25294
|
params: Object.assign(Object.assign({}, params), { options: {
|
|
24700
25295
|
token: toToken(page, 'skiplimit'),
|
|
24701
25296
|
} }),
|
|
24702
25297
|
});
|
|
24703
25298
|
const { paging } = queryPayload, payload = __rest(queryPayload, ["paging"]);
|
|
24704
|
-
const
|
|
24705
|
-
const
|
|
25299
|
+
const channelPayload = prepareChannelPayload(payload);
|
|
25300
|
+
const preparedPayload = prepareMembershipPayload(channelPayload, 'channelUsers');
|
|
25301
|
+
const { channelUsers } = preparedPayload;
|
|
24706
25302
|
const cachedAt = client.cache && Date.now();
|
|
24707
25303
|
if (client.cache) {
|
|
24708
|
-
ingestInCache(
|
|
25304
|
+
ingestInCache(preparedPayload);
|
|
24709
25305
|
const cacheKey = [
|
|
24710
25306
|
'channelUsers',
|
|
24711
25307
|
'query',
|
|
@@ -24718,7 +25314,12 @@ const queryChannelMembers = async (query) => {
|
|
|
24718
25314
|
}
|
|
24719
25315
|
const nextPage = toPage(paging.next);
|
|
24720
25316
|
const prevPage = toPage(paging.previous);
|
|
24721
|
-
return {
|
|
25317
|
+
return {
|
|
25318
|
+
data: channelUsers,
|
|
25319
|
+
cachedAt,
|
|
25320
|
+
nextPage,
|
|
25321
|
+
prevPage,
|
|
25322
|
+
};
|
|
24722
25323
|
};
|
|
24723
25324
|
/**
|
|
24724
25325
|
* ```js
|
|
@@ -24739,7 +25340,7 @@ queryChannelMembers.locally = (query) => {
|
|
|
24739
25340
|
client.log('channel/queryChannelMembers.locally', query);
|
|
24740
25341
|
if (!client.cache)
|
|
24741
25342
|
return;
|
|
24742
|
-
const { page
|
|
25343
|
+
const { page } = query, params = __rest(query, ["page"]);
|
|
24743
25344
|
const cacheKey = [
|
|
24744
25345
|
'channelUsers',
|
|
24745
25346
|
'query',
|
|
@@ -24785,7 +25386,7 @@ const addChannelMembers = async (channelId, userIds) => {
|
|
|
24785
25386
|
/*
|
|
24786
25387
|
* should fire event for each user that was added
|
|
24787
25388
|
*/
|
|
24788
|
-
userIds.forEach(id => fireEvent('
|
|
25389
|
+
userIds.forEach(id => fireEvent('channel.membersAdded', Object.assign(Object.assign({}, payload), { channelUsers: data.channelUsers.filter(u => u.userId === id) })));
|
|
24789
25390
|
const { channelUsers } = data;
|
|
24790
25391
|
return !!channelUsers.find(channelUser => channelUser.channelId === channelId && channelUser.membership === 'member');
|
|
24791
25392
|
};
|
|
@@ -24812,7 +25413,10 @@ const removeChannelMembers = async (channelId, userIds) => {
|
|
|
24812
25413
|
const data = prepareChannelPayload(payload);
|
|
24813
25414
|
if (client.cache)
|
|
24814
25415
|
ingestInCache(data);
|
|
24815
|
-
|
|
25416
|
+
/*
|
|
25417
|
+
* should fire event for each user that was removed
|
|
25418
|
+
*/
|
|
25419
|
+
userIds.forEach(id => fireEvent('channel.membersRemoved', Object.assign(Object.assign({}, payload), { channelUsers: data.channelUsers.filter(u => u.userId === id) })));
|
|
24816
25420
|
const { channelUsers } = data;
|
|
24817
25421
|
return !!channelUsers.find(channelUser => channelUser.channelId === channelId && channelUser.membership !== 'member');
|
|
24818
25422
|
};
|
|
@@ -24891,11 +25495,12 @@ const banChannelMembers = async (channelId, userIds) => {
|
|
|
24891
25495
|
const { data: payload } = await client.http.put(`/api/v3/channels/${channelId}/users/ban`, {
|
|
24892
25496
|
userIds,
|
|
24893
25497
|
});
|
|
24894
|
-
const
|
|
25498
|
+
const channelPayload = prepareChannelPayload(payload);
|
|
25499
|
+
const preparedPayload = prepareMembershipPayload(channelPayload, 'channelUsers');
|
|
25500
|
+
const { channelUsers } = preparedPayload;
|
|
24895
25501
|
const cachedAt = client.cache && Date.now();
|
|
24896
25502
|
if (client.cache)
|
|
24897
|
-
ingestInCache(
|
|
24898
|
-
const { channelUsers } = data;
|
|
25503
|
+
ingestInCache(preparedPayload, { cachedAt });
|
|
24899
25504
|
return {
|
|
24900
25505
|
data: channelUsers === null || channelUsers === void 0 ? void 0 : channelUsers.filter(user => user.membership === 'banned'),
|
|
24901
25506
|
cachedAt,
|
|
@@ -24922,17 +25527,92 @@ const unbanChannelMembers = async (channelId, userIds) => {
|
|
|
24922
25527
|
const { data: payload } = await client.http.put(`/api/v3/channels/${encodeURIComponent(channelId)}/users/unban`, {
|
|
24923
25528
|
userIds,
|
|
24924
25529
|
});
|
|
24925
|
-
const
|
|
25530
|
+
const channelPayload = prepareChannelPayload(payload);
|
|
25531
|
+
const preparedPayload = prepareMembershipPayload(channelPayload, 'channelUsers');
|
|
24926
25532
|
const cachedAt = client.cache && Date.now();
|
|
24927
25533
|
if (client.cache)
|
|
24928
|
-
ingestInCache(
|
|
24929
|
-
const { channelUsers } =
|
|
25534
|
+
ingestInCache(preparedPayload, { cachedAt });
|
|
25535
|
+
const { channelUsers } = preparedPayload;
|
|
24930
25536
|
return {
|
|
24931
25537
|
data: channelUsers === null || channelUsers === void 0 ? void 0 : channelUsers.filter(user => user.membership === 'member'),
|
|
24932
25538
|
cachedAt,
|
|
24933
25539
|
};
|
|
24934
25540
|
};
|
|
24935
25541
|
|
|
25542
|
+
/**
|
|
25543
|
+
* ```js
|
|
25544
|
+
* import { startReading } from '@amityco/ts-sdk'
|
|
25545
|
+
* const success = await startReading('foo')
|
|
25546
|
+
* ```
|
|
25547
|
+
*
|
|
25548
|
+
* Mark all messages as read and start reading message inside channel
|
|
25549
|
+
*
|
|
25550
|
+
* @param {string} subChannelId - The channel ID to start reading.
|
|
25551
|
+
* @param {string} userId - The user ID of channel member.
|
|
25552
|
+
* @return {Promise<boolean>>} A success boolean if the channel reading of the sub channel had begun.
|
|
25553
|
+
*
|
|
25554
|
+
* @category Message API
|
|
25555
|
+
* @async
|
|
25556
|
+
*/
|
|
25557
|
+
const startReading = async (subChannelId, userId = getActiveClient().userId) => {
|
|
25558
|
+
const client = getActiveClient();
|
|
25559
|
+
client.log('message/startReading', { subChannelId });
|
|
25560
|
+
const subChannel = pullFromCache(['subChannel', 'get', subChannelId]);
|
|
25561
|
+
if (!subChannel) {
|
|
25562
|
+
return;
|
|
25563
|
+
}
|
|
25564
|
+
const { channelId } = subChannel.data;
|
|
25565
|
+
const channel = pullFromCache(['channel', 'get', channelId]);
|
|
25566
|
+
if (!channel || channel.data.defaultSubChannelId !== subChannelId) {
|
|
25567
|
+
return;
|
|
25568
|
+
}
|
|
25569
|
+
const update = Object.assign(Object.assign({}, channel.data), {
|
|
25570
|
+
// @ts-ignore
|
|
25571
|
+
localReading: true, defaultSubChannelUnreadCount: 0, defaultSubChannelHasUnreadMention: false });
|
|
25572
|
+
pushToCache(['channel', 'get', channelId], update);
|
|
25573
|
+
client.emitter.emit('local.channel.updated', { channels: [update] });
|
|
25574
|
+
const result = await synchronousWSCall(client, 'channel.startReading', {
|
|
25575
|
+
channelId,
|
|
25576
|
+
});
|
|
25577
|
+
return !!result;
|
|
25578
|
+
};
|
|
25579
|
+
|
|
25580
|
+
/**
|
|
25581
|
+
* ```js
|
|
25582
|
+
* import { stopReading } from '@amityco/ts-sdk'
|
|
25583
|
+
* const success = await stopReading('foo')
|
|
25584
|
+
* ```
|
|
25585
|
+
*
|
|
25586
|
+
* Stop reading message inside channel
|
|
25587
|
+
*
|
|
25588
|
+
* @param {string} subChannelId - The channel ID to stop reading.
|
|
25589
|
+
* @return {Promise<boolean>>} A success boolean if the sub channel reading was halted.
|
|
25590
|
+
*
|
|
25591
|
+
* @category Message API
|
|
25592
|
+
* @async
|
|
25593
|
+
*/
|
|
25594
|
+
const stopReading = async (subChannelId) => {
|
|
25595
|
+
const client = getActiveClient();
|
|
25596
|
+
client.log('message/stopReading', { subChannelId });
|
|
25597
|
+
const subChannel = pullFromCache(['subChannel', 'get', subChannelId]);
|
|
25598
|
+
if (!subChannel) {
|
|
25599
|
+
return;
|
|
25600
|
+
}
|
|
25601
|
+
const { channelId } = subChannel.data;
|
|
25602
|
+
const channel = pullFromCache(['channel', 'get', channelId]);
|
|
25603
|
+
if (!channel || channel.data.defaultSubChannelId !== subChannelId) {
|
|
25604
|
+
return;
|
|
25605
|
+
}
|
|
25606
|
+
const update = Object.assign(Object.assign({}, channel.data), {
|
|
25607
|
+
// @ts-ignore
|
|
25608
|
+
localReading: false });
|
|
25609
|
+
pushToCache(['channel', 'get', channelId], update);
|
|
25610
|
+
const result = await synchronousWSCall(client, 'channel.stopReading', {
|
|
25611
|
+
channelId,
|
|
25612
|
+
});
|
|
25613
|
+
return !!result;
|
|
25614
|
+
};
|
|
25615
|
+
|
|
24936
25616
|
/**
|
|
24937
25617
|
* ```js
|
|
24938
25618
|
* import { onChannelCreated } from '@amityco/ts-sdk'
|
|
@@ -24955,7 +25635,7 @@ const onChannelCreated = (callback) => {
|
|
|
24955
25635
|
ingestInCache(data);
|
|
24956
25636
|
callback(data.channels[0]);
|
|
24957
25637
|
};
|
|
24958
|
-
return createEventSubscriber(client, 'onChannelCreated', '
|
|
25638
|
+
return createEventSubscriber(client, 'onChannelCreated', 'channel.created', filter);
|
|
24959
25639
|
};
|
|
24960
25640
|
|
|
24961
25641
|
/**
|
|
@@ -24980,7 +25660,13 @@ const onChannelUpdated = (callback) => {
|
|
|
24980
25660
|
ingestInCache(data);
|
|
24981
25661
|
callback(data.channels[0]);
|
|
24982
25662
|
};
|
|
24983
|
-
|
|
25663
|
+
const disposers = [
|
|
25664
|
+
createEventSubscriber(client, 'onChannelUpdated', 'channel.updated', filter),
|
|
25665
|
+
createEventSubscriber(client, 'local.channel.updated', 'local.channel.updated', payload => callback(payload.channels[0])),
|
|
25666
|
+
];
|
|
25667
|
+
return () => {
|
|
25668
|
+
disposers.forEach(fn => fn());
|
|
25669
|
+
};
|
|
24984
25670
|
};
|
|
24985
25671
|
|
|
24986
25672
|
/**
|
|
@@ -25005,13 +25691,13 @@ const onChannelDeleted = (callback) => {
|
|
|
25005
25691
|
ingestInCache(data);
|
|
25006
25692
|
callback(data.channels[0]);
|
|
25007
25693
|
};
|
|
25008
|
-
return createEventSubscriber(client, 'onChannelDeleted', '
|
|
25694
|
+
return createEventSubscriber(client, 'onChannelDeleted', 'channel.deleted', filter);
|
|
25009
25695
|
};
|
|
25010
25696
|
|
|
25011
25697
|
/**
|
|
25012
25698
|
* ```js
|
|
25013
25699
|
* import { onChannelJoined } from '@amityco/ts-sdk'
|
|
25014
|
-
* const dispose = onChannelJoined(channel => {
|
|
25700
|
+
* const dispose = onChannelJoined((channel, member) => {
|
|
25015
25701
|
* // ...
|
|
25016
25702
|
* })
|
|
25017
25703
|
* ```
|
|
@@ -25026,17 +25712,19 @@ const onChannelDeleted = (callback) => {
|
|
|
25026
25712
|
const onChannelJoined = (callback) => {
|
|
25027
25713
|
const client = getActiveClient();
|
|
25028
25714
|
const filter = (payload) => {
|
|
25029
|
-
const
|
|
25030
|
-
|
|
25031
|
-
|
|
25715
|
+
const channelPayload = prepareChannelPayload(payload);
|
|
25716
|
+
const preparedPayload = prepareMembershipPayload(channelPayload, 'channelUsers');
|
|
25717
|
+
const { channels, channelUsers } = preparedPayload;
|
|
25718
|
+
ingestInCache(preparedPayload);
|
|
25719
|
+
callback(channels[0], channelUsers[0]);
|
|
25032
25720
|
};
|
|
25033
|
-
return createEventSubscriber(client, 'onChannelJoined', '
|
|
25721
|
+
return createEventSubscriber(client, 'onChannelJoined', 'channel.joined', filter);
|
|
25034
25722
|
};
|
|
25035
25723
|
|
|
25036
25724
|
/**
|
|
25037
25725
|
* ```js
|
|
25038
25726
|
* import { onChannelLeft } from '@amityco/ts-sdk'
|
|
25039
|
-
* const dispose = onChannelLeft(channel => {
|
|
25727
|
+
* const dispose = onChannelLeft((channel, member) => {
|
|
25040
25728
|
* // ...
|
|
25041
25729
|
* })
|
|
25042
25730
|
* ```
|
|
@@ -25049,13 +25737,40 @@ const onChannelJoined = (callback) => {
|
|
|
25049
25737
|
* @category Channel Events
|
|
25050
25738
|
*/
|
|
25051
25739
|
const onChannelLeft = (callback) => {
|
|
25740
|
+
const client = getActiveClient();
|
|
25741
|
+
const filter = (payload) => {
|
|
25742
|
+
const channelPayload = prepareChannelPayload(payload);
|
|
25743
|
+
const preparedPayload = prepareMembershipPayload(channelPayload, 'channelUsers');
|
|
25744
|
+
const { channels, channelUsers } = preparedPayload;
|
|
25745
|
+
ingestInCache(preparedPayload);
|
|
25746
|
+
callback(channels[0], channelUsers[0]);
|
|
25747
|
+
};
|
|
25748
|
+
return createEventSubscriber(client, 'onChannelLeft', 'channel.left', filter);
|
|
25749
|
+
};
|
|
25750
|
+
|
|
25751
|
+
/**
|
|
25752
|
+
* ```js
|
|
25753
|
+
* import { onChannelMuted } from '@amityco/ts-sdk'
|
|
25754
|
+
* const dispose = onChannelMuted(channel => {
|
|
25755
|
+
* // ...
|
|
25756
|
+
* })
|
|
25757
|
+
* ```
|
|
25758
|
+
*
|
|
25759
|
+
* Fired when any {@link Amity.Channel} have been muted
|
|
25760
|
+
*
|
|
25761
|
+
* @param callback The function to call when the event was fired
|
|
25762
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
25763
|
+
*
|
|
25764
|
+
* @category Channel Events
|
|
25765
|
+
*/
|
|
25766
|
+
const onChannelMuted = (callback) => {
|
|
25052
25767
|
const client = getActiveClient();
|
|
25053
25768
|
const filter = (payload) => {
|
|
25054
25769
|
const data = prepareChannelPayload(payload);
|
|
25055
25770
|
ingestInCache(data);
|
|
25056
25771
|
callback(data.channels[0]);
|
|
25057
25772
|
};
|
|
25058
|
-
return createEventSubscriber(client, '
|
|
25773
|
+
return createEventSubscriber(client, 'onChannelMuted', 'channel.setMuted', filter);
|
|
25059
25774
|
};
|
|
25060
25775
|
|
|
25061
25776
|
/**
|
|
@@ -25076,11 +25791,13 @@ const onChannelLeft = (callback) => {
|
|
|
25076
25791
|
const onChannelMemberAdded = (callback) => {
|
|
25077
25792
|
const client = getActiveClient();
|
|
25078
25793
|
const filter = (payload) => {
|
|
25079
|
-
const
|
|
25080
|
-
|
|
25081
|
-
|
|
25794
|
+
const channelPayload = prepareChannelPayload(payload);
|
|
25795
|
+
const preparedPayload = prepareMembershipPayload(channelPayload, 'channelUsers');
|
|
25796
|
+
const { channels, channelUsers } = preparedPayload;
|
|
25797
|
+
ingestInCache(preparedPayload);
|
|
25798
|
+
callback(channels[0], channelUsers.find(cu => cu.membership === 'member'));
|
|
25082
25799
|
};
|
|
25083
|
-
return createEventSubscriber(client, 'onChannelMemberAdded', '
|
|
25800
|
+
return createEventSubscriber(client, 'onChannelMemberAdded', 'channel.membersAdded', filter);
|
|
25084
25801
|
};
|
|
25085
25802
|
|
|
25086
25803
|
/**
|
|
@@ -25101,11 +25818,13 @@ const onChannelMemberAdded = (callback) => {
|
|
|
25101
25818
|
const onChannelMemberRemoved = (callback) => {
|
|
25102
25819
|
const client = getActiveClient();
|
|
25103
25820
|
const filter = (payload) => {
|
|
25104
|
-
const
|
|
25105
|
-
|
|
25106
|
-
|
|
25821
|
+
const channelPayload = prepareChannelPayload(payload);
|
|
25822
|
+
const preparedPayload = prepareMembershipPayload(channelPayload, 'channelUsers');
|
|
25823
|
+
const { channels, channelUsers } = preparedPayload;
|
|
25824
|
+
ingestInCache(preparedPayload);
|
|
25825
|
+
callback(channels[0], channelUsers[0]);
|
|
25107
25826
|
};
|
|
25108
|
-
return createEventSubscriber(client, 'onChannelMemberRemoved', '
|
|
25827
|
+
return createEventSubscriber(client, 'onChannelMemberRemoved', 'channel.membersRemoved', filter);
|
|
25109
25828
|
};
|
|
25110
25829
|
|
|
25111
25830
|
/**
|
|
@@ -25126,11 +25845,13 @@ const onChannelMemberRemoved = (callback) => {
|
|
|
25126
25845
|
const onChannelMemberBanned = (callback) => {
|
|
25127
25846
|
const client = getActiveClient();
|
|
25128
25847
|
const filter = (payload) => {
|
|
25129
|
-
const
|
|
25130
|
-
|
|
25131
|
-
|
|
25848
|
+
const channelPayload = prepareChannelPayload(payload);
|
|
25849
|
+
const preparedPayload = prepareMembershipPayload(channelPayload, 'channelUsers');
|
|
25850
|
+
const { channels, channelUsers } = preparedPayload;
|
|
25851
|
+
ingestInCache(preparedPayload);
|
|
25852
|
+
callback(channels[0], channelUsers.find(cu => cu.membership === 'banned'));
|
|
25132
25853
|
};
|
|
25133
|
-
return createEventSubscriber(client, 'onChannelMemberBanned', '
|
|
25854
|
+
return createEventSubscriber(client, 'onChannelMemberBanned', 'channel.banned', filter);
|
|
25134
25855
|
};
|
|
25135
25856
|
|
|
25136
25857
|
/**
|
|
@@ -25151,11 +25872,13 @@ const onChannelMemberBanned = (callback) => {
|
|
|
25151
25872
|
const onChannelMemberUnbanned = (callback) => {
|
|
25152
25873
|
const client = getActiveClient();
|
|
25153
25874
|
const filter = (payload) => {
|
|
25154
|
-
const
|
|
25155
|
-
|
|
25156
|
-
|
|
25875
|
+
const channelPayload = prepareChannelPayload(payload);
|
|
25876
|
+
const preparedPayload = prepareMembershipPayload(channelPayload, 'channelUsers');
|
|
25877
|
+
const { channels, channelUsers } = preparedPayload;
|
|
25878
|
+
ingestInCache(preparedPayload);
|
|
25879
|
+
callback(channels[0], channelUsers.find(cu => cu.membership === 'none'));
|
|
25157
25880
|
};
|
|
25158
|
-
return createEventSubscriber(client, '
|
|
25881
|
+
return createEventSubscriber(client, 'onChannelMemberUnbanned', 'channel.unbanned', filter);
|
|
25159
25882
|
};
|
|
25160
25883
|
|
|
25161
25884
|
/**
|
|
@@ -25191,7 +25914,7 @@ const observeChannel = (channelId, callback) => {
|
|
|
25191
25914
|
(_c = callback[action]) === null || _c === void 0 ? void 0 : _c.call(callback, result);
|
|
25192
25915
|
};
|
|
25193
25916
|
const disposers = [];
|
|
25194
|
-
disposers.push(onChannelUpdated(data => router({ data, loading: false, origin: 'event' }, 'onUpdate')), onChannelDeleted(data => router({ data, loading: false, origin: 'event' }, 'onDelete')), onChannelJoined(data => router({ data, loading: false, origin: 'event' }, 'onJoin')), onChannelLeft(data => router({ data, loading: false, origin: 'event' }, 'onLeft')), onChannelMemberAdded(data => router({ data, loading: false, origin: 'event' }, 'onMemberAdded')), onChannelMemberRemoved(data => router({ data, loading: false, origin: 'event' }, 'onMemberRemoved')));
|
|
25917
|
+
disposers.push(onChannelUpdated(data => router({ data, loading: false, origin: 'event' }, 'onUpdate')), onChannelDeleted(data => router({ data, loading: false, origin: 'event' }, 'onDelete')), onChannelJoined(data => router({ data, loading: false, origin: 'event' }, 'onJoin')), onChannelLeft(data => router({ data, loading: false, origin: 'event' }, 'onLeft')), onChannelMuted(data => router({ data, loading: false, origin: 'event' }, 'onMute')), onChannelMemberAdded(data => router({ data, loading: false, origin: 'event' }, 'onMemberAdded')), onChannelMemberRemoved(data => router({ data, loading: false, origin: 'event' }, 'onMemberRemoved')));
|
|
25195
25918
|
runQuery(createQuery(getChannel, channelId), result => result.data && router(result, 'onFetch'));
|
|
25196
25919
|
return () => {
|
|
25197
25920
|
log(`observeChannel(tmpid: ${timestamp}) > dispose`);
|
|
@@ -25226,44 +25949,384 @@ const observeChannels = (callback) => {
|
|
|
25226
25949
|
(_a = callback.onEvent) === null || _a === void 0 ? void 0 : _a.call(callback, action, channel);
|
|
25227
25950
|
(_b = callback[action]) === null || _b === void 0 ? void 0 : _b.call(callback, channel);
|
|
25228
25951
|
};
|
|
25229
|
-
disposers.push(onChannelCreated(data => router(data, 'onCreate')), onChannelUpdated(data => router(data, 'onUpdate')), onChannelDeleted(data => router(data, 'onDelete')), onChannelJoined(data => router(data, 'onJoin')), onChannelLeft(data => router(data, 'onLeft')), onChannelMemberAdded(data => router(data, 'onMemberAdded')), onChannelMemberRemoved(data => router(data, 'onMemberRemoved')));
|
|
25952
|
+
disposers.push(onChannelCreated(data => router(data, 'onCreate')), onChannelUpdated(data => router(data, 'onUpdate')), onChannelDeleted(data => router(data, 'onDelete')), onChannelJoined(data => router(data, 'onJoin')), onChannelLeft(data => router(data, 'onLeft')), onChannelMuted(data => router(data, 'onMute')), onChannelMemberAdded(data => router(data, 'onMemberAdded')), onChannelMemberRemoved(data => router(data, 'onMemberRemoved')));
|
|
25230
25953
|
return () => {
|
|
25231
25954
|
log(`observeChannels(tmpid: ${timestamp}) > dispose`);
|
|
25232
25955
|
disposers.forEach(fn => fn());
|
|
25233
25956
|
};
|
|
25234
25957
|
};
|
|
25235
25958
|
|
|
25959
|
+
function hasMention(model, userId) {
|
|
25960
|
+
var _a, _b;
|
|
25961
|
+
const channelMention = (_a = model === null || model === void 0 ? void 0 : model.mentionees) === null || _a === void 0 ? void 0 : _a.find(x => x.type === 'channel');
|
|
25962
|
+
if (channelMention) {
|
|
25963
|
+
return true;
|
|
25964
|
+
}
|
|
25965
|
+
const userMention = (_b = model === null || model === void 0 ? void 0 : model.mentionees) === null || _b === void 0 ? void 0 : _b.find(x => x.type === 'user');
|
|
25966
|
+
if (userMention) {
|
|
25967
|
+
return userMention.userIds.includes(userId);
|
|
25968
|
+
}
|
|
25969
|
+
return false;
|
|
25970
|
+
}
|
|
25971
|
+
function isCollectionOutdated(collection, newItem, action) {
|
|
25972
|
+
const key = identifyModelKey(newItem);
|
|
25973
|
+
// @ts-ignore
|
|
25974
|
+
const item = collection.find(x => x[key] === newItem[key]);
|
|
25975
|
+
if (item) {
|
|
25976
|
+
return hasUpdates(item, newItem);
|
|
25977
|
+
}
|
|
25978
|
+
return action === 'onCreate';
|
|
25979
|
+
}
|
|
25980
|
+
function merge(collection1, collection2) {
|
|
25981
|
+
const temp = [...collection1, ...collection2];
|
|
25982
|
+
if (temp.length === 0) {
|
|
25983
|
+
return [];
|
|
25984
|
+
}
|
|
25985
|
+
const key = identifyModelKey(temp[0]);
|
|
25986
|
+
const map = new Map();
|
|
25987
|
+
// @ts-ignore
|
|
25988
|
+
temp.forEach(x => map.set(x[key], x));
|
|
25989
|
+
return Array.from(map.values());
|
|
25990
|
+
}
|
|
25991
|
+
|
|
25236
25992
|
/**
|
|
25237
25993
|
* ```js
|
|
25238
|
-
* import {
|
|
25239
|
-
*
|
|
25240
|
-
*
|
|
25241
|
-
*
|
|
25242
|
-
* displayName: Amity.Channel['displayName'],
|
|
25243
|
-
* }, response => merge(channels, response.data))
|
|
25994
|
+
* import { onMessageCreated } from '@amityco/ts-sdk'
|
|
25995
|
+
* const dispose = onMessageCreated(message => {
|
|
25996
|
+
* // ...
|
|
25997
|
+
* })
|
|
25244
25998
|
* ```
|
|
25245
25999
|
*
|
|
25246
|
-
*
|
|
26000
|
+
* Fired when an {@link Amity.Message} has been created
|
|
25247
26001
|
*
|
|
25248
|
-
* @param
|
|
25249
|
-
* @
|
|
25250
|
-
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the channels
|
|
26002
|
+
* @param callback The function to call when the event was fired
|
|
26003
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
25251
26004
|
*
|
|
25252
|
-
* @category
|
|
26005
|
+
* @category Message Events
|
|
25253
26006
|
*/
|
|
25254
|
-
const
|
|
25255
|
-
const
|
|
25256
|
-
|
|
25257
|
-
|
|
25258
|
-
|
|
25259
|
-
|
|
25260
|
-
|
|
25261
|
-
|
|
25262
|
-
|
|
25263
|
-
|
|
25264
|
-
|
|
25265
|
-
|
|
25266
|
-
|
|
26007
|
+
const onMessageCreated = (callback) => {
|
|
26008
|
+
const client = getActiveClient();
|
|
26009
|
+
const filter = (rawPayload) => {
|
|
26010
|
+
const payload = prepareMessagePayload(rawPayload);
|
|
26011
|
+
const message = payload.messages[0];
|
|
26012
|
+
const cached = pullFromCache(['channel', 'get', message.channelId]);
|
|
26013
|
+
if (cached && cached.data.defaultSubChannelId === message.subChannelId) {
|
|
26014
|
+
let update;
|
|
26015
|
+
// @ts-ignore
|
|
26016
|
+
if (cached.data.localReading) {
|
|
26017
|
+
update = Object.assign(Object.assign({}, cached.data), { localReadToSegment: message.channelSegment, localLastMentionSegment: hasMention(message, client.userId)
|
|
26018
|
+
? message.channelSegment
|
|
26019
|
+
: cached.data.localLastMentionSegment, messageCount: message.channelSegment, lastActivity: message.createdAt, updatedAt: message.createdAt });
|
|
26020
|
+
}
|
|
26021
|
+
else {
|
|
26022
|
+
update = Object.assign(Object.assign({}, cached.data), { defaultSubChannelHasUnreadMention: hasMention(message, client.userId) || cached.data.defaultSubChannelHasUnreadMention, defaultSubChannelUnreadCount: message.channelSegment - cached.data.localReadToSegment, lastActivity: message.createdAt, messageCount: message.channelSegment, updatedAt: message.createdAt });
|
|
26023
|
+
}
|
|
26024
|
+
pushToCache(['channel', 'get', message.channelId], update);
|
|
26025
|
+
client.emitter.emit('local.channel.updated', { channels: [update] });
|
|
26026
|
+
}
|
|
26027
|
+
ingestInCache(payload);
|
|
26028
|
+
callback(message);
|
|
26029
|
+
};
|
|
26030
|
+
const disposers = [
|
|
26031
|
+
createEventSubscriber(client, 'message/onMessageCreated', 'message.created', filter),
|
|
26032
|
+
createEventSubscriber(client, 'local.message.created', 'local.message.created', payload => callback(payload.messages[0])),
|
|
26033
|
+
];
|
|
26034
|
+
return () => {
|
|
26035
|
+
disposers.forEach(fn => fn());
|
|
26036
|
+
};
|
|
26037
|
+
};
|
|
26038
|
+
|
|
26039
|
+
/**
|
|
26040
|
+
* ```js
|
|
26041
|
+
* import { onMessageUpdated } from '@amityco/ts-sdk'
|
|
26042
|
+
* const dispose = onMessageUpdated(message => {
|
|
26043
|
+
* // ...
|
|
26044
|
+
* })
|
|
26045
|
+
* ```
|
|
26046
|
+
*
|
|
26047
|
+
* Fired when an {@link Amity.Message} has been updated
|
|
26048
|
+
*
|
|
26049
|
+
* @param callback The function to call when the event was fired
|
|
26050
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
26051
|
+
*
|
|
26052
|
+
* @category Message Events
|
|
26053
|
+
*/
|
|
26054
|
+
const onMessageUpdated = (callback) => {
|
|
26055
|
+
const client = getActiveClient();
|
|
26056
|
+
const filter = (rawPayload) => {
|
|
26057
|
+
const payload = prepareMessagePayload(rawPayload);
|
|
26058
|
+
ingestInCache(payload);
|
|
26059
|
+
callback(payload.messages[0]);
|
|
26060
|
+
};
|
|
26061
|
+
const disposers = [
|
|
26062
|
+
createEventSubscriber(client, 'message/onMessageUpdated', 'message.updated', filter),
|
|
26063
|
+
createEventSubscriber(client, 'local.message.updated', 'local.message.updated', payload => callback(payload.messages[0])),
|
|
26064
|
+
];
|
|
26065
|
+
return () => {
|
|
26066
|
+
disposers.forEach(fn => fn());
|
|
26067
|
+
};
|
|
26068
|
+
};
|
|
26069
|
+
|
|
26070
|
+
/**
|
|
26071
|
+
* ```js
|
|
26072
|
+
* import { onMessageDeleted } from '@amityco/ts-sdk'
|
|
26073
|
+
* const dispose = onMessageDeleted(message => {
|
|
26074
|
+
* // ...
|
|
26075
|
+
* })
|
|
26076
|
+
* ```
|
|
26077
|
+
*
|
|
26078
|
+
* Fired when an {@link Amity.Message} was deleted
|
|
26079
|
+
*
|
|
26080
|
+
* @param callback The function to call when the event was fired
|
|
26081
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
26082
|
+
*
|
|
26083
|
+
* @category Message Events
|
|
26084
|
+
*/
|
|
26085
|
+
const onMessageDeleted = (callback) => {
|
|
26086
|
+
const client = getActiveClient();
|
|
26087
|
+
const filter = (rawPayload) => {
|
|
26088
|
+
const payload = prepareMessagePayload(rawPayload);
|
|
26089
|
+
ingestInCache(payload);
|
|
26090
|
+
callback(payload.messages[0]);
|
|
26091
|
+
};
|
|
26092
|
+
const disposers = [
|
|
26093
|
+
createEventSubscriber(client, 'message/onMessageDeleted', 'message.deleted', filter),
|
|
26094
|
+
createEventSubscriber(client, 'local.message.deleted', 'local.message.deleted', payload => callback(payload.messages[0])),
|
|
26095
|
+
];
|
|
26096
|
+
return () => {
|
|
26097
|
+
disposers.forEach(fn => fn());
|
|
26098
|
+
};
|
|
26099
|
+
};
|
|
26100
|
+
|
|
26101
|
+
/**
|
|
26102
|
+
* ```js
|
|
26103
|
+
* import { onMessageFlagged } from '@amityco/ts-sdk'
|
|
26104
|
+
* const dispose = onMessageFlagged(message => {
|
|
26105
|
+
* // ...
|
|
26106
|
+
* })
|
|
26107
|
+
* ```
|
|
26108
|
+
*
|
|
26109
|
+
* Fired when a {@link Amity.Message} has been flagged
|
|
26110
|
+
*
|
|
26111
|
+
* @param callback The function to call when the event was fired
|
|
26112
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
26113
|
+
*
|
|
26114
|
+
* @category Message Events
|
|
26115
|
+
*/
|
|
26116
|
+
const onMessageFlagged = (callback) => {
|
|
26117
|
+
const client = getActiveClient();
|
|
26118
|
+
const filter = (rawPayload) => {
|
|
26119
|
+
const payload = prepareMessagePayload(rawPayload);
|
|
26120
|
+
ingestInCache(payload);
|
|
26121
|
+
callback(payload.messages[0]);
|
|
26122
|
+
};
|
|
26123
|
+
return createEventSubscriber(client, 'onMessageFlagged', 'message.flagged', filter);
|
|
26124
|
+
};
|
|
26125
|
+
|
|
26126
|
+
/**
|
|
26127
|
+
* ```js
|
|
26128
|
+
* import { onMessageUnflagged } from '@amityco/ts-sdk'
|
|
26129
|
+
* const dispose = onMessageUnflagged(message => {
|
|
26130
|
+
* // ...
|
|
26131
|
+
* })
|
|
26132
|
+
* ```
|
|
26133
|
+
*
|
|
26134
|
+
* Fired when a flag has been removed from a {@link Amity.Message}
|
|
26135
|
+
*
|
|
26136
|
+
* @param callback The function to call when the event was fired
|
|
26137
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
26138
|
+
*
|
|
26139
|
+
* @category Message Events
|
|
26140
|
+
*/
|
|
26141
|
+
const onMessageUnflagged = (callback) => {
|
|
26142
|
+
const client = getActiveClient();
|
|
26143
|
+
const filter = (rawPayload) => {
|
|
26144
|
+
const payload = prepareMessagePayload(rawPayload);
|
|
26145
|
+
ingestInCache(payload);
|
|
26146
|
+
callback(payload.messages[0]);
|
|
26147
|
+
};
|
|
26148
|
+
return createEventSubscriber(client, 'onMessageUnflagged', 'message.unflagged', filter);
|
|
26149
|
+
};
|
|
26150
|
+
|
|
26151
|
+
/**
|
|
26152
|
+
* ```js
|
|
26153
|
+
* import { onMessageFlagCleared } from '@amityco/ts-sdk'
|
|
26154
|
+
* const dispose = onMessageFlagCleared(message => {
|
|
26155
|
+
* // ...
|
|
26156
|
+
* })
|
|
26157
|
+
* ```
|
|
26158
|
+
*
|
|
26159
|
+
* Fired when flags have been cleared for a {@link Amity.Message}
|
|
26160
|
+
*
|
|
26161
|
+
* @param callback The function to call when the event was fired
|
|
26162
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
26163
|
+
*
|
|
26164
|
+
* @category Message Events
|
|
26165
|
+
*/
|
|
26166
|
+
const onMessageFlagCleared = (callback) => {
|
|
26167
|
+
const client = getActiveClient();
|
|
26168
|
+
const filter = (rawPayload) => {
|
|
26169
|
+
const payload = prepareMessagePayload(rawPayload);
|
|
26170
|
+
ingestInCache(payload);
|
|
26171
|
+
callback(payload.messages[0]);
|
|
26172
|
+
};
|
|
26173
|
+
return createEventSubscriber(client, 'onMessageFlagCleared', 'message.flagCleared', filter);
|
|
26174
|
+
};
|
|
26175
|
+
|
|
26176
|
+
/**
|
|
26177
|
+
* ```js
|
|
26178
|
+
* import { onMessageReactionAdded } from '@amityco/ts-sdk';
|
|
26179
|
+
*
|
|
26180
|
+
* const unsubscribe = onMessageReactionAdded(message => {
|
|
26181
|
+
* // ...
|
|
26182
|
+
* });
|
|
26183
|
+
* ```
|
|
26184
|
+
*
|
|
26185
|
+
* Fired when a {@link Amity.Message} has been reacted
|
|
26186
|
+
*
|
|
26187
|
+
* @param callback The function to call when the event was fired
|
|
26188
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
26189
|
+
*
|
|
26190
|
+
* @category Post Events
|
|
26191
|
+
*/
|
|
26192
|
+
const onMessageReactionAdded = (callback) => {
|
|
26193
|
+
const client = getActiveClient();
|
|
26194
|
+
const filter = (rawPayload) => {
|
|
26195
|
+
const payload = prepareMessagePayload(rawPayload);
|
|
26196
|
+
ingestInCache(payload);
|
|
26197
|
+
callback(payload.messages[0]);
|
|
26198
|
+
};
|
|
26199
|
+
return createEventSubscriber(client, 'onMessageReactionAdded', 'message.reactionAdded', filter);
|
|
26200
|
+
};
|
|
26201
|
+
|
|
26202
|
+
/**
|
|
26203
|
+
* ```js
|
|
26204
|
+
* import { onMessageReactionRemoved } from '@amityco/ts-sdk';
|
|
26205
|
+
*
|
|
26206
|
+
* const unsubscribe = onMessageReactionRemoved(message => {
|
|
26207
|
+
* // ...
|
|
26208
|
+
* });
|
|
26209
|
+
* ```
|
|
26210
|
+
*
|
|
26211
|
+
* Fired when a reaction has been removed from a {@link Amity.Message}
|
|
26212
|
+
*
|
|
26213
|
+
* @param callback The function to call when the event was fired
|
|
26214
|
+
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
26215
|
+
*
|
|
26216
|
+
* @category Post Events
|
|
26217
|
+
*/
|
|
26218
|
+
const onMessageReactionRemoved = (callback) => {
|
|
26219
|
+
const client = getActiveClient();
|
|
26220
|
+
const filter = (rawPayload) => {
|
|
26221
|
+
const payload = prepareMessagePayload(rawPayload);
|
|
26222
|
+
ingestInCache(payload);
|
|
26223
|
+
callback(payload.messages[0]);
|
|
26224
|
+
};
|
|
26225
|
+
return createEventSubscriber(client, 'onMessageReactionRemoved', 'message.reactionRemoved', filter);
|
|
26226
|
+
};
|
|
26227
|
+
|
|
26228
|
+
/**
|
|
26229
|
+
* ```js
|
|
26230
|
+
* import { liveChannel } from '@amityco/ts-sdk';
|
|
26231
|
+
*
|
|
26232
|
+
* let channel;
|
|
26233
|
+
*
|
|
26234
|
+
* const unsubscribe = liveChannel(channelId, response => {
|
|
26235
|
+
* channel = response.data;
|
|
26236
|
+
* });
|
|
26237
|
+
* ```
|
|
26238
|
+
*
|
|
26239
|
+
* Observe all mutation on a given {@link Amity.Channel}
|
|
26240
|
+
*
|
|
26241
|
+
* @param channelId the ID of the channel to observe
|
|
26242
|
+
* @param callback the function to call when new data are available
|
|
26243
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the channel
|
|
26244
|
+
*
|
|
26245
|
+
* @category Message Live Object
|
|
26246
|
+
*/
|
|
26247
|
+
const liveChannel = (channelId, callback) => {
|
|
26248
|
+
const { log, cache } = getActiveClient();
|
|
26249
|
+
const timestamp = Date.now();
|
|
26250
|
+
log(`liveChannel(tmpid: ${timestamp}) > listen`);
|
|
26251
|
+
if (!cache) {
|
|
26252
|
+
console.log(LIVE_OBJECT_ENABLE_CACHE_MESSAGE);
|
|
26253
|
+
}
|
|
26254
|
+
let channel;
|
|
26255
|
+
let isUnsyncedObject = false;
|
|
26256
|
+
const disposers = [];
|
|
26257
|
+
const dispatcher = (data) => {
|
|
26258
|
+
channel = data.data;
|
|
26259
|
+
callback(data);
|
|
26260
|
+
};
|
|
26261
|
+
const realtimeRouter = (eventChannel) => {
|
|
26262
|
+
if (channelId !== eventChannel.channelId) {
|
|
26263
|
+
return;
|
|
26264
|
+
}
|
|
26265
|
+
if (channel && !hasUpdates(channel, eventChannel)) {
|
|
26266
|
+
return;
|
|
26267
|
+
}
|
|
26268
|
+
dispatcher({ loading: false, data: eventChannel, origin: 'event' });
|
|
26269
|
+
};
|
|
26270
|
+
const onFetch = () => {
|
|
26271
|
+
const query = createQuery(getChannel, channelId);
|
|
26272
|
+
runQuery(query, ({ loading, data, error, origin, cachedAt }) => {
|
|
26273
|
+
if (cachedAt === UNSYNCED_OBJECT_CACHED_AT_VALUE) {
|
|
26274
|
+
dispatcher({
|
|
26275
|
+
data,
|
|
26276
|
+
origin,
|
|
26277
|
+
loading: false,
|
|
26278
|
+
error: new ASCApiError(UNSYNCED_OBJECT_CACHED_AT_MESSAGE, 800800 /* Amity.ClientError.DISALOOW_UNSYNCED_OBJECT */, "error" /* Amity.ErrorLevel.ERROR */),
|
|
26279
|
+
});
|
|
26280
|
+
isUnsyncedObject = true;
|
|
26281
|
+
disposers.forEach(fn => fn());
|
|
26282
|
+
}
|
|
26283
|
+
else if (!isUnsyncedObject) {
|
|
26284
|
+
dispatcher({ loading, data, origin, error });
|
|
26285
|
+
}
|
|
26286
|
+
if (error) {
|
|
26287
|
+
disposers.forEach(fn => fn());
|
|
26288
|
+
}
|
|
26289
|
+
});
|
|
26290
|
+
};
|
|
26291
|
+
disposers.push(onChannelUpdated(realtimeRouter), onChannelDeleted(realtimeRouter), onChannelJoined(realtimeRouter), onChannelMuted(realtimeRouter), onMessageCreated(() => null));
|
|
26292
|
+
onFetch();
|
|
26293
|
+
return () => {
|
|
26294
|
+
log(`liveChannel(tmpid: ${timestamp}) > dispose`);
|
|
26295
|
+
disposers.forEach(fn => fn());
|
|
26296
|
+
};
|
|
26297
|
+
};
|
|
26298
|
+
|
|
26299
|
+
/**
|
|
26300
|
+
* ```js
|
|
26301
|
+
* import { liveChannels } from '@amityco/ts-sdk'
|
|
26302
|
+
*
|
|
26303
|
+
* let channels = []
|
|
26304
|
+
* const unsub = liveChannels({
|
|
26305
|
+
* displayName: Amity.Channel['displayName'],
|
|
26306
|
+
* }, response => merge(channels, response.data))
|
|
26307
|
+
* ```
|
|
26308
|
+
*
|
|
26309
|
+
* Observe all mutations on a list of {@link Amity.Channel}s
|
|
26310
|
+
*
|
|
26311
|
+
* @param params for querying channels
|
|
26312
|
+
* @param callback the function to call when new data are available
|
|
26313
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the channels
|
|
26314
|
+
*
|
|
26315
|
+
* @category Channel Live Collection
|
|
26316
|
+
*/
|
|
26317
|
+
const liveChannels = (params, callback, config) => {
|
|
26318
|
+
const { log, cache, userId } = getActiveClient();
|
|
26319
|
+
if (!cache) {
|
|
26320
|
+
console.log(ENABLE_CACHE_MESSAGE);
|
|
26321
|
+
}
|
|
26322
|
+
const timestamp = Date.now();
|
|
26323
|
+
log(`liveChannels(tmpid: ${timestamp}) > listen`);
|
|
26324
|
+
const { limit: queryLimit } = params, queryParams = __rest(params, ["limit"]);
|
|
26325
|
+
const limit = queryLimit !== null && queryLimit !== void 0 ? queryLimit : COLLECTION_DEFAULT_PAGINATION_LIMIT;
|
|
26326
|
+
const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config !== null && config !== void 0 ? config : {};
|
|
26327
|
+
const disposers = [];
|
|
26328
|
+
const cacheKey = ['channel', 'collection', {}];
|
|
26329
|
+
const responder = (data) => {
|
|
25267
26330
|
var _a, _b;
|
|
25268
26331
|
let channels = (_a = data.data
|
|
25269
26332
|
.map(channelId => pullFromCache(['channel', 'get', channelId]))
|
|
@@ -25309,21 +26372,17 @@ const liveChannels = (params, callback, config) => {
|
|
|
25309
26372
|
error: data.error,
|
|
25310
26373
|
});
|
|
25311
26374
|
};
|
|
25312
|
-
const realtimeRouter = (
|
|
26375
|
+
const realtimeRouter = (action) => (channel) => {
|
|
25313
26376
|
var _a;
|
|
25314
26377
|
const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
|
|
25315
26378
|
if (!collection)
|
|
25316
26379
|
return;
|
|
25317
|
-
if (action === '
|
|
25318
|
-
collection.data = [...new Set([channel.channelId, ...collection.data])];
|
|
25319
|
-
/*
|
|
25320
|
-
* if the filter isDeleted is set, only collection item must be updated and not
|
|
25321
|
-
* the collection
|
|
25322
|
-
*/
|
|
25323
|
-
}
|
|
25324
|
-
else if (action === 'onDelete' && !params.isDeleted) {
|
|
26380
|
+
if (action === 'onDelete' && params.isDeleted === false) {
|
|
25325
26381
|
collection.data = collection.data.filter(c => c !== channel.channelId);
|
|
25326
26382
|
}
|
|
26383
|
+
else {
|
|
26384
|
+
collection.data = [...new Set([channel.channelId, ...collection.data])];
|
|
26385
|
+
}
|
|
25327
26386
|
pushToCache(cacheKey, collection);
|
|
25328
26387
|
responder(collection);
|
|
25329
26388
|
};
|
|
@@ -25348,7 +26407,7 @@ const liveChannels = (params, callback, config) => {
|
|
|
25348
26407
|
responder(data);
|
|
25349
26408
|
}, queryOptions(policy));
|
|
25350
26409
|
};
|
|
25351
|
-
disposers.push(onChannelCreated(
|
|
26410
|
+
disposers.push(onChannelCreated(realtimeRouter('onCreate')), onChannelDeleted(realtimeRouter('onDelete')), onChannelUpdated(realtimeRouter('onUpdate')), onChannelMuted(realtimeRouter('onMute')), onChannelJoined(realtimeRouter('onJoin')), onChannelLeft(realtimeRouter('onLeft')), onChannelMemberAdded(realtimeRouter('onMemberAdded')), onChannelMemberRemoved(realtimeRouter('onMemberRemoved')), onMessageCreated(() => null));
|
|
25352
26411
|
onFetch();
|
|
25353
26412
|
return () => {
|
|
25354
26413
|
log(`liveChannels(tmpid: ${timestamp}) > dispose`);
|
|
@@ -25366,184 +26425,655 @@ const liveChannels = (params, callback, config) => {
|
|
|
25366
26425
|
* }, response => merge(channelMembers, response.data))
|
|
25367
26426
|
* ```
|
|
25368
26427
|
*
|
|
25369
|
-
* Observe all mutations on a list of {@link Amity.ChannelUser}s
|
|
26428
|
+
* Observe all mutations on a list of {@link Amity.ChannelUser}s
|
|
26429
|
+
*
|
|
26430
|
+
* @param params for querying channel users
|
|
26431
|
+
* @param callback the function to call when new data are available
|
|
26432
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the channel users
|
|
26433
|
+
*
|
|
26434
|
+
* @category Channel Live Collection
|
|
26435
|
+
*/
|
|
26436
|
+
const liveChannelMembers = (params, callback, config) => {
|
|
26437
|
+
const { log, cache } = getActiveClient();
|
|
26438
|
+
if (!cache) {
|
|
26439
|
+
console.log(ENABLE_CACHE_MESSAGE);
|
|
26440
|
+
}
|
|
26441
|
+
const timestamp = Date.now();
|
|
26442
|
+
log(`liveChannelMembers(tmpid: ${timestamp}) > listen`);
|
|
26443
|
+
const { limit: queryLimit } = params, queryParams = __rest(params, ["limit"]);
|
|
26444
|
+
const limit = queryLimit !== null && queryLimit !== void 0 ? queryLimit : COLLECTION_DEFAULT_PAGINATION_LIMIT;
|
|
26445
|
+
const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config !== null && config !== void 0 ? config : {};
|
|
26446
|
+
const disposers = [];
|
|
26447
|
+
const cacheKey = ['channelUsers', 'collection', { channelId: params.channelId }];
|
|
26448
|
+
const responder = (data) => {
|
|
26449
|
+
var _a, _b;
|
|
26450
|
+
let channelMembers = (_a = data.data
|
|
26451
|
+
.map(id => pullFromCache(['channelUsers', 'get', id]))
|
|
26452
|
+
.filter(Boolean)
|
|
26453
|
+
.map(({ data }) => data)) !== null && _a !== void 0 ? _a : [];
|
|
26454
|
+
channelMembers = filterByPropEquality(channelMembers, 'roles', params.roles);
|
|
26455
|
+
if (params.memberships) {
|
|
26456
|
+
/*
|
|
26457
|
+
* even though membership includes muted as a possible value
|
|
26458
|
+
* when querying the server.
|
|
26459
|
+
* Muted is specified under seperarte property namely isMuted
|
|
26460
|
+
* Hence why I've seperately checked for it's equality
|
|
26461
|
+
*/
|
|
26462
|
+
channelMembers = channelMembers.filter(member => {
|
|
26463
|
+
// @ts-ignore
|
|
26464
|
+
if (params.memberships.includes('muted') && member.isMuted) {
|
|
26465
|
+
return true;
|
|
26466
|
+
}
|
|
26467
|
+
// @ts-ignore
|
|
26468
|
+
return params.memberships.includes(member.membership);
|
|
26469
|
+
});
|
|
26470
|
+
}
|
|
26471
|
+
if (params.search) {
|
|
26472
|
+
const containsMatcher = new RegExp(params.search);
|
|
26473
|
+
channelMembers = channelMembers.filter(m => m.userId.match(containsMatcher));
|
|
26474
|
+
}
|
|
26475
|
+
// sort, 'lastCreated' is the default sort order
|
|
26476
|
+
const sortBy = params.sortBy ? params.sortBy : 'lastCreated';
|
|
26477
|
+
channelMembers = channelMembers.sort(sortBy === 'lastCreated' ? sortByLastCreated : sortByFirstCreated);
|
|
26478
|
+
callback({
|
|
26479
|
+
onNextPage: onFetch,
|
|
26480
|
+
data: channelMembers,
|
|
26481
|
+
hasNextPage: !!((_b = data.params) === null || _b === void 0 ? void 0 : _b.page),
|
|
26482
|
+
loading: data.loading,
|
|
26483
|
+
error: data.error,
|
|
26484
|
+
});
|
|
26485
|
+
};
|
|
26486
|
+
const realtimeRouter = (action) => (channel, channelMember) => {
|
|
26487
|
+
var _a;
|
|
26488
|
+
if (params.channelId !== channelMember.channelId) {
|
|
26489
|
+
return;
|
|
26490
|
+
}
|
|
26491
|
+
const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
|
|
26492
|
+
if (!collection)
|
|
26493
|
+
return;
|
|
26494
|
+
const channelMemberCacheId = getResolver('channelUsers')({
|
|
26495
|
+
channelId: params.channelId,
|
|
26496
|
+
userId: channelMember.userId,
|
|
26497
|
+
});
|
|
26498
|
+
if (channelMember.membership === 'none') {
|
|
26499
|
+
collection.data = collection.data.filter(m => m !== channelMemberCacheId);
|
|
26500
|
+
}
|
|
26501
|
+
else if (!collection.data.includes(channelMemberCacheId)) {
|
|
26502
|
+
collection.data = [channelMemberCacheId, ...collection.data];
|
|
26503
|
+
}
|
|
26504
|
+
pushToCache(cacheKey, collection);
|
|
26505
|
+
responder(collection);
|
|
26506
|
+
};
|
|
26507
|
+
const onFetch = () => {
|
|
26508
|
+
var _a, _b, _c;
|
|
26509
|
+
const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
|
|
26510
|
+
const channelMembers = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
|
|
26511
|
+
if (channelMembers.length > 0 && !(collection === null || collection === void 0 ? void 0 : collection.params.page))
|
|
26512
|
+
return;
|
|
26513
|
+
const query = createQuery(queryChannelMembers, Object.assign(Object.assign({}, queryParams), { page: (_c = collection === null || collection === void 0 ? void 0 : collection.params.page) !== null && _c !== void 0 ? _c : { limit } }));
|
|
26514
|
+
runQuery(query, ({ data: result, error, loading, nextPage: page }) => {
|
|
26515
|
+
const data = {
|
|
26516
|
+
loading,
|
|
26517
|
+
error,
|
|
26518
|
+
params: { page },
|
|
26519
|
+
data: channelMembers,
|
|
26520
|
+
};
|
|
26521
|
+
if (result) {
|
|
26522
|
+
data.data = [...new Set([...channelMembers, ...result.map(getResolver('channelUsers'))])];
|
|
26523
|
+
}
|
|
26524
|
+
pushToCache(cacheKey, data);
|
|
26525
|
+
responder(data);
|
|
26526
|
+
}, queryOptions(policy));
|
|
26527
|
+
};
|
|
26528
|
+
disposers.push(onChannelJoined(realtimeRouter()), onChannelLeft(realtimeRouter()), onChannelMemberAdded(realtimeRouter()), onChannelMemberRemoved(realtimeRouter()), onChannelMemberBanned(realtimeRouter()), onChannelMemberUnbanned(realtimeRouter()));
|
|
26529
|
+
onFetch();
|
|
26530
|
+
return () => {
|
|
26531
|
+
log(`liveChannelMembers(tmpid: ${timestamp}) > dispose`);
|
|
26532
|
+
disposers.forEach(fn => fn());
|
|
26533
|
+
};
|
|
26534
|
+
};
|
|
26535
|
+
|
|
26536
|
+
function convertFromRaw(_a) {
|
|
26537
|
+
var { channelId, channelPublicId, channelType, childCount, creatorId, creatorPublicId, lastMessageId, lastMessageTimestamp, messageFeedId, name } = _a, rest = __rest(_a, ["channelId", "channelPublicId", "channelType", "childCount", "creatorId", "creatorPublicId", "lastMessageId", "lastMessageTimestamp", "messageFeedId", "name"]);
|
|
26538
|
+
return Object.assign(Object.assign({}, rest), { channelId: channelPublicId, creatorId: creatorPublicId, displayName: name, lastActivity: lastMessageTimestamp, latestMessageId: lastMessageId, messageCount: childCount, subChannelId: messageFeedId });
|
|
26539
|
+
}
|
|
26540
|
+
const prepareSubChannelPayload = (payload) => {
|
|
26541
|
+
return Object.assign(Object.assign({}, payload), { messageFeeds: payload.messageFeeds.map(convertFromRaw) });
|
|
26542
|
+
};
|
|
26543
|
+
function convertQueryParams(_a) {
|
|
26544
|
+
var { excludeDefaultSubChannel } = _a, rest = __rest(_a, ["excludeDefaultSubChannel"]);
|
|
26545
|
+
const out = Object.assign({}, rest);
|
|
26546
|
+
if (excludeDefaultSubChannel !== undefined) {
|
|
26547
|
+
out.excludeDefaultMessageFeed = excludeDefaultSubChannel;
|
|
26548
|
+
}
|
|
26549
|
+
return out;
|
|
26550
|
+
}
|
|
26551
|
+
|
|
26552
|
+
/**
|
|
26553
|
+
* ```js
|
|
26554
|
+
* import { querySubChannels } from '@amityco/ts-sdk'
|
|
26555
|
+
* const subChannels = await querySubChannels({ channelId: 'channelId' })
|
|
26556
|
+
* ```
|
|
26557
|
+
*
|
|
26558
|
+
* Queries a paginable list of {@link Amity.SubChannel} objects
|
|
26559
|
+
*
|
|
26560
|
+
* @param query The query parameters
|
|
26561
|
+
* @returns A page of {@link Amity.SubChannel} objects
|
|
26562
|
+
*
|
|
26563
|
+
* @category Channel API
|
|
26564
|
+
* @async
|
|
26565
|
+
*/
|
|
26566
|
+
const querySubChannels = async (query) => {
|
|
26567
|
+
const client = getActiveClient();
|
|
26568
|
+
client.log('channel/querySubChannels', query);
|
|
26569
|
+
const _a = convertQueryParams(query), { page = { limit: 10 }, channelId } = _a, params = __rest(_a, ["page", "channelId"]);
|
|
26570
|
+
const response = await client.http.get(`/api/v5/message-feeds/channel/${channelId}`, {
|
|
26571
|
+
params: Object.assign(Object.assign({}, params), { options: {
|
|
26572
|
+
token: toToken(page, 'skiplimit'),
|
|
26573
|
+
} }),
|
|
26574
|
+
});
|
|
26575
|
+
const _b = response.data, { paging } = _b, payload = __rest(_b, ["paging"]);
|
|
26576
|
+
const data = prepareSubChannelPayload(payload);
|
|
26577
|
+
const subChannels = data.messageFeeds;
|
|
26578
|
+
const cachedAt = client.cache && Date.now();
|
|
26579
|
+
if (client.cache) {
|
|
26580
|
+
ingestInCache(data, { cachedAt });
|
|
26581
|
+
const cacheKey = [
|
|
26582
|
+
'subChannel',
|
|
26583
|
+
'query',
|
|
26584
|
+
Object.assign(Object.assign({ channelId }, params), { options: Object.assign({}, page) }),
|
|
26585
|
+
];
|
|
26586
|
+
pushToCache(cacheKey, { subChannels: subChannels.map(getResolver('subChannel')), paging });
|
|
26587
|
+
}
|
|
26588
|
+
fireEvent('local.message-feed.fetched', data);
|
|
26589
|
+
const nextPage = toPage(paging.next);
|
|
26590
|
+
const prevPage = toPage(paging.previous);
|
|
26591
|
+
return { data: subChannels, cachedAt, prevPage, nextPage };
|
|
26592
|
+
};
|
|
26593
|
+
/**
|
|
26594
|
+
* ```js
|
|
26595
|
+
* import { querySubChannels } from '@amityco/ts-sdk'
|
|
26596
|
+
* const subChannels = querySubChannels.locally({ channelId: 'channelId' })
|
|
26597
|
+
* ```
|
|
26598
|
+
*
|
|
26599
|
+
* Queries a paginable list of {@link Amity.SubChannel} objects from cache
|
|
26600
|
+
*
|
|
26601
|
+
* @param query The query parameters
|
|
26602
|
+
* @returns sub channels
|
|
26603
|
+
*
|
|
26604
|
+
* @category Channel API
|
|
26605
|
+
*/
|
|
26606
|
+
querySubChannels.locally = (query) => {
|
|
26607
|
+
var _a, _b, _c;
|
|
26608
|
+
const client = getActiveClient();
|
|
26609
|
+
client.log('channel/querySubChannels.locally', query);
|
|
26610
|
+
// @ts-ignore
|
|
26611
|
+
const _d = (_a = convertQueryParams(query)) !== null && _a !== void 0 ? _a : {}, { page = { limit: 10 }, channelId } = _d, params = __rest(_d, ["page", "channelId"]);
|
|
26612
|
+
const cacheKey = [
|
|
26613
|
+
'subChannel',
|
|
26614
|
+
'query',
|
|
26615
|
+
Object.assign(Object.assign({ channelId }, params), { options: Object.assign({}, page) }),
|
|
26616
|
+
];
|
|
26617
|
+
const { data, cachedAt } = (_b = pullFromCache(cacheKey)) !== null && _b !== void 0 ? _b : {};
|
|
26618
|
+
if (!(data === null || data === void 0 ? void 0 : data.subChannels.length))
|
|
26619
|
+
return;
|
|
26620
|
+
const subChannels = data.subChannels
|
|
26621
|
+
.map(ck => pullFromCache(['subChannel', 'get', ck]))
|
|
26622
|
+
.filter(Boolean)
|
|
26623
|
+
.map(({ data }) => data);
|
|
26624
|
+
const prevPage = toPage(data === null || data === void 0 ? void 0 : data.paging.previous);
|
|
26625
|
+
const nextPage = toPage(data === null || data === void 0 ? void 0 : data.paging.next);
|
|
26626
|
+
return subChannels.length === ((_c = data === null || data === void 0 ? void 0 : data.subChannels) === null || _c === void 0 ? void 0 : _c.length)
|
|
26627
|
+
? { data: subChannels, cachedAt, prevPage, nextPage }
|
|
26628
|
+
: undefined;
|
|
26629
|
+
};
|
|
26630
|
+
|
|
26631
|
+
/**
|
|
26632
|
+
* ```js
|
|
26633
|
+
* import { getSubChannels } from '@amityco/ts-sdk'
|
|
26634
|
+
* const subChannels = await getSubChannels(['foo', 'bar'])
|
|
26635
|
+
* ```
|
|
26636
|
+
*
|
|
26637
|
+
* Fetches a collection of {@link Amity.SubChannel} objects
|
|
26638
|
+
*
|
|
26639
|
+
* @param subChannelIds the IDs of the {@link Amity.SubChannel} to fetch
|
|
26640
|
+
* @returns the associated collection of {@link Amity.SubChannel} objects
|
|
26641
|
+
*
|
|
26642
|
+
* @category Channel API
|
|
26643
|
+
* @async
|
|
26644
|
+
*/
|
|
26645
|
+
const getSubChannels = async (subChannelIds) => {
|
|
26646
|
+
const client = getActiveClient();
|
|
26647
|
+
client.log('channel/getSubChannels', subChannelIds);
|
|
26648
|
+
const response = await client.http.get('/api/v5/message-feeds/list', {
|
|
26649
|
+
params: { messageFeedIds: subChannelIds.map(encodeURIComponent) },
|
|
26650
|
+
});
|
|
26651
|
+
const data = prepareSubChannelPayload(response.data);
|
|
26652
|
+
const cachedAt = client.cache && Date.now();
|
|
26653
|
+
if (client.cache)
|
|
26654
|
+
ingestInCache(data, { cachedAt });
|
|
26655
|
+
fireEvent('local.message-feed.fetched', data);
|
|
26656
|
+
return {
|
|
26657
|
+
data: data.messageFeeds,
|
|
26658
|
+
cachedAt,
|
|
26659
|
+
};
|
|
26660
|
+
};
|
|
26661
|
+
/**
|
|
26662
|
+
* ```js
|
|
26663
|
+
* import { getSubChannels } from '@amityco/ts-sdk'
|
|
26664
|
+
* const subChannels = getSubChannels.locally(['foo', 'bar']) ?? []
|
|
26665
|
+
* ```
|
|
26666
|
+
*
|
|
26667
|
+
* Fetches a collection of {@link Amity.SubChannel} objects from cache
|
|
26668
|
+
*
|
|
26669
|
+
* @param subChannelIds the IDs of the {@link Amity.SubChannel} to fetch
|
|
26670
|
+
* @returns the associated collection of {@link Amity.SubChannel} objects
|
|
26671
|
+
*
|
|
26672
|
+
* @category Channel API
|
|
26673
|
+
*/
|
|
26674
|
+
getSubChannels.locally = (subChannelIds) => {
|
|
26675
|
+
var _a;
|
|
26676
|
+
const client = getActiveClient();
|
|
26677
|
+
client.log('channel/getSubChannels.locally', subChannelIds);
|
|
26678
|
+
if (!client.cache)
|
|
26679
|
+
return;
|
|
26680
|
+
const cached = subChannelIds
|
|
26681
|
+
.map(channelId => pullFromCache(['subChannel', 'get', channelId]))
|
|
26682
|
+
.filter(Boolean);
|
|
26683
|
+
if ((cached === null || cached === void 0 ? void 0 : cached.length) < subChannelIds.length)
|
|
26684
|
+
return;
|
|
26685
|
+
const subChannels = cached.map(({ data }) => data);
|
|
26686
|
+
const oldest = (_a = cached.sort((a, b) => (a.cachedAt < b.cachedAt ? -1 : 1))) === null || _a === void 0 ? void 0 : _a[0];
|
|
26687
|
+
return {
|
|
26688
|
+
data: subChannels,
|
|
26689
|
+
cachedAt: oldest.cachedAt,
|
|
26690
|
+
};
|
|
26691
|
+
};
|
|
26692
|
+
|
|
26693
|
+
/**
|
|
26694
|
+
* ```js
|
|
26695
|
+
* import { getSubChannel } from '@amityco/ts-sdk'
|
|
26696
|
+
* const subChannel = await getSubChannel('foobar')
|
|
26697
|
+
* ```
|
|
26698
|
+
*
|
|
26699
|
+
* Fetches a {@link Amity.SubChannel} object
|
|
26700
|
+
*
|
|
26701
|
+
* @param subChannelId the ID of the {@link Amity.SubChannel} to fetch
|
|
26702
|
+
* @returns the associated {@link Amity.SubChannel} object
|
|
26703
|
+
*
|
|
26704
|
+
* @category Channel API
|
|
26705
|
+
* @async
|
|
26706
|
+
*/
|
|
26707
|
+
const getSubChannel = async (subChannelId) => {
|
|
26708
|
+
const client = getActiveClient();
|
|
26709
|
+
client.log('channel/getSubChannel', subChannelId);
|
|
26710
|
+
isInTombstone('subChannel', subChannelId);
|
|
26711
|
+
try {
|
|
26712
|
+
const response = await client.http.get(`/api/v5/message-feeds/${encodeURIComponent(subChannelId)}`);
|
|
26713
|
+
const data = prepareSubChannelPayload(response.data);
|
|
26714
|
+
const cachedAt = client.cache && Date.now();
|
|
26715
|
+
if (client.cache)
|
|
26716
|
+
ingestInCache(data, { cachedAt });
|
|
26717
|
+
fireEvent('local.message-feed.fetched', data);
|
|
26718
|
+
return {
|
|
26719
|
+
data: data.messageFeeds[0],
|
|
26720
|
+
cachedAt,
|
|
26721
|
+
};
|
|
26722
|
+
}
|
|
26723
|
+
catch (error) {
|
|
26724
|
+
if (checkIfShouldGoesToTombstone(error === null || error === void 0 ? void 0 : error.code)) {
|
|
26725
|
+
pushToTombstone('subChannel', subChannelId);
|
|
26726
|
+
}
|
|
26727
|
+
throw error;
|
|
26728
|
+
}
|
|
26729
|
+
};
|
|
26730
|
+
/**
|
|
26731
|
+
* ```js
|
|
26732
|
+
* import { getSubChannel } from '@amityco/ts-sdk'
|
|
26733
|
+
* const subChannel = getSubChannel.locally('foobar')
|
|
26734
|
+
* ```
|
|
26735
|
+
*
|
|
26736
|
+
* Fetches a {@link Amity.SubChannel} object from cache
|
|
26737
|
+
*
|
|
26738
|
+
* @param subChannelId the ID of the {@link Amity.SubChannel} to fetch
|
|
26739
|
+
* @returns the associated {@link Amity.SubChannel} object
|
|
26740
|
+
*
|
|
26741
|
+
* @category Channel API
|
|
26742
|
+
*/
|
|
26743
|
+
getSubChannel.locally = (subChannelId) => {
|
|
26744
|
+
const client = getActiveClient();
|
|
26745
|
+
client.log('channel/getSubChannel.locally', subChannelId);
|
|
26746
|
+
if (!client.cache)
|
|
26747
|
+
return;
|
|
26748
|
+
const cached = pullFromCache(['subChannel', 'get', subChannelId]);
|
|
26749
|
+
if (!cached)
|
|
26750
|
+
return;
|
|
26751
|
+
return {
|
|
26752
|
+
data: cached.data,
|
|
26753
|
+
cachedAt: cached.cachedAt,
|
|
26754
|
+
};
|
|
26755
|
+
};
|
|
26756
|
+
|
|
26757
|
+
/**
|
|
26758
|
+
* ```js
|
|
26759
|
+
* import { createSubChannel } from '@amityco/ts-sdk'
|
|
26760
|
+
* const created = await createSubChannel({ channelId: 'foobar', name: 'foobar' })
|
|
26761
|
+
* ```
|
|
26762
|
+
*
|
|
26763
|
+
* Creates an {@link Amity.SubChannel}
|
|
26764
|
+
*
|
|
26765
|
+
* @param bundle The data necessary to create a new {@link Amity.SubChannel}
|
|
26766
|
+
* @returns The newly created {@link Amity.SubChannel}
|
|
26767
|
+
*
|
|
26768
|
+
* @category Channel API
|
|
26769
|
+
* @async
|
|
26770
|
+
*/
|
|
26771
|
+
const createSubChannel = async (bundle) => {
|
|
26772
|
+
const client = getActiveClient();
|
|
26773
|
+
client.log('user/createSubChannel', bundle);
|
|
26774
|
+
const response = await client.http.post('/api/v5/message-feeds', {
|
|
26775
|
+
channelId: bundle.channelId,
|
|
26776
|
+
name: bundle.displayName,
|
|
26777
|
+
});
|
|
26778
|
+
const data = prepareSubChannelPayload(response.data);
|
|
26779
|
+
const cachedAt = client.cache && Date.now();
|
|
26780
|
+
if (client.cache)
|
|
26781
|
+
ingestInCache(data, { cachedAt });
|
|
26782
|
+
fireEvent('message-feed.created', response.data);
|
|
26783
|
+
return {
|
|
26784
|
+
data: data.messageFeeds[0],
|
|
26785
|
+
cachedAt,
|
|
26786
|
+
};
|
|
26787
|
+
};
|
|
26788
|
+
|
|
26789
|
+
/**
|
|
26790
|
+
* ```js
|
|
26791
|
+
* import { updateSubChannel } from '@amityco/ts-sdk'
|
|
26792
|
+
* const updated = await updateSubChannel(subChannelId, { name: 'foobar' })
|
|
26793
|
+
* ```
|
|
26794
|
+
*
|
|
26795
|
+
* Updates an {@link Amity.SubChannel}
|
|
26796
|
+
*
|
|
26797
|
+
* @param subChannelId The ID of the {@link Amity.SubChannel} to edit
|
|
26798
|
+
* @param patch The patch data to apply
|
|
26799
|
+
* @returns the updated {@link Amity.SubChannel} object
|
|
26800
|
+
*
|
|
26801
|
+
* @category Channel API
|
|
26802
|
+
* @async
|
|
26803
|
+
*/
|
|
26804
|
+
const updateSubChannel = async (subChannelId, patch) => {
|
|
26805
|
+
const client = getActiveClient();
|
|
26806
|
+
client.log('channel/updateSubChannel', subChannelId, patch);
|
|
26807
|
+
const response = await client.http.put(`/api/v5/message-feeds/${encodeURIComponent(subChannelId)}`, { name: patch.displayName });
|
|
26808
|
+
const data = prepareSubChannelPayload(response.data);
|
|
26809
|
+
const cachedAt = client.cache && Date.now();
|
|
26810
|
+
if (client.cache)
|
|
26811
|
+
ingestInCache(data, { cachedAt });
|
|
26812
|
+
fireEvent('message-feed.updated', response.data);
|
|
26813
|
+
return {
|
|
26814
|
+
data: data.messageFeeds[0],
|
|
26815
|
+
cachedAt,
|
|
26816
|
+
};
|
|
26817
|
+
};
|
|
26818
|
+
|
|
26819
|
+
/**
|
|
26820
|
+
* ```js
|
|
26821
|
+
* import { deleteSubChannel } from '@amityco/ts-sdk'
|
|
26822
|
+
* const success = await deleteSubChannel('foobar')
|
|
26823
|
+
* ```
|
|
26824
|
+
*
|
|
26825
|
+
* Deletes a {@link Amity.SubChannel}
|
|
25370
26826
|
*
|
|
25371
|
-
* @param
|
|
25372
|
-
* @
|
|
25373
|
-
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the channel users
|
|
26827
|
+
* @param subChannelId The {@link Amity.SubChannel} ID to delete
|
|
26828
|
+
* @return A success boolean if the {@link Amity.SubChannel} was deleted
|
|
25374
26829
|
*
|
|
25375
|
-
* @category Channel
|
|
26830
|
+
* @category Channel API
|
|
26831
|
+
* @async
|
|
25376
26832
|
*/
|
|
25377
|
-
const
|
|
25378
|
-
const
|
|
25379
|
-
|
|
25380
|
-
|
|
26833
|
+
const deleteSubChannel = async (subChannelId, permanent = false) => {
|
|
26834
|
+
const client = getActiveClient();
|
|
26835
|
+
client.log('channel/deleteSubChannel', subChannelId);
|
|
26836
|
+
const subChannel = await getSubChannel(subChannelId);
|
|
26837
|
+
await client.http.delete(`/api/v5/message-feeds/${encodeURIComponent(subChannelId)}`, { params: { permanent } });
|
|
26838
|
+
const deleted = Object.assign(Object.assign({}, subChannel.data), { isDeleted: true, updatedAt: getFutureDate(subChannel.data.updatedAt) });
|
|
26839
|
+
if (permanent) {
|
|
26840
|
+
setTimeout(() => {
|
|
26841
|
+
pushToTombstone('subChannel', subChannelId);
|
|
26842
|
+
}, 0);
|
|
25381
26843
|
}
|
|
25382
|
-
|
|
25383
|
-
|
|
25384
|
-
|
|
25385
|
-
|
|
25386
|
-
|
|
25387
|
-
const disposers = [];
|
|
25388
|
-
const cacheKey = ['channelUsers', 'collection', { channelId: params.channelId }];
|
|
25389
|
-
const responder = (data) => {
|
|
25390
|
-
var _a, _b;
|
|
25391
|
-
let channelMembers = (_a = data.data
|
|
25392
|
-
.map(id => pullFromCache(['channelUsers', 'get', id]))
|
|
25393
|
-
.filter(Boolean)
|
|
25394
|
-
.map(({ data }) => data)) !== null && _a !== void 0 ? _a : [];
|
|
25395
|
-
channelMembers = filterByPropEquality(channelMembers, 'roles', params.roles);
|
|
25396
|
-
if (params.memberships) {
|
|
25397
|
-
/*
|
|
25398
|
-
* even though membership includes muted as a possible value
|
|
25399
|
-
* when querying the server.
|
|
25400
|
-
* Muted is specified under seperarte property namely isMuted
|
|
25401
|
-
* Hence why I've seperately checked for it's equality
|
|
25402
|
-
*/
|
|
25403
|
-
if (params.memberships.some(m => ['member', 'banned'].includes(m))) {
|
|
25404
|
-
channelMembers = filterByPropInclusion(channelMembers, 'membership', params.memberships);
|
|
25405
|
-
}
|
|
25406
|
-
if (params.memberships.includes('muted')) {
|
|
25407
|
-
channelMembers = filterByPropEquality(channelMembers, 'isMuted', true);
|
|
25408
|
-
}
|
|
25409
|
-
}
|
|
25410
|
-
if (params.search) {
|
|
25411
|
-
const containsMatcher = new RegExp(params.search);
|
|
25412
|
-
channelMembers = channelMembers.filter(m => m.userId.match(containsMatcher));
|
|
25413
|
-
}
|
|
25414
|
-
// sort, 'lastCreated' is the default sort order
|
|
25415
|
-
const sortBy = params.sortBy ? params.sortBy : 'lastCreated';
|
|
25416
|
-
channelMembers = channelMembers.sort(sortBy === 'lastCreated' ? sortByLastCreated : sortByFirstCreated);
|
|
25417
|
-
callback({
|
|
25418
|
-
onNextPage: onFetch,
|
|
25419
|
-
data: channelMembers,
|
|
25420
|
-
hasNextPage: !!((_b = data.params) === null || _b === void 0 ? void 0 : _b.page),
|
|
25421
|
-
loading: data.loading,
|
|
25422
|
-
error: data.error,
|
|
25423
|
-
});
|
|
25424
|
-
};
|
|
25425
|
-
const realtimeRouter = (channelMember, action) => {
|
|
25426
|
-
var _a;
|
|
25427
|
-
const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
|
|
25428
|
-
if (!collection)
|
|
25429
|
-
return;
|
|
25430
|
-
const channelMemberCacheId = getResolver('channelUsers')({
|
|
25431
|
-
channelId: params.channelId,
|
|
25432
|
-
userId: channelMember.userId,
|
|
25433
|
-
});
|
|
25434
|
-
if (action === 'onMemberAdded') {
|
|
25435
|
-
collection.data = [...new Set([channelMemberCacheId, ...collection.data])];
|
|
25436
|
-
}
|
|
25437
|
-
if (action === 'onMemberRemoved') {
|
|
25438
|
-
collection.data = collection.data.filter(m => m !== channelMemberCacheId);
|
|
25439
|
-
}
|
|
25440
|
-
pushToCache(cacheKey, collection);
|
|
25441
|
-
responder(collection);
|
|
25442
|
-
};
|
|
25443
|
-
const onFetch = () => {
|
|
25444
|
-
var _a, _b, _c;
|
|
25445
|
-
const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
|
|
25446
|
-
const channelMembers = (_b = collection === null || collection === void 0 ? void 0 : collection.data) !== null && _b !== void 0 ? _b : [];
|
|
25447
|
-
if (channelMembers.length > 0 && !(collection === null || collection === void 0 ? void 0 : collection.params.page))
|
|
25448
|
-
return;
|
|
25449
|
-
const query = createQuery(queryChannelMembers, Object.assign(Object.assign({}, queryParams), { page: (_c = collection === null || collection === void 0 ? void 0 : collection.params.page) !== null && _c !== void 0 ? _c : { limit } }));
|
|
25450
|
-
runQuery(query, ({ data: result, error, loading, nextPage: page }) => {
|
|
25451
|
-
const data = {
|
|
25452
|
-
loading,
|
|
25453
|
-
error,
|
|
25454
|
-
params: { page },
|
|
25455
|
-
data: channelMembers,
|
|
25456
|
-
};
|
|
25457
|
-
if (result) {
|
|
25458
|
-
data.data = [...new Set([...channelMembers, ...result.map(getResolver('channelUsers'))])];
|
|
25459
|
-
}
|
|
25460
|
-
pushToCache(cacheKey, data);
|
|
25461
|
-
responder(data);
|
|
25462
|
-
}, queryOptions(policy));
|
|
25463
|
-
};
|
|
25464
|
-
disposers.push(onChannelMemberAdded((_, member) => realtimeRouter(member, 'onMemberAdded')), onChannelMemberRemoved((_, member) => realtimeRouter(member, 'onMemberRemoved')), onChannelMemberBanned((_, member) => realtimeRouter(member, 'onChannelMemberBanned')), onChannelMemberUnbanned((_, member) => realtimeRouter(member, 'onChannelMemberUnbanned')));
|
|
25465
|
-
onFetch();
|
|
25466
|
-
return () => {
|
|
25467
|
-
log(`liveChannelMembers(tmpid: ${timestamp}) > dispose`);
|
|
25468
|
-
disposers.forEach(fn => fn());
|
|
25469
|
-
};
|
|
26844
|
+
else {
|
|
26845
|
+
upsertInCache(['subChannel', 'get', subChannelId], deleted);
|
|
26846
|
+
}
|
|
26847
|
+
fireEvent('local.message-feed.deleted', { messageFeeds: [deleted] });
|
|
26848
|
+
return deleted;
|
|
25470
26849
|
};
|
|
25471
26850
|
|
|
25472
26851
|
/**
|
|
25473
26852
|
* ```js
|
|
25474
|
-
* import {
|
|
25475
|
-
* const dispose =
|
|
26853
|
+
* import { onChannelSubCreated } from '@amityco/ts-sdk'
|
|
26854
|
+
* const dispose = onSubChannelCreated(subChannel => {
|
|
25476
26855
|
* // ...
|
|
25477
26856
|
* })
|
|
25478
26857
|
* ```
|
|
25479
26858
|
*
|
|
25480
|
-
* Fired when
|
|
26859
|
+
* Fired when any {@link Amity.SubChannel} have been created
|
|
25481
26860
|
*
|
|
25482
26861
|
* @param callback The function to call when the event was fired
|
|
25483
26862
|
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
25484
26863
|
*
|
|
25485
|
-
* @category
|
|
26864
|
+
* @category Channel Events
|
|
25486
26865
|
*/
|
|
25487
|
-
const
|
|
26866
|
+
const onSubChannelCreated = (callback) => {
|
|
25488
26867
|
const client = getActiveClient();
|
|
25489
|
-
const filter = (
|
|
26868
|
+
const filter = (rawPayload) => {
|
|
26869
|
+
const payload = prepareSubChannelPayload(rawPayload);
|
|
25490
26870
|
ingestInCache(payload);
|
|
25491
|
-
callback(payload.
|
|
26871
|
+
callback(payload.messageFeeds[0]);
|
|
25492
26872
|
};
|
|
25493
|
-
return createEventSubscriber(client, '
|
|
26873
|
+
return createEventSubscriber(client, 'onSubChannelCreated', 'message-feed.created', filter);
|
|
25494
26874
|
};
|
|
25495
26875
|
|
|
25496
26876
|
/**
|
|
25497
26877
|
* ```js
|
|
25498
|
-
* import {
|
|
25499
|
-
* const dispose =
|
|
26878
|
+
* import { onSubChannelUpdated } from '@amityco/ts-sdk'
|
|
26879
|
+
* const dispose = onSubChannelUpdated(subChannel => {
|
|
25500
26880
|
* // ...
|
|
25501
26881
|
* })
|
|
25502
26882
|
* ```
|
|
25503
26883
|
*
|
|
25504
|
-
* Fired when
|
|
26884
|
+
* Fired when any {@link Amity.SubChannel} have been updated
|
|
25505
26885
|
*
|
|
25506
26886
|
* @param callback The function to call when the event was fired
|
|
25507
26887
|
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
25508
26888
|
*
|
|
25509
|
-
* @category
|
|
26889
|
+
* @category Channel Events
|
|
25510
26890
|
*/
|
|
25511
|
-
const
|
|
26891
|
+
const onSubChannelUpdated = (callback) => {
|
|
25512
26892
|
const client = getActiveClient();
|
|
25513
|
-
const filter = (
|
|
26893
|
+
const filter = (rawPayload) => {
|
|
26894
|
+
const payload = prepareSubChannelPayload(rawPayload);
|
|
25514
26895
|
ingestInCache(payload);
|
|
25515
|
-
|
|
25516
|
-
// if payload does not have myReactions
|
|
25517
|
-
if (!(message === null || message === void 0 ? void 0 : message.myReactions)) {
|
|
25518
|
-
prepareMessagePayloadForCache(message);
|
|
25519
|
-
}
|
|
25520
|
-
callback(message);
|
|
26896
|
+
callback(payload.messageFeeds[0]);
|
|
25521
26897
|
};
|
|
25522
|
-
return createEventSubscriber(client, '
|
|
26898
|
+
return createEventSubscriber(client, 'onSubChannelUpdated', 'message-feed.updated', filter);
|
|
25523
26899
|
};
|
|
25524
26900
|
|
|
25525
26901
|
/**
|
|
25526
26902
|
* ```js
|
|
25527
|
-
* import {
|
|
25528
|
-
* const dispose =
|
|
26903
|
+
* import { onSubChannelDeleted } from '@amityco/ts-sdk'
|
|
26904
|
+
* const dispose = onSubChannelDeleted(subChannel => {
|
|
25529
26905
|
* // ...
|
|
25530
26906
|
* })
|
|
25531
26907
|
* ```
|
|
25532
26908
|
*
|
|
25533
|
-
* Fired when
|
|
26909
|
+
* Fired when any {@link Amity.SubChannel} have been deleted
|
|
25534
26910
|
*
|
|
25535
26911
|
* @param callback The function to call when the event was fired
|
|
25536
26912
|
* @returns an {@link Amity.Unsubscriber} function to stop listening
|
|
25537
26913
|
*
|
|
25538
|
-
* @category
|
|
26914
|
+
* @category Channel Events
|
|
25539
26915
|
*/
|
|
25540
|
-
const
|
|
26916
|
+
const onSubChannelDeleted = (callback) => {
|
|
25541
26917
|
const client = getActiveClient();
|
|
25542
|
-
const filter = (
|
|
26918
|
+
const filter = (rawPayload) => {
|
|
26919
|
+
const payload = prepareSubChannelPayload(rawPayload);
|
|
25543
26920
|
ingestInCache(payload);
|
|
25544
|
-
callback(payload.
|
|
26921
|
+
callback(payload.messageFeeds[0]);
|
|
26922
|
+
};
|
|
26923
|
+
const disposers = [
|
|
26924
|
+
createEventSubscriber(client, 'onSubChannelDeleted', 'message-feed.deleted', filter),
|
|
26925
|
+
createEventSubscriber(client, 'local.message-feed.deleted', 'local.message-feed.deleted', payload => callback(payload.messageFeeds[0])),
|
|
26926
|
+
];
|
|
26927
|
+
return () => {
|
|
26928
|
+
disposers.forEach(fn => fn());
|
|
26929
|
+
};
|
|
26930
|
+
};
|
|
26931
|
+
|
|
26932
|
+
/** @hidden */
|
|
26933
|
+
const onSubChannelFetched = (callback) => {
|
|
26934
|
+
return createEventSubscriber(getActiveClient(), 'onSubChannelFetched', 'local.message-feed.fetched', payload => callback(payload.messageFeeds[0]));
|
|
26935
|
+
};
|
|
26936
|
+
|
|
26937
|
+
/**
|
|
26938
|
+
* ```js
|
|
26939
|
+
* import { liveSubChannel } from '@amityco/ts-sdk';
|
|
26940
|
+
*
|
|
26941
|
+
* let subChannel;
|
|
26942
|
+
*
|
|
26943
|
+
* const unsubscribe = liveSubChannel(subChannelId, response => {
|
|
26944
|
+
* subChannel = response.data;
|
|
26945
|
+
* });
|
|
26946
|
+
* ```
|
|
26947
|
+
*
|
|
26948
|
+
* Observe all mutation on a given {@link Amity.SubChannel}
|
|
26949
|
+
*
|
|
26950
|
+
* @param subChannelId the ID of the message to observe
|
|
26951
|
+
* @param callback the function to call when new data are available
|
|
26952
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the sub channel
|
|
26953
|
+
*
|
|
26954
|
+
* @category SubChannel Live Object
|
|
26955
|
+
*/
|
|
26956
|
+
const liveSubChannel = (subChannelId, callback) => {
|
|
26957
|
+
const { log, cache } = getActiveClient();
|
|
26958
|
+
const timestamp = Date.now();
|
|
26959
|
+
log(`liveSubChannel(tmpid: ${timestamp}) > listen`);
|
|
26960
|
+
if (!cache) {
|
|
26961
|
+
console.log(LIVE_OBJECT_ENABLE_CACHE_MESSAGE);
|
|
26962
|
+
}
|
|
26963
|
+
let subChannel;
|
|
26964
|
+
const disposers = [];
|
|
26965
|
+
const dispatcher = (data) => {
|
|
26966
|
+
subChannel = data.data;
|
|
26967
|
+
callback(data);
|
|
26968
|
+
};
|
|
26969
|
+
const realtimeRouter = (eventSubChannel) => {
|
|
26970
|
+
if (subChannelId !== eventSubChannel.subChannelId) {
|
|
26971
|
+
return;
|
|
26972
|
+
}
|
|
26973
|
+
if (subChannel && !hasUpdates(subChannel, eventSubChannel)) {
|
|
26974
|
+
return;
|
|
26975
|
+
}
|
|
26976
|
+
dispatcher({ loading: false, data: eventSubChannel, origin: 'event' });
|
|
26977
|
+
};
|
|
26978
|
+
const onFetch = () => {
|
|
26979
|
+
const query = createQuery(getSubChannel, subChannelId);
|
|
26980
|
+
runQuery(query, ({ error, data, loading, origin }) => {
|
|
26981
|
+
dispatcher({ error, data, loading, origin });
|
|
26982
|
+
if (error) {
|
|
26983
|
+
disposers.forEach(fn => fn());
|
|
26984
|
+
}
|
|
26985
|
+
});
|
|
26986
|
+
};
|
|
26987
|
+
disposers.push(onSubChannelFetched(realtimeRouter), onSubChannelUpdated(realtimeRouter), onSubChannelDeleted(realtimeRouter));
|
|
26988
|
+
onFetch();
|
|
26989
|
+
return () => {
|
|
26990
|
+
log(`liveSubChannel(tmpid: ${timestamp}) > dispose`);
|
|
26991
|
+
disposers.forEach(fn => fn());
|
|
26992
|
+
};
|
|
26993
|
+
};
|
|
26994
|
+
|
|
26995
|
+
/**
|
|
26996
|
+
* ```js
|
|
26997
|
+
* import { liveSubChannels } from '@amityco/ts-sdk';
|
|
26998
|
+
*
|
|
26999
|
+
* let subChannels = [];
|
|
27000
|
+
*
|
|
27001
|
+
* const unsubscribe = liveSubChannels({ channelId }, response => {
|
|
27002
|
+
* merge(subChannels, response.data);
|
|
27003
|
+
* });
|
|
27004
|
+
* ```
|
|
27005
|
+
*
|
|
27006
|
+
* Observe all mutations on a list of {@link Amity.SubChannel}s
|
|
27007
|
+
*
|
|
27008
|
+
* @param params for querying sub channels
|
|
27009
|
+
* @param callback the function to call when new data are available
|
|
27010
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the sub channels
|
|
27011
|
+
*
|
|
27012
|
+
* @category SubChannel Live Collection
|
|
27013
|
+
*/
|
|
27014
|
+
const liveSubChannels = (params, callback, config) => {
|
|
27015
|
+
const { log, cache } = getActiveClient();
|
|
27016
|
+
const timestamp = Date.now();
|
|
27017
|
+
log(`liveSubChannels(tmpid: ${timestamp}) > listen`);
|
|
27018
|
+
if (!cache) {
|
|
27019
|
+
console.log(ENABLE_CACHE_MESSAGE);
|
|
27020
|
+
}
|
|
27021
|
+
const { limit = COLLECTION_DEFAULT_PAGINATION_LIMIT } = params, queryParams = __rest(params, ["limit"]);
|
|
27022
|
+
const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config !== null && config !== void 0 ? config : {};
|
|
27023
|
+
let snapshot;
|
|
27024
|
+
const disposers = [];
|
|
27025
|
+
const responder = (data) => {
|
|
27026
|
+
var _a;
|
|
27027
|
+
let subChannels = data.data;
|
|
27028
|
+
if (params.isDeleted !== undefined) {
|
|
27029
|
+
subChannels = filterByPropEquality(subChannels, 'isDeleted', params.isDeleted);
|
|
27030
|
+
}
|
|
27031
|
+
subChannels.sort(sortByLastActivity);
|
|
27032
|
+
callback({
|
|
27033
|
+
onNextPage: onFetch,
|
|
27034
|
+
data: subChannels,
|
|
27035
|
+
hasNextPage: !!((_a = data.params) === null || _a === void 0 ? void 0 : _a.page),
|
|
27036
|
+
loading: data.loading,
|
|
27037
|
+
error: data.error,
|
|
27038
|
+
});
|
|
27039
|
+
};
|
|
27040
|
+
const realtimeRouter = (action) => (subChannel) => {
|
|
27041
|
+
if (params.channelId !== subChannel.channelId ||
|
|
27042
|
+
!snapshot ||
|
|
27043
|
+
!isCollectionOutdated(snapshot.data, subChannel, action)) {
|
|
27044
|
+
return;
|
|
27045
|
+
}
|
|
27046
|
+
if (action === 'onCreate') {
|
|
27047
|
+
snapshot.data = [subChannel, ...snapshot.data];
|
|
27048
|
+
}
|
|
27049
|
+
else {
|
|
27050
|
+
snapshot.data = snapshot.data.map(x => x.subChannelId === subChannel.subChannelId ? subChannel : x);
|
|
27051
|
+
}
|
|
27052
|
+
responder(snapshot);
|
|
27053
|
+
};
|
|
27054
|
+
const onFetch = () => {
|
|
27055
|
+
var _a;
|
|
27056
|
+
const subChannels = snapshot ? snapshot.data : [];
|
|
27057
|
+
if (subChannels.length > 0 && !(snapshot === null || snapshot === void 0 ? void 0 : snapshot.params.page)) {
|
|
27058
|
+
return;
|
|
27059
|
+
}
|
|
27060
|
+
const query = createQuery(querySubChannels, Object.assign(Object.assign({}, queryParams), { page: (_a = snapshot === null || snapshot === void 0 ? void 0 : snapshot.params.page) !== null && _a !== void 0 ? _a : { limit } }));
|
|
27061
|
+
runQuery(query, ({ data: result, error, loading, nextPage: page }) => {
|
|
27062
|
+
snapshot = {
|
|
27063
|
+
loading,
|
|
27064
|
+
error,
|
|
27065
|
+
params: { page },
|
|
27066
|
+
data: result ? merge(subChannels, result) : subChannels,
|
|
27067
|
+
};
|
|
27068
|
+
responder(snapshot);
|
|
27069
|
+
}, queryOptions(policy));
|
|
27070
|
+
};
|
|
27071
|
+
disposers.push(onSubChannelCreated(realtimeRouter('onCreate')), onSubChannelDeleted(realtimeRouter('onDelete')), onSubChannelFetched(realtimeRouter('onFetch')), onSubChannelUpdated(realtimeRouter('onUpdate')));
|
|
27072
|
+
onFetch();
|
|
27073
|
+
return () => {
|
|
27074
|
+
log(`liveSubChannels(tmpid: ${timestamp}) > dispose`);
|
|
27075
|
+
disposers.forEach(fn => fn());
|
|
25545
27076
|
};
|
|
25546
|
-
return createEventSubscriber(client, 'message/onMessageDeleted', 'v3.message.didDelete', filter);
|
|
25547
27077
|
};
|
|
25548
27078
|
|
|
25549
27079
|
/**
|
|
@@ -25567,46 +27097,50 @@ const onMessageFetched = (callback) => {
|
|
|
25567
27097
|
ingestInCache(payload);
|
|
25568
27098
|
callback(payload.messages[0]);
|
|
25569
27099
|
};
|
|
25570
|
-
return createEventSubscriber(client, 'message/onMessageFetched', '
|
|
27100
|
+
return createEventSubscriber(client, 'message/onMessageFetched', 'local.message.fetched', filter);
|
|
25571
27101
|
};
|
|
25572
27102
|
|
|
25573
27103
|
/**
|
|
25574
27104
|
* ```js
|
|
25575
|
-
* import { liveMessage } from '@amityco/ts-sdk'
|
|
27105
|
+
* import { liveMessage } from '@amityco/ts-sdk';
|
|
25576
27106
|
*
|
|
25577
|
-
* let
|
|
25578
|
-
*
|
|
27107
|
+
* let message;
|
|
27108
|
+
*
|
|
27109
|
+
* const unsubscribe = liveMessage(messageId, response => {
|
|
27110
|
+
* message = response.data;
|
|
27111
|
+
* });
|
|
25579
27112
|
* ```
|
|
25580
27113
|
*
|
|
25581
27114
|
* Observe all mutation on a given {@link Amity.Message}
|
|
25582
27115
|
*
|
|
25583
27116
|
* @param messageId the ID of the message to observe
|
|
25584
27117
|
* @param callback the function to call when new data are available
|
|
25585
|
-
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the
|
|
27118
|
+
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the message
|
|
25586
27119
|
*
|
|
25587
27120
|
* @category Message Live Object
|
|
25588
27121
|
*/
|
|
25589
27122
|
const liveMessage = (messageId, callback) => {
|
|
25590
27123
|
const { log, cache } = getActiveClient();
|
|
25591
|
-
|
|
25592
|
-
|
|
27124
|
+
const timestamp = Date.now();
|
|
27125
|
+
log(`liveMessage(tmpid: ${timestamp}) > listen`);
|
|
25593
27126
|
if (!cache) {
|
|
25594
27127
|
console.log(LIVE_OBJECT_ENABLE_CACHE_MESSAGE);
|
|
25595
27128
|
}
|
|
25596
|
-
|
|
25597
|
-
|
|
27129
|
+
let message;
|
|
27130
|
+
let isUnsyncedObject = false;
|
|
25598
27131
|
const disposers = [];
|
|
25599
27132
|
const dispatcher = (data) => {
|
|
25600
|
-
var _a;
|
|
25601
|
-
// we check updatedAt to see if the current snapshot is new so
|
|
25602
|
-
// we won't send old data back, only fresh new data
|
|
25603
|
-
if (message && ((_a = data.data) === null || _a === void 0 ? void 0 : _a.updatedAt) === (message === null || message === void 0 ? void 0 : message.updatedAt) && data.origin !== 'server')
|
|
25604
|
-
return;
|
|
25605
|
-
callback(data);
|
|
25606
27133
|
message = data.data;
|
|
27134
|
+
callback(data);
|
|
25607
27135
|
};
|
|
25608
|
-
const realtimeRouter = (
|
|
25609
|
-
|
|
27136
|
+
const realtimeRouter = (eventMessage) => {
|
|
27137
|
+
if (messageId !== eventMessage.messageId) {
|
|
27138
|
+
return;
|
|
27139
|
+
}
|
|
27140
|
+
if (message && !hasUpdates(message, eventMessage)) {
|
|
27141
|
+
return;
|
|
27142
|
+
}
|
|
27143
|
+
dispatcher({ loading: false, data: eventMessage, origin: 'event' });
|
|
25610
27144
|
};
|
|
25611
27145
|
const onFetch = () => {
|
|
25612
27146
|
const query = createQuery(getMessage, messageId, true);
|
|
@@ -25625,12 +27159,11 @@ const liveMessage = (messageId, callback) => {
|
|
|
25625
27159
|
dispatcher({ loading, data, origin, error });
|
|
25626
27160
|
}
|
|
25627
27161
|
if (error) {
|
|
25628
|
-
// Terminate observe functionality on error
|
|
25629
27162
|
disposers.forEach(fn => fn());
|
|
25630
27163
|
}
|
|
25631
27164
|
});
|
|
25632
27165
|
};
|
|
25633
|
-
disposers.push(onMessageFetched(
|
|
27166
|
+
disposers.push(onMessageFetched(realtimeRouter), onMessageUpdated(realtimeRouter), onMessageDeleted(realtimeRouter), onMessageFlagged(realtimeRouter), onMessageUnflagged(realtimeRouter), onMessageFlagCleared(realtimeRouter), onMessageReactionAdded(realtimeRouter), onMessageReactionRemoved(realtimeRouter));
|
|
25634
27167
|
onFetch();
|
|
25635
27168
|
return () => {
|
|
25636
27169
|
log(`liveMessage(tmpid: ${timestamp}) > dispose`);
|
|
@@ -25640,17 +27173,18 @@ const liveMessage = (messageId, callback) => {
|
|
|
25640
27173
|
|
|
25641
27174
|
/**
|
|
25642
27175
|
* ```js
|
|
25643
|
-
* import { liveMessages } from '@amityco/ts-sdk'
|
|
27176
|
+
* import { liveMessages } from '@amityco/ts-sdk';
|
|
25644
27177
|
*
|
|
25645
|
-
* let messages = []
|
|
25646
|
-
*
|
|
25647
|
-
*
|
|
25648
|
-
*
|
|
27178
|
+
* let messages = [];
|
|
27179
|
+
*
|
|
27180
|
+
* const unsubscribe = liveMessages({
|
|
27181
|
+
* subChannelId: Amity.SubChannel['subChannelId'];
|
|
27182
|
+
* }, response => merge(messages, response.data));
|
|
25649
27183
|
* ```
|
|
25650
27184
|
*
|
|
25651
27185
|
* Observe all mutations on a list of {@link Amity.Message} for a given target object
|
|
25652
27186
|
*
|
|
25653
|
-
* @param params for querying
|
|
27187
|
+
* @param params for querying messages from a sub channel
|
|
25654
27188
|
* @param callback the function to call when new data are available
|
|
25655
27189
|
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the messages
|
|
25656
27190
|
*
|
|
@@ -25667,7 +27201,7 @@ const liveMessages = (params, callback, config) => {
|
|
|
25667
27201
|
const limit = queryLimit !== null && queryLimit !== void 0 ? queryLimit : COLLECTION_DEFAULT_PAGINATION_LIMIT;
|
|
25668
27202
|
const { policy = COLLECTION_DEFAULT_CACHING_POLICY } = config !== null && config !== void 0 ? config : {};
|
|
25669
27203
|
const disposers = [];
|
|
25670
|
-
const cacheKey = ['message', 'collection', {
|
|
27204
|
+
const cacheKey = ['message', 'collection', { subChannelId: params.subChannelId }];
|
|
25671
27205
|
const responder = (data) => {
|
|
25672
27206
|
var _a, _b;
|
|
25673
27207
|
let messages = (_a = data.data
|
|
@@ -25687,10 +27221,10 @@ const liveMessages = (params, callback, config) => {
|
|
|
25687
27221
|
error: data.error,
|
|
25688
27222
|
});
|
|
25689
27223
|
};
|
|
25690
|
-
const realtimeRouter = (
|
|
27224
|
+
const realtimeRouter = (action) => (message) => {
|
|
25691
27225
|
var _a;
|
|
25692
27226
|
const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
|
|
25693
|
-
if (params.
|
|
27227
|
+
if (params.subChannelId !== message.subChannelId || !collection)
|
|
25694
27228
|
return;
|
|
25695
27229
|
if (action === 'onCreate') {
|
|
25696
27230
|
collection.data = [...new Set([message.messageId, ...collection.data])];
|
|
@@ -25722,7 +27256,7 @@ const liveMessages = (params, callback, config) => {
|
|
|
25722
27256
|
responder(data);
|
|
25723
27257
|
}, queryOptions(policy));
|
|
25724
27258
|
};
|
|
25725
|
-
disposers.push(onMessageFetched(
|
|
27259
|
+
disposers.push(onMessageFetched(realtimeRouter('onFetch')), onMessageCreated(realtimeRouter('onCreate')), onMessageUpdated(realtimeRouter('onUpdate')), onMessageDeleted(realtimeRouter('onDelete')), onMessageFlagged(realtimeRouter('onFlagged')), onMessageUnflagged(realtimeRouter('onUnflagged')), onMessageFlagCleared(realtimeRouter('onFlagCleared')), onMessageReactionAdded(realtimeRouter('onReactionAdded')), onMessageReactionRemoved(realtimeRouter('onReactionRemoved')));
|
|
25726
27260
|
onFetch();
|
|
25727
27261
|
return () => {
|
|
25728
27262
|
log(`liveMessages(tmpid: ${timestamp}) > dispose`);
|
|
@@ -25735,6 +27269,7 @@ const liveMessages = (params, callback, config) => {
|
|
|
25735
27269
|
* import { observeMessage } from '@amityco/ts-sdk'
|
|
25736
27270
|
*
|
|
25737
27271
|
* let message = {}
|
|
27272
|
+
*
|
|
25738
27273
|
* const unsub = observeMessage(messageId, updated => message = updated)
|
|
25739
27274
|
* ```
|
|
25740
27275
|
*
|
|
@@ -25781,25 +27316,26 @@ const observeMessage = (messageId, callback, policy = 'cache_then_server') => {
|
|
|
25781
27316
|
* import { observeMessages } from '@amityco/ts-sdk'
|
|
25782
27317
|
*
|
|
25783
27318
|
* let messages = []
|
|
25784
|
-
*
|
|
27319
|
+
*
|
|
27320
|
+
* const unsubscribe = observeMessages(channelId, message => merge(messages, message))
|
|
25785
27321
|
* ```
|
|
25786
27322
|
*
|
|
25787
|
-
* Observe all mutations on a list of {@link Amity.Message} for a given {@link Amity.
|
|
27323
|
+
* Observe all mutations on a list of {@link Amity.Message} for a given {@link Amity.SubChannel} object
|
|
25788
27324
|
*
|
|
25789
|
-
* @param
|
|
27325
|
+
* @param subChannelId the ID of the channel where to observe the messages
|
|
25790
27326
|
* @param callback the function to call when new data are available
|
|
25791
27327
|
* @returns An {@link Amity.Unsubscriber} function to run when willing to stop observing the messages
|
|
25792
27328
|
*
|
|
25793
27329
|
* @category Message Observer
|
|
25794
27330
|
*/
|
|
25795
|
-
const observeMessages = (
|
|
27331
|
+
const observeMessages = (subChannelId, callback) => {
|
|
25796
27332
|
const { log } = getActiveClient();
|
|
25797
27333
|
const timestamp = Date.now();
|
|
25798
27334
|
log(`observeMessages(tmpid: ${timestamp}) > listen`);
|
|
25799
27335
|
const disposers = [];
|
|
25800
27336
|
const router = (message, action) => {
|
|
25801
27337
|
var _a, _b, _c;
|
|
25802
|
-
if (((_a = message.data) === null || _a === void 0 ? void 0 : _a.
|
|
27338
|
+
if (((_a = message.data) === null || _a === void 0 ? void 0 : _a.subChannelId) !== subChannelId)
|
|
25803
27339
|
return;
|
|
25804
27340
|
if (callback instanceof Function)
|
|
25805
27341
|
return callback(message);
|
|
@@ -25862,22 +27398,23 @@ function getEventRelatedMember(event, payload) {
|
|
|
25862
27398
|
const createCommunityMemberEventSubscriber = (event, callback) => {
|
|
25863
27399
|
const client = getActiveClient();
|
|
25864
27400
|
const filter = (payload) => {
|
|
25865
|
-
const
|
|
27401
|
+
const communityPayload = unpackCommunityPayload(payload);
|
|
27402
|
+
const preparedPayload = prepareMembershipPayload(communityPayload, 'communityUsers');
|
|
25866
27403
|
if (!client.cache) {
|
|
25867
27404
|
// TODO: here we are missing specific properties here!
|
|
25868
|
-
callback(
|
|
27405
|
+
callback(preparedPayload.communities[0], getEventRelatedMember(event, preparedPayload));
|
|
25869
27406
|
}
|
|
25870
27407
|
else {
|
|
25871
|
-
ingestInCache(
|
|
27408
|
+
ingestInCache(preparedPayload);
|
|
25872
27409
|
const community = pullFromCache([
|
|
25873
27410
|
'community',
|
|
25874
27411
|
'get',
|
|
25875
|
-
|
|
27412
|
+
preparedPayload.communities[0].communityId,
|
|
25876
27413
|
]);
|
|
25877
27414
|
const member = pullFromCache([
|
|
25878
27415
|
'communityUsers',
|
|
25879
27416
|
'get',
|
|
25880
|
-
getResolver('communityUsers')(getEventRelatedMember(event,
|
|
27417
|
+
getResolver('communityUsers')(getEventRelatedMember(event, preparedPayload)),
|
|
25881
27418
|
]);
|
|
25882
27419
|
callback(community.data, member.data);
|
|
25883
27420
|
}
|
|
@@ -26916,7 +28453,7 @@ const liveFollowers = (params, callback, config) => {
|
|
|
26916
28453
|
error: data.error,
|
|
26917
28454
|
});
|
|
26918
28455
|
};
|
|
26919
|
-
const realtimeRouter = (
|
|
28456
|
+
const realtimeRouter = (action) => (followStatus) => {
|
|
26920
28457
|
var _a;
|
|
26921
28458
|
const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
|
|
26922
28459
|
if (params.userId !== followStatus.to || !collection)
|
|
@@ -26951,7 +28488,7 @@ const liveFollowers = (params, callback, config) => {
|
|
|
26951
28488
|
responder(data);
|
|
26952
28489
|
}, queryOptions(policy));
|
|
26953
28490
|
};
|
|
26954
|
-
disposers.push(onFollowerRequested(
|
|
28491
|
+
disposers.push(onFollowerRequested(realtimeRouter('onRequested')), onFollowRequestAccepted(realtimeRouter('onAccepted')), onFollowRequestDeclined(realtimeRouter('onDeclined')), onFollowRequestCanceled(realtimeRouter('onCanceled')), onUserFollowed(realtimeRouter('onFollowed')), onUserUnfollowed(realtimeRouter('onUnfollowed')), onFollowerDeleted(realtimeRouter('onDeleted')));
|
|
26955
28492
|
onFetch();
|
|
26956
28493
|
return () => {
|
|
26957
28494
|
log(`liveFollowers(tmpid: ${timestamp}) > dispose`);
|
|
@@ -27006,7 +28543,7 @@ const liveFollowings = (params, callback, config) => {
|
|
|
27006
28543
|
error: data.error,
|
|
27007
28544
|
});
|
|
27008
28545
|
};
|
|
27009
|
-
const realtimeRouter = (
|
|
28546
|
+
const realtimeRouter = (action) => (followStatus) => {
|
|
27010
28547
|
var _a;
|
|
27011
28548
|
const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
|
|
27012
28549
|
if (params.userId !== followStatus.from || !collection)
|
|
@@ -27041,7 +28578,7 @@ const liveFollowings = (params, callback, config) => {
|
|
|
27041
28578
|
responder(data);
|
|
27042
28579
|
}, queryOptions(policy));
|
|
27043
28580
|
};
|
|
27044
|
-
disposers.push(onFollowerRequested(
|
|
28581
|
+
disposers.push(onFollowerRequested(realtimeRouter('onRequested')), onFollowRequestAccepted(realtimeRouter('onAccepted')), onFollowRequestDeclined(realtimeRouter('onDeclined')), onFollowRequestCanceled(realtimeRouter('onCanceled')), onUserFollowed(realtimeRouter('onFollowed')), onUserUnfollowed(realtimeRouter('onUnfollowed')), onFollowerDeleted(realtimeRouter('onDeleted')));
|
|
27045
28582
|
onFetch();
|
|
27046
28583
|
return () => {
|
|
27047
28584
|
log(`liveFollowings(tmpid: ${timestamp}) > dispose`);
|
|
@@ -27179,6 +28716,9 @@ const livePosts = (params, callback, config) => {
|
|
|
27179
28716
|
posts = filterByPropEquality(posts, 'isDeleted', params.isDeleted);
|
|
27180
28717
|
const sortBy = params.sortBy ? params.sortBy : 'lastCreated';
|
|
27181
28718
|
posts = posts.sort(sortBy === 'lastCreated' ? sortByLastCreated : sortByFirstCreated);
|
|
28719
|
+
if (params.tags) {
|
|
28720
|
+
posts = posts.filter(p => { var _a; return (_a = p.tags) === null || _a === void 0 ? void 0 : _a.some(t => { var _a; return (_a = params.tags) === null || _a === void 0 ? void 0 : _a.includes(t); }); });
|
|
28721
|
+
}
|
|
27182
28722
|
callback({
|
|
27183
28723
|
onNextPage: onFetch,
|
|
27184
28724
|
data: posts,
|
|
@@ -27187,17 +28727,32 @@ const livePosts = (params, callback, config) => {
|
|
|
27187
28727
|
error: data.error,
|
|
27188
28728
|
});
|
|
27189
28729
|
};
|
|
27190
|
-
const realtimeRouter = (
|
|
28730
|
+
const realtimeRouter = (action) => (post) => {
|
|
27191
28731
|
var _a;
|
|
27192
28732
|
const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
|
|
27193
28733
|
if (params.targetId !== post.targetId || params.targetType !== post.targetType || !collection)
|
|
27194
28734
|
return;
|
|
27195
|
-
if (
|
|
28735
|
+
if (['onCreate', 'onApproved'].includes(action)) {
|
|
27196
28736
|
collection.data = [...new Set([post.postId, ...collection.data])];
|
|
27197
28737
|
}
|
|
27198
28738
|
else if (['onDelete', 'onDeclined'].includes(action)) {
|
|
27199
28739
|
collection.data = collection.data.filter(p => p !== post.postId);
|
|
28740
|
+
/*
|
|
28741
|
+
* if isDeleted is true, collection should add deleted items to the
|
|
28742
|
+
* begining of the collection
|
|
28743
|
+
*/
|
|
28744
|
+
if (params.isDeleted)
|
|
28745
|
+
collection.data.unshift(post.postId);
|
|
27200
28746
|
}
|
|
28747
|
+
/*
|
|
28748
|
+
* FIX-ME
|
|
28749
|
+
* This is a shortcoming as the feed object is not saved to the cache.
|
|
28750
|
+
* The responder should pull the feed from cache once available and should
|
|
28751
|
+
* match the collection to feedType param
|
|
28752
|
+
* https://ekoapp.atlassian.net/browse/ASC-9382
|
|
28753
|
+
*/
|
|
28754
|
+
if (params.feedType === 'reviewing' && action === 'onApproved')
|
|
28755
|
+
collection.data = collection.data.filter(p => p !== post.postId);
|
|
27201
28756
|
pushToCache(cacheKey, collection);
|
|
27202
28757
|
responder(collection);
|
|
27203
28758
|
};
|
|
@@ -27222,7 +28777,7 @@ const livePosts = (params, callback, config) => {
|
|
|
27222
28777
|
responder(data);
|
|
27223
28778
|
}, queryOptions(policy));
|
|
27224
28779
|
};
|
|
27225
|
-
disposers.push(onPostCreated(
|
|
28780
|
+
disposers.push(onPostCreated(realtimeRouter('onCreate')), onPostApproved(realtimeRouter('onApproved')), onPostDeclined(realtimeRouter('onDeclined')), onPostDeleted(realtimeRouter('onDelete')), onPostUpdated(realtimeRouter('onUpdate')), onPostFlagged(realtimeRouter('onFlagged')), onPostUnflagged(realtimeRouter('onUnflagged')), onPostReactionAdded(realtimeRouter('onReactionAdded')), onPostReactionRemoved(realtimeRouter('onReactionRemoved')));
|
|
27226
28781
|
onFetch();
|
|
27227
28782
|
return () => {
|
|
27228
28783
|
log(`livePosts(tmpid: ${timestamp}) > dispose`);
|
|
@@ -27364,7 +28919,7 @@ const liveComments = (params, callback, config) => {
|
|
|
27364
28919
|
error: data.error,
|
|
27365
28920
|
});
|
|
27366
28921
|
};
|
|
27367
|
-
const realtimeRouter = (
|
|
28922
|
+
const realtimeRouter = (action) => (comment) => {
|
|
27368
28923
|
var _a;
|
|
27369
28924
|
const collection = (_a = pullFromCache(cacheKey)) === null || _a === void 0 ? void 0 : _a.data;
|
|
27370
28925
|
if (params.referenceId !== comment.referenceId ||
|
|
@@ -27403,7 +28958,7 @@ const liveComments = (params, callback, config) => {
|
|
|
27403
28958
|
responder(data);
|
|
27404
28959
|
}, queryOptions(policy));
|
|
27405
28960
|
};
|
|
27406
|
-
disposers.push(onCommentCreated(
|
|
28961
|
+
disposers.push(onCommentCreated(realtimeRouter('onCreate')), onCommentUpdated(realtimeRouter('onUpdate')), onCommentDeleted(realtimeRouter('onDelete')), onCommentFlagged(realtimeRouter('onFlagged')), onCommentUnflagged(realtimeRouter('onUnflagged')), onCommentReactionAdded(realtimeRouter('onReactionAdded')), onCommentReactionRemoved(realtimeRouter('onReactionRemoved')));
|
|
27407
28962
|
onFetch();
|
|
27408
28963
|
return () => {
|
|
27409
28964
|
log(`liveComments(tmpid: ${timestamp}) > dispose`);
|
|
@@ -27989,10 +29544,10 @@ const onStreamRecorded = (callback) => {
|
|
|
27989
29544
|
*/
|
|
27990
29545
|
const createUserToken = async (apiKey, apiRegion, params) => {
|
|
27991
29546
|
const deviceId = getDeviceId();
|
|
27992
|
-
const deviceInfo =
|
|
29547
|
+
const deviceInfo = getDeviceInfo();
|
|
27993
29548
|
const http = createHttpTransport(computeUrl('http', apiRegion));
|
|
27994
|
-
const { data } = await http.post('/api/
|
|
29549
|
+
const { data } = await http.post('/api/v4/sessions', Object.assign(Object.assign({}, params), { deviceId, deviceInfo: Object.assign(Object.assign({}, deviceInfo), { model: 'token management API on TS-SDK' }) }), { headers: { 'X-API-Key': apiKey } });
|
|
27995
29550
|
return { accessToken: data.accessToken };
|
|
27996
29551
|
};
|
|
27997
29552
|
|
|
27998
|
-
export { API_REGIONS, CommunityPostSettingMaps, CommunityPostSettings, ContentFeedType, DefaultCommunityPostSetting, FileType, MessageContentType, PostContentType, SubscriptionLevels, VERSION$1 as VERSION, VideoResolution, VideoSize, VideoTranscodingStatus, acceptFollower, addChannelMembers, addChannelMembersRole, addCommunityMembers, addCommunityMembersRoles, addReaction, approvePost, backupCache, banChannelMembers, closePoll, connectClient, createChannel, createClient, createComment, createCommunity, createFile, createImage, createMessage, createPoll, createPost, createQuery, createReport, createStream, createUserToken, createVideo, declineFollower, declinePost, deleteChannel, deleteComment, deleteCommunity, deleteFile, deleteMessage, deletePoll, deletePost, deleteReport, deleteStream, disableCache, disconnectClient, disposeStream, dropFromCache, enableCache, fileUrlWithSize, filterByChannelMembership, filterByPropEquality, filterByPropInclusion, follow, getActiveClient, getCategory, getChannel, getChannels, getComment, getCommentTopic, getComments, getCommunities, getCommunity, getCommunityTopic, getFeedSettings, getFile, getFollowInfo, getMessage, getMessages, getMyFollowersTopic, getMyFollowingsTopic, getNetworkTopic, getPoll, getPost, getPostTopic, getPosts, getRecommendedCommunities, getRole, getStream, getTopTrendingCommunities, getUser, getUserTopic, getUsers, isAfterBefore, isAfterBeforeRaw, isCachable, isConnected, isFetcher, isFresh, isLocal, isMutator, isOffline, isPaged, isReportedByMe, isSkipLimit, joinChannel, joinCommunity, leaveChannel, leaveCommunity, liveChannelMembers, liveChannels, liveComments, liveFollowers, liveFollowings, liveMessage, liveMessages, livePosts, liveReactions, liveUser, mergeInCache, observeChannel, observeChannels, observeComment, observeComments, observeCommunity, observeFile, observeFollowInfo, observeFollowers, observeFollowings, observeMessage, observeMessages, observePoll, observePost, observePosts, observeUser, onChannelCreated, onChannelDeleted, onChannelJoined, onChannelLeft, onChannelMemberAdded, onChannelMemberBanned, onChannelMemberRemoved, onChannelMemberUnbanned, onChannelUpdated, onClientBanned, onClientDisconnected, onCommentCreated, onCommentDeleted, onCommentFlagged, onCommentReactionAdded, onCommentReactionRemoved, onCommentUnflagged, onCommentUpdated, onCommunityCreated, onCommunityDeleted, onCommunityJoined, onCommunityLeft, onCommunityUpdated, onCommunityUserAdded, onCommunityUserBanned, onCommunityUserRemoved, onCommunityUserUnbanned, onConnectionError, onFollowInfoUpdated, onFollowRequestAccepted, onFollowRequestCanceled, onFollowRequestDeclined, onFollowerDeleted, onFollowerRequested, onMessageCreated, onMessageDeleted,
|
|
29553
|
+
export { API_REGIONS, CommunityPostSettingMaps, CommunityPostSettings, ContentFeedType, DefaultCommunityPostSetting, FileType, MessageContentType, PostContentType, SubscriptionLevels, VERSION$1 as VERSION, VideoResolution, VideoSize, VideoTranscodingStatus, acceptFollower, addChannelMembers, addChannelMembersRole, addCommunityMembers, addCommunityMembersRoles, addReaction, approvePost, backupCache, banChannelMembers, closePoll, connectClient, createChannel, createClient, createComment, createCommunity, createFile, createImage, createMessage, createPoll, createPost, createQuery, createReport, createStream, createSubChannel, createUserToken, createVideo, declineFollower, declinePost, deleteChannel, deleteComment, deleteCommunity, deleteFile, deleteMessage, deletePoll, deletePost, deleteReport, deleteStream, deleteSubChannel, disableCache, disconnectClient, disposeStream, dropFromCache, enableCache, fileUrlWithSize, filterByChannelMembership, filterByPropEquality, filterByPropInclusion, follow, getActiveClient, getCategory, getChannel, getChannelTopic, getChannels, getComment, getCommentTopic, getComments, getCommunities, getCommunity, getCommunityTopic, getFeedSettings, getFile, getFollowInfo, getMessage, getMessageTopic, getMessages, getMyFollowersTopic, getMyFollowingsTopic, getNetworkTopic, getPoll, getPost, getPostTopic, getPosts, getRecommendedCommunities, getRole, getSmartFeedChannelTopic, getSmartFeedMessageTopic, getSmartFeedSubChannelTopic, getStream, getSubChannel, getSubChannelTopic, getSubChannels, getTopTrendingCommunities, getUser, getUserTopic, getUsers, isAfterBefore, isAfterBeforeRaw, isCachable, isConnected, isFetcher, isFresh, isLocal, isMutator, isOffline, isPaged, isReportedByMe, isSkipLimit, joinChannel, joinCommunity, leaveChannel, leaveCommunity, liveChannel, liveChannelMembers, liveChannels, liveComments, liveFollowers, liveFollowings, liveMessage, liveMessages, livePosts, liveReactions, liveSubChannel, liveSubChannels, liveUser, mergeInCache, observeChannel, observeChannels, observeComment, observeComments, observeCommunity, observeFile, observeFollowInfo, observeFollowers, observeFollowings, observeMessage, observeMessages, observePoll, observePost, observePosts, observeUser, onChannelCreated, onChannelDeleted, onChannelJoined, onChannelLeft, onChannelMemberAdded, onChannelMemberBanned, onChannelMemberRemoved, onChannelMemberUnbanned, onChannelMuted, onChannelUpdated, onClientBanned, onClientDisconnected, onCommentCreated, onCommentDeleted, onCommentFlagged, onCommentReactionAdded, onCommentReactionRemoved, onCommentUnflagged, onCommentUpdated, onCommunityCreated, onCommunityDeleted, onCommunityJoined, onCommunityLeft, onCommunityUpdated, onCommunityUserAdded, onCommunityUserBanned, onCommunityUserRemoved, onCommunityUserUnbanned, onConnectionError, onFollowInfoUpdated, onFollowRequestAccepted, onFollowRequestCanceled, onFollowRequestDeclined, onFollowerDeleted, onFollowerRequested, onMessageCreated, onMessageDeleted, onMessageFlagCleared, onMessageFlagged, onMessageReactionAdded, onMessageReactionRemoved, onMessageUnflagged, onMessageUpdated, onPollDeleted, onPollUpdated, onPostApproved, onPostCreated, onPostDeclined, onPostDeleted, onPostFlagged, onPostReactionAdded, onPostReactionRemoved, onPostUnflagged, onPostUpdated, onReactionAdded, onReactionRemoved, onReactorAdded, onReactorRemoved, onSessionStateChange, onStreamRecorded, onStreamStarted, onStreamStopped, onSubChannelCreated, onSubChannelDeleted, onSubChannelUpdated, onUserDeleted, onUserFlagCleared, onUserFlagged, onUserFollowed, onUserUnflagged, onUserUnfollowed, onUserUpdated, pullFromCache, pushToCache, queryCache, queryCategories, queryChannelMembers, queryChannels, queryComments, queryCommunities, queryCommunityMembers, queryFollowers, queryFollowings, queryGlobalFeed, queryMessages, queryOptions, queryPosts, queryReactions, queryRoles, queryStreams, querySubChannels, queryUsers, removeChannelMembers, removeChannelMembersRole, removeCommunityMembers, removeCommunityMembersRoles, removeReaction, renewal, restoreCache, runQuery, sortByChannelSegment, sortByDisplayName, sortByFirstCreated, sortByLastActivity, sortByLastCreated, sortBySegmentNumber, startReading, stopReading, subscribeTopic, toPage, toPageRaw, toToken, unbanChannelMembers, unfollow, updateChannel, updateComment, updateCommunity, updateMessage, updatePost, updateStream, updateSubChannel, updateUser, upsertInCache, votePoll, wipeCache };
|