実際にチェックしない場合はPush購読チェックのprogressを出さない
This commit is contained in:
parent
5e4ed90980
commit
a2d1e6ec79
|
@ -369,7 +369,10 @@ class TootInstance(parser: TootParser, src: JsonObject) {
|
||||||
// misskeyのインスタンス情報を読めたら、それはmisskeyのインスタンス
|
// misskeyのインスタンス情報を読めたら、それはmisskeyのインスタンス
|
||||||
val r2 = getInstanceInformationMisskey(forceAccessToken) ?: return null
|
val r2 = getInstanceInformationMisskey(forceAccessToken) ?: return null
|
||||||
if (r2.jsonObject != null) return r2
|
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
|
val r1 = getInstanceInformationMastodon(forceAccessToken) ?: return null
|
||||||
|
|
|
@ -257,9 +257,9 @@ class PollingChecker(
|
||||||
}
|
}
|
||||||
|
|
||||||
accountMutex(accountDbId).withLock {
|
accountMutex(accountDbId).withLock {
|
||||||
progress(account, PollingState.CheckPushSubscription)
|
|
||||||
val wps = PushSubscriptionHelper(context, account)
|
val wps = PushSubscriptionHelper(context, account)
|
||||||
if (wps.flags != 0) {
|
if (wps.flags != 0) {
|
||||||
|
progress(account, PollingState.CheckServerInformation)
|
||||||
val (instance, instanceResult) = TootInstance.get(client)
|
val (instance, instanceResult) = TootInstance.get(client)
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
log.e("missing instance. ${instanceResult?.error} ${instanceResult?.requestInfo}".trim())
|
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.
|
?: return@withLock // cancelled.
|
||||||
|
|
||||||
val wpsLog = wps.logString
|
val wpsLog = wps.logString
|
||||||
|
|
|
@ -3,6 +3,7 @@ package jp.juggler.subwaytooter.notification
|
||||||
enum class PollingState(val desc: String) {
|
enum class PollingState(val desc: String) {
|
||||||
PrepareInstallId("preparing install id"),
|
PrepareInstallId("preparing install id"),
|
||||||
CheckNetworkConnection("check network connection"),
|
CheckNetworkConnection("check network connection"),
|
||||||
|
CheckServerInformation("check server information"),
|
||||||
CheckPushSubscription("check push subscription"),
|
CheckPushSubscription("check push subscription"),
|
||||||
CheckNotifications("check notifications"),
|
CheckNotifications("check notifications"),
|
||||||
Complete("complete"),
|
Complete("complete"),
|
||||||
|
|
|
@ -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,
|
context: Context,
|
||||||
logPrefix: String,
|
logPrefix: String,
|
||||||
onlySubscription: Boolean = false,
|
onlySubscription: Boolean = false,
|
||||||
progress: suspend (Map<PollingState, MutableList<String>>) -> Unit = {},
|
progress: suspend (Map<PollingState, List<String>>) -> Unit = {},
|
||||||
) {
|
) {
|
||||||
CheckerWakeLocks.checkerWakeLocks(context).checkConnection()
|
CheckerWakeLocks.checkerWakeLocks(context).checkConnection()
|
||||||
|
|
||||||
|
@ -274,17 +282,14 @@ suspend fun checkNoticifationAll(
|
||||||
try {
|
try {
|
||||||
val tmpMap = synchronized(statusMap) {
|
val tmpMap = synchronized(statusMap) {
|
||||||
statusMap[a.acct.pretty] = s
|
statusMap[a.acct.pretty] = s
|
||||||
buildMap<PollingState, MutableList<String>> {
|
statusMap.trans()
|
||||||
statusMap.entries.forEach { (k, v) ->
|
|
||||||
getOrPut(v) { mutableListOf() }.add(k)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
progress(tmpMap)
|
progress(tmpMap)
|
||||||
} catch (ex: Throwable) {
|
} catch (ex: Throwable) {
|
||||||
log.trace(ex)
|
log.trace(ex)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SavedAccount.loadAccountList(context).mapNotNull { sa ->
|
SavedAccount.loadAccountList(context).mapNotNull { sa ->
|
||||||
when {
|
when {
|
||||||
sa.isPseudo || !sa.isConfirmed -> null
|
sa.isPseudo || !sa.isConfirmed -> null
|
||||||
|
@ -311,11 +316,7 @@ suspend fun checkNoticifationAll(
|
||||||
}.toTypedArray().let { joinAll(*it) }
|
}.toTypedArray().let { joinAll(*it) }
|
||||||
|
|
||||||
try {
|
try {
|
||||||
val tmpMap = buildMap<PollingState, MutableList<String>> {
|
val tmpMap = statusMap.trans()
|
||||||
statusMap.entries.forEach { (k, v) ->
|
|
||||||
getOrPut(v) { mutableListOf() }.add(k)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
progress(tmpMap)
|
progress(tmpMap)
|
||||||
} catch (ex: Throwable) {
|
} catch (ex: Throwable) {
|
||||||
log.trace(ex)
|
log.trace(ex)
|
||||||
|
|
|
@ -96,11 +96,26 @@ class PollingWorker2(
|
||||||
setForeground(ForegroundInfo(NOTIFICATION_ID_POLLING_WORKER, it))
|
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() {
|
private suspend fun workImpl() {
|
||||||
val context = applicationContext
|
val context = applicationContext
|
||||||
coroutineScope {
|
coroutineScope {
|
||||||
if (importProtector.get()) {
|
if (importProtector.get()) {
|
||||||
log.w("doWork: abort by importProtector.")
|
log.w("abort by importProtector.")
|
||||||
return@coroutineScope
|
return@coroutineScope
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,19 +123,7 @@ class PollingWorker2(
|
||||||
showMessage(context.getString(R.string.loading_notification_title))
|
showMessage(context.getString(R.string.loading_notification_title))
|
||||||
|
|
||||||
checkNoticifationAll(context, "") { map ->
|
checkNoticifationAll(context, "") { map ->
|
||||||
val sb = StringBuilder()
|
showMessage(stateMapToString(map))
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -158,46 +158,50 @@ class PushSubscriptionHelper(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun updateSubscription(client: TootApiClient, force: Boolean = false): TootApiResult? =
|
suspend fun updateSubscription(
|
||||||
try {
|
client: TootApiClient,
|
||||||
when {
|
force: Boolean = false,
|
||||||
isRecentlyChecked() ->
|
progress: suspend (SavedAccount, PollingState) -> Unit = { _, _ -> },
|
||||||
TootApiResult(ERROR_PREVENT_FREQUENTLY_CHECK)
|
): TootApiResult? = try {
|
||||||
|
when {
|
||||||
|
isRecentlyChecked() ->
|
||||||
|
TootApiResult(ERROR_PREVENT_FREQUENTLY_CHECK)
|
||||||
|
|
||||||
account.isPseudo ->
|
account.isPseudo ->
|
||||||
TootApiResult(context.getString(R.string.pseudo_account_not_supported))
|
TootApiResult(context.getString(R.string.pseudo_account_not_supported))
|
||||||
|
|
||||||
account.isMisskey ->
|
else -> {
|
||||||
updateSubscriptionMisskey(client)
|
progress(account, PollingState.CheckPushSubscription)
|
||||||
|
when {
|
||||||
else ->
|
account.isMisskey -> updateSubscriptionMisskey(client)
|
||||||
updateSubscriptionMastodon(client, force)
|
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.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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? {
|
private suspend fun updateSubscriptionMisskey(client: TootApiClient): TootApiResult? {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue