Skip to content

Commit

Permalink
Merge branch 'release/1.84.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoy committed Apr 17, 2024
2 parents 14ddd31 + 3948b09 commit af10c2d
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ extension IdentityTheftRestorationPagesUserScript: WKScriptMessageHandler {
/// Use Subscription sub-feature
///
final class IdentityTheftRestorationPagesFeature: Subfeature {
var broker: UserScriptMessageBroker?
weak var broker: UserScriptMessageBroker?

var featureName = "useIdentityTheftRestoration"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ extension SubscriptionPagesUserScript: WKScriptMessageHandler {
/// Use Subscription sub-feature
///
final class SubscriptionPagesUseSubscriptionFeature: Subfeature {
var broker: UserScriptMessageBroker?
weak var broker: UserScriptMessageBroker?
var featureName = "useSubscription"
var messageOriginPolicy: MessageOriginPolicy = .only(rules: [
.exact(hostname: "duckduckgo.com"),
Expand Down Expand Up @@ -465,7 +465,11 @@ final class SubscriptionPagesUseSubscriptionFeature: Subfeature {
}

func pushAction(method: SubscribeActionName, webView: WKWebView, params: Encodable) {
let broker = UserScriptMessageBroker(context: SubscriptionPagesUserScript.context, requiresRunInPageContentWorld: true )
guard let broker else {
assertionFailure("Cannot continue without broker instance")
return
}

broker.push(method: method.rawValue, params: params, for: self, into: webView)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,19 @@ extension DataBrokerProtectionDatabase {
// The queries we need to create are the one that exist on the new ones but not in the database
let profileQueriesToCreate = Set(newProfileQueries).subtracting(Set(databaseProfileQueries))

// The queries that need update exist in both the new and the database
// We assume updated queries will be not deprecated
var profileQueriesToUpdate = Array(Set(databaseProfileQueries).intersection(Set(newProfileQueries))).map {
$0.with(deprecated: false)
}
// Updated profile queries. This is only for use for deprecated matches.
// We do not use it for updating a particular profile query. The reason is that
// updates do not exist because the UI returns a complete profile, and does not
// discriminate if a change was something new
//
// Examples:
// - If a user John Doe, Miami, FL. Changes its name to Jonathan, for us it will be a new profile query.
// and we should remove the John Doe one.
// - The same happens with addresses, if a user changes the address from Miami to Aventura. We want
// to delete all profile queries that have Miami, FL as an address, and add the new ones.
//
var profileQueriesToUpdate = [ProfileQuery]()

// The ones that we need to remove are the ones that exist in the database but not in the new ones
var profileQueriesToRemove = Set(databaseProfileQueries).subtracting(Set(newProfileQueries))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ extension ProfileQuery: Equatable {
return
lhs.firstName.lowercased() == rhs.firstName.lowercased() &&
lhs.lastName.lowercased() == rhs.lastName.lowercased() &&
lhs.middleName?.lowercased() == rhs.middleName?.lowercased() &&
lhs.middleName.normalized() == rhs.middleName.normalized() &&
lhs.suffix?.lowercased() == rhs.suffix?.lowercased() &&
lhs.city.lowercased() == rhs.city.lowercased() &&
lhs.state.lowercased() == rhs.state.lowercased() &&
Expand All @@ -94,6 +94,20 @@ extension ProfileQuery: Equatable {
}
}

extension Optional where Wrapped == String {

/// Returns a comparable string optional for profile query optional fields.
/// - Returns nil if the string is blank
/// - Returns nil when the value is nil, or the lowercased String if present
func normalized() -> String? {
guard let nonNilString = self else {
return nil
}

return nonNilString.isBlank ? nil : nonNilString.lowercased()
}
}

extension Address: Equatable {
static func == (lhs: Address, rhs: Address) -> Bool {
return lhs.city.lowercased() == rhs.city.lowercased() &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,13 @@ final class DefaultDataBrokerProtectionDatabaseProvider: GRDBSecureStorageDataba

func update(_ profileQuery: ProfileQueryDB) throws -> Int64 {
try db.write { db in
try profileQuery.upsert(db)
return db.lastInsertedRowID
if let id = profileQuery.id {
try profileQuery.update(db)
return id
} else {
try profileQuery.insert(db)
return db.lastInsertedRowID
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// ProfileQueryTests.swift
//
// Copyright © 2023 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import XCTest
import Foundation
@testable import DataBrokerProtection

final class ProfileQueryTests: XCTestCase {

func testWhenTwoProfileQueryHaveANilAndAnEmptyMiddleName_theyAreStillTheSameQuery() {
let profileQueryOne = ProfileQuery(firstName: "John", lastName: "Doe", middleName: nil, city: "Miami", state: "FL", birthYear: 1950)
let profileQueryTwo = ProfileQuery(firstName: "John", lastName: "Doe", middleName: "", city: "Miami", state: "FL", birthYear: 1950)

XCTAssertTrue(profileQueryOne == profileQueryTwo)
}

func testWhenTwoProfileQueryHaveANilAndABlankMiddleName_theyAreStillTheSameQuery() {
let profileQueryOne = ProfileQuery(firstName: "John", lastName: "Doe", middleName: nil, city: "Miami", state: "FL", birthYear: 1950)
let profileQueryTwo = ProfileQuery(firstName: "John", lastName: "Doe", middleName: " ", city: "Miami", state: "FL", birthYear: 1950)

XCTAssertTrue(profileQueryOne == profileQueryTwo)
}

func testWhenTwoProfileQueryHaveTheSameMiddleNames_theyAreTheSameQuery() {
let profileQueryOne = ProfileQuery(firstName: "John", lastName: "Doe", middleName: "M", city: "Miami", state: "FL", birthYear: 1950)
let profileQueryTwo = ProfileQuery(firstName: "John", lastName: "Doe", middleName: "M", city: "Miami", state: "FL", birthYear: 1950)

XCTAssertTrue(profileQueryOne == profileQueryTwo)
}

func testWhenTwoProfileQueryHaveDifferentMiddleNames_theyAreDifferentQueries() {
let profileQueryOne = ProfileQuery(firstName: "John", lastName: "Doe", middleName: "M.", city: "Miami", state: "FL", birthYear: 1950)
let profileQueryTwo = ProfileQuery(firstName: "John", lastName: "Doe", middleName: "J.", city: "Miami", state: "FL", birthYear: 1950)

XCTAssertFalse(profileQueryOne == profileQueryTwo)
}
}

0 comments on commit af10c2d

Please sign in to comment.