Skip to content

Commit c343149

Browse files
committed
Fix an issue with posts duplicated in Discover
1 parent 3319f60 commit c343149

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

RELEASE-NOTES.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
26.6
22
-----
3-
* [*] Fix horizontal insets in Reader article view [#25010]
43

54

65
26.5
@@ -11,13 +10,15 @@
1110
* [*] Add "Access" section to "Post Settings" [#24942]
1211
* [*] Add "Discussion" to "Post Settings" [#24948]
1312
* [*] Add "File Size" to Site Media Details [#24947]
13+
* [*] Fix an issue with posts duplicated in Discover [#25015]
1414
* [*] Add "Email to Subscribers" row to "Publishing" sheet [#24946]
1515
* [*] Add permalink preview in the slug editor and make other improvements [#24949]
1616
* [*] Add two accessible font sizes to Reader display settings [#25013]
1717
* [*] Add "Taxonomies" to Site Settings [#24955]
1818
* [*] Update "Categories" picker to indicate multiple selection [#24952]
1919
* [*] Fix overly long related post titles in Reader [#25011]
2020
* [*] Increase number of lines for post tiles in Reader to three [#25019]
21+
* [*] Fix horizontal insets in Reader article view [#25010]
2122

2223
26.4
2324
-----

Sources/WordPressData/Swift/ReaderCard+CoreDataClass.swift

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,47 @@ public class ReaderCard: NSManagedObject {
4343
sites?.array as? [ReaderSiteTopic] ?? []
4444
}
4545

46-
public convenience init?(context: NSManagedObjectContext, from remoteCard: RemoteReaderCard) {
46+
public static func createOrReuse(context: NSManagedObjectContext, from remoteCard: RemoteReaderCard) -> ReaderCard? {
4747
guard remoteCard.type != .unknown else {
4848
return nil
4949
}
5050

51-
self.init(context: context)
52-
5351
switch remoteCard.type {
5452
case .post:
55-
post = ReaderPost.createOrReplace(fromRemotePost: remoteCard.post, for: nil, context: context)
53+
let post = ReaderPost.createOrReplace(fromRemotePost: remoteCard.post, for: nil, context: context)
54+
55+
// Check if a card already exists with this post to prevent duplicates
56+
if let existingCard = findExistingCard(with: post, context: context) {
57+
return existingCard
58+
}
59+
60+
let card = ReaderCard(context: context)
61+
card.post = post
62+
return card
63+
5664
case .interests:
57-
return nil // Disabled in v26.6
65+
return nil // Disabled in v26.5
5866
case .sites:
59-
sites = NSOrderedSet(array: remoteCard.sites?.prefix(3).map {
67+
let card = ReaderCard(context: context)
68+
card.sites = NSOrderedSet(array: remoteCard.sites?.prefix(3).map {
6069
ReaderSiteTopic.createIfNeeded(from: $0, context: context)
6170
} ?? [])
71+
return card
6272

6373
default:
64-
break
74+
return nil
6575
}
6676
}
77+
78+
private static func findExistingCard(with post: ReaderPost?, context: NSManagedObjectContext) -> ReaderCard? {
79+
guard let post else {
80+
return nil
81+
}
82+
83+
let fetchRequest = ReaderCard.fetchRequest()
84+
fetchRequest.predicate = NSPredicate(format: "post = %@", post)
85+
fetchRequest.fetchLimit = 1
86+
87+
return try? context.fetch(fetchRequest).first
88+
}
6789
}

WordPress/Classes/Services/ReaderCardService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class ReaderCardService {
8181
}
8282

8383
updatedCards.enumerated().forEach { index, remoteCard in
84-
let card = ReaderCard(context: context, from: remoteCard)
84+
let card = ReaderCard.createOrReuse(context: context, from: remoteCard)
8585

8686
// Assign each interest an endpoint
8787
card?

0 commit comments

Comments
 (0)