Skip to content

Commit

Permalink
Merge pull request #242 from RocketChat/develop
Browse files Browse the repository at this point in the history
[RELEASE] Merge DEVELOP into MASTER
  • Loading branch information
philipbrito authored Mar 27, 2019
2 parents 647f7d5 + 38f60f1 commit 018ccfd
Show file tree
Hide file tree
Showing 64 changed files with 869 additions and 627 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
version: 2
build:
machine:
java: oraclejdk8
jobs:
formatting:
docker:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {

dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}"
classpath "gradle.plugin.org.jmailen.gradle:kotlinter-gradle:${versions.kotlinter}"
classpath "org.jmailen.gradle:kotlinter-gradle:${versions.kotlinter}"
classpath "org.jetbrains.dokka:dokka-gradle-plugin:${versions.dokka}"
}
}
Expand Down
1 change: 1 addition & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ apply plugin: 'kotlin-kapt'
apply plugin: 'org.jetbrains.dokka'

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${versions.kotlin}"
Expand Down
4 changes: 4 additions & 0 deletions common/src/main/kotlin/chat/rocket/common/model/BaseRoom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ sealed class RoomType {
@Json(name = LIVECHAT) class LiveChat : RoomType()
class Custom(val rawType: String) : RoomType()

override fun equals(other: Any?): Boolean {
return other != null && other is RoomType && other.toString() == toString()
}

override fun toString(): String {
return when (this) {
is Channel -> CHANNEL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.TimeZone
import java.util.Locale

class CalendarISO8601Converter : ISO8601Converter {

Expand All @@ -13,7 +14,7 @@ class CalendarISO8601Converter : ISO8601Converter {
calendar.timeInMillis = timestamp

// Quoted "Z" to indicate UTC, no timezone offset
val df = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
val df = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US)
df.timeZone = tz

return df.format(calendar.time)
Expand All @@ -27,4 +28,4 @@ class CalendarISO8601Converter : ISO8601Converter {
df.timeZone = tz
return df.parse(date).time
}
}
}
32 changes: 24 additions & 8 deletions common/src/main/kotlin/chat/rocket/common/util/Logger.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
package chat.rocket.common.util

class Logger(val platformLogger: PlatformLogger, private val url: String) {
interface Logger {
fun debug(msg: () -> Any?)
fun info(msg: () -> Any?)
fun warn(msg: () -> Any?)
}

val enabled: Boolean = true
class RealLogger(val platformLogger: PlatformLogger, private val url: String) : Logger {

fun debug(msg: () -> Any?) {
if (enabled) platformLogger.debug("SDK($url): ${msg.toStringSafe()}")

override fun debug(msg: () -> Any?) {
platformLogger.debug("SDK($url): ${msg.toStringSafe()}")
}

override fun info(msg: () -> Any?) {
platformLogger.info("SDK($url): ${msg.toStringSafe()}")
}

override fun warn(msg: () -> Any?) {
platformLogger.warn("SDK($url): ${msg.toStringSafe()}")
}
}

object NoOpLogger : Logger {
override fun debug(msg: () -> Any?) {
}

fun info(msg: () -> Any?) {
if (enabled) platformLogger.info("SDK($url): ${msg.toStringSafe()}")
override fun info(msg: () -> Any?) {
}

fun warn(msg: () -> Any?) {
if (enabled) platformLogger.warn("SDK($url): ${msg.toStringSafe()}")
override fun warn(msg: () -> Any?) {
}
}
7 changes: 1 addition & 6 deletions compat/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ apply plugin: 'org.jetbrains.dokka'
apply plugin: "org.jmailen.kotlinter"

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${versions.kotlin}"
Expand All @@ -35,12 +36,6 @@ compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

kotlin {
experimental {
coroutines "enable"
}
}

dokka {
outputFormat = 'html'
outputDirectory = "$buildDir/javadoc"
Expand Down
2 changes: 1 addition & 1 deletion compat/src/main/kotlin/chat/rocket/core/compat/Call.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package chat.rocket.core.compat

import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.Job

class Call(val job: Job) {
fun cancel() {
Expand Down
11 changes: 6 additions & 5 deletions compat/src/main/kotlin/chat/rocket/core/compat/Server.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import chat.rocket.common.model.ServerInfo
import chat.rocket.core.RocketChatClient
import chat.rocket.core.compat.internal.callback
import chat.rocket.core.internal.rest.serverInfo
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.Dispatchers

fun RocketChatClient.serverInfo(future: Callback<ServerInfo>): Call =
callback(CommonPool, future) {
serverInfo()
}
/**
* Returns the current logged server information.
* Must be used with a coroutine context (async, launch, etc)
*/
fun RocketChatClient.serverInfo(future: Callback<ServerInfo>): Call = callback(Dispatchers.IO, future) { serverInfo() }
11 changes: 2 additions & 9 deletions compat/src/main/kotlin/chat/rocket/core/compat/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@ import chat.rocket.core.RocketChatClient
import chat.rocket.core.compat.internal.callback
import chat.rocket.core.internal.rest.me
import chat.rocket.core.model.Myself
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.Dispatchers

/**
* Returns the current logged user information, useful to check if the Token from TokenProvider
* is still valid. Must be used with a coroutine context (async, launch, etc)
*
* @return Call
* @see
* @see RocketChatException
*/
fun RocketChatClient.me(future: Callback<Myself>): Call =
callback(CommonPool, future) {
me()
}
fun RocketChatClient.me(future: Callback<Myself>): Call = callback(Dispatchers.IO, future) { me() }
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,35 @@ package chat.rocket.core.compat.internal
import chat.rocket.common.RocketChatException
import chat.rocket.core.compat.Call
import chat.rocket.core.compat.Callback
import kotlinx.coroutines.experimental.AbstractCoroutine
import kotlinx.coroutines.experimental.CoroutineScope
import kotlinx.coroutines.experimental.DefaultDispatcher
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.newCoroutineContext
import kotlin.coroutines.experimental.CoroutineContext
import kotlin.coroutines.experimental.startCoroutine
import kotlinx.coroutines.AbstractCoroutine
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.newCoroutineContext
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.startCoroutine

@JvmOverloads
fun <T> callback(
context: CoroutineContext = DefaultDispatcher,
context: CoroutineContext = Dispatchers.Default,
callback: Callback<T>,
block: suspend CoroutineScope.() -> T
): Call {
val newContext = newCoroutineContext(context)
val newContext = GlobalScope.newCoroutineContext(context)
val job = Job(newContext[Job])
val coroutine = CallbackCoroutine(newContext + job, callback)
block.startCoroutine(coroutine, coroutine)
return Call(job)
}

@UseExperimental(InternalCoroutinesApi::class)
private class CallbackCoroutine<in T>(
parentContext: CoroutineContext,
private val callback: Callback<T>
) : AbstractCoroutine<T>(parentContext, true) {

override fun onCompleted(value: T) {
callback.onSuccess(value)
}
Expand Down
7 changes: 1 addition & 6 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ apply plugin: 'org.jetbrains.dokka'
apply plugin: "org.jmailen.kotlinter"

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

dependencies {
compile project (':common')
Expand Down Expand Up @@ -40,12 +41,6 @@ compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

kotlin {
experimental {
coroutines "enable"
}
}

dokka {
outputFormat = 'html'
outputDirectory = "$buildDir/javadoc"
Expand Down
17 changes: 14 additions & 3 deletions core/src/main/kotlin/chat/rocket/core/RocketChatClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import chat.rocket.common.model.TimestampAdapter
import chat.rocket.common.model.User
import chat.rocket.common.util.CalendarISO8601Converter
import chat.rocket.common.util.Logger
import chat.rocket.common.util.NoOpLogger
import chat.rocket.common.util.PlatformLogger
import chat.rocket.common.util.RealLogger
import chat.rocket.common.util.ifNull
import chat.rocket.core.internal.RestResult
import chat.rocket.core.internal.RestMultiResult
Expand All @@ -25,19 +27,25 @@ import chat.rocket.core.model.Myself
import chat.rocket.core.model.Room
import chat.rocket.core.model.url.MetaJsonAdapter
import com.squareup.moshi.Moshi
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import okhttp3.HttpUrl
import okhttp3.MediaType
import okhttp3.OkHttpClient
import java.security.InvalidParameterException
import kotlin.coroutines.CoroutineContext

class RocketChatClient private constructor(
internal val httpClient: OkHttpClient,
baseUrl: String,
userAgent: String,
internal val tokenRepository: TokenRepository,
internal val logger: Logger
) {
) : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = Dispatchers.Default

internal val moshi: Moshi = Moshi.Builder()
.add(FallbackSealedClassJsonAdapter.ADAPTER_FACTORY)
.add(RestResult.JsonAdapterFactory())
Expand Down Expand Up @@ -99,7 +107,7 @@ class RocketChatClient private constructor(
builder.restUrl,
builder.userAgent,
builder.tokenRepository,
Logger(builder.platformLogger, builder.restUrl))
if (builder.enableLogger) RealLogger(builder.platformLogger, builder.restUrl) else NoOpLogger)

companion object {
val CONTENT_TYPE_JSON = MediaType.parse("application/json; charset=utf-8")
Expand All @@ -118,6 +126,7 @@ class RocketChatClient private constructor(
lateinit var userAgent: String
lateinit var tokenRepository: TokenRepository
lateinit var platformLogger: PlatformLogger
var enableLogger: Boolean = true

fun httpClient(init: Builder.() -> OkHttpClient) = apply { httpClient = init() }

Expand All @@ -129,6 +138,8 @@ class RocketChatClient private constructor(

fun platformLogger(init: Builder.() -> PlatformLogger) = apply { platformLogger = init() }

fun enableLogger(init: Builder.() -> Boolean) = apply { enableLogger = init() }

fun build() = RocketChatClient(this)
}

Expand Down
Loading

0 comments on commit 018ccfd

Please sign in to comment.