Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Constrain contract on identifyUser to take Petname? instead of Peer?
Browse files Browse the repository at this point in the history
Also expand test assertions
  • Loading branch information
bfollington committed Oct 11, 2023
1 parent 80cbd87 commit 938703f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
16 changes: 8 additions & 8 deletions xcode/Subconscious/Shared/Services/UserProfileService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ actor UserProfileService {
let entries = try await localAddressBook.listEntries()

for entry in entries {
let user = try await self.identifyUser(entry: entry, context: address.peer)
let user = try await self.identifyUser(entry: entry, context: address.petname)
following.append(
StoryUser(entry: entry, user: user)
)
Expand Down Expand Up @@ -361,7 +361,7 @@ actor UserProfileService {
/// `context` will be used to determine the preferred navigation address for a user.
func identifyUser(
entry: AddressBookEntry,
context: Peer?
context: Petname?
) async throws -> UserProfile {
return try await self.identifyUser(
did: entry.did,
Expand All @@ -378,7 +378,7 @@ actor UserProfileService {
func identifyUser(
did: Did,
address: Slashlink,
context: Peer?
context: Petname?
) async throws -> UserProfile {
switch address.peer {
case .petname(let petname):
Expand All @@ -396,7 +396,7 @@ actor UserProfileService {
func identifyUser(
did: Did,
petname: Petname?,
context: Peer?
context: Petname?
) async throws -> UserProfile {
// Special case: our profile
let identity = try await self.noosphere.identity()
Expand All @@ -414,12 +414,12 @@ actor UserProfileService {
// If this is us, chop off the petname altogether
let address = Func.run {
switch (petname, context) {
case let (.some(petname), .some(peer)):
return Slashlink(petname: petname).rebaseIfNeeded(peer: peer)
case let (.some(petname), .some(context)):
return Slashlink(petname: petname).rebaseIfNeeded(peer: .petname(context))
case let (.some(petname), .none):
return Slashlink(petname: petname)
case (.none, .some(let peer)):
return Slashlink.ourProfile.rebaseIfNeeded(peer: peer)
case (.none, .some(let context)):
return Slashlink.ourProfile.rebaseIfNeeded(peer: .petname(context))
case (.none, .none):
return Slashlink.ourProfile
}
Expand Down
32 changes: 14 additions & 18 deletions xcode/Subconscious/SubconsciousTests/Tests_UserProfileService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class Tests_UserProfileService: XCTestCase {
let tmp = try TestUtilities.createTmpDir()
let environment = try await TestUtilities.createDataServiceEnvironment(tmp: tmp)

let firstPass = {
let createPeerSphere = {
let receipt = try await environment.noosphere.createSphere(ownerKeyName: "test")
let did = Did(receipt.identity)!

Expand All @@ -56,7 +56,7 @@ final class Tests_UserProfileService: XCTestCase {
return did
}
// In a closure to avoid polluting lexical scope
let did = try await firstPass()
let peerDid = try await createPeerSphere()

let receipt = try await environment.noosphere.createSphere(ownerKeyName: "test")
let ourDid = Did(receipt.identity)!
Expand All @@ -70,37 +70,33 @@ final class Tests_UserProfileService: XCTestCase {
)
)

try await environment.addressBook.followUser(did: peerDid, petname: Petname("finn-the-human")!)

let _ = try await environment.data.indexOurSphere()

let ourProfile = try await environment.userProfile.identifyUser(
did: ourDid,
address: Slashlink(petname: Petname("more.path")!),
context: .petname(Petname("some.long.path")!)
context: Petname("some.long.path")!
)

// Our profile should not be rebased using context
// Our profile should use the shortest path
XCTAssertEqual(ourProfile.did, ourDid)
XCTAssertEqual(ourProfile.address, Slashlink.ourProfile)

// Someone we know SHOULD be rebased
// Deliberately give this user a different address to our known name for them
let profile = try await environment.userProfile.identifyUser(
did: did,
did: peerDid,
address: Slashlink(petname: Petname("more.path")!),
context: .petname(Petname("some.long.path")!)
context: Petname("some.long.path")!
)

XCTAssertEqual(profile.did, did)
// Someone we know SHOULD be rebased
XCTAssertEqual(profile.did, peerDid)
XCTAssertEqual(profile.address, Slashlink(petname: Petname("more.path.some.long.path")!))

// Do not rebase on a DID path
let profileB = try await environment.userProfile.identifyUser(
did: did,
address: Slashlink(petname: Petname("more.path")!),
context: .did(ourDid)
)

XCTAssertEqual(profileB.did, did)
XCTAssertEqual(profileB.address, Slashlink(petname: Petname("more.path")!))
XCTAssertEqual(profile.aliases.count, 2)
XCTAssertTrue(profile.aliases.contains(where: { petname in petname == Petname("finn-the-human")! }))
XCTAssertTrue(profile.aliases.contains(where: { petname in petname == Petname("more.path") }))
}

func testIdentifyUserSignatures() async throws {
Expand Down

0 comments on commit 938703f

Please sign in to comment.