Skip to content

Commit

Permalink
Merge pull request #378 from Shikkanime/dev
Browse files Browse the repository at this point in the history
Fix Bsky facets
  • Loading branch information
Ziedelth authored Apr 16, 2024
2 parents c74f217 + bdd5c57 commit bbf4b0c
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/main/kotlin/fr/shikkanime/wrappers/BskyWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ object BskyWrapper {
text: String,
images: List<Image> = emptyList(),
): JsonObject {
val (finalText, facets) = getFacets(text)

val recordMap = mutableMapOf<String, Any>(
"text" to text,
"text" to finalText,
TYPE to "app.bsky.feed.post",
"createdAt" to ZonedDateTime.now().withZoneSameInstant(ZoneId.of("UTC"))
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME).replace("+00:00", "Z"),
Expand All @@ -82,16 +84,6 @@ object BskyWrapper {
)
}

// Get all links in the text, and their start and end indexes
val facets = text.split(" ").mapNotNull { word ->
val link = word.trim()
if (link.startsWith("http")) {
val start = text.indexOf(link)
val end = start + link.length
Facet(link, start, end)
} else null
}

if (facets.isNotEmpty()) {
recordMap["facets"] = facets.map {
mapOf(
Expand Down Expand Up @@ -128,4 +120,23 @@ object BskyWrapper {
require(response.status.value == 200) { "Failed to create record (${response.bodyAsText()})" }
return ObjectParser.fromJson(response.bodyAsText())
}

private fun getFacets(text: String): Pair<String, List<Facet>> {
var tmpText = text

val facets = text.split(" ").mapNotNull { word ->
val link = word.trim()

if (link.startsWith("http")) {
val beautifulLink = link.replace("https?://www\\.|\\?.*".toRegex(), "")
tmpText = tmpText.replace(link, beautifulLink)

val start = tmpText.indexOf(beautifulLink)
val end = start + beautifulLink.length
Facet(link, start, end)
} else null
}

return tmpText to facets
}
}

0 comments on commit bbf4b0c

Please sign in to comment.