Skip to content

Commit

Permalink
Allow overwriting of an existing feed item with the same url only the…
Browse files Browse the repository at this point in the history
… replacement is considered better because it has a date.
  • Loading branch information
tonytw1 committed May 12, 2023
1 parent 73b2b46 commit 119b359
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,22 @@ class FeedItemDAO @Autowired constructor(private val dataStoreFactory: DataStore

fun add(feedItem: FeedItem): Boolean {
return try {
val existingFeeditemFromSameSubscription = dataStoreFactory.get().find(FeedItem::class.java).filter(Filters.eq("url", feedItem.url), Filters.eq(SUBSCRIPTION_ID, feedItem.subscriptionId))
if (existingFeeditemFromSameSubscription.iterator().toList().isEmpty()) { // TODO shouldn't need to read before every write - use an upsert?
val existingSubscriptionFeeditemsWithSameUrl = dataStoreFactory.get().find(FeedItem::class.java).filter(Filters.eq("url", feedItem.url), Filters.eq(SUBSCRIPTION_ID, feedItem.subscriptionId))
val existing = existingSubscriptionFeeditemsWithSameUrl.first()
val isNewItem = existing == null
val replacesItemWithNoDate = feedItem.date != null && existing != null && existing.date == null
val shouldSave = isNewItem || replacesItemWithNoDate
// We allow overwriting of the existing feed item with the same url only if
// the replacement is considered better because it has a date
log.info("Deciding if to save: " + feedItem + " / " + existing + " / " + shouldSave)
if (shouldSave) {
if (existing != null) {
feedItem.objectId = existing.objectId
}
log.info("Added: " + feedItem.subscriptionId + ", " + feedItem.title)
dataStoreFactory.get().save(feedItem)
true

} else {
log.debug("Skipping previously added: " + feedItem.title)
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,8 @@ class FeedItem : RssFeedable {
)
}

override fun toString(): String {
return "FeedItem(objectId=$objectId, title=$title, url='$url', body=$body, date=$date, accepted=$accepted, place=$place, imageUrl=$imageUrl, subscriptionId='$subscriptionId', channelId='$channelId', author=$author, _categories=$_categories)"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,50 @@ class FeedItemDAOTest {
assertEquals(listOf("consultations", "news"), reloadedCategories?.map{it -> it.value})
}

@Test
fun canOverwriteUndatedFeedItems() {
val channel = Channel()
channel.id = UUID.randomUUID().toString()
val subscription = testSubscription(channel)
subscriptionsDAO.add(subscription)

val url = "http://localhost/" + UUID.randomUUID().toString()
val feedItem = FeedItem(
"Without date",
url,
null,
null,
null,
null,
null,
null,
subscription.id,
subscription.channelId,
null,
)
feedItemDAO.add(feedItem)

val betterFeedItem = FeedItem(
"With date",
url,
null,
DateTime.now().toDate(),
null,
null,
null,
null,
subscription.id,
subscription.channelId,
null,
)
feedItemDAO.add(betterFeedItem)

val feedItems = feedItemDAO.getSubscriptionFeedItems(subscription, 1)

assertEquals(1, feedItems.totalCount)
assertEquals("With date", feedItems.feedsItems.get(0).title)
}

@Test
fun canFetchChannelFeedItems() {
val channel = Channel()
Expand Down

0 comments on commit 119b359

Please sign in to comment.