diff --git a/app/src/main/java/jp/juggler/subwaytooter/action/Action_Follow.kt b/app/src/main/java/jp/juggler/subwaytooter/action/Action_Follow.kt index 322b7bdd..780ac051 100644 --- a/app/src/main/java/jp/juggler/subwaytooter/action/Action_Follow.kt +++ b/app/src/main/java/jp/juggler/subwaytooter/action/Action_Follow.kt @@ -169,16 +169,39 @@ object Action_Follow { TootTaskRunner(activity, TootTaskRunner.PROGRESS_NONE).run(access_info, object : TootTask { + val parser = TootParser(activity, access_info) + var relation : UserRelation? = null override fun background(client : TootApiClient) : TootApiResult? { - // リモートユーザの同期 var userId = who.id if(who.acct.contains("@")) { - val (result, ar) = client.syncAccountByAcct(access_info, who.acct) - val user = ar?.get() ?: return result - userId = user.id + + // リモートユーザの確認 + + + val skipAccountSync = if(access_info.isMisskey) { + // Misskey の /users/show はリモートユーザに関して404を返すので + // userIdからリモートユーザを照合することはできない。 + // ただし検索APIがエラーになるかどうかは未確認 + false + } else { + // https://github.com/tateisu/SubwayTooter/issues/124 + // によると、閉じたタンスのユーザを同期しようとすると検索APIがエラーを返す + // この問題を回避するため、手持ちのuserIdで照合したユーザのacctが目的のユーザと同じなら + // 検索APIを呼び出さないようにする + val result = client.request("/api/v1/accounts/${userId}") + ?: return null + who.acct == parser.account(result.jsonObject)?.acct + } + + if(!skipAccountSync){ + // 同タンスのIDではなかった場合、検索APIを使う + val (result, ar) = client.syncAccountByAcct(access_info, who.acct) + val user = ar?.get() ?: return result + userId = user.id + } } return if(access_info.isMisskey) { @@ -224,7 +247,6 @@ object Action_Follow { "/api/v1/accounts/${userId}/${if(bFollow) "follow" else "unfollow"}" , "".toFormRequestBody().toPost() )?.also { result -> - val parser = TootParser(activity, access_info) val newRelation = parseItem(::TootRelationShip, parser, result.jsonObject) if(newRelation != null) { relation = saveUserRelation(access_info, newRelation) diff --git a/app/src/main/java/jp/juggler/util/Json.kt b/app/src/main/java/jp/juggler/util/Json.kt index 800c5185..161946d5 100644 --- a/app/src/main/java/jp/juggler/util/Json.kt +++ b/app/src/main/java/jp/juggler/util/Json.kt @@ -121,10 +121,10 @@ class JsonArray : ArrayList { fun optLong(key : Int, defVal : Long = 0L) = long(key) ?: defVal @Suppress("unused") - fun optFloat(key : Int, defVal : Float) = float(key) ?: defVal + fun optFloat(key : Int, defVal : Float = 0f) = float(key) ?: defVal @Suppress("unused") - fun optDouble(key : Int, defVal : Double) = double(key) ?: defVal + fun optDouble(key : Int, defVal : Double = 0.0) = double(key) ?: defVal @Suppress("unused") fun notEmptyOrThrow(key : Int) = notEmptyOrThrow(key.toString(), string(key)) @@ -263,9 +263,9 @@ class JsonObject : LinkedHashMap() { fun optBoolean(name : String, defVal : Boolean = false) = boolean(name) ?: defVal fun optInt(name : String, defVal : Int = 0) = int(name) ?: defVal fun optLong(name : String, defVal : Long = 0L) = long(name) ?: defVal - fun optFloat(name : String, defVal : Float) = float(name) ?: defVal + fun optFloat(name : String, defVal : Float = 0f) = float(name) ?: defVal @Suppress("unused") - fun optDouble(name : String, defVal : Double) = double(name) ?: defVal + fun optDouble(name : String, defVal : Double = 0.0) = double(name) ?: defVal fun notEmptyOrThrow(name : String) = notEmptyOrThrow(name, string(name)) fun isNull(name : String) = this[name] == null @@ -1074,3 +1074,9 @@ inline fun jsonArray(initializer : JsonArray.() -> Unit) = JsonArray().apply { initializer() } fun jsonArray(vararg args : String) = JsonArray(args) + +fun jsonObject(vararg args : Pair) = JsonObject().apply { + for(pair in args){ + put(pair.first,pair.second) + } +}