From 96a7682a6ea3412cba4f454bd67d233b9bb4079c Mon Sep 17 00:00:00 2001 From: marcin-cebo <102806110+marcin-cebo@users.noreply.github.com> Date: Tue, 1 Oct 2024 17:13:07 +0200 Subject: [PATCH] Mc/refactor1 (#99) * Added integration tests. * Small fixes. * Handles a few ToDo-s --- .../kotlin/com/pubnub/chat/Channel.kt | 6 +++--- .../commonMain/kotlin/com/pubnub/chat/Chat.kt | 2 +- .../kotlin/com/pubnub/chat/Membership.kt | 3 +-- .../kotlin/com/pubnub/chat/Message.kt | 16 +++++++-------- .../kotlin/com/pubnub/chat/ThreadChannel.kt | 8 +++----- .../com/pubnub/chat/internal/ChatImpl.kt | 1 - .../chat/internal/channel/BaseChannel.kt | 4 ++-- .../chat/internal/message/BaseMessage.kt | 1 - .../kotlin/com/pubnub/chat/mediators.kt | 20 +++++++++++++++++++ 9 files changed, 38 insertions(+), 23 deletions(-) diff --git a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Channel.kt b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Channel.kt index 049785ad..921e9df9 100644 --- a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Channel.kt +++ b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Channel.kt @@ -194,8 +194,8 @@ interface Channel { /** * Returns historical messages for the [Channel] * - * @param startTimetoken - * @param endTimetoken + * @param startTimetoken Timetoken delimiting the start of a time slice (exclusive) to pull messages from. + * @param endTimetoken Timetoken delimiting the end of a time slice (inclusive) to pull messages from * @param count The maximum number of messages to retrieve. Default and maximum values is 25. * * @return [PNFuture] containing a list of messages with pagination information (isMore: Boolean). The result of @@ -443,7 +443,7 @@ interface Channel { * * @param callback Function that takes a single Channel object. It defines the custom behavior to be executed when detecting channel changes. * - * @return AutoCloseable interface that lets you stop receiving channel-related updates (objects events) + * @return [AutoCloseable] interface that lets you stop receiving channel-related updates (objects events) * and clean up resources by invoking the close() method. */ fun streamUpdates(callback: (channel: Channel?) -> Unit): AutoCloseable diff --git a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Chat.kt b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Chat.kt index 36f84e05..bbc6b221 100644 --- a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Chat.kt +++ b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Chat.kt @@ -341,7 +341,7 @@ interface Chat { * Lets you watch a selected channel for any new custom events emitted by your chat app. * * @param channelId Channel to listen for new events. - * @param customMethod An optional custom method for emitting events. If not provided, defaults to null. + * @param customMethod An optional custom method for emitting events. If not provided, defaults to EmitEventMethod.PUBLISH. * @param callback A function that is called with an Event as its parameter. * It defines the custom behavior to be executed whenever an event is detected on the specified channel. * diff --git a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Membership.kt b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Membership.kt index 35061647..27e79f23 100644 --- a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Membership.kt +++ b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Membership.kt @@ -56,6 +56,7 @@ interface Membership { * Updates the channel membership information for a given user. * * @param custom Any custom properties or metadata associated with the channel-user membership in a `Map`. Values must be scalar only; arrays or objects are not supported. App Context filtering language doesn’t support filtering by custom properties. + * @return A [PNFuture] that returns an updated [Membership] object. */ fun update(custom: CustomObject): PNFuture @@ -74,8 +75,6 @@ interface Membership { */ fun getUnreadMessagesCount(): PNFuture - // todo do we have test for this? - /** * You can receive updates when specific user-channel Membership object(s) are added, edited, or removed. * diff --git a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Message.kt b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Message.kt index cb85adf7..c59a8360 100644 --- a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Message.kt +++ b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Message.kt @@ -11,7 +11,9 @@ import com.pubnub.chat.types.QuotedMessage import com.pubnub.chat.types.TextLink import com.pubnub.kmp.PNFuture -// todo do we have tests for methods in this class? +/** + * Represents an object that refers to a single message in a chat. + */ interface Message { /** * Reference to the main Chat object. @@ -108,7 +110,7 @@ interface Message { * @param reaction Specific emoji added to the message. * @return Specifies if the current user added a given emoji to the message or not. */ - fun hasUserReaction(reaction: String): Boolean + fun hasUserReaction(reaction: String): Boolean // todo add test /** * Changes the content of the existing message to a new one. @@ -142,14 +144,14 @@ interface Message { * @param channelId Unique identifier of the channel to which you want to forward the message. You can forward a message to the same channel on which it was published or to any other. * @return [PNFuture] containing [PNPublishResult] that holds the timetoken of the forwarded message. */ - fun forward(channelId: String): PNFuture + fun forward(channelId: String): PNFuture // todo add test /** * Attach this message to its channel. * * @return `PNFuture` containing the updated channel metadata */ - fun pin(): PNFuture + fun pin(): PNFuture // todo add test // todo do we have test for this? @@ -175,7 +177,7 @@ interface Message { * * @return A pair of values containing an object with details about the result of the remove message action (indicating whether the message was successfully removed and potentially including additional metadata or information about the removal) and the updated channel object after the removal of the thread. */ - fun removeThread(): PNFuture> + fun removeThread(): PNFuture> // todo add test /** * Add or remove a reaction to a message. @@ -189,15 +191,13 @@ interface Message { */ fun toggleReaction(reaction: String): PNFuture - // todo do we want to have test for this? - /** * You can receive updates when this message and related message reactions are added, edited, or removed. * * @param callback Function that takes a single Message object. It defines the custom behavior to be executed when detecting message or message reaction changes. * @return Interface that lets you stop receiving message-related updates by invoking the close() method */ - fun streamUpdates(callback: (message: T) -> Unit): AutoCloseable + fun streamUpdates(callback: (message: T) -> Unit): AutoCloseable // todo add test /** * If you delete a message, you can restore its content together with the attached files using the restore() method. diff --git a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/ThreadChannel.kt b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/ThreadChannel.kt index 8af67d1a..b85f37a8 100644 --- a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/ThreadChannel.kt +++ b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/ThreadChannel.kt @@ -3,8 +3,6 @@ package com.pubnub.chat import com.pubnub.chat.types.HistoryResponse import com.pubnub.kmp.PNFuture -// todo add unit and integTests - /** * Represents an object that refers to a single thread (channel) in a chat. */ @@ -34,7 +32,7 @@ interface ThreadChannel : Channel { * * @return [PNFuture] containing [ThreadChannel] */ - override fun unpinMessage(): PNFuture + override fun unpinMessage(): PNFuture // todo add test /** * Returns historical messages for the [ThreadChannel] @@ -58,7 +56,7 @@ interface ThreadChannel : Channel { * * @return [PNFuture] containing the updated [Channel] with the pinned message metadata. */ - fun pinMessageToParentChannel(message: ThreadMessage): PNFuture + fun pinMessageToParentChannel(message: ThreadMessage): PNFuture // todo add test /** * Unpins the currently pinned message from the parent channel. This updates the parent channel's metadata by removing @@ -68,7 +66,7 @@ interface ThreadChannel : Channel { * * @return [PNFuture] containing the updated [Channel] after the message is unpinned. */ - fun unpinMessageFromParentChannel(): PNFuture + fun unpinMessageFromParentChannel(): PNFuture // todo add test companion object } diff --git a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/ChatImpl.kt b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/ChatImpl.kt index 53e135e3..8d70033d 100644 --- a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/ChatImpl.kt +++ b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/ChatImpl.kt @@ -140,7 +140,6 @@ class ChatImpl( init { KmLogging.setLogLevel(mapLogLevelFromConfigToKmLogging()) - // todo move this to config initialization or setters? if (config.storeUserActivityInterval < 60.seconds) { log.pnError(STORE_USER_ACTIVITY_INTERVAL_SHOULD_BE_AT_LEAST_1_MIN) } diff --git a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/channel/BaseChannel.kt b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/channel/BaseChannel.kt index 8f1dc92c..196c3cfb 100644 --- a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/channel/BaseChannel.kt +++ b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/channel/BaseChannel.kt @@ -356,7 +356,7 @@ abstract class BaseChannel( ).then { setMembershipsResult -> MembershipImpl.fromMembershipDTO(chat, setMembershipsResult.data.first(), user) }.thenAsync { membership -> - chat.pubNub.time().thenAsync { time -> // todo time API maybe removed from SDK soon + chat.pubNub.time().thenAsync { time -> membership.setLastReadMessageTimetoken(time.timetoken) } }.alsoAsync { @@ -459,7 +459,7 @@ abstract class BaseChannel( this.id, custom ) - ), // todo should null overwrite? wait for optionals? + ), // todo should null overwrite? Waiting for optionals? includeChannelDetails = PNChannelDetailsLevel.CHANNEL_WITH_CUSTOM, includeCustom = true, includeCount = true, diff --git a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/message/BaseMessage.kt b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/message/BaseMessage.kt index bc693729..5aee6185 100644 --- a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/message/BaseMessage.kt +++ b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/message/BaseMessage.kt @@ -257,7 +257,6 @@ abstract class BaseMessage( } private fun deleteThread(soft: Boolean): PNFuture { - // todo check on server, discuss with Team if (hasThread) { return getThread().thenAsync { it.delete(soft) diff --git a/src/commonMain/kotlin/com/pubnub/chat/mediators.kt b/src/commonMain/kotlin/com/pubnub/chat/mediators.kt index d4ec544a..59fb55dc 100644 --- a/src/commonMain/kotlin/com/pubnub/chat/mediators.kt +++ b/src/commonMain/kotlin/com/pubnub/chat/mediators.kt @@ -29,11 +29,31 @@ fun ThreadMessage.Companion.streamUpdatesOn( callback: (messages: Collection) -> Unit, ): AutoCloseable = BaseMessage.streamUpdatesOn(messages, callback) +/** + * Receives updates on list of [Channel] object. + * + * @param channels Collection of channels to get updates. + * @param callback Function that takes a single Channel object. It defines the custom behavior to be executed when + * detecting channel changes. + * + * @return [AutoCloseable] interface that lets you stop receiving channel-related updates (objects events) + * and clean up resources by invoking the close() method. + */ fun Channel.Companion.streamUpdatesOn( channels: Collection, callback: (channels: Collection) -> Unit, ): AutoCloseable = BaseChannel.streamUpdatesOn(channels, callback) +/** + * Receives updates on list of [Channel] object. + * + * @param channels Collection of channels to get updates. + * @param callback Function that takes a single Channel object. It defines the custom behavior to be executed when + * detecting channel changes. + * + * @return [AutoCloseable] interface that lets you stop receiving channel-related updates (objects events) + * and clean up resources by invoking the close() method. + */ fun ThreadChannel.Companion.streamUpdatesOn( channels: Collection, callback: (channels: Collection) -> Unit,