Add basic Fever synchronisation with feeds and folders insertion

This commit is contained in:
Shinokuni 2021-12-27 14:03:52 +01:00
parent 0885a9a3e4
commit 64b6fb0b4d
4 changed files with 75 additions and 17 deletions

View File

@ -5,6 +5,7 @@ import com.readrops.api.localfeed.LocalRSSDataSource
import com.readrops.api.services.Credentials import com.readrops.api.services.Credentials
import com.readrops.api.services.fever.FeverDataSource import com.readrops.api.services.fever.FeverDataSource
import com.readrops.api.services.fever.FeverService import com.readrops.api.services.fever.FeverService
import com.readrops.api.services.fever.adapters.*
import com.readrops.api.services.freshrss.FreshRSSDataSource import com.readrops.api.services.freshrss.FreshRSSDataSource
import com.readrops.api.services.freshrss.FreshRSSService import com.readrops.api.services.freshrss.FreshRSSService
import com.readrops.api.services.freshrss.adapters.* import com.readrops.api.services.freshrss.adapters.*
@ -98,9 +99,21 @@ val apiModule = module {
Retrofit.Builder() Retrofit.Builder()
.baseUrl(credentials.url) .baseUrl(credentials.url)
.client(get()) .client(get())
.addConverterFactory(MoshiConverterFactory.create(get(named("feverMoshi"))))
.build() .build()
.create(FeverService::class.java) .create(FeverService::class.java)
} }
single(named("feverMoshi")) {
Moshi.Builder()
.add(FeverFoldersAdapter())
.add(FeverFeedsAdapter())
.add(FeverItemsAdapter())
.add(FeverFaviconsAdapter())
.add(Boolean::class.java, FeverAPIAdapter())
.add(FeverItemsIdsAdapter())
.build()
}
//endregion Fever //endregion Fever
} }

View File

@ -1,8 +1,8 @@
package com.readrops.api.services.fever package com.readrops.api.services.fever
import com.readrops.api.services.SyncResult
import com.readrops.api.services.fever.adapters.FeverAPIAdapter import com.readrops.api.services.fever.adapters.FeverAPIAdapter
import com.readrops.api.utils.ApiUtils import com.readrops.api.utils.ApiUtils
import com.readrops.db.entities.Feed
import com.squareup.moshi.Moshi import com.squareup.moshi.Moshi
import okhttp3.MultipartBody import okhttp3.MultipartBody
@ -26,5 +26,13 @@ class FeverDataSource(private val service: FeverService) {
adapter.fromJson(response.source())!! adapter.fromJson(response.source())!!
} }
suspend fun getFeeds(): List<Feed> = service.getFeeds() suspend fun sync(body: MultipartBody): SyncResult {
return SyncResult(
feeds = service.getFeeds(body),
folders = service.getFolders(body),
items = service.getItems(body),
unreadIds = service.getUnreadItemsIds(body),
starredIds = service.getStarredItemsIds(body),
)
}
} }

View File

@ -1,5 +1,6 @@
package com.readrops.api.services.fever package com.readrops.api.services.fever
import com.readrops.api.services.fever.adapters.Favicon
import com.readrops.db.entities.Feed import com.readrops.db.entities.Feed
import com.readrops.db.entities.Folder import com.readrops.db.entities.Folder
import com.readrops.db.entities.Item import com.readrops.db.entities.Item
@ -15,27 +16,20 @@ interface FeverService {
suspend fun login(@Body body: MultipartBody): ResponseBody suspend fun login(@Body body: MultipartBody): ResponseBody
@POST("?feeds") @POST("?feeds")
suspend fun getFeeds(): List<Feed> suspend fun getFeeds(@Body body: MultipartBody): List<Feed>
@get:POST("?groups") @POST("?groups")
val folders: List<Folder> suspend fun getFolders(@Body body: MultipartBody): List<Folder>
@get:GET("?favicons") @POST("?favicons")
val favicons: Unit suspend fun getFavicons(@Body body: MultipartBody): List<Favicon>
@POST("?items") @POST("?items")
suspend fun items(): List<Item> suspend fun getItems(@Body body: MultipartBody): List<Item>
@POST("?unread_item_ids") @POST("?unread_item_ids")
suspend fun unreadItemsIds(): List<String> suspend fun getUnreadItemsIds(@Body body: MultipartBody): List<String>
@POST("?saved_item_ids") @POST("?saved_item_ids")
suspend fun starredItemsIds() suspend fun getStarredItemsIds(@Body body: MultipartBody): List<String>
@get:GET("?api")
val api: Unit
companion object {
const val END_POINT = ""
}
} }

View File

@ -1,17 +1,22 @@
package com.readrops.app.repositories package com.readrops.app.repositories
import android.content.Context import android.content.Context
import com.readrops.api.services.SyncType
import com.readrops.api.services.fever.FeverDataSource import com.readrops.api.services.fever.FeverDataSource
import com.readrops.api.utils.ApiUtils
import com.readrops.app.addfeed.FeedInsertionResult import com.readrops.app.addfeed.FeedInsertionResult
import com.readrops.app.addfeed.ParsingResult import com.readrops.app.addfeed.ParsingResult
import com.readrops.db.Database import com.readrops.db.Database
import com.readrops.db.entities.Feed import com.readrops.db.entities.Feed
import com.readrops.db.entities.Folder
import com.readrops.db.entities.Item
import com.readrops.db.entities.account.Account import com.readrops.db.entities.account.Account
import io.reactivex.Completable import io.reactivex.Completable
import io.reactivex.Single import io.reactivex.Single
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.rx2.await import kotlinx.coroutines.rx2.await
import kotlinx.coroutines.rx2.rxCompletable import kotlinx.coroutines.rx2.rxCompletable
import okhttp3.MultipartBody
class FeverRepository( class FeverRepository(
private val feverDataSource: FeverDataSource, private val feverDataSource: FeverDataSource,
@ -38,11 +43,49 @@ class FeverRepository(
override fun sync(feeds: List<Feed>?, update: FeedUpdate?): Completable { override fun sync(feeds: List<Feed>?, update: FeedUpdate?): Completable {
return rxCompletable(context = dispatcher) { return rxCompletable(context = dispatcher) {
try {
val syncType = if (account.lastModified != 0L) {
SyncType.CLASSIC_SYNC
} else {
SyncType.INITIAL_SYNC
}
val credentials = ApiUtils.md5hash("${account.login}:${account.password}")
val requestBody = MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("api_key", credentials)
.build()
val syncResult = feverDataSource.sync(requestBody)
insertFolders(syncResult.folders)
insertFeeds(syncResult.feeds)
} catch (e: Exception) {
error(e.message!!)
}
} }
} }
override fun addFeeds(results: List<ParsingResult>?): Single<List<FeedInsertionResult>> { override fun addFeeds(results: List<ParsingResult>?): Single<List<FeedInsertionResult>> {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
private fun insertFolders(folders: List<Folder>) {
folders.forEach { it.accountId = account.id }
database.folderDao().foldersUpsert(folders, account)
}
private fun insertFeeds(feeds: List<Feed>) {
feeds.forEach { it.accountId = account.id }
database.feedDao().feedsUpsert(feeds, account)
}
private fun insertItems(items: List<Item>) {
}
private fun insertItemsIds(unreadIds: List<String>, starredIds: List<String>) {
}
} }