/api/v1/instance の urls.streaming_api を読む
This commit is contained in:
parent
04ca3d017a
commit
cb30ea6af0
|
@ -1190,20 +1190,20 @@ class TestTootApiClient {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testGetInstanceInformation() = runTest {
|
fun testGetInstanceInformation() = runTest {
|
||||||
val callback = ProgressRecordTootApiCallback()
|
val callback = ProgressRecordTootApiCallback()
|
||||||
val client = TootApiClient(
|
val client = TootApiClient(
|
||||||
appContext,
|
appContext,
|
||||||
httpClient = createHttpClientMock(),
|
httpClient = createHttpClientMock(),
|
||||||
callback = callback
|
callback = callback
|
||||||
)
|
)
|
||||||
val instance = Host.parse("unit-test")
|
val instance = Host.parse("unit-test")
|
||||||
client.apiHost = instance
|
client.apiHost = instance
|
||||||
val (instanceInfo, instanceResult) = TootInstance.get(client)
|
val (instanceInfo, instanceResult) = TootInstance.get(client)
|
||||||
assertNull("no error", instanceResult?.error)
|
assertNull("no error", instanceResult?.error)
|
||||||
assertNotNull("instance info", instanceInfo)
|
assertNotNull("instance info", instanceInfo)
|
||||||
val json = instanceResult?.jsonObject
|
val json = instanceResult?.jsonObject
|
||||||
if (json != null) println(json.toString())
|
if (json != null) println(json.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testGetHttp() = runTest {
|
fun testGetHttp() = runTest {
|
||||||
|
@ -1265,7 +1265,7 @@ class TestTootApiClient {
|
||||||
)
|
)
|
||||||
client.account = accessInfo
|
client.account = accessInfo
|
||||||
val (_, ws) = client.webSocket(
|
val (_, ws) = client.webSocket(
|
||||||
"/api/v1/streaming/?stream=public:local",
|
"wss://${accessInfo.apiHost.ascii}/api/v1/streaming/?stream=public:local",
|
||||||
object : WebSocketListener() {}
|
object : WebSocketListener() {}
|
||||||
)
|
)
|
||||||
assertNotNull(ws)
|
assertNotNull(ws)
|
||||||
|
|
|
@ -489,7 +489,7 @@ class TootApiClient(
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun webSocket(
|
suspend fun webSocket(
|
||||||
path: String,
|
urlArg: String,
|
||||||
wsListener: WebSocketListener,
|
wsListener: WebSocketListener,
|
||||||
): Pair<TootApiResult?, WebSocket?> {
|
): Pair<TootApiResult?, WebSocket?> {
|
||||||
var ws: WebSocket? = null
|
var ws: WebSocket? = null
|
||||||
|
@ -497,10 +497,8 @@ class TootApiClient(
|
||||||
if (result.error != null) return Pair(result, null)
|
if (result.error != null) return Pair(result, null)
|
||||||
val account = this.account ?: return Pair(TootApiResult("account is null"), null)
|
val account = this.account ?: return Pair(TootApiResult("account is null"), null)
|
||||||
try {
|
try {
|
||||||
var url = "wss://${apiHost?.ascii}$path"
|
|
||||||
|
|
||||||
val requestBuilder = Request.Builder()
|
val requestBuilder = Request.Builder()
|
||||||
|
var url = urlArg
|
||||||
if (account.isMisskey) {
|
if (account.isMisskey) {
|
||||||
val accessToken = account.misskeyApiToken
|
val accessToken = account.misskeyApiToken
|
||||||
if (accessToken?.isNotEmpty() == true) {
|
if (accessToken?.isNotEmpty() == true) {
|
||||||
|
@ -515,7 +513,7 @@ class TootApiClient(
|
||||||
}
|
}
|
||||||
|
|
||||||
val request = requestBuilder.url(url).build()
|
val request = requestBuilder.url(url).build()
|
||||||
publishApiProgress(context.getString(R.string.request_api, request.method, path))
|
publishApiProgress(context.getString(R.string.request_api, request.method, urlArg))
|
||||||
ws = httpClient.getWebSocket(request, wsListener)
|
ws = httpClient.getWebSocket(request, wsListener)
|
||||||
if (isApiCancelled()) {
|
if (isApiCancelled()) {
|
||||||
ws.cancel()
|
ws.cancel()
|
||||||
|
|
|
@ -166,7 +166,7 @@ class TootInstance(parser: TootParser, src: JsonObject) {
|
||||||
|
|
||||||
var configuration: JsonObject? = null
|
var configuration: JsonObject? = null
|
||||||
|
|
||||||
// XXX: urls をパースしてない。使ってないから…
|
var urls: JsonObject? = null
|
||||||
|
|
||||||
init {
|
init {
|
||||||
if (parser.serviceType == ServiceType.MISSKEY) {
|
if (parser.serviceType == ServiceType.MISSKEY) {
|
||||||
|
@ -252,6 +252,7 @@ class TootInstance(parser: TootParser, src: JsonObject) {
|
||||||
?.stringList()?.toSet()
|
?.stringList()?.toSet()
|
||||||
|
|
||||||
this.configuration = src.jsonObject("configuration")
|
this.configuration = src.jsonObject("configuration")
|
||||||
|
this.urls = src.jsonObject("urls")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -507,13 +507,24 @@ class StreamConnection(
|
||||||
|
|
||||||
status = StreamStatus.Connecting
|
status = StreamStatus.Connecting
|
||||||
|
|
||||||
val path = group?.spec?.path ?: when {
|
val url = when {
|
||||||
acctGroup.account.isMisskey -> "/streaming"
|
acctGroup.account.isMisskey ->
|
||||||
else -> "/api/v1/streaming/"
|
"wss://${acctGroup.account.apiHost.ascii}${group?.spec?.misskeyPath ?: "/streaming"}"
|
||||||
|
else -> {
|
||||||
|
val ti = TootInstance.getCached(acctGroup.account.apiHost)
|
||||||
|
val prefix = ti?.urls?.string("streaming_api")
|
||||||
|
?: "wss://${acctGroup.account.apiHost}"
|
||||||
|
val path = when (val query = group?.spec?.mastodonQuery) {
|
||||||
|
null, "" -> "/api/v1/streaming/"
|
||||||
|
else -> "/api/v1/streaming/?${query}"
|
||||||
|
}
|
||||||
|
"$prefix$path"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val (result, ws) = try {
|
val (result, ws) = try {
|
||||||
client.webSocket(path, this)
|
log.i("webSocket $url")
|
||||||
|
client.webSocket(url, this)
|
||||||
} catch (ex: Throwable) {
|
} catch (ex: Throwable) {
|
||||||
Pair(TootApiResult(ex.withCaption("can't create WebSocket.")), null)
|
Pair(TootApiResult(ex.withCaption("can't create WebSocket.")), null)
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,8 @@ private fun StringWriter.appendValue(v: Any?) {
|
||||||
|
|
||||||
class StreamSpec(
|
class StreamSpec(
|
||||||
val params: JsonObject,
|
val params: JsonObject,
|
||||||
val path: String,
|
val misskeyPath: String? = null,
|
||||||
|
val mastodonQuery: String? = null,
|
||||||
val name: String,
|
val name: String,
|
||||||
val streamFilter: Column.(JsonArray, TimelineItem) -> Boolean = { _, _ -> true },
|
val streamFilter: Column.(JsonArray, TimelineItem) -> Boolean = { _, _ -> true },
|
||||||
) {
|
) {
|
||||||
|
@ -44,7 +45,8 @@ class StreamSpec(
|
||||||
const val PARAMS = "params"
|
const val PARAMS = "params"
|
||||||
}
|
}
|
||||||
|
|
||||||
val keyString = "$path?${params.toString(indentFactor = 0, sort = true)}"
|
val keyString =
|
||||||
|
"${misskeyPath ?: mastodonQuery}?${params.toString(indentFactor = 0, sort = true)}"
|
||||||
|
|
||||||
val channelId = keyString.digestSHA256Base64Url()
|
val channelId = keyString.digestSHA256Base64Url()
|
||||||
|
|
||||||
|
@ -82,7 +84,7 @@ private fun Column.streamSpecMastodon(): StreamSpec? {
|
||||||
|
|
||||||
return StreamSpec(
|
return StreamSpec(
|
||||||
params = root,
|
params = root,
|
||||||
path = "/api/v1/streaming/?${root.encodeQuery()}",
|
mastodonQuery = root.encodeQuery(),
|
||||||
name = encodeStreamNameMastodon(root),
|
name = encodeStreamNameMastodon(root),
|
||||||
streamFilter = type.streamFilterMastodon
|
streamFilter = type.streamFilterMastodon
|
||||||
)
|
)
|
||||||
|
@ -133,7 +135,7 @@ fun Column.streamSpecMisskey(): StreamSpec? {
|
||||||
|
|
||||||
return StreamSpec(
|
return StreamSpec(
|
||||||
params = root,
|
params = root,
|
||||||
path = path,
|
misskeyPath = path,
|
||||||
name = encodeStreamNameMisskey(root),
|
name = encodeStreamNameMisskey(root),
|
||||||
// no stream filter
|
// no stream filter
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue