mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-25 15:00:55 +02:00
Uses Hex to idex Users
This commit is contained in:
@@ -39,7 +39,7 @@ class Account(
|
|||||||
) {
|
) {
|
||||||
|
|
||||||
fun userProfile(): User {
|
fun userProfile(): User {
|
||||||
return LocalCache.getOrCreateUser(loggedIn.pubKey)
|
return LocalCache.getOrCreateUser(loggedIn.pubKey.toHexKey())
|
||||||
}
|
}
|
||||||
|
|
||||||
fun followingChannels(): List<Channel> {
|
fun followingChannels(): List<Channel> {
|
||||||
@@ -47,7 +47,7 @@ class Account(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun hiddenUsers(): List<User> {
|
fun hiddenUsers(): List<User> {
|
||||||
return hiddenUsers.map { LocalCache.getOrCreateUser(it.toByteArray()) }
|
return hiddenUsers.map { LocalCache.getOrCreateUser(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isWriteable(): Boolean {
|
fun isWriteable(): Boolean {
|
||||||
|
@@ -17,6 +17,10 @@ class Channel(val id: ByteArray) {
|
|||||||
|
|
||||||
val notes = ConcurrentHashMap<HexKey, Note>()
|
val notes = ConcurrentHashMap<HexKey, Note>()
|
||||||
|
|
||||||
|
fun toBestDisplayName(): String {
|
||||||
|
return info.name ?: idDisplayHex
|
||||||
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun addNote(note: Note) {
|
fun addNote(note: Note) {
|
||||||
notes[note.idHex] = note
|
notes[note.idHex] = note
|
||||||
|
@@ -11,6 +11,17 @@ import nostr.postr.toHex
|
|||||||
/** Makes the distinction between String and Hex **/
|
/** Makes the distinction between String and Hex **/
|
||||||
typealias HexKey = String
|
typealias HexKey = String
|
||||||
|
|
||||||
|
typealias NPubKey = String
|
||||||
|
typealias NoteId = String
|
||||||
|
|
||||||
|
fun NPubKey.toDisplayKey(): String {
|
||||||
|
return this.toShortenHex()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun NoteId.toDisplayId(): String {
|
||||||
|
return this.toShortenHex()
|
||||||
|
}
|
||||||
|
|
||||||
fun ByteArray.toNote() = Bech32.encodeBytes(hrp = "note", this, Bech32.Encoding.Bech32)
|
fun ByteArray.toNote() = Bech32.encodeBytes(hrp = "note", this, Bech32.Encoding.Bech32)
|
||||||
|
|
||||||
fun ByteArray.toHexKey(): HexKey {
|
fun ByteArray.toHexKey(): HexKey {
|
||||||
|
@@ -41,10 +41,9 @@ object LocalCache {
|
|||||||
val channels = ConcurrentHashMap<HexKey, Channel>()
|
val channels = ConcurrentHashMap<HexKey, Channel>()
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun getOrCreateUser(pubkey: ByteArray): User {
|
fun getOrCreateUser(key: HexKey): User {
|
||||||
val key = pubkey.toHexKey()
|
|
||||||
return users[key] ?: run {
|
return users[key] ?: run {
|
||||||
val answer = User(pubkey)
|
val answer = User(key)
|
||||||
users.put(key, answer)
|
users.put(key, answer)
|
||||||
answer
|
answer
|
||||||
}
|
}
|
||||||
@@ -71,7 +70,7 @@ object LocalCache {
|
|||||||
|
|
||||||
fun consume(event: MetadataEvent) {
|
fun consume(event: MetadataEvent) {
|
||||||
// new event
|
// new event
|
||||||
val oldUser = getOrCreateUser(event.pubKey)
|
val oldUser = getOrCreateUser(event.pubKey.toHexKey())
|
||||||
if (event.createdAt > oldUser.updatedMetadataAt) {
|
if (event.createdAt > oldUser.updatedMetadataAt) {
|
||||||
//Log.d("MT", "New User ${users.size} ${event.contactMetaData.name}")
|
//Log.d("MT", "New User ${users.size} ${event.contactMetaData.name}")
|
||||||
|
|
||||||
@@ -98,7 +97,7 @@ object LocalCache {
|
|||||||
fun consume(event: TextNoteEvent, relay: Relay? = null) {
|
fun consume(event: TextNoteEvent, relay: Relay? = null) {
|
||||||
val note = getOrCreateNote(event.id.toHex())
|
val note = getOrCreateNote(event.id.toHex())
|
||||||
|
|
||||||
val author = getOrCreateUser(event.pubKey)
|
val author = getOrCreateUser(event.pubKey.toHexKey())
|
||||||
|
|
||||||
if (relay != null)
|
if (relay != null)
|
||||||
author.addRelay(relay, event.createdAt)
|
author.addRelay(relay, event.createdAt)
|
||||||
@@ -106,7 +105,7 @@ object LocalCache {
|
|||||||
// Already processed this event.
|
// Already processed this event.
|
||||||
if (note.event != null) return
|
if (note.event != null) return
|
||||||
|
|
||||||
val mentions = Collections.synchronizedList(event.mentions.map { getOrCreateUser(decodePublicKey(it)) })
|
val mentions = Collections.synchronizedList(event.mentions.map { getOrCreateUser(it) })
|
||||||
val replyTo = Collections.synchronizedList(event.replyTos.map { getOrCreateNote(it) }.toMutableList())
|
val replyTo = Collections.synchronizedList(event.replyTos.map { getOrCreateNote(it) }.toMutableList())
|
||||||
|
|
||||||
note.loadEvent(event, author, mentions, replyTo)
|
note.loadEvent(event, author, mentions, replyTo)
|
||||||
@@ -137,7 +136,7 @@ object LocalCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun consume(event: ContactListEvent) {
|
fun consume(event: ContactListEvent) {
|
||||||
val user = getOrCreateUser(event.pubKey)
|
val user = getOrCreateUser(event.pubKey.toHexKey())
|
||||||
|
|
||||||
if (event.createdAt > user.updatedFollowsAt) {
|
if (event.createdAt > user.updatedFollowsAt) {
|
||||||
//Log.d("CL", "AAA ${user.toBestDisplayName()} ${event.follows.size}")
|
//Log.d("CL", "AAA ${user.toBestDisplayName()} ${event.follows.size}")
|
||||||
@@ -145,7 +144,7 @@ object LocalCache {
|
|||||||
event.follows.map {
|
event.follows.map {
|
||||||
try {
|
try {
|
||||||
val pubKey = decodePublicKey(it.pubKeyHex)
|
val pubKey = decodePublicKey(it.pubKeyHex)
|
||||||
getOrCreateUser(pubKey)
|
getOrCreateUser(pubKey.toHexKey())
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
println("Could not parse Hex key: ${it.pubKeyHex}")
|
println("Could not parse Hex key: ${it.pubKeyHex}")
|
||||||
println("UpdateFollows: " + event.toJson())
|
println("UpdateFollows: " + event.toJson())
|
||||||
@@ -179,13 +178,13 @@ object LocalCache {
|
|||||||
// Already processed this event.
|
// Already processed this event.
|
||||||
if (note.event != null) return
|
if (note.event != null) return
|
||||||
|
|
||||||
val author = getOrCreateUser(event.pubKey)
|
val author = getOrCreateUser(event.pubKey.toHexKey())
|
||||||
val recipient = event.recipientPubKey?.let { getOrCreateUser(it) }
|
val recipient = event.recipientPubKey?.let { getOrCreateUser(it.toHexKey()) }
|
||||||
|
|
||||||
//Log.d("PM", "${author.toBestDisplayName()} to ${recipient?.toBestDisplayName()}")
|
//Log.d("PM", "${author.toBestDisplayName()} to ${recipient?.toBestDisplayName()}")
|
||||||
|
|
||||||
val repliesTo = event.tags.filter { it.firstOrNull() == "e" }.mapNotNull { it.getOrNull(1) }.map { getOrCreateNote(it) }.toMutableList()
|
val repliesTo = event.tags.filter { it.firstOrNull() == "e" }.mapNotNull { it.getOrNull(1) }.map { getOrCreateNote(it) }.toMutableList()
|
||||||
val mentions = event.tags.filter { it.firstOrNull() == "p" }.mapNotNull { it.getOrNull(1) }.map { getOrCreateUser(decodePublicKey(it)) }
|
val mentions = event.tags.filter { it.firstOrNull() == "p" }.mapNotNull { it.getOrNull(1) }.map { getOrCreateUser(it) }
|
||||||
|
|
||||||
note.loadEvent(event, author, mentions, repliesTo)
|
note.loadEvent(event, author, mentions, repliesTo)
|
||||||
|
|
||||||
@@ -209,8 +208,8 @@ object LocalCache {
|
|||||||
|
|
||||||
//Log.d("TN", "New Boost (${notes.size},${users.size}) ${note.author?.toBestDisplayName()} ${formattedDateTime(event.createdAt)}")
|
//Log.d("TN", "New Boost (${notes.size},${users.size}) ${note.author?.toBestDisplayName()} ${formattedDateTime(event.createdAt)}")
|
||||||
|
|
||||||
val author = getOrCreateUser(event.pubKey)
|
val author = getOrCreateUser(event.pubKey.toHexKey())
|
||||||
val mentions = event.originalAuthor.map { getOrCreateUser(decodePublicKey(it)) }.toList()
|
val mentions = event.originalAuthor.map { getOrCreateUser(it) }.toList()
|
||||||
val repliesTo = event.boostedPost.map { getOrCreateNote(it) }.toMutableList()
|
val repliesTo = event.boostedPost.map { getOrCreateNote(it) }.toMutableList()
|
||||||
|
|
||||||
note.loadEvent(event, author, mentions, repliesTo)
|
note.loadEvent(event, author, mentions, repliesTo)
|
||||||
@@ -235,13 +234,13 @@ object LocalCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun consume(event: ReactionEvent) {
|
fun consume(event: ReactionEvent) {
|
||||||
val note = getOrCreateNote(event.id.toHex())
|
val note = getOrCreateNote(event.id.toHexKey())
|
||||||
|
|
||||||
// Already processed this event.
|
// Already processed this event.
|
||||||
if (note.event != null) return
|
if (note.event != null) return
|
||||||
|
|
||||||
val author = getOrCreateUser(event.pubKey)
|
val author = getOrCreateUser(event.pubKey.toHexKey())
|
||||||
val mentions = event.originalAuthor.map { getOrCreateUser(decodePublicKey(it)) }
|
val mentions = event.originalAuthor.map { getOrCreateUser(it) }
|
||||||
val repliesTo = event.originalPost.map { getOrCreateNote(it) }.toMutableList()
|
val repliesTo = event.originalPost.map { getOrCreateNote(it) }.toMutableList()
|
||||||
|
|
||||||
note.loadEvent(event, author, mentions, repliesTo)
|
note.loadEvent(event, author, mentions, repliesTo)
|
||||||
@@ -285,8 +284,8 @@ object LocalCache {
|
|||||||
// Already processed this event.
|
// Already processed this event.
|
||||||
if (note.event != null) return
|
if (note.event != null) return
|
||||||
|
|
||||||
val author = getOrCreateUser(event.pubKey)
|
val author = getOrCreateUser(event.pubKey.toHexKey())
|
||||||
val mentions = event.reportedAuthor.map { getOrCreateUser(decodePublicKey(it)) }
|
val mentions = event.reportedAuthor.map { getOrCreateUser(it) }
|
||||||
val repliesTo = event.reportedPost.map { getOrCreateNote(it) }.toMutableList()
|
val repliesTo = event.reportedPost.map { getOrCreateNote(it) }.toMutableList()
|
||||||
|
|
||||||
note.loadEvent(event, author, mentions, repliesTo)
|
note.loadEvent(event, author, mentions, repliesTo)
|
||||||
@@ -305,7 +304,7 @@ object LocalCache {
|
|||||||
//Log.d("MT", "New Event ${event.content} ${event.id.toHex()}")
|
//Log.d("MT", "New Event ${event.content} ${event.id.toHex()}")
|
||||||
// new event
|
// new event
|
||||||
val oldChannel = getOrCreateChannel(event.id.toHex())
|
val oldChannel = getOrCreateChannel(event.id.toHex())
|
||||||
val author = getOrCreateUser(event.pubKey)
|
val author = getOrCreateUser(event.pubKey.toHexKey())
|
||||||
if (event.createdAt > oldChannel.updatedMetadataAt) {
|
if (event.createdAt > oldChannel.updatedMetadataAt) {
|
||||||
if (oldChannel.creator == null || oldChannel.creator == author) {
|
if (oldChannel.creator == null || oldChannel.creator == author) {
|
||||||
oldChannel.updateChannelInfo(author, event.channelInfo, event.createdAt)
|
oldChannel.updateChannelInfo(author, event.channelInfo, event.createdAt)
|
||||||
@@ -327,7 +326,7 @@ object LocalCache {
|
|||||||
|
|
||||||
// new event
|
// new event
|
||||||
val oldChannel = getOrCreateChannel(event.channel)
|
val oldChannel = getOrCreateChannel(event.channel)
|
||||||
val author = getOrCreateUser(event.pubKey)
|
val author = getOrCreateUser(event.pubKey.toHexKey())
|
||||||
if (event.createdAt > oldChannel.updatedMetadataAt) {
|
if (event.createdAt > oldChannel.updatedMetadataAt) {
|
||||||
if (oldChannel.creator == null || oldChannel.creator == author) {
|
if (oldChannel.creator == null || oldChannel.creator == author) {
|
||||||
oldChannel.updateChannelInfo(author, event.channelInfo, event.createdAt)
|
oldChannel.updateChannelInfo(author, event.channelInfo, event.createdAt)
|
||||||
@@ -355,11 +354,18 @@ object LocalCache {
|
|||||||
// Already processed this event.
|
// Already processed this event.
|
||||||
if (note.event != null) return
|
if (note.event != null) return
|
||||||
|
|
||||||
val author = getOrCreateUser(event.pubKey)
|
val author = getOrCreateUser(event.pubKey.toHexKey())
|
||||||
val mentions = Collections.synchronizedList(event.mentions.map { getOrCreateUser(decodePublicKey(it)) })
|
val mentions = Collections.synchronizedList(event.mentions.map { getOrCreateUser(it) })
|
||||||
val replyTo = Collections.synchronizedList(
|
val replyTo = Collections.synchronizedList(
|
||||||
event.replyTos
|
event.replyTos
|
||||||
.map { getOrCreateNote(it) }
|
.mapNotNull {
|
||||||
|
try {
|
||||||
|
getOrCreateNote(it)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("Failed to parse Key: $it")
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
.filter { it.event !is ChannelCreateEvent }
|
.filter { it.event !is ChannelCreateEvent }
|
||||||
.toMutableList()
|
.toMutableList()
|
||||||
)
|
)
|
||||||
|
@@ -5,6 +5,7 @@ import com.vitorpamplona.amethyst.service.NostrSingleUserDataSource
|
|||||||
import com.vitorpamplona.amethyst.service.relays.Client
|
import com.vitorpamplona.amethyst.service.relays.Client
|
||||||
import com.vitorpamplona.amethyst.service.relays.Relay
|
import com.vitorpamplona.amethyst.service.relays.Relay
|
||||||
import com.vitorpamplona.amethyst.ui.note.toShortenHex
|
import com.vitorpamplona.amethyst.ui.note.toShortenHex
|
||||||
|
import fr.acinq.secp256k1.Hex
|
||||||
import java.util.Collections
|
import java.util.Collections
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
@@ -17,8 +18,8 @@ import nostr.postr.events.Event
|
|||||||
import nostr.postr.events.MetadataEvent
|
import nostr.postr.events.MetadataEvent
|
||||||
import nostr.postr.toNpub
|
import nostr.postr.toNpub
|
||||||
|
|
||||||
class User(val pubkey: ByteArray) {
|
class User(val pubkeyHex: String) {
|
||||||
val pubkeyHex = pubkey.toHexKey()
|
val pubkey = Hex.decode(pubkeyHex)
|
||||||
val pubkeyDisplayHex = pubkey.toNpub().toShortenHex()
|
val pubkeyDisplayHex = pubkey.toNpub().toShortenHex()
|
||||||
|
|
||||||
var info = UserMetadata()
|
var info = UserMetadata()
|
||||||
|
@@ -14,7 +14,7 @@ object NostrUserProfileDataSource: NostrDataSource<Note>("UserProfileFeed") {
|
|||||||
var user: User? = null
|
var user: User? = null
|
||||||
|
|
||||||
fun loadUserProfile(userId: String) {
|
fun loadUserProfile(userId: String) {
|
||||||
user = LocalCache.getOrCreateUser(userId.toByteArray())
|
user = LocalCache.getOrCreateUser(userId)
|
||||||
resetFilters()
|
resetFilters()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -16,6 +16,7 @@ import com.vitorpamplona.amethyst.model.LocalCache
|
|||||||
import com.vitorpamplona.amethyst.model.Note
|
import com.vitorpamplona.amethyst.model.Note
|
||||||
import com.vitorpamplona.amethyst.model.User
|
import com.vitorpamplona.amethyst.model.User
|
||||||
import com.vitorpamplona.amethyst.model.decodePublicKey
|
import com.vitorpamplona.amethyst.model.decodePublicKey
|
||||||
|
import com.vitorpamplona.amethyst.model.toHexKey
|
||||||
import com.vitorpamplona.amethyst.ui.components.isValidURL
|
import com.vitorpamplona.amethyst.ui.components.isValidURL
|
||||||
import com.vitorpamplona.amethyst.ui.components.noProtocolUrlValidator
|
import com.vitorpamplona.amethyst.ui.components.noProtocolUrlValidator
|
||||||
import nostr.postr.toNpub
|
import nostr.postr.toNpub
|
||||||
@@ -78,7 +79,7 @@ class NewPostViewModel: ViewModel() {
|
|||||||
val restOfWord = word.substring(64)
|
val restOfWord = word.substring(64)
|
||||||
|
|
||||||
val key = decodePublicKey(keyB32.removePrefix("@"))
|
val key = decodePublicKey(keyB32.removePrefix("@"))
|
||||||
val user = LocalCache.getOrCreateUser(key)
|
val user = LocalCache.getOrCreateUser(key.toHexKey())
|
||||||
|
|
||||||
val index = addUserToMentionsIfNotInAndReturnIndex(user)
|
val index = addUserToMentionsIfNotInAndReturnIndex(user)
|
||||||
|
|
||||||
|
@@ -12,6 +12,7 @@ import androidx.compose.ui.text.input.VisualTransformation
|
|||||||
import androidx.compose.ui.text.style.TextDecoration
|
import androidx.compose.ui.text.style.TextDecoration
|
||||||
import com.vitorpamplona.amethyst.model.LocalCache
|
import com.vitorpamplona.amethyst.model.LocalCache
|
||||||
import com.vitorpamplona.amethyst.model.decodePublicKey
|
import com.vitorpamplona.amethyst.model.decodePublicKey
|
||||||
|
import com.vitorpamplona.amethyst.model.toHexKey
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
data class RangesChanges(val original: TextRange, val modified: TextRange)
|
data class RangesChanges(val original: TextRange, val modified: TextRange)
|
||||||
@@ -43,7 +44,7 @@ fun buildAnnotatedStringWithUrlHighlighting(text: AnnotatedString, color: Color)
|
|||||||
val endIndex = startIndex + keyB32.length
|
val endIndex = startIndex + keyB32.length
|
||||||
|
|
||||||
val key = decodePublicKey(keyB32.removePrefix("@"))
|
val key = decodePublicKey(keyB32.removePrefix("@"))
|
||||||
val user = LocalCache.getOrCreateUser(key)
|
val user = LocalCache.getOrCreateUser(key.toHexKey())
|
||||||
|
|
||||||
val newWord = "@${user.toBestDisplayName()}"
|
val newWord = "@${user.toBestDisplayName()}"
|
||||||
val startNew = builderAfter.toString().length
|
val startNew = builderAfter.toString().length
|
||||||
|
Reference in New Issue
Block a user