Skip to content

Commit

Permalink
Merge pull request #229 from RocketChat/improvement/createDirectMessa…
Browse files Browse the repository at this point in the history
…ge-package-location

[IMPROVEMENT][PROJECT] Puts the createDirectMessage on the proper package.
  • Loading branch information
luciofm authored Dec 26, 2018
2 parents bea2945 + 88bca2d commit 54676f5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 46 deletions.
26 changes: 25 additions & 1 deletion core/src/main/kotlin/chat/rocket/core/internal/rest/Channel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package chat.rocket.core.internal.rest
import chat.rocket.common.model.RoomType
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.model.DirectMessage
import chat.rocket.core.model.Room
import com.squareup.moshi.Types
import kotlinx.coroutines.experimental.CommonPool
Expand Down Expand Up @@ -35,4 +37,26 @@ suspend fun RocketChatClient.createChannel(
val type = Types.newParameterizedType(RestResult::class.java, Room::class.java)

return@withContext handleRestCall<RestResult<Room>>(request, type).result()
}
}

/**
* Create a direct message (DM) room with an user.
*
* @param username The username of the user to create a DM with.
* @return A DirectMessage object.
*/
suspend fun RocketChatClient.createDirectMessage(username: String): DirectMessage =
withContext(CommonPool) {
val payload = CreateDirectMessagePayload(username = username)
val adapter = moshi.adapter(CreateDirectMessagePayload::class.java)
val payloadBody = adapter.toJson(payload)

val body = RequestBody.create(MEDIA_TYPE_JSON, payloadBody)

val url = requestUrl(restUrl, "im.create").build()
val request = requestBuilderForAuthenticatedMethods(url).post(body).build()

val type = Types.newParameterizedType(RestResult::class.java, DirectMessage::class.java)

return@withContext handleRestCall<RestResult<DirectMessage>>(request, type).result()
}
26 changes: 1 addition & 25 deletions core/src/main/kotlin/chat/rocket/core/internal/rest/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import chat.rocket.common.model.BaseResult
import chat.rocket.common.model.RoomType
import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.RestResult
import chat.rocket.core.internal.model.CreateDirectMessagePayload
import chat.rocket.core.internal.model.DeletePayload
import chat.rocket.core.internal.model.MessageReportPayload
import chat.rocket.core.internal.model.PostMessagePayload
Expand All @@ -13,7 +12,6 @@ import chat.rocket.core.internal.model.SendMessageBody
import chat.rocket.core.internal.model.SendMessagePayload
import chat.rocket.core.model.DeleteResult
import chat.rocket.core.model.Message
import chat.rocket.core.model.NewDirectMessageResult
import chat.rocket.core.model.PagedResult
import chat.rocket.core.model.ReadReceipt
import chat.rocket.core.model.attachment.Attachment
Expand Down Expand Up @@ -397,26 +395,4 @@ suspend fun RocketChatClient.reportMessage(
val request = requestBuilderForAuthenticatedMethods(url).post(body).build()

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

/**
* Create a direct message session with another user.
*
* @param username The username of the user to create a session with.
* @return The updated Message object.
*/
suspend fun RocketChatClient.createDirectMessage(username: String): NewDirectMessageResult =
withContext(CommonPool) {
val payload = CreateDirectMessagePayload(username = username)
val adapter = moshi.adapter(CreateDirectMessagePayload::class.java)
val payloadBody = adapter.toJson(payload)

val body = RequestBody.create(MEDIA_TYPE_JSON, payloadBody)

val url = requestUrl(restUrl, "im.create").build()
val request = requestBuilderForAuthenticatedMethods(url).post(body).build()

val type = Types.newParameterizedType(RestResult::class.java, NewDirectMessageResult::class.java)

return@withContext handleRestCall<RestResult<NewDirectMessageResult>>(request, type).result()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.squareup.moshi.Json
import se.ansman.kotshi.JsonSerializable

@JsonSerializable
data class NewDirectMessageResult(
data class DirectMessage(
@Json(name = "_id")
val id: String,
@Json(name = "_updatedAt")
Expand Down
19 changes: 19 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 @@ -4,6 +4,7 @@ import chat.rocket.common.RocketChatAuthException
import chat.rocket.common.RocketChatException
import chat.rocket.common.model.RoomType
import chat.rocket.common.model.Token
import chat.rocket.common.model.roomTypeOf
import chat.rocket.common.util.PlatformLogger
import chat.rocket.core.RocketChatClient
import chat.rocket.core.TokenRepository
Expand Down Expand Up @@ -110,6 +111,24 @@ class ChannelTest {
}
}

@Test
fun `createDirectMessage() should return true and yield no exceptions`() {
mockServer.expect()
.post()
.withPath("/api/v1/im.create")
.andReturn(200, CREATE_DM_OK)
.once()

runBlocking {
val result = sut.createDirectMessage("rocket.cat")
assertThat(result.id, isEqualTo("Lymsiu4Mn6xjTAan4RtMDEYc28fQ5aHpf4"))
assertThat(result.type, isEqualTo(roomTypeOf("d")))
assertThat(result.usernames.size, isEqualTo(2))
assertThat(result.usernames[0], isEqualTo("rocket.cat"))
assertThat(result.usernames[1], isEqualTo("user.test"))
}
}

@After
fun shutdown() {
mockServer.shutdown()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package chat.rocket.core.internal.rest

import chat.rocket.common.RocketChatException
import chat.rocket.common.model.Token
import chat.rocket.common.model.roomTypeOf
import chat.rocket.common.util.PlatformLogger
import chat.rocket.core.RocketChatClient
import chat.rocket.core.TokenRepository
Expand Down Expand Up @@ -256,24 +255,6 @@ class MessagesTest {
}
}

@Test
fun `createDirectMessage() should return true and yield no exceptions`() {
mockServer.expect()
.post()
.withPath("/api/v1/im.create")
.andReturn(200, CREATE_DM_OK)
.once()

runBlocking {
val result = sut.createDirectMessage(username = "rocket.cat")
assertThat(result.id, isEqualTo("Lymsiu4Mn6xjTAan4RtMDEYc28fQ5aHpf4"))
assertThat(result.type, isEqualTo(roomTypeOf("d")))
assertThat(result.usernames.size, isEqualTo(2))
assertThat(result.usernames[0], isEqualTo("rocket.cat"))
assertThat(result.usernames[1], isEqualTo("user.test"))
}
}

@After
fun shutdown() {
mockServer.shutdown()
Expand Down

0 comments on commit 54676f5

Please sign in to comment.