mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-04-08 11:58:03 +02:00
add support for status
This commit is contained in:
parent
bd836a9580
commit
59cde5699e
@ -1453,65 +1453,97 @@ class Account(
|
||||
}
|
||||
|
||||
fun updateStatus(oldStatus: AddressableNote, newStatus: String) {
|
||||
if (!isWriteable()) return
|
||||
if (!isWriteable() && !loginWithAmber) 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)
|
||||
LocalCache.consume(event, null)
|
||||
}
|
||||
|
||||
fun createStatus(newStatus: String) {
|
||||
if (!isWriteable()) return
|
||||
|
||||
val event = StatusEvent.create(newStatus, "general", expiration = null, keyPair.privKey!!)
|
||||
if (!isWriteable() && !loginWithAmber) return
|
||||
|
||||
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)
|
||||
LocalCache.consume(event, null)
|
||||
}
|
||||
|
||||
fun deleteStatus(oldStatus: AddressableNote) {
|
||||
if (!isWriteable()) return
|
||||
if (!isWriteable() && !loginWithAmber) 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)
|
||||
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)
|
||||
LocalCache.consume(event2)
|
||||
}
|
||||
|
||||
fun removeEmojiPack(usersEmojiList: Note, emojiList: Note) {
|
||||
if (!isWriteable()) return
|
||||
if (!isWriteable() && !loginWithAmber) return
|
||||
|
||||
val noteEvent = usersEmojiList.event
|
||||
if (noteEvent !is EmojiPackSelectionEvent) return
|
||||
val emojiListEvent = emojiList.event
|
||||
if (emojiListEvent !is EmojiPackEvent) return
|
||||
|
||||
val event = EmojiPackSelectionEvent.create(
|
||||
var event = EmojiPackSelectionEvent.create(
|
||||
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)
|
||||
LocalCache.consume(event)
|
||||
}
|
||||
|
||||
fun addEmojiPack(usersEmojiList: Note, emojiList: Note) {
|
||||
if (!isWriteable()) return
|
||||
if (!isWriteable() && !loginWithAmber) return
|
||||
val emojiListEvent = emojiList.event
|
||||
if (emojiListEvent !is EmojiPackEvent) return
|
||||
|
||||
val event = if (usersEmojiList.event == null) {
|
||||
var event = if (usersEmojiList.event == null) {
|
||||
EmojiPackSelectionEvent.create(
|
||||
listOf(emojiListEvent.address()),
|
||||
keyPair.privKey!!
|
||||
keyPair
|
||||
)
|
||||
} else {
|
||||
val noteEvent = usersEmojiList.event
|
||||
@ -1523,10 +1555,18 @@ class Account(
|
||||
|
||||
EmojiPackSelectionEvent.create(
|
||||
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)
|
||||
LocalCache.consume(event)
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import androidx.compose.runtime.Immutable
|
||||
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||
import com.vitorpamplona.quartz.encoders.toHexKey
|
||||
import com.vitorpamplona.quartz.crypto.CryptoUtils
|
||||
import com.vitorpamplona.quartz.crypto.KeyPair
|
||||
import com.vitorpamplona.quartz.encoders.ATag
|
||||
import com.vitorpamplona.quartz.encoders.HexKey
|
||||
|
||||
@ -21,11 +22,11 @@ class EmojiPackSelectionEvent(
|
||||
|
||||
fun create(
|
||||
listOfEmojiPacks: List<ATag>?,
|
||||
privateKey: ByteArray,
|
||||
keyPair: KeyPair,
|
||||
createdAt: Long = TimeUtils.now()
|
||||
): EmojiPackSelectionEvent {
|
||||
val msg = ""
|
||||
val pubKey = CryptoUtils.pubkeyCreate(privateKey).toHexKey()
|
||||
val pubKey = keyPair.pubKey.toHexKey()
|
||||
val tags = mutableListOf<List<String>>()
|
||||
|
||||
listOfEmojiPacks?.forEach {
|
||||
@ -33,8 +34,14 @@ class EmojiPackSelectionEvent(
|
||||
}
|
||||
|
||||
val id = generateId(pubKey, createdAt, kind, tags, msg)
|
||||
val sig = CryptoUtils.sign(id, privateKey)
|
||||
return EmojiPackSelectionEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig.toHexKey())
|
||||
val sig = if (keyPair.privKey == null) null else CryptoUtils.sign(id, keyPair.privKey)
|
||||
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.encoders.toHexKey
|
||||
import com.vitorpamplona.quartz.crypto.CryptoUtils
|
||||
import com.vitorpamplona.quartz.crypto.KeyPair
|
||||
import com.vitorpamplona.quartz.encoders.ATag
|
||||
import com.vitorpamplona.quartz.encoders.HexKey
|
||||
|
||||
@ -24,7 +25,7 @@ class StatusEvent(
|
||||
msg: String,
|
||||
type: String,
|
||||
expiration: Long?,
|
||||
privateKey: ByteArray,
|
||||
keyPair: KeyPair,
|
||||
createdAt: Long = TimeUtils.now()
|
||||
): StatusEvent {
|
||||
val tags = mutableListOf<List<String>>()
|
||||
@ -32,36 +33,43 @@ class StatusEvent(
|
||||
tags.add(listOf("d", type))
|
||||
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 sig = CryptoUtils.sign(id, privateKey)
|
||||
return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig.toHexKey())
|
||||
val sig = if (keyPair.privKey == null) null else CryptoUtils.sign(id, keyPair.privKey)
|
||||
return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig?.toHexKey() ?: "")
|
||||
}
|
||||
|
||||
fun update(
|
||||
event: StatusEvent,
|
||||
newStatus: String,
|
||||
privateKey: ByteArray,
|
||||
keyPair: KeyPair,
|
||||
createdAt: Long = TimeUtils.now()
|
||||
): StatusEvent {
|
||||
val tags = event.tags
|
||||
val pubKey = event.pubKey()
|
||||
val id = generateId(pubKey, createdAt, kind, tags, newStatus)
|
||||
val sig = CryptoUtils.sign(id, privateKey)
|
||||
return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, newStatus, sig.toHexKey())
|
||||
val sig = if (keyPair.privKey == null) null else CryptoUtils.sign(id, keyPair.privKey)
|
||||
return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, newStatus, sig?.toHexKey() ?: "")
|
||||
}
|
||||
|
||||
fun clear(
|
||||
event: StatusEvent,
|
||||
privateKey: ByteArray,
|
||||
keyPair: KeyPair,
|
||||
createdAt: Long = TimeUtils.now()
|
||||
): StatusEvent {
|
||||
val msg = ""
|
||||
val tags = event.tags.filter { it.size > 1 && it[0] == "d" }
|
||||
val pubKey = event.pubKey()
|
||||
val id = generateId(pubKey, createdAt, kind, tags, msg)
|
||||
val sig = CryptoUtils.sign(id, privateKey)
|
||||
return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig.toHexKey())
|
||||
val sig = if (keyPair.privKey == null) null else CryptoUtils.sign(id, keyPair.privKey)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user