Skip to content

Commit

Permalink
Merge pull request #688 from planetary-social/fix-reposts-of-reposts
Browse files Browse the repository at this point in the history
Fix reposts
  • Loading branch information
mplorentz authored Dec 11, 2023
2 parents 58b8714 + 3860239 commit 16c498e
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added the option to copy the text content while browsing a note.
- Fixed UI bugs when displaying the root note of replies.
- Keep track of read stories.
- Fix an issue where reposts were not displaying correctly.

## [0.1 (94)] - 2023-11-17Z

Expand Down
51 changes: 11 additions & 40 deletions Nos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
C930055F2A6AF8320098CA9E /* LoadingContent.swift in Sources */ = {isa = PBXBuildFile; fileRef = C930055E2A6AF8320098CA9E /* LoadingContent.swift */; };
C93005602A6AF8320098CA9E /* LoadingContent.swift in Sources */ = {isa = PBXBuildFile; fileRef = C930055E2A6AF8320098CA9E /* LoadingContent.swift */; };
C931517D29B915AF00934506 /* StaggeredGrid.swift in Sources */ = {isa = PBXBuildFile; fileRef = C931517C29B915AF00934506 /* StaggeredGrid.swift */; };
C9332C632ADECFA700AD7B0E /* StarscreamOld in Frameworks */ = {isa = PBXBuildFile; productRef = C9332C622ADECFA700AD7B0E /* StarscreamOld */; };
C9332C662ADED0D700AD7B0E /* StarscreamOld in Frameworks */ = {isa = PBXBuildFile; productRef = C9332C652ADED0D700AD7B0E /* StarscreamOld */; };
C936B4592A4C7B7C00DF1EB9 /* Nos.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = C936B4572A4C7B7C00DF1EB9 /* Nos.xcdatamodeld */; };
C936B45A2A4C7B7C00DF1EB9 /* Nos.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = C936B4572A4C7B7C00DF1EB9 /* Nos.xcdatamodeld */; };
Expand All @@ -109,6 +110,7 @@
C94A5E162A716A6D00B6EC5D /* EditableNoteText.swift in Sources */ = {isa = PBXBuildFile; fileRef = C94A5E142A716A6D00B6EC5D /* EditableNoteText.swift */; };
C94A5E182A72C84200B6EC5D /* ReportCategory.swift in Sources */ = {isa = PBXBuildFile; fileRef = C94A5E172A72C84200B6EC5D /* ReportCategory.swift */; };
C94A5E192A72C84200B6EC5D /* ReportCategory.swift in Sources */ = {isa = PBXBuildFile; fileRef = C94A5E172A72C84200B6EC5D /* ReportCategory.swift */; };
C94B2D182B17F5EC002104B6 /* sample_repost.json in Resources */ = {isa = PBXBuildFile; fileRef = C94B2D172B17F5EC002104B6 /* sample_repost.json */; };
C94C4CF32AD993CA00F801CA /* UNSErrorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C94C4CF22AD993CA00F801CA /* UNSErrorView.swift */; };
C94D14812A12B3F70014C906 /* SearchBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = C94D14802A12B3F70014C906 /* SearchBar.swift */; };
C94D59AC2AE711A400295AE8 /* WalletConnectErrorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C94D59AB2AE711A400295AE8 /* WalletConnectErrorView.swift */; };
Expand Down Expand Up @@ -270,11 +272,6 @@
C9B678E229EEC41000303F33 /* SocialGraphCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9B678E029EEC41000303F33 /* SocialGraphCache.swift */; };
C9B678E429EED2DC00303F33 /* SocialGraphTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9B678E329EED2DC00303F33 /* SocialGraphTests.swift */; };
C9B678E729F01A8500303F33 /* FullscreenProgressView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9B678E629F01A8500303F33 /* FullscreenProgressView.swift */; };
C9B6AC412B14F07800C72EED /* Web3Wallet in Frameworks */ = {isa = PBXBuildFile; productRef = C9B6AC402B14F07800C72EED /* Web3Wallet */; };
C9B6AC432B14F07E00C72EED /* Web3 in Frameworks */ = {isa = PBXBuildFile; productRef = C9B6AC422B14F07E00C72EED /* Web3 */; };
C9B6AC452B14F08700C72EED /* WalletConnectModal in Frameworks */ = {isa = PBXBuildFile; productRef = C9B6AC442B14F08700C72EED /* WalletConnectModal */; };
C9B6AC472B14F1A200C72EED /* WalletConnect in Frameworks */ = {isa = PBXBuildFile; productRef = C9B6AC462B14F1A200C72EED /* WalletConnect */; };
C9B6AC492B14F1F400C72EED /* StarscreamOld in Frameworks */ = {isa = PBXBuildFile; productRef = C9B6AC482B14F1F400C72EED /* StarscreamOld */; };
C9B708BB2A13BE41006C613A /* NoteTextEditor.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9B708BA2A13BE41006C613A /* NoteTextEditor.swift */; };
C9B71DBE2A8E9BAD0031ED9F /* Sentry in Frameworks */ = {isa = PBXBuildFile; productRef = C9B71DBD2A8E9BAD0031ED9F /* Sentry */; };
C9B71DC02A8E9BAD0031ED9F /* SentrySwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = C9B71DBF2A8E9BAD0031ED9F /* SentrySwiftUI */; };
Expand Down Expand Up @@ -520,6 +517,7 @@
C94437E529B0DB83004D8C86 /* NotificationsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationsView.swift; sourceTree = "<group>"; };
C94A5E142A716A6D00B6EC5D /* EditableNoteText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditableNoteText.swift; sourceTree = "<group>"; };
C94A5E172A72C84200B6EC5D /* ReportCategory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReportCategory.swift; sourceTree = "<group>"; };
C94B2D172B17F5EC002104B6 /* sample_repost.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = sample_repost.json; sourceTree = "<group>"; };
C94BC09A2A0AC74A0098F6F1 /* PreviewData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreviewData.swift; sourceTree = "<group>"; };
C94C4CF22AD993CA00F801CA /* UNSErrorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UNSErrorView.swift; sourceTree = "<group>"; };
C94D14802A12B3F70014C906 /* SearchBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchBar.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -739,6 +737,7 @@
C97797BC298AB1890046BD25 /* secp256k1 in Frameworks */,
C9AA1BB42ABA3D5C00E8BD6D /* Web3Wallet in Frameworks */,
C9B71DBE2A8E9BAD0031ED9F /* Sentry in Frameworks */,
C9332C632ADECFA700AD7B0E /* StarscreamOld in Frameworks */,
C9646E9A29B79E04007239A4 /* Logger in Frameworks */,
C9646EA729B7A3DD007239A4 /* Dependencies in Frameworks */,
C9AA1BBC2ABB6E8900E8BD6D /* WalletConnectModal in Frameworks */,
Expand All @@ -754,16 +753,11 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
C9B6AC432B14F07E00C72EED /* Web3 in Frameworks */,
C99DBF802A9E8BCF00F7068F /* SDWebImageSwiftUI in Frameworks */,
C97797BF298ABE060046BD25 /* secp256k1 in Frameworks */,
C9B6AC412B14F07800C72EED /* Web3Wallet in Frameworks */,
CDDA1F7B29A527650047ACD8 /* Starscream in Frameworks */,
C9B71DC52A9008300031ED9F /* Sentry in Frameworks */,
C9B6AC452B14F08700C72EED /* WalletConnectModal in Frameworks */,
C9646EA929B7A4F2007239A4 /* PostHog in Frameworks */,
C9B6AC472B14F1A200C72EED /* WalletConnect in Frameworks */,
C9B6AC492B14F1F400C72EED /* StarscreamOld in Frameworks */,
C9646EAC29B7A520007239A4 /* Dependencies in Frameworks */,
C905B0752A619367009B8A78 /* DequeModule in Frameworks */,
C9646E9C29B79E4D007239A4 /* Logger in Frameworks */,
Expand Down Expand Up @@ -1037,6 +1031,7 @@
C9DEC0042989477A0078B43A /* Fixtures */ = {
isa = PBXGroup;
children = (
C94B2D172B17F5EC002104B6 /* sample_repost.json */,
CD27177529A7C8B200AE8888 /* sample_replies.json */,
C9DEC005298947900078B43A /* sample_data.json */,
C9ADB134299288230075E7F8 /* KeyFixture.swift */,
Expand Down Expand Up @@ -1308,6 +1303,7 @@
C9AA1BB32ABA3D5C00E8BD6D /* Web3Wallet */,
C9AA1BB92ABB62EB00E8BD6D /* Web3 */,
C9AA1BBB2ABB6E8900E8BD6D /* WalletConnectModal */,
C9332C622ADECFA700AD7B0E /* StarscreamOld */,
C9332C652ADED0D700AD7B0E /* StarscreamOld */,
);
productName = Nos;
Expand Down Expand Up @@ -1339,11 +1335,6 @@
C905B0742A619367009B8A78 /* DequeModule */,
C9B71DC42A9008300031ED9F /* Sentry */,
C99DBF7F2A9E8BCF00F7068F /* SDWebImageSwiftUI */,
C9B6AC402B14F07800C72EED /* Web3Wallet */,
C9B6AC422B14F07E00C72EED /* Web3 */,
C9B6AC442B14F08700C72EED /* WalletConnectModal */,
C9B6AC462B14F1A200C72EED /* WalletConnect */,
C9B6AC482B14F1F400C72EED /* StarscreamOld */,
);
productName = NosTests;
productReference = C9DEBFE4298941020078B43A /* NosTests.xctest */;
Expand Down Expand Up @@ -1505,6 +1496,7 @@
C987F85529BA951E00B44E7A /* ClarityCity-Thin.otf in Resources */,
C987F83F29BA951E00B44E7A /* ClarityCity-SemiBold.otf in Resources */,
C9DEC006298947900078B43A /* sample_data.json in Resources */,
C94B2D182B17F5EC002104B6 /* sample_repost.json in Resources */,
C987F84929BA951E00B44E7A /* ClarityCity-BlackItalic.otf in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -2496,6 +2488,10 @@
package = C96CB98A2A6040C500498C4E /* XCRemoteSwiftPackageReference "swift-collections" */;
productName = DequeModule;
};
C9332C622ADECFA700AD7B0E /* StarscreamOld */ = {
isa = XCSwiftPackageProductDependency;
productName = StarscreamOld;
};
C9332C652ADED0D700AD7B0E /* StarscreamOld */ = {
isa = XCSwiftPackageProductDependency;
productName = StarscreamOld;
Expand Down Expand Up @@ -2590,31 +2586,6 @@
package = C95F0AC82ABA379700A0D9CE /* XCRemoteSwiftPackageReference "WalletConnectSwiftV2" */;
productName = WalletConnectModal;
};
C9B6AC402B14F07800C72EED /* Web3Wallet */ = {
isa = XCSwiftPackageProductDependency;
package = C95F0AC82ABA379700A0D9CE /* XCRemoteSwiftPackageReference "WalletConnectSwiftV2" */;
productName = Web3Wallet;
};
C9B6AC422B14F07E00C72EED /* Web3 */ = {
isa = XCSwiftPackageProductDependency;
package = C9AA1BB82ABB62EB00E8BD6D /* XCRemoteSwiftPackageReference "Web3" */;
productName = Web3;
};
C9B6AC442B14F08700C72EED /* WalletConnectModal */ = {
isa = XCSwiftPackageProductDependency;
package = C95F0AC82ABA379700A0D9CE /* XCRemoteSwiftPackageReference "WalletConnectSwiftV2" */;
productName = WalletConnectModal;
};
C9B6AC462B14F1A200C72EED /* WalletConnect */ = {
isa = XCSwiftPackageProductDependency;
package = C95F0AC82ABA379700A0D9CE /* XCRemoteSwiftPackageReference "WalletConnectSwiftV2" */;
productName = WalletConnect;
};
C9B6AC482B14F1F400C72EED /* StarscreamOld */ = {
isa = XCSwiftPackageProductDependency;
package = C9332C642ADED0D700AD7B0E /* XCLocalSwiftPackageReference "StarscreamOld" */;
productName = StarscreamOld;
};
C9B71DBD2A8E9BAD0031ED9F /* Sentry */ = {
isa = XCSwiftPackageProductDependency;
package = C9B71DBC2A8E9BAD0031ED9F /* XCRemoteSwiftPackageReference "sentry-cocoa" */;
Expand Down
34 changes: 34 additions & 0 deletions Nos/Models/Event+CoreDataClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,10 @@ public class Event: NosManagedObject {
case .mute:
hydrateMuteList(from: jsonEvent, context: context)

case .repost:
hydrateDefault(from: jsonEvent, context: context)
parseContent(from: jsonEvent, context: context)

default:
hydrateDefault(from: jsonEvent, context: context)
}
Expand Down Expand Up @@ -832,6 +836,22 @@ public class Event: NosManagedObject {
}
}

