Skip to content

Commit

Permalink
refactor: enumの値を使用するようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
pantasystem committed Dec 21, 2023
1 parent 428944e commit c5277dc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,41 @@ data class MstNotificationDTO(
val emojiReaction: EmojiReaction? = null,
) {
@kotlinx.serialization.Serializable
enum class NotificationType {
enum class NotificationType(
val value: String
) {
@SerialName("mention")
Mention,
Mention("mention"),

@SerialName("status")
Status,
Status("status"),

@SerialName("reblog")
Reblog,
Reblog("reblog"),

@SerialName("follow")
Follow,
Follow("follow"),

@SerialName("follow_request")
FollowRequest,
FollowRequest("follow_request"),

@SerialName("favourite")
Favourite,
Favourite("favourite"),

@SerialName("poll")
Poll,
Poll("poll"),

@SerialName("update")
Update,
Update("update"),

@SerialName("admin.sign_up")
AdminSingUp,
AdminSingUp("admin.sign_up"),

@SerialName("admin.report")
AdminReport,
AdminReport("admin.report"),

@SerialName("emoji_reaction")
EmojiReaction
EmojiReaction("emoji_reaction"),
}

@kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package net.pantasystem.milktea.data.infrastructure.note
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.sync.Mutex
import net.pantasystem.milktea.api.mastodon.notification.MstNotificationDTO
import net.pantasystem.milktea.api.mastodon.status.TootStatusDTO
import net.pantasystem.milktea.common.MastodonLinkHeaderDecoder
import net.pantasystem.milktea.common.PageableState
Expand Down Expand Up @@ -60,25 +61,30 @@ internal class MastodonTimelineStorePagingStoreImpl(
pageableTimeline.hashtag,
minId = getSinceId(),
).getBodyOrFail()

is Pageable.Mastodon.HomeTimeline -> api.getHomeTimeline(
minId = getSinceId(),
visibilities = getVisibilitiesParameter(getAccount()),
).getBodyOrFail()

is Pageable.Mastodon.ListTimeline -> api.getListTimeline(
minId = getSinceId(),
listId = pageableTimeline.listId,
).getBodyOrFail()

is Pageable.Mastodon.LocalTimeline -> api.getPublicTimeline(
local = true,
minId = getSinceId(),
visibilities = getVisibilitiesParameter(getAccount()),
onlyMedia = pageableTimeline.getOnlyMedia()
).getBodyOrFail()

is Pageable.Mastodon.PublicTimeline -> api.getPublicTimeline(
minId = getSinceId(),
visibilities = getVisibilitiesParameter(getAccount()),
onlyMedia = pageableTimeline.getOnlyMedia()
).getBodyOrFail()

is Pageable.Mastodon.UserTimeline -> {
api.getAccountTimeline(
accountId = pageableTimeline.userId,
Expand All @@ -90,24 +96,30 @@ internal class MastodonTimelineStorePagingStoreImpl(
updateMinIdFrom(it)
}.getBodyOrFail()
}

Pageable.Mastodon.BookmarkTimeline -> {
api.getBookmarks(
minId = minId
).throwIfHasError().also {
updateMinIdFrom(it)
}.getBodyOrFail()
}

is Pageable.Mastodon.SearchTimeline -> {
return@runCancellableCatching emptyList()
}

is Pageable.Mastodon.TrendTimeline -> {
return@runCancellableCatching emptyList()
}

is Pageable.Mastodon.Mention -> {
api.getNotifications(
minId = minId,
types = listOf("mention"),
excludeTypes = listOf("status", "reblog", "follow", "follow_request", "favourite", "poll", "update", "update", "admin.sign_up", "admin.report", "emoji_reaction")
types = listOf(MstNotificationDTO.NotificationType.Mention.value),
excludeTypes = MstNotificationDTO.NotificationType.values()
.filterNot { it == MstNotificationDTO.NotificationType.Mention }
.map { it.value },
).throwIfHasError().also {
updateMinIdFrom(it)
}.body()?.mapNotNull {
Expand Down Expand Up @@ -170,25 +182,30 @@ internal class MastodonTimelineStorePagingStoreImpl(
maxId = maxId,
onlyMedia = pageableTimeline.getOnlyMedia()
).getBodyOrFail()

is Pageable.Mastodon.HomeTimeline -> api.getHomeTimeline(
maxId = maxId,
visibilities = getVisibilitiesParameter(getAccount())
).getBodyOrFail()

is Pageable.Mastodon.ListTimeline -> api.getListTimeline(
maxId = maxId,
listId = pageableTimeline.listId,
).getBodyOrFail()
).getBodyOrFail()

is Pageable.Mastodon.LocalTimeline -> api.getPublicTimeline(
local = true,
maxId = maxId,
visibilities = getVisibilitiesParameter(getAccount()),
onlyMedia = pageableTimeline.getOnlyMedia()
).getBodyOrFail()

is Pageable.Mastodon.PublicTimeline -> api.getPublicTimeline(
maxId = maxId,
visibilities = getVisibilitiesParameter(getAccount()),
onlyMedia = pageableTimeline.getOnlyMedia()
).getBodyOrFail()

is Pageable.Mastodon.UserTimeline -> {
api.getAccountTimeline(
accountId = pageableTimeline.userId,
Expand All @@ -200,13 +217,15 @@ internal class MastodonTimelineStorePagingStoreImpl(
updateMaxIdFrom(it)
}.body()
}

Pageable.Mastodon.BookmarkTimeline -> {
api.getBookmarks(
maxId = maxId,
).throwIfHasError().also {
updateMaxIdFrom(it)
}.body()
}

is Pageable.Mastodon.SearchTimeline -> {
api.search(
q = pageableTimeline.query,
Expand All @@ -218,16 +237,20 @@ internal class MastodonTimelineStorePagingStoreImpl(
updateMaxIdFrom(it)
}.body()?.statuses
}

is Pageable.Mastodon.TrendTimeline -> {
api.getTrendStatuses(
offset = (getState().content as? StateContent.Exist)?.rawContent?.size ?: 0
).getBodyOrFail()
}

is Pageable.Mastodon.Mention -> {
api.getNotifications(
maxId = maxId,
types = listOf("mention"),
excludeTypes = listOf("status", "reblog", "follow", "follow_request", "favourite", "poll", "update", "update", "admin.sign_up", "admin.report", "emoji_reaction")
types = listOf(MstNotificationDTO.NotificationType.Mention.value),
excludeTypes = MstNotificationDTO.NotificationType.values()
.filterNot { it == MstNotificationDTO.NotificationType.Mention }
.map { it.value },
).throwIfHasError().also {
updateMaxIdFrom(it)
}.body()?.mapNotNull {
Expand All @@ -253,7 +276,7 @@ internal class MastodonTimelineStorePagingStoreImpl(
}

private fun isEmpty(): Boolean {
return when(val content = _state.value.content) {
return when (val content = _state.value.content) {
is StateContent.Exist -> content.rawContent.isEmpty()
is StateContent.NotExist -> true
}
Expand Down Expand Up @@ -289,7 +312,7 @@ internal class MastodonTimelineStorePagingStoreImpl(
val decoder = MastodonLinkHeaderDecoder(response.headers()["link"])

// NOTE: 次のページネーションのIdが取得できない場合は次のIdが取得できるまで同じIdを使い回し続ける
when(val nextId = decoder.getMaxId()) {
when (val nextId = decoder.getMaxId()) {
null -> Unit
else -> {
maxId = nextId
Expand All @@ -309,7 +332,7 @@ internal class MastodonTimelineStorePagingStoreImpl(
val decoder = MastodonLinkHeaderDecoder(response.headers()["link"])

// NOTE: 次のページネーションのIdが取得できない場合は次のIdが取得できるまで同じIdを使い回し続ける
when(val nextId = decoder.getMinId()) {
when (val nextId = decoder.getMinId()) {
null -> Unit
else -> {
minId = nextId
Expand Down

0 comments on commit c5277dc

Please sign in to comment.