Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MOB-1930 - Show profile recommendations if only ens domain owned #479

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ extension WalletEntity: Identifiable {
var id: String { address }
}

extension WalletEntity {
enum WalletProfileState {
case noProfile, udDomain(DomainDisplayInfo), ensDomain(DomainDisplayInfo)
}

func getCurrentWalletProfileState() -> WalletProfileState {
if let rrDomain = rrDomain {
return .udDomain(rrDomain)
} else if let ensDomain = domains.first(where: { $0.isENSDomain }),
!isReverseResolutionChangeAllowed() {
return .ensDomain(ensDomain)
}
return .noProfile
}
}

extension Array where Element == WalletEntity {
func findWithAddress(_ address: HexAddress?) -> Element? {
guard let address = address?.normalized else { return nil }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,9 @@ private extension HomeExploreViewModel {
}

func loadSuggestedProfilesFor(wallet: WalletEntity) {
guard let rrDomain = getSelectedUserProfileRRDomain() else { return }
Task {
let suggestedProfiles = try await domainProfilesService.getSuggestionsFor(wallet: wallet)
let suggestedProfiles = try await domainProfilesService.getSuggestionsFor(domainName: rrDomain.name)
setSuggestedProfiles(suggestedProfiles)
}
}
Expand Down Expand Up @@ -288,7 +289,12 @@ private extension HomeExploreViewModel {
func getSelectedUserProfileRRDomain() -> DomainDisplayInfo? {
guard case .wallet(let wallet) = selectedProfile else { return nil }

return wallet.rrDomain
switch wallet.getCurrentWalletProfileState() {
case .udDomain(let domain), .ensDomain(let domain):
return domain
case .noProfile:
return nil
}
}

func loadNewProfileSuggestionsIfAllFollowing() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ extension HomeWalletView {
case .receive:
router.showingWalletInfo = selectedWallet
case .profile:
switch getCurrentWalletProfileState() {
switch selectedWallet.getCurrentWalletProfileState() {
case .udDomain(let domain), .ensDomain(let domain):
showProfile(of: domain)
case .noProfile:
Expand Down Expand Up @@ -142,7 +142,7 @@ extension HomeWalletView {
}

var isProfileButtonEnabled: Bool {
switch getCurrentWalletProfileState() {
switch selectedWallet.getCurrentWalletProfileState() {
case .udDomain, .ensDomain:
return true
case .noProfile:
Expand Down Expand Up @@ -310,19 +310,3 @@ fileprivate extension HomeWalletView.HomeWalletViewModel {
}
}
}

fileprivate extension HomeWalletView.HomeWalletViewModel {
enum WalletProfileState {
case noProfile, udDomain(DomainDisplayInfo), ensDomain(DomainDisplayInfo)
}

func getCurrentWalletProfileState() -> WalletProfileState {
if let rrDomain = selectedWallet.rrDomain {
return .udDomain(rrDomain)
} else if let ensDomain = selectedWallet.domains.first(where: { $0.isENSDomain }),
!selectedWallet.isReverseResolutionChangeAllowed() {
return .ensDomain(ensDomain)
}
return .noProfile
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,8 @@ extension DomainProfilesService: DomainProfilesServiceProtocol {
loadMoreSocialIfAbleFor(relationshipType: relationshipType, walletAddress: wallet.address)
}

func getSuggestionsFor(wallet: WalletEntity) async throws -> [DomainProfileSuggestion] {
guard let rrDomain = wallet.rrDomain else { return [] } // No suggestions for user without domain

let serializedSuggestions = try await networkService.getProfileSuggestions(for: rrDomain.name)
func getSuggestionsFor(domainName: DomainName) async throws -> [DomainProfileSuggestion] {
let serializedSuggestions = try await networkService.getProfileSuggestions(for: domainName)
let profileSuggestions = serializedSuggestions.map { DomainProfileSuggestion(serializedProfile: $0) }
return profileSuggestions
}
Expand Down Expand Up @@ -206,7 +204,12 @@ private extension DomainProfilesService {

func getProfileDomainNameFor(walletAddress: HexAddress) -> DomainName? {
let wallet = walletsDataService.wallets.findWithAddress(walletAddress)
return wallet?.profileDomainName
switch wallet?.getCurrentWalletProfileState() {
case .noProfile, nil:
return nil
case .udDomain(let domain), .ensDomain(let domain):
return domain.name
}
}

func getSerializedPublicDomainProfile(for domainName: DomainName) async throws -> SerializedPublicDomainProfile {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ protocol DomainProfilesServiceProtocol {
in wallet: WalletEntity)
func publisherForWalletDomainProfileDetails(wallet: WalletEntity) async -> CurrentValueSubject<WalletDomainProfileDetails, Never>

func getSuggestionsFor(wallet: WalletEntity) async throws -> [DomainProfileSuggestion]
func getSuggestionsFor(domainName: DomainName) async throws -> [DomainProfileSuggestion]
func getTrendingDomainNames() async throws -> [DomainName]
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,27 +207,21 @@ final class DomainProfilesServiceTests: BaseTestClass {
}

// MARK: - Profile Suggestions tests
func testEmptyProfileSuggestionsIfNoDomainInWallet() async throws {
let walletWithoutDomain = MockEntitiesFabric.Wallet.mockEntities(hasRRDomain: false).first!
let suggestions = try await service.getSuggestionsFor(wallet: walletWithoutDomain)

XCTAssertTrue(networkService.suggestionsCallDomainNames.isEmpty)
XCTAssertTrue(suggestions.isEmpty)
}

func testProfileSuggestionsReturnSuccess() async throws {
let wallet = mockWallet()
let suggestions = try await service.getSuggestionsFor(wallet: wallet)
let domainName = wallet.rrDomain!.name
let suggestions = try await service.getSuggestionsFor(domainName: domainName)

XCTAssertEqual(networkService.suggestionsCallDomainNames, [wallet.rrDomain!.name])
XCTAssertEqual(networkService.suggestionsCallDomainNames, [domainName])
XCTAssertEqual(suggestions.map { $0.domain }, networkService.suggestionToReturn.map { $0.domain })
}

func testProfileSuggestionsFails() async {
networkService.shouldFail = true
do {
let wallet = mockWallet()
let _ = try await service.getSuggestionsFor(wallet: wallet)
let domainName = wallet.rrDomain!.name
let _ = try await service.getSuggestionsFor(domainName: domainName)
XCTFail("Expected network error")
} catch {
assertNetworkErrorThrown(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ final class TestableDomainProfilesService: DomainProfilesServiceProtocol, Failab
loadMoreCallsHistory.append(relationshipType)
}

func getSuggestionsFor(wallet: WalletEntity) async throws -> [DomainProfileSuggestion] {
loadSuggestionsCallsHistory.append(wallet.address)
func getSuggestionsFor(domainName: DomainName) async throws -> [DomainProfileSuggestion] {
loadSuggestionsCallsHistory.append(domainName)
try failIfNeeded()
return profilesSuggestions
}
Expand Down