Skip to content

Commit 7dbb399

Browse files
refactor(db): move admin moderation store ownership
Signed-off-by: tornquist <[email protected]>
1 parent 27378c8 commit 7dbb399

3 files changed

Lines changed: 91 additions & 58 deletions

File tree

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
//! [`CommunityId`](buzz_core::CommunityId). Keep ordinary moderation reads in
55
//! [`crate::moderation`] tenant-fenced.
66
7+
use buzz_datastore_tracing::datastore_span;
78
use chrono::{DateTime, Utc};
89
use serde::Serialize;
910
use sqlx::{PgPool, Row as _};
1011
use uuid::Uuid;
1112

1213
use crate::error::Result;
14+
use crate::Db;
1315

1416
/// Maximum rows accepted by one admin query.
1517
pub const MAX_PAGE_SIZE: i64 = 200;
@@ -284,11 +286,59 @@ fn row_to_feedback(row: sqlx::postgres::PgRow) -> Result<AdminFeedback> {
284286
})
285287
}
286288

289+
impl Db {
290+
/// List reports for the deployment-global read-only admin plane.
291+
#[allow(clippy::too_many_arguments)]
292+
#[datastore_span(name = "admin_list_reports", system = "postgresql")]
293+
pub async fn admin_list_reports(
294+
&self,
295+
community_id: Option<Uuid>,
296+
status: Option<&str>,
297+
report_type: Option<&str>,
298+
target_kind: Option<&str>,
299+
after: Option<DateTime<Utc>>,
300+
before: Option<DateTime<Utc>>,
301+
cursor: Option<(DateTime<Utc>, Uuid)>,
302+
limit: i64,
303+
) -> Result<Vec<AdminReport>> {
304+
list_reports(
305+
&self.pool,
306+
community_id,
307+
status,
308+
report_type,
309+
target_kind,
310+
after,
311+
before,
312+
cursor,
313+
limit,
314+
)
315+
.await
316+
}
317+
318+
/// Fetch one report for the deployment-global read-only admin plane.
319+
#[datastore_span(name = "admin_get_report", system = "postgresql")]
320+
pub async fn admin_get_report(&self, id: Uuid) -> Result<Option<AdminReportDetail>> {
321+
get_report(&self.pool, id).await
322+
}
323+
324+
/// List feedback for the deployment-global read-only admin plane.
325+
#[datastore_span(name = "admin_list_feedback", system = "postgresql")]
326+
pub async fn admin_list_feedback(&self, limit: i64) -> Result<Vec<AdminFeedback>> {
327+
list_feedback(&self.pool, limit).await
328+
}
329+
330+
/// Fetch one feedback submission for the deployment-global admin plane.
331+
#[datastore_span(name = "admin_get_feedback", system = "postgresql")]
332+
pub async fn admin_get_feedback(&self, id: Uuid) -> Result<Option<AdminFeedback>> {
333+
get_feedback(&self.pool, id).await
334+
}
335+
}
336+
287337
#[cfg(test)]
288338
mod tests {
289339
use super::*;
290340

291-
const TEST_DB_URL: &str = "postgres://buzz:buzz_dev@localhost:5432/buzz";
341+
const TEST_DB_URL: &str = "postgres://buzz:buzz_dev@localhost:5432/buzz"; // sadscan:disable np.postgres.1
292342

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

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

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -993,61 +993,6 @@ impl Db {
993993
})
994994
}
995995

996-
/// List reports for the deployment-global read-only admin plane.
997-
#[allow(clippy::too_many_arguments)]
998-
#[datastore_span(name = "admin_list_reports", system = "postgresql")]
999-
pub async fn admin_list_reports(
1000-
&self,
1001-
community_id: Option<Uuid>,
1002-
status: Option<&str>,
1003-
report_type: Option<&str>,
1004-
target_kind: Option<&str>,
1005-
after: Option<DateTime<Utc>>,
1006-
before: Option<DateTime<Utc>>,
1007-
cursor: Option<(DateTime<Utc>, Uuid)>,
1008-
limit: i64,
1009-
) -> Result<Vec<admin_moderation::AdminReport>> {
1010-
admin_moderation::list_reports(
1011-
&self.pool,
1012-
community_id,
1013-
status,
1014-
report_type,
1015-
target_kind,
1016-
after,
1017-
before,
1018-
cursor,
1019-
limit,
1020-
)
1021-
.await
1022-
}
1023-
1024-
/// Fetch one report for the deployment-global read-only admin plane.
1025-
#[datastore_span(name = "admin_get_report", system = "postgresql")]
1026-
pub async fn admin_get_report(
1027-
&self,
1028-
id: Uuid,
1029-
) -> Result<Option<admin_moderation::AdminReportDetail>> {
1030-
admin_moderation::get_report(&self.pool, id).await
1031-
}
1032-
1033-
/// List feedback for the deployment-global read-only admin plane.
1034-
#[datastore_span(name = "admin_list_feedback", system = "postgresql")]
1035-
pub async fn admin_list_feedback(
1036-
&self,
1037-
limit: i64,
1038-
) -> Result<Vec<admin_moderation::AdminFeedback>> {
1039-
admin_moderation::list_feedback(&self.pool, limit).await
1040-
}
1041-
1042-
/// Fetch one feedback submission for the deployment-global admin plane.
1043-
#[datastore_span(name = "admin_get_feedback", system = "postgresql")]
1044-
pub async fn admin_get_feedback(
1045-
&self,
1046-
id: Uuid,
1047-
) -> Result<Option<admin_moderation::AdminFeedback>> {
1048-
admin_moderation::get_feedback(&self.pool, id).await
1049-
}
1050-
1051996
/// Return the shared durable whole-community deletion adapter.
1052997
pub fn deletion_store(&self) -> deletion::DeletionStore {
1053998
deletion::DeletionStore::new(self.pool.clone())

‎crates/buzz-db/tests/store_ownership.rs‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -962,8 +962,9 @@ fn workflow_lifecycle_store_has_single_ownership() {
962962
);
963963

964964
for ty in ["WorkflowRecord", "ScheduledWorkflowFireClaim"] {
965-
assert_eq!(count(root, &format!("struct {ty}")), 0);
966-
assert_eq!(count(store, &format!("struct {ty}")), 1);
965+
let declaration = format!("pub struct {ty} {{");
966+
assert_eq!(count(root, &declaration), 0);
967+
assert_eq!(count(store, &declaration), 1);
967968
}
968969
assert_eq!(count(root, "enum WorkflowStatus"), 0);
969970
assert_eq!(count(store, "enum WorkflowStatus"), 1);
@@ -1114,3 +1115,40 @@ fn usage_store_has_single_ownership() {
11141115
1
11151116
);
11161117
}
1118+
1119+
#[test]
1120+
fn admin_moderation_store_has_single_ownership() {
1121+
let root = include_str!("../src/lib.rs");
1122+
let store = include_str!("../src/admin_moderation.rs");
1123+
1124+
for method in [
1125+
"admin_list_reports",
1126+
"admin_get_report",
1127+
"admin_list_feedback",
1128+
"admin_get_feedback",
1129+
] {
1130+
let signature = format!("pub async fn {method}(");
1131+
assert_eq!(count(root, &signature), 0, "{method} remains in lib.rs");
1132+
assert_eq!(
1133+
count(store, &signature),
1134+
1,
1135+
"{method} Db wrapper is not unique"
1136+
);
1137+
assert_eq!(
1138+
count(store, &format!("name = \"{method}\"")),
1139+
1,
1140+
"{method} span is not unique"
1141+
);
1142+
}
1143+
1144+
for ty in [
1145+
"AdminReport",
1146+
"AdminReportedMessage",
1147+
"AdminReportDetail",
1148+
"AdminFeedback",
1149+
] {
1150+
let declaration = format!("pub struct {ty} {{");
1151+
assert_eq!(count(root, &declaration), 0);
1152+
assert_eq!(count(store, &declaration), 1);
1153+
}
1154+
}

0 commit comments

Comments
 (0)