Skip to content

Commit

Permalink
Fixed linting issues as per coderabbitai suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Nov 25, 2024
1 parent 40c0981 commit 7f6d306
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 38 deletions.
10 changes: 6 additions & 4 deletions chat-android/src/main/java/com/ably/chat/Discontinuities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.ably.chat

import io.ably.lib.types.ErrorInfo
import io.ably.lib.util.EventEmitter
import io.ably.lib.util.Log
import io.ably.lib.realtime.ChannelBase as AblyRealtimeChannel

/**
Expand Down Expand Up @@ -44,12 +43,15 @@ interface EmitsDiscontinuities {
}
}

class DiscontinuityEmitter : EventEmitter<String, EmitsDiscontinuities.Listener>() {
internal class DiscontinuityEmitter(logger: Logger) : EventEmitter<String, EmitsDiscontinuities.Listener>() {
private val logger = logger.withContext("DiscontinuityEmitter")

override fun apply(listener: EmitsDiscontinuities.Listener?, event: String?, vararg args: Any?) {
try {
listener?.discontinuityEmitted(args[0] as? ErrorInfo?)
val reason = args.firstOrNull() as? ErrorInfo?
listener?.discontinuityEmitted(reason)
} catch (t: Throwable) {
Log.e("DiscontinuityEmitter", "Unexpected exception calling Discontinuity Listener", t)
logger.error("Unexpected exception calling Discontinuity Listener", t)
}
}
}
3 changes: 2 additions & 1 deletion chat-android/src/main/java/com/ably/chat/Messages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ internal class DefaultMessages(
private val roomId: String,
private val realtimeChannels: AblyRealtime.Channels,
private val chatApi: ChatApi,
) : Messages, ContributesToRoomLifecycleImpl() {
private val logger: Logger,
) : Messages, ContributesToRoomLifecycleImpl(logger) {

override val featureName: String = "messages"

Expand Down
2 changes: 1 addition & 1 deletion chat-android/src/main/java/com/ably/chat/Occupancy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal class DefaultOccupancy(
private val chatApi: ChatApi,
private val roomId: String,
private val logger: Logger,
) : Occupancy, ContributesToRoomLifecycleImpl() {
) : Occupancy, ContributesToRoomLifecycleImpl(logger) {

override val featureName: String = "occupancy"

Expand Down
3 changes: 2 additions & 1 deletion chat-android/src/main/java/com/ably/chat/Presence.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ internal class DefaultPresence(
private val clientId: String,
override val channel: Channel,
private val presence: PubSubPresence,
) : Presence, ContributesToRoomLifecycleImpl() {
private val logger: Logger,
) : Presence, ContributesToRoomLifecycleImpl(logger) {

override val featureName = "presence"

Expand Down
3 changes: 3 additions & 0 deletions chat-android/src/main/java/com/ably/chat/Room.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ internal class DefaultRoom(
roomId = roomId,
realtimeChannels = realtimeClient.channels,
chatApi = chatApi,
logger = logger.withContext(tag = "Messages"),
)

private var _presence: Presence? = null
Expand Down Expand Up @@ -186,6 +187,7 @@ internal class DefaultRoom(
clientId = clientId,
channel = messages.channel,
presence = messages.channel.presence,
logger = logger.withContext(tag = "Presence"),
)
roomFeatures.add(presenceContributor)
_presence = presenceContributor
Expand All @@ -208,6 +210,7 @@ internal class DefaultRoom(
roomId = roomId,
clientId = clientId,
realtimeChannels = realtimeClient.channels,
logger = logger.withContext(tag = "Reactions"),
)
roomFeatures.add(reactionsContributor)
_reactions = reactionsContributor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import io.ably.lib.realtime.Channel as AblyRealtimeChannel
* An interface for features that contribute to the room status.
*/
interface ContributesToRoomLifecycle : EmitsDiscontinuities, HandlesDiscontinuity {

/**
* Name of the feature
*/
Expand Down Expand Up @@ -47,9 +48,9 @@ interface ContributesToRoomLifecycle : EmitsDiscontinuities, HandlesDiscontinuit
fun release()
}

