32 lines
1.0 KiB
Python
32 lines
1.0 KiB
Python
"""Trust scoring. Append events, recompute score + tier.
|
|
|
|
Phase 1 wiring: email verification grants trust. Listing-survival and flag
|
|
penalties hook in during Phase 2/3. Thresholds are intentionally simple here.
|
|
"""
|
|
from app.extensions import db
|
|
from app.models.trust import TrustEvent
|
|
from app.models.enums import TrustEventType, TrustTier
|
|
|
|
# tier thresholds by cumulative score
|
|
_TIER_THRESHOLDS = [
|
|
(50, TrustTier.verified),
|
|
(20, TrustTier.trusted),
|
|
(5, TrustTier.basic),
|
|
(0, TrustTier.new),
|
|
]
|
|
|
|
|
|
def record_event(user, event_type: TrustEventType, delta: int):
|
|
"""Append a trust event, bump score, recompute tier. Caller commits."""
|
|
db.session.add(TrustEvent(user_id=user.id, type=event_type, delta=delta))
|
|
user.trust_score = max(0, (user.trust_score or 0) + delta)
|
|
user.trust_tier = _tier_for(user.trust_score)
|
|
return user
|
|
|
|
|
|
def _tier_for(score: int) -> TrustTier:
|
|
for threshold, tier in _TIER_THRESHOLDS:
|
|
if score >= threshold:
|
|
return tier
|
|
return TrustTier.new
|