Skip to content

Commit

Permalink
add badge support for vvo
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlaster committed Oct 18, 2024
1 parent 61afbf8 commit f0fdf37
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,20 @@ internal class ComposeInAppNotification : InAppNotification {
}

override fun onSuccess(message: Message) {
val messageId =
when (message) {
Message.Compose -> R.string.compose_notification_success_title
}
_source.value = Event(Notification.StringNotification(messageId, success = true))
_source.value = Event(Notification.StringNotification(message.title, success = true))
}

override fun onError(
message: Message,
throwable: Throwable,
) {
val messageId =
when (message) {
Message.Compose -> R.string.compose_notification_error_title
}
_source.value = Event(Notification.StringNotification(messageId, success = false))
_source.value = Event(Notification.StringNotification(message.title, success = false))
}
}

private val Message.title
get() =
when (this) {
Message.Compose -> R.string.compose_notification_title
Message.LoginExpired -> R.string.notification_login_expired
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -671,4 +671,6 @@

<string name="profile_search_user_using_account">Find %1$s in %2$s using %3$s</string>

<string name="notification_login_expired">Login expired, please login again</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ interface InAppNotification {

enum class Message {
Compose,
LoginExpired,
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal class CommentPagingSource(
private val service: VVOService,
private val event: StatusEvent.VVO,
private val accountKey: MicroBlogKey,
private val onClearMarker: () -> Unit,
) : PagingSource<Int, UiTimeline>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, UiTimeline> {
return try {
Expand All @@ -22,6 +23,9 @@ internal class CommentPagingSource(
LoginExpiredException,
)
}
if (params.key == null) {
onClearMarker.invoke()
}
val response =
service.getComments(
page = params.key ?: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import androidx.paging.ExperimentalPagingApi
import androidx.paging.LoadType
import androidx.paging.PagingState
import androidx.paging.RemoteMediator
import dev.dimension.flare.common.InAppNotification
import dev.dimension.flare.common.Message
import dev.dimension.flare.data.database.cache.CacheDatabase
import dev.dimension.flare.data.database.cache.mapper.VVO
import dev.dimension.flare.data.database.cache.model.DbPagingTimelineView
Expand All @@ -17,6 +19,7 @@ internal class HomeTimelineRemoteMediator(
private val database: CacheDatabase,
private val accountKey: MicroBlogKey,
private val pagingKey: String,
private val inAppNotification: InAppNotification,
) : RemoteMediator<Int, DbPagingTimelineView>() {
override suspend fun initialize(): InitializeAction = InitializeAction.SKIP_INITIAL_REFRESH

Expand All @@ -27,6 +30,10 @@ internal class HomeTimelineRemoteMediator(
return try {
val config = service.config()
if (config.data?.login != true) {
inAppNotification.onError(
Message.LoginExpired,
LoginExpiredException,
)
return MediatorResult.Error(
LoginExpiredException,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal class LikePagingSource(
private val service: VVOService,
private val event: StatusEvent.VVO,
private val accountKey: MicroBlogKey,
private val onClearMarker: () -> Unit,
) : PagingSource<Int, UiTimeline>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, UiTimeline> {
return try {
Expand All @@ -22,6 +23,9 @@ internal class LikePagingSource(
LoginExpiredException,
)
}
if (params.key == null) {
onClearMarker.invoke()
}
val response =
service.getAttitudes(
page = params.key ?: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal class MentionRemoteMediator(
private val database: CacheDatabase,
private val accountKey: MicroBlogKey,
private val pagingKey: String,
private val onClearMarker: () -> Unit,
) : RemoteMediator<Int, DbPagingTimelineView>() {
var page = 1

Expand All @@ -40,6 +41,7 @@ internal class MentionRemoteMediator(
page = page,
).also {
database.pagingTimelineDao().delete(pagingKey = pagingKey, accountKey = accountKey)
onClearMarker.invoke()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.paging.cachedIn
import dev.dimension.flare.common.CacheData
import dev.dimension.flare.common.Cacheable
import dev.dimension.flare.common.FileItem
import dev.dimension.flare.common.InAppNotification
import dev.dimension.flare.common.MemCacheable
import dev.dimension.flare.common.decodeJson
import dev.dimension.flare.data.database.cache.CacheDatabase
Expand Down Expand Up @@ -41,8 +42,10 @@ import dev.dimension.flare.ui.model.toUi
import dev.dimension.flare.ui.presenter.compose.ComposeStatus
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.launch
import kotlinx.datetime.Clock
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

Expand All @@ -56,6 +59,7 @@ class VVODataSource(
private val database: CacheDatabase by inject()
private val localFilterRepository: LocalFilterRepository by inject()
private val coroutineScope: CoroutineScope by inject()
private val inAppNotification: InAppNotification by inject()
private val service by lazy {
VVOService(credential.chocolate)
}
Expand All @@ -78,6 +82,7 @@ class VVODataSource(
database,
accountKey,
pagingKey,
inAppNotification,
),
)

Expand All @@ -103,6 +108,9 @@ class VVODataSource(
database,
accountKey,
pagingKey,
onClearMarker = {
MemCacheable.update(notificationMarkerMentionKey, 0)
},
),
)

Expand All @@ -114,6 +122,9 @@ class VVODataSource(
service = service,
accountKey = accountKey,
event = this,
onClearMarker = {
MemCacheable.update(notificationMarkerCommentKey, 0)
},
)
}.flow.cachedIn(scope)

Expand All @@ -125,6 +136,9 @@ class VVODataSource(
service = service,
accountKey = accountKey,
event = this,
onClearMarker = {
MemCacheable.update(notificationMarkerLikeKey, 0)
},
)
}.flow.cachedIn(scope)
}
Expand Down Expand Up @@ -715,4 +729,42 @@ class VVODataSource(
}
}
}

private val notificationMarkerMentionKey: String
get() = "notificationBadgeCount_mention_$accountKey"

private val notificationMarkerCommentKey: String
get() = "notificationBadgeCount_comment_$accountKey"

private val notificationMarkerLikeKey: String
get() = "notificationBadgeCount_like_$accountKey"

override fun notificationBadgeCount(): CacheData<Int> =
Cacheable(
fetchSource = {
val config = service.config()
val st = config.data?.st
requireNotNull(st) { "st is null" }
val response =
service.remindUnread(
time = Clock.System.now().toEpochMilliseconds() / 1000,
st = st,
)
val mention = response.data?.mentionStatus ?: 0
val comment = response.data?.cmt ?: 0
val like = response.data?.attitude ?: 0

MemCacheable.update(notificationMarkerMentionKey, mention)
MemCacheable.update(notificationMarkerCommentKey, comment)
MemCacheable.update(notificationMarkerLikeKey, like)
},
cacheSource = {
val mentionFlow = MemCacheable.subscribe<Long>(notificationMarkerMentionKey)
val commentFlow = MemCacheable.subscribe<Long>(notificationMarkerCommentKey)
val likeFlow = MemCacheable.subscribe<Long>(notificationMarkerLikeKey)
combine(mentionFlow, commentFlow, likeFlow) { mention, comment, like ->
(mention + comment + like).toInt()
}
},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import dev.dimension.flare.data.network.nodeinfo.model.Schema21
import dev.dimension.flare.model.PlatformType
import dev.dimension.flare.model.vvo
import dev.dimension.flare.model.vvoHost
import dev.dimension.flare.model.vvoHostLong
import dev.dimension.flare.model.vvoHostShort
import dev.dimension.flare.model.xqtHost
import io.ktor.client.call.body
Expand Down Expand Up @@ -79,7 +80,7 @@ internal data object NodeInfoService {
if (host.equals(xqtHost, ignoreCase = true) || host.equals("x.social", ignoreCase = true)) {
return@coroutineScope PlatformType.xQt
}
val vvo = listOf(vvoHost, vvo, vvoHostShort, "vvo.social")
val vvo = listOf(vvoHost, vvo, vvoHostShort, "vvo.social", vvoHostLong)
if (vvo.any { it.equals(host, ignoreCase = true) }) {
return@coroutineScope PlatformType.VVo
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package dev.dimension.flare.data.network.vvo.api

import de.jensklingenberg.ktorfit.http.GET
import de.jensklingenberg.ktorfit.http.Header
import de.jensklingenberg.ktorfit.http.Query
import dev.dimension.flare.data.network.vvo.model.Config
import dev.dimension.flare.data.network.vvo.model.UnreadData
import dev.dimension.flare.data.network.vvo.model.VVOResponse

internal interface ConfigApi {
@GET("api/config")
suspend fun config(): VVOResponse<Config>

@GET("api/remind/unread")
suspend fun remindUnread(
@Query("t") time: Long,
@Header("X-Xsrf-Token") st: String,
): VVOResponse<UnreadData>
}
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,25 @@ data class UploadResponse(
@SerialName("original_pic")
val originalPic: String? = null,
)

@Serializable
data class UnreadData(
val cmt: Long? = null,
val status: Long? = null,
val follower: Long? = null,
val dm: Long? = null,
@SerialName("mention_cmt")
val mentionCmt: Long? = null,
@SerialName("mention_status")
val mentionStatus: Long? = null,
val attitude: Long? = null,
val unreadmblog: Long? = null,
val uid: String? = null,
val bi: Long? = null,
val newfans: Long? = null,
val unreadmsg: Map<String, Long>? = null,
// val group: Any? = null,
val notice: Long? = null,
val photo: Long? = null,
val msgbox: Long? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ val vvoHostShort: String =
append(vvo)
append("LmNu".decodeBase64String())
}

val vvoHostLong: String =
buildString {
append("d2Vp".decodeBase64String())
append("Ym8uY29t".decodeBase64String())
}

0 comments on commit f0fdf37

Please sign in to comment.