Skip to content

Commit f87152c

Browse files
refactor(db): move moderation store ownership
Signed-off-by: tornquist <[email protected]>
1 parent 64fa6de commit f87152c

3 files changed

Lines changed: 210 additions & 161 deletions

File tree

‎crates/buzz-db/src/lib.rs‎

Lines changed: 0 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,165 +1335,6 @@ impl Db {
13351335
Ok(result.rows_affected())
13361336
}
13371337

1338-
/// Insert a tenant-scoped NIP-56 report row, idempotent by report event id.
1339-
#[datastore_span(name = "insert_moderation_report", system = "postgresql")]
1340-
pub async fn insert_moderation_report(
1341-
&self,
1342-
community: CommunityId,
1343-
report: moderation::NewReport<'_>,
1344-
) -> Result<Uuid> {
1345-
moderation::insert_report(&self.pool, community, report).await
1346-
}
1347-
1348-
/// List moderation reports for a community, newest first.
1349-
#[datastore_span(name = "list_moderation_reports", system = "postgresql")]
1350-
pub async fn list_moderation_reports(
1351-
&self,
1352-
community: CommunityId,
1353-
status: Option<&str>,
1354-
limit: i64,
1355-
) -> Result<Vec<moderation::ReportRecord>> {
1356-
moderation::list_reports(&self.pool, community, status, limit).await
1357-
}
1358-
1359-
/// Fetch one moderation report by row id.
1360-
#[datastore_span(name = "get_moderation_report", system = "postgresql")]
1361-
pub async fn get_moderation_report(
1362-
&self,
1363-
community: CommunityId,
1364-
report_id: Uuid,
1365-
) -> Result<Option<moderation::ReportRecord>> {
1366-
moderation::get_report(&self.pool, community, report_id).await
1367-
}
1368-
1369-
/// Fetch one moderation report by signed NIP-56 report event id.
1370-
#[datastore_span(name = "get_moderation_report_by_event", system = "postgresql")]
1371-
pub async fn get_moderation_report_by_event(
1372-
&self,
1373-
community: CommunityId,
1374-
report_event_id: &[u8],
1375-
) -> Result<Option<moderation::ReportRecord>> {
1376-
moderation::get_report_by_event(&self.pool, community, report_event_id).await
1377-
}
1378-
1379-
/// Resolve, dismiss, or escalate an open moderation report.
1380-
#[datastore_span(name = "resolve_moderation_report", system = "postgresql")]
1381-
pub async fn resolve_moderation_report(
1382-
&self,
1383-
community: CommunityId,
1384-
report_id: Uuid,
1385-
status: &str,
1386-
resolved_by: &[u8],
1387-
action_id: Option<Uuid>,
1388-
) -> Result<bool> {
1389-
moderation::resolve_report(
1390-
&self.pool,
1391-
community,
1392-
report_id,
1393-
status,
1394-
resolved_by,
1395-
action_id,
1396-
)
1397-
.await
1398-
}
1399-
1400-
/// Upsert a community ban for a member pubkey.
1401-
#[datastore_span(name = "ban_community_member", system = "postgresql")]
1402-
pub async fn ban_community_member(
1403-
&self,
1404-
community: CommunityId,
1405-
pubkey: &[u8],
1406-
actor: &[u8],
1407-
reason: Option<&str>,
1408-
expires_at: Option<DateTime<Utc>>,
1409-
) -> Result<()> {
1410-
moderation::ban_member(&self.pool, community, pubkey, actor, reason, expires_at).await
1411-
}
1412-
1413-
/// Lift a community ban for a member pubkey.
1414-
#[datastore_span(name = "unban_community_member", system = "postgresql")]
1415-
pub async fn unban_community_member(
1416-
&self,
1417-
community: CommunityId,
1418-
pubkey: &[u8],
1419-
actor: &[u8],
1420-
) -> Result<bool> {
1421-
moderation::unban_member(&self.pool, community, pubkey, actor).await
1422-
}
1423-
1424-
/// Upsert a community timeout/write-block for a member pubkey.
1425-
#[datastore_span(name = "timeout_community_member", system = "postgresql")]
1426-
pub async fn timeout_community_member(
1427-
&self,
1428-
community: CommunityId,
1429-
pubkey: &[u8],
1430-
actor: &[u8],
1431-
muted_until: DateTime<Utc>,
1432-
reason: Option<&str>,
1433-
) -> Result<()> {
1434-
moderation::timeout_member(&self.pool, community, pubkey, actor, muted_until, reason).await
1435-
}
1436-
1437-
/// Clear a community timeout/write-block for a member pubkey.
1438-
#[datastore_span(name = "untimeout_community_member", system = "postgresql")]
1439-
pub async fn untimeout_community_member(
1440-
&self,
1441-
community: CommunityId,
1442-
pubkey: &[u8],
1443-
actor: &[u8],
1444-
) -> Result<bool> {
1445-
moderation::untimeout_member(&self.pool, community, pubkey, actor).await
1446-
}
1447-
1448-
/// Fetch the active ban/timeout restriction state for enforcement hot paths.
1449-
#[datastore_span(name = "moderation_restriction_state", system = "postgresql")]
1450-
pub async fn moderation_restriction_state(
1451-
&self,
1452-
community: CommunityId,
1453-
pubkey: &[u8],
1454-
) -> Result<moderation::RestrictionState> {
1455-
moderation::restriction_state(&self.pool, community, pubkey).await
1456-
}
1457-
1458-
/// Fetch the full ban/timeout row for a member pubkey.
1459-
#[datastore_span(name = "get_community_ban", system = "postgresql")]
1460-
pub async fn get_community_ban(
1461-
&self,
1462-
community: CommunityId,
1463-
pubkey: &[u8],
1464-
) -> Result<Option<moderation::BanRecord>> {
1465-
moderation::get_ban(&self.pool, community, pubkey).await
1466-
}
1467-
1468-
/// List currently restricted members in a community.
1469-
#[datastore_span(name = "list_community_restrictions", system = "postgresql")]
1470-
pub async fn list_community_restrictions(
1471-
&self,
1472-
community: CommunityId,
1473-
) -> Result<Vec<moderation::BanRecord>> {
1474-
moderation::list_restricted(&self.pool, community).await
1475-
}
1476-
1477-
/// Insert a moderation audit action row.
1478-
#[datastore_span(name = "insert_moderation_action", system = "postgresql")]
1479-
pub async fn insert_moderation_action(
1480-
&self,
1481-
community: CommunityId,
1482-
action: moderation::NewAction<'_>,
1483-
) -> Result<Uuid> {
1484-
moderation::insert_action(&self.pool, community, action).await
1485-
}
1486-
1487-
/// List moderation audit action rows, newest first.
1488-
#[datastore_span(name = "list_moderation_actions", system = "postgresql")]
1489-
pub async fn list_moderation_actions(
1490-
&self,
1491-
community: CommunityId,
1492-
limit: i64,
1493-
) -> Result<Vec<moderation::ActionRecord>> {
1494-
moderation::list_actions(&self.pool, community, limit).await
1495-
}
1496-
14971338
/// Return the current owner of git repo name `repo_id` in `community`, or
14981339
/// `None` if unreserved. See [`git_repo::repo_name_owner`].
14991340
#[datastore_span(name = "repo_name_owner", system = "postgresql")]

