1
0
mirror of https://github.com/tateisu/SubwayTooter synced 2025-01-30 18:44:52 +01:00

プッシュ購読APIがサーバキーを返さない場合、購読の更新ボタンを押した際の表示にエラーを含める

This commit is contained in:
tateisu 2018-05-24 16:56:30 +09:00
parent d7fe519f8d
commit 44175241d9

View File

@ -45,52 +45,55 @@ class PushSubscriptionHelper(
} }
} }
// returns error string or null
private fun updateServerKey( private fun updateServerKey(
client : TootApiClient, client : TootApiClient,
clientIdentifier : String, clientIdentifier : String,
serverKey : String? serverKey : String?
) { ) : TootApiResult? {
if( serverKey == null ){
addLog("(missing server public key in subscription.)") if(serverKey == null) {
return return TootApiResult(error="!! Server public key is missing. There is server misconfiguration , push notification will not work.")
}else if( serverKey.isEmpty() ){ } else if(serverKey.isEmpty()) {
addLog("(empty server public key in subscription.)") return TootApiResult(error="!! Server public key is empty. There is server misconfiguration, push notification will not work.")
return
} }
// 既に登録済みの値と同じなら何もしない // 既に登録済みの値と同じなら何もしない
val oldKey = SubscriptionServerKey.find(clientIdentifier) val oldKey = SubscriptionServerKey.find(clientIdentifier)
if(oldKey == serverKey) return if(oldKey != serverKey) {
// サーバキーをアプリサーバに登録 // サーバキーをアプリサーバに登録
val r = client.http( val r = client.http(
Request.Builder() Request.Builder()
.url("${PollingWorker.APP_SERVER}/webpushserverkey") .url("${PollingWorker.APP_SERVER}/webpushserverkey")
.post( .post(
RequestBody.create( RequestBody.create(
TootApiClient.MEDIA_TYPE_JSON, TootApiClient.MEDIA_TYPE_JSON,
JSONObject().apply { JSONObject().apply {
put("client_id", clientIdentifier) put("client_id", clientIdentifier)
put("server_key", serverKey) put("server_key", serverKey)
}.toString() }.toString()
)
) )
) .build()
.build() )
) val res = r?.response
if(res != null) {
when(res.code()) {
200 -> {
// 登録できたサーバーキーをアプリ内DBに保存
SubscriptionServerKey.save(clientIdentifier, serverKey)
addLog("(server public key is registered.)")
}
val res = r?.response ?: return else -> {
when(res.code()) { addLog("(server public key registration failed.)")
200 -> { addLog("${res.code()} ${res.message()}")
// 登録できたサーバーキーをアプリ内DBに保存 }
SubscriptionServerKey.save(clientIdentifier, serverKey) }
addLog("(server public key is registered.)")
}
else -> {
addLog("(server public key registration failed.)")
addLog("${res.code()} ${res.message()}")
} }
} }
return TootApiResult()
} }
private fun updateSubscription_sub(client : TootApiClient) : TootApiResult? { private fun updateSubscription_sub(client : TootApiClient) : TootApiResult? {
@ -118,11 +121,11 @@ class PushSubscriptionHelper(
403 -> { 403 -> {
// アクセストークンにpushスコープがない // アクセストークンにpushスコープがない
return if(verbose){ return if(verbose) {
addLog(context.getString(R.string.missing_push_scope)) addLog(context.getString(R.string.missing_push_scope))
r r
}else{ } else {
if( flags!=0 ) addLog(context.getString(R.string.missing_push_scope)) if(flags != 0) addLog(context.getString(R.string.missing_push_scope))
TootApiResult() TootApiResult()
} }
} }
@ -184,13 +187,12 @@ class PushSubscriptionHelper(
val endpoint = val endpoint =
"${PollingWorker.APP_SERVER}/webpushcallback/${device_id.encodePercent()}/${account.acct.encodePercent()}/$flags/$clientIdentifier" "${PollingWorker.APP_SERVER}/webpushcallback/${device_id.encodePercent()}/${account.acct.encodePercent()}/$flags/$clientIdentifier"
if( oldSubscription != null) { if(oldSubscription != null) {
if(oldSubscription.endpoint == endpoint) { if(oldSubscription.endpoint == endpoint) {
// 既に登録済みで、endpointも一致している // 既に登録済みで、endpointも一致している
subscribed = true subscribed = true
if(verbose) addLog(context.getString(R.string.push_subscription_already_exists)) if(verbose) addLog(context.getString(R.string.push_subscription_already_exists))
updateServerKey(client, clientIdentifier, oldSubscription.server_key) return updateServerKey(client, clientIdentifier, oldSubscription.server_key)
return TootApiResult()
} }
} }
@ -317,13 +319,17 @@ class PushSubscriptionHelper(
200 -> { 200 -> {
subscribed = true subscribed = true
if( verbose) { if(verbose) {
addLog(context.getString(R.string.push_subscription_updated)) addLog(context.getString(R.string.push_subscription_updated))
} }
val newSubscription = parseItem(::TootPushSubscription, r?.jsonObject) val newSubscription = parseItem(::TootPushSubscription, r?.jsonObject)
if(newSubscription != null) { if(newSubscription != null) {
updateServerKey(client, clientIdentifier, newSubscription.server_key) return updateServerKey(
client,
clientIdentifier,
newSubscription.server_key
)
} }
TootApiResult() TootApiResult()