-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: お気に入り一覧処理の取得処理をRepositoryに移動した
- Loading branch information
1 parent
5459cd2
commit 20c0cd2
Showing
5 changed files
with
192 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...stem/milktea/data/infrastructure/note/timeline/favorite/FavoriteTimelineRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package net.pantasystem.milktea.data.infrastructure.note.timeline.favorite | ||
|
||
import net.pantasystem.milktea.api.misskey.notes.NoteRequest | ||
import net.pantasystem.milktea.common.MastodonLinkHeaderDecoder | ||
import net.pantasystem.milktea.common.runCancellableCatching | ||
import net.pantasystem.milktea.common.throwIfHasError | ||
import net.pantasystem.milktea.data.api.mastodon.MastodonAPIProvider | ||
import net.pantasystem.milktea.data.api.misskey.MisskeyAPIProvider | ||
import net.pantasystem.milktea.data.infrastructure.note.NoteDataSourceAdder | ||
import net.pantasystem.milktea.model.account.Account | ||
import net.pantasystem.milktea.model.account.AccountRepository | ||
import net.pantasystem.milktea.model.note.timeline.favorite.FavoriteTimelineRepository | ||
import net.pantasystem.milktea.model.note.timeline.favorite.FavoriteTimelineResponse | ||
import javax.inject.Inject | ||
|
||
class FavoriteTimelineRepositoryImpl @Inject constructor( | ||
private val accountRepository: AccountRepository, | ||
private val misskeyAPIProvider: MisskeyAPIProvider, | ||
private val mastodonAPIProvider: MastodonAPIProvider, | ||
private val noteDataSourceAdder: NoteDataSourceAdder, | ||
): FavoriteTimelineRepository { | ||
override suspend fun findPreviousTimeline( | ||
accountId: Long, | ||
untilId: String?, | ||
untilDate: Long?, | ||
limit: Int | ||
): Result<FavoriteTimelineResponse> = runCancellableCatching { | ||
val account = accountRepository.get(accountId).getOrThrow() | ||
when(account.instanceType) { | ||
Account.InstanceType.MISSKEY, Account.InstanceType.FIREFISH -> { | ||
val res = misskeyAPIProvider.get(account).favorites( | ||
NoteRequest( | ||
i = account.token, | ||
limit = limit, | ||
untilId = untilId, | ||
untilDate = untilDate | ||
) | ||
).throwIfHasError() | ||
val nextSinceId = res.body()?.firstOrNull()?.id | ||
val nextUntilId = res.body()?.lastOrNull()?.id | ||
val ids = noteDataSourceAdder.addNoteDtoListToDataSource( | ||
account, | ||
requireNotNull(res.body()).map { it.note } | ||
) | ||
FavoriteTimelineResponse( | ||
timelineItems = ids, | ||
sinceId = nextSinceId, | ||
untilId = nextUntilId | ||
) | ||
} | ||
Account.InstanceType.MASTODON, Account.InstanceType.PLEROMA -> { | ||
val res = mastodonAPIProvider.get(account).getFavouriteStatuses( | ||
maxId = untilId, | ||
).throwIfHasError() | ||
val decoder = MastodonLinkHeaderDecoder(res.headers()["Link"]) | ||
val maxId = decoder.getMaxId() | ||
val minId = decoder.getMinId() | ||
val ids = noteDataSourceAdder.addTootStatusDtoListIntoDataSource( | ||
account, | ||
requireNotNull(res.body()) | ||
) | ||
FavoriteTimelineResponse( | ||
timelineItems = ids, | ||
sinceId = maxId, | ||
untilId = minId | ||
) | ||
} | ||
} | ||
} | ||
|
||
override suspend fun findLaterTimeline( | ||
accountId: Long, | ||
sinceId: String?, | ||
sinceDate: Long?, | ||
limit: Int | ||
): Result<FavoriteTimelineResponse> = runCancellableCatching { | ||
val account = accountRepository.get(accountId).getOrThrow() | ||
when(account.instanceType) { | ||
Account.InstanceType.MISSKEY, Account.InstanceType.FIREFISH -> { | ||
val res = misskeyAPIProvider.get(account).favorites( | ||
NoteRequest( | ||
i = account.token, | ||
limit = limit, | ||
sinceId = sinceId, | ||
sinceDate = sinceDate | ||
) | ||
).throwIfHasError() | ||
val nextSinceId = res.body()?.firstOrNull()?.id | ||
val nextUntilId = res.body()?.lastOrNull()?.id | ||
val ids = noteDataSourceAdder.addNoteDtoListToDataSource( | ||
account, | ||
requireNotNull(res.body()).map { it.note } | ||
) | ||
FavoriteTimelineResponse( | ||
timelineItems = ids, | ||
sinceId = nextSinceId, | ||
untilId = nextUntilId | ||
) | ||
} | ||
Account.InstanceType.MASTODON, Account.InstanceType.PLEROMA -> { | ||
val res = mastodonAPIProvider.get(account).getFavouriteStatuses( | ||
minId = sinceId, | ||
).throwIfHasError() | ||
val decoder = MastodonLinkHeaderDecoder(res.headers()["Link"]) | ||
val maxId = decoder.getMaxId() | ||
val minId = decoder.getMinId() | ||
val ids = noteDataSourceAdder.addTootStatusDtoListIntoDataSource( | ||
account, | ||
requireNotNull(res.body()) | ||
) | ||
FavoriteTimelineResponse( | ||
timelineItems = ids, | ||
sinceId = maxId, | ||
untilId = minId | ||
) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.