Skip to content

Commit

Permalink
Merge pull request #602 from DimensionDev/feature/mastodon_forks
Browse files Browse the repository at this point in the history
add support for mastodon forks
  • Loading branch information
Tlaster authored Dec 16, 2024
2 parents 58684f6 + 4c5a00c commit abf1b12
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,19 @@ internal data object NodeInfoService {
"http://nodeinfo.diaspora.software/ns/schema/2.1",
)

suspend fun fetchNodeInfo(host: String): String {
private val mastodonNodeInfoName =
listOf(
"mastodon",
"kmyblue",
"fedibird",
)

private val misskeyNodeInfoName =
listOf(
"misskey",
)

suspend fun fetchNodeInfo(host: String): String? {
val response =
ktorClient()
.get(
Expand All @@ -48,27 +60,33 @@ internal data object NodeInfoService {
ktorClient()
.get(
it.href,
).body<Schema10.Coordinate>()
.software.name.value
).body<Schema10>()
.software
?.name
?.value

"http://nodeinfo.diaspora.software/ns/schema/1.1" ->
ktorClient()
.get(
it.href,
).body<Schema11.Coordinate>()
.software.name.value
).body<Schema11>()
.software
?.name
?.value

"http://nodeinfo.diaspora.software/ns/schema/2.0" ->
ktorClient()
.get(it.href)
.body<Schema20.Coordinate>()
.software.name
.body<Schema20>()
.software
?.name

"http://nodeinfo.diaspora.software/ns/schema/2.1" ->
ktorClient()
.get(it.href)
.body<Schema21.Coordinate>()
.software.name
.body<Schema21>()
.software
?.name

else -> throw IllegalArgumentException("Unsupported schema: ${it.rel}")
}
Expand All @@ -90,10 +108,12 @@ internal data object NodeInfoService {
runCatching {
val nodeInfo = fetchNodeInfo(host)
when {
nodeInfo.contains("Mastodon", ignoreCase = true) -> PlatformType.Mastodon
nodeInfo.contains("Misskey", ignoreCase = true) -> PlatformType.Misskey
mastodonNodeInfoName.any { it.equals(nodeInfo, ignoreCase = true) } -> PlatformType.Mastodon
misskeyNodeInfoName.any { it.equals(nodeInfo, ignoreCase = true) } -> PlatformType.Misskey
else -> throw IllegalArgumentException("Unsupported platform: $nodeInfo")
}
}.onFailure {
it.printStackTrace()
}.getOrNull()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,41 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject

internal class Schema10 {
/**
* NodeInfo schema version 1.0.
*/
@Serializable
internal data class Schema10(
/**
* NodeInfo schema version 1.0.
* Free form key value pairs for software specific values. Clients should not rely on any
* specific key present.
*/
@Serializable
data class Coordinate(
/**
* Free form key value pairs for software specific values. Clients should not rely on any
* specific key present.
*/
val metadata: JsonObject,
/**
* Whether this server allows open self-registration.
*/
val openRegistrations: Boolean,
/**
* The protocols supported on this server.
*/
val protocols: Protocols,
/**
* The third party sites this server can connect to via their application API.
*/
val services: Services,
/**
* Metadata about server software in use.
*/
val software: Software,
/**
* Usage statistics for this server.
*/
val usage: Usage,
/**
* The schema version, must be 1.0.
*/
val version: Version,
)

val metadata: JsonObject? = null,
/**
* Whether this server allows open self-registration.
*/
val openRegistrations: Boolean? = null,
/**
* The protocols supported on this server.
*/
val protocols: Protocols? = null,
/**
* The third party sites this server can connect to via their application API.
*/
val services: Services? = null,
/**
* Metadata about server software in use.
*/
val software: Software? = null,
/**
* Usage statistics for this server.
*/
val usage: Usage? = null,
/**
* The schema version, must be 1.0.
*/
val version: Version? = null,
) {
/**
* The protocols supported on this server.
*/
Expand All @@ -49,11 +47,11 @@ internal class Schema10 {
/**
* The protocols this server can receive traffic for.
*/
val inbound: List<Bound>,
val inbound: List<Bound>? = null,
/**
* The protocols this server can generate traffic for.
*/
val outbound: List<Bound>,
val outbound: List<Bound>? = null,
)

@Serializable
Expand Down Expand Up @@ -100,11 +98,11 @@ internal class Schema10 {
* The third party sites this server can retrieve messages from for combined display with
* regular traffic.
*/
val inbound: List<Inbound>,
val inbound: List<Inbound>? = null,
/**
* The third party sites this server can publish messages to on the behalf of a user.
*/
val outbound: List<Outbound>,
val outbound: List<Outbound>? = null,
)

@Serializable
Expand Down Expand Up @@ -212,11 +210,11 @@ internal class Schema10 {
/**
* The canonical name of this server software.
*/
val name: Name,
val name: Name? = null,
/**
* The version of this server software.
*/
val version: String,
val version: String? = null,
)

/**
Expand Down Expand Up @@ -252,7 +250,7 @@ internal class Schema10 {
/**
* statistics about the users of this server.
*/
val users: Users,
val users: Users? = null,
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,41 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject

internal class Schema11 {
/**
* NodeInfo schema version 1.1.
*/
@Serializable
internal data class Schema11(
/**
* NodeInfo schema version 1.1.
* Free form key value pairs for software specific values. Clients should not rely on any
* specific key present.
*/
@Serializable
data class Coordinate(
/**
* Free form key value pairs for software specific values. Clients should not rely on any
* specific key present.
*/
val metadata: JsonObject,
/**
* Whether this server allows open self-registration.
*/
val openRegistrations: Boolean,
/**
* The protocols supported on this server.
*/
val protocols: Protocols,
/**
* The third party sites this server can connect to via their application API.
*/
val services: Services,
/**
* Metadata about server software in use.
*/
val software: Software,
/**
* Usage statistics for this server.
*/
val usage: Usage,
/**
* The schema version, must be 1.1.
*/
val version: Version,
)

val metadata: JsonObject? = null,
/**
* Whether this server allows open self-registration.
*/
val openRegistrations: Boolean? = null,
/**
* The protocols supported on this server.
*/
val protocols: Protocols? = null,
/**
* The third party sites this server can connect to via their application API.
*/
val services: Services? = null,
/**
* Metadata about server software in use.
*/
val software: Software? = null,
/**
* Usage statistics for this server.
*/
val usage: Usage? = null,
/**
* The schema version, must be 1.1.
*/
val version: Version? = null,
) {
/**
* The protocols supported on this server.
*/
Expand All @@ -49,11 +47,11 @@ internal class Schema11 {
/**
* The protocols this server can receive traffic for.
*/
val inbound: List<Bound>,
val inbound: List<Bound>? = null,
/**
* The protocols this server can generate traffic for.
*/
val outbound: List<Bound>,
val outbound: List<Bound>? = null,
)

@Serializable
Expand Down Expand Up @@ -103,11 +101,11 @@ internal class Schema11 {
* The third party sites this server can retrieve messages from for combined display with
* regular traffic.
*/
val inbound: List<Inbound>,
val inbound: List<Inbound>? = null,
/**
* The third party sites this server can publish messages to on the behalf of a user.
*/
val outbound: List<Outbound>,
val outbound: List<Outbound>? = null,
)

@Serializable
Expand Down Expand Up @@ -215,11 +213,11 @@ internal class Schema11 {
/**
* The canonical name of this server software.
*/
val name: Name,
val name: Name? = null,
/**
* The version of this server software.
*/
val version: String,
val version: String? = null,
)

/**
Expand Down Expand Up @@ -258,7 +256,7 @@ internal class Schema11 {
/**
* statistics about the users of this server.
*/
val users: Users,
val users: Users? = null,
)

/**
Expand Down
Loading

0 comments on commit abf1b12

Please sign in to comment.