Skip to content

Commit

Permalink
Merge pull request #1639 from planetary-social/add-nos-tag-to-contact…
Browse files Browse the repository at this point in the history
…-list

Add a nos tag to published contact list events
  • Loading branch information
mplorentz authored Oct 16, 2024
2 parents ef2b8dc + 0331e64 commit 72744a0
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Release Notes
- Added a tip to the Feed to welcome first-time users and explain how the Feed works. [#1602](https://github.com/planetary-social/nos/issues/1602)
- Added a tag to published contact lists to help us detect the source of lost contact lists. [cleanstr#51](https://github.com/planetary-social/cleanstr/issues/51)
- Removed integration with Universal Name Space [#1636](https://github.com/planetary-social/nos/issues/1636)

### Internal Changes
Expand Down
5 changes: 5 additions & 0 deletions Nos/Models/JSONEvent+Kinds.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ extension JSONEvent {
/// - relays: Relays the user wishes to associate with their profile.
/// - Returns: The ``JSONEvent`` of the contact list.
static func contactList(pubKey: String, tags: [[String]], relayAddresses: [String]) -> JSONEvent {
var tags = tags
// Append client tag so we can detect if Nos overwrites a user's contact list.
// https://github.com/planetary-social/cleanstr/issues/51
tags.append(["client", "nos", "https://nos.social"])

let relayStrings = relayAddresses.map { "\"\($0)\":{\"write\":true,\"read\":true}" }
let content = "{" + relayStrings.joined(separator: ",") + "}"

Expand Down
12 changes: 12 additions & 0 deletions Nos/Service/Analytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ class Analytics {
"Discover Search Displayed Note"
)
}

/// Call this when publishing a new contact list for the user.
/// This is part of our solution to detect if Nos overwrites a user's contact list.
/// https://github.com/planetary-social/cleanstr/issues/51
func published(contactList: JSONEvent) {
let properties: [String: Any] = [
"date": contactList.createdAt,
"identifier": contactList.identifier ?? "null"
]

track("Published Contact List", properties: properties)
}

// MARK: - Relays

Expand Down
7 changes: 6 additions & 1 deletion Nos/Service/CurrentUser+PublishEvents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ extension CurrentUser {
let jsonEvent = JSONEvent.contactList(pubKey: pubKey, tags: tags, relayAddresses: relays)

do {
try await relayService.publishToAll(event: jsonEvent, signingKey: keyPair, context: viewContext)
let signedEvent = try await relayService.publishToAll(
event: jsonEvent,
signingKey: keyPair,
context: viewContext
)
analytics.published(contactList: signedEvent)
} catch {
Log.debug("failed to update Follows \(error.localizedDescription)")
}
Expand Down
2 changes: 1 addition & 1 deletion Nos/Service/CurrentUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Dependencies
// swiftlint:disable type_body_length
@Observable class CurrentUser: NSObject, NSFetchedResultsControllerDelegate {

@ObservationIgnored @Dependency(\.analytics) private var analytics
@ObservationIgnored @Dependency(\.analytics) var analytics
@ObservationIgnored @Dependency(\.crashReporting) private var crashReporting
@ObservationIgnored @Dependency(\.persistenceController) private var persistenceController
@ObservationIgnored @Dependency(\.pushNotificationService) private var pushNotificationService
Expand Down
8 changes: 7 additions & 1 deletion Nos/Service/Relay/RelayService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -631,11 +631,17 @@ extension RelayService {
})
}

func publishToAll(event: JSONEvent, signingKey: KeyPair, context: NSManagedObjectContext) async throws {
@discardableResult
func publishToAll(
event: JSONEvent,
signingKey: KeyPair,
context: NSManagedObjectContext
) async throws -> JSONEvent {
let signedEvent = try await signAndSave(event: event, signingKey: signingKey, in: context)
for socket in await subscriptionManager.sockets() {
try await publish(from: socket, jsonEvent: signedEvent)
}
return signedEvent
}

func publish(
Expand Down
6 changes: 4 additions & 2 deletions NosTests/Models/JSONEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class JSONEventTests: XCTestCase {
["p", "14aeb8dad4", "wss://bobrelay.com/nostr", "bob"],
["p", "612aee610f", "ws://carolrelay.com/ws", "carol"]
]
let expectedTags = pTags + [["client", "nos", "https://nos.social"]]
let relayAddresses = [
"wss://relay1.lol",
"wss://relay2.lol"
Expand All @@ -37,7 +38,7 @@ class JSONEventTests: XCTestCase {
"""

XCTAssertEqual(event.kind, 3)
XCTAssertEqual(event.tags, pTags)
XCTAssertEqual(event.tags, expectedTags)
XCTAssertEqual(event.content, expectedContent)
}

Expand All @@ -47,6 +48,7 @@ class JSONEventTests: XCTestCase {
["p", "14aeb8dad4", "wss://bobrelay.com/nostr", "bob"],
["p", "612aee610f", "ws://carolrelay.com/ws", "carol"]
]
let expectedTags = pTags + [["client", "nos", "https://nos.social"]]

let event = JSONEvent.contactList(
pubKey: "",
Expand All @@ -57,7 +59,7 @@ class JSONEventTests: XCTestCase {
let expectedContent = "{}"

XCTAssertEqual(event.kind, 3)
XCTAssertEqual(event.tags, pTags)
XCTAssertEqual(event.tags, expectedTags)
XCTAssertEqual(event.content, expectedContent)
}

Expand Down

0 comments on commit 72744a0

Please sign in to comment.