Skip to content

Commit

Permalink
Merge pull request #86 from takke/support_pinned_post
Browse files Browse the repository at this point in the history
Support pinned post
  • Loading branch information
uakihir0 authored Sep 29, 2024
2 parents 7d5ca1d + e705b1c commit c498a6e
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package work.socialhub.kbsky.api.entity.app.bsky.actor

import work.socialhub.kbsky.api.entity.share.AuthRequest
import work.socialhub.kbsky.model.com.atproto.repo.RepoStrongRef
import work.socialhub.kbsky.model.share.Blob

class ActorUpdateProfileRequest(
Expand All @@ -10,5 +11,8 @@ class ActorUpdateProfileRequest(
val avatar: Blob? = null,
val banner: Blob? = null,
// set true if you want to clear the avatar
val clearBanner: Boolean = false
val clearBanner: Boolean = false,
val pinnedPost: RepoStrongRef? = null,
// set true if you want to clear the pinned post
val clearPinnedPost: Boolean = false,
) : AuthRequest(accessJwt)
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ class FeedGetAuthorFeedRequest(

var filter: Filter? = null

var includePins: Boolean = false

override fun toMap(): Map<String, Any> {
return mutableMapOf<String, Any>().also {
it.addParam("actor", actor)
it.addParam("limit", limit)
it.addParam("cursor", cursor)
it.addParam("filter", filter?.value)
it.addParam("includePins", includePins)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class _ActorResource(

val original = repoResource.getRecord(
RepoGetRecordRequest(
repo = request.did!!,
repo = request.did,
collection = BlueskyTypes.ActorProfile,
rkey = "self"
)
Expand All @@ -93,13 +93,19 @@ class _ActorResource(
} else {
it.banner = request.banner ?: originalActorProfile.banner
}

if (request.clearPinnedPost) {
it.pinnedPost = null
} else {
it.pinnedPost = request.pinnedPost ?: originalActorProfile.pinnedPost
}
}

val r = repoResource.putRecord(
RepoPutRecordRequest(
collection = BlueskyTypes.ActorProfile,
accessJwt = request.accessJwt,
repo = request.did!!,
repo = request.did,
rkey = "self",
record = modifiedActorProfileRecord
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package work.socialhub.kbsky.model.app.bsky.actor

import kotlinx.serialization.Serializable
import work.socialhub.kbsky.model.com.atproto.label.LabelDefsLabel
import work.socialhub.kbsky.model.com.atproto.repo.RepoStrongRef
import work.socialhub.kbsky.util.json.PinnedPostSerializer

@Serializable
open class ActorDefsProfileViewDetailed {
Expand All @@ -21,4 +23,10 @@ open class ActorDefsProfileViewDetailed {
var createdAt: String? = null
var viewer: ActorDefsViewerState? = null
var labels: List<LabelDefsLabel>? = null

// In Japan, many clients are using "string type declaration" for pinnedPost field,
// like "pinnedPost": "at://did:plc:xxx".
// Parse the string type declaration as uri of RepoStrongRef by using [PinnedPostSerializer]
@Serializable(with = PinnedPostSerializer::class)
var pinnedPost: RepoStrongRef? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package work.socialhub.kbsky.model.app.bsky.actor
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import work.socialhub.kbsky.BlueskyTypes
import work.socialhub.kbsky.model.com.atproto.repo.RepoStrongRef
import work.socialhub.kbsky.model.share.Blob
import work.socialhub.kbsky.model.share.RecordUnion

Expand All @@ -20,4 +21,5 @@ class ActorProfile : RecordUnion() {
var description: String? = null
var avatar: Blob? = null
var banner: Blob? = null
var pinnedPost: RepoStrongRef? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import kotlinx.serialization.Serializable
class FeedDefsFeedViewPost {
lateinit var post: FeedDefsPostView
var reply: FeedDefsReplyRef? = null
var reason: FeedDefsReasonRepost? = null
var reason: FeedDefsReasonUnion? = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package work.socialhub.kbsky.model.app.bsky.feed

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import work.socialhub.kbsky.BlueskyTypes

@Serializable
class FeedDefsReasonPin : FeedDefsReasonUnion() {

companion object {
val TYPE = BlueskyTypes.FeedDefs + "#reasonPin"
}

@SerialName("\$type")
override var type = TYPE
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package work.socialhub.kbsky.model.app.bsky.feed

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import work.socialhub.kbsky.BlueskyTypes
import work.socialhub.kbsky.model.app.bsky.actor.ActorDefsProfileViewBasic

@Serializable
class FeedDefsReasonRepost {
class FeedDefsReasonRepost : FeedDefsReasonUnion() {

companion object {
val TYPE = BlueskyTypes.FeedDefs + "#reasonRepost"
}

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

var by: ActorDefsProfileViewBasic? = null
var indexedAt: String? = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package work.socialhub.kbsky.model.app.bsky.feed

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

/**
* @see FeedDefsReasonRepost
* @see FeedDefsReasonPin
*/
@Serializable(with = FeedDefsReasonPolymorphicSerializer::class)
abstract class FeedDefsReasonUnion {
@SerialName("\$type")
abstract var type: String

val asReasonRepost get() = this as? FeedDefsReasonRepost
val asReasonPin get() = this as? FeedDefsReasonPin
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ class FeedDefsViewerState {
var like: String? = null
var replyDisabled: Boolean? = null
var embeddingDisabled: Boolean? = null
var pinned: Boolean? = null
}
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.feed.FeedDefsReasonPin
import work.socialhub.kbsky.model.app.bsky.feed.FeedDefsReasonRepost
import work.socialhub.kbsky.model.app.bsky.feed.FeedDefsReasonUnion
import work.socialhub.kbsky.util.json.JsonElementUtil.type

object FeedDefsReasonPolymorphicSerializer :
JsonContentPolymorphicSerializer<FeedDefsReasonUnion>(
FeedDefsReasonUnion::class
) {

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

@Serializable
class Unknown : FeedDefsReasonUnion() {
override var type: String = "unknown"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package work.socialhub.kbsky.util.json

import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.JsonTransformingSerializer
import work.socialhub.kbsky.model.com.atproto.repo.RepoStrongRef

object PinnedPostSerializer :
JsonTransformingSerializer<RepoStrongRef>(
RepoStrongRef.serializer()
) {

override fun transformDeserialize(
element: JsonElement
): JsonElement {
return if (element is JsonObject) {
element
} else {
// fallback for old pinned post format
JsonObject(mapOf("uri" to element, "cid" to JsonPrimitive("")))
}
}
}

0 comments on commit c498a6e

Please sign in to comment.