use jacquard_api::com_atproto::label::{LabelValue, LabelValueDefinition};
use jacquard_common::bos::{BosStr, DefaultStr};
use jacquard_common::types::string::Did;
use smol_str::SmolStr;
use std::collections::HashMap;
use std::hash::Hash;
#[derive(Debug, Clone)]
pub struct ModerationPrefs {
pub adult_content_enabled: bool,
pub labels: HashMap<SmolStr, LabelPref>,
pub labelers: HashMap<Did, HashMap<SmolStr, LabelPref>>,
}
impl Default for ModerationPrefs {
fn default() -> Self {
Self {
adult_content_enabled: false,
labels: HashMap::new(),
labelers: HashMap::new(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LabelPref {
Hide,
Warn,
Ignore,
}
#[derive(Debug, Clone)]
pub struct LabelerDefs<S: BosStr + Hash + Eq = DefaultStr> {
pub defs: HashMap<Did<S>, Vec<LabelValueDefinition<S>>>,
}
impl<S: BosStr + Hash + Eq> Default for LabelerDefs<S> {
fn default() -> Self {
Self {
defs: HashMap::new(),
}
}
}
impl<S: BosStr + Hash + Eq> LabelerDefs<S> {
pub fn new() -> Self {
Self::default()
}
pub fn insert(&mut self, did: Did<S>, definitions: Vec<LabelValueDefinition<S>>) {
self.defs.insert(did, definitions);
}
pub fn get(&self, did: &Did<impl BosStr>) -> Option<&[LabelValueDefinition<S>]> {
self.defs
.iter()
.find(|(k, _)| k.as_ref() == did.as_ref())
.map(|(_, v)| v.as_slice())
}
pub fn find_def(
&self,
labeler: &Did<impl BosStr>,
identifier: &str,
) -> Option<&LabelValueDefinition<S>> {
self.defs
.iter()
.find(|(k, _)| k.as_ref() == labeler.as_ref())
.and_then(|(_, v)| v.iter().find(|def| def.identifier.as_ref() == identifier))
}
}
#[derive(Debug, Clone, Default)]
pub struct ModerationDecision {
pub filter: bool,
pub blur: Blur,
pub alert: bool,
pub inform: bool,
pub no_override: bool,
pub causes: Vec<LabelCause>,
}
impl ModerationDecision {
pub fn none() -> Self {
Self::default()
}
pub fn is_moderated(&self) -> bool {
self.filter || self.blur != Blur::None || self.alert || self.inform
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Blur {
#[default]
None,
Content,
Media,
}
#[derive(Debug, Clone)]
pub struct LabelCause<S: BosStr = DefaultStr> {
pub label: LabelValue<S>,
pub source: Did<S>,
pub target: LabelTarget,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LabelTarget {
Account,
Content,
}