Skip to content

Commit

Permalink
feat(api): allowing some credentials to override others (#754)
Browse files Browse the repository at this point in the history
* wip

* feat(api): allowing some credentials to override others
  • Loading branch information
lucianHymer authored Dec 17, 2024
1 parent 2409fdb commit ea0a02b
Showing 1 changed file with 48 additions and 6 deletions.
54 changes: 48 additions & 6 deletions api/registry/atasks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import copy
from datetime import datetime, timezone
from decimal import Decimal
from typing import Dict
from typing import Dict, List, Set, TypedDict

from django.conf import settings
from ninja_extra.exceptions import APIException
Expand Down Expand Up @@ -119,9 +119,11 @@ async def acalculate_score(
stamps[stamp_name] = {
"score": f"{Decimal(stamp_score):.5f}",
"dedup": matching_stamp is not None,
"expiration_date": matching_stamp["credential"]["expirationDate"]
if matching_stamp
else scoreData.stamp_expiration_dates[stamp_name].isoformat(),
"expiration_date": (
matching_stamp["credential"]["expirationDate"]
if matching_stamp
else scoreData.stamp_expiration_dates[stamp_name].isoformat()
),
}
# Add stamps present in clashing_stamps but not in stamp_scores
for c_povider, c_stamp in clashing_stamps.items():
Expand Down Expand Up @@ -240,6 +242,45 @@ async def asave_stamps(passport: Passport, deduped_passport_data) -> None:
)


class StampOverride(TypedDict):
preferred: str
overridden: str


STAMP_OVERRIDES: List[StampOverride] = [
{
"preferred": "CoinbaseDualVerification",
"overridden": "CoinbaseDualVerification2",
},
{"preferred": "BinanceBABT", "overridden": "BinanceBABT2"},
]

# Quick lookup map
OVERRIDDEN_TO_PREFERRED_PROVIDER = {
override["overridden"]: override["preferred"] for override in STAMP_OVERRIDES
}


# Overridden if the provider is listed as a overridden provider and
# the preferred provider is in the current list of all
# providers for this user
def is_overridden(provider: str, all_user_providers: Set[str]) -> bool:
return (
provider in OVERRIDDEN_TO_PREFERRED_PROVIDER
and OVERRIDDEN_TO_PREFERRED_PROVIDER[provider] in all_user_providers
)


def filter_stamps(passport_data):
providers = set([stamp["provider"] for stamp in passport_data["stamps"]])
new_stamps = [
stamp
for stamp in passport_data["stamps"]
if not is_overridden(stamp["provider"], providers)
]
return {**passport_data, "stamps": new_stamps}


async def ascore_passport(
community: Community, passport: Passport, address: str, score: Score
):
Expand All @@ -255,8 +296,9 @@ async def ascore_passport(
(deduped_passport_data, clashing_stamps) = await aprocess_deduplication(
passport, community, validated_passport_data, score
)
await asave_stamps(passport, deduped_passport_data)
await aremove_stale_stamps_from_db(passport, deduped_passport_data)
filtered_passport_data = filter_stamps(deduped_passport_data)
await asave_stamps(passport, filtered_passport_data)
await aremove_stale_stamps_from_db(passport, filtered_passport_data)
await acalculate_score(passport, community.pk, score, clashing_stamps)

except APIException as e:
Expand Down

0 comments on commit ea0a02b

Please sign in to comment.