Move common third-party resolver to thirdparty pkg
resolveOrCreateCommonThirdParty lived as a package-level helper in the tracker mapping worker, but the common pattern enrichment worker now reuses it. Homing shared catalog logic in a mapping-named file made the enrichment worker quietly depend on the mapping worker's file, and it is not a mapping concern. Move it to pkg/thirdparty as exported ResolveOrCreateCommonThirdParty, decoupled from cookiebanner's TrackerMappingAgentResult (it now takes a name and category) to avoid an import cycle. It stays a transaction- scoped free function so both workers compose it into their own tx for atomicity rather than receiving a service that owns its own connection. Relocate the catalog dedup DB test alongside it. Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
@@ -27,7 +27,6 @@ import (
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/llm"
|
||||
"go.probo.inc/probo/pkg/slug"
|
||||
"go.probo.inc/probo/pkg/thirdparty"
|
||||
"go.probo.inc/probo/pkg/uri"
|
||||
)
|
||||
@@ -661,10 +660,12 @@ func (h *trackerMappingHandler) persistAgentIdentification(
|
||||
tp coredata.TrackerPattern,
|
||||
ident agentIdentification,
|
||||
) (*catalogMatch, error) {
|
||||
commonThirdPartyID, err := h.resolveOrCreateCommonThirdParty(
|
||||
commonThirdPartyID, err := thirdparty.ResolveOrCreateCommonThirdParty(
|
||||
ctx,
|
||||
tx,
|
||||
ident.result,
|
||||
h.logger,
|
||||
ident.result.ThirdPartyName,
|
||||
ident.result.Category,
|
||||
ident.domains,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -702,67 +703,6 @@ func (h *trackerMappingHandler) persistAgentIdentification(
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *trackerMappingHandler) resolveOrCreateCommonThirdParty(
|
||||
ctx context.Context,
|
||||
tx pg.Tx,
|
||||
identification TrackerMappingAgentResult,
|
||||
domains []string,
|
||||
) (*gid.GID, error) {
|
||||
var party coredata.CommonThirdParty
|
||||
if err := party.LoadByName(ctx, tx, identification.ThirdPartyName); err == nil {
|
||||
return &party.ID, nil
|
||||
}
|
||||
|
||||
partySlug := slug.Make(identification.ThirdPartyName)
|
||||
if partySlug == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if err := party.LoadBySlug(ctx, tx, partySlug); err == nil {
|
||||
return &party.ID, nil
|
||||
}
|
||||
|
||||
category := identification.Category
|
||||
|
||||
now := time.Now()
|
||||
party = coredata.CommonThirdParty{
|
||||
ID: gid.New(gid.NilTenant, coredata.CommonThirdPartyEntityType),
|
||||
Name: identification.ThirdPartyName,
|
||||
Slug: partySlug,
|
||||
Category: category,
|
||||
Certifications: []string{},
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
if err := party.Insert(ctx, tx); err != nil {
|
||||
return nil, fmt.Errorf("cannot create common third party: %w", err)
|
||||
}
|
||||
|
||||
for _, domain := range domains {
|
||||
domainRecord := coredata.CommonThirdPartyDomain{
|
||||
ID: gid.New(gid.NilTenant, coredata.CommonThirdPartyDomainEntityType),
|
||||
CommonThirdPartyID: party.ID,
|
||||
Domain: domain,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
if _, err := domainRecord.Upsert(ctx, tx); err != nil {
|
||||
return nil, fmt.Errorf("cannot create common third party domain: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
h.logger.InfoCtx(
|
||||
ctx,
|
||||
"created common third party from agent identification",
|
||||
log.String("name", identification.ThirdPartyName),
|
||||
log.String("category", category.String()),
|
||||
)
|
||||
|
||||
return &party.ID, nil
|
||||
}
|
||||
|
||||
// matchBySiblingOrigin finds other tracker patterns on the same banner
|
||||
// that share initiator domains with the current pattern. Sharing an
|
||||
// origin across multiple detected patterns is a strong indicator of the
|
||||
|
||||
Reference in New Issue
Block a user