Skip to content

Commit

Permalink
Changes after review.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-cebo committed Aug 3, 2024
1 parent 54b2377 commit e165344
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 119 deletions.
19 changes: 15 additions & 4 deletions pubnub-chat-api/api/pubnub-chat-api.api
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,18 @@ public final class com/pubnub/chat/User$DefaultImpls {

public abstract interface class com/pubnub/chat/config/ChatConfiguration {
public abstract fun getCustomPayloads ()Lcom/pubnub/chat/config/CustomPayloads;
public abstract fun getErrorLogger ()Ljava/lang/Object;
public abstract fun getLogLevel ()Lcom/pubnub/chat/config/LogLevel;
public abstract fun getPushNotifications ()Lcom/pubnub/chat/config/PushNotificationsConfig;
public abstract fun getRateLimitFactor ()I
public abstract fun getRateLimitPerChannel ()Ljava/util/Map;
public abstract fun getSaveDebugLog ()Z
public abstract fun getStoreUserActivityInterval-UwyO8pc ()J
public abstract fun getStoreUserActivityTimestamps ()Z
public abstract fun getTypingTimeout-UwyO8pc ()J
}

public final class com/pubnub/chat/config/ChatConfigurationKt {
public static final fun ChatConfiguration-ZH2SSTU (ZJJZLcom/pubnub/chat/config/PushNotificationsConfig;ILjava/util/Map;Ljava/lang/Object;Lcom/pubnub/chat/config/CustomPayloads;)Lcom/pubnub/chat/config/ChatConfiguration;
public static synthetic fun ChatConfiguration-ZH2SSTU$default (ZJJZLcom/pubnub/chat/config/PushNotificationsConfig;ILjava/util/Map;Ljava/lang/Object;Lcom/pubnub/chat/config/CustomPayloads;ILjava/lang/Object;)Lcom/pubnub/chat/config/ChatConfiguration;
public static final fun ChatConfiguration-QkTvx9o (Lcom/pubnub/chat/config/LogLevel;JJZLcom/pubnub/chat/config/PushNotificationsConfig;ILjava/util/Map;Lcom/pubnub/chat/config/CustomPayloads;)Lcom/pubnub/chat/config/ChatConfiguration;
public static synthetic fun ChatConfiguration-QkTvx9o$default (Lcom/pubnub/chat/config/LogLevel;JJZLcom/pubnub/chat/config/PushNotificationsConfig;ILjava/util/Map;Lcom/pubnub/chat/config/CustomPayloads;ILjava/lang/Object;)Lcom/pubnub/chat/config/ChatConfiguration;
public static final fun RateLimitPerChannel-InTURus (JJJJ)Ljava/util/Map;
public static synthetic fun RateLimitPerChannel-InTURus$default (JJJJILjava/lang/Object;)Ljava/util/Map;
}
Expand All @@ -280,6 +279,18 @@ public final class com/pubnub/chat/config/CustomPayloads {
public final fun getGetMessageResponseBody ()Lkotlin/jvm/functions/Function1;
}

public final class com/pubnub/chat/config/LogLevel : java/lang/Enum {
public static final field DEBUG Lcom/pubnub/chat/config/LogLevel;
public static final field ERROR Lcom/pubnub/chat/config/LogLevel;
public static final field INFO Lcom/pubnub/chat/config/LogLevel;
public static final field OFF Lcom/pubnub/chat/config/LogLevel;
public static final field VERBOSE Lcom/pubnub/chat/config/LogLevel;
public static final field WARN Lcom/pubnub/chat/config/LogLevel;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public static fun valueOf (Ljava/lang/String;)Lcom/pubnub/chat/config/LogLevel;
public static fun values ()[Lcom/pubnub/chat/config/LogLevel;
}

public final class com/pubnub/chat/config/PushNotificationsConfig {
public fun <init> (ZLjava/lang/String;Lcom/pubnub/api/enums/PNPushType;Ljava/lang/String;Lcom/pubnub/api/enums/PNPushEnvironment;)V
public final fun getApnsEnvironment ()Lcom/pubnub/api/enums/PNPushEnvironment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import com.pubnub.chat.types.EventContent

class CustomPayloads(
val getMessagePublishBody: ((m: EventContent.TextMessageContent, channelId: String) -> Map<String, Any>)? = null,
val getMessageResponseBody: ((m: JsonElement) -> EventContent.TextMessageContent)? = null, // todo do we have tests that checks this functionality
val getMessageResponseBody: (
(m: JsonElement) -> EventContent.TextMessageContent
)? = null, // todo do we have tests that checks this functionality
val editMessageActionName: String? = null,
val deleteMessageActionName: String? = null,
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.pubnub.chat.config

enum class LogLevel {
OFF, ERROR, WARN, INFO, DEBUG, VERBOSE
OFF,
ERROR,
WARN,
INFO,
DEBUG,
VERBOSE
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class ChatImpl(
type: String?,
): PNFuture<User> {
if (!isValidId(id)) {
log.pnError(ID_IS_REQUIRED)
return log.logErrorAndReturnException(ID_IS_REQUIRED).asFuture()
}

return getUser(id).thenAsync { user: User? ->
Expand All @@ -203,14 +203,14 @@ class ChatImpl(

override fun getUser(userId: String): PNFuture<User?> {
if (!isValidId(userId)) {
log.pnError(ID_IS_REQUIRED)
return log.logErrorAndReturnException(ID_IS_REQUIRED).asFuture()
}

return pubNub.getUUIDMetadata(uuid = userId, includeCustom = true)
.then<PNUUIDMetadataResult, User?> { pnUUIDMetadataResult: PNUUIDMetadataResult ->
pnUUIDMetadataResult.data?.let { pnUUIDMetadata ->
UserImpl.fromDTO(this, pnUUIDMetadata)
} ?: log.pnError(PNUUID_METADATA_RESULT_IS_NULL)
} ?: log.pnError(PNUUID_METADATA_RESULT_IS_NULL)
}.catch {
if (it is PubNubException && it.statusCode == HTTP_ERROR_404) {
Result.success(null)
Expand Down Expand Up @@ -259,7 +259,7 @@ class ChatImpl(
type: String?
): PNFuture<User> {
if (!isValidId(id)) {
log.pnError(ID_IS_REQUIRED)
return log.logErrorAndReturnException(ID_IS_REQUIRED).asFuture()
}

return getUser(id).thenAsync { user ->
Expand All @@ -283,15 +283,13 @@ class ChatImpl(
} else {
performUserDelete(notNullUser)
}
} ?: run {
log.pnError(USER_NOT_EXIST)
}
} ?: log.pnError(USER_NOT_EXIST)
}
}

override fun wherePresent(userId: String): PNFuture<List<String>> {
if (!isValidId(userId)) {
log.pnError(ID_IS_REQUIRED)
return log.logErrorAndReturnException(ID_IS_REQUIRED).asFuture()
}

return pubNub.whereNow(uuid = userId).then { pnWhereNowResult ->
Expand All @@ -308,10 +306,10 @@ class ChatImpl(

override fun isPresent(userId: String, channelId: String): PNFuture<Boolean> {
if (!isValidId(userId)) {
log.pnError("$ID_IS_REQUIRED$channelId")
return log.logErrorAndReturnException(ID_IS_REQUIRED).asFuture()
}
if (!isValidId(channelId)) {
log.pnError("$CHANNEL_ID_IS_REQUIRED$channelId")
return log.logErrorAndReturnException(CHANNEL_ID_IS_REQUIRED).asFuture()
}

return pubNub.whereNow(uuid = userId).then { pnWhereNowResult ->
Expand All @@ -330,7 +328,7 @@ class ChatImpl(
status: String?
): PNFuture<Channel> {
if (!isValidId(id)) {
log.pnError(CHANNEL_ID_IS_REQUIRED)
return log.logErrorAndReturnException(CHANNEL_ID_IS_REQUIRED).asFuture()
}
return getChannel(id).thenAsync { channel: Channel? ->
if (channel != null) {
Expand Down Expand Up @@ -371,15 +369,13 @@ class ChatImpl(

override fun getChannel(channelId: String): PNFuture<Channel?> {
if (!isValidId(channelId)) {
log.pnError(CHANNEL_ID_IS_REQUIRED)
return log.logErrorAndReturnException(CHANNEL_ID_IS_REQUIRED).asFuture()
}
return pubNub.getChannelMetadata(channel = channelId)
.then<PNChannelMetadataResult, Channel?> { pnChannelMetadataResult: PNChannelMetadataResult ->
pnChannelMetadataResult.data?.let { pnChannelMetadata: PNChannelMetadata ->
ChannelImpl.fromDTO(this, pnChannelMetadata)
} ?: run {
log.pnError(PNCHANNEL_METADATA_IS_NULL)
}
} ?: log.pnError(PNCHANNEL_METADATA_IS_NULL)
}.catch { exception ->
if (exception is PubNubException && exception.statusCode == HTTP_ERROR_404) {
Result.success(null)
Expand All @@ -398,7 +394,7 @@ class ChatImpl(
type: ChannelType?
): PNFuture<Channel> {
if (!isValidId(id)) {
log.pnError(CHANNEL_ID_IS_REQUIRED)
return log.logErrorAndReturnException(CHANNEL_ID_IS_REQUIRED).asFuture()
}

return getChannel(id).thenAsync { channel: Channel? ->
Expand All @@ -412,7 +408,7 @@ class ChatImpl(

override fun deleteChannel(id: String, soft: Boolean): PNFuture<Channel> {
if (!isValidId(id)) {
log.pnError(CHANNEL_ID_IS_REQUIRED)
return log.logErrorAndReturnException(CHANNEL_ID_IS_REQUIRED).asFuture()
}

return getChannelData(id).thenAsync { channel: Channel ->
Expand All @@ -426,10 +422,10 @@ class ChatImpl(

override fun forwardMessage(message: Message, channelId: String): PNFuture<PNPublishResult> {
if (!isValidId(channelId)) {
log.pnError(CHANNEL_ID_IS_REQUIRED)
return log.logErrorAndReturnException(CHANNEL_ID_IS_REQUIRED).asFuture()
}
if (message.channelId == channelId) {
log.pnError(CANNOT_FORWARD_MESSAGE_TO_THE_SAME_CHANNEL)
return log.logErrorAndReturnException(CANNOT_FORWARD_MESSAGE_TO_THE_SAME_CHANNEL).asFuture()
}

val meta = message.meta?.toMutableMap() ?: mutableMapOf()
Expand Down Expand Up @@ -572,7 +568,7 @@ class ChatImpl(

override fun whoIsPresent(channelId: String): PNFuture<Collection<String>> {
if (!isValidId(channelId)) {
log.pnError(CHANNEL_ID_IS_REQUIRED)
return log.logErrorAndReturnException(CHANNEL_ID_IS_REQUIRED).asFuture()
}
return pubNub.hereNow(listOf(channelId)).then {
(it.channels[channelId]?.occupants?.map(PNHereNowOccupantData::uuid) ?: emptyList())
Expand Down Expand Up @@ -729,9 +725,7 @@ class ChatImpl(
pnMessageCountResult.channels.map { (channelId, messageCount) ->
val membershipMatchingChannel =
memberships.find { membership: Membership -> membership.channel.id == channelId }
?: run {
log.pnError("$CAN_NOT_FIND_CHANNEL_WITH_ID$channelId")
}
?: log.pnError("$CAN_NOT_FIND_CHANNEL_WITH_ID$channelId")
GetUnreadMessagesCounts(
channel = membershipMatchingChannel.channel,
membership = membershipMatchingChannel,
Expand Down Expand Up @@ -894,7 +888,7 @@ class ChatImpl(
count: Int
): PNFuture<GetCurrentUserMentionsResult> {
if (count > 100) {
log.pnError(COUNT_SHOULD_NOT_EXCEED_100)
return log.logErrorAndReturnException(COUNT_SHOULD_NOT_EXCEED_100).asFuture()
}
var isMore = false

Expand Down Expand Up @@ -960,9 +954,7 @@ class ChatImpl(
.then { pnChannelMetadataResult: PNChannelMetadataResult ->
pnChannelMetadataResult.data?.let { pnChannelMetadata ->
ChannelImpl.fromDTO(this, pnChannelMetadata)
} ?: run {
log.pnError(CHANNEL_META_DATA_IS_EMPTY)
}
} ?: log.pnError(CHANNEL_META_DATA_IS_EMPTY)
}.catch { exception ->
Result.failure(PubNubException(FAILED_TO_RETRIEVE_CHANNEL_DATA, exception))
}
Expand All @@ -983,9 +975,7 @@ class ChatImpl(
).then { pnUUIDMetadataResult ->
pnUUIDMetadataResult.data?.let { pnUUIDMetadata: PNUUIDMetadata ->
UserImpl.fromDTO(this, pnUUIDMetadata)
} ?: run {
log.pnError(PNUUID_METADATA_IS_NULL)
}
} ?: log.pnError(PNUUID_METADATA_IS_NULL)
}
}

Expand All @@ -1005,9 +995,7 @@ class ChatImpl(
).then { pnChannelMetadataResult ->
pnChannelMetadataResult.data?.let { pnChannelMetadata: PNChannelMetadata ->
ChannelImpl.fromDTO(this, pnChannelMetadata)
} ?: run {
log.pnError(PNCHANNEL_METADATA_IS_NULL)
}
} ?: log.pnError(PNCHANNEL_METADATA_IS_NULL)
}.catch { exception ->
Result.failure(PubNubException(FAILED_TO_SOFT_DELETE_CHANNEL, exception))
}
Expand Down Expand Up @@ -1035,9 +1023,7 @@ class ChatImpl(
).then { pnChannelMetadataResult ->
pnChannelMetadataResult.data?.let { pnChannelMetadata ->
ChannelImpl.fromDTO(this, pnChannelMetadata)
} ?: run {
log.pnError(NO_DATA_AVAILABLE_TO_CREATE_OR_UPDATE_CHANNEL)
}
} ?: log.pnError(NO_DATA_AVAILABLE_TO_CREATE_OR_UPDATE_CHANNEL)
}.catch { exception ->
Result.failure(PubNubException(FAILED_TO_CREATE_UPDATE_CHANNEL_DATA, exception))
}
Expand Down Expand Up @@ -1097,18 +1083,17 @@ class ChatImpl(

internal fun createThreadChannel(chat: ChatInternal, message: Message): PNFuture<ThreadChannel> {
if (message.channelId.startsWith(MESSAGE_THREAD_ID_PREFIX)) {
log.pnError(ONLY_ONE_LEVEL_OF_THREAD_NESTING_IS_ALLOWED)
return log.logErrorAndReturnException(ONLY_ONE_LEVEL_OF_THREAD_NESTING_IS_ALLOWED).asFuture()
}
if (message.deleted) {
log.pnError(YOU_CAN_NOT_CREATE_THREAD_ON_DELETED_MESSAGES)
return log.logErrorAndReturnException(YOU_CAN_NOT_CREATE_THREAD_ON_DELETED_MESSAGES).asFuture()
}

val threadChannelId =
getThreadId(message.channelId, message.timetoken)
return chat.getChannel(threadChannelId).thenAsync { it: Channel? ->
if (it != null) {
log.info { "Error in createThreadChannel: $THREAD_FOR_THIS_MESSAGE_ALREADY_EXISTS" }
return@thenAsync PubNubException(THREAD_FOR_THIS_MESSAGE_ALREADY_EXISTS).asFuture()
return@thenAsync log.logErrorAndReturnException(THREAD_FOR_THIS_MESSAGE_ALREADY_EXISTS).asFuture()
}
ThreadChannelImpl(
message,
Expand All @@ -1133,13 +1118,13 @@ class ChatImpl(

val actionTimetoken =
message.actions?.get("threadRootId")?.get(threadId)?.get(0)?.actionTimetoken
?: run {
return PubNubException(THERE_IS_NO_ACTION_TIMETOKEN_CORRESPONDING_TO_THE_THREAD).logErrorAndReturnException(log).asFuture()
}
?: return PubNubException(THERE_IS_NO_ACTION_TIMETOKEN_CORRESPONDING_TO_THE_THREAD).logErrorAndReturnException(
log
).asFuture()

return chat.getChannel(threadId).thenAsync { threadChannel ->
if (threadChannel == null) {
log.pnError(THERE_IS_NO_THREAD_WITH_ID)
log.pnError("$THERE_IS_NO_THREAD_WITH_ID$threadId")
}
awaitAll(
chat.pubNub.removeMessageAction(message.channelId, message.timetoken, actionTimetoken),
Expand Down Expand Up @@ -1179,7 +1164,7 @@ class ChatImpl(
runPeriodically(config.storeUserActivityInterval) {
saveTimeStampFunc().async { result: Result<Unit> ->
result.onFailure { e ->
log.e(err = e, msg = {e.message})
log.error(err = e, msg = { e.message })
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.pubnub.chat.internal

import com.pubnub.api.PubNubException
import com.pubnub.api.models.consumer.objects.member.PNMember
import com.pubnub.api.models.consumer.objects.membership.PNChannelDetailsLevel
import com.pubnub.api.models.consumer.objects.membership.PNChannelMembership
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.pubnub.chat.internal.error.PubNubErrorMessage.CAN_NOT_STREAM_USER_UPD
import com.pubnub.chat.internal.error.PubNubErrorMessage.MODERATION_CAN_BE_SET_ONLY_BY_CLIENT_HAVING_SECRET_KEY
import com.pubnub.chat.internal.error.PubNubErrorMessage.STORE_USER_ACTIVITY_INTERVAL_IS_FALSE
import com.pubnub.chat.internal.restrictions.RestrictionImpl
import com.pubnub.chat.internal.util.logErrorAndReturnException
import com.pubnub.chat.internal.util.pnError
import com.pubnub.chat.membership.IncludeParameters
import com.pubnub.chat.membership.MembershipsResponse
Expand Down Expand Up @@ -115,7 +116,7 @@ data class UserImpl(

override fun setRestrictions(channel: Channel, ban: Boolean, mute: Boolean, reason: String?): PNFuture<Unit> {
if (chat.pubNub.configuration.secretKey.isEmpty()) {
log.pnError(MODERATION_CAN_BE_SET_ONLY_BY_CLIENT_HAVING_SECRET_KEY)
return log.logErrorAndReturnException(MODERATION_CAN_BE_SET_ONLY_BY_CLIENT_HAVING_SECRET_KEY).asFuture()
}
return chat.setRestrictions(
Restriction(
Expand Down Expand Up @@ -170,13 +171,13 @@ data class UserImpl(

override fun active(): PNFuture<Boolean> {
if (!chat.config.storeUserActivityTimestamps) {
log.pnError(STORE_USER_ACTIVITY_INTERVAL_IS_FALSE)
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()
lastActiveTimestamp?.let { lastActiveTimestampNonNull ->
Clock.System.now() - Instant.fromEpochMilliseconds(lastActiveTimestampNonNull) <= chat.config.storeUserActivityInterval
} ?: false
).asFuture()
}

override fun report(reason: String): PNFuture<PNPublishResult> {
Expand Down
Loading

0 comments on commit e165344

Please sign in to comment.