Skip to content

Commit

Permalink
Merge pull request #107 from takke/add_labeler_getServices
Browse files Browse the repository at this point in the history
Add `labeler.getServices`
  • Loading branch information
uakihir0 authored Nov 21, 2024
2 parents e65fdf7 + ee05b86 commit 63339dd
Show file tree
Hide file tree
Showing 16 changed files with 289 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/src/commonMain/kotlin/work/socialhub/kbsky/Bluesky.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package work.socialhub.kbsky
import work.socialhub.kbsky.api.app.bsky.ActorResource
import work.socialhub.kbsky.api.app.bsky.FeedResource
import work.socialhub.kbsky.api.app.bsky.GraphResource
import work.socialhub.kbsky.api.app.bsky.LabelerResource
import work.socialhub.kbsky.api.app.bsky.NotificationResource
import work.socialhub.kbsky.api.app.bsky.UnspeccedResource
import work.socialhub.kbsky.api.app.bsky.VideoResource
Expand All @@ -12,6 +13,7 @@ interface Bluesky : ATProtocol {
fun actor(): ActorResource
fun feed(): FeedResource
fun graph(): GraphResource
fun labeler(): LabelerResource
fun notification(): NotificationResource
fun unspecced(): UnspeccedResource
fun video(): VideoResource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ object BlueskyTypes {
const val GraphList = "app.bsky.graph.list"
const val GraphListItem = "app.bsky.graph.listitem"

// Labeler
const val LabelerDefs = "app.bsky.labeler.defs"
const val LabelerGetServices = "app.bsky.labeler.getServices"

// Notification
const val NotificationGetUnreadCount = "app.bsky.notification.getUnreadCount"
const val NotificationListNotifications = "app.bsky.notification.listNotifications"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package work.socialhub.kbsky.api.app.bsky

import work.socialhub.kbsky.api.entity.app.bsky.labeler.LabelerGetServicesRequest
import work.socialhub.kbsky.api.entity.app.bsky.labeler.LabelerGetServicesResponse
import work.socialhub.kbsky.api.entity.share.Response

/**
* Bluesky/Labeler
* [Reference](https://github.com/bluesky-social/atproto/tree/main/lexicons/app/bsky/labeler)
*/
interface LabelerResource {

/**
* Get information about a list of labeler services.
*/
fun getServices(
request: LabelerGetServicesRequest
): Response<LabelerGetServicesResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package work.socialhub.kbsky.api.entity.app.bsky.labeler

import work.socialhub.kbsky.api.entity.share.AuthRequest
import work.socialhub.kbsky.auth.AuthProvider

class LabelerGetServicesRequest(
auth: AuthProvider
) : AuthRequest(auth) {

lateinit var dids: List<String>
var detailed: Boolean? = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package work.socialhub.kbsky.api.entity.app.bsky.labeler

import kotlinx.serialization.Serializable
import work.socialhub.kbsky.model.app.bsky.labeler.LabelerViewUnion

@Serializable
class LabelerGetServicesResponse {
lateinit var views: List<LabelerViewUnion>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import work.socialhub.kbsky.BlueskyConfig
import work.socialhub.kbsky.api.app.bsky.ActorResource
import work.socialhub.kbsky.api.app.bsky.FeedResource
import work.socialhub.kbsky.api.app.bsky.GraphResource
import work.socialhub.kbsky.api.app.bsky.LabelerResource
import work.socialhub.kbsky.api.app.bsky.NotificationResource
import work.socialhub.kbsky.api.app.bsky.UnspeccedResource
import work.socialhub.kbsky.api.app.bsky.VideoResource
import work.socialhub.kbsky.api.chat.bsky.ConvoResource
import work.socialhub.kbsky.internal.app.bsky._ActorResource
import work.socialhub.kbsky.internal.app.bsky._FeedResource
import work.socialhub.kbsky.internal.app.bsky._GraphResource
import work.socialhub.kbsky.internal.app.bsky._LabelerResource
import work.socialhub.kbsky.internal.app.bsky._NotificationResource
import work.socialhub.kbsky.internal.app.bsky._UnspeccedResource
import work.socialhub.kbsky.internal.app.bsky._VideoResource
Expand All @@ -24,6 +26,7 @@ class _Bluesky(
protected val actor: ActorResource = _ActorResource(config)
protected val feed: FeedResource = _FeedResource(config)
protected val graph: GraphResource = _GraphResource(config)
protected val labeler: LabelerResource = _LabelerResource(config)
protected val notification: NotificationResource = _NotificationResource(config)
protected val undoc: UnspeccedResource = _UnspeccedResource(config)
protected val video: VideoResource = _VideoResource(config)
Expand All @@ -44,6 +47,11 @@ class _Bluesky(
*/
override fun graph() = graph

/**
* {@inheritDoc}
*/
override fun labeler() = labeler

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package work.socialhub.kbsky.internal.app.bsky

import kotlinx.coroutines.runBlocking
import work.socialhub.kbsky.BlueskyConfig
import work.socialhub.kbsky.BlueskyTypes.LabelerGetServices
import work.socialhub.kbsky.api.app.bsky.LabelerResource
import work.socialhub.kbsky.api.entity.app.bsky.labeler.LabelerGetServicesRequest
import work.socialhub.kbsky.api.entity.app.bsky.labeler.LabelerGetServicesResponse
import work.socialhub.kbsky.api.entity.share.Response
import work.socialhub.kbsky.internal.share._InternalUtility.getWithAuth
import work.socialhub.kbsky.internal.share._InternalUtility.proceed
import work.socialhub.kbsky.internal.share._InternalUtility.xrpc
import work.socialhub.kbsky.util.MediaType
import work.socialhub.khttpclient.HttpRequest

class _LabelerResource(
private val config: BlueskyConfig
) : LabelerResource {

override fun getServices(
request: LabelerGetServicesRequest
): Response<LabelerGetServicesResponse> {

return proceed {
runBlocking {
HttpRequest()
.url(xrpc(config, LabelerGetServices))
.accept(MediaType.JSON)
.also { req ->
// dids=did1&dids=did2&...
request.dids.forEach {
req.query("dids", it)
}
request.detailed?.let {
req.query("detailed", it)
}
}
.getWithAuth(request.auth)
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package work.socialhub.kbsky.model.app.bsky.labeler

import kotlinx.serialization.Serializable
import work.socialhub.kbsky.model.com.atproto.label.LabelDefsLabelValueDefinition

@Serializable
class LabelerPolicies {

lateinit var labelValues: List<String>

var labelValueDefinitions: List<LabelDefsLabelValueDefinition> = emptyList()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package work.socialhub.kbsky.model.app.bsky.labeler

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import work.socialhub.kbsky.BlueskyTypes
import work.socialhub.kbsky.model.app.bsky.actor.ActorDefsProfileView
import work.socialhub.kbsky.model.com.atproto.label.LabelDefsLabel

@Serializable
class LabelerView : LabelerViewUnion() {

companion object {
val TYPE = BlueskyTypes.LabelerDefs + "#labelerView"
}

@SerialName("\$type")
override var type = TYPE

lateinit var uri: String
lateinit var cid: String
lateinit var creator: ActorDefsProfileView
var likeCount: Int? = null
var viewer: LabelerViewerState? = null
var indexedAt: String? = null
var labels: List<LabelDefsLabel>? = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package work.socialhub.kbsky.model.app.bsky.labeler

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import work.socialhub.kbsky.BlueskyTypes
import work.socialhub.kbsky.model.app.bsky.actor.ActorDefsProfileView
import work.socialhub.kbsky.model.com.atproto.label.LabelDefsLabel

@Serializable
class LabelerViewDetailed : LabelerViewUnion() {

companion object {
val TYPE = BlueskyTypes.LabelerDefs + "#labelerViewDetailed"
}

@SerialName("\$type")
override var type = TYPE

lateinit var uri: String
lateinit var cid: String
lateinit var creator: ActorDefsProfileView
lateinit var policies: LabelerPolicies
var likeCount: Int? = null
var viewer: LabelerViewerState? = null
var indexedAt: String? = null
var labels: List<LabelDefsLabel>? = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package work.socialhub.kbsky.model.app.bsky.labeler

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import work.socialhub.kbsky.util.json.LabelerViewPolymorphicSerializer

/**
* @see LabelerView
* @see LabelerViewDetailed
*/
@Serializable(with = LabelerViewPolymorphicSerializer::class)
abstract class LabelerViewUnion {
@SerialName("\$type")
abstract var type: String

val asLabelerView get() = this as? LabelerView
val asLabelerViewDetailed get() = this as? LabelerViewDetailed
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package work.socialhub.kbsky.model.app.bsky.labeler

import kotlinx.serialization.Serializable

@Serializable
class LabelerViewerState {

var like: String? = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package work.socialhub.kbsky.model.com.atproto.label

import kotlinx.serialization.Serializable

@Serializable
class LabelDefsLabelValueDefinition {

// The value of the label being defined. Must only include lowercase ascii and the '-' character ([a-z-]+).
lateinit var identifier: String

// "inform", "alert", "none"
lateinit var severity: String

// "content", "media", "none"
lateinit var blurs: String

// "ignore", "warn", "hide"
var defaultSetting: String = ""

var adultOnly: Boolean = false

lateinit var locales: List<LabelDefsLabelValueDefinitionStrings>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package work.socialhub.kbsky.model.com.atproto.label

import kotlinx.serialization.Serializable

@Serializable
class LabelDefsLabelValueDefinitionStrings {

lateinit var lang: String

lateinit var name: String

lateinit var description: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package work.socialhub.kbsky.util.json

import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonContentPolymorphicSerializer
import kotlinx.serialization.json.JsonElement
import work.socialhub.kbsky.model.app.bsky.labeler.LabelerView
import work.socialhub.kbsky.model.app.bsky.labeler.LabelerViewDetailed
import work.socialhub.kbsky.model.app.bsky.labeler.LabelerViewUnion
import work.socialhub.kbsky.util.json.JsonElementUtil.type

object LabelerViewPolymorphicSerializer :
JsonContentPolymorphicSerializer<LabelerViewUnion>(
LabelerViewUnion::class
) {

override fun selectDeserializer(
element: JsonElement
): DeserializationStrategy<LabelerViewUnion> {
return when (val type = element.type()) {
LabelerView.TYPE -> LabelerView.serializer()
LabelerViewDetailed.TYPE -> LabelerViewDetailed.serializer()
else -> {
println("[Warning] Unknown Item type: $type (LabelerViewUnion)")
Unknown.serializer()
}
}
}

@Serializable
class Unknown : LabelerViewUnion() {
override var type: String = "unknown"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package work.socialhub.kbsky.app.bsky.labeler

import work.socialhub.kbsky.AbstractTest
import work.socialhub.kbsky.BlueskyFactory
import work.socialhub.kbsky.api.entity.app.bsky.labeler.LabelerGetServicesRequest
import work.socialhub.kbsky.domain.Service.BSKY_SOCIAL
import kotlin.test.Test

class GetServicesTest : AbstractTest() {

@Test
fun testGetServices() {
val response = BlueskyFactory
.instance(BSKY_SOCIAL.uri)
.labeler()
.getServices(
LabelerGetServicesRequest(auth()).also {
it.dids = listOf("did:plc:ar7c4by46qjdydhdevvrndac", "did:plc:fcikraffwejtuqffifeykcml")
it.detailed = true
}
)

response.data.views
.forEach {
println(it.asLabelerViewDetailed?.creator?.displayName)
println(" likes: " + it.asLabelerViewDetailed?.likeCount)
println(" labelValues: " + it.asLabelerViewDetailed?.policies?.labelValues)
}
}
}

0 comments on commit 63339dd

Please sign in to comment.