プッシュ購読チェックのリファクタ

This commit is contained in:
tateisu 2019-12-18 04:29:30 +09:00
parent cd5adec5e2
commit 5fbaec7b45

View File

@ -39,7 +39,6 @@ class PushSubscriptionHelper(
} }
} }
private fun preventRapid() : Boolean { private fun preventRapid() : Boolean {
if(verbose) return true if(verbose) return true
val now = System.currentTimeMillis() val now = System.currentTimeMillis()
@ -127,17 +126,7 @@ class PushSubscriptionHelper(
return TootApiResult() return TootApiResult()
} }
private fun updateSubscription_sub(client : TootApiClient) : TootApiResult? { private fun updateSubscriptionMisskey(client : TootApiClient) : TootApiResult? {
try {
if(! preventRapid()) return TootApiResult(ERROR_PREVENT_FREQUENTLY_CHECK)
// 疑似アカウントの確認
if(account.isPseudo) {
return TootApiResult(error = context.getString(R.string.pseudo_account_not_supported))
}
if(account.isMisskey) {
// 現在の購読状態を取得できない // 現在の購読状態を取得できない
// 購読を解除できない // 購読を解除できない
if(flags == 0) return TootApiResult(error = ERROR_MISSKEY_LACK_UNSUBSCRIBE_API) if(flags == 0) return TootApiResult(error = ERROR_MISSKEY_LACK_UNSUBSCRIBE_API)
@ -167,16 +156,20 @@ class PushSubscriptionHelper(
// "${PollingWorker.APP_SERVER}/webpushcallback/${device_id.encodePercent()}/${account.acct.encodePercent()}/$flags/$clientIdentifier" // "${PollingWorker.APP_SERVER}/webpushcallback/${device_id.encodePercent()}/${account.acct.encodePercent()}/$flags/$clientIdentifier"
// 購読 // 購読
val params = account.putMisskeyApiToken(JSONObject()) return client.request(
"/api/sw/register",
account.putMisskeyApiToken()
.put("endpoint", endpoint) .put("endpoint", endpoint)
.put("auth", "iRdmDrOS6eK6xvG1H6KshQ") .put("auth", "iRdmDrOS6eK6xvG1H6KshQ")
.put( .put(
"publickey", "publickey",
"BBEUVi7Ehdzzpe_ZvlzzkQnhujNJuBKH1R0xYg7XdAKNFKQG9Gpm0TSGRGSuaU7LUFKX-uz8YW0hAshifDCkPuE" "BBEUVi7Ehdzzpe_ZvlzzkQnhujNJuBKH1R0xYg7XdAKNFKQG9Gpm0TSGRGSuaU7LUFKX-uz8YW0hAshifDCkPuE"
) )
.toPostRequestBuilder()
val result = client.request("/api/sw/register", params.toPostRequestBuilder()) )?.also { result ->
if(result?.jsonObject != null) { if(result.jsonObject == null) {
addLog("API error.")
} else {
subscribed = true subscribed = true
// Misskeyのプッシュ購読APIはサーバーキーを返さないので // Misskeyのプッシュ購読APIはサーバーキーを返さないので
@ -186,12 +179,11 @@ class PushSubscriptionHelper(
if(verbose) { if(verbose) {
addLog(context.getString(R.string.push_subscription_updated)) addLog(context.getString(R.string.push_subscription_updated))
} }
} else if(result != null) {
addLog("subscription API returns error : ${result.error}")
} }
return result }
} else { }
private fun updateSubscriptionMastodon(client : TootApiClient) : TootApiResult? {
// 現在の購読状態を取得 // 現在の購読状態を取得
// https://github.com/tootsuite/mastodon/pull/7471 // https://github.com/tootsuite/mastodon/pull/7471
// https://github.com/tootsuite/mastodon/pull/7472 // https://github.com/tootsuite/mastodon/pull/7472
@ -202,13 +194,12 @@ class PushSubscriptionHelper(
200 -> { 200 -> {
if(r.error?.isNotEmpty() == true && r.jsonObject == null) { if(r.error?.isNotEmpty() == true && r.jsonObject == null) {
// Pleromaが200応用でエラーHTMLを返す // Pleromaが200応用でエラーHTMLを返す
return TootApiResult( return r.setError(
error = context.getString( "${context.getString(
R.string.instance_does_not_support_push_api_pleroma R.string.instance_does_not_support_push_api_pleroma
) )} : ${r.error}"
) )
} }
// たぶん購読が存在する // たぶん購読が存在する
} }
@ -223,16 +214,13 @@ class PushSubscriptionHelper(
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() r
} }
} }
in 400 until 500 -> { in 400 until 500 -> {
return TootApiResult( addLog(context.getString(R.string.instance_does_not_support_push_api_pleroma))
error = context.getString( return r
R.string.instance_does_not_support_push_api_pleroma
)
)
} }
else -> { else -> {
@ -314,10 +302,14 @@ class PushSubscriptionHelper(
.url("${PollingWorker.APP_SERVER}/webpushtokencheck") .url("${PollingWorker.APP_SERVER}/webpushtokencheck")
.build() .build()
) )
r ?: return r
res = r?.response ?: return r res = r.response ?: return r
if(res.code != 200) { if(res.code != 200) {
return TootApiResult(error = context.getString(R.string.token_exported)) if(res.code == 403) addLog(context.getString(R.string.token_exported))
r.caption = "(SubwayTooter App server)"
client.readBodyString(r)
return r
} }
if(flags == 0) { if(flags == 0) {
@ -349,13 +341,9 @@ class PushSubscriptionHelper(
} }
403 -> { 403 -> {
if(! verbose) {
TootApiResult()
} else {
addLog(context.getString(R.string.missing_push_scope)) addLog(context.getString(R.string.missing_push_scope))
r r
} }
}
else -> { else -> {
addLog("${res.request}") addLog("${res.request}")
@ -399,22 +387,14 @@ class PushSubscriptionHelper(
return when(res.code) { return when(res.code) {
404 -> { 404 -> {
if(! verbose) {
TootApiResult()
} else {
addLog(context.getString(R.string.missing_push_api)) addLog(context.getString(R.string.missing_push_api))
r r
} }
}
403 -> { 403 -> {
if(! verbose) {
TootApiResult()
} else {
addLog(context.getString(R.string.missing_push_scope)) addLog(context.getString(R.string.missing_push_scope))
r r
} }
}
200 -> { 200 -> {
subscribed = true subscribed = true
@ -441,26 +421,42 @@ class PushSubscriptionHelper(
} }
} }
} }
}
private fun updateSubscription_sub(client : TootApiClient) : TootApiResult? =
try {
when {
! preventRapid() ->
TootApiResult(ERROR_PREVENT_FREQUENTLY_CHECK)
account.isPseudo ->
TootApiResult(context.getString(R.string.pseudo_account_not_supported))
account.isMisskey ->
updateSubscriptionMisskey(client)
else ->
updateSubscriptionMastodon(client)
} }
} catch(ex : Throwable) { } catch(ex : Throwable) {
return TootApiResult(error = ex.withCaption("error.")) TootApiResult(ex.withCaption("error."))
}
} }
fun updateSubscription(client : TootApiClient) : TootApiResult? = fun updateSubscription(client : TootApiClient) : TootApiResult? =
updateSubscription_sub(client)?.apply { updateSubscription_sub(client)?.apply {
error?.let { addLog(it) } if(error != null) addLog("$error $requestInfo".trimEnd())
val wps_log = log val log = log
when { when {
wps_log.contains(ERROR_PREVENT_FREQUENTLY_CHECK) -> { log.contains(ERROR_PREVENT_FREQUENTLY_CHECK) -> {
// don't update error info. // don't update error info.
} }
subscribed || wps_log.isEmpty() -> subscribed || log.isEmpty() ->
account.updateSubscriptionError(null) account.updateSubscriptionError(null)
else -> account.updateSubscriptionError(wps_log) else -> account.updateSubscriptionError(log)
} }
} }
} }