sign unfollow event

This commit is contained in:
greenart7c3
2023-08-09 07:39:03 -03:00
parent 3a08db2dfb
commit b761aaf970
5 changed files with 58 additions and 21 deletions
@@ -618,12 +618,20 @@ class Account(
LocalCache.consume(event)
}
fun unfollow(user: User) {
if (!isWriteable()) return
fun unfollow(user: User, signEvent: Boolean = true): ContactListEvent? {
if (!isWriteable() && signEvent) return null
val contactList = migrateCommunitiesAndChannelsIfNeeded(userProfile().latestContactList)
if (contactList != null && contactList.tags.isNotEmpty()) {
if (!signEvent) {
return ContactListEvent.unfollowUser(
contactList,
user.pubkeyHex,
keyPair.pubKey.toHexKey()
)
}
val event = ContactListEvent.unfollowUser(
contactList,
user.pubkeyHex,
@@ -633,6 +641,8 @@ class Account(
Client.send(event)
LocalCache.consume(event)
}
return null
}
fun unfollowHashtag(tag: String) {
@@ -3,7 +3,15 @@ package com.vitorpamplona.amethyst.service
import android.content.Context
object PackageUtils {
fun isPackageInstalled(context: Context, target: String): Boolean {
private fun isPackageInstalled(context: Context, target: String): Boolean {
return context.packageManager.getInstalledApplications(0).find { info -> info.packageName == target } != null
}
fun isOrbotInstalled(context: Context): Boolean {
return isPackageInstalled(context, "org.torproject.android")
}
fun isAmberInstalled(context: Context): Boolean {
return isPackageInstalled(context, "com.greenart7c3.nostrsigner")
}
}
@@ -165,6 +165,17 @@ class ContactListEvent(
)
}
fun unfollowUser(earlierVersion: ContactListEvent, pubKeyHex: String, pubKey: HexKey, createdAt: Long = TimeUtils.now()): ContactListEvent {
if (!earlierVersion.isTaggedUser(pubKeyHex)) return earlierVersion
return create(
content = earlierVersion.content,
tags = earlierVersion.tags.filter { it.size > 1 && it[1] != pubKeyHex },
pubKey = pubKey,
createdAt = createdAt
)
}
fun unfollowUser(earlierVersion: ContactListEvent, pubKeyHex: String, privateKey: ByteArray, createdAt: Long = TimeUtils.now()): ContactListEvent {
if (!earlierVersion.isTaggedUser(pubKeyHex)) return earlierVersion
@@ -768,14 +768,18 @@ private fun DisplayFollowUnfollowButton(
if (isLoggedInFollowingUser) {
UnfollowButton {
if (!accountViewModel.isWriteable()) {
scope.launch {
Toast
.makeText(
context,
context.getString(R.string.login_with_a_private_key_to_be_able_to_unfollow),
Toast.LENGTH_SHORT
)
.show()
if (PackageUtils.isAmberInstalled(context)) {
event = accountViewModel.account.unfollow(baseUser, false)
} else {
scope.launch {
Toast
.makeText(
context,
context.getString(R.string.login_with_a_private_key_to_be_able_to_unfollow),
Toast.LENGTH_SHORT
)
.show()
}
}
} else {
scope.launch(Dispatchers.IO) {
@@ -787,14 +791,18 @@ private fun DisplayFollowUnfollowButton(
if (isUserFollowingLoggedIn) {
FollowButton(R.string.follow_back) {
if (!accountViewModel.isWriteable()) {
scope.launch {
Toast
.makeText(
context,
context.getString(R.string.login_with_a_private_key_to_be_able_to_follow),
Toast.LENGTH_SHORT
)
.show()
if (PackageUtils.isAmberInstalled(context)) {
event = accountViewModel.account.follow(baseUser, false)
} else {
scope.launch {
Toast
.makeText(
context,
context.getString(R.string.login_with_a_private_key_to_be_able_to_follow),
Toast.LENGTH_SHORT
)
.show()
}
}
} else {
scope.launch(Dispatchers.IO) {
@@ -805,7 +813,7 @@ private fun DisplayFollowUnfollowButton(
} else {
FollowButton(R.string.follow) {
if (!accountViewModel.isWriteable()) {
if (PackageUtils.isPackageInstalled(context, "com.greenart7c3.nostrsigner")) {
if (PackageUtils.isAmberInstalled(context)) {
event = accountViewModel.account.follow(baseUser, false)
} else {
scope.launch {
@@ -229,7 +229,7 @@ fun LoginPage(
}
}
if (PackageUtils.isPackageInstalled(context, "org.torproject.android")) {
if (PackageUtils.isOrbotInstalled(context)) {
Row(verticalAlignment = Alignment.CenterVertically) {
Checkbox(
checked = useProxy.value,