‎crates/buzz-db/src/moderation.rs‎

Lines changed: 164 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
//! Lane ownership: L1 (Max). Signatures below are the contract; changes go
1515
//! through the integration thread.
1616
17+
use buzz_datastore_tracing::datastore_span;
1718
use chrono::{DateTime, Utc};
1819
use sqlx::{PgPool, Row as _};
1920
use uuid::Uuid;
2021

2122
use crate::error::Result;
22-
use crate::CommunityId;
23+
use crate::{CommunityId, Db};
2324

2425
/// What a report points at. Exactly one target class per report row.
2526
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -627,13 +628,174 @@ fn row_to_action(row: sqlx::postgres::PgRow) -> Result<ActionRecord> {
627628
})
628629
}
629630

631+
impl Db {
632+
/// Insert a tenant-scoped NIP-56 report row, idempotent by report event id.
633+
#[datastore_span(name = "insert_moderation_report", system = "postgresql")]
634+
pub async fn insert_moderation_report(
635+
&self,
636+
community: CommunityId,
637+
report: NewReport<'_>,
638+
) -> Result<Uuid> {
639+
insert_report(&self.pool, community, report).await
640+
}
641+
642+
/// List moderation reports for a community, newest first.
643+
#[datastore_span(name = "list_moderation_reports", system = "postgresql")]
644+
pub async fn list_moderation_reports(
645+
&self,
646+
community: CommunityId,
647+
status: Option<&str>,
648+
limit: i64,
649+
) -> Result<Vec<ReportRecord>> {
650+
list_reports(&self.pool, community, status, limit).await
651+
}
652+
653+
/// Fetch one moderation report by row id.
654+
#[datastore_span(name = "get_moderation_report", system = "postgresql")]
655+
pub async fn get_moderation_report(
656+
&self,
657+
community: CommunityId,
658+
report_id: Uuid,
659+
) -> Result<Option<ReportRecord>> {
660+
get_report(&self.pool, community, report_id).await
661+
}
662+
663+
/// Fetch one moderation report by signed NIP-56 report event id.
664+
#[datastore_span(name = "get_moderation_report_by_event", system = "postgresql")]
665+
pub async fn get_moderation_report_by_event(
666+
&self,
667+
community: CommunityId,
668+
report_event_id: &[u8],
669+
) -> Result<Option<ReportRecord>> {
670+
get_report_by_event(&self.pool, community, report_event_id).await
671+
}
672+
673+
/// Resolve, dismiss, or escalate an open moderation report.
674+
#[datastore_span(name = "resolve_moderation_report", system = "postgresql")]
675+
pub async fn resolve_moderation_report(
676+
&self,
677+
community: CommunityId,
678+
report_id: Uuid,
679+
status: &str,
680+
resolved_by: &[u8],
681+
action_id: Option<Uuid>,
682+
) -> Result<bool> {
683+
resolve_report(
684+
&self.pool,
685+
community,
686+
report_id,
687+
status,
688+
resolved_by,
689+
action_id,
690+
)
691+
.await
692+
}
693+
694+
/// Upsert a community ban for a member pubkey.
695+
#[datastore_span(name = "ban_community_member", system = "postgresql")]
696+
pub async fn ban_community_member(
697+
&self,
698+
community: CommunityId,
699+
pubkey: &[u8],
700+
actor: &[u8],
701+
reason: Option<&str>,
702+
expires_at: Option<DateTime<Utc>>,
703+
) -> Result<()> {
704+
ban_member(&self.pool, community, pubkey, actor, reason, expires_at).await
705+
}
706+
707+
/// Lift a community ban for a member pubkey.
708+
#[datastore_span(name = "unban_community_member", system = "postgresql")]
709+
pub async fn unban_community_member(
710+
&self,
711+
community: CommunityId,
712+
pubkey: &[u8],
713+
actor: &[u8],
714+
) -> Result<bool> {
715+
unban_member(&self.pool, community, pubkey, actor).await
716+
}
717+
718+
/// Upsert a community timeout/write-block for a member pubkey.
719+
#[datastore_span(name = "timeout_community_member", system = "postgresql")]
720+
pub async fn timeout_community_member(
721+
&self,
722+
community: CommunityId,
723+
pubkey: &[u8],
724+
actor: &[u8],
725+
muted_until: DateTime<Utc>,
726+
reason: Option<&str>,
727+
) -> Result<()> {
728+
timeout_member(&self.pool, community, pubkey, actor, muted_until, reason).await
729+
}
730+
731+
/// Clear a community timeout/write-block for a member pubkey.
732+
#[datastore_span(name = "untimeout_community_member", system = "postgresql")]
733+
pub async fn untimeout_community_member(
734+
&self,
735+
community: CommunityId,
736+
pubkey: &[u8],
737+
actor: &[u8],
738+
) -> Result<bool> {
739+
untimeout_member(&self.pool, community, pubkey, actor).await
740+
}
741+
742+
/// Fetch the active ban/timeout restriction state for enforcement hot paths.
743+
#[datastore_span(name = "moderation_restriction_state", system = "postgresql")]
744+
pub async fn moderation_restriction_state(
745+
&self,
746+
community: CommunityId,
747+
pubkey: &[u8],
748+
) -> Result<RestrictionState> {
749+
restriction_state(&self.pool, community, pubkey).await
750+
}
751+
752+
/// Fetch the full ban/timeout row for a member pubkey.
753+
#[datastore_span(name = "get_community_ban", system = "postgresql")]
754+
pub async fn get_community_ban(
755+
&self,
756+
community: CommunityId,
757+
pubkey: &[u8],
758+
) -> Result<Option<BanRecord>> {
759+
get_ban(&self.pool, community, pubkey).await
760+
}
761+
762+
/// List currently restricted members in a community.
763+
#[datastore_span(name = "list_community_restrictions", system = "postgresql")]
764+
pub async fn list_community_restrictions(
765+
&self,
766+
community: CommunityId,
767+
) -> Result<Vec<BanRecord>> {
768+
list_restricted(&self.pool, community).await
769+
}
770+
771+
/// Insert a moderation audit action row.
772+
#[datastore_span(name = "insert_moderation_action", system = "postgresql")]
773+
pub async fn insert_moderation_action(
774+
&self,
775+
community: CommunityId,
776+
action: NewAction<'_>,
777+
) -> Result<Uuid> {
778+
insert_action(&self.pool, community, action).await
779+
}
780+
781+
/// List moderation audit action rows, newest first.
782+
#[datastore_span(name = "list_moderation_actions", system = "postgresql")]
783+
pub async fn list_moderation_actions(
784+
&self,
785+
community: CommunityId,
786+
limit: i64,
787+
) -> Result<Vec<ActionRecord>> {
788+
list_actions(&self.pool, community, limit).await
789+
}
790+
}
791+
630792
#[cfg(test)]
631793
mod tests {
632794
use super::*;
633795
use chrono::Duration;
634796
use uuid::Uuid;
635797

636-
const TEST_DB_URL: &str = "postgres://buzz:buzz_dev@localhost:5432/buzz";
798+
const TEST_DB_URL: &str = "postgres://buzz:buzz_dev@localhost:5432/buzz"; // sadscan:disable np.postgres.1
637799

638800
async fn setup_pool() -> PgPool {
639801
let database_url = std::env::var("BUZZ_TEST_DATABASE_URL")

0 commit comments

Comments
 (0)