Skip to content

Commit

Permalink
feat(greader): support mark as read or starred
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashinch committed Jan 18, 2024
1 parent 814aeac commit a9b2279
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ enum class MarkAsReadConditions {
;

fun toDate(): Date? = when (this) {
All -> Date()
All -> null
else -> Calendar.getInstance().apply {
time = Date()
add(Calendar.DAY_OF_MONTH, when (this@MarkAsReadConditions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import me.ash.reader.infrastructure.di.IODispatcher
import me.ash.reader.infrastructure.di.MainDispatcher
import me.ash.reader.infrastructure.rss.RssHelper
import me.ash.reader.infrastructure.rss.provider.greader.GoogleReaderAPI
import me.ash.reader.infrastructure.rss.provider.greader.GoogleReaderAPI.Companion.ofCategoryIdToStreamId
import me.ash.reader.infrastructure.rss.provider.greader.GoogleReaderAPI.Companion.ofCategoryStreamIdToId
import me.ash.reader.infrastructure.rss.provider.greader.GoogleReaderAPI.Companion.ofFeedIdToStreamId
import me.ash.reader.infrastructure.rss.provider.greader.GoogleReaderAPI.Companion.ofFeedStreamIdToId
import me.ash.reader.infrastructure.rss.provider.greader.GoogleReaderAPI.Companion.ofItemStreamIdToId
import me.ash.reader.ui.ext.currentAccountId
Expand Down Expand Up @@ -346,49 +348,46 @@ class GoogleReaderRssService @Inject constructor(
) {
super.markAsRead(groupId, feedId, articleId, before, isUnread)
val googleReaderAPI = getGoogleReaderAPI()
val beforeUnixTimestamp = (before?.time ?: Date(Long.MAX_VALUE).time) / 1000
val sinceTime = before?.time
when {
groupId != null -> {
// googleReaderAPI.markGroup(
// status = if (isUnread) FeverDTO.StatusEnum.Unread else FeverDTO.StatusEnum.Read,
// id = groupId.dollarLast().toLong(),
// before = beforeUnixTimestamp
// )
googleReaderAPI.markAllAsRead(
streamId = groupId.dollarLast().ofCategoryIdToStreamId(),
sinceTimestamp = sinceTime
)
}

feedId != null -> {
// googleReaderAPI.markFeed(
// status = if (isUnread) FeverDTO.StatusEnum.Unread else FeverDTO.StatusEnum.Read,
// id = feedId.dollarLast().toLong(),
// before = beforeUnixTimestamp
// )
// TODO: Nothing happened???
googleReaderAPI.markAllAsRead(
streamId = feedId.dollarLast().ofFeedIdToStreamId(),
sinceTimestamp = sinceTime
)
}

articleId != null -> {
// googleReaderAPI.markItem(
// status = if (isUnread) FeverDTO.StatusEnum.Unread else FeverDTO.StatusEnum.Read,
// id = articleId.dollarLast(),
// )
googleReaderAPI.editTag(
itemIds = listOf(articleId.dollarLast()),
mark = if (!isUnread) GoogleReaderAPI.Label.READ else null,
unmark = if (isUnread) GoogleReaderAPI.Label.READ else null,
)
}

else -> {
feedDao.queryAll(context.currentAccountId).forEach {
// googleReaderAPI.markFeed(
// status = if (isUnread) FeverDTO.StatusEnum.Unread else FeverDTO.StatusEnum.Read,
// id = it.id.dollarLast().toLong(),
// before = beforeUnixTimestamp
// )
}
googleReaderAPI.markAllAsRead(
streamId = GoogleReaderAPI.Label.ALL_ITEMS,
sinceTimestamp = sinceTime
)
}
}
}

override suspend fun markAsStarred(articleId: String, isStarred: Boolean) {
super.markAsStarred(articleId, isStarred)
val googleReaderAPI = getGoogleReaderAPI()
// googleReaderAPI.markItem(
// status = if (isStarred) FeverDTO.StatusEnum.Saved else FeverDTO.StatusEnum.Unsaved,
// id = articleId.dollarLast()
// )
getGoogleReaderAPI().editTag(
itemIds = listOf(articleId.dollarLast()),
mark = if (isStarred) GoogleReaderAPI.Label.STARRED else null,
unmark = if (!isStarred) GoogleReaderAPI.Label.STARRED else null,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class GoogleReaderAPI private constructor(
retryableGetRequest<GoogleReaderDTO.ItemIds>(
query = "reader/api/0/stream/items/ids",
params = listOf(
Pair("s", "user/-/state/com.google/read"),
Pair("s", Label.READ),
Pair("ot", since.toString()),
Pair("n", MAXIMUM_ITEMS_LIMIT),
))
Expand All @@ -191,16 +191,16 @@ class GoogleReaderAPI private constructor(
retryableGetRequest<GoogleReaderDTO.ItemIds>(
query = "reader/api/0/stream/items/ids",
params = listOf(
Pair("s", "user/-/state/com.google/reading-list"),
Pair("xt", "user/-/state/com.google/read"),
Pair("s", Label.ALL_ITEMS),
Pair("xt", Label.READ),
Pair("n", MAXIMUM_ITEMS_LIMIT),
))

suspend fun getStarredItemIds(): GoogleReaderDTO.ItemIds =
retryableGetRequest<GoogleReaderDTO.ItemIds>(
query = "reader/api/0/stream/items/ids",
params = listOf(
Pair("s", "user/-/state/com.google/starred"),
Pair("s", Label.STARRED),
Pair("n", MAXIMUM_ITEMS_LIMIT),
))

Expand All @@ -221,10 +221,23 @@ class GoogleReaderAPI private constructor(

enum class subscriptionOperationType

suspend fun editTag(categoryName: String): String =
object Label {

const val ALL_ITEMS = "user/-/state/com.google/reading-list"
const val READ = "user/-/state/com.google/read"
const val STARRED = "user/-/state/com.google/starred"
const val LIKE = "user/-/state/com.google/like"
const val BROADCAST = "user/-/state/com.google/broadcast"
}

suspend fun editTag(itemIds: List<String>, mark: String? = null, unmark: String? = null): String =
retryablePostRequest<String>(
query = "reader/api/0/edit-tag",
form = listOf(Pair("a", categoryName.ofCategoryIdToStreamId()))
form = mutableListOf<Pair<String, String>>().apply {
itemIds.forEach { add(Pair("i", it.ofItemIdToStreamId())) }
mark?.let { add(Pair("a", mark)) }
unmark?.let { add(Pair("r", unmark)) }
}
)

suspend fun disableTag(categoryId: String): String =
Expand All @@ -248,13 +261,23 @@ class GoogleReaderAPI private constructor(
): String = retryablePostRequest<String>(
query = "reader/api/0/subscription/edit",
form = mutableListOf(Pair("ac", action)).apply {
if (destFeedId != null) add(Pair("s", destFeedId.ofFeedIdToStreamId()))
if (destCategoryId != null) add(Pair("a", destCategoryId.ofCategoryIdToStreamId()))
if (originCategoryId != null) add(Pair("r", originCategoryId.ofCategoryIdToStreamId()))
if (destFeedName?.isNotBlank() == true) add(Pair("t", destFeedName))
destFeedId?.let { add(Pair("s", it.ofFeedIdToStreamId())) }
destCategoryId?.let { add(Pair("a", it.ofCategoryIdToStreamId())) }
originCategoryId?.let { add(Pair("r", it.ofCategoryIdToStreamId())) }
destFeedName?.takeIf { it.isNotBlank() }?.let { add(Pair("t", destFeedName)) }
}
)

suspend fun markAllAsRead(streamId: String, sinceTimestamp: Long? = null): String =
retryablePostRequest<String>(
query = "reader/api/0/mark-all-as-read",
form = mutableListOf(
Pair("s", streamId),
).apply {
sinceTimestamp?.let { add(Pair("ts", it.toString())) }
}
)

companion object {

const val MAXIMUM_ITEMS_LIMIT = "10000"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ fun FlowPage(
feedId = null,
articleId = it.article.id,
MarkAsReadConditions.All
)
)
}
item {
Spacer(modifier = Modifier.height(128.dp))
Expand Down

0 comments on commit a9b2279

Please sign in to comment.