Skip to content

Commit 50b482f

Browse files
authored
PIR: Implement getDataBrokers message (#6613)
Task/Issue URL: https://app.asana.com/1/137249556945/project/488551667048375/task/1211088335699800?focus=true ### Description Adds proper implementation of the getDataBrokers message for PIR Web UI. ### Steps to test this PR https://app.asana.com/1/137249556945/task/1211095110950029?focus=true ### UI changes No UI changes
1 parent bb17bb4 commit 50b482f

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

pir/pir-impl/src/main/java/com/duckduckgo/pir/impl/dashboard/messaging/handlers/PirWebGetDataBrokersMessageHandler.kt

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,37 @@ class PirWebGetDataBrokersMessageHandler @Inject constructor(
5555
logcat { "PIR-WEB: PirWebGetDataBrokersMessageHandler: process $jsMessage" }
5656

5757
appCoroutineScope.launch(dispatcherProvider.io()) {
58-
val brokers = repository.getAllActiveBrokerObjects()
59-
6058
jsMessaging.sendResponse(
6159
jsMessage,
6260
response = PirWebMessageResponse.GetDataBrokersResponse(
63-
dataBrokers = brokers.map {
64-
PirWebMessageResponse.GetDataBrokersResponse.DataBroker(
65-
url = it.url,
66-
name = it.name,
67-
parentURL = it.parent,
68-
)
69-
},
61+
dataBrokers = getDataBrokers(),
7062
),
7163
)
7264
}
7365
}
66+
67+
private suspend fun getDataBrokers(): List<PirWebMessageResponse.GetDataBrokersResponse.DataBroker> {
68+
val activeBrokers = repository.getAllActiveBrokerObjects().associateBy { it.name }
69+
val mirrorSites = repository.getAllMirrorSites().filter { it.removedAt == 0L }
70+
val brokerOptOutUrls = repository.getAllBrokerOptOutUrls()
71+
72+
val mappedBrokers = activeBrokers.values.map {
73+
PirWebMessageResponse.GetDataBrokersResponse.DataBroker(
74+
url = it.url,
75+
name = it.name,
76+
parentURL = it.parent,
77+
optOutUrl = brokerOptOutUrls[it.name],
78+
)
79+
}
80+
val mappedMirrorSites = mirrorSites.map {
81+
PirWebMessageResponse.GetDataBrokersResponse.DataBroker(
82+
url = it.url,
83+
name = it.name,
84+
parentURL = activeBrokers[it.parentSite]?.url,
85+
optOutUrl = brokerOptOutUrls[it.parentSite],
86+
)
87+
}
88+
89+
return (mappedBrokers + mappedMirrorSites).distinct()
90+
}
7491
}

pir/pir-impl/src/main/java/com/duckduckgo/pir/impl/store/PirRepository.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ interface PirRepository {
6666

6767
suspend fun getAllBrokersForScan(): List<String>
6868

69+
/**
70+
* Returns a map of broker names to their opt-out URLs.
71+
*/
72+
suspend fun getAllBrokerOptOutUrls(): Map<String, String?>
73+
6974
suspend fun getEtagForFilename(fileName: String): String?
7075

7176
suspend fun updateBrokerData(
@@ -234,6 +239,12 @@ internal class RealPirRepository(
234239
return@withContext brokerDao.getAllBrokersNamesWithScanSteps()
235240
}
236241

242+
override suspend fun getAllBrokerOptOutUrls(): Map<String, String?> =
243+
withContext(dispatcherProvider.io()) {
244+
val brokerOptOuts = brokerDao.getAllBrokerOptOuts()
245+
return@withContext brokerOptOuts.associate { it.brokerName to it.optOutUrl }
246+
}
247+
237248
override suspend fun getEtagForFilename(fileName: String): String =
238249
withContext(dispatcherProvider.io()) {
239250
return@withContext brokerJsonDao.getEtag(fileName)

pir/pir-impl/src/main/java/com/duckduckgo/pir/impl/store/db/BrokerDao.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ interface BrokerDao {
4848
@Query("DELETE from pir_broker_details where name = :brokerName")
4949
fun deleteBroker(brokerName: String)
5050

51+
@Query("SELECT * FROM pir_broker_opt_out")
52+
fun getAllBrokerOptOuts(): List<BrokerOptOut>
53+
5154
@Query("SELECT * from pir_broker_mirror_sites")
5255
fun getAllMirrorSites(): List<MirrorSiteEntity>
5356

0 commit comments

Comments
 (0)