Skip to content

Commit

Permalink
Merge pull request #209 from ouchadam/release-candidate
Browse files Browse the repository at this point in the history
[Auto] Release Candidate
  • Loading branch information
ouchadam committed Oct 17, 2022
2 parents e2f7b93 + 856e18c commit fc9a864
Show file tree
Hide file tree
Showing 156 changed files with 2,345 additions and 1,241 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ dependencies {

implementation project(":core")

implementation project(":chat-engine")
implementation project(":matrix-chat-engine")

implementation Dependencies.google.androidxComposeUi
implementation Dependencies.mavenCentral.ktorAndroid
implementation Dependencies.mavenCentral.sqldelightAndroid
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/kotlin/app/dapk/st/SmallTalkApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ class SmallTalkApplication : Application(), ModuleProvider {
val notificationsModule = featureModules.notificationsModule
val storeModule = appModule.storeModule.value
val eventLogStore = storeModule.eventLogStore()
val loggingStore = storeModule.loggingStore()

val logger: (String, String) -> Unit = { tag, message ->
Log.e(tag, message)
applicationScope.launch { eventLogStore.insert(tag, message) }
applicationScope.launch {
if (loggingStore.isEnabled()) {
eventLogStore.insert(tag, message)
}
}
}
attachAppLogger(logger)
_appLogger = logger
Expand Down
335 changes: 61 additions & 274 deletions app/src/main/kotlin/app/dapk/st/graph/AppModule.kt

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions app/src/main/kotlin/app/dapk/st/graph/AppTaskRunner.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package app.dapk.st.graph

import app.dapk.st.matrix.push.PushService
import app.dapk.st.engine.ChatEngine
import app.dapk.st.push.PushTokenPayload
import app.dapk.st.work.TaskRunner
import io.ktor.client.plugins.*
import kotlinx.serialization.json.Json

class AppTaskRunner(
private val pushService: PushService,
private val chatEngine: ChatEngine,
) {

suspend fun run(workTask: TaskRunner.RunnableWorkTask): TaskRunner.TaskResult {
return when (val type = workTask.task.type) {
"push_token" -> {
runCatching {
val payload = Json.decodeFromString(PushTokenPayload.serializer(), workTask.task.jsonPayload)
pushService.registerPush(payload.token, payload.gatewayUrl)
chatEngine.registerPushToken(payload.token, payload.gatewayUrl)
}.fold(
onSuccess = { TaskRunner.TaskResult.Success(workTask.source) },
onFailure = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class BackgroundWorkAdapter(private val workScheduler: WorkScheduler) : Backgrou
WorkScheduler.WorkTask(
jobId = 1,
type = task.type,
jsonPayload = task.jsonPayload,
jsonPayload = task.jsonPayload.value,
)
)
}
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/kotlin/app/dapk/st/graph/TaskRunnerAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package app.dapk.st.graph

import app.dapk.st.matrix.MatrixTaskRunner
import app.dapk.st.engine.ChatEngine
import app.dapk.st.engine.ChatEngineTask
import app.dapk.st.work.TaskRunner

class TaskRunnerAdapter(
private val matrixTaskRunner: suspend (MatrixTaskRunner.MatrixTask) -> MatrixTaskRunner.TaskResult,
private val chatEngine: ChatEngine,
private val appTaskRunner: AppTaskRunner,
) : TaskRunner {

override suspend fun run(tasks: List<TaskRunner.RunnableWorkTask>): List<TaskRunner.TaskResult> {
return tasks.map {
when {
it.task.type.startsWith("matrix") -> {
when (val result = matrixTaskRunner(MatrixTaskRunner.MatrixTask(it.task.type, it.task.jsonPayload))) {
is MatrixTaskRunner.TaskResult.Failure -> TaskRunner.TaskResult.Failure(it.source, canRetry = result.canRetry)
MatrixTaskRunner.TaskResult.Success -> TaskRunner.TaskResult.Success(it.source)
when (val result = chatEngine.runTask(ChatEngineTask(it.task.type, it.task.jsonPayload))) {
is app.dapk.st.engine.TaskRunner.TaskResult.Failure -> TaskRunner.TaskResult.Failure(it.source, canRetry = result.canRetry)
app.dapk.st.engine.TaskRunner.TaskResult.Success -> TaskRunner.TaskResult.Success(it.source)
}
}

else -> appTaskRunner.run(it)
}
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ ext.applyCrashlyticsIfRelease = { project ->
ext.kotlinTest = { dependencies ->
dependencies.testImplementation Dependencies.mavenCentral.kluent
dependencies.testImplementation Dependencies.mavenCentral.kotlinTest
dependencies.testImplementation "org.jetbrains.kotlin:kotlin-test-junit:1.6.10"
dependencies.testImplementation "org.jetbrains.kotlin:kotlin-test-junit:1.7.20"
dependencies.testImplementation 'io.mockk:mockk:1.13.2'
dependencies.testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4'

Expand Down
13 changes: 13 additions & 0 deletions chat-engine/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
id 'kotlin'
id 'java-test-fixtures'
}

dependencies {
api Dependencies.mavenCentral.kotlinCoroutinesCore
api project(":matrix:common")

kotlinFixtures(it)
testFixturesImplementation(testFixtures(project(":matrix:common")))
testFixturesImplementation(testFixtures(project(":core")))
}
82 changes: 82 additions & 0 deletions chat-engine/src/main/kotlin/app/dapk/st/engine/ChatEngine.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package app.dapk.st.engine

import app.dapk.st.matrix.common.EventId
import app.dapk.st.matrix.common.JsonString
import app.dapk.st.matrix.common.RoomId
import app.dapk.st.matrix.common.RoomMember
import kotlinx.coroutines.flow.Flow
import java.io.InputStream

interface ChatEngine : TaskRunner {

fun directory(): Flow<DirectoryState>
fun invites(): Flow<InviteState>
fun messages(roomId: RoomId, disableReadReceipts: Boolean): Flow<MessengerState>

fun notificationsMessages(): Flow<UnreadNotifications>
fun notificationsInvites(): Flow<InviteNotification>

suspend fun login(request: LoginRequest): LoginResult

suspend fun me(forceRefresh: Boolean): Me

suspend fun InputStream.importRoomKeys(password: String): Flow<ImportResult>

suspend fun send(message: SendMessage, room: RoomOverview)

suspend fun registerPushToken(token: String, gatewayUrl: String)

suspend fun joinRoom(roomId: RoomId)

suspend fun rejectJoinRoom(roomId: RoomId)

suspend fun findMembersSummary(roomId: RoomId): List<RoomMember>

fun mediaDecrypter(): MediaDecrypter

fun pushHandler(): PushHandler

}

interface TaskRunner {

suspend fun runTask(task: ChatEngineTask): TaskResult

sealed interface TaskResult {
object Success : TaskResult
data class Failure(val canRetry: Boolean) : TaskResult
}

}


data class ChatEngineTask(val type: String, val jsonPayload: String)

interface MediaDecrypter {

fun decrypt(input: InputStream, k: String, iv: String): Collector

fun interface Collector {
fun collect(partial: (ByteArray) -> Unit)
}

}

interface PushHandler {
fun onNewToken(payload: JsonString)
fun onMessageReceived(eventId: EventId?, roomId: RoomId?)
}

typealias UnreadNotifications = Pair<Map<RoomOverview, List<RoomEvent>>, NotificationDiff>

data class NotificationDiff(
val unchanged: Map<RoomId, List<EventId>>,
val changedOrNew: Map<RoomId, List<EventId>>,
val removed: Map<RoomId, List<EventId>>,
val newRooms: Set<RoomId>
)

data class InviteNotification(
val content: String,
val roomId: RoomId
)
Loading

0 comments on commit fc9a864

Please sign in to comment.