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

Use the PublicKey type for public keys in the database #1291

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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: 2 additions & 2 deletions atox/src/androidTest/kotlin/IntegrationTest.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2020-2024 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2020-2025 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: GPL-3.0-only

Expand Down Expand Up @@ -34,8 +34,8 @@ import ltd.evilcorp.atox.di.DaoModule
import ltd.evilcorp.atox.di.ViewModelModule
import ltd.evilcorp.atox.tox.BootstrapNodeRegistryImpl
import ltd.evilcorp.core.db.Database
import ltd.evilcorp.core.vo.PublicKey
import ltd.evilcorp.domain.tox.BootstrapNodeRegistry
import ltd.evilcorp.domain.tox.PublicKey
import ltd.evilcorp.domain.tox.SaveManager
import org.hamcrest.core.AllOf.allOf
import org.junit.Rule
Expand Down
9 changes: 6 additions & 3 deletions atox/src/androidTest/kotlin/ui/AvatarFactoryTest.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2022-2025 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: GPL-3.0-only

Expand All @@ -7,16 +7,19 @@ package ltd.evilcorp.atox.ui
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import kotlin.test.Test
import ltd.evilcorp.core.vo.PublicKey
import org.junit.runner.RunWith

private val pk = PublicKey("123")

@RunWith(AndroidJUnit4::class)
class AvatarFactoryTest {
@Test
fun emptyName() {
AvatarFactory.create(
InstrumentationRegistry.getInstrumentation().targetContext.resources,
name = "",
publicKey = "123",
pk = pk,
)
}

Expand All @@ -25,7 +28,7 @@ class AvatarFactoryTest {
AvatarFactory.create(
InstrumentationRegistry.getInstrumentation().targetContext.resources,
name = "a ",
publicKey = "123",
pk = pk,
)
}
}
14 changes: 7 additions & 7 deletions atox/src/main/kotlin/ActionReceiver.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021-2024 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2025 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2022 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only
Expand All @@ -23,12 +23,12 @@ import kotlinx.coroutines.withContext
import ltd.evilcorp.atox.ui.NotificationHelper
import ltd.evilcorp.core.repository.ContactRepository
import ltd.evilcorp.core.vo.Contact
import ltd.evilcorp.core.vo.PublicKey
import ltd.evilcorp.core.vo.UserStatus
import ltd.evilcorp.domain.feature.CallManager
import ltd.evilcorp.domain.feature.CallState
import ltd.evilcorp.domain.feature.ChatManager
import ltd.evilcorp.domain.feature.ContactManager
import ltd.evilcorp.domain.tox.PublicKey
import ltd.evilcorp.domain.tox.Tox

