;; lib/mod/policies.sx — per-domain policy registry.
;;
;; rose-ash spans domains (blog, market, events, federation, …) that want
;; different moderation — a marketplace listing and a blog comment are not held to
;; the same bar. This registry maps a domain to a rule set; mod/decide-in resolves
;; the right policy and decides. Unregistered domains fall back to the default
;; rules, so adding a domain never leaves it unmoderated.

(define mod/*policies* (list))

(define mod/policies-reset! (fn () (set! mod/*policies* (list))))

(define
  mod/register-policy!
  (fn (domain rules) (begin (append! mod/*policies* {:domain domain :rules rules}) true)))

(define
  mod/policy-registered?
  (fn
    (domain)
    (mod/any? (fn (p) (= (get p :domain) domain)) mod/*policies*)))

(define
  mod/policy-for
  (fn
    (domain)
    (reduce
      (fn (acc p) (if (= (get p :domain) domain) (get p :rules) acc))
      mod/default-rules
      mod/*policies*)))

(define
  mod/decide-in
  (fn
    (domain r reports)
    (mod/decide-report r reports (mod/policy-for domain))))

(define
  mod/registered-domains
  (fn () (map (fn (p) (get p :domain)) mod/*policies*)))
