refactor(groups): dedup the group-service value-size check + shared-emit tail

Two clean, local dedups in the group shared-collection services:

- Extract `GroupKvServiceImpl::check_value_size` — `set` and `set_if` inlined
  the identical encode-and-cap block; now one method (mirrors the sibling
  `group_docs_service::check_data_size`).
- Add `group_collection_repo::best_effort_emit_shared` — the KV/docs/files
  services each copied the same emit-and-log-on-error tail for shared-collection
  triggers. Centralize the tail (logging the event's own `source`/`op`); each
  service still builds its own `ServiceEvent` (payload shapes differ).

Pure refactor, pinned by the existing group-service unit tests. (The
`owning_group` resolver was evaluated for dedup too but left as-is: the five
error enums diverge — `GroupFilesError::InvalidCollection` carries a `String`,
`GroupPubsubError` lacks the variant, and files/pubsub skip the empty-check kv/
docs/queue do — so a shared conversion would be a behavior change, not a
refactor.)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
MechaCat02
2026-07-13 19:44:06 +02:00
parent f1b480f2eb
commit 5e6bb762f4
4 changed files with 78 additions and 72 deletions

View File

@@ -13,11 +13,29 @@
//! name does not resolve.
use async_trait::async_trait;
use picloud_shared::{AppId, GroupId, ScriptOwner};
use picloud_shared::{AppId, GroupId, ScriptOwner, SdkCallCx, ServiceEvent, ServiceEventEmitter};
use sqlx::{PgPool, Postgres, Transaction};
use crate::config_resolver::CHAIN_LEVELS_CTE;
/// §11.6: fire a `shared = true` trigger event on the owning group, best-effort.
/// An emit failure is logged (with the event's `source`/`op`) and swallowed —
/// the write already committed, and shared triggers are fire-and-forget, so a
/// dropped emit must never fail the caller. The single home for the tail the
/// group KV/docs/files services previously each copied; each still builds its
/// own `ServiceEvent` (payload shapes differ) and calls this.
pub(crate) async fn best_effort_emit_shared(
events: &dyn ServiceEventEmitter,
cx: &SdkCallCx,
group_id: GroupId,
event: ServiceEvent,
) {
let (source, op) = (event.source, event.op);
if let Err(e) = events.emit_shared(cx, group_id, event).await {
tracing::error!(error = %e, source, op, event_emit_failure = true, "shared event emit failed");
}
}
/// List **all** shared-collection declarations at `owner` as `(name, kind)`
/// pairs, sorted. Used by the kind-aware apply diff and `collections ls`.
pub async fn list_all_for_owner(