mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-10-06 23:03:17 +02:00
Merge pull request #261 from Chemaclass/refactor-LocalCache
Refactor LocalCache
This commit is contained in:
@@ -58,7 +58,7 @@ object LocalCache {
|
|||||||
|
|
||||||
fun checkGetOrCreateUser(key: String): User? {
|
fun checkGetOrCreateUser(key: String): User? {
|
||||||
return try {
|
return try {
|
||||||
val checkHex = Hex.decode(key).toNpub() // Checks if this is a valid Hex
|
checkIfValidHex(key)
|
||||||
getOrCreateUser(key)
|
getOrCreateUser(key)
|
||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IllegalArgumentException) {
|
||||||
Log.e("LocalCache", "Invalid Key to create user: $key", e)
|
Log.e("LocalCache", "Invalid Key to create user: $key", e)
|
||||||
@@ -80,7 +80,7 @@ object LocalCache {
|
|||||||
return checkGetOrCreateAddressableNote(key)
|
return checkGetOrCreateAddressableNote(key)
|
||||||
}
|
}
|
||||||
return try {
|
return try {
|
||||||
val checkHex = Hex.decode(key).toNote() // Checks if this is a valid Hex
|
checkIfValidHex(key)
|
||||||
getOrCreateNote(key)
|
getOrCreateNote(key)
|
||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IllegalArgumentException) {
|
||||||
Log.e("LocalCache", "Invalid Key to create note: $key", e)
|
Log.e("LocalCache", "Invalid Key to create note: $key", e)
|
||||||
@@ -99,7 +99,7 @@ object LocalCache {
|
|||||||
|
|
||||||
fun checkGetOrCreateChannel(key: String): Channel? {
|
fun checkGetOrCreateChannel(key: String): Channel? {
|
||||||
return try {
|
return try {
|
||||||
val checkHex = Hex.decode(key).toNote() // Checks if this is a valid Hex
|
checkIfValidHex(key)
|
||||||
getOrCreateChannel(key)
|
getOrCreateChannel(key)
|
||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IllegalArgumentException) {
|
||||||
Log.e("LocalCache", "Invalid Key to create channel: $key", e)
|
Log.e("LocalCache", "Invalid Key to create channel: $key", e)
|
||||||
@@ -107,6 +107,10 @@ object LocalCache {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun checkIfValidHex(key: String) {
|
||||||
|
Hex.decode(key).toNpub()
|
||||||
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun getOrCreateChannel(key: String): Channel {
|
fun getOrCreateChannel(key: String): Channel {
|
||||||
return channels[key] ?: run {
|
return channels[key] ?: run {
|
||||||
@@ -585,10 +589,11 @@ object LocalCache {
|
|||||||
|
|
||||||
fun consume(event: ChannelCreateEvent) {
|
fun consume(event: ChannelCreateEvent) {
|
||||||
// Log.d("MT", "New Event ${event.content} ${event.id.toHex()}")
|
// Log.d("MT", "New Event ${event.content} ${event.id.toHex()}")
|
||||||
// new event
|
|
||||||
val oldChannel = getOrCreateChannel(event.id)
|
val oldChannel = getOrCreateChannel(event.id)
|
||||||
val author = getOrCreateUser(event.pubKey)
|
val author = getOrCreateUser(event.pubKey)
|
||||||
if (event.createdAt > oldChannel.updatedMetadataAt) {
|
if (event.createdAt <= oldChannel.updatedMetadataAt) {
|
||||||
|
return // older data, does nothing
|
||||||
|
}
|
||||||
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)
|
||||||
|
|
||||||
@@ -598,9 +603,6 @@ object LocalCache {
|
|||||||
|
|
||||||
refreshObservers()
|
refreshObservers()
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// older data, does nothing
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun consume(event: ChannelMetadataEvent) {
|
fun consume(event: ChannelMetadataEvent) {
|
||||||
@@ -795,7 +797,7 @@ object LocalCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun pruneOldAndHiddenMessages(account: Account) {
|
fun pruneOldAndHiddenMessages(account: Account) {
|
||||||
channels.forEach {
|
channels.forEach { it ->
|
||||||
val toBeRemoved = it.value.pruneOldAndHiddenMessages(account)
|
val toBeRemoved = it.value.pruneOldAndHiddenMessages(account)
|
||||||
|
|
||||||
toBeRemoved.forEach {
|
toBeRemoved.forEach {
|
||||||
@@ -811,7 +813,7 @@ object LocalCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Counts the replies
|
// Counts the replies
|
||||||
it.replyTo?.forEach { replyingNote ->
|
it.replyTo?.forEach { _ ->
|
||||||
it.removeReply(it)
|
it.removeReply(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user