Skip to content

Commit 527c390

Browse files
committed
Insert FreshRSS items tags
1 parent cb1526a commit 527c390

File tree

8 files changed

+65
-10
lines changed

8 files changed

+65
-10
lines changed

api/src/main/java/com/readrops/api/services/greader/adapters/GReaderFoldersTagsAdapter.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ class GReaderFoldersTagsAdapter {
9595

9696
"tag" -> {
9797
tags += Tag(
98-
name = name
98+
name = name,
99+
remoteId = remoteId!!
99100
)
100101
}
101102
}

api/src/main/java/com/readrops/api/services/greader/adapters/GReaderItemsAdapter.kt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.readrops.api.utils.exceptions.ParseException
66
import com.readrops.api.utils.extensions.nextNonEmptyString
77
import com.readrops.api.utils.extensions.nextNullableString
88
import com.readrops.db.entities.Item
9+
import com.readrops.db.entities.Tag
910
import com.readrops.db.util.DateUtils
1011
import com.squareup.moshi.JsonAdapter
1112
import com.squareup.moshi.JsonReader
@@ -104,15 +105,31 @@ class GReaderItemsAdapter : JsonAdapter<List<Item>>() {
104105
}
105106

106107
private fun getStates(reader: JsonReader, item: Item) = with(reader) {
108+
val tags = mutableListOf<Tag>()
107109
beginArray()
108110

109111
while (hasNext()) {
110-
when (nextString()) {
111-
GOOGLE_READ -> item.isRead = true
112-
GOOGLE_STARRED -> item.isStarred = true
112+
val value = nextString()
113+
114+
with(value) {
115+
when {
116+
equals(GOOGLE_READ) -> item.isRead = true
117+
equals(GOOGLE_STARRED) -> item.isStarred = true
118+
// might also contain a folder, filtering is needed
119+
contains("user/-/label/") -> {
120+
val tag = Tag(
121+
name = value.removePrefix("user/-/label/"),
122+
remoteId = value
123+
)
124+
125+
tags += tag
126+
}
127+
}
113128
}
114129
}
115130

131+
item.tags = tags
132+
116133
endArray()
117134
}
118135

api/src/test/java/com/readrops/api/services/greader/adapters/GReaderItemsAdapterTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class GReaderItemsAdapterTest {
3232
assertEquals(pubDate, DateUtils.fromEpochSeconds(1625234040))
3333
assertEquals(isRead, false)
3434
assertEquals(isStarred, false)
35+
36+
assertEquals(1, tags.size)
37+
assertEquals("Libre", tags.first().name)
3538
}
3639

3740
with(items[1]) {

app/src/main/java/com/readrops/app/repositories/GReaderRepository.kt

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.readrops.db.entities.Folder
1212
import com.readrops.db.entities.Item
1313
import com.readrops.db.entities.ItemState
1414
import com.readrops.db.entities.Tag
15+
import com.readrops.db.entities.TagJoin
1516
import com.readrops.db.entities.account.Account
1617
import org.koin.core.component.KoinComponent
1718
import org.koin.core.component.get
@@ -70,11 +71,13 @@ class GReaderRepository(
7071
return dataSource.synchronize(syncType, syncData, account.writeToken!!).run {
7172
insertFolders(folders)
7273
val newFeeds = insertFeeds(feeds)
73-
insertTags(tags)
74+
val tags = insertTags(tags)
7475

7576
val newItems = insertItems(items, false)
7677
insertItems(starredItems, true)
7778

79+
insertItemsTags(newItems, tags)
80+
7881
insertItemsIds(unreadIds, readIds, starredIds.toMutableList())
7982

8083
account.lastModified = newLastModified
@@ -140,8 +143,9 @@ class GReaderRepository(
140143
database.folderDao().upsertFolders(folders, account)
141144
}
142145

143-
private suspend fun insertTags(tags: List<Tag>) {
146+
private suspend fun insertTags(tags: List<Tag>): List<Tag> {
144147
database.tagDao().insert(tags.map { it.copy(accountId = account.id) })
148+
return database.tagDao().selectAll(account.id)
145149
}
146150

147151
private suspend fun insertItems(items: List<Item>, starredItems: Boolean): List<Item> {
@@ -185,6 +189,24 @@ class GReaderRepository(
185189
return newItems
186190
}
187191

192+
private suspend fun insertItemsTags(newItems: List<Item>, allTags: List<Tag>) {
193+
newItems.associate { it to it.tags }
194+
.map { (item, tags) ->
195+
tags
196+
.filter { tag -> allTags.any { tag.remoteId == it.remoteId } }
197+
.map { tag ->
198+
TagJoin(
199+
itemId = item.id,
200+
tagId = allTags.first { tag.remoteId == it.remoteId }.id,
201+
)
202+
}
203+
}
204+
.flatten()
205+
.run {
206+
database.tagJoinDao().insert(this)
207+
}
208+
}
209+
188210
private suspend fun insertItemsIds(
189211
unreadIds: List<String>,
190212
readIds: List<String>,

db/schemas/com.readrops.db.Database/6.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"formatVersion": 1,
33
"database": {
44
"version": 6,
5-
"identityHash": "fccb174ca1fd4c5f42125779dd33197d",
5+
"identityHash": "93177df58b7d5e26f8cb1862c79834e1",
66
"entities": [
77
{
88
"tableName": "Feed",
@@ -390,7 +390,7 @@
390390
},
391391
{
392392
"tableName": "Tag",
393-
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `account_id` INTEGER NOT NULL, FOREIGN KEY(`account_id`) REFERENCES `Account`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )",
393+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `remote_id` TEXT NOT NULL, `account_id` INTEGER NOT NULL, FOREIGN KEY(`account_id`) REFERENCES `Account`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )",
394394
"fields": [
395395
{
396396
"fieldPath": "id",
@@ -404,6 +404,12 @@
404404
"affinity": "TEXT",
405405
"notNull": true
406406
},
407+
{
408+
"fieldPath": "remoteId",
409+
"columnName": "remote_id",
410+
"affinity": "TEXT",
411+
"notNull": true
412+
},
407413
{
408414
"fieldPath": "accountId",
409415
"columnName": "account_id",
@@ -645,7 +651,7 @@
645651
],
646652
"setupQueries": [
647653
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
648-
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'fccb174ca1fd4c5f42125779dd33197d')"
654+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '93177df58b7d5e26f8cb1862c79834e1')"
649655
]
650656
}
651657
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.readrops.db.dao
22

33
import androidx.room.Dao
4+
import androidx.room.Query
45
import com.readrops.db.entities.Tag
56

67
@Dao
78
interface TagDao : BaseDao<Tag> {
9+
10+
@Query("Select * From Tag Where account_id = :accountId")
11+
suspend fun selectAll(accountId: Int): List<Tag>
812
}

db/src/main/java/com/readrops/db/entities/Item.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ data class Item(
3333
@ColumnInfo(name = "starred") var isStarred: Boolean = false,
3434
@ColumnInfo(name = "remote_id") var remoteId: String? = null,
3535
@Ignore var feedRemoteId: String? = null,
36+
@Ignore var tags: List<Tag> = listOf()
3637
) : Comparable<Item> {
3738

3839
val text

db/src/main/java/com/readrops/db/entities/Tag.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.readrops.db.entities.account.Account
2323
data class Tag(
2424
@PrimaryKey(autoGenerate = true) val id: Int = 0,
2525
val name: String,
26+
@ColumnInfo(name = "remote_id") val remoteId: String,
2627
@ColumnInfo(name = "account_id", index = true) val accountId: Int = 0,
2728
)
2829

@@ -41,7 +42,7 @@ data class Tag(
4142
]
4243
)
4344
data class TagJoin(
44-
@PrimaryKey(autoGenerate = true) val id: Int,
45+
@PrimaryKey(autoGenerate = true) val id: Int = 0,
4546
@ColumnInfo(name = "tag_id", index = true) val tagId: Int,
4647
@ColumnInfo(name = "item_id", index = true) val itemId: Int
4748
)

0 commit comments

Comments
 (0)