Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Add channels.delete endpoint #230

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package chat.rocket.core.internal.model

import se.ansman.kotshi.JsonSerializable

@JsonSerializable
data class DeleteChannelPayload(
val roomId: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package chat.rocket.core.internal.model

import se.ansman.kotshi.JsonDefaultValueBoolean
import se.ansman.kotshi.JsonSerializable

@JsonSerializable
data class DeleteChannelResult(
@JsonDefaultValueBoolean(false)
val success: Boolean
)
25 changes: 25 additions & 0 deletions core/src/main/kotlin/chat/rocket/core/internal/rest/Channel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.RestResult
import chat.rocket.core.internal.model.CreateDirectMessagePayload
import chat.rocket.core.internal.model.CreateNewChannelPayload
import chat.rocket.core.internal.model.DeleteChannelPayload
import chat.rocket.core.internal.model.DeleteChannelResult
import chat.rocket.core.model.DirectMessage
import chat.rocket.core.model.Room
import com.squareup.moshi.Types
Expand Down Expand Up @@ -39,6 +41,29 @@ suspend fun RocketChatClient.createChannel(
return@withContext handleRestCall<RestResult<Room>>(request, type).result()
}

/**
* Deletes the chat room.
*
* @param roomType The type of the room.
* @param roomId ID of the room
* @return True if the channel was deleted, false otherwise.
*/
suspend fun RocketChatClient.deleteChannel(
roomType: RoomType,
roomId: String
): DeleteChannelResult = withContext(CommonPool) {
val payload = DeleteChannelPayload(roomId = roomId)
val adapter = moshi.adapter(DeleteChannelPayload::class.java)
val payloadBody = adapter.toJson(payload)

val body = RequestBody.create(MEDIA_TYPE_JSON,payloadBody)

val url = requestUrl(restUrl, getRestApiMethodNameByRoomType(roomType,"delete")).build()
val request = requestBuilderForAuthenticatedMethods(url).post(body).build()

return@withContext handleRestCall<DeleteChannelResult>(request,DeleteChannelResult::class.java)
}

/**
* Create a direct message (DM) room with an user.
*
Expand Down
18 changes: 18 additions & 0 deletions core/src/test/kotlin/chat/rocket/core/internal/rest/ChannelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ class ChannelTest {
}
}

@Test
fun `deleteChannel() should return a DeleteChannelResult object`() {
mockServer.expect()
.post()
.withPath("/api/v1/channels.delete")
.andReturn(200, DELETE_CHANNEL_OK)
.once()

runBlocking {
val result = sut.deleteChannel(
roomType = RoomType.Channel(),
roomId = "GENERAL"
)

assertThat(result.success, isEqualTo(true))
}
}

@Test
fun `createDirectMessage() should return true and yield no exceptions`() {
mockServer.expect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,5 @@ const val CREATE_DM_OK = """
"success": true
}
"""

const val DELETE_CHANNEL_OK = "{\"success\":true}"