abstract class ContributesToRoomLifecycleImpl : ContributesToRoomLifecycle {
internal abstract class ContributesToRoomLifecycleImpl(logger: Logger) : ContributesToRoomLifecycle {

private val discontinuityEmitter = DiscontinuityEmitter()
private val discontinuityEmitter = DiscontinuityEmitter(logger)

override fun onDiscontinuity(listener: EmitsDiscontinuities.Listener): Subscription {
discontinuityEmitter.on(listener)
Expand Down
3 changes: 2 additions & 1 deletion chat-android/src/main/java/com/ably/chat/RoomReactions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ internal class DefaultRoomReactions(
roomId: String,
private val clientId: String,
private val realtimeChannels: AblyRealtime.Channels,
) : RoomReactions, ContributesToRoomLifecycleImpl() {
private val logger: Logger,
) : RoomReactions, ContributesToRoomLifecycleImpl(logger) {

override val featureName = "reactions"

Expand Down
16 changes: 10 additions & 6 deletions chat-android/src/main/java/com/ably/chat/RoomStatus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.ably.chat

import io.ably.lib.types.ErrorInfo
import io.ably.lib.util.EventEmitter
import io.ably.lib.util.Log

/**
* (CHA-RS1)
Expand Down Expand Up @@ -156,13 +155,18 @@ interface InternalRoomLifecycle : RoomLifecycle {
fun setStatus(params: NewRoomStatus)
}

class RoomStatusEventEmitter : EventEmitter<RoomStatus, RoomLifecycle.Listener>() {
internal class RoomStatusEventEmitter(logger: Logger) : EventEmitter<RoomStatus, RoomLifecycle.Listener>() {
private val logger = logger.withContext("RoomEventEmitter")

override fun apply(listener: RoomLifecycle.Listener?, event: RoomStatus?, vararg args: Any?) {
try {
listener?.roomStatusChanged(args[0] as RoomStatusChange)
if (args.isNotEmpty() && args[0] is RoomStatusChange) {
listener?.roomStatusChanged(args[0] as RoomStatusChange)
} else {
logger.error("Invalid arguments received in apply method")
}
} catch (t: Throwable) {
Log.e("RoomEventEmitter", "Unexpected exception calling Room Status Listener", t)
logger.error("Unexpected exception calling Room Status Listener", t)
}
}
}
Expand All @@ -177,8 +181,8 @@ internal class DefaultRoomLifecycle(logger: Logger) : InternalRoomLifecycle {
override val error: ErrorInfo?
get() = _error

private val externalEmitter = RoomStatusEventEmitter()
private val internalEmitter = RoomStatusEventEmitter()
private val externalEmitter = RoomStatusEventEmitter(logger)
private val internalEmitter = RoomStatusEventEmitter(logger)

override fun onChange(listener: RoomLifecycle.Listener): Subscription {
externalEmitter.on(listener)
Expand Down
2 changes: 1 addition & 1 deletion chat-android/src/main/java/com/ably/chat/Rooms.kt
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,5 @@ internal class DefaultRooms(
* Spec: CHA-RC1f3
*/
private fun makeRoom(roomId: String, options: RoomOptions): DefaultRoom =
DefaultRoom(roomId, options, realtimeClient, chatApi, clientId, logger)
DefaultRoom(roomId, options.copy(), realtimeClient, chatApi, clientId, logger)
}
2 changes: 1 addition & 1 deletion chat-android/src/main/java/com/ably/chat/Typing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ internal class DefaultTyping(
private val clientId: String,
private val options: TypingOptions?,
private val logger: Logger,
) : Typing, ContributesToRoomLifecycleImpl() {
) : Typing, ContributesToRoomLifecycleImpl(logger) {
private val typingIndicatorsChannelName = "$roomId::\$chat::\$typingIndicators"

override val featureName = "typing"
Expand Down
41 changes: 25 additions & 16 deletions chat-android/src/main/java/com/ably/chat/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -215,33 +215,42 @@ internal class DeferredValue<T> {
fun lifeCycleErrorInfo(
errorMessage: String,
errorCode: ErrorCode,
statusCode: Int = HttpStatusCode.InternalServerError,
) = ErrorInfo(errorMessage, statusCode, errorCode.code)
) = createErrorInfo(errorMessage, errorCode, HttpStatusCode.InternalServerError)

fun lifeCycleException(
errorMessage: String,
errorCode: ErrorCode,
statusCode: Int = HttpStatusCode.InternalServerError,
cause: Throwable? = null,
): AblyException = ablyException(errorMessage, errorCode, statusCode, cause)

fun lifeCycleException(errorInfo: ErrorInfo, cause: Throwable? = null) = ablyException(errorInfo, cause)
): AblyException = createAblyException(lifeCycleErrorInfo(errorMessage, errorCode), cause)

fun ablyException(errorInfo: ErrorInfo, cause: Throwable? = null): AblyException {
cause?.let {
return AblyException.fromErrorInfo(cause, errorInfo)
}
return AblyException.fromErrorInfo(errorInfo)
}
fun lifeCycleException(
errorInfo: ErrorInfo,
cause: Throwable? = null,
): AblyException = createAblyException(errorInfo, cause)

fun ablyException(
errorMessage: String,
errorCode: ErrorCode,
statusCode: Int = HttpStatusCode.BadRequest,
cause: Throwable? = null,
): AblyException {
cause?.let {
return AblyException.fromErrorInfo(cause, ErrorInfo(errorMessage, statusCode, errorCode.code))
}
return AblyException.fromErrorInfo(ErrorInfo(errorMessage, statusCode, errorCode.code))
val errorInfo = createErrorInfo(errorMessage, errorCode, statusCode)
return createAblyException(errorInfo, cause)
}

fun ablyException(
errorInfo: ErrorInfo,
cause: Throwable? = null,
): AblyException = createAblyException(errorInfo, cause)

private fun createErrorInfo(
errorMessage: String,
errorCode: ErrorCode,
statusCode: Int,
) = ErrorInfo(errorMessage, statusCode, errorCode.code)

private fun createAblyException(
errorInfo: ErrorInfo,
cause: Throwable?,
) = cause?.let { AblyException.fromErrorInfo(it, errorInfo) }
?: AblyException.fromErrorInfo(errorInfo)
3 changes: 3 additions & 0 deletions chat-android/src/test/java/com/ably/chat/MessagesTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ably.chat

import com.ably.chat.room.createMockLogger
import com.google.gson.JsonObject
import io.ably.lib.realtime.AblyRealtime.Channels
import io.ably.lib.realtime.Channel
Expand Down Expand Up @@ -30,6 +31,7 @@ class MessagesTest {
private val realtimeChannel = spyk<Channel>(buildRealtimeChannel())
private val chatApi = spyk(ChatApi(realtimeClient, "clientId", EmptyLogger(LogContext(tag = "TEST"))))
private lateinit var messages: DefaultMessages
private val logger = createMockLogger()

private val channelStateListenerSlot = slot<ChannelStateListener>()

Expand All @@ -45,6 +47,7 @@ class MessagesTest {
roomId = "room1",
realtimeChannels = realtimeChannels,
chatApi = chatApi,
logger,
)
}

Expand Down
3 changes: 3 additions & 0 deletions chat-android/src/test/java/com/ably/chat/PresenceTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ably.chat

import com.ably.chat.room.createMockLogger
import com.google.gson.JsonObject
import com.google.gson.JsonPrimitive
import io.ably.lib.realtime.Channel
Expand All @@ -20,13 +21,15 @@ class PresenceTest {
private val pubSubChannel = spyk<Channel>(buildRealtimeChannel("room1::\$chat::\$messages"))
private val pubSubPresence = mockk<PubSubPresence>(relaxed = true)
private lateinit var presence: DefaultPresence
private val logger = createMockLogger()

@Before
fun setUp() {
presence = DefaultPresence(
clientId = "client1",
channel = pubSubChannel,
presence = pubSubPresence,
logger,
)
}

Expand Down
4 changes: 4 additions & 0 deletions chat-android/src/test/java/com/ably/chat/RoomReactionsTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ably.chat

import com.ably.chat.room.createMockLogger
import com.google.gson.JsonObject
import io.ably.lib.realtime.AblyRealtime.Channels
import io.ably.lib.realtime.Channel
Expand All @@ -19,6 +20,7 @@ class RoomReactionsTest {
private val realtimeChannels = mockk<Channels>(relaxed = true)
private val realtimeChannel = spyk<Channel>(buildRealtimeChannel("room1::\$chat::\$reactions"))
private lateinit var roomReactions: DefaultRoomReactions
private val logger = createMockLogger()

@Before
fun setUp() {
Expand All @@ -35,6 +37,7 @@ class RoomReactionsTest {
roomId = "room1",
clientId = "client1",
realtimeChannels = realtimeChannels,
logger,
)
}

Expand All @@ -47,6 +50,7 @@ class RoomReactionsTest {
roomId = "foo",
clientId = "client1",
realtimeChannels = realtimeChannels,
logger,
)

assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ fun createRoomFeatureMocks(roomId: String = "1234"): List<ContributesToRoomLifec
val chatApi = mockk<ChatApi>(relaxed = true)
val logger = createMockLogger()

val messagesContributor = spyk(DefaultMessages(roomId, realtimeClient.channels, chatApi), recordPrivateCalls = true)
val messagesContributor = spyk(DefaultMessages(roomId, realtimeClient.channels, chatApi, logger), recordPrivateCalls = true)
val presenceContributor = spyk(
DefaultPresence(clientId, messagesContributor.channel, messagesContributor.channel.presence),
DefaultPresence(clientId, messagesContributor.channel, messagesContributor.channel.presence, logger),
recordPrivateCalls = true,
)
val occupancyContributor = spyk(DefaultOccupancy(realtimeClient.channels, chatApi, roomId, logger), recordPrivateCalls = true)
val typingContributor = spyk(
DefaultTyping(roomId, realtimeClient, clientId, RoomOptions.default.typing, logger),
recordPrivateCalls = true,
)
val reactionsContributor = spyk(DefaultRoomReactions(roomId, clientId, realtimeClient.channels), recordPrivateCalls = true)
val reactionsContributor = spyk(DefaultRoomReactions(roomId, clientId, realtimeClient.channels, logger), recordPrivateCalls = true)
return listOf(messagesContributor, presenceContributor, occupancyContributor, typingContributor, reactionsContributor)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Popup
import com.ably.chat.ChatClient
import com.ably.chat.PresenceMember
import com.ably.chat.RoomOptions
import com.ably.chat.Subscription
import com.ably.chat.example.Settings
import com.google.gson.JsonObject
Expand All @@ -33,7 +34,9 @@ import kotlinx.coroutines.runBlocking
fun PresencePopup(chatClient: ChatClient, onDismiss: () -> Unit) {
var members by remember { mutableStateOf(listOf<PresenceMember>()) }
val coroutineScope = rememberCoroutineScope()
val presence = runBlocking { chatClient.rooms.get(Settings.ROOM_ID).presence }
val presence = runBlocking {
chatClient.rooms.get(Settings.ROOM_ID, RoomOptions.default).presence
}

DisposableEffect(Unit) {
var subscription: Subscription? = null
Expand Down

0 comments on commit 7f6d306

Please sign in to comment.