diff --git a/core/src/main/kotlin/chat/rocket/core/internal/model/DeleteChannelPayload.kt b/core/src/main/kotlin/chat/rocket/core/internal/model/DeleteChannelPayload.kt new file mode 100644 index 00000000..247afc08 --- /dev/null +++ b/core/src/main/kotlin/chat/rocket/core/internal/model/DeleteChannelPayload.kt @@ -0,0 +1,8 @@ +package chat.rocket.core.internal.model + +import se.ansman.kotshi.JsonSerializable + +@JsonSerializable +data class DeleteChannelPayload( + val roomId: String +) \ No newline at end of file diff --git a/core/src/main/kotlin/chat/rocket/core/internal/model/DeleteChannelResult.kt b/core/src/main/kotlin/chat/rocket/core/internal/model/DeleteChannelResult.kt new file mode 100644 index 00000000..33be0546 --- /dev/null +++ b/core/src/main/kotlin/chat/rocket/core/internal/model/DeleteChannelResult.kt @@ -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 +) \ No newline at end of file diff --git a/core/src/main/kotlin/chat/rocket/core/internal/rest/Channel.kt b/core/src/main/kotlin/chat/rocket/core/internal/rest/Channel.kt index 5fb11dc2..88d67d23 100644 --- a/core/src/main/kotlin/chat/rocket/core/internal/rest/Channel.kt +++ b/core/src/main/kotlin/chat/rocket/core/internal/rest/Channel.kt @@ -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 @@ -39,6 +41,29 @@ suspend fun RocketChatClient.createChannel( return@withContext handleRestCall>(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(request,DeleteChannelResult::class.java) + } + /** * Create a direct message (DM) room with an user. * diff --git a/core/src/test/kotlin/chat/rocket/core/internal/rest/ChannelTest.kt b/core/src/test/kotlin/chat/rocket/core/internal/rest/ChannelTest.kt index 79e7445f..218c2f62 100644 --- a/core/src/test/kotlin/chat/rocket/core/internal/rest/ChannelTest.kt +++ b/core/src/test/kotlin/chat/rocket/core/internal/rest/ChannelTest.kt @@ -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() diff --git a/core/src/test/kotlin/chat/rocket/core/internal/rest/Constants.kt b/core/src/test/kotlin/chat/rocket/core/internal/rest/Constants.kt index 137f3a20..69676afb 100644 --- a/core/src/test/kotlin/chat/rocket/core/internal/rest/Constants.kt +++ b/core/src/test/kotlin/chat/rocket/core/internal/rest/Constants.kt @@ -227,3 +227,5 @@ const val CREATE_DM_OK = """ "success": true } """ + +const val DELETE_CHANNEL_OK = "{\"success\":true}" \ No newline at end of file