2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter.action
|
|
|
|
|
|
|
|
import jp.juggler.subwaytooter.ActMain
|
|
|
|
import jp.juggler.subwaytooter.App1
|
|
|
|
import jp.juggler.subwaytooter.R
|
2018-01-21 17:47:13 +01:00
|
|
|
import jp.juggler.subwaytooter.api.*
|
2019-08-13 18:29:48 +02:00
|
|
|
import jp.juggler.subwaytooter.api.entity.*
|
2018-01-04 19:52:25 +01:00
|
|
|
import jp.juggler.subwaytooter.dialog.DlgConfirm
|
|
|
|
import jp.juggler.subwaytooter.table.SavedAccount
|
2018-12-02 11:25:00 +01:00
|
|
|
import jp.juggler.util.*
|
2018-01-04 19:52:25 +01:00
|
|
|
import okhttp3.Request
|
2018-12-01 00:02:18 +01:00
|
|
|
import org.json.JSONArray
|
|
|
|
import org.json.JSONObject
|
|
|
|
import java.util.regex.Pattern
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
object Action_ListMember {
|
|
|
|
|
|
|
|
private val reFollowError = Pattern.compile("follow", Pattern.CASE_INSENSITIVE)
|
|
|
|
|
|
|
|
interface Callback {
|
|
|
|
fun onListMemberUpdated(willRegistered : Boolean, bSuccess : Boolean)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun add(
|
2018-01-21 13:46:36 +01:00
|
|
|
activity : ActMain,
|
|
|
|
access_info : SavedAccount,
|
2018-08-18 12:58:14 +02:00
|
|
|
list_id : EntityId,
|
2018-01-21 13:46:36 +01:00
|
|
|
local_who : TootAccount,
|
2018-08-31 14:47:40 +02:00
|
|
|
bFollow : Boolean = false,
|
2018-01-21 13:46:36 +01:00
|
|
|
callback : Callback?
|
2018-01-04 19:52:25 +01:00
|
|
|
) {
|
2018-08-31 14:47:40 +02:00
|
|
|
if(access_info.isMe(local_who)) {
|
|
|
|
showToast(activity, false, R.string.it_is_you)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-10 16:47:35 +01:00
|
|
|
TootTaskRunner(activity).run(access_info, object : TootTask {
|
2018-01-04 19:52:25 +01:00
|
|
|
override fun background(client : TootApiClient) : TootApiResult? {
|
|
|
|
|
2018-11-01 13:48:54 +01:00
|
|
|
val parser = TootParser(activity, access_info)
|
|
|
|
|
2019-08-13 18:29:48 +02:00
|
|
|
var userId = local_who.id
|
|
|
|
|
2018-08-31 14:47:40 +02:00
|
|
|
return if(access_info.isMisskey) {
|
|
|
|
// misskeyのリストはフォロー無関係
|
|
|
|
|
|
|
|
val params = access_info.putMisskeyApiToken(JSONObject())
|
|
|
|
.put("listId", list_id)
|
|
|
|
.put("userId", local_who.id)
|
|
|
|
|
|
|
|
client.request("/api/users/lists/push", params.toPostRequestBuilder())
|
|
|
|
// 204 no content
|
|
|
|
} else {
|
|
|
|
if(bFollow) {
|
2019-08-13 18:29:48 +02:00
|
|
|
|
|
|
|
// リモートユーザの解決
|
|
|
|
if(! access_info.isLocalUser(local_who)) {
|
|
|
|
val (r2, ar) = client.syncAccountByAcct(access_info, local_who.acct)
|
|
|
|
val user = ar?.get() ?: return r2
|
|
|
|
userId = user.id
|
2018-08-31 14:47:40 +02:00
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2019-08-13 18:29:48 +02:00
|
|
|
val result = client.request(
|
|
|
|
"/api/v1/accounts/$userId/follow",
|
2019-08-24 05:35:22 +02:00
|
|
|
"".toFormRequestBody().toPost()
|
2019-08-13 18:29:48 +02:00
|
|
|
) ?: return null
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2019-08-13 18:29:48 +02:00
|
|
|
val relation = saveUserRelation(
|
|
|
|
access_info,
|
|
|
|
parseItem(::TootRelationShip, parser, result.jsonObject)
|
|
|
|
)
|
|
|
|
?: return TootApiResult("parse error.")
|
2018-08-31 14:47:40 +02:00
|
|
|
|
|
|
|
if(! relation.following) {
|
|
|
|
if(relation.requested) {
|
|
|
|
return TootApiResult(activity.getString(R.string.cant_add_list_follow_requesting))
|
|
|
|
} else {
|
|
|
|
// リモートフォローの場合、正常ケースでもここを通る場合がある
|
|
|
|
// 何もしてはいけない…
|
|
|
|
}
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
2018-08-31 14:47:40 +02:00
|
|
|
// リストメンバー追加
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2018-12-02 11:25:00 +01:00
|
|
|
client.request(
|
|
|
|
"/api/v1/lists/$list_id/accounts",
|
2019-08-13 18:29:48 +02:00
|
|
|
JSONObject().apply {
|
|
|
|
put(
|
|
|
|
"account_ids",
|
|
|
|
JSONArray().apply {
|
|
|
|
put(userId.toString())
|
|
|
|
}
|
2018-12-02 11:25:00 +01:00
|
|
|
)
|
2019-08-13 18:29:48 +02:00
|
|
|
}
|
2018-12-02 11:25:00 +01:00
|
|
|
.toPostRequestBuilder()
|
2018-01-21 13:46:36 +01:00
|
|
|
)
|
2018-08-31 14:47:40 +02:00
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun handleResult(result : TootApiResult?) {
|
|
|
|
var bSuccess = false
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if(result == null) return // cancelled.
|
|
|
|
|
|
|
|
if(result.jsonObject != null) {
|
|
|
|
for(column in App1.getAppState(activity).column_list) {
|
|
|
|
// リストメンバー追加イベントをカラムに伝達
|
|
|
|
column.onListMemberUpdated(access_info, list_id, local_who, true)
|
|
|
|
}
|
|
|
|
// フォロー状態の更新を表示に反映させる
|
|
|
|
if(bFollow) activity.showColumnMatchAccount(access_info)
|
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
showToast(activity, false, R.string.list_member_added)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
bSuccess = true
|
|
|
|
|
|
|
|
} else {
|
2018-01-10 16:47:35 +01:00
|
|
|
val response = result.response
|
|
|
|
val error = result.error
|
|
|
|
if(response != null
|
2019-08-24 05:35:22 +02:00
|
|
|
&& response.code == 422
|
2018-08-31 14:47:40 +02:00
|
|
|
&& error != null
|
|
|
|
&& reFollowError.matcher(error).find()
|
|
|
|
) {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
if(! bFollow) {
|
|
|
|
DlgConfirm.openSimple(
|
2018-01-21 13:46:36 +01:00
|
|
|
activity,
|
|
|
|
activity.getString(
|
|
|
|
R.string.list_retry_with_follow,
|
|
|
|
access_info.getFullAcct(local_who)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
Action_ListMember.add(
|
|
|
|
activity,
|
|
|
|
access_info,
|
|
|
|
list_id,
|
|
|
|
local_who,
|
2018-08-31 14:47:40 +02:00
|
|
|
bFollow = true,
|
|
|
|
callback = callback
|
2018-01-21 13:46:36 +01:00
|
|
|
)
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
} else {
|
|
|
|
android.app.AlertDialog.Builder(activity)
|
|
|
|
.setCancelable(true)
|
|
|
|
.setMessage(R.string.cant_add_list_follow_requesting)
|
|
|
|
.setNeutralButton(R.string.close, null)
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
showToast(activity, true, error)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
callback?.onListMemberUpdated(true, bSuccess)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fun delete(
|
2018-01-21 13:46:36 +01:00
|
|
|
activity : ActMain,
|
|
|
|
access_info : SavedAccount,
|
2018-08-18 12:58:14 +02:00
|
|
|
list_id : EntityId,
|
2018-01-21 13:46:36 +01:00
|
|
|
local_who : TootAccount,
|
|
|
|
callback : Callback?
|
2018-01-04 19:52:25 +01:00
|
|
|
) {
|
2018-01-10 16:47:35 +01:00
|
|
|
TootTaskRunner(activity).run(access_info, object : TootTask {
|
2018-01-04 19:52:25 +01:00
|
|
|
override fun background(client : TootApiClient) : TootApiResult? {
|
2018-12-19 08:22:21 +01:00
|
|
|
return if(access_info.isMisskey) {
|
|
|
|
client.request(
|
|
|
|
"/api/users/lists/pull",
|
|
|
|
access_info.putMisskeyApiToken()
|
2019-08-13 18:29:48 +02:00
|
|
|
.put("listId", list_id.toString())
|
|
|
|
.put("userId", local_who.id.toString())
|
2018-12-19 08:22:21 +01:00
|
|
|
.toPostRequestBuilder()
|
|
|
|
)
|
2019-08-13 18:29:48 +02:00
|
|
|
} else {
|
2018-12-19 08:22:21 +01:00
|
|
|
client.request(
|
2018-12-19 10:11:14 +01:00
|
|
|
"/api/v1/lists/${list_id}/accounts?account_ids[]=${local_who.id}",
|
2018-12-19 08:22:21 +01:00
|
|
|
Request.Builder().delete()
|
|
|
|
)
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun handleResult(result : TootApiResult?) {
|
|
|
|
var bSuccess = false
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if(result == null) return // cancelled.
|
|
|
|
|
|
|
|
if(result.jsonObject != null) {
|
|
|
|
|
|
|
|
for(column in App1.getAppState(activity).column_list) {
|
|
|
|
column.onListMemberUpdated(access_info, list_id, local_who, false)
|
|
|
|
}
|
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
showToast(activity, false, R.string.delete_succeeded)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
bSuccess = true
|
|
|
|
|
|
|
|
} else {
|
2018-01-21 13:46:36 +01:00
|
|
|
showToast(activity, false, result.error)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
callback?.onListMemberUpdated(false, bSuccess)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|