Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/loader/statuses/UserTimelineLoader.kt

217 lines
9.7 KiB
Kotlin
Raw Normal View History

2016-07-02 06:37:42 +02:00
/*
2017-04-21 11:23:55 +02:00
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
2016-07-02 06:37:42 +02:00
* 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.
2017-04-21 11:23:55 +02:00
*
2016-07-02 06:37:42 +02:00
* This program 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.
2017-04-21 11:23:55 +02:00
*
2016-07-02 06:37:42 +02:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2017-04-21 11:23:55 +02:00
package org.mariotaku.twidere.loader.statuses
2016-07-02 06:37:42 +02:00
import android.content.Context
2020-01-26 08:35:15 +01:00
import androidx.annotation.WorkerThread
2016-07-02 06:37:42 +02:00
import android.text.TextUtils
2017-04-07 13:05:02 +02:00
import okhttp3.HttpUrl
import org.attoparser.ParseException
import org.attoparser.config.ParseConfiguration
import org.attoparser.simple.AbstractSimpleMarkupHandler
import org.attoparser.simple.SimpleMarkupParser
2016-07-02 06:37:42 +02:00
import org.mariotaku.microblog.library.MicroBlog
import org.mariotaku.microblog.library.MicroBlogException
2017-04-19 08:50:47 +02:00
import org.mariotaku.microblog.library.mastodon.Mastodon
2017-04-21 14:24:15 +02:00
import org.mariotaku.microblog.library.mastodon.model.LinkHeaderList
2016-07-02 06:37:42 +02:00
import org.mariotaku.microblog.library.twitter.model.Paging
import org.mariotaku.microblog.library.twitter.model.Status
2017-03-27 05:08:09 +02:00
import org.mariotaku.microblog.library.twitter.model.TimelineOption
2017-04-07 13:05:02 +02:00
import org.mariotaku.restfu.annotation.method.GET
import org.mariotaku.restfu.http.Endpoint
import org.mariotaku.restfu.http.HttpRequest
import org.mariotaku.restfu.http.mime.SimpleBody
2017-04-21 14:24:15 +02:00
import org.mariotaku.twidere.alias.MastodonStatus
import org.mariotaku.twidere.alias.MastodonTimelineOption
2017-04-07 13:05:02 +02:00
import org.mariotaku.twidere.annotation.AccountType
2017-09-16 13:11:27 +02:00
import org.mariotaku.twidere.annotation.FilterScope
import org.mariotaku.twidere.extension.api.tryShowUser
2017-04-21 14:24:15 +02:00
import org.mariotaku.twidere.extension.model.api.mastodon.mapToPaginated
2017-04-19 08:50:47 +02:00
import org.mariotaku.twidere.extension.model.api.mastodon.toParcelable
import org.mariotaku.twidere.extension.model.api.toParcelable
2017-09-16 13:11:27 +02:00
import org.mariotaku.twidere.extension.model.api.updateFilterInfoForUserTimeline
2017-04-19 08:50:47 +02:00
import org.mariotaku.twidere.extension.model.newMicroBlogInstance
2016-12-04 04:58:03 +01:00
import org.mariotaku.twidere.model.AccountDetails
2016-07-02 06:37:42 +02:00
import org.mariotaku.twidere.model.ParcelableStatus
import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.model.timeline.UserTimelineFilter
2017-04-07 13:05:02 +02:00
import org.mariotaku.twidere.util.JsonSerializer
import org.mariotaku.twidere.util.dagger.DependencyHolder
2017-09-16 13:11:27 +02:00
import org.mariotaku.twidere.util.database.ContentFiltersUtils
2017-04-07 13:05:02 +02:00
import java.io.IOException
2016-08-19 16:25:27 +02:00
import java.util.concurrent.atomic.AtomicReference
2016-07-02 06:37:42 +02:00
class UserTimelineLoader(
context: Context,
2017-04-12 14:58:08 +02:00
accountKey: UserKey?,
2017-04-07 13:05:02 +02:00
private val userKey: UserKey?,
2016-07-02 06:37:42 +02:00
private val screenName: String?,
2017-04-07 13:05:02 +02:00
private val profileUrl: String?,
2016-07-02 06:37:42 +02:00
data: List<ParcelableStatus>?,
savedStatusesArgs: Array<String>?,
tabPosition: Int,
fromUser: Boolean,
2016-08-19 16:25:27 +02:00
loadingMore: Boolean,
val loadPinnedStatus: Boolean,
val timelineFilter: UserTimelineFilter? = null
2017-04-21 14:24:15 +02:00
) : AbsRequestStatusesLoader(context, accountKey, data, savedStatusesArgs, tabPosition, fromUser, loadingMore) {
2016-08-19 16:25:27 +02:00
private val pinnedStatusesRef = AtomicReference<List<ParcelableStatus>>()
var pinnedStatuses: List<ParcelableStatus>?
get() = pinnedStatusesRef.get()
private set(value) {
pinnedStatusesRef.set(value)
}
2016-07-02 06:37:42 +02:00
@Throws(MicroBlogException::class)
2017-04-19 08:50:47 +02:00
override fun getStatuses(account: AccountDetails, paging: Paging) = when (account.type) {
2017-04-21 14:24:15 +02:00
AccountType.MASTODON -> getMastodonStatuses(account, paging).mapToPaginated {
it.toParcelable(account)
2017-04-19 08:50:47 +02:00
}
2017-04-21 14:24:15 +02:00
else -> getMicroBlogStatuses(account, paging).mapMicroBlogToPaginated {
2017-09-16 13:11:27 +02:00
it.toParcelable(account, profileImageSize = profileImageSize,
updateFilterInfoAction = ::updateFilterInfoForUserTimeline)
2017-04-19 08:50:47 +02:00
}
}
@WorkerThread
2017-09-16 13:11:27 +02:00
override fun shouldFilterStatus(status: ParcelableStatus): Boolean {
2017-05-12 13:23:28 +02:00
if (timelineFilter != null) {
if (status.is_retweet && !timelineFilter.isIncludeRetweets) {
return true
}
}
2017-04-19 08:50:47 +02:00
if (accountKey != null && userKey != null && TextUtils.equals(accountKey.id, userKey.id))
return false
2017-09-16 13:11:27 +02:00
return ContentFiltersUtils.isFiltered(context.contentResolver, status, true,
FilterScope.USER_TIMELINE)
2017-04-19 08:50:47 +02:00
}
2017-04-21 14:24:15 +02:00
private fun getMastodonStatuses(account: AccountDetails, paging: Paging): LinkHeaderList<MastodonStatus> {
2017-04-19 08:50:47 +02:00
val mastodon = account.newMicroBlogInstance(context, Mastodon::class.java)
val id = userKey?.id ?: throw MicroBlogException("Only ID are supported at this moment")
2017-04-21 14:24:15 +02:00
val option = MastodonTimelineOption()
2017-04-19 08:50:47 +02:00
if (timelineFilter != null) {
option.excludeReplies(!timelineFilter.isIncludeReplies)
}
2017-05-12 13:23:28 +02:00
2017-04-19 08:50:47 +02:00
return mastodon.getStatuses(id, paging, option)
}
private fun getMicroBlogStatuses(account: AccountDetails, paging: Paging): List<Status> {
val microBlog = account.newMicroBlogInstance(context, MicroBlog::class.java)
if (loadPinnedStatus && account.type == AccountType.TWITTER && !loadingMore) {
2017-03-02 03:42:40 +01:00
pinnedStatuses = try {
val pinnedIds = microBlog.tryShowUser(userKey?.id, screenName, AccountType.TWITTER).pinnedTweetIds
if (pinnedIds != null && pinnedIds.isNotEmpty()) {
microBlog.lookupStatuses(pinnedIds).mapIndexed { idx, status ->
val created = status.toParcelable(account, profileImageSize = profileImageSize)
created.sort_id = idx.toLong()
created.is_pinned_status = true
return@mapIndexed created
}
} else {
null
2016-08-19 16:25:27 +02:00
}
} catch (e: MicroBlogException) {
2017-03-02 03:42:40 +01:00
null
2016-08-19 16:25:27 +02:00
}
}
2017-03-27 05:08:09 +02:00
val option = TimelineOption()
if (timelineFilter != null) {
option.setExcludeReplies(!timelineFilter.isIncludeReplies)
option.setIncludeRetweets(timelineFilter.isIncludeRetweets)
}
2020-06-09 02:21:48 +02:00
when {
userKey != null -> {
if (account.type == AccountType.STATUSNET && userKey.host != account.key.host
2017-04-07 13:05:02 +02:00
&& profileUrl != null) {
2020-06-09 02:21:48 +02:00
try {
return showStatusNetExternalTimeline(profileUrl, paging)
} catch (e: IOException) {
throw MicroBlogException(e)
}
2017-04-07 13:05:02 +02:00
}
2020-06-09 02:21:48 +02:00
return microBlog.getUserTimeline(userKey.id, paging, option)
}
screenName != null -> {
return microBlog.getUserTimelineByScreenName(screenName, paging, option)
}
else -> {
throw MicroBlogException("Invalid user")
2017-04-07 13:05:02 +02:00
}
2016-07-02 06:37:42 +02:00
}
}
2017-04-07 13:46:45 +02:00
@Throws(IOException::class)
2017-04-07 13:05:02 +02:00
private fun showStatusNetExternalTimeline(profileUrl: String, paging: Paging): List<Status> {
val holder = DependencyHolder.get(context)
val client = holder.restHttpClient
val parser = SimpleMarkupParser(ParseConfiguration.htmlConfiguration())
val pageRequest = HttpRequest.Builder().apply {
method(GET.METHOD)
url(profileUrl)
}.build()
val validAtomSuffix = ".atom"
val requestLink = client.newCall(pageRequest).execute().use {
if (!it.isSuccessful) throw IOException("Server returned ${it.status} response")
val handler = AtomLinkFindHandler(profileUrl)
try {
parser.parse(SimpleBody.reader(it.body), handler)
} catch (e: ParseException) {
// Ignore
}
return@use handler.atomLink
}?.takeIf { it.endsWith(validAtomSuffix) }?.let {
it.replaceRange(it.length - validAtomSuffix.length, it.length, ".json")
} ?: throw IOException("No atom link found fof external user")
val queries = paging.asMap().map { arrayOf(it.key, it.value?.toString()) }.toTypedArray()
val restRequest = HttpRequest.Builder().apply {
method(GET.METHOD)
url(Endpoint.constructUrl(requestLink, *queries))
}.build()
return client.newCall(restRequest).execute().use {
if (!it.isSuccessful) throw IOException("Server returned ${it.status} response")
return@use JsonSerializer.parseList(it.body.stream(), Status::class.java)
}
}
private class AtomLinkFindHandler(val profileUrl: String) : AbstractSimpleMarkupHandler() {
var atomLink: String? = null
override fun handleStandaloneElement(elementName: String, attributes: Map<String, String>?,
minimized: Boolean, line: Int, col: Int) {
if (atomLink != null || elementName != "link" || attributes == null) return
if (attributes["rel"] == "alternate" && attributes["type"] == "application/atom+xml") {
val href = attributes["href"] ?: return
atomLink = HttpUrl.parse(profileUrl)?.resolve(href)?.toString()
2017-04-07 13:05:02 +02:00
}
}
}
2017-04-19 13:30:07 +02:00
companion object {
fun getMastodonStatuses(mastodon: Mastodon, userKey: UserKey?, screenName: String?, paging: Paging,
2017-04-21 14:24:15 +02:00
option: MastodonTimelineOption?): LinkHeaderList<MastodonStatus> {
2017-04-19 13:30:07 +02:00
val id = userKey?.id ?: throw MicroBlogException("Only ID are supported at this moment")
return mastodon.getStatuses(id, paging, option)
}
}
2016-07-02 06:37:42 +02:00
}