/// Tries to parse a new event out of the given jsonEvent's `content` field.
@discardableResult
func parseContent(from jsonEvent: JSONEvent, context: NSManagedObjectContext) -> Event? {
do {
if let contentData = jsonEvent.content.data(using: .utf8) {
let jsonEvent = try JSONDecoder().decode(JSONEvent.self, from: contentData)
return try Event().createIfNecessary(jsonEvent: jsonEvent, relay: nil, context: context)
}
} catch {
Log.error("Could not parse content for jsonEvent: \(jsonEvent)")
return nil
}

return nil
}

// MARK: - Helpers

var serializedEventForSigning: [Any?] {
Expand Down Expand Up @@ -1080,6 +1100,20 @@ public class Event: NosManagedObject {
return nil
}

/// Returns the event this note is reposting, if this note is a kind 6 repost.
func repostedNote() -> Event? {
guard kind == EventKind.repost.rawValue else {
return nil
}

if let reference = eventReferences.firstObject as? EventReference,
let repostedNote = reference.referencedEvent {
return repostedNote
}

return nil
}

/// This tracks which relays this event is deleted on. Hide posts with deletedOn.count > 0
func trackDelete(on relay: Relay, context: NSManagedObjectContext) throws {
if EventKind(rawValue: kind) == .delete, let eTags = allTags as? [[String]] {
Expand Down
2 changes: 1 addition & 1 deletion Nos/Views/NoteButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct NoteButton: View {
/// The note displayed in the note card. Could be different from `note` i.e. in the case of a repost.
var displayedNote: Event {
if note.kind == EventKind.repost.rawValue,
let repostedNote = note.referencedNote() {
let repostedNote = note.repostedNote() {
return repostedNote
} else {
return note
Expand Down
2 changes: 1 addition & 1 deletion Nos/Views/StoryNoteView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ struct StoryNoteView: View {
if shouldShowSpacing {
Spacer(minLength: 85)
}
if note.kind == EventKind.repost.rawValue, let repostedNote = note.referencedNote() {
if note.kind == EventKind.repost.rawValue, let repostedNote = note.repostedNote() {
Button {
router.push(repostedNote)
} label: {
Expand Down
50 changes: 50 additions & 0 deletions NosTests/EventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ final class EventTests: XCTestCase {
XCTAssertEqual(replies?.count, 2)
}

func testParseRepost() throws {
// Arrange
let sampleData = try Data(contentsOf: Bundle.current.url(forResource: "sample_repost", withExtension: "json")!)
let sampleEventID = "f41e430f632b1e747da7efbb0ce11616876851e2fa3bbac440101c1b8a091152"
let repostedEventID = "f82507f7c770a39d0eabf276ced34fbd6a172be869bd3a3231c9c0272f405008"
let repostedEventContents = "#kraftwerk https://v.nostr.build/lx7e.mp4 "
let testContext = persistenceController.container.viewContext

// Act
let events = try EventProcessor.parse(jsonData: sampleData, from: nil, in: persistenceController)
let sampleEvent = try XCTUnwrap(events.first(where: { $0.identifier == sampleEventID }))

// Assert
XCTAssertEqual(sampleEvent.identifier, sampleEventID)
let repostedEvent = try XCTUnwrap(sampleEvent.repostedNote())
XCTAssertEqual(repostedEvent.identifier, repostedEventID)
XCTAssertEqual(repostedEvent.content, repostedEventContents)
}

func testSerializedEventForSigning() throws {
// Arrange
let testContext = persistenceController.container.viewContext
Expand Down Expand Up @@ -371,6 +390,37 @@ final class EventTests: XCTestCase {

XCTAssertNil(testEvent.referencedNote())
}

func testRepostedNote() throws {
let testContext = persistenceController.container.viewContext
let testEvent = try createTestEvent(in: testContext)
testEvent.kind = 6

let mention = try EventReference(
jsonTag: ["e", "646daa2f5d2d990dc98fb50a6ce8de65d77419cee689d7153c912175e85ca95d"],
context: testContext
)
testEvent.addToEventReferences(mention)

XCTAssertEqual(
testEvent.repostedNote()?.identifier,
"646daa2f5d2d990dc98fb50a6ce8de65d77419cee689d7153c912175e85ca95d"
)
}

func testRepostedNoteGivenNonRepost() throws {
let testContext = persistenceController.container.viewContext
let testEvent = try createTestEvent(in: testContext)
testEvent.kind = 1

let mention = try EventReference(
jsonTag: ["e", "646daa2f5d2d990dc98fb50a6ce8de65d77419cee689d7153c912175e85ca95d"],
context: testContext
)
testEvent.addToEventReferences(mention)

XCTAssertEqual(testEvent.repostedNote()?.identifier, nil)
}

// MARK: - Helpers

Expand Down
28 changes: 28 additions & 0 deletions NosTests/Fixtures/sample_repost.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[
{
"id" : "f41e430f632b1e747da7efbb0ce11616876851e2fa3bbac440101c1b8a091152",
"content" : "{\"sig\":\"c33f8ee08b47a862c159f09f75794369041b2d982b2e79762b9ed79fbb24e2801b9668b0bf60e7f8b01f9c514989e5e26ae2b205097f62b37aab838cb3783b8c\",\"pubkey\":\"76c71aae3a491f1d9eec47cba17e229cda4113a0bbb6e6ae1776d7643e29cafa\",\"kind\":1,\"id\":\"f82507f7c770a39d0eabf276ced34fbd6a172be869bd3a3231c9c0272f405008\",\"tags\":[[\"imeta\",\"url https:\\\/\\\/v.nostr.build\\\/lx7e.mp4\",\"blurhash eL2RZYkVVukUi|kBfjayfjfjQDaLoyafbtbXe:jbfQfRptkBV[f*jH\",\"dim 720x1280\"],[\"t\",\"kraftwerk\"],[\"r\",\"https:\\\/\\\/v.nostr.build\\\/lx7e.mp4\"]],\"content\":\"#kraftwerk https:\\\/\\\/v.nostr.build\\\/lx7e.mp4 \",\"created_at\":1701245356}",
"kind" : 6,
"tags" : [
[
"e",
"f82507f7c770a39d0eabf276ced34fbd6a172be869bd3a3231c9c0272f405008",
"wss:\/\/nostr.oxtr.dev",
"wss:\/\/nos.lol",
"wss:\/\/relay.damus.io",
"wss:\/\/relay.nostr.band",
"wss:\/\/relay.current.fyi",
"wss:\/\/relay.nostr.bg",
"wss:\/\/e.nos.lol",
"wss:\/\/nostr.fmt.wiz.biz"
],
[
"p",
"76c71aae3a491f1d9eec47cba17e229cda4113a0bbb6e6ae1776d7643e29cafa"
]
],
"sig" : "f9c1af62ad567c72a692a9881799724c4c94df0955c0ec1ae6322fc9f7468821390d5c45a73e544d79513ccc541d7f430fd4d29aa28426471f40895afcf46614",
"created_at" : 1701258681,
"pubkey" : "e731ca427c18059d66636ddfaeeeb15012bc2db3cdd27b9e4cade5057a6e82ed"
}
]

0 comments on commit 16c498e

Please sign in to comment.