use super::PgExecResult; use crate::indexer::records::AppBskyLabelerService; use deadpool_postgres::GenericClient; use ipld_core::cid::Cid; use lexica::com_atproto::label::{LabelValueDefinition, SelfLabels}; use std::collections::HashMap; pub async fn maintain_label_defs( conn: &mut C, repo: &str, rec: &AppBskyLabelerService, ) -> PgExecResult { // drop any label defs not currently in the list conn.execute( "DELETE FROM labeler_defs WHERE labeler=$1 AND NOT label_identifier = any($2)", &[&repo, &rec.policies.label_values], ) .await?; let definitions = rec .policies .label_value_definitions .iter() .map(|def| (def.identifier.clone(), def)) .collect::>(); for label in &rec.policies.label_values { let definition = definitions.get(label); let severity = definition.map(|v| v.severity.to_string()); let blurs = definition.map(|v| v.blurs.to_string()); let default_setting = definition .and_then(|v| v.default_setting) .map(|v| v.to_string()); let adult_only = definition.and_then(|v| v.adult_only).unwrap_or_default(); let locales = definition.and_then(|v| serde_json::to_value(&v.locales).ok()); conn.execute( include_str!("sql/label_defs_upsert.sql"), &[ &repo, &label, &severity, &blurs, &default_setting, &adult_only, &locales, ], ) .await?; } Ok(0) } pub async fn maintain_self_labels( conn: &mut C, repo: &str, cid: Option, at_uri: &str, self_labels: SelfLabels, ) -> PgExecResult { conn.execute( "DELETE FROM labels WHERE self_label=TRUE AND uri=$1", &[&at_uri], ) .await?; let cid = cid.map(|cid| cid.to_string()); let stmt = conn.prepare_cached("INSERT INTO labels (labeler, label, uri, self_label, cid, created_at) VALUES ($1, $2, $3, TRUE, $4, NOW())").await?; for label in self_labels.values { conn.execute(&stmt, &[&repo, &label.val, &at_uri, &cid.clone()]) .await?; } Ok(0) }