@@ -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