Skip to content

Commit

Permalink
Change User.active() to synchronous val User.active
Browse files Browse the repository at this point in the history
  • Loading branch information
wkal-pubnub committed Oct 11, 2024
1 parent 739e578 commit 7437e1d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions pubnub-chat-api/api/pubnub-chat-api.api
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ public abstract interface class com/pubnub/chat/User {
public abstract fun active ()Lcom/pubnub/kmp/PNFuture;
public abstract fun delete (Z)Lcom/pubnub/kmp/PNFuture;
public static synthetic fun delete$default (Lcom/pubnub/chat/User;ZILjava/lang/Object;)Lcom/pubnub/kmp/PNFuture;
public abstract fun getActive ()Z
public abstract fun getChannelRestrictions (Lcom/pubnub/chat/Channel;)Lcom/pubnub/kmp/PNFuture;
public abstract fun getChannelsRestrictions (Ljava/lang/Integer;Lcom/pubnub/api/models/consumer/objects/PNPage;Ljava/util/Collection;)Lcom/pubnub/kmp/PNFuture;
public static synthetic fun getChannelsRestrictions$default (Lcom/pubnub/chat/User;Ljava/lang/Integer;Lcom/pubnub/api/models/consumer/objects/PNPage;Ljava/util/Collection;ILjava/lang/Object;)Lcom/pubnub/kmp/PNFuture;
Expand Down
6 changes: 6 additions & 0 deletions pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ interface User {
*/
val lastActiveTimestamp: Long?

/**
* Indicates whether the user is currently (at the time of obtaining this `User` object) active.
*/
val active: Boolean

/**
* Updates the metadata of the user with the provided details.
*
Expand Down Expand Up @@ -187,6 +192,7 @@ interface User {
*
* @return [PNFuture] containing a boolean indicating whether the user is active.
*/
@Deprecated("Use non-async `active` property instead.", ReplaceWith("active"))
fun active(): PNFuture<Boolean>

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ data class UserImpl(
override val updated: String? = null,
override val lastActiveTimestamp: Long? = null,
) : User {
override val active: Boolean
get() {
if (!chat.config.storeUserActivityTimestamps) {
log.pnError(STORE_USER_ACTIVITY_INTERVAL_IS_FALSE)
}
return lastActiveTimestamp?.let { lastActiveTimestampNonNull ->
Clock.System.now() - Instant.fromEpochMilliseconds(lastActiveTimestampNonNull) <= chat.config.storeUserActivityInterval
} ?: false
}

override fun update(
name: String?,
externalId: String?,
Expand Down Expand Up @@ -168,16 +178,8 @@ data class UserImpl(
}
}

override fun active(): PNFuture<Boolean> {
if (!chat.config.storeUserActivityTimestamps) {
return log.logErrorAndReturnException(STORE_USER_ACTIVITY_INTERVAL_IS_FALSE).asFuture()
}
return (
lastActiveTimestamp?.let { lastActiveTimestampNonNull ->
Clock.System.now() - Instant.fromEpochMilliseconds(lastActiveTimestampNonNull) <= chat.config.storeUserActivityInterval
} ?: false
).asFuture()
}
@Deprecated("Use non-async `active` property instead.", replaceWith = ReplaceWith("active"))
override fun active(): PNFuture<Boolean> = active.asFuture()

override operator fun plus(update: PNUUIDMetadata): User {
return fromDTO(chat, toUUIDMetadata() + update)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class UserIntegrationTest : BaseChatIntegrationTest() {
@Test
fun calling_active_should_throw_exception_when_storeUserActivityTimestamps_is_false() = runTest {
val e = assertFailsWith<PubNubException> {
someUser.active().await()
someUser.active
}

assertEquals(
Expand All @@ -181,7 +181,7 @@ class UserIntegrationTest : BaseChatIntegrationTest() {
someUser = chatNew.currentUser

// when
val isUserActive = someUser.active().await()
val isUserActive = someUser.active

// then
assertTrue(isUserActive)
Expand All @@ -201,7 +201,7 @@ class UserIntegrationTest : BaseChatIntegrationTest() {
someUser = chatNew2.currentUser

// when
val isUserActive = someUser.active().await()
val isUserActive = someUser.active

// then
assertTrue(isUserActive)
Expand Down

0 comments on commit 7437e1d

Please sign in to comment.