実際にチェックしない場合はPush購読チェックのprogressを出さない

This commit is contained in:
tateisu 2022-06-15 11:57:56 +09:00
parent 5e4ed90980
commit a2d1e6ec79
6 changed files with 75 additions and 63 deletions

View File

@ -369,7 +369,10 @@ class TootInstance(parser: TootParser, src: JsonObject) {
// misskeyのインスタンス情報を読めたら、それはmisskeyのインスタンス
val r2 = getInstanceInformationMisskey(forceAccessToken) ?: return null
if (r2.jsonObject != null) return r2
if (r2.response?.code == 403 && r2.error?.contains(""""code":"AUTHENTICATION_FAILED"""") == true) return r2
when (r2.response?.code) {
null, 404 -> Unit // fall
else -> return r2
}
// マストドンのインスタンス情報を読めたら、それはマストドンのインスタンス
val r1 = getInstanceInformationMastodon(forceAccessToken) ?: return null

View File

@ -257,9 +257,9 @@ class PollingChecker(
}
accountMutex(accountDbId).withLock {
progress(account, PollingState.CheckPushSubscription)
val wps = PushSubscriptionHelper(context, account)
if (wps.flags != 0) {
progress(account, PollingState.CheckServerInformation)
val (instance, instanceResult) = TootInstance.get(client)
if (instance == null) {
log.e("missing instance. ${instanceResult?.error} ${instanceResult?.requestInfo}".trim())
@ -268,7 +268,7 @@ class PollingChecker(
}
}
wps.updateSubscription(client)
wps.updateSubscription(client, progress = progress)
?: return@withLock // cancelled.
val wpsLog = wps.logString

View File

@ -3,6 +3,7 @@ package jp.juggler.subwaytooter.notification
enum class PollingState(val desc: String) {
PrepareInstallId("preparing install id"),
CheckNetworkConnection("check network connection"),
CheckServerInformation("check server information"),
CheckPushSubscription("check push subscription"),
CheckNotifications("check notifications"),
Complete("complete"),

View File

@ -234,6 +234,14 @@ fun checkNotificationImmediate(
}
}
// K,Vのマップを V,List<K>のマップにする
private fun <K, V> Map<K, V>.trans() = HashMap<V, ArrayList<K>>()
.also { dst ->
entries.forEach { (k, v) ->
dst.getOrPut(v) { ArrayList() }.add(k)
}
}
/**
* 全アカウントの通知チェックを行う
* -
@ -242,7 +250,7 @@ suspend fun checkNoticifationAll(
context: Context,
logPrefix: String,
onlySubscription: Boolean = false,
progress: suspend (Map<PollingState, MutableList<String>>) -> Unit = {},
progress: suspend (Map<PollingState, List<String>>) -> Unit = {},
) {
CheckerWakeLocks.checkerWakeLocks(context).checkConnection()
@ -274,17 +282,14 @@ suspend fun checkNoticifationAll(
try {
val tmpMap = synchronized(statusMap) {
statusMap[a.acct.pretty] = s
buildMap<PollingState, MutableList<String>> {
statusMap.entries.forEach { (k, v) ->
getOrPut(v) { mutableListOf() }.add(k)
}
}
statusMap.trans()
}
progress(tmpMap)
} catch (ex: Throwable) {
log.trace(ex)
}
}
SavedAccount.loadAccountList(context).mapNotNull { sa ->
when {
sa.isPseudo || !sa.isConfirmed -> null
@ -311,11 +316,7 @@ suspend fun checkNoticifationAll(
}.toTypedArray().let { joinAll(*it) }
try {
val tmpMap = buildMap<PollingState, MutableList<String>> {
statusMap.entries.forEach { (k, v) ->
getOrPut(v) { mutableListOf() }.add(k)
}
}
val tmpMap = statusMap.trans()
progress(tmpMap)
} catch (ex: Throwable) {
log.trace(ex)

View File

@ -96,11 +96,26 @@ class PollingWorker2(
setForeground(ForegroundInfo(NOTIFICATION_ID_POLLING_WORKER, it))
}
private fun stateMapToString(map: Map<PollingState, List<String>>) =
StringBuilder().apply {
for (state in PollingState.valuesCache) {
val list = map[state] ?: continue
if (isNotEmpty()) append(" |")
append(state.desc)
append(": ")
if (list.size <= 2) {
append(list.sorted().joinToString(", "))
} else {
append("${list.size}")
}
}
}.toString()
private suspend fun workImpl() {
val context = applicationContext
coroutineScope {
if (importProtector.get()) {
log.w("doWork: abort by importProtector.")
log.w("abort by importProtector.")
return@coroutineScope
}
@ -108,19 +123,7 @@ class PollingWorker2(
showMessage(context.getString(R.string.loading_notification_title))
checkNoticifationAll(context, "") { map ->
val sb = StringBuilder()
for (state in PollingState.valuesCache) {
val list = map[state] ?: continue
if (sb.isNotEmpty()) sb.append("\n")
sb.append(state.desc)
sb.append(": ")
if (list.size <= 2) {
sb.append(list.joinToString(", "))
} else {
sb.append("${list.size}")
}
}
showMessage(sb.toString())
showMessage(stateMapToString(map))
}
}
}

View File

@ -158,46 +158,50 @@ class PushSubscriptionHelper(
}
}
suspend fun updateSubscription(client: TootApiClient, force: Boolean = false): TootApiResult? =
try {
when {
isRecentlyChecked() ->
TootApiResult(ERROR_PREVENT_FREQUENTLY_CHECK)
suspend fun updateSubscription(
client: TootApiClient,
force: Boolean = false,
progress: suspend (SavedAccount, PollingState) -> Unit = { _, _ -> },
): TootApiResult? = try {
when {
isRecentlyChecked() ->
TootApiResult(ERROR_PREVENT_FREQUENTLY_CHECK)
account.isPseudo ->
TootApiResult(context.getString(R.string.pseudo_account_not_supported))
account.isPseudo ->
TootApiResult(context.getString(R.string.pseudo_account_not_supported))
account.isMisskey ->
updateSubscriptionMisskey(client)
else ->
updateSubscriptionMastodon(client, force)
}
} catch (ex: Throwable) {
TootApiResult(ex.withCaption("error."))
}?.apply {
if (error != null) addLog("$error $requestInfo".trimEnd())
// update error text on account table
val log = logString
when {
log.contains(ERROR_PREVENT_FREQUENTLY_CHECK) -> {
// don't update if check was skipped.
else -> {
progress(account, PollingState.CheckPushSubscription)
when {
account.isMisskey -> updateSubscriptionMisskey(client)
else -> updateSubscriptionMastodon(client, force)
}
subscribed || log.isEmpty() ->
// clear error text if succeeded or no error log
if (account.last_subscription_error != null) {
account.updateSubscriptionError(null)
}
else ->
// record error text
account.updateSubscriptionError(log)
}
}
} catch (ex: Throwable) {
TootApiResult(ex.withCaption("error."))
}?.apply {
if (error != null) addLog("$error $requestInfo".trimEnd())
// update error text on account table
val log = logString
when {
log.contains(ERROR_PREVENT_FREQUENTLY_CHECK) -> {
// don't update if check was skipped.
}
subscribed || log.isEmpty() ->
// clear error text if succeeded or no error log
if (account.last_subscription_error != null) {
account.updateSubscriptionError(null)
}
else ->
// record error text
account.updateSubscriptionError(log)
}
}
private suspend fun updateSubscriptionMisskey(client: TootApiClient): TootApiResult? {