amethyst/app/src/main/java/com/vitorpamplona/amethyst/service/model/ReportEvent.kt

104 wiersze
3.6 KiB
Kotlin
Czysty Zwykły widok Historia

package com.vitorpamplona.amethyst.service.model
import com.vitorpamplona.amethyst.model.HexKey
import com.vitorpamplona.amethyst.model.toHexKey
import nostr.postr.Utils
2023-03-07 19:43:34 +00:00
import java.util.Date
2023-02-28 20:24:23 +00:00
data class ReportedKey(val key: String, val reportType: ReportEvent.ReportType)
// NIP 56 event.
2023-03-07 19:43:34 +00:00
class ReportEvent(
id: HexKey,
pubKey: HexKey,
createdAt: Long,
tags: List<List<String>>,
content: String,
sig: HexKey
) : Event(id, pubKey, createdAt, kind, tags, content, sig) {
private fun defaultReportType(): ReportType {
// Works with old and new structures for report.
2023-03-13 17:47:44 +00:00
var reportType = tags.filter { it.firstOrNull() == "report" }.mapNotNull { it.getOrNull(1) }.map { ReportType.valueOf(it.uppercase()) }.firstOrNull()
2023-03-07 19:43:34 +00:00
if (reportType == null) {
2023-03-13 17:47:44 +00:00
reportType = tags.mapNotNull { it.getOrNull(2) }.map { ReportType.valueOf(it.uppercase()) }.firstOrNull()
2023-03-07 19:43:34 +00:00
}
if (reportType == null) {
reportType = ReportType.SPAM
}
return reportType
}
2023-02-28 20:24:23 +00:00
2023-03-07 19:43:34 +00:00
fun reportedPost() = tags
.filter { it.firstOrNull() == "e" && it.getOrNull(1) != null }
.map {
ReportedKey(
it[1],
2023-03-13 17:47:44 +00:00
it.getOrNull(2)?.uppercase()?.let { it1 -> ReportType.valueOf(it1) } ?: defaultReportType()
2023-03-07 19:43:34 +00:00
)
}
fun reportedAuthor() = tags
.filter { it.firstOrNull() == "p" && it.getOrNull(1) != null }
.map {
ReportedKey(
it[1],
2023-03-13 17:47:44 +00:00
it.getOrNull(2)?.uppercase()?.let { it1 -> ReportType.valueOf(it1) } ?: defaultReportType()
2023-03-07 19:43:34 +00:00
)
}
fun taggedAddresses() = tags.filter { it.firstOrNull() == "a" }.mapNotNull {
val aTagValue = it.getOrNull(1)
val relay = it.getOrNull(2)
if (aTagValue != null) ATag.parse(aTagValue, relay) else null
}
2023-03-07 19:43:34 +00:00
companion object {
const val kind = 1984
2023-03-14 17:41:39 +00:00
fun create(
reportedPost: EventInterface,
type: ReportType,
privateKey: ByteArray,
content: String = "",
createdAt: Long = Date().time / 1000
): ReportEvent {
2023-03-07 19:43:34 +00:00
val reportPostTag = listOf("e", reportedPost.id(), type.name.lowercase())
val reportAuthorTag = listOf("p", reportedPost.pubKey(), type.name.lowercase())
2023-03-07 19:43:34 +00:00
val pubKey = Utils.pubkeyCreate(privateKey).toHexKey()
var tags: List<List<String>> = listOf(reportPostTag, reportAuthorTag)
2023-03-07 19:43:34 +00:00
if (reportedPost is LongTextNoteEvent) {
tags = tags + listOf(listOf("a", reportedPost.address().toTag()))
}
2023-03-07 19:43:34 +00:00
val id = generateId(pubKey, createdAt, kind, tags, content)
val sig = Utils.sign(id, privateKey)
return ReportEvent(id.toHexKey(), pubKey, createdAt, tags, content, sig.toHexKey())
}
2023-03-07 19:43:34 +00:00
fun create(reportedUser: String, type: ReportType, privateKey: ByteArray, createdAt: Long = Date().time / 1000): ReportEvent {
val content = ""
2023-03-07 19:43:34 +00:00
val reportAuthorTag = listOf("p", reportedUser, type.name.lowercase())
2023-03-07 19:43:34 +00:00
val pubKey = Utils.pubkeyCreate(privateKey).toHexKey()
val tags: List<List<String>> = listOf(reportAuthorTag)
val id = generateId(pubKey, createdAt, kind, tags, content)
val sig = Utils.sign(id, privateKey)
return ReportEvent(id.toHexKey(), pubKey, createdAt, tags, content, sig.toHexKey())
}
}
2023-03-07 19:43:34 +00:00
enum class ReportType() {
EXPLICIT, // Not used anymore.
ILLEGAL,
SPAM,
IMPERSONATION,
NUDITY,
PROFANITY
}
}