All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.net.folivo.trixnity.clientserverapi.model.discovery.GetSupport.kt Maven / Gradle / Ivy

There is a newer version: 4.7.1
Show newest version
package net.folivo.trixnity.clientserverapi.model.discovery

import io.ktor.resources.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import net.folivo.trixnity.clientserverapi.model.discovery.GetSupport.Response.Contact.Role
import net.folivo.trixnity.core.HttpMethod
import net.folivo.trixnity.core.HttpMethodType.GET
import net.folivo.trixnity.core.MatrixEndpoint
import net.folivo.trixnity.core.WithoutAuth
import net.folivo.trixnity.core.model.UserId

/**
 * @see matrix spec
 */
@Serializable
@Resource("/.well-known/matrix/support")
@HttpMethod(GET)
@WithoutAuth
object GetSupport : MatrixEndpoint {
    @Serializable
    data class Response(
        @SerialName("contacts") val contacts: List = listOf(),
        @SerialName("support_page") val supportPage: String? = null,
    ) {
        @Serializable
        data class Contact(
            @SerialName("email_address") val emailAddress: String? = null,
            @SerialName("matrix_id") val userId: UserId? = null,
            @SerialName("role") val role: Role,
        ) {
            @Serializable(with = RoleSerializer::class)
            sealed interface Role {
                val name: String

                data object Admin : Role {
                    override val name: String = "m.role.admin"
                }

                data object Security : Role {
                    override val name: String = "m.role.security"
                }

                data class Unknown(override val name: String) : Role
            }
        }
    }
}

object RoleSerializer : KSerializer {
    override val descriptor: SerialDescriptor =
        PrimitiveSerialDescriptor("RoleSerializer", PrimitiveKind.STRING)

    override fun deserialize(decoder: Decoder): Role {
        return when (val name = decoder.decodeString()) {
            Role.Admin.name -> Role.Admin
            Role.Security.name -> Role.Security
            else -> Role.Unknown(name)
        }
    }

    override fun serialize(encoder: Encoder, value: Role) {
        encoder.encodeString(value.name)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy