|
14 | 14 | //! Lane ownership: L1 (Max). Signatures below are the contract; changes go |
15 | 15 | //! through the integration thread. |
16 | 16 |
|
| 17 | +use buzz_datastore_tracing::datastore_span; |
17 | 18 | use chrono::{DateTime, Utc}; |
18 | 19 | use sqlx::{PgPool, Row as _}; |
19 | 20 | use uuid::Uuid; |
20 | 21 |
|
21 | 22 | use crate::error::Result; |
22 | | -use crate::CommunityId; |
| 23 | +use crate::{CommunityId, Db}; |
23 | 24 |
|
24 | 25 | /// What a report points at. Exactly one target class per report row. |
25 | 26 | #[derive(Debug, Clone, PartialEq, Eq)] |
@@ -627,13 +628,174 @@ fn row_to_action(row: sqlx::postgres::PgRow) -> Result<ActionRecord> { |
627 | 628 | }) |
628 | 629 | } |
629 | 630 |
|
| 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 | + |
630 | 792 | #[cfg(test)] |
631 | 793 | mod tests { |
632 | 794 | use super::*; |
633 | 795 | use chrono::Duration; |
634 | 796 | use uuid::Uuid; |
635 | 797 |
|
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 |
637 | 799 |
|
638 | 800 | async fn setup_pool() -> PgPool { |
639 | 801 | let database_url = std::env::var("BUZZ_TEST_DATABASE_URL") |
|
0 commit comments