mirror of
https://github.com/tateisu/SubwayTooter
synced 2025-02-05 21:23:26 +01:00
リファクタ
This commit is contained in:
parent
e091be3e7d
commit
1594391db3
@ -265,8 +265,6 @@ class TootApiClient(
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// ユーティリティ
|
||||
|
||||
|
||||
|
||||
// リクエストをokHttpに渡してレスポンスを取得する
|
||||
internal inline fun sendRequest(
|
||||
result : TootApiResult,
|
||||
@ -1336,22 +1334,6 @@ class TootApiClient(
|
||||
|
||||
}
|
||||
|
||||
private fun parseMisskeyApShow(parser : TootParser, jsonObject : JSONObject?) : Any? {
|
||||
return when(jsonObject?.parseString("type")) {
|
||||
"Note" -> {
|
||||
parser.status(jsonObject.optJSONObject("object"))
|
||||
}
|
||||
|
||||
"User" -> {
|
||||
parser.status(jsonObject.optJSONObject("object"))
|
||||
}
|
||||
|
||||
else -> {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun TootApiClient.syncAccountByUrl(accessInfo : SavedAccount, who_url : String) : TootApiResult? {
|
||||
|
||||
// misskey由来のアカウントURLは https://host/@user@instance などがある
|
||||
@ -1464,67 +1446,57 @@ fun TootApiClient.syncStatus(accessInfo : SavedAccount, urlArg : String) : TootA
|
||||
val host = m.group(1)
|
||||
val noteId = m.group(2)
|
||||
|
||||
val result = TootApiClient(context, callback = callback)
|
||||
TootApiClient(context, callback = callback)
|
||||
|
||||
.apply { instance = host }
|
||||
|
||||
.request(
|
||||
"/api/notes/show",
|
||||
JSONObject()
|
||||
.put("noteId", noteId)
|
||||
.toPostRequestBuilder()
|
||||
) ?: return null
|
||||
|
||||
val obj = TootParser(
|
||||
context,
|
||||
LinkHelper.newLinkHelper(host, isMisskey = true),
|
||||
serviceType = ServiceType.MISSKEY
|
||||
).status(result.jsonObject)
|
||||
|
||||
if(obj != null) {
|
||||
if(host.equals(accessInfo.host, ignoreCase = true)) {
|
||||
result.data = obj
|
||||
return result
|
||||
)
|
||||
|
||||
?.also { result ->
|
||||
TootParser(
|
||||
context,
|
||||
LinkHelper.newLinkHelper(host, isMisskey = true),
|
||||
serviceType = ServiceType.MISSKEY
|
||||
)
|
||||
.status(result.jsonObject)
|
||||
?.apply {
|
||||
if(host.equals(accessInfo.host, ignoreCase = true)) {
|
||||
result.data = this
|
||||
return result
|
||||
}
|
||||
uri.letNotEmpty { url = it }
|
||||
}
|
||||
}
|
||||
val uri = obj.uri
|
||||
if(uri.isNotEmpty() ) {
|
||||
url = uri
|
||||
}
|
||||
}
|
||||
|
||||
?: return null // cancelled.
|
||||
}
|
||||
|
||||
return if(accessInfo.isMisskey) {
|
||||
val params = accessInfo.putMisskeyApiToken().put("uri", url)
|
||||
val result = request("/api/ap/show", params.toPostRequestBuilder())
|
||||
if(result != null) {
|
||||
val obj = parseMisskeyApShow(TootParser(context, accessInfo), result.jsonObject)
|
||||
if(obj != null) result.data = obj
|
||||
}
|
||||
result
|
||||
} else {
|
||||
val path = String.format(
|
||||
Locale.JAPAN,
|
||||
Column.PATH_SEARCH,
|
||||
url.encodePercent()
|
||||
) + "&resolve=1"
|
||||
// 使いたいタンス上の投稿IDを取得する
|
||||
val parser = TootParser(context, accessInfo)
|
||||
return when {
|
||||
|
||||
val result = request(path)
|
||||
if(result != null) {
|
||||
val tmp = TootParser(context, accessInfo).results(result.jsonObject)
|
||||
if(tmp != null && tmp.statuses.isNotEmpty()) {
|
||||
result.data = tmp.statuses[0]
|
||||
}
|
||||
accessInfo.isMisskey -> request(
|
||||
"/api/ap/show",
|
||||
accessInfo.putMisskeyApiToken()
|
||||
.put("uri", url)
|
||||
.toPostRequestBuilder()
|
||||
)?.apply {
|
||||
data = parser.parseMisskeyApShow(jsonObject)
|
||||
}
|
||||
|
||||
else -> request(
|
||||
"/api/v1/search?q=${url.encodePercent()}&resolve=true"
|
||||
)?.apply {
|
||||
data = parser.results(jsonObject)?.statuses?.firstOrNull()
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private inline fun <Z : Any?> String?.useNotEmpty(block : (String) -> Z?) : Z? =
|
||||
if(this?.isNotEmpty() == true) {
|
||||
block(this)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
fun TootApiClient.syncStatus(
|
||||
accessInfo : SavedAccount,
|
||||
statusRemote : TootStatus
|
||||
@ -1534,7 +1506,7 @@ fun TootApiClient.syncStatus(
|
||||
|
||||
val uriList = ArrayList<String>(2)
|
||||
|
||||
statusRemote.url.useNotEmpty {
|
||||
statusRemote.url.letNotEmpty {
|
||||
if(it.contains("/notes/")) {
|
||||
// Misskeyタンスから読んだマストドンの投稿はurlがmisskeyタンス上のものになる
|
||||
// ActivityPub object id としては不適切なので使わない
|
||||
@ -1543,12 +1515,12 @@ fun TootApiClient.syncStatus(
|
||||
}
|
||||
}
|
||||
|
||||
statusRemote.uri.useNotEmpty {
|
||||
statusRemote.uri.letNotEmpty {
|
||||
// uri の方は↑の問題はない
|
||||
uriList.add(it)
|
||||
}
|
||||
|
||||
if(accessInfo.isMisskey && uriList.size > 1 && uriList[0].contains("@")) {
|
||||
if(accessInfo.isMisskey && uriList.firstOrNull()?.contains("@") == true) {
|
||||
// https://github.com/syuilo/misskey/pull/2832
|
||||
// @user を含むuri はMisskeyだと少し効率が悪いそうなので順序を入れ替える
|
||||
uriList.reverse()
|
||||
|
@ -9,6 +9,7 @@ import org.json.JSONObject
|
||||
|
||||
import jp.juggler.subwaytooter.util.WordTrieTree
|
||||
import jp.juggler.subwaytooter.util.LinkHelper
|
||||
import jp.juggler.util.parseString
|
||||
|
||||
class TootParser(
|
||||
val context : Context,
|
||||
@ -42,4 +43,14 @@ class TootParser(
|
||||
|
||||
|
||||
fun getMisskeyUserRelation(whoId:EntityId) = misskeyUserRelationMap[whoId]
|
||||
|
||||
fun parseMisskeyApShow( jsonObject : JSONObject? ) : Any? {
|
||||
// ap/show の戻り値はActivityPubオブジェクトではなく、Misskeyのエンティティです。
|
||||
return when(jsonObject?.parseString("type")) {
|
||||
"Note" -> status(jsonObject.optJSONObject("object"))
|
||||
"User" -> account(jsonObject.optJSONObject("object"))
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ class VersionString(src : String?) : Comparable<VersionString> {
|
||||
|
||||
init {
|
||||
this.src = src ?: ""
|
||||
if(src != null && src.isNotEmpty()) {
|
||||
if(src?.isNotEmpty() == true) {
|
||||
val end = src.length
|
||||
var next = 0
|
||||
while(next < end) {
|
||||
|
@ -88,7 +88,7 @@ class MyNetworkImageView : AppCompatImageView {
|
||||
|
||||
val gif_url = if(Pref.bpEnableGifAnimation(pref)) gifUrlArg else null
|
||||
|
||||
if(gif_url != null && gif_url.isNotEmpty()) {
|
||||
if(gif_url?.isNotEmpty() == true) {
|
||||
mUrl = gif_url
|
||||
mMayGif = true
|
||||
} else {
|
||||
|
@ -13,13 +13,12 @@ import java.util.*
|
||||
import java.util.regex.Matcher
|
||||
import java.util.regex.Pattern
|
||||
|
||||
object StringUtils{
|
||||
object StringUtils {
|
||||
val log = LogCategory("StringUtils")
|
||||
|
||||
val hexLower =
|
||||
charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f')
|
||||
|
||||
|
||||
// BDI制御文字からその制御文字を閉じる文字を得るためのマップ
|
||||
val sanitizeBdiMap = HashMap<Char, Char>().apply {
|
||||
|
||||
@ -134,6 +133,12 @@ fun CharSequence.codePointBefore(index : Int) : Int {
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <S : CharSequence, Z : Any?> S?.letNotEmpty(block : (S) -> Z?) : Z? =
|
||||
if(this?.isNotEmpty() == true) {
|
||||
block(this)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// string
|
||||
@ -145,7 +150,6 @@ fun String.encodeUTF8() = this.toByteArray(charsetUTF8)
|
||||
|
||||
fun ByteArray.decodeUTF8() = this.toString(charsetUTF8)
|
||||
|
||||
|
||||
// 16進ダンプ
|
||||
private fun ByteArray.encodeHex() : String {
|
||||
val sb = StringBuilder()
|
||||
@ -239,7 +243,6 @@ fun String.encodePercent(allow : String? = null) : String = Uri.encode(this, all
|
||||
// return sb
|
||||
//}
|
||||
|
||||
|
||||
// 指定した文字数までの部分文字列を返す
|
||||
// 文字列の長さが足りない場合は指定オフセットから終端までの長さを返す
|
||||
fun String.safeSubstring(count : Int, offset : Int = 0) = when {
|
||||
@ -304,7 +307,6 @@ fun String?.mayUri() : Uri? = try {
|
||||
|
||||
fun String.unescapeUri() : String = Uri.decode(this)
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Throwable
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user