Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/loader/users/StatusFavoritersLoader.kt

109 lines
4.6 KiB
Kotlin
Raw Normal View History

2016-08-13 16:04:31 +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-08-13 16:04:31 +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-08-13 16:04:31 +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-08-13 16:04:31 +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.users
2016-08-13 16:04:31 +02:00
import android.content.Context
2017-04-01 10:04:01 +02:00
import org.attoparser.config.ParseConfiguration
import org.attoparser.simple.AbstractSimpleMarkupHandler
import org.attoparser.simple.SimpleMarkupParser
2016-08-13 16:04:31 +02:00
import org.mariotaku.microblog.library.MicroBlog
import org.mariotaku.microblog.library.MicroBlogException
2017-04-20 19:23:11 +02:00
import org.mariotaku.microblog.library.mastodon.Mastodon
2017-04-01 10:04:01 +02:00
import org.mariotaku.microblog.library.twitter.TwitterWeb
2017-04-21 11:23:55 +02:00
import org.mariotaku.microblog.library.twitter.model.IDs
import org.mariotaku.microblog.library.twitter.model.IDsAccessor
2016-08-13 16:04:31 +02:00
import org.mariotaku.microblog.library.twitter.model.Paging
2017-04-01 10:04:01 +02:00
import org.mariotaku.twidere.annotation.AccountType
2017-04-21 11:23:55 +02:00
import org.mariotaku.twidere.exception.APINotSupportedException
import org.mariotaku.twidere.extension.api.lookupUsersMapPaginated
import org.mariotaku.twidere.extension.model.api.mastodon.mapToPaginated
2017-04-20 19:23:11 +02:00
import org.mariotaku.twidere.extension.model.api.mastodon.toParcelable
import org.mariotaku.twidere.extension.model.api.toParcelable
2017-04-01 10:04:01 +02:00
import org.mariotaku.twidere.extension.model.isOfficial
import org.mariotaku.twidere.extension.model.newMicroBlogInstance
2016-12-03 17:09:27 +01:00
import org.mariotaku.twidere.model.AccountDetails
2016-08-13 16:04:31 +02:00
import org.mariotaku.twidere.model.ParcelableUser
import org.mariotaku.twidere.model.UserKey
2017-04-21 11:23:55 +02:00
import org.mariotaku.twidere.model.pagination.PaginatedList
2017-04-01 10:04:01 +02:00
import java.text.ParseException
2016-08-13 16:04:31 +02:00
2016-12-03 17:09:27 +01:00
class StatusFavoritersLoader(
2016-08-13 16:04:31 +02:00
context: Context,
2016-12-03 17:09:27 +01:00
accountKey: UserKey,
private val statusId: String,
2016-08-13 16:04:31 +02:00
data: List<ParcelableUser>?,
fromUser: Boolean
2017-04-21 11:23:55 +02:00
) : AbsRequestUsersLoader(context, accountKey, data, fromUser) {
2016-08-13 16:04:31 +02:00
@Throws(MicroBlogException::class)
2017-04-21 11:23:55 +02:00
override fun getUsers(details: AccountDetails, paging: Paging): PaginatedList<ParcelableUser> {
2017-04-20 19:23:11 +02:00
when (details.type) {
AccountType.MASTODON -> {
val mastodon = details.newMicroBlogInstance(context, Mastodon::class.java)
2017-04-21 11:23:55 +02:00
return mastodon.getStatusFavouritedBy(statusId).mapToPaginated {
it.toParcelable(details)
2017-04-20 19:23:11 +02:00
}
}
AccountType.TWITTER -> {
val microBlog = details.newMicroBlogInstance(context, MicroBlog::class.java)
val ids = if (details.isOfficial(context)) {
2017-04-21 11:23:55 +02:00
microBlog.getStatusActivitySummary(statusId).favoriters
2017-04-20 19:23:11 +02:00
} else {
val web = details.newMicroBlogInstance(context, TwitterWeb::class.java)
val htmlUsers = web.getFavoritedPopup(statusId).htmlUsers
2017-04-21 11:23:55 +02:00
IDsAccessor.setIds(IDs(), parseUserIds(htmlUsers))
2017-04-20 19:23:11 +02:00
}
2017-04-21 11:23:55 +02:00
return microBlog.lookupUsersMapPaginated(ids) {
it.toParcelable(details, profileImageSize = profileImageSize)
2017-04-20 19:23:11 +02:00
}
}
else -> {
2017-04-21 11:23:55 +02:00
throw APINotSupportedException(details.type)
2017-04-01 10:04:01 +02:00
}
}
2016-12-03 17:09:27 +01:00
}
2017-04-01 10:04:01 +02:00
@Throws(MicroBlogException::class)
private fun parseUserIds(html: String): Array<String> {
val parser = SimpleMarkupParser(ParseConfiguration.htmlConfiguration())
val userIds = ArrayList<String>()
val handler = object : AbstractSimpleMarkupHandler() {
override fun handleOpenElement(elementName: String, attributes: Map<String, String>?,
line: Int, col: Int) {
if (elementName == "div" && attributes != null) {
2019-10-24 17:52:11 +02:00
if (attributes["class"]?.split(" ")?.contains("account") == true) {
2017-04-01 10:04:01 +02:00
attributes["data-user-id"]?.let { userIds.add(it) }
}
}
}
}
try {
parser.parse(html, handler)
} catch (e: ParseException) {
throw MicroBlogException(e)
}
if (userIds.isEmpty()) {
throw MicroBlogException("Invalid response")
}
return userIds.distinct().toTypedArray()
}
2016-08-13 16:04:31 +02:00
}