2018-03-27 19:47:00 +02:00
|
|
|
/* Copyright 2018 charlag
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
2022-06-20 16:45:54 +02:00
|
|
|
package com.keylesspalace.tusky.usecase
|
2018-03-27 19:47:00 +02:00
|
|
|
|
2020-11-17 20:10:54 +01:00
|
|
|
import android.util.Log
|
2021-06-28 21:13:24 +02:00
|
|
|
import com.keylesspalace.tusky.appstore.BlockEvent
|
|
|
|
import com.keylesspalace.tusky.appstore.BookmarkEvent
|
|
|
|
import com.keylesspalace.tusky.appstore.EventHub
|
|
|
|
import com.keylesspalace.tusky.appstore.FavoriteEvent
|
|
|
|
import com.keylesspalace.tusky.appstore.MuteConversationEvent
|
|
|
|
import com.keylesspalace.tusky.appstore.MuteEvent
|
|
|
|
import com.keylesspalace.tusky.appstore.PinEvent
|
|
|
|
import com.keylesspalace.tusky.appstore.PollVoteEvent
|
|
|
|
import com.keylesspalace.tusky.appstore.ReblogEvent
|
|
|
|
import com.keylesspalace.tusky.appstore.StatusDeletedEvent
|
2019-08-28 19:54:46 +02:00
|
|
|
import com.keylesspalace.tusky.entity.DeletedStatus
|
2019-04-22 10:11:00 +02:00
|
|
|
import com.keylesspalace.tusky.entity.Poll
|
2018-03-27 19:47:00 +02:00
|
|
|
import com.keylesspalace.tusky.entity.Status
|
2022-06-20 16:45:54 +02:00
|
|
|
import com.keylesspalace.tusky.network.MastodonApi
|
2021-05-16 19:53:27 +02:00
|
|
|
import io.reactivex.rxjava3.core.Single
|
|
|
|
import io.reactivex.rxjava3.disposables.CompositeDisposable
|
|
|
|
import io.reactivex.rxjava3.kotlin.addTo
|
2022-01-23 20:24:55 +01:00
|
|
|
import javax.inject.Inject
|
2018-03-27 19:47:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by charlag on 3/24/18.
|
|
|
|
*/
|
|
|
|
|
2022-01-23 20:24:55 +01:00
|
|
|
class TimelineCases @Inject constructor(
|
2021-06-11 20:15:40 +02:00
|
|
|
private val mastodonApi: MastodonApi,
|
|
|
|
private val eventHub: EventHub
|
2022-01-23 20:24:55 +01:00
|
|
|
) {
|
2018-10-03 21:27:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unused yet but can be use for cancellation later. It's always a good idea to save
|
|
|
|
* Disposables.
|
|
|
|
*/
|
|
|
|
private val cancelDisposable = CompositeDisposable()
|
|
|
|
|
2022-01-23 20:24:55 +01:00
|
|
|
fun reblog(statusId: String, reblog: Boolean): Single<Status> {
|
2018-03-27 19:47:00 +02:00
|
|
|
val call = if (reblog) {
|
2021-06-11 20:15:40 +02:00
|
|
|
mastodonApi.reblogStatus(statusId)
|
2018-03-27 19:47:00 +02:00
|
|
|
} else {
|
2021-06-11 20:15:40 +02:00
|
|
|
mastodonApi.unreblogStatus(statusId)
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
Caching toots (#809)
* Initial timeline cache implementation
* Fix build/DI errors for caching
* Rename timeline entities tables. Add migration. Add DB scheme file.
* Fix uniqueness problem, change offline strategy, improve mapping
* Try to merge in new statuses, fix bottom loading, fix saving spans.
* Fix reblogs IDs, fix inserting elements from top
* Send one more request to get latest timeline statuses
* Give Timeline placeholders string id. Rewrite Either in Kotlin
* Initial placeholder implementation for caching
* Fix crash on removing overlap statuses
* Migrate counters to long
* Remove unused counters. Add minimal TimelineDAOTest
* Fix bug with placeholder ID
* Update cache in response to events. Refactor TimelineCases
* Fix crash, reduce number of placeholders
* Fix crash, fix filtering, improve placeholder handling
* Fix migration, add 8-9 migration test
* Fix initial timeline update, remove more placeholders
* Add cleanup for old statuses
* Fix cleanup
* Delete ExampleInstrumentedTest
* Improve timeline UX regarding caching
* Fix typos
* Fix initial timeline update
* Cleanup/fix initial timeline update
* Workaround for weird behavior of first post on initial tl update.
* Change counter types back to int
* Clear timeline cache on logout
* Fix loading when timeline is completely empty
* Fix androidx migration issues
* Fix tests
* Apply caching feedback
* Save account emojis to cache
* Fix warnings and bugs
2019-01-14 22:05:08 +01:00
|
|
|
return call.doAfterSuccess {
|
2021-06-11 20:15:40 +02:00
|
|
|
eventHub.dispatch(ReblogEvent(statusId, reblog))
|
Caching toots (#809)
* Initial timeline cache implementation
* Fix build/DI errors for caching
* Rename timeline entities tables. Add migration. Add DB scheme file.
* Fix uniqueness problem, change offline strategy, improve mapping
* Try to merge in new statuses, fix bottom loading, fix saving spans.
* Fix reblogs IDs, fix inserting elements from top
* Send one more request to get latest timeline statuses
* Give Timeline placeholders string id. Rewrite Either in Kotlin
* Initial placeholder implementation for caching
* Fix crash on removing overlap statuses
* Migrate counters to long
* Remove unused counters. Add minimal TimelineDAOTest
* Fix bug with placeholder ID
* Update cache in response to events. Refactor TimelineCases
* Fix crash, reduce number of placeholders
* Fix crash, fix filtering, improve placeholder handling
* Fix migration, add 8-9 migration test
* Fix initial timeline update, remove more placeholders
* Add cleanup for old statuses
* Fix cleanup
* Delete ExampleInstrumentedTest
* Improve timeline UX regarding caching
* Fix typos
* Fix initial timeline update
* Cleanup/fix initial timeline update
* Workaround for weird behavior of first post on initial tl update.
* Change counter types back to int
* Clear timeline cache on logout
* Fix loading when timeline is completely empty
* Fix androidx migration issues
* Fix tests
* Apply caching feedback
* Save account emojis to cache
* Fix warnings and bugs
2019-01-14 22:05:08 +01:00
|
|
|
}
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-23 20:24:55 +01:00
|
|
|
fun favourite(statusId: String, favourite: Boolean): Single<Status> {
|
2018-03-27 19:47:00 +02:00
|
|
|
val call = if (favourite) {
|
2021-06-11 20:15:40 +02:00
|
|
|
mastodonApi.favouriteStatus(statusId)
|
2018-03-27 19:47:00 +02:00
|
|
|
} else {
|
2021-06-11 20:15:40 +02:00
|
|
|
mastodonApi.unfavouriteStatus(statusId)
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
Caching toots (#809)
* Initial timeline cache implementation
* Fix build/DI errors for caching
* Rename timeline entities tables. Add migration. Add DB scheme file.
* Fix uniqueness problem, change offline strategy, improve mapping
* Try to merge in new statuses, fix bottom loading, fix saving spans.
* Fix reblogs IDs, fix inserting elements from top
* Send one more request to get latest timeline statuses
* Give Timeline placeholders string id. Rewrite Either in Kotlin
* Initial placeholder implementation for caching
* Fix crash on removing overlap statuses
* Migrate counters to long
* Remove unused counters. Add minimal TimelineDAOTest
* Fix bug with placeholder ID
* Update cache in response to events. Refactor TimelineCases
* Fix crash, reduce number of placeholders
* Fix crash, fix filtering, improve placeholder handling
* Fix migration, add 8-9 migration test
* Fix initial timeline update, remove more placeholders
* Add cleanup for old statuses
* Fix cleanup
* Delete ExampleInstrumentedTest
* Improve timeline UX regarding caching
* Fix typos
* Fix initial timeline update
* Cleanup/fix initial timeline update
* Workaround for weird behavior of first post on initial tl update.
* Change counter types back to int
* Clear timeline cache on logout
* Fix loading when timeline is completely empty
* Fix androidx migration issues
* Fix tests
* Apply caching feedback
* Save account emojis to cache
* Fix warnings and bugs
2019-01-14 22:05:08 +01:00
|
|
|
return call.doAfterSuccess {
|
2021-06-11 20:15:40 +02:00
|
|
|
eventHub.dispatch(FavoriteEvent(statusId, favourite))
|
Caching toots (#809)
* Initial timeline cache implementation
* Fix build/DI errors for caching
* Rename timeline entities tables. Add migration. Add DB scheme file.
* Fix uniqueness problem, change offline strategy, improve mapping
* Try to merge in new statuses, fix bottom loading, fix saving spans.
* Fix reblogs IDs, fix inserting elements from top
* Send one more request to get latest timeline statuses
* Give Timeline placeholders string id. Rewrite Either in Kotlin
* Initial placeholder implementation for caching
* Fix crash on removing overlap statuses
* Migrate counters to long
* Remove unused counters. Add minimal TimelineDAOTest
* Fix bug with placeholder ID
* Update cache in response to events. Refactor TimelineCases
* Fix crash, reduce number of placeholders
* Fix crash, fix filtering, improve placeholder handling
* Fix migration, add 8-9 migration test
* Fix initial timeline update, remove more placeholders
* Add cleanup for old statuses
* Fix cleanup
* Delete ExampleInstrumentedTest
* Improve timeline UX regarding caching
* Fix typos
* Fix initial timeline update
* Cleanup/fix initial timeline update
* Workaround for weird behavior of first post on initial tl update.
* Change counter types back to int
* Clear timeline cache on logout
* Fix loading when timeline is completely empty
* Fix androidx migration issues
* Fix tests
* Apply caching feedback
* Save account emojis to cache
* Fix warnings and bugs
2019-01-14 22:05:08 +01:00
|
|
|
}
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-23 20:24:55 +01:00
|
|
|
fun bookmark(statusId: String, bookmark: Boolean): Single<Status> {
|
2019-11-19 10:15:32 +01:00
|
|
|
val call = if (bookmark) {
|
2021-06-11 20:15:40 +02:00
|
|
|
mastodonApi.bookmarkStatus(statusId)
|
2019-11-19 10:15:32 +01:00
|
|
|
} else {
|
2021-06-11 20:15:40 +02:00
|
|
|
mastodonApi.unbookmarkStatus(statusId)
|
2019-11-19 10:15:32 +01:00
|
|
|
}
|
|
|
|
return call.doAfterSuccess {
|
2021-06-11 20:15:40 +02:00
|
|
|
eventHub.dispatch(BookmarkEvent(statusId, bookmark))
|
2019-11-19 10:15:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-23 20:24:55 +01:00
|
|
|
fun muteConversation(statusId: String, mute: Boolean): Single<Status> {
|
2020-03-24 21:06:04 +01:00
|
|
|
val call = if (mute) {
|
2021-06-11 20:15:40 +02:00
|
|
|
mastodonApi.muteConversation(statusId)
|
2020-03-24 21:06:04 +01:00
|
|
|
} else {
|
2021-06-11 20:15:40 +02:00
|
|
|
mastodonApi.unmuteConversation(statusId)
|
2020-03-24 21:06:04 +01:00
|
|
|
}
|
|
|
|
return call.doAfterSuccess {
|
2021-06-11 20:15:40 +02:00
|
|
|
eventHub.dispatch(MuteConversationEvent(statusId, mute))
|
2020-03-24 21:06:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-23 20:24:55 +01:00
|
|
|
fun mute(statusId: String, notifications: Boolean, duration: Int?) {
|
2021-06-11 20:15:40 +02:00
|
|
|
mastodonApi.muteAccount(statusId, notifications, duration)
|
2021-06-28 21:13:24 +02:00
|
|
|
.subscribe(
|
|
|
|
{
|
|
|
|
eventHub.dispatch(MuteEvent(statusId))
|
|
|
|
},
|
|
|
|
{ t ->
|
|
|
|
Log.w("Failed to mute account", t)
|
|
|
|
}
|
|
|
|
)
|
2021-06-11 20:15:40 +02:00
|
|
|
.addTo(cancelDisposable)
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-23 20:24:55 +01:00
|
|
|
fun block(statusId: String) {
|
2021-06-11 20:15:40 +02:00
|
|
|
mastodonApi.blockAccount(statusId)
|
2021-06-28 21:13:24 +02:00
|
|
|
.subscribe(
|
|
|
|
{
|
|
|
|
eventHub.dispatch(BlockEvent(statusId))
|
|
|
|
},
|
|
|
|
{ t ->
|
|
|
|
Log.w("Failed to block account", t)
|
|
|
|
}
|
|
|
|
)
|
2021-06-11 20:15:40 +02:00
|
|
|
.addTo(cancelDisposable)
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-23 20:24:55 +01:00
|
|
|
fun delete(statusId: String): Single<DeletedStatus> {
|
2021-06-11 20:15:40 +02:00
|
|
|
return mastodonApi.deleteStatus(statusId)
|
|
|
|
.doAfterSuccess {
|
|
|
|
eventHub.dispatch(StatusDeletedEvent(statusId))
|
|
|
|
}
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-23 20:24:55 +01:00
|
|
|
fun pin(statusId: String, pin: Boolean): Single<Status> {
|
2018-10-03 21:27:52 +02:00
|
|
|
// Replace with extension method if we use RxKotlin
|
2021-06-11 20:15:40 +02:00
|
|
|
return (if (pin) mastodonApi.pinStatus(statusId) else mastodonApi.unpinStatus(statusId))
|
|
|
|
.doAfterSuccess {
|
|
|
|
eventHub.dispatch(PinEvent(statusId, pin))
|
|
|
|
}
|
2018-10-03 21:27:52 +02:00
|
|
|
}
|
|
|
|
|
2022-01-23 20:24:55 +01:00
|
|
|
fun voteInPoll(statusId: String, pollId: String, choices: List<Int>): Single<Poll> {
|
2021-06-11 20:15:40 +02:00
|
|
|
if (choices.isEmpty()) {
|
2019-04-22 10:11:00 +02:00
|
|
|
return Single.error(IllegalStateException())
|
|
|
|
}
|
|
|
|
|
|
|
|
return mastodonApi.voteInPoll(pollId, choices).doAfterSuccess {
|
2021-06-11 20:15:40 +02:00
|
|
|
eventHub.dispatch(PollVoteEvent(statusId, it))
|
2019-04-22 10:11:00 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-28 21:13:24 +02:00
|
|
|
}
|