From 290205dba9aa9b0f1316994c87486c27753d0e79 Mon Sep 17 00:00:00 2001 From: tateisu Date: Sat, 16 May 2020 21:01:24 +0900 Subject: [PATCH] =?UTF-8?q?=E4=BA=88=E7=B4=84=E6=8A=95=E7=A8=BF=E3=82=AB?= =?UTF-8?q?=E3=83=A9=E3=83=A0=E3=81=A7=E3=82=AB=E3=83=A9=E3=83=A0=E3=83=AD?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=81=98=E3=81=AB=E6=8A=95=E7=A8=BF=E8=80=85?= =?UTF-8?q?=E3=81=AE=E6=83=85=E5=A0=B1=E3=82=82=E8=AA=AD=E3=82=80=E3=80=82?= =?UTF-8?q?=E3=81=82=E3=81=A8=E5=BF=98=E3=82=8C=E3=81=9F=E3=81=91=E3=81=A9?= =?UTF-8?q?=E3=81=AA=E3=81=AB=E3=81=8B=E3=83=AA=E3=83=95=E3=82=A1=E3=82=AF?= =?UTF-8?q?=E3=82=BF=E3=82=89=E3=81=97=E3=81=8D=E3=82=82=E3=81=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/jp/juggler/subwaytooter/ActPost.kt | 95 +++++++++---------- .../jp/juggler/subwaytooter/ColumnType.kt | 16 +++- .../jp/juggler/subwaytooter/ItemViewHolder.kt | 4 +- .../subwaytooter/api/TootTaskRunner.kt | 4 +- 4 files changed, 66 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/jp/juggler/subwaytooter/ActPost.kt b/app/src/main/java/jp/juggler/subwaytooter/ActPost.kt index efab1d01..d299cf14 100644 --- a/app/src/main/java/jp/juggler/subwaytooter/ActPost.kt +++ b/app/src/main/java/jp/juggler/subwaytooter/ActPost.kt @@ -817,7 +817,7 @@ class ActPost : AsyncActivity(), // 比較する前にデフォルトの公開範囲を計算する visibility = visibility ?: account.visibility - // ?: TootVisibility.Public + // ?: TootVisibility.Public // VISIBILITY_WEB_SETTING だと 1.5未満のタンスでトラブルになる if(TootVisibility.WebSetting == visibility) { @@ -2276,55 +2276,54 @@ class ActPost : AsyncActivity(), fun postV1() = postMedia("/api/v1/media") - var result : TootApiResult? - - if(! ti.versionGE(TootInstance.VERSION_3_1_3)) { + fun postV2() : TootApiResult? { // 3.1.3未満は v1 APIを使う - result = postV1() - } else { + if(! ti.versionGE(TootInstance.VERSION_3_1_3)) + return postV1() + // v2 APIを試す - result = postMedia("/api/v2/media") - when(result?.response?.code) { - + val result = postMedia("/api/v2/media") + val code = result?.response?.code // complete,or 4xx error + when { // 404ならv1 APIにフォールバック - 404 -> result = postV1() + code == 404 -> return postV1() + // 202 accepted 以外はポーリングしない + code != 202 -> return result + } + + // ポーリングして処理完了を待つ + val id = parseItem( + ::TootAttachment, + ServiceType.MASTODON, + result?.jsonObject + ) + ?.id + ?: return TootApiResult("/api/v2/media did not return the media ID.") + + var lastResponse = SystemClock.elapsedRealtime() + loop@ while(true) { - // 202 accepted ならポーリングして処理完了を待つ - 202 -> { - val id = parseItem( - ::TootAttachment, - ServiceType.MASTODON, - result.jsonObject - ) - ?.id - ?: error("/api/v2/media did not returns media ID.") - - var lastResponse = SystemClock.elapsedRealtime() - while(true) { - result = client.request("/api/v1/media/$id") - ?: break // cancelled. - - val code = result.response?.code - - // complete,or 4xx error - if(code == 200 || code in 400 until 500) break - - val now = SystemClock.elapsedRealtime() - if( code == 206) { - // continue to wait - lastResponse = now - }else if( now - lastResponse >= 120000L ) { - // too many temporary error without 206 response. - result = TootApiResult("timeout.") - break - } // else temporary error - - sleep(1000L) - } + sleep(1000L) + val r2 = client.request("/api/v1/media/$id") + ?: return null // cancelled + + val now = SystemClock.elapsedRealtime() + when(r2.response?.code) { + // complete,or 4xx error + 200, in 400 until 500 -> return r2 + + // continue to wait + 206 -> lastResponse = now + + // too many temporary error without 206 response. + else -> + if(now - lastResponse >= 120000L) + return TootApiResult("timeout.") } } } + val result = postV2() opener.deleteTempFile() onUploadEnd() @@ -2780,11 +2779,11 @@ class ActPost : AsyncActivity(), val list_warning = ArrayList() var target_account : SavedAccount? = null - + runWithProgress( "restore from draft", - {progress-> - fun isTaskCancelled() = !this.coroutineContext.isActive + { progress -> + fun isTaskCancelled() = ! this.coroutineContext.isActive var content = draft.string(DRAFT_CONTENT) ?: "" val account_db_id = draft.long(DRAFT_ACCOUNT_DB_ID) ?: - 1L @@ -2816,7 +2815,7 @@ class ActPost : AsyncActivity(), return@runWithProgress "OK" } - + target_account = account // アカウントがあるなら基本的にはすべての情報を復元できるはずだが、いくつか確認が必要だ @@ -2874,9 +2873,9 @@ class ActPost : AsyncActivity(), "OK" }, - {result-> + { result -> // cancelled. - if( result == null) return@runWithProgress + if(result == null) return@runWithProgress val content = draft.string(DRAFT_CONTENT) ?: "" val content_warning = draft.string(DRAFT_CONTENT_WARNING) ?: "" diff --git a/app/src/main/java/jp/juggler/subwaytooter/ColumnType.kt b/app/src/main/java/jp/juggler/subwaytooter/ColumnType.kt index f398ac67..db6958b1 100644 --- a/app/src/main/java/jp/juggler/subwaytooter/ColumnType.kt +++ b/app/src/main/java/jp/juggler/subwaytooter/ColumnType.kt @@ -1473,7 +1473,21 @@ enum class ColumnType( bAllowPseudo = false, bAllowMisskey = false, - loading = { client -> getScheduledStatuses(client) }, + loading = { client -> + val result = client.request("/api/v1/accounts/verify_credentials") + if(result==null || result.error != null){ + result + }else{ + val a = parser.account(result.jsonObject) ?: access_info.loginAccount + if(a==null){ + TootApiResult("can't parse account information") + }else { + column.who_account = TootAccountRef(parser, a) + getScheduledStatuses(client) + } + } + }, + refresh = { client -> getScheduledStatuses(client) } ), diff --git a/app/src/main/java/jp/juggler/subwaytooter/ItemViewHolder.kt b/app/src/main/java/jp/juggler/subwaytooter/ItemViewHolder.kt index eb5e1d05..854395d7 100644 --- a/app/src/main/java/jp/juggler/subwaytooter/ItemViewHolder.kt +++ b/app/src/main/java/jp/juggler/subwaytooter/ItemViewHolder.kt @@ -572,8 +572,8 @@ internal class ItemViewHolder( showStatusTimeScheduled(activity, tvTime, item) - val who = access_info.loginAccount !! - val whoRef = TootAccountRef(TootParser(activity, access_info), who) + val who = column.who_account!!.get() + val whoRef = TootAccountRef(TootParser(activity, access_info), who ) this.status_account = whoRef setAcct(tvAcct, access_info, who) diff --git a/app/src/main/java/jp/juggler/subwaytooter/api/TootTaskRunner.kt b/app/src/main/java/jp/juggler/subwaytooter/api/TootTaskRunner.kt index b6711f59..4faf3f32 100644 --- a/app/src/main/java/jp/juggler/subwaytooter/api/TootTaskRunner.kt +++ b/app/src/main/java/jp/juggler/subwaytooter/api/TootTaskRunner.kt @@ -22,7 +22,6 @@ import java.util.concurrent.atomic.AtomicBoolean - TootApiClientからの進捗イベントをProgressDialogに伝達します。 */ -@Suppress("DEPRECATION", "MemberVisibilityCanPrivate") class TootTaskRunner( context : Context, private val progress_style : Int = PROGRESS_SPINNER, @@ -30,7 +29,6 @@ class TootTaskRunner( ) : TootApiCallback { companion object { - const val PROGRESS_NONE = - 1 const val PROGRESS_SPINNER = ProgressDialogEx.STYLE_SPINNER const val PROGRESS_HORIZONTAL = ProgressDialogEx.STYLE_HORIZONTAL @@ -153,6 +151,7 @@ class TootTaskRunner( this.progress = progress progress.setCancelable(true) progress.setOnCancelListener { task?.cancel() } + @Suppress("DEPRECATION") progress.setProgressStyle(progress_style) progressSetupCallback(progress) showProgressMessage() @@ -169,6 +168,7 @@ class TootTaskRunner( // ダイアログのメッセージを更新する // 初期化時とメッセージ更新時に呼ばれる + @Suppress("DEPRECATION") private fun showProgressMessage() { val progress = this.progress ?: return