Skip to content

Commit

Permalink
Fix remember my choice when rejecting
Browse files Browse the repository at this point in the history
  • Loading branch information
greenart7c3 committed Oct 14, 2024
1 parent e3595db commit 799421d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ interface ApplicationDao {
name: String,
)

@Insert(onConflict = OnConflictStrategy.IGNORE)
@Insert(onConflict = OnConflictStrategy.REPLACE)
@Transaction
fun insertPermissions(permissions: List<ApplicationPermissionsEntity>): List<Long>? {
permissions.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.greenart7c3.nostrsigner.service
import android.content.Context
import android.util.Log
import com.greenart7c3.nostrsigner.database.AppDatabase
import com.greenart7c3.nostrsigner.database.ApplicationEntity
import com.greenart7c3.nostrsigner.database.ApplicationPermissionsEntity
import com.greenart7c3.nostrsigner.database.ApplicationWithPermissions
import com.greenart7c3.nostrsigner.models.Account
Expand Down Expand Up @@ -143,34 +142,13 @@ object AmberUtils {
}

fun acceptOrRejectPermission(
application: ApplicationWithPermissions,
key: String,
intentData: IntentData,
kind: Int?,
value: Boolean,
appName: String,
account: Account,
database: AppDatabase,
) {
val application =
database
.applicationDao()
.getByKey(key) ?: ApplicationWithPermissions(
application = ApplicationEntity(
key,
appName,
listOf(),
"",
"",
"",
account.keyPair.pubKey.toHexKey(),
true,
intentData.bunkerRequest?.secret ?: "",
intentData.bunkerRequest?.secret != null,
account.signPolicy,
),
permissions = mutableListOf(),
)

val noPermission = application.permissions.none {
val nip = it.kind?.kindToNip()?.toIntOrNull()
(it.type == intentData.type.toString() && it.kind == kind) || (nip != null && it.type == "NIP" && it.kind == nip)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,43 +380,40 @@ fun MultiEventHomeScreen(

val database = NostrSigner.getInstance().getDatabase(localAccount.keyPair.pubKey.toNpub())

val applicationEntity = database.applicationDao().getByKey(key)
val application =
database
.applicationDao()
.getByKey(key) ?: ApplicationWithPermissions(
application = ApplicationEntity(
key,
"",
listOf(),
"",
"",
"",
localAccount.keyPair.pubKey.toHexKey(),
true,
intentData.bunkerRequest?.secret ?: "",
intentData.bunkerRequest?.secret != null,
localAccount.signPolicy,
),
permissions = mutableListOf(),
)

if (intentData.type == SignerType.SIGN_EVENT) {
val localEvent = intentData.event!!

if (intentData.rememberMyChoice.value) {
AmberUtils.acceptOrRejectPermission(
application,
key,
intentData,
localEvent.kind,
intentData.rememberMyChoice.value,
applicationEntity?.application?.name?.ifBlank { applicationEntity.application.key.toShortenHex() } ?: "",
localAccount,
database,
)
}

val application =
database
.applicationDao()
.getByKey(key) ?: ApplicationWithPermissions(
application = ApplicationEntity(
key,
applicationEntity?.application?.name?.ifBlank { applicationEntity.application.key.toShortenHex() } ?: "",
listOf(),
"",
"",
"",
localAccount.keyPair.pubKey.toHexKey(),
true,
intentData.bunkerRequest?.secret ?: "",
intentData.bunkerRequest?.secret != null,
localAccount.signPolicy,
),
permissions = mutableListOf(),
)

database.applicationDao().insertApplicationWithPermissions(application)

database.applicationDao().addHistory(
Expand All @@ -436,7 +433,7 @@ fun MultiEventHomeScreen(
localAccount,
intentData.bunkerRequest,
BunkerResponse(intentData.bunkerRequest.id, localEvent.toJson(), null),
applicationEntity?.application?.relays ?: emptyList(),
application.application.relays,
onLoading = {},
onDone = {},
)
Expand Down Expand Up @@ -471,38 +468,16 @@ fun MultiEventHomeScreen(
} else if (intentData.type == SignerType.SIGN_MESSAGE) {
if (intentData.rememberMyChoice.value) {
AmberUtils.acceptOrRejectPermission(
application,
key,
intentData,
null,
intentData.rememberMyChoice.value,
applicationEntity?.application?.name?.ifBlank { applicationEntity.application.key.toShortenHex() } ?: "",
localAccount,
database,
)
}

val application =
database
.applicationDao()
.getByKey(key) ?: ApplicationWithPermissions(
application = ApplicationEntity(
key,
applicationEntity?.application?.name?.ifBlank { applicationEntity.application.key.toShortenHex() } ?: "",
listOf(),
"",
"",
"",
localAccount.keyPair.pubKey.toHexKey(),
true,
intentData.bunkerRequest?.secret ?: "",
intentData.bunkerRequest?.secret != null,
localAccount.signPolicy,
),
permissions = mutableListOf(),
)

database.applicationDao().insertApplicationWithPermissions(application)

database.applicationDao().addHistory(
HistoryEntity(
0,
Expand All @@ -522,7 +497,7 @@ fun MultiEventHomeScreen(
localAccount,
intentData.bunkerRequest,
BunkerResponse(intentData.bunkerRequest.id, signedMessage, null),
applicationEntity?.application?.relays ?: emptyList(),
application.application.relays,
onLoading = {},
onDone = {},
)
Expand All @@ -539,36 +514,15 @@ fun MultiEventHomeScreen(
} else {
if (intentData.rememberMyChoice.value) {
AmberUtils.acceptOrRejectPermission(
application,
key,
intentData,
null,
intentData.rememberMyChoice.value,
applicationEntity?.application?.name?.ifBlank { applicationEntity.application.key.toShortenHex() } ?: "",
localAccount,
database,
)
}

val application =
database
.applicationDao()
.getByKey(key) ?: ApplicationWithPermissions(
application = ApplicationEntity(
key,
applicationEntity?.application?.name?.ifBlank { applicationEntity.application.key.toShortenHex() } ?: "",
listOf(),
"",
"",
"",
localAccount.keyPair.pubKey.toHexKey(),
true,
intentData.bunkerRequest?.secret ?: "",
intentData.bunkerRequest?.secret != null,
localAccount.signPolicy,
),
permissions = mutableListOf(),
)

database.applicationDao().insertApplicationWithPermissions(application)

database.applicationDao().addHistory(
Expand All @@ -590,7 +544,7 @@ fun MultiEventHomeScreen(
localAccount,
intentData.bunkerRequest,
BunkerResponse(intentData.bunkerRequest.id, signature, null),
applicationEntity?.application?.relays ?: emptyList(),
application.application.relays,
onLoading = {},
onDone = {},
)
Expand Down
Loading

0 comments on commit 799421d

Please sign in to comment.