const val KEY_TEXT_REPLY = "key_text_reply"
Expand Down Expand Up @@ -76,18 +76,18 @@ class ActionReceiver : BroadcastReceiver() {
Log.e(TAG, "Got intent without required key $KEY_CONTACT_PK $intent")
return@launch
}
if (!contactRepository.exists(pk.string())) {
if (!contactRepository.exists(pk)) {
notificationHelper.dismissNotifications(pk)
notificationHelper.dismissCallNotification(pk)
return@launch
}

RemoteInput.getResultsFromIntent(intent)?.let { results ->
results.getCharSequence(KEY_TEXT_REPLY)?.toString()?.let { input ->
contactRepository.setHasUnreadMessages(pk.string(), false)
contactRepository.setHasUnreadMessages(pk, false)
chatManager.sendMessage(pk, input)
notificationHelper.showMessageNotification(
Contact(pk.string(), tox.getName()),
Contact(pk, tox.getName()),
input,
outgoing = true,
)
Expand All @@ -103,7 +103,7 @@ class ActionReceiver : BroadcastReceiver() {
}
Action.CallIgnore -> callManager.removePendingCall(pk)
Action.MarkAsRead -> {
contactRepository.setHasUnreadMessages(pk.string(), false)
contactRepository.setHasUnreadMessages(pk, false)
notificationHelper.dismissNotifications(pk)
}
null -> Log.e(TAG, "Missing action in intent $intent")
Expand All @@ -117,7 +117,7 @@ class ActionReceiver : BroadcastReceiver() {
it
} else {
Log.e(TAG, "Unable to get contact ${pk.fingerprint()} for call notification")
Contact(publicKey = pk.string(), name = pk.fingerprint())
Contact(publicKey = pk, name = pk.fingerprint())
}
}

Expand Down
4 changes: 2 additions & 2 deletions atox/src/main/kotlin/ToxService.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2024 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2019-2025 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2022 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only
Expand Down Expand Up @@ -139,7 +139,7 @@ class ToxService : LifecycleService() {
}

lifecycleScope.launch(Dispatchers.Default) {
userRepository.get(tox.publicKey.string())
userRepository.get(tox.publicKey)
.filterNotNull()
.filter { it.connectionStatus != connectionStatus }
.flowWithLifecycle(lifecycle)
Expand Down
25 changes: 11 additions & 14 deletions atox/src/main/kotlin/tox/EventListenerCallbacks.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2024 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2019-2025 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: GPL-3.0-only

Expand Down Expand Up @@ -30,13 +30,13 @@ import ltd.evilcorp.core.vo.FileKind
import ltd.evilcorp.core.vo.FileTransfer
import ltd.evilcorp.core.vo.FriendRequest
import ltd.evilcorp.core.vo.Message
import ltd.evilcorp.core.vo.PublicKey
import ltd.evilcorp.core.vo.Sender
import ltd.evilcorp.core.vo.UserStatus
import ltd.evilcorp.domain.av.AudioPlayer
import ltd.evilcorp.domain.feature.CallManager
import ltd.evilcorp.domain.feature.ChatManager
import ltd.evilcorp.domain.feature.FileTransferManager
import ltd.evilcorp.domain.tox.PublicKey
import ltd.evilcorp.domain.tox.Tox
import ltd.evilcorp.domain.tox.ToxAvEventListener
import ltd.evilcorp.domain.tox.ToxEventListener
Expand All @@ -52,9 +52,6 @@ private fun isImage(filename: String) = try {
false
}

private const val FINGERPRINT_LEN = 8
private fun String.fingerprint() = this.take(FINGERPRINT_LEN)

@Singleton
class EventListenerCallbacks @Inject constructor(
private val ctx: Context,
Expand All @@ -73,7 +70,7 @@ class EventListenerCallbacks @Inject constructor(
private var audioPlayer: AudioPlayer? = null
private val scope = CoroutineScope(Dispatchers.Default)

private suspend fun tryGetContact(pk: String, tag: String) = contactRepository.get(pk).firstOrNull().let {
private suspend fun tryGetContact(pk: PublicKey, tag: String) = contactRepository.get(pk).firstOrNull().let {
if (it == null) Log.e(TAG, "$tag -> unable to get contact for ${pk.fingerprint()}")
it
}
Expand Down Expand Up @@ -147,7 +144,7 @@ class EventListenerCallbacks @Inject constructor(
}

fileRecvHandler = { publicKey, fileNo, kind, fileSize, filename ->
val name = if (kind == FileKind.Avatar.ordinal) publicKey else filename
val name = if (kind == FileKind.Avatar.ordinal) publicKey.string() else filename

val id = fileTransferManager.add(FileTransfer(publicKey, fileNo, kind, fileSize, name, outgoing = false))

Expand All @@ -168,16 +165,16 @@ class EventListenerCallbacks @Inject constructor(
}
}

fileRecvControlHandler = { publicKey: String, fileNo: Int, control: ToxFileControl ->
fileTransferManager.setStatus(publicKey, fileNo, control)
fileRecvControlHandler = { pk: PublicKey, fileNo: Int, control: ToxFileControl ->
fileTransferManager.setStatus(pk, fileNo, control)
}

fileChunkRequestHandler = { publicKey: String, fileNo: Int, position: Long, length: Int ->
fileTransferManager.sendChunk(publicKey, fileNo, position, length)
fileChunkRequestHandler = { pk: PublicKey, fileNo: Int, position: Long, length: Int ->
fileTransferManager.sendChunk(pk, fileNo, position, length)
}

selfConnectionStatusHandler = { status ->
userRepository.updateConnection(tox.publicKey.string(), status)
userRepository.updateConnection(tox.publicKey, status)
}

friendTypingHandler = { publicKey, isTyping ->
Expand All @@ -201,8 +198,8 @@ class EventListenerCallbacks @Inject constructor(
audioPlayer?.stop()
audioPlayer?.release()
audioPlayer = null
notificationHelper.dismissCallNotification(PublicKey(pk))
callManager.endCall(PublicKey(pk))
notificationHelper.dismissCallNotification(pk)
callManager.endCall(pk)
}
}

Expand Down
4 changes: 2 additions & 2 deletions atox/src/main/kotlin/tox/ToxStarter.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2021 aTox contributors
// SPDX-FileCopyrightText: 2019-2025 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: GPL-3.0-only

Expand All @@ -13,9 +13,9 @@ import im.tox.tox4j.crypto.exceptions.ToxDecryptionException
import javax.inject.Inject
import ltd.evilcorp.atox.ToxService
import ltd.evilcorp.atox.settings.Settings
import ltd.evilcorp.core.vo.PublicKey
import ltd.evilcorp.domain.feature.FileTransferManager
import ltd.evilcorp.domain.feature.UserManager
import ltd.evilcorp.domain.tox.PublicKey
import ltd.evilcorp.domain.tox.SaveManager
import ltd.evilcorp.domain.tox.SaveOptions
import ltd.evilcorp.domain.tox.Tox
Expand Down
8 changes: 5 additions & 3 deletions atox/src/main/kotlin/ui/AvatarFactory.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SPDX-FileCopyrightText: 2019-2022 aTox contributors
// SPDX-FileCopyrightText: 2019-2025 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2022 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only

Expand All @@ -14,6 +15,7 @@ import android.graphics.RectF
import android.graphics.Typeface
import kotlin.math.abs
import ltd.evilcorp.atox.R
import ltd.evilcorp.core.vo.PublicKey

internal object AvatarFactory {

Expand All @@ -27,7 +29,7 @@ internal object AvatarFactory {
fun create(
resources: Resources,
name: String,
publicKey: String,
pk: PublicKey,
size: Px = Px(resources.getDimension(R.dimen.default_avatar_size).toInt()),
): Bitmap {
val defaultAvatarSize = resources.getDimension(R.dimen.default_avatar_size)
Expand All @@ -37,7 +39,7 @@ internal object AvatarFactory {
val canvas = Canvas(bitmap)
val rect = RectF(0f, 0f, bitmap.width.toFloat(), bitmap.height.toFloat())
val colors = resources.getIntArray(R.array.contactBackgrounds)
val backgroundPaint = Paint().apply { color = colors[abs(publicKey.hashCode()).rem(colors.size)] }
val backgroundPaint = Paint().apply { color = colors[abs(pk.hashCode()).rem(colors.size)] }

val textPaint = Paint().apply {
color = Color.WHITE
Expand Down
5 changes: 3 additions & 2 deletions atox/src/main/kotlin/ui/AvatarImageView.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-FileCopyrightText: 2022 aTox contributors
// SPDX-FileCopyrightText: 2022-2024 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2022-2025 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: GPL-3.0-only

Expand All @@ -21,6 +21,7 @@ import kotlin.math.sqrt
import ltd.evilcorp.atox.R
import ltd.evilcorp.core.vo.ConnectionStatus
import ltd.evilcorp.core.vo.Contact
import ltd.evilcorp.core.vo.PublicKey
import ltd.evilcorp.core.vo.UserStatus

private const val STATUS_INDICATOR_SIZE_RATIO_WITH_AVATAR = 12f / 50
Expand Down Expand Up @@ -50,7 +51,7 @@ class AvatarImageView @JvmOverloads constructor(context: Context, attrs: Attribu
}

private var name = ""
private var publicKey = ""
private var publicKey = PublicKey("")
private var avatarUri = ""

fun setFrom(contact: Contact) {
Expand Down
Loading
Loading