add support for status

This commit is contained in:
greenart7c3
2023-09-04 18:28:11 -03:00
parent bd836a9580
commit 59cde5699e
3 changed files with 87 additions and 32 deletions
@@ -1453,65 +1453,97 @@ class Account(
} }
fun updateStatus(oldStatus: AddressableNote, newStatus: String) { fun updateStatus(oldStatus: AddressableNote, newStatus: String) {
if (!isWriteable()) return if (!isWriteable() && !loginWithAmber) return
val oldEvent = oldStatus.event as? StatusEvent ?: return val oldEvent = oldStatus.event as? StatusEvent ?: return
val event = StatusEvent.update(oldEvent, newStatus, keyPair.privKey!!) var event = StatusEvent.update(oldEvent, newStatus, keyPair)
if (loginWithAmber) {
AmberUtils.openAmber(event)
if (AmberUtils.content.isBlank()) {
return
}
event = StatusEvent.create(event, AmberUtils.content)
}
Client.send(event) Client.send(event)
LocalCache.consume(event, null) LocalCache.consume(event, null)
} }
fun createStatus(newStatus: String) { fun createStatus(newStatus: String) {
if (!isWriteable()) return if (!isWriteable() && !loginWithAmber) return
val event = StatusEvent.create(newStatus, "general", expiration = null, keyPair.privKey!!)
var event = StatusEvent.create(newStatus, "general", expiration = null, keyPair)
if (loginWithAmber) {
AmberUtils.openAmber(event)
if (AmberUtils.content.isBlank()) {
return
}
event = StatusEvent.create(event, AmberUtils.content)
}
Client.send(event) Client.send(event)
LocalCache.consume(event, null) LocalCache.consume(event, null)
} }
fun deleteStatus(oldStatus: AddressableNote) { fun deleteStatus(oldStatus: AddressableNote) {
if (!isWriteable()) return if (!isWriteable() && !loginWithAmber) return
val oldEvent = oldStatus.event as? StatusEvent ?: return val oldEvent = oldStatus.event as? StatusEvent ?: return
val event = StatusEvent.clear(oldEvent, keyPair.privKey!!) var event = StatusEvent.clear(oldEvent, keyPair)
if (loginWithAmber) {
AmberUtils.openAmber(event)
if (AmberUtils.content.isBlank()) {
return
}
event = StatusEvent.create(event, AmberUtils.content)
}
Client.send(event) Client.send(event)
LocalCache.consume(event, null) LocalCache.consume(event, null)
val event2 = DeletionEvent.create(listOf(event.id), keyPair) var event2 = DeletionEvent.create(listOf(event.id), keyPair)
if (loginWithAmber) {
AmberUtils.openAmber(event2)
if (AmberUtils.content.isBlank()) {
return
}
event2 = DeletionEvent.create(event2, AmberUtils.content)
}
Client.send(event2) Client.send(event2)
LocalCache.consume(event2) LocalCache.consume(event2)
} }
fun removeEmojiPack(usersEmojiList: Note, emojiList: Note) { fun removeEmojiPack(usersEmojiList: Note, emojiList: Note) {
if (!isWriteable()) return if (!isWriteable() && !loginWithAmber) return
val noteEvent = usersEmojiList.event val noteEvent = usersEmojiList.event
if (noteEvent !is EmojiPackSelectionEvent) return if (noteEvent !is EmojiPackSelectionEvent) return
val emojiListEvent = emojiList.event val emojiListEvent = emojiList.event
if (emojiListEvent !is EmojiPackEvent) return if (emojiListEvent !is EmojiPackEvent) return
val event = EmojiPackSelectionEvent.create( var event = EmojiPackSelectionEvent.create(
noteEvent.taggedAddresses().filter { it != emojiListEvent.address() }, noteEvent.taggedAddresses().filter { it != emojiListEvent.address() },
keyPair.privKey!! keyPair
) )
if (loginWithAmber) {
AmberUtils.openAmber(event)
if (AmberUtils.content.isBlank()) {
return
}
event = EmojiPackSelectionEvent.create(event, AmberUtils.content)
}
Client.send(event) Client.send(event)
LocalCache.consume(event) LocalCache.consume(event)
} }
fun addEmojiPack(usersEmojiList: Note, emojiList: Note) { fun addEmojiPack(usersEmojiList: Note, emojiList: Note) {
if (!isWriteable()) return if (!isWriteable() && !loginWithAmber) return
val emojiListEvent = emojiList.event val emojiListEvent = emojiList.event
if (emojiListEvent !is EmojiPackEvent) return if (emojiListEvent !is EmojiPackEvent) return
val event = if (usersEmojiList.event == null) { var event = if (usersEmojiList.event == null) {
EmojiPackSelectionEvent.create( EmojiPackSelectionEvent.create(
listOf(emojiListEvent.address()), listOf(emojiListEvent.address()),
keyPair.privKey!! keyPair
) )
} else { } else {
val noteEvent = usersEmojiList.event val noteEvent = usersEmojiList.event
@@ -1523,10 +1555,18 @@ class Account(
EmojiPackSelectionEvent.create( EmojiPackSelectionEvent.create(
noteEvent.taggedAddresses().plus(emojiListEvent.address()), noteEvent.taggedAddresses().plus(emojiListEvent.address()),
keyPair.privKey!! keyPair
) )
} }
if (loginWithAmber) {
AmberUtils.openAmber(event)
if (AmberUtils.content.isBlank()) {
return
}
event = EmojiPackSelectionEvent.create(event, AmberUtils.content)
}
Client.send(event) Client.send(event)
LocalCache.consume(event) LocalCache.consume(event)
} }
@@ -4,6 +4,7 @@ import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.utils.TimeUtils import com.vitorpamplona.quartz.utils.TimeUtils
import com.vitorpamplona.quartz.encoders.toHexKey import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.crypto.CryptoUtils import com.vitorpamplona.quartz.crypto.CryptoUtils
import com.vitorpamplona.quartz.crypto.KeyPair
import com.vitorpamplona.quartz.encoders.ATag import com.vitorpamplona.quartz.encoders.ATag
import com.vitorpamplona.quartz.encoders.HexKey import com.vitorpamplona.quartz.encoders.HexKey
@@ -21,11 +22,11 @@ class EmojiPackSelectionEvent(
fun create( fun create(
listOfEmojiPacks: List<ATag>?, listOfEmojiPacks: List<ATag>?,
privateKey: ByteArray, keyPair: KeyPair,
createdAt: Long = TimeUtils.now() createdAt: Long = TimeUtils.now()
): EmojiPackSelectionEvent { ): EmojiPackSelectionEvent {
val msg = "" val msg = ""
val pubKey = CryptoUtils.pubkeyCreate(privateKey).toHexKey() val pubKey = keyPair.pubKey.toHexKey()
val tags = mutableListOf<List<String>>() val tags = mutableListOf<List<String>>()
listOfEmojiPacks?.forEach { listOfEmojiPacks?.forEach {
@@ -33,8 +34,14 @@ class EmojiPackSelectionEvent(
} }
val id = generateId(pubKey, createdAt, kind, tags, msg) val id = generateId(pubKey, createdAt, kind, tags, msg)
val sig = CryptoUtils.sign(id, privateKey) val sig = if (keyPair.privKey == null) null else CryptoUtils.sign(id, keyPair.privKey)
return EmojiPackSelectionEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig.toHexKey()) return EmojiPackSelectionEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig?.toHexKey() ?: "")
}
fun create(
unsignedEvent: EmojiPackSelectionEvent, signature: String
): EmojiPackSelectionEvent {
return EmojiPackSelectionEvent(unsignedEvent.id, unsignedEvent.pubKey, unsignedEvent.createdAt, unsignedEvent.tags, unsignedEvent.content, signature)
} }
} }
} }
@@ -4,6 +4,7 @@ import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.utils.TimeUtils import com.vitorpamplona.quartz.utils.TimeUtils
import com.vitorpamplona.quartz.encoders.toHexKey import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.crypto.CryptoUtils import com.vitorpamplona.quartz.crypto.CryptoUtils
import com.vitorpamplona.quartz.crypto.KeyPair
import com.vitorpamplona.quartz.encoders.ATag import com.vitorpamplona.quartz.encoders.ATag
import com.vitorpamplona.quartz.encoders.HexKey import com.vitorpamplona.quartz.encoders.HexKey
@@ -24,7 +25,7 @@ class StatusEvent(
msg: String, msg: String,
type: String, type: String,
expiration: Long?, expiration: Long?,
privateKey: ByteArray, keyPair: KeyPair,
createdAt: Long = TimeUtils.now() createdAt: Long = TimeUtils.now()
): StatusEvent { ): StatusEvent {
val tags = mutableListOf<List<String>>() val tags = mutableListOf<List<String>>()
@@ -32,36 +33,43 @@ class StatusEvent(
tags.add(listOf("d", type)) tags.add(listOf("d", type))
expiration?.let { tags.add(listOf("expiration", it.toString())) } expiration?.let { tags.add(listOf("expiration", it.toString())) }
val pubKey = CryptoUtils.pubkeyCreate(privateKey).toHexKey() val pubKey = keyPair.pubKey.toHexKey()
val id = generateId(pubKey, createdAt, kind, tags, msg) val id = generateId(pubKey, createdAt, kind, tags, msg)
val sig = CryptoUtils.sign(id, privateKey) val sig = if (keyPair.privKey == null) null else CryptoUtils.sign(id, keyPair.privKey)
return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig.toHexKey()) return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig?.toHexKey() ?: "")
} }
fun update( fun update(
event: StatusEvent, event: StatusEvent,
newStatus: String, newStatus: String,
privateKey: ByteArray, keyPair: KeyPair,
createdAt: Long = TimeUtils.now() createdAt: Long = TimeUtils.now()
): StatusEvent { ): StatusEvent {
val tags = event.tags val tags = event.tags
val pubKey = event.pubKey() val pubKey = event.pubKey()
val id = generateId(pubKey, createdAt, kind, tags, newStatus) val id = generateId(pubKey, createdAt, kind, tags, newStatus)
val sig = CryptoUtils.sign(id, privateKey) val sig = if (keyPair.privKey == null) null else CryptoUtils.sign(id, keyPair.privKey)
return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, newStatus, sig.toHexKey()) return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, newStatus, sig?.toHexKey() ?: "")
} }
fun clear( fun clear(
event: StatusEvent, event: StatusEvent,
privateKey: ByteArray, keyPair: KeyPair,
createdAt: Long = TimeUtils.now() createdAt: Long = TimeUtils.now()
): StatusEvent { ): StatusEvent {
val msg = "" val msg = ""
val tags = event.tags.filter { it.size > 1 && it[0] == "d" } val tags = event.tags.filter { it.size > 1 && it[0] == "d" }
val pubKey = event.pubKey() val pubKey = event.pubKey()
val id = generateId(pubKey, createdAt, kind, tags, msg) val id = generateId(pubKey, createdAt, kind, tags, msg)
val sig = CryptoUtils.sign(id, privateKey) val sig = if (keyPair.privKey == null) null else CryptoUtils.sign(id, keyPair.privKey)
return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig.toHexKey()) return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig?.toHexKey() ?: "")
}
fun create(
unsignedEvent: StatusEvent,
signature: String
): StatusEvent {
return StatusEvent(unsignedEvent.id, unsignedEvent.pubKey, unsignedEvent.createdAt, unsignedEvent.tags, unsignedEvent.content, signature)
} }
} }
} }