add support for add/remove public bookmarks

This commit is contained in:
greenart7c3
2023-08-28 11:58:48 -03:00
parent ee5fbce2ce
commit a450dcbac1
3 changed files with 150 additions and 3 deletions

View File

@@ -1381,6 +1381,54 @@ class Account(
LocalCache.consume(event)
}
fun addPublicBookmark(note: Note, decryptedContent: String): BookmarkListEvent? {
val bookmarks = userProfile().latestBookmarkList
val privTags = mutableListOf<List<String>>()
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<List<String>>()
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

View File

@@ -517,11 +517,53 @@ fun NoteDropDownMenu(note: Note, popupExpanded: MutableState<Boolean>, 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))
}
}

View File

@@ -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)
}