Skip to content

Commit

Permalink
Merge pull request #255 from RocketChat/beta
Browse files Browse the repository at this point in the history
[RELEASE] Merge BETA into MASTER
  • Loading branch information
philipbrito authored Jul 26, 2019
2 parents b4aa0a4 + 76dbff1 commit 567fce9
Show file tree
Hide file tree
Showing 7 changed files with 325 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ data class ChatRoomPayload(@Json(name = "rid") val roomId: String)
@JsonSerializable
data class ChatRoomUnreadPayload(val roomId: String)

@JsonSerializable
data class ChatRoomIdUserPayload(@Json(name = "rid") val roomId: String, val userId: String)

@JsonSerializable
data class ChatRoomUserIgnorePayload(@Json(name = "rid") val roomId: String, val userId: String, val ignore: Boolean)

@JsonSerializable
data class ChatRoomUserPayload(val roomId: String, val userId: String)

@JsonSerializable
data class ChatRoomNamePayload(val roomId: String, val name: String?)

Expand All @@ -27,8 +36,14 @@ data class ChatRoomReadOnlyPayload(val roomId: String, val readOnly: Boolean)
@JsonSerializable
data class ChatRoomTypePayload(val roomId: String, val type: String)

@JsonSerializable
data class ChatRoomInvitePayload(val roomId: String, val userId: String)

@JsonSerializable
data class ChatRoomJoinCodePayload(val roomId: String, val joinCode: String)

@JsonSerializable
data class ChatRoomKickPayload(val roomId: String, val userId: String)

@JsonSerializable
data class ChatRoomFavoritePayload(val roomId: String, val favorite: Boolean)
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ class Socket(
return
}

reschedulePing(message.type)
message.type?.let {
reschedulePing(message.type)
}

when (currentState) {
is State.Connecting -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import se.ansman.kotshi.JsonSerializable
@JsonSerializable
data class SocketMessage(
@Json(name = "msg")
val type: MessageType,
val type: MessageType?,
val id: String?,
val collection: String?,
@Json(name = "reason")
Expand Down
153 changes: 152 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,6 +3,7 @@ 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.ChatRoomUserPayload
import chat.rocket.core.internal.model.CreateDirectMessagePayload
import chat.rocket.core.internal.model.CreateNewChannelPayload
import chat.rocket.core.model.DirectMessage
Expand Down Expand Up @@ -59,4 +60,154 @@ suspend fun RocketChatClient.createDirectMessage(username: String): DirectMessag
val type = Types.newParameterizedType(RestResult::class.java, DirectMessage::class.java)

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

/**
* Add the owner of a chat room.
*
* @param roomId The room id.
* @param roomType The room type.
* @param userId The user id.
*/
suspend fun RocketChatClient.addOwner(
roomId: String,
roomType: RoomType,
userId: String
) {
withContext(Dispatchers.IO) {
val payload = ChatRoomUserPayload(roomId, userId)
val adapter = moshi.adapter(ChatRoomUserPayload::class.java)
val payloadBody = adapter.toJson(payload)
val body = RequestBody.create(MEDIA_TYPE_JSON, payloadBody)

val url = requestUrl(restUrl, getRestApiMethodNameByRoomType(roomType, "addOwner")).build()

val request = requestBuilderForAuthenticatedMethods(url).post(body).build()

handleRestCall<Any>(request, Any::class.java)
}
}

/**
* Adds the leader of a channel.
* @param roomId The room id.
* @param roomType The room type.
* @param userId The user id.
*/
suspend fun RocketChatClient.addLeader(
roomId: String,
roomType: RoomType,
userId: String
) {
withContext(Dispatchers.IO) {
val payload = ChatRoomUserPayload(roomId, userId)
val adapter = moshi.adapter(ChatRoomUserPayload::class.java)
val payloadBody = adapter.toJson(payload)
val body = RequestBody.create(MEDIA_TYPE_JSON, payloadBody)

val url = requestUrl(restUrl, getRestApiMethodNameByRoomType(roomType, "addLeader")).build()

val request = requestBuilderForAuthenticatedMethods(url).post(body).build()

handleRestCall<Any>(request, Any::class.java)
}
}

/**
* Adds the moderator of a channel.
* @param roomId The room id.
* @param roomType The room type.
* @param userId The user id.
*/
suspend fun RocketChatClient.addModerator(
roomId: String,
roomType: RoomType,
userId: String
) {
withContext(Dispatchers.IO) {
val payload = ChatRoomUserPayload(roomId, userId)
val adapter = moshi.adapter(ChatRoomUserPayload::class.java)
val payloadBody = adapter.toJson(payload)
val body = RequestBody.create(MEDIA_TYPE_JSON, payloadBody)

val url = requestUrl(restUrl, getRestApiMethodNameByRoomType(roomType, "addModerator")).build()

val request = requestBuilderForAuthenticatedMethods(url).post(body).build()

handleRestCall<Any>(request, Any::class.java)
}
}

/**
* Removes the owner of a channel.
*
* @param roomId The room id.
* @param roomType The room type.
* @param userId The user id.
*/
suspend fun RocketChatClient.removeOwner(
roomId: String,
roomType: RoomType,
userId: String
) = withContext(Dispatchers.IO) {
val payload = ChatRoomUserPayload(roomId, userId)
val adapter = moshi.adapter(ChatRoomUserPayload::class.java)
val payloadBody = adapter.toJson(payload)
val body = RequestBody.create(MEDIA_TYPE_JSON, payloadBody)

val url = requestUrl(restUrl, getRestApiMethodNameByRoomType(roomType, "removeOwner")).build()

val request = requestBuilderForAuthenticatedMethods(url).post(body).build()

handleRestCall<Any>(request, Any::class.java)
}

/**
* Removes the leader of a channel.
* @param roomId The room id.
* @param roomType The room type.
* @param userId The user id.
*/
suspend fun RocketChatClient.removeLeader(
roomId: String,
roomType: RoomType,
userId: String
) = withContext(Dispatchers.IO) {
val payload = ChatRoomUserPayload(roomId, userId)
val adapter = moshi.adapter(ChatRoomUserPayload::class.java)
val payloadBody = adapter.toJson(payload)
val body = RequestBody.create(MEDIA_TYPE_JSON, payloadBody)

val url = requestUrl(restUrl, getRestApiMethodNameByRoomType(roomType, "removeLeader")).build()

val request = requestBuilderForAuthenticatedMethods(url).post(body).build()

handleRestCall<Any>(request, Any::class.java)
}

/**
* Removes the moderator of a channel.
*
* @param roomId The room id.
* @param roomType The room type.
* @param userId The user id.
*/
suspend fun RocketChatClient.removeModerator(
roomId: String,
roomType: RoomType,
userId: String
) = withContext(Dispatchers.IO) {
val payload = ChatRoomUserPayload(roomId, userId)
val adapter = moshi.adapter(ChatRoomUserPayload::class.java)
val payloadBody = adapter.toJson(payload)
val body = RequestBody.create(MEDIA_TYPE_JSON, payloadBody)

val url = requestUrl(restUrl, getRestApiMethodNameByRoomType(roomType, "removeModerator")).build()

val request = requestBuilderForAuthenticatedMethods(url).post(body).build()

handleRestCall<Any>(request, Any::class.java)
}
Loading

0 comments on commit 567fce9

Please sign in to comment.