From a450dcbac16c93965f7f61dd920959bb7fe49c13 Mon Sep 17 00:00:00 2001 From: greenart7c3 Date: Mon, 28 Aug 2023 11:58:48 -0300 Subject: [PATCH] add support for add/remove public bookmarks --- .../vitorpamplona/amethyst/model/Account.kt | 99 ++++++++++++++++++- .../amethyst/ui/note/UserProfilePicture.kt | 46 ++++++++- .../ui/screen/loggedIn/AccountViewModel.kt | 8 ++ 3 files changed, 150 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt index 4bf6b75eb..2758d368b 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -1381,6 +1381,54 @@ class Account( LocalCache.consume(event) } + fun addPublicBookmark(note: Note, decryptedContent: String): BookmarkListEvent? { + val bookmarks = userProfile().latestBookmarkList + + val privTags = mutableListOf>() + + val privEvents = bookmarks?.privateTaggedEvents(decryptedContent) ?: emptyList() + val privUsers = bookmarks?.privateTaggedUsers(decryptedContent) ?: emptyList() + val privAddresses = bookmarks?.privateTaggedAddresses(decryptedContent) ?: emptyList() + + privEvents.forEach { + privTags.add(listOf("e", it)) + } + privUsers.forEach { + privTags.add(listOf("p", it)) + } + privAddresses.forEach { + privTags.add(listOf("a", it.toTag())) + } + val msg = Event.mapper.writeValueAsString(privTags) + + AmberUtils.encryptBookmark(msg, keyPair.pubKey.toHexKey()) + + if (AmberUtils.content.isBlank()) { + return null + } + + val event = if (note is AddressableNote) { + BookmarkListEvent.create( + "bookmark", + bookmarks?.taggedEvents() ?: emptyList(), + bookmarks?.taggedUsers() ?: emptyList(), + bookmarks?.taggedAddresses()?.plus(note.address) ?: listOf(note.address), + AmberUtils.content, + keyPair.pubKey.toHexKey() + ) + } else { + BookmarkListEvent.create( + "bookmark", + bookmarks?.taggedEvents()?.plus(note.idHex) ?: listOf(note.idHex), + bookmarks?.taggedUsers() ?: emptyList(), + bookmarks?.taggedAddresses() ?: emptyList(), + AmberUtils.content, + keyPair.pubKey.toHexKey() + ) + } + return event + } + fun addPublicBookmark(note: Note) { if (!isWriteable()) return @@ -1498,6 +1546,55 @@ class Account( LocalCache.consume(event) } + fun removePublicBookmark(note: Note, decryptedContent: String): BookmarkListEvent? { + val bookmarks = userProfile().latestBookmarkList + + val privTags = mutableListOf>() + + val privEvents = bookmarks?.privateTaggedEvents(decryptedContent) ?: emptyList() + val privUsers = bookmarks?.privateTaggedUsers(decryptedContent) ?: emptyList() + val privAddresses = bookmarks?.privateTaggedAddresses(decryptedContent) ?: emptyList() + + privEvents.forEach { + privTags.add(listOf("e", it)) + } + privUsers.forEach { + privTags.add(listOf("p", it)) + } + privAddresses.forEach { + privTags.add(listOf("a", it.toTag())) + } + val msg = Event.mapper.writeValueAsString(privTags) + + AmberUtils.encryptBookmark(msg, keyPair.pubKey.toHexKey()) + + if (AmberUtils.content.isBlank()) { + return null + } + + val event = if (note is AddressableNote) { + BookmarkListEvent.create( + "bookmark", + bookmarks?.taggedEvents() ?: emptyList(), + bookmarks?.taggedUsers() ?: emptyList(), + bookmarks?.taggedAddresses()?.minus(note.address), + AmberUtils.content, + keyPair.pubKey.toHexKey() + ) + } else { + BookmarkListEvent.create( + "bookmark", + bookmarks?.taggedEvents()?.minus(note.idHex), + bookmarks?.taggedUsers() ?: emptyList(), + bookmarks?.taggedAddresses() ?: emptyList(), + AmberUtils.content, + keyPair.pubKey.toHexKey() + ) + } + + return event + } + fun isInPrivateBookmarks(note: Note): Boolean { if (!isWriteable() && !loginWithAmber) return false @@ -1521,7 +1618,7 @@ class Account( } fun isInPublicBookmarks(note: Note): Boolean { - if (!isWriteable()) return false + if (!isWriteable() && !loginWithAmber) return false if (note is AddressableNote) { return userProfile().latestBookmarkList?.taggedAddresses()?.contains(note.address) == true diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/UserProfilePicture.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/UserProfilePicture.kt index 4d65502bb..054e0a3ee 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/UserProfilePicture.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/UserProfilePicture.kt @@ -517,11 +517,53 @@ fun NoteDropDownMenu(note: Note, popupExpanded: MutableState, accountVi } } if (state.isPublicBookmarkNote) { - DropdownMenuItem(onClick = { scope.launch(Dispatchers.IO) { accountViewModel.removePublicBookmark(note); onDismiss() } }) { + DropdownMenuItem( + onClick = { + scope.launch(Dispatchers.IO) { + if (accountViewModel.loggedInWithAmber()) { + val bookmarks = accountViewModel.userProfile().latestBookmarkList + AmberUtils.decryptBookmark( + bookmarks?.content ?: "", + accountViewModel.account.keyPair.pubKey.toHexKey() + ) + bookmarks?.decryptedContent = AmberUtils.content + AmberUtils.content = "" + event = accountViewModel.removePublicBookmark( + note, + bookmarks?.decryptedContent ?: "" + ) + } else { + accountViewModel.removePublicBookmark(note) + onDismiss() + } + } + } + ) { Text(stringResource(R.string.remove_from_public_bookmarks)) } } else { - DropdownMenuItem(onClick = { scope.launch(Dispatchers.IO) { accountViewModel.addPublicBookmark(note); onDismiss() } }) { + DropdownMenuItem( + onClick = { + scope.launch(Dispatchers.IO) { + if (accountViewModel.loggedInWithAmber()) { + val bookmarks = accountViewModel.userProfile().latestBookmarkList + AmberUtils.decryptBookmark( + bookmarks?.content ?: "", + accountViewModel.account.keyPair.pubKey.toHexKey() + ) + bookmarks?.decryptedContent = AmberUtils.content + AmberUtils.content = "" + event = accountViewModel.addPublicBookmark( + note, + bookmarks?.decryptedContent ?: "" + ) + } else { + accountViewModel.addPublicBookmark(note) + onDismiss() + } + } + } + ) { Text(stringResource(R.string.add_to_public_bookmarks)) } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt index 556ff39d3..5e3742b42 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt @@ -315,6 +315,14 @@ class AccountViewModel(val account: Account) : ViewModel() { return account.addPrivateBookmark(note, decryptedContent) } + fun addPublicBookmark(note: Note, decryptedContent: String): BookmarkListEvent? { + return account.addPublicBookmark(note, decryptedContent) + } + + fun removePublicBookmark(note: Note, decryptedContent: String): BookmarkListEvent? { + return account.removePublicBookmark(note, decryptedContent) + } + fun addPublicBookmark(note: Note) { account.addPublicBookmark(note) }