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