Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from Jackson to plain JSONObjects for exporting message history #1179

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,6 @@ maven_install(
"androidx.room:room-runtime:2.2.6",
"androidx.room:room-testing:2.2.6",
"androidx.test.ext:junit:1.1.2",
"com.fasterxml.jackson.core:jackson-annotations:2.13.4",
"com.fasterxml.jackson.core:jackson-core:2.13.4",
"com.fasterxml.jackson.core:jackson-databind:2.13.4",
"com.fasterxml.jackson.module:jackson-module-kotlin:2.13.4",
"com.google.android.material:material:1.4.0",
"com.google.code.gson:gson:2.8.6",
"com.google.guava:guava:19.0",
Expand Down
4 changes: 0 additions & 4 deletions domain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ kt_android_library(
":tox4j",
"//core",
"@maven//:androidx_core_core_ktx",
"@maven//:com_fasterxml_jackson_core_jackson_annotations",
"@maven//:com_fasterxml_jackson_core_jackson_core",
"@maven//:com_fasterxml_jackson_core_jackson_databind",
"@maven//:com_fasterxml_jackson_module_jackson_module_kotlin",
],
)

Expand Down
6 changes: 0 additions & 6 deletions domain/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ dependencies {
api(libs.tox4j.api)
implementation(libs.tox4j.c)

// JSON
implementation(libs.jackson.kotlin)
implementation(libs.jackson.databind)
implementation(libs.jackson.core)
implementation(libs.jackson.annotations)

testImplementation(kotlin("test"))
androidTestImplementation(kotlin("test"))
androidTestImplementation(libs.test.runner)
Expand Down
44 changes: 25 additions & 19 deletions domain/src/main/kotlin/feature/ExportManager.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// SPDX-FileCopyrightText: 2022 Akito <the@akito.ooo>
// SPDX-FileCopyrightText: 2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: GPL-3.0-only

package ltd.evilcorp.domain.feature

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
Expand All @@ -13,24 +13,36 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import ltd.evilcorp.core.repository.MessageRepository
import ltd.evilcorp.core.vo.Message
import org.json.JSONArray
import org.json.JSONObject

class ExportManager @Inject constructor(
private val messageRepository: MessageRepository,
) {
// TODO @Akito: Increment version programmatically on major changes.
private fun List<Message>.generateExportMessages(): ExportMessages {
return ExportMessages(
version = 1,
timestamp = SimpleDateFormat(
"""yyyy-MM-dd'T'HH-mm-ss""",
Locale.getDefault(),
).format(Date()),
entries = this,
private fun generateExportMessagesJString(messages: List<Message>): String {
val root = JSONObject()
root.put("version", 1)
robinlinden marked this conversation as resolved.
Show resolved Hide resolved
root.put(
"timestamp",
SimpleDateFormat("""yyyy-MM-dd'T'HH-mm-ss""", Locale.getDefault()).format(Date()),
)
}

private fun generateExportMessagesJString(messages: List<Message>): String =
jacksonObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(messages.generateExportMessages())
val entries = JSONArray()
for (message in messages) {
val jsonMessage = JSONObject().apply {
put("publicKey", message.publicKey)
put("message", message.message)
put("sender", message.sender.toString())
put("type", message.type.toString())
put("correlationId", message.correlationId)
put("timestamp", message.timestamp)
put("id", message.id)
}
entries.put(jsonMessage)
}
robinlinden marked this conversation as resolved.
Show resolved Hide resolved
root.put("entries", entries)
return root.toString(2)
}
robinlinden marked this conversation as resolved.
Show resolved Hide resolved

private fun getMessages(publicKey: String): List<Message> = runBlocking {
messageRepository.get(publicKey).first()
Expand All @@ -40,9 +52,3 @@ class ExportManager @Inject constructor(
getMessages(publicKey),
)
}

data class ExportMessages(
val version: Int,
val timestamp: String,
val entries: List<Message>,
)
6 changes: 0 additions & 6 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ android-plugin = "8.1.2"
coroutines = "1.7.3"
dagger = "2.48"
espresso = "3.5.1"
jackson = "2.15.2"
lifecycle = "2.6.2"
navigation = "2.6.0" # 2.7.5 requires API 34.
room = "2.5.2" # 2.6.0 requires API 34.
Expand Down Expand Up @@ -48,11 +47,6 @@ google-android-material = "com.google.android.material:material:1.9.0" # 1.10.0
google-dagger-compiler = { module = "com.google.dagger:dagger-compiler", version.ref = "dagger" }
google-dagger-core = { module = "com.google.dagger:dagger", version.ref = "dagger" }

jackson-kotlin = { module = "com.fasterxml.jackson.module:jackson-module-kotlin", version.ref = "jackson" }
jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson" }
jackson-core = { module = "com.fasterxml.jackson.core:jackson-core", version.ref = "jackson" }
jackson-annotations = { module = "com.fasterxml.jackson.core:jackson-annotations", version.ref = "jackson" }

javax-inject = "javax.inject:javax.inject:1"

kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" }
Expand Down
Loading