Skip to content

Commit

Permalink
[Mastodon] PerProviderId情報とSearch APIを使って、できる限りローカルIDの取得を試みてからFav/BT/…
Browse files Browse the repository at this point in the history
…Deleteを実行する

refs #253
  • Loading branch information
shibafu528 committed Jul 11, 2019
1 parent e755162 commit 796e3e8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
37 changes: 33 additions & 4 deletions Yukari/src/main/java/shibafu/yukari/mastodon/MastodonApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ class MastodonApi : ProviderApi {
val client = getApiClient(userRecord) as? MastodonClient ?: throw IllegalStateException("Mastodonとの通信の準備に失敗しました")
try {
val statuses = Statuses(client)
statuses.postFavourite(status.id).execute()
val localId = resolveLocalId(userRecord, status as DonStatus) ?: throw ProviderApiException("IDが分かりません : ${status.url}")
statuses.postFavourite(localId).execute()
Handler(Looper.getMainLooper()).post {
Toast.makeText(service.applicationContext, "ふぁぼりました (@" + userRecord.ScreenName + ")", Toast.LENGTH_SHORT).show()
}
Expand All @@ -88,7 +89,8 @@ class MastodonApi : ProviderApi {
val client = getApiClient(userRecord) as? MastodonClient ?: throw IllegalStateException("Mastodonとの通信の準備に失敗しました")
try {
val statuses = Statuses(client)
statuses.postUnfavourite(status.id).execute()
val localId = resolveLocalId(userRecord, status as DonStatus) ?: throw ProviderApiException("IDが分かりません : ${status.url}")
statuses.postUnfavourite(localId).execute()
Handler(Looper.getMainLooper()).post {
Toast.makeText(service.applicationContext, "あんふぁぼしました (@" + userRecord.ScreenName + ")", Toast.LENGTH_SHORT).show()
}
Expand Down Expand Up @@ -156,7 +158,8 @@ class MastodonApi : ProviderApi {
val client = getApiClient(userRecord) as? MastodonClient ?: throw IllegalStateException("Mastodonとの通信の準備に失敗しました")
try {
val statuses = Statuses(client)
statuses.postReblog(status.id).execute()
val localId = resolveLocalId(userRecord, status as DonStatus) ?: throw ProviderApiException("IDが分かりません : ${status.url}")
statuses.postReblog(localId).execute()
Handler(Looper.getMainLooper()).post {
Toast.makeText(service.applicationContext, "ブーストしました (@" + userRecord.ScreenName + ")", Toast.LENGTH_SHORT).show()
}
Expand All @@ -174,7 +177,8 @@ class MastodonApi : ProviderApi {
val client = getApiClient(userRecord) as? MastodonClient ?: throw IllegalStateException("Mastodonとの通信の準備に失敗しました")
try {
val statuses = Statuses(client)
statuses.deleteStatus(status.id)
val localId = resolveLocalId(userRecord, status as DonStatus) ?: throw ProviderApiException("IDが分かりません : ${status.url}")
statuses.deleteStatus(localId)
Handler(Looper.getMainLooper()).post {
Toast.makeText(service.applicationContext, "トゥートを削除しました", Toast.LENGTH_SHORT).show()
}
Expand Down Expand Up @@ -211,4 +215,29 @@ class MastodonApi : ProviderApi {
)
).execute()
}

/**
* 可能な範囲で [status] のインスタンスローカルなIDを取得します。
* @param userRecord 操作アカウント
* @param status IDを取得したい [Status]
* @return インスタンスローカルID、取得に失敗した場合は -1
* @throws Mastodon4jRequestException サーバへのリクエストを試行し、失敗した場合にスロー
*/
@Throws(Mastodon4jRequestException::class)
private fun resolveLocalId(userRecord: AuthUserRecord, status: DonStatus): Long? {
if (status.providerHost == userRecord.Provider.host) {
return status.id
}

val perProviderId = status.perProviderId.getIfAbsent(userRecord.Provider.host, -1L)
if (perProviderId != -1L) {
return perProviderId
}

val client = getApiClient(userRecord) as? MastodonClient ?: return -1
val public = Public(client)
val searchResult = public.getSearch(status.url, true).execute()
val localStatus = searchResult.statuses.firstOrNull() ?: return -1
return localStatus.id
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class DonStatus(val status: Status,

val isLocal: Boolean = user.host == representUser.Provider.host

var perProviderId = ObjectLongHashMap.newWithKeysValues(representUser.Provider.host, status.id)
var perProviderId: ObjectLongHashMap<String> = ObjectLongHashMap.newWithKeysValues(representUser.Provider.host, status.id)
private set

override fun equals(other: Any?): Boolean {
Expand Down

0 comments on commit 796e3e8

Please sign in to comment.