Skip to content

Commit c6a2293

Browse files
authored
CMM-993 reader posts in discover are duplicated (#22366)
* Injecting duplicated fake posts * Checking post duplication * Removing debug code * Compile fix
1 parent f852a50 commit c6a2293

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

WordPress/src/main/java/org/wordpress/android/ui/reader/services/discover/ReaderDiscoverLogic.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ class ReaderDiscoverLogic @Inject constructor(
179179

180180
private fun parseCards(cardsJsonArray: JSONArray): ArrayList<ReaderDiscoverCard> {
181181
val cards: ArrayList<ReaderDiscoverCard> = arrayListOf()
182+
val addedPostIds = mutableSetOf<Long>()
183+
182184
for (i in 0 until cardsJsonArray.length()) {
183185
val cardJson = cardsJsonArray.getJSONObject(i)
184186
when (cardJson.getString(JSON_CARD_TYPE)) {
@@ -188,6 +190,15 @@ class ReaderDiscoverLogic @Inject constructor(
188190
}
189191
JSON_CARD_POST -> {
190192
val post = parseDiscoverCardsJsonUseCase.parsePostCard(cardJson)
193+
194+
// Check for duplicate post
195+
if (addedPostIds.contains(post.postId)) {
196+
AppLog.w(READER, "Duplicate post detected in parseCards - ID: ${post.postId}, " +
197+
"skipping duplicate entry")
198+
continue
199+
}
200+
201+
addedPostIds.add(post.postId)
191202
cards.add(ReaderPostCard(post))
192203
}
193204
JSON_CARD_RECOMMENDED_BLOGS -> {
@@ -219,9 +230,10 @@ class ReaderDiscoverLogic @Inject constructor(
219230
* It for example copies only ids from post object as we don't need to store the gigantic post in the json
220231
* as it's already stored in the db.
221232
*/
222-
@Suppress("NestedBlockDepth")
233+
@Suppress("NestedBlockDepth", "CyclomaticComplexMethod", "LoopWithTooManyJumpStatements")
223234
private fun createSimplifiedJson(cardsJsonArray: JSONArray, discoverTasks: DiscoverTasks): JSONArray {
224235
val simplifiedJsonList = mutableListOf<JSONObject>()
236+
val addedPostIds = mutableSetOf<Long>()
225237
var firstRecommendationCard: JSONObject? = null
226238
val isFirstPage = discoverTasks == REQUEST_FIRST_PAGE
227239
for (i in 0 until cardsJsonArray.length()) {
@@ -246,6 +258,17 @@ class ReaderDiscoverLogic @Inject constructor(
246258
simplifiedJsonList.add(cardJson)
247259
}
248260
JSON_CARD_POST -> {
261+
// Check for duplicate posts in simplified JSON
262+
val postData = cardJson.getJSONObject(JSON_CARD_DATA)
263+
val postId = postData.optLong(POST_ID)
264+
265+
if (addedPostIds.contains(postId)) {
266+
AppLog.w(READER, "Duplicate post detected in createSimplifiedJson - ID: $postId, " +
267+
"skipping duplicate entry")
268+
continue
269+
}
270+
271+
addedPostIds.add(postId)
249272
simplifiedJsonList.add(createSimplifiedPostJson(cardJson))
250273
}
251274
}

0 commit comments

Comments
 (0)