mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-28 09:46:35 +02:00
fix block/unblock
This commit is contained in:
@@ -12,6 +12,7 @@ import coil.disk.DiskCache
|
|||||||
import coil.util.DebugLogger
|
import coil.util.DebugLogger
|
||||||
import com.vitorpamplona.amethyst.model.Account
|
import com.vitorpamplona.amethyst.model.Account
|
||||||
import com.vitorpamplona.amethyst.model.LocalCache
|
import com.vitorpamplona.amethyst.model.LocalCache
|
||||||
|
import com.vitorpamplona.amethyst.service.AmberUtils
|
||||||
import com.vitorpamplona.amethyst.service.HttpClient
|
import com.vitorpamplona.amethyst.service.HttpClient
|
||||||
import com.vitorpamplona.amethyst.service.NostrAccountDataSource
|
import com.vitorpamplona.amethyst.service.NostrAccountDataSource
|
||||||
import com.vitorpamplona.amethyst.service.NostrChannelDataSource
|
import com.vitorpamplona.amethyst.service.NostrChannelDataSource
|
||||||
@@ -41,6 +42,7 @@ object ServiceManager {
|
|||||||
|
|
||||||
fun start(account: Account, context: Context) {
|
fun start(account: Account, context: Context) {
|
||||||
this.account = account
|
this.account = account
|
||||||
|
AmberUtils.account = account
|
||||||
start(context)
|
start(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -108,31 +108,43 @@ class Account(
|
|||||||
live.combineWith(getBlockListNote().live().metadata) { localLive, liveMuteListEvent ->
|
live.combineWith(getBlockListNote().live().metadata) { localLive, liveMuteListEvent ->
|
||||||
val blockList = liveMuteListEvent?.note?.event as? PeopleListEvent
|
val blockList = liveMuteListEvent?.note?.event as? PeopleListEvent
|
||||||
if (loginWithAmber) {
|
if (loginWithAmber) {
|
||||||
if (blockList?.decryptedContent == null) {
|
val id = blockList?.id
|
||||||
GlobalScope.launch(Dispatchers.IO) {
|
if (id != null) {
|
||||||
val content = blockList?.content ?: ""
|
if (AmberUtils.cachedDecryptedContent[blockList.id] == null) {
|
||||||
if (content.isEmpty()) return@launch
|
GlobalScope.launch(Dispatchers.IO) {
|
||||||
AmberUtils.content = ""
|
val content = blockList.content
|
||||||
AmberUtils.decrypt(
|
if (content.isEmpty()) return@launch
|
||||||
content,
|
AmberUtils.content = ""
|
||||||
keyPair.pubKey.toHexKey(),
|
AmberUtils.decryptBlockList(
|
||||||
blockList?.id() ?: ""
|
content,
|
||||||
)
|
keyPair.pubKey.toHexKey(),
|
||||||
blockList?.decryptedContent = AmberUtils.content
|
blockList.id()
|
||||||
live.invalidateData()
|
)
|
||||||
AmberUtils.content = ""
|
blockList.decryptedContent = AmberUtils.cachedDecryptedContent[blockList.id]
|
||||||
}
|
live.invalidateData()
|
||||||
|
}
|
||||||
|
|
||||||
|
LiveHiddenUsers(
|
||||||
|
hiddenUsers = persistentSetOf(),
|
||||||
|
spammers = localLive?.account?.transientHiddenUsers
|
||||||
|
?: persistentSetOf(),
|
||||||
|
showSensitiveContent = showSensitiveContent
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
blockList.decryptedContent = AmberUtils.cachedDecryptedContent[blockList.id]
|
||||||
|
val liveBlockedUsers = blockList.publicAndPrivateUsers(blockList.decryptedContent ?: "")
|
||||||
|
LiveHiddenUsers(
|
||||||
|
hiddenUsers = liveBlockedUsers,
|
||||||
|
spammers = localLive?.account?.transientHiddenUsers
|
||||||
|
?: persistentSetOf(),
|
||||||
|
showSensitiveContent = showSensitiveContent
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
LiveHiddenUsers(
|
LiveHiddenUsers(
|
||||||
hiddenUsers = persistentSetOf(),
|
hiddenUsers = persistentSetOf(),
|
||||||
spammers = localLive?.account?.transientHiddenUsers ?: persistentSetOf(),
|
spammers = localLive?.account?.transientHiddenUsers
|
||||||
showSensitiveContent = showSensitiveContent
|
?: persistentSetOf(),
|
||||||
)
|
|
||||||
} else {
|
|
||||||
val liveBlockedUsers = blockList.publicAndPrivateUsers(blockList.decryptedContent ?: "")
|
|
||||||
LiveHiddenUsers(
|
|
||||||
hiddenUsers = liveBlockedUsers,
|
|
||||||
spammers = localLive?.account?.transientHiddenUsers ?: persistentSetOf(),
|
|
||||||
showSensitiveContent = showSensitiveContent
|
showSensitiveContent = showSensitiveContent
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -2167,8 +2179,8 @@ class Account(
|
|||||||
fun hideUser(pubkeyHex: String) {
|
fun hideUser(pubkeyHex: String) {
|
||||||
val blockList = migrateHiddenUsersIfNeeded(getBlockList())
|
val blockList = migrateHiddenUsersIfNeeded(getBlockList())
|
||||||
if (loginWithAmber) {
|
if (loginWithAmber) {
|
||||||
val content = blockList?.content ?: ""
|
val id = blockList?.id
|
||||||
val encryptedContent = if (content.isBlank()) {
|
val encryptedContent = if (id == null) {
|
||||||
val privateTags = listOf(listOf("p", pubkeyHex))
|
val privateTags = listOf(listOf("p", pubkeyHex))
|
||||||
val msg = Event.mapper.writeValueAsString(privateTags)
|
val msg = Event.mapper.writeValueAsString(privateTags)
|
||||||
|
|
||||||
@@ -2177,12 +2189,15 @@ class Account(
|
|||||||
if (AmberUtils.content.isBlank()) return
|
if (AmberUtils.content.isBlank()) return
|
||||||
AmberUtils.content
|
AmberUtils.content
|
||||||
} else {
|
} else {
|
||||||
AmberUtils.content = ""
|
var decryptedContent = AmberUtils.cachedDecryptedContent[id]
|
||||||
AmberUtils.decrypt(content, keyPair.pubKey.toHexKey(), blockList?.id ?: "")
|
if (decryptedContent == null) {
|
||||||
if (AmberUtils.content.isBlank()) return
|
AmberUtils.content = ""
|
||||||
val decryptedContent = AmberUtils.content
|
AmberUtils.decrypt(blockList.content, keyPair.pubKey.toHexKey(), blockList.id)
|
||||||
AmberUtils.content = ""
|
if (AmberUtils.content.isBlank()) return
|
||||||
val privateTags = blockList?.privateTagsOrEmpty(decryptedContent)?.plus(element = listOf("p", pubkeyHex))
|
decryptedContent = AmberUtils.content
|
||||||
|
}
|
||||||
|
|
||||||
|
val privateTags = blockList.privateTagsOrEmpty(decryptedContent).plus(element = listOf("p", pubkeyHex))
|
||||||
val msg = Event.mapper.writeValueAsString(privateTags)
|
val msg = Event.mapper.writeValueAsString(privateTags)
|
||||||
AmberUtils.content = ""
|
AmberUtils.content = ""
|
||||||
AmberUtils.encrypt(msg, keyPair.pubKey.toHexKey())
|
AmberUtils.encrypt(msg, keyPair.pubKey.toHexKey())
|
||||||
@@ -2251,7 +2266,7 @@ class Account(
|
|||||||
|
|
||||||
if (blockList != null) {
|
if (blockList != null) {
|
||||||
if (loginWithAmber) {
|
if (loginWithAmber) {
|
||||||
val content = blockList.content ?: ""
|
val content = blockList.content
|
||||||
val encryptedContent = if (content.isBlank()) {
|
val encryptedContent = if (content.isBlank()) {
|
||||||
val privateTags = listOf(listOf("p", pubkeyHex))
|
val privateTags = listOf(listOf("p", pubkeyHex))
|
||||||
val msg = Event.mapper.writeValueAsString(privateTags)
|
val msg = Event.mapper.writeValueAsString(privateTags)
|
||||||
@@ -2261,10 +2276,13 @@ class Account(
|
|||||||
if (AmberUtils.content.isBlank()) return
|
if (AmberUtils.content.isBlank()) return
|
||||||
AmberUtils.content
|
AmberUtils.content
|
||||||
} else {
|
} else {
|
||||||
AmberUtils.content = ""
|
var decryptedContent = AmberUtils.cachedDecryptedContent[blockList.id]
|
||||||
AmberUtils.decrypt(content, keyPair.pubKey.toHexKey(), blockList.id)
|
if (decryptedContent == null) {
|
||||||
if (AmberUtils.content.isBlank()) return
|
AmberUtils.content = ""
|
||||||
val decryptedContent = AmberUtils.content
|
AmberUtils.decrypt(blockList.content, keyPair.pubKey.toHexKey(), blockList.id)
|
||||||
|
if (AmberUtils.content.isBlank()) return
|
||||||
|
decryptedContent = AmberUtils.content
|
||||||
|
}
|
||||||
AmberUtils.content = ""
|
AmberUtils.content = ""
|
||||||
val privateTags = blockList.privateTagsOrEmpty(decryptedContent).minus(element = listOf("p", pubkeyHex))
|
val privateTags = blockList.privateTagsOrEmpty(decryptedContent).minus(element = listOf("p", pubkeyHex))
|
||||||
val msg = Event.mapper.writeValueAsString(privateTags)
|
val msg = Event.mapper.writeValueAsString(privateTags)
|
||||||
@@ -2551,8 +2569,16 @@ class Account(
|
|||||||
val event = note.event
|
val event = note.event
|
||||||
return when (event) {
|
return when (event) {
|
||||||
is PrivateDmEvent -> {
|
is PrivateDmEvent -> {
|
||||||
AmberUtils.decrypt(event.content, event.talkingWith(userProfile().pubkeyHex), event.id)
|
if (AmberUtils.cachedDecryptedContent[event.id] == null) {
|
||||||
AmberUtils.cachedDecryptedContent[event.id]
|
AmberUtils.decrypt(
|
||||||
|
event.content,
|
||||||
|
event.talkingWith(userProfile().pubkeyHex),
|
||||||
|
event.id
|
||||||
|
)
|
||||||
|
AmberUtils.cachedDecryptedContent[event.id]
|
||||||
|
} else {
|
||||||
|
AmberUtils.cachedDecryptedContent[event.id]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
is LnZapRequestEvent -> {
|
is LnZapRequestEvent -> {
|
||||||
decryptZapContentAuthor(note)?.content()
|
decryptZapContentAuthor(note)?.content()
|
||||||
|
@@ -4,6 +4,7 @@ import android.content.Intent
|
|||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import androidx.activity.result.ActivityResultLauncher
|
import androidx.activity.result.ActivityResultLauncher
|
||||||
import com.vitorpamplona.amethyst.ServiceManager
|
import com.vitorpamplona.amethyst.ServiceManager
|
||||||
|
import com.vitorpamplona.amethyst.model.Account
|
||||||
import com.vitorpamplona.amethyst.ui.actions.SignerType
|
import com.vitorpamplona.amethyst.ui.actions.SignerType
|
||||||
import com.vitorpamplona.quartz.encoders.HexKey
|
import com.vitorpamplona.quartz.encoders.HexKey
|
||||||
import com.vitorpamplona.quartz.events.Event
|
import com.vitorpamplona.quartz.events.Event
|
||||||
@@ -14,6 +15,7 @@ object AmberUtils {
|
|||||||
var content: String = ""
|
var content: String = ""
|
||||||
var isActivityRunning: Boolean = false
|
var isActivityRunning: Boolean = false
|
||||||
val cachedDecryptedContent = mutableMapOf<HexKey, String>()
|
val cachedDecryptedContent = mutableMapOf<HexKey, String>()
|
||||||
|
lateinit var account: Account
|
||||||
|
|
||||||
fun openAmber(
|
fun openAmber(
|
||||||
data: String,
|
data: String,
|
||||||
@@ -67,6 +69,17 @@ object AmberUtils {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun decryptBlockList(encryptedContent: String, pubKey: HexKey, id: String, signerType: SignerType = SignerType.NIP04_DECRYPT) {
|
||||||
|
isActivityRunning = true
|
||||||
|
openAmber(
|
||||||
|
encryptedContent,
|
||||||
|
signerType,
|
||||||
|
IntentUtils.blockListResultLauncher,
|
||||||
|
pubKey,
|
||||||
|
id
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fun decrypt(encryptedContent: String, pubKey: HexKey, id: String, signerType: SignerType = SignerType.NIP04_DECRYPT) {
|
fun decrypt(encryptedContent: String, pubKey: HexKey, id: String, signerType: SignerType = SignerType.NIP04_DECRYPT) {
|
||||||
if (content.isBlank()) {
|
if (content.isBlank()) {
|
||||||
isActivityRunning = true
|
isActivityRunning = true
|
||||||
|
@@ -21,6 +21,7 @@ import kotlinx.coroutines.launch
|
|||||||
object IntentUtils {
|
object IntentUtils {
|
||||||
lateinit var activityResultLauncher: ActivityResultLauncher<Intent>
|
lateinit var activityResultLauncher: ActivityResultLauncher<Intent>
|
||||||
lateinit var decryptGossipResultLauncher: ActivityResultLauncher<Intent>
|
lateinit var decryptGossipResultLauncher: ActivityResultLauncher<Intent>
|
||||||
|
lateinit var blockListResultLauncher: ActivityResultLauncher<Intent>
|
||||||
val eventCache = LruCache<String, Event>(100)
|
val eventCache = LruCache<String, Event>(100)
|
||||||
|
|
||||||
@OptIn(DelicateCoroutinesApi::class)
|
@OptIn(DelicateCoroutinesApi::class)
|
||||||
@@ -85,6 +86,29 @@ object IntentUtils {
|
|||||||
ServiceManager.shouldPauseService = true
|
ServiceManager.shouldPauseService = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
blockListResultLauncher = activity.registerForActivityResult(
|
||||||
|
ActivityResultContracts.StartActivityForResult()
|
||||||
|
) {
|
||||||
|
if (it.resultCode != Activity.RESULT_OK) {
|
||||||
|
GlobalScope.launch(Dispatchers.Main) {
|
||||||
|
Toast.makeText(
|
||||||
|
Amethyst.instance,
|
||||||
|
"Sign request rejected",
|
||||||
|
Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
val decryptedContent = it.data?.getStringExtra("signature") ?: ""
|
||||||
|
val id = it.data?.getStringExtra("id") ?: ""
|
||||||
|
if (id.isNotBlank()) {
|
||||||
|
AmberUtils.cachedDecryptedContent[id] = decryptedContent
|
||||||
|
AmberUtils.account.live.invalidateData()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AmberUtils.isActivityRunning = false
|
||||||
|
ServiceManager.shouldPauseService = true
|
||||||
|
}
|
||||||
|
|
||||||
decryptGossipResultLauncher = activity.registerForActivityResult(
|
decryptGossipResultLauncher = activity.registerForActivityResult(
|
||||||
ActivityResultContracts.StartActivityForResult()
|
ActivityResultContracts.StartActivityForResult()
|
||||||
) {
|
) {
|
||||||
|
Reference in New Issue
Block a user