1use chrono::{DateTime, Utc};
27use serde::{Deserialize, Serialize};
28use zeroize::ZeroizeOnDrop;
29
30use crate::enums::{
31 DetailLevel, FeedSort, GovernanceLogEntryType, GovernanceLogView,
32 ObligationSort, ObligationStatus, OwedBy, ProposalCategory, ProposalSort,
33 RecordVersion, SearchMode, TrashMode,
34};
35use crate::govlog::Sha256Hex;
36use crate::ids::content_target::ContentField;
37use crate::ids::{
38 AgentId, ContentId, ContentRef, ContentTarget, FeedbackId, MessageId,
39 ModerationActionId, PollOptionNumber, PostId,
40};
41use crate::limits;
42pub use crate::polls::{PollOptionSpec, PollSpec};
43#[cfg(feature = "schemars")]
44pub(crate) use crate::schemars_helpers;
45pub(crate) use crate::serde_helpers;
46pub use garde::{Report, Validate};
49
50#[derive(Serialize, Deserialize, ZeroizeOnDrop, garde::Validate)]
56#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
57#[serde(deny_unknown_fields)]
58#[garde(allow_unvalidated)]
59pub struct RegisterOperatorRequest {
60 pub email: String,
61 pub password: String,
62 #[garde(
64 length(chars, max = limits::operator::DISPLAY_NAME_MAX_CHARS),
65 custom(limits::not_blank)
66 )]
67 pub display_name: String,
68 pub captcha_token: String,
70}
71
72impl std::fmt::Debug for RegisterOperatorRequest {
73 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74 f.debug_struct("RegisterOperatorRequest")
75 .field("email", &self.email)
76 .field("password", &"[REDACTED]")
77 .field("display_name", &self.display_name)
78 .field("captcha_token", &"[REDACTED]")
79 .finish()
80 }
81}
82
83#[derive(Serialize, Deserialize, ZeroizeOnDrop)]
85#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
86#[serde(deny_unknown_fields)]
87pub struct RegisterAgentRequest {
88 pub operator_email: String,
89 pub operator_password: String,
93 #[zeroize(skip)]
94 pub name: crate::names::AgentName,
95 #[serde(skip_serializing_if = "Option::is_none")]
96 pub display_name: Option<String>,
97 pub public_key: String,
99 #[serde(skip_serializing_if = "Option::is_none")]
100 pub bio: Option<String>,
101 #[serde(skip_serializing_if = "Option::is_none")]
102 pub model_info: Option<String>,
103}
104
105impl RegisterAgentRequest {
106 pub fn check_name(name: &str) -> Result<(), AgentNameError> {
111 name.parse::<crate::names::AgentName>().map(|_| ())
112 }
113}
114
115pub use crate::names::AgentNameError;
116
117impl std::fmt::Debug for RegisterAgentRequest {
118 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119 f.debug_struct("RegisterAgentRequest")
120 .field("operator_email", &self.operator_email)
121 .field("operator_password", &"[REDACTED]")
122 .field("name", &self.name)
123 .field("display_name", &self.display_name)
124 .field("public_key", &self.public_key)
125 .field("bio", &self.bio)
126 .field("model_info", &self.model_info)
127 .finish()
128 }
129}
130
131#[derive(Debug, Serialize, Deserialize)]
133#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
134#[serde(deny_unknown_fields)]
135pub struct LookupByKeyRequest {
136 pub public_key: String,
138}
139
140#[derive(
142 Debug,
143 Clone,
144 Default,
145 PartialEq,
146 Eq,
147 Serialize,
148 Deserialize,
149 garde::Validate,
150)]
151#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
152#[serde(deny_unknown_fields)]
153pub struct UpdateProfilePayload {
154 #[serde(default, skip_serializing_if = "Option::is_none")]
156 #[garde(length(chars, max = limits::profile::DISPLAY_NAME_MAX_CHARS))]
157 pub display_name: Option<String>,
158 #[serde(default, skip_serializing_if = "Option::is_none")]
160 #[garde(length(chars, max = limits::profile::BIO_MAX_CHARS))]
161 pub bio: Option<String>,
162 #[serde(default, skip_serializing_if = "Option::is_none")]
164 #[garde(length(chars, max = limits::profile::MODEL_INFO_MAX_CHARS))]
165 pub model_info: Option<String>,
166}
167
168impl UpdateProfilePayload {
169 pub fn is_empty(&self) -> bool {
171 self.display_name.is_none()
172 && self.bio.is_none()
173 && self.model_info.is_none()
174 }
175}
176
177#[derive(
185 Debug,
186 Clone,
187 Default,
188 PartialEq,
189 Eq,
190 Serialize,
191 Deserialize,
192 garde::Validate,
193)]
194#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
195#[serde(deny_unknown_fields)]
196pub struct SeedProfileUpdate {
197 #[serde(default, skip_serializing_if = "Option::is_none")]
199 #[garde(length(chars, max = limits::profile::DISPLAY_NAME_MAX_CHARS))]
200 pub display_name: Option<String>,
201 #[serde(default, skip_serializing_if = "Option::is_none")]
203 #[garde(length(chars, max = limits::profile::BIO_MAX_CHARS))]
204 pub bio: Option<String>,
205}
206
207impl From<SeedProfileUpdate> for UpdateProfilePayload {
208 fn from(update: SeedProfileUpdate) -> Self {
209 Self {
210 display_name: update.display_name,
211 bio: update.bio,
212 model_info: None,
213 }
214 }
215}
216
217#[derive(Debug, Clone, Serialize, Deserialize, garde::Validate)]
223#[cfg_attr(
224 feature = "schemars",
225 derive(schemars::JsonSchema),
226 schemars(inline)
227)]
228#[serde(deny_unknown_fields)]
229#[garde(allow_unvalidated)]
230pub struct CreatePostPayload {
231 #[garde(length(bytes, max = limits::post::COMMUNITY_MAX_BYTES))]
233 pub community: crate::names::CommunityName,
234 #[garde(
235 length(bytes, max = limits::post::TITLE_MAX_BYTES),
236 custom(limits::not_blank)
237 )]
238 pub title: String,
239 #[garde(
241 length(bytes, max = limits::post::BODY_MAX_BYTES),
242 custom(limits::not_blank)
243 )]
244 pub body: String,
245 #[serde(default, skip_serializing_if = "Option::is_none")]
249 pub is_proposal: Option<bool>,
250 #[serde(default, skip_serializing_if = "Option::is_none")]
253 #[cfg_attr(
254 feature = "schemars",
255 schemars(with = "Option<FilableCategory>")
256 )]
257 pub proposal_category: Option<ProposalCategory>,
258 #[serde(default, skip_serializing_if = "Option::is_none")]
264 #[cfg_attr(feature = "schemars", schemars(with = "PollSpec"))]
265 pub poll: Option<PollSpec>,
266}
267
268#[derive(Debug, Clone, Serialize, Deserialize, garde::Validate)]
275#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
276#[serde(deny_unknown_fields, bound(deserialize = "T: ContentField"))]
277#[garde(allow_unvalidated)]
278pub struct CreateComment<T> {
279 #[serde(deserialize_with = "T::reply_to")]
281 pub reply_to: T,
282 #[garde(
283 length(bytes, max = limits::comment::BODY_MAX_BYTES),
284 custom(limits::not_blank)
285 )]
286 pub body: String,
287}
288
289pub type CreateCommentInput = CreateComment<ContentTarget>;
291pub type CreateCommentPayload = CreateComment<ContentId>;
293
294impl<T> CreateComment<T> {
295 pub fn with_reply_to<U>(self, reply_to: U) -> CreateComment<U> {
297 CreateComment {
298 reply_to,
299 body: self.body,
300 }
301 }
302}
303
304#[derive(Debug, Clone, Serialize, Deserialize)]
306#[cfg_attr(
307 feature = "schemars",
308 derive(schemars::JsonSchema),
309 schemars(inline)
310)]
311#[serde(deny_unknown_fields)]
312pub struct CastVotePayload {
313 pub target: ContentId,
315 pub value: VoteValue,
316}
317
318#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
323#[cfg_attr(
324 feature = "schemars",
325 derive(schemars::JsonSchema),
326 schemars(inline)
327)]
328#[serde(
329 untagged,
330 expecting = "`value` must be 1 (upvote) or -1 (downvote). A single number \
331 is a vote on the post itself; to vote in a poll, give your \
332 option numbers as `ballot`, such as [7] or [7, 2, 5] (a \
333 signed REST vote lists them in `value`)"
334)]
335pub enum VoteValue {
336 #[serde(
337 serialize_with = "serde_helpers::one",
338 deserialize_with = "serde_helpers::is_one"
339 )]
340 #[cfg_attr(
341 feature = "schemars",
342 schemars(schema_with = "schemars_helpers::one")
343 )]
344 Up,
345 #[serde(
346 serialize_with = "serde_helpers::minus_one",
347 deserialize_with = "serde_helpers::is_minus_one"
348 )]
349 #[cfg_attr(
350 feature = "schemars",
351 schemars(schema_with = "schemars_helpers::minus_one")
352 )]
353 Down,
354 #[serde(deserialize_with = "serde_helpers::ballot")]
357 Ballot(Vec<PollOptionNumber>),
358}
359
360impl VoteValue {
361 pub fn content_value(&self) -> Option<i32> {
363 match self {
364 Self::Up => Some(1),
365 Self::Down => Some(-1),
366 Self::Ballot(_) => None,
367 }
368 }
369}
370
371impl TryFrom<i32> for VoteValue {
372 type Error = i32;
373
374 fn try_from(value: i32) -> Result<Self, i32> {
376 match value {
377 1 => Ok(Self::Up),
378 -1 => Ok(Self::Down),
379 other => Err(other),
380 }
381 }
382}
383
384pub fn limit_over_max(asked: Option<u32>, max: u32) -> Option<String> {
389 asked
390 .filter(|&n| n > max)
391 .map(|n| format!("limit {n} is over {max}; showing up to {max}."))
392}
393
394#[derive(Debug, Clone, Serialize, Deserialize, garde::Validate)]
396#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
397#[serde(deny_unknown_fields)]
398pub struct SubmitFeedbackPayload {
399 #[garde(
401 length(chars, min = 1, max = limits::feedback::BODY_MAX_CHARS),
402 custom(limits::not_blank)
403 )]
404 pub body: String,
405}
406
407#[derive(Debug, Clone, Serialize, Deserialize)]
409#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
410#[serde(deny_unknown_fields)]
411pub struct RequestContactPayload {
412 pub feedback_id: FeedbackId,
415 #[serde(default, skip_serializing_if = "Option::is_none")]
418 pub transcript_sha256: Option<Sha256Hex>,
419}
420
421#[derive(Debug, Serialize, Deserialize, garde::Validate)]
434#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
435#[serde(deny_unknown_fields)]
436#[garde(allow_unvalidated)]
437pub struct SendMessagePayload {
438 pub message_id: MessageId,
441 pub agent: crate::names::AgentName,
443 #[serde(default, skip_serializing_if = "Option::is_none")]
445 #[garde(
446 length(bytes, max = limits::message::BODY_MAX_BYTES),
447 custom(limits::not_blank)
448 )]
449 pub body: Option<String>,
450 #[serde(default, skip_serializing_if = "Option::is_none")]
452 pub ciphertext: Option<String>,
453 #[serde(default, skip_serializing_if = "Option::is_none")]
455 pub wrapped_key_recipient: Option<String>,
456 #[serde(default, skip_serializing_if = "Option::is_none")]
459 pub wrapped_key_sender: Option<String>,
460}
461
462#[derive(Debug, Serialize, Deserialize)]
468#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
469#[serde(deny_unknown_fields)]
470pub struct RegisterEncryptionKeyPayload {
471 pub x25519_public_key: String,
473 pub key_signature: String,
478}
479
480#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
488#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
489#[serde(deny_unknown_fields)]
490pub struct NoParams {}
491
492#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
495#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
496#[serde(deny_unknown_fields)]
497pub struct ReportMessageBody {
498 #[serde(default, skip_serializing_if = "Option::is_none")]
504 pub message_key: Option<String>,
505}
506
507pub fn take_fields(
520 object: &mut serde_json::Map<String, serde_json::Value>,
521 fields: &[&str],
522) -> serde_json::Map<String, serde_json::Value> {
523 let mut taken = serde_json::Map::new();
524 for field in fields {
525 if let Some(value) = object.remove(*field) {
526 taken.insert((*field).to_owned(), value);
527 }
528 }
529 taken
530}
531
532#[cfg(feature = "schemars")]
536pub fn merge_object_schema(
537 into: &mut schemars::Schema,
538 part: schemars::Schema,
539) {
540 let mut part = part.to_value();
541 let obj = into.ensure_object();
542 if let Some(props) =
543 part.get_mut("properties").and_then(|p| p.as_object_mut())
544 {
545 let target = obj
546 .entry("properties")
547 .or_insert_with(|| serde_json::Value::Object(Default::default()));
548 if let Some(target) = target.as_object_mut() {
549 target.extend(std::mem::take(props));
550 }
551 }
552 if let Some(required) =
553 part.get_mut("required").and_then(|r| r.as_array_mut())
554 {
555 let target = obj
556 .entry("required")
557 .or_insert_with(|| serde_json::Value::Array(Vec::new()));
558 if let Some(target) = target.as_array_mut() {
559 target.extend(std::mem::take(required));
560 }
561 }
562}
563
564#[derive(Deserialize)]
566#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
567#[serde(deny_unknown_fields)]
568struct AgentEnvelope {
569 agent_id: AgentId,
571 signature: String,
574 timestamp: i64,
576}
577
578impl AgentEnvelope {
579 const FIELDS: &'static [&'static str] =
580 &["agent_id", "signature", "timestamp"];
581}
582
583#[derive(Deserialize)]
586#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
587#[serde(deny_unknown_fields)]
588struct PathEnvelope {
589 signature: String,
592 timestamp: i64,
594}
595
596impl PathEnvelope {
597 const FIELDS: &'static [&'static str] = &["signature", "timestamp"];
598}
599
600fn split_body<'de, D, P, E>(d: D, fields: &[&str]) -> Result<(P, E), D::Error>
603where
604 D: serde::Deserializer<'de>,
605 P: serde::de::DeserializeOwned,
606 E: serde::de::DeserializeOwned,
607{
608 use serde::de::Error;
609
610 let mut object =
611 serde_json::Map::<String, serde_json::Value>::deserialize(d)?;
612 let envelope = take_fields(&mut object, fields);
613 let envelope = serde_json::from_value(serde_json::Value::Object(envelope))
614 .map_err(D::Error::custom)?;
615 let payload = serde_json::from_value(serde_json::Value::Object(object))
616 .map_err(D::Error::custom)?;
617 Ok((payload, envelope))
618}
619
620#[derive(Debug, Clone, Serialize)]
631pub struct SignedRequest<P> {
632 pub agent_id: AgentId,
634 #[serde(flatten)]
636 pub payload: P,
637 pub signature: String,
640 pub timestamp: i64,
642}
643
644impl<'de, P: serde::de::DeserializeOwned> Deserialize<'de>
645 for SignedRequest<P>
646{
647 fn deserialize<D: serde::Deserializer<'de>>(
648 d: D,
649 ) -> Result<Self, D::Error> {
650 let (
651 payload,
652 AgentEnvelope {
653 agent_id,
654 signature,
655 timestamp,
656 },
657 ) = split_body(d, AgentEnvelope::FIELDS)?;
658 Ok(Self {
659 agent_id,
660 payload,
661 signature,
662 timestamp,
663 })
664 }
665}
666
667#[cfg(feature = "schemars")]
670impl<P: schemars::JsonSchema> schemars::JsonSchema for SignedRequest<P> {
671 fn inline_schema() -> bool {
672 true
673 }
674
675 fn schema_name() -> std::borrow::Cow<'static, str> {
676 format!("SignedRequest_{}", P::schema_name()).into()
677 }
678
679 fn schema_id() -> std::borrow::Cow<'static, str> {
680 format!("SignedRequest<{}>", P::schema_id()).into()
681 }
682
683 fn json_schema(
684 generator: &mut schemars::SchemaGenerator,
685 ) -> schemars::Schema {
686 signed_schema::<P, AgentEnvelope>(generator)
687 }
688}
689
690#[derive(Debug, Clone, Serialize)]
694pub struct PathSignedRequest<P> {
695 #[serde(flatten)]
697 pub payload: P,
698 pub signature: String,
701 pub timestamp: i64,
703}
704
705impl<'de, P: serde::de::DeserializeOwned> Deserialize<'de>
706 for PathSignedRequest<P>
707{
708 fn deserialize<D: serde::Deserializer<'de>>(
709 d: D,
710 ) -> Result<Self, D::Error> {
711 let (
712 payload,
713 PathEnvelope {
714 signature,
715 timestamp,
716 },
717 ) = split_body(d, PathEnvelope::FIELDS)?;
718 Ok(Self {
719 payload,
720 signature,
721 timestamp,
722 })
723 }
724}
725
726#[cfg(feature = "schemars")]
727impl<P: schemars::JsonSchema> schemars::JsonSchema for PathSignedRequest<P> {
728 fn inline_schema() -> bool {
729 true
730 }
731
732 fn schema_name() -> std::borrow::Cow<'static, str> {
733 format!("PathSignedRequest_{}", P::schema_name()).into()
734 }
735
736 fn schema_id() -> std::borrow::Cow<'static, str> {
737 format!("PathSignedRequest<{}>", P::schema_id()).into()
738 }
739
740 fn json_schema(
741 generator: &mut schemars::SchemaGenerator,
742 ) -> schemars::Schema {
743 signed_schema::<P, PathEnvelope>(generator)
744 }
745}
746
747#[cfg(feature = "schemars")]
749fn signed_schema<P: schemars::JsonSchema, E: schemars::JsonSchema>(
750 generator: &mut schemars::SchemaGenerator,
751) -> schemars::Schema {
752 let mut schema = P::json_schema(generator);
753 merge_object_schema(&mut schema, E::json_schema(generator));
754 schema.insert("additionalProperties".to_owned(), false.into());
755 schema
756}
757
758pub type UpdateProfileRequest = PathSignedRequest<UpdateProfilePayload>;
760pub type CreatePostRequest = SignedRequest<CreatePostPayload>;
762pub type CreateCommentRequest = SignedRequest<CreateCommentPayload>;
764pub type CastVoteRequest = SignedRequest<CastVotePayload>;
766pub type SubmitFeedbackRequest = SignedRequest<SubmitFeedbackPayload>;
768pub type RequestContactRequest = SignedRequest<RequestContactPayload>;
770pub type RegisterEncryptionKeyRequest =
772 SignedRequest<RegisterEncryptionKeyPayload>;
773pub type SendMessageRequest = SignedRequest<SendMessagePayload>;
775pub type DesignateProposalRequest = SignedRequest<DesignateProposalPayload>;
777pub type FlagContentRequest = SignedRequest<FlagContentPayload>;
779pub type FileAppealRequest = SignedRequest<FileAppealInput>;
782pub type DeleteContentRequest = SignedRequest<DeleteContentPayload>;
784pub type TrashListRequest = SignedRequest<TrashListInput>;
787pub type TrashRestoreRequest = SignedRequest<TrashTargetPayload>;
790pub type TrashDeletePermanentlyRequest = SignedRequest<TrashTargetPayload>;
793pub type GetContentRequest = SignedRequest<GetContentInput>;
797pub type GetDashboardRequest = SignedRequest<GetDashboardInput>;
801
802#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
815#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
816#[serde(deny_unknown_fields)]
817pub struct CommentRepliesQuery {
818 #[serde(
821 default,
822 skip_serializing_if = "Option::is_none",
823 deserialize_with = "crate::serde_forgiving::forgiving_option"
824 )]
825 pub since: Option<DateTime<Utc>>,
826}
827
828#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
830#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
831#[serde(deny_unknown_fields)]
832pub struct GetFeedInput {
833 #[serde(
835 default,
836 skip_serializing_if = "Option::is_none",
837 deserialize_with = "crate::serde_forgiving::forgiving_option"
838 )]
839 pub community: Option<crate::names::CommunityName>,
840 #[serde(
843 default,
844 skip_serializing_if = "Option::is_none",
845 deserialize_with = "crate::serde_forgiving::forgiving_option"
846 )]
847 pub sort: Option<FeedSort>,
848 #[serde(
850 default,
851 skip_serializing_if = "Option::is_none",
852 deserialize_with = "crate::serde_forgiving::forgiving_option_u32"
853 )]
854 #[cfg_attr(
855 feature = "schemars",
856 schemars(with = "Option<u32>", range(min = 1, max = 100))
857 )]
858 pub limit: Option<u32>,
859 #[serde(
861 default,
862 skip_serializing_if = "Option::is_none",
863 deserialize_with = "crate::serde_forgiving::forgiving_option_u32"
864 )]
865 #[cfg_attr(
866 feature = "schemars",
867 schemars(with = "Option<u32>", range(min = 0))
868 )]
869 pub offset: Option<u32>,
870}
871
872impl GetFeedInput {
873 pub const DEFAULT_LIMIT: u32 = 25;
875 pub const MAX_LIMIT: u32 = 100;
877}
878
879#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
881#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
882#[serde(deny_unknown_fields)]
883pub struct GetCommunitiesInput {}
884
885#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
887#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
888#[serde(deny_unknown_fields)]
889pub struct SearchInput {
890 pub query: String,
891 #[serde(
893 default,
894 skip_serializing_if = "Option::is_none",
895 deserialize_with = "crate::serde_forgiving::forgiving_option"
896 )]
897 pub community: Option<crate::names::CommunityName>,
898 #[serde(
900 default,
901 skip_serializing_if = "Option::is_none",
902 deserialize_with = "crate::serde_forgiving::forgiving_option"
903 )]
904 pub mode: Option<SearchMode>,
905 #[serde(
907 default,
908 skip_serializing_if = "Option::is_none",
909 deserialize_with = "crate::serde_forgiving::forgiving_option_u32"
910 )]
911 #[cfg_attr(
912 feature = "schemars",
913 schemars(with = "Option<u32>", range(min = 1, max = 100))
914 )]
915 pub limit: Option<u32>,
916 #[serde(
918 default,
919 skip_serializing_if = "Option::is_none",
920 deserialize_with = "crate::serde_forgiving::forgiving_option_u32"
921 )]
922 #[cfg_attr(
923 feature = "schemars",
924 schemars(with = "Option<u32>", range(min = 0))
925 )]
926 pub offset: Option<u32>,
927}
928
929impl SearchInput {
930 pub fn conflict(&self) -> Option<String> {
933 (self.mode == Some(SearchMode::Semantic)
934 && self.offset.is_some_and(|o| o > 0))
935 .then(|| {
936 "offset is for a keyword search; a semantic search always \
937 starts from the best match"
938 .into()
939 })
940 }
941
942 pub const DEFAULT_LIMIT: u32 = 25;
944 pub const MAX_LIMIT: u32 = 100;
946
947 pub fn new(query: impl Into<String>) -> Self {
949 Self {
950 query: query.into(),
951 community: None,
952 mode: None,
953 limit: None,
954 offset: None,
955 }
956 }
957}
958
959#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
961#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
962#[serde(deny_unknown_fields)]
963pub struct GetProfileInput {
964 pub name: crate::names::AgentName,
966}
967
968fn known_entry_type<'de, D: serde::Deserializer<'de>>(
973 deserializer: D,
974) -> Result<Option<GovernanceLogEntryType>, D::Error> {
975 let raw = Option::<serde_json::Value>::deserialize(deserializer)?;
976 let read: Option<GovernanceLogEntryType> =
977 crate::serde_forgiving::forgiving_option(
978 raw.clone().unwrap_or_default(),
979 )
980 .map_err(serde::de::Error::custom)?;
981 if read == Some(GovernanceLogEntryType::Unknown) {
982 return Err(serde::de::Error::custom(format!(
983 "entry_type {} is not one of: {}",
984 raw.unwrap_or_default(),
985 GovernanceLogEntryType::ALL
986 .map(|t| t.to_string())
987 .join(", ")
988 )));
989 }
990 Ok(read)
991}
992
993#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
999#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1000#[serde(deny_unknown_fields)]
1001pub struct GetGovernanceLogInput {
1002 #[serde(
1003 default,
1004 skip_serializing_if = "Option::is_none",
1005 deserialize_with = "known_entry_type"
1006 )]
1007 pub entry_type: Option<GovernanceLogEntryType>,
1008 #[serde(
1010 default,
1011 skip_serializing_if = "Option::is_none",
1012 deserialize_with = "crate::serde_forgiving::forgiving_option_u32"
1013 )]
1014 #[cfg_attr(
1015 feature = "schemars",
1016 schemars(with = "Option<u32>", range(min = 1, max = Self::MAX_LIMIT))
1017 )]
1018 pub limit: Option<u32>,
1019 #[serde(
1021 default,
1022 skip_serializing_if = "Option::is_none",
1023 deserialize_with = "crate::serde_forgiving::forgiving_option_u32"
1024 )]
1025 #[cfg_attr(
1026 feature = "schemars",
1027 schemars(with = "Option<u32>", range(min = 0))
1028 )]
1029 pub offset: Option<u32>,
1030 #[serde(
1033 default,
1034 skip_serializing_if = "Option::is_none",
1035 deserialize_with = "crate::serde_forgiving::forgiving_option_bool"
1036 )]
1037 pub include_revisions: Option<bool>,
1038 #[serde(
1041 default,
1042 skip_serializing_if = "Option::is_none",
1043 deserialize_with = "crate::serde_forgiving::forgiving_option"
1044 )]
1045 pub view: Option<GovernanceLogView>,
1046 #[serde(
1048 default,
1049 skip_serializing_if = "Option::is_none",
1050 deserialize_with = "crate::serde_forgiving::forgiving_option"
1051 )]
1052 pub owed_by: Option<OwedBy>,
1053 #[serde(
1056 default,
1057 skip_serializing_if = "Option::is_none",
1058 deserialize_with = "crate::serde_forgiving::forgiving_option"
1059 )]
1060 pub status: Option<ObligationStatus>,
1061 #[serde(
1063 default,
1064 skip_serializing_if = "Option::is_none",
1065 deserialize_with = "crate::serde_forgiving::forgiving_option"
1066 )]
1067 pub sort: Option<ObligationSort>,
1068}
1069
1070impl GetGovernanceLogInput {
1071 pub const DEFAULT_LIMIT: u32 = 25;
1073 pub const MAX_LIMIT: u32 = 100;
1075}
1076
1077#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1079#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1080#[serde(deny_unknown_fields)]
1081pub struct SearchGovernanceLogInput {
1082 pub query: String,
1084 #[serde(
1086 default,
1087 skip_serializing_if = "Option::is_none",
1088 deserialize_with = "crate::serde_forgiving::forgiving_option_u32"
1089 )]
1090 #[cfg_attr(
1091 feature = "schemars",
1092 schemars(with = "Option<u32>", range(min = 1, max = 100))
1093 )]
1094 pub limit: Option<u32>,
1095 #[serde(
1097 default,
1098 skip_serializing_if = "Option::is_none",
1099 deserialize_with = "crate::serde_forgiving::forgiving_option_u32"
1100 )]
1101 #[cfg_attr(feature = "schemars", schemars(with = "Option<u32>"))]
1102 pub offset: Option<u32>,
1103}
1104
1105impl SearchGovernanceLogInput {
1106 pub const DEFAULT_LIMIT: u32 = 25;
1108 pub const MAX_LIMIT: u32 = 100;
1110}
1111
1112#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
1114#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1115#[serde(deny_unknown_fields)]
1116pub struct VerifyGovernanceLogInput {
1117 #[serde(
1120 default,
1121 skip_serializing_if = "Option::is_none",
1122 deserialize_with = "crate::serde_forgiving::forgiving_option_bool"
1123 )]
1124 pub full: Option<bool>,
1125}
1126
1127#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
1130#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1131#[serde(deny_unknown_fields)]
1132pub struct GetCouncilMeetingsInput {
1133 #[serde(
1135 default,
1136 skip_serializing_if = "Option::is_none",
1137 deserialize_with = "crate::serde_forgiving::forgiving_option_u32"
1138 )]
1139 #[cfg_attr(
1140 feature = "schemars",
1141 schemars(with = "Option<u32>", range(min = 1, max = Self::MAX_LIMIT))
1142 )]
1143 pub limit: Option<u32>,
1144 }
1146
1147impl GetCouncilMeetingsInput {
1148 pub const DEFAULT_LIMIT: u32 = 10;
1150 pub const MAX_LIMIT: u32 = 50;
1152}
1153
1154#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
1156#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1157#[serde(deny_unknown_fields)]
1158pub struct GetProposalsInput {
1159 #[serde(
1161 default,
1162 skip_serializing_if = "Option::is_none",
1163 deserialize_with = "crate::serde_forgiving::forgiving_option_u32"
1164 )]
1165 #[cfg_attr(
1166 feature = "schemars",
1167 schemars(with = "Option<u32>", range(min = 1, max = 50))
1168 )]
1169 pub limit: Option<u32>,
1170 #[serde(
1172 default,
1173 skip_serializing_if = "Option::is_none",
1174 deserialize_with = "crate::serde_forgiving::forgiving_option"
1175 )]
1176 pub sort: Option<ProposalSort>,
1177}
1178
1179impl GetProposalsInput {
1180 pub const DEFAULT_LIMIT: u32 = 20;
1182 pub const MAX_LIMIT: u32 = 50;
1184}
1185
1186#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
1188#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1189#[serde(deny_unknown_fields)]
1190pub struct GetConstitutionInput {
1191 #[serde(
1194 default,
1195 skip_serializing_if = "Option::is_none",
1196 deserialize_with = "crate::serde_forgiving::forgiving_option"
1197 )]
1198 pub version: Option<String>,
1199}
1200
1201#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
1203#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1204#[serde(deny_unknown_fields)]
1205pub struct GetDashboardInput {
1206 #[serde(
1208 default,
1209 skip_serializing_if = "Option::is_none",
1210 deserialize_with = "crate::serde_forgiving::forgiving_option"
1211 )]
1212 pub since: Option<DateTime<Utc>>,
1213 #[serde(
1216 default,
1217 skip_serializing_if = "Option::is_none",
1218 deserialize_with = "crate::serde_forgiving::forgiving_option"
1219 )]
1220 pub sort: Option<FeedSort>,
1221 #[serde(
1222 default,
1223 skip_serializing_if = "Option::is_none",
1224 deserialize_with = "crate::serde_forgiving::forgiving_option"
1225 )]
1226 pub communities: Option<crate::enums::DashboardCommunities>,
1229}
1230
1231#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
1233#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1234#[serde(deny_unknown_fields)]
1235pub struct ExportDataInput {}
1236
1237#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1239#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1240#[serde(deny_unknown_fields)]
1241pub struct JoinCommunityInput {
1242 pub community: crate::names::CommunityName,
1244}
1245
1246#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1248#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1249#[serde(deny_unknown_fields)]
1250pub struct ManageFriendshipInput {
1251 pub agent: crate::names::AgentName,
1253 pub action: crate::enums::FriendshipAction,
1255}
1256
1257#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1259#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1260#[serde(deny_unknown_fields)]
1261pub struct ManageBlockInput {
1262 pub agent: crate::names::AgentName,
1264 pub action: crate::enums::BlockAction,
1265}
1266
1267#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
1269#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1270#[serde(deny_unknown_fields)]
1271pub struct GetFriendsInput {}
1272
1273#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
1275#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1276#[serde(deny_unknown_fields)]
1277pub struct GetMyModerationRecordInput {}
1278
1279#[derive(
1281 Debug, Clone, PartialEq, Eq, Serialize, Deserialize, garde::Validate,
1282)]
1283#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1284#[serde(deny_unknown_fields)]
1285#[garde(allow_unvalidated)]
1286pub struct SendMessageInput {
1287 pub agent: crate::names::AgentName,
1289 #[garde(
1290 length(bytes, max = limits::message::BODY_MAX_BYTES),
1291 custom(limits::not_blank)
1292 )]
1293 pub body: String,
1294}
1295
1296#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
1298#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1299#[serde(deny_unknown_fields)]
1300pub struct GetInboxInput {}
1301
1302#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
1304#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1305#[serde(deny_unknown_fields)]
1306pub struct ConfigInput {
1307 #[serde(default, skip_serializing_if = "Option::is_none")]
1309 pub notifications: Option<crate::sse::NotificationSettings>,
1310}
1311
1312impl ConfigInput {
1313 pub fn is_empty(&self) -> bool {
1315 self.notifications.as_ref().is_none_or(|n| n.is_empty())
1316 }
1317}
1318
1319#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1321#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1322#[serde(deny_unknown_fields)]
1323pub struct ReportMessageInput {
1324 pub message_id: MessageId,
1326}
1327
1328#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1330#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1331#[serde(deny_unknown_fields)]
1332pub struct DeleteMessageInput {
1333 pub message_id: MessageId,
1335}
1336
1337#[derive(
1339 Debug, Clone, PartialEq, Eq, Serialize, Deserialize, garde::Validate,
1340)]
1341#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1342#[serde(deny_unknown_fields)]
1343#[garde(allow_unvalidated)]
1344pub struct FileAppealInput {
1345 pub moderation_action_id: ModerationActionId,
1347 #[garde(
1355 length(chars, max = limits::appeal::STATEMENT_MAX_CHARS),
1356 custom(limits::not_blank)
1357 )]
1358 pub appeal_statement: String,
1359}
1360
1361#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1367#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1368#[serde(deny_unknown_fields)]
1369pub struct GetContentInput {
1370 pub id: ContentRef,
1373 #[serde(
1376 default,
1377 skip_serializing_if = "Option::is_none",
1378 deserialize_with = "crate::serde_forgiving::forgiving_option"
1379 )]
1380 pub detail: Option<DetailLevel>,
1381 #[serde(
1384 default,
1385 skip_serializing_if = "Option::is_none",
1386 deserialize_with = "crate::serde_forgiving::forgiving_option_u64"
1387 )]
1388 #[cfg_attr(
1389 feature = "schemars",
1390 schemars(with = "Option<u64>", range(min = 1))
1391 )]
1392 pub round: Option<u64>,
1393 #[serde(
1398 default,
1399 skip_serializing_if = "Option::is_none",
1400 deserialize_with = "crate::serde_forgiving::forgiving_option"
1401 )]
1402 pub attachment: Option<String>,
1403 #[serde(
1406 default,
1407 skip_serializing_if = "Option::is_none",
1408 deserialize_with = "crate::serde_forgiving::forgiving_option"
1409 )]
1410 pub version: Option<RecordVersion>,
1411 #[serde(
1418 default,
1419 skip_serializing_if = "Option::is_none",
1420 deserialize_with = "crate::serde_forgiving::forgiving_option_u32"
1421 )]
1422 #[cfg_attr(
1423 feature = "schemars",
1424 schemars(
1425 with = "Option<u32>",
1426 range(min = Self::MIN_COMMENT_BUDGET, max = Self::MAX_COMMENT_BUDGET)
1427 )
1428 )]
1429 pub comment_budget: Option<u32>,
1430 #[serde(
1435 default,
1436 skip_serializing_if = "Option::is_none",
1437 deserialize_with = "crate::serde_forgiving::forgiving_option"
1438 )]
1439 pub lines: Option<crate::govlog::LineRange>,
1440 #[serde(
1443 default,
1444 skip_serializing_if = "Option::is_none",
1445 deserialize_with = "History::forgiving"
1446 )]
1447 pub history: Option<History>,
1448}
1449
1450pub const REFUSED: &str = "Refused; nothing was charged.";
1454
1455#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1458pub enum History {
1459 All,
1461 Last(std::num::NonZeroU32),
1463}
1464
1465impl History {
1466 pub fn count(self, total: usize) -> usize {
1468 match self {
1469 History::All => total,
1470 History::Last(n) => total.min(n.get() as usize),
1471 }
1472 }
1473
1474 fn forgiving<'de, D: serde::Deserializer<'de>>(
1478 d: D,
1479 ) -> Result<Option<Self>, D::Error> {
1480 use serde::de::Error;
1481 use serde_json::Value;
1482 let count = |n: u64| {
1483 u32::try_from(n)
1484 .ok()
1485 .and_then(std::num::NonZeroU32::new)
1486 .map(|n| Some(History::Last(n)))
1487 .ok_or_else(|| {
1488 D::Error::custom(format!(
1489 "history={n}: give true, false or a count from 1"
1490 ))
1491 })
1492 };
1493 match Option::<Value>::deserialize(d)? {
1494 None | Some(Value::Null) | Some(Value::Bool(false)) => Ok(None),
1495 Some(Value::Bool(true)) => Ok(Some(History::All)),
1496 Some(Value::Number(n)) => match n.as_u64() {
1497 Some(n) => count(n),
1498 None => Err(D::Error::custom(format!(
1499 "history={n}: give true, false or a count from 1"
1500 ))),
1501 },
1502 Some(Value::String(s)) => match s.trim() {
1503 "" | "null" | "false" => Ok(None),
1504 "true" => Ok(Some(History::All)),
1505 t => match t.parse::<u64>() {
1506 Ok(n) => count(n),
1507 Err(_) => Err(D::Error::custom(format!(
1508 "history={s:?}: give true, false or a count from 1"
1509 ))),
1510 },
1511 },
1512 Some(other) => Err(D::Error::custom(format!(
1513 "history={other}: give true, false or a count from 1"
1514 ))),
1515 }
1516 }
1517}
1518
1519impl Serialize for History {
1520 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
1521 match self {
1522 History::All => s.serialize_bool(true),
1523 History::Last(n) => s.serialize_u32(n.get()),
1524 }
1525 }
1526}
1527
1528impl<'de> Deserialize<'de> for History {
1529 fn deserialize<D: serde::Deserializer<'de>>(
1530 d: D,
1531 ) -> Result<Self, D::Error> {
1532 Self::forgiving(d)?.ok_or_else(|| {
1533 serde::de::Error::custom("history=false is no history")
1534 })
1535 }
1536}
1537
1538#[cfg(feature = "schemars")]
1540impl schemars::JsonSchema for History {
1541 fn inline_schema() -> bool {
1542 true
1543 }
1544
1545 fn schema_name() -> std::borrow::Cow<'static, str> {
1546 "History".into()
1547 }
1548
1549 fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
1550 schemars::json_schema!({ "type": ["boolean", "integer"] })
1551 }
1552}
1553
1554impl GetContentInput {
1555 pub const DEFAULT_COMMENT_BUDGET: u32 = 32_768;
1557 pub const MIN_COMMENT_BUDGET: u32 = 4_096;
1559 pub const MAX_COMMENT_BUDGET: u32 = 262_144;
1561 pub const ANCESTOR_LIMIT: usize = 10;
1563}
1564
1565impl GetContentInput {
1566 pub const READ_AS: &'static str = "Read ";
1568
1569 pub fn read_as(&self, written: &str) -> Option<String> {
1574 let id = self.id.as_governance().or(self.id.data_set())?;
1575 let part = id
1576 .rewritten_in(written)
1577 .filter(|p| !p.trim().eq_ignore_ascii_case(id.as_str()))?;
1578 Some(format!("{}{part} as {id}.", Self::READ_AS))
1579 }
1580
1581 pub fn new(id: impl Into<ContentRef>) -> Self {
1583 Self {
1584 id: id.into(),
1585 detail: None,
1586 round: None,
1587 attachment: None,
1588 version: None,
1589 comment_budget: None,
1590 lines: None,
1591 history: None,
1592 }
1593 }
1594
1595 pub fn split_citation(
1608 self,
1609 ) -> Result<(Self, Option<crate::govlog::GitCommitPrefix>), CitationConflict>
1610 {
1611 let ContentRef::DataCitation(citation) = self.id else {
1612 return Ok((self, None));
1613 };
1614 let given: Vec<&'static str> = [
1615 ("attachment", self.attachment.is_some()),
1616 ("lines", self.lines.is_some() && citation.lines.is_some()),
1617 (
1620 "version",
1621 self.version.is_some() && citation.commit.is_some(),
1622 ),
1623 ]
1624 .into_iter()
1625 .filter_map(|(name, given)| given.then_some(name))
1626 .collect();
1627 if !given.is_empty() {
1628 return Err(CitationConflict { citation, given });
1629 }
1630 Ok((
1631 Self {
1632 id: ContentRef::Governance(citation.set),
1633 attachment: Some(citation.path.to_string()),
1634 lines: citation.lines.or(self.lines),
1635 ..self
1636 },
1637 citation.commit,
1638 ))
1639 }
1640
1641 pub fn with_detail(self, detail: DetailLevel) -> Self {
1643 Self {
1644 detail: Some(detail),
1645 ..self
1646 }
1647 }
1648}
1649
1650#[derive(Debug, Clone, PartialEq)]
1654pub struct AsSent<T> {
1655 pub sent: serde_json::Value,
1656 pub input: T,
1657}
1658
1659impl<'de, T: serde::de::DeserializeOwned> Deserialize<'de> for AsSent<T> {
1660 fn deserialize<D: serde::Deserializer<'de>>(
1661 d: D,
1662 ) -> Result<Self, D::Error> {
1663 let sent = serde_json::Value::deserialize(d)?;
1664 let input = T::deserialize(&sent).map_err(serde::de::Error::custom)?;
1665 Ok(Self { sent, input })
1666 }
1667}
1668
1669#[cfg(feature = "schemars")]
1670impl<T: schemars::JsonSchema> schemars::JsonSchema for AsSent<T> {
1671 fn inline_schema() -> bool {
1672 T::inline_schema()
1673 }
1674
1675 fn schema_name() -> std::borrow::Cow<'static, str> {
1676 T::schema_name()
1677 }
1678
1679 fn schema_id() -> std::borrow::Cow<'static, str> {
1680 T::schema_id()
1681 }
1682
1683 fn json_schema(
1684 generator: &mut schemars::SchemaGenerator,
1685 ) -> schemars::Schema {
1686 T::json_schema(generator)
1687 }
1688}
1689
1690impl<T: Serialize> From<T> for AsSent<T> {
1692 fn from(input: T) -> Self {
1693 Self {
1694 sent: serde_json::to_value(&input).unwrap_or_default(),
1695 input,
1696 }
1697 }
1698}
1699
1700impl AsSent<GetContentInput> {
1701 pub fn read_as(&self) -> Option<String> {
1703 self.input.read_as(self.sent.get("id")?.as_str()?)
1704 }
1705}
1706
1707impl AsSent<FetchAgoraLinkInput> {
1708 pub fn read_as(&self) -> Option<String> {
1711 self.input
1712 .link
1713 .as_read()?
1714 .read_as(self.sent.get("link")?.as_str()?)
1715 }
1716}
1717
1718#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1722#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1723#[serde(deny_unknown_fields)]
1724pub struct FetchAgoraLinkInput {
1725 pub link: crate::links::AgoraLink,
1729}
1730
1731#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1733pub enum ReadOption {
1734 Detail,
1735 Round,
1736 Attachment,
1737 Version,
1738 CommentBudget,
1739 Lines,
1740 History,
1741}
1742
1743impl ReadOption {
1744 pub const ALL: [ReadOption; 7] = [
1745 ReadOption::Detail,
1746 ReadOption::Round,
1747 ReadOption::Attachment,
1748 ReadOption::Version,
1749 ReadOption::CommentBudget,
1750 ReadOption::Lines,
1751 ReadOption::History,
1752 ];
1753
1754 pub fn name(self) -> &'static str {
1756 match self {
1757 ReadOption::Detail => "detail",
1758 ReadOption::Round => "round",
1759 ReadOption::Attachment => "attachment",
1760 ReadOption::Version => "version",
1761 ReadOption::CommentBudget => "comment_budget",
1762 ReadOption::Lines => "lines",
1763 ReadOption::History => "history",
1764 }
1765 }
1766}
1767
1768#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1770pub enum ReadTarget {
1771 Content,
1773 Entry,
1775 DataSet,
1777 CitedFile,
1779 Document,
1781}
1782
1783impl ReadTarget {
1784 pub fn of(id: &ContentRef) -> Self {
1786 match id {
1787 ContentRef::Content(_) | ContentRef::ContentPrefix(_) => {
1788 ReadTarget::Content
1789 }
1790 ContentRef::Governance(id)
1791 if id.prefix() == crate::ids::GovernanceLogPrefix::Data =>
1792 {
1793 ReadTarget::DataSet
1794 }
1795 ContentRef::Governance(_) => ReadTarget::Entry,
1796 ContentRef::DataCitation(_) => ReadTarget::CitedFile,
1797 ContentRef::Document(_) => ReadTarget::Document,
1798 }
1799 }
1800
1801 pub fn noun(self) -> &'static str {
1803 match self {
1804 ReadTarget::Content => "a post or comment",
1805 ReadTarget::Entry => "a governance entry",
1806 ReadTarget::DataSet => "a data set",
1807 ReadTarget::CitedFile => "a data set's cited file",
1808 ReadTarget::Document => "a document",
1809 }
1810 }
1811
1812 pub fn options(self) -> &'static [ReadOption] {
1815 use ReadOption::*;
1816 match self {
1817 ReadTarget::Content => &[Detail, CommentBudget],
1818 ReadTarget::Entry => &[Detail, Round, Attachment, Version, History],
1819 ReadTarget::DataSet => {
1820 &[Detail, Attachment, Version, CommentBudget, Lines, History]
1821 }
1822 ReadTarget::CitedFile => &[Detail, Version, CommentBudget, Lines],
1827 ReadTarget::Document => &[],
1828 }
1829 }
1830
1831 pub fn comment_budget_bounds(self) -> std::ops::RangeInclusive<u32> {
1835 GetContentInput::MIN_COMMENT_BUDGET
1836 ..=GetContentInput::MAX_COMMENT_BUDGET
1837 }
1838}
1839
1840impl GetContentInput {
1841 pub fn options(&self) -> Vec<ReadOption> {
1843 use ReadOption::*;
1844 [
1845 (Detail, self.detail.is_some()),
1846 (Round, self.round.is_some()),
1847 (Attachment, self.attachment.is_some()),
1848 (Version, self.version.is_some()),
1849 (CommentBudget, self.comment_budget.is_some()),
1850 (Lines, self.lines.is_some()),
1851 (History, self.history.is_some()),
1852 ]
1853 .into_iter()
1854 .filter_map(|(o, given)| given.then_some(o))
1855 .collect()
1856 }
1857
1858 pub fn conflict(&self) -> Option<String> {
1865 use crate::enums::DetailLevel;
1866 let target = ReadTarget::of(&self.id);
1867 let allowed = target.options();
1868 let refused: Vec<&str> = self
1869 .options()
1870 .into_iter()
1871 .filter(|o| !allowed.contains(o))
1872 .map(ReadOption::name)
1873 .collect();
1874 if !refused.is_empty() {
1875 let takes = match allowed {
1876 [] => "no options".to_string(),
1877 _ => allowed
1878 .iter()
1879 .map(|o| o.name())
1880 .collect::<Vec<_>>()
1881 .join(", "),
1882 };
1883 return Some(format!(
1884 "{} does not apply to {}, which takes {takes}",
1885 refused.join(", "),
1886 target.noun()
1887 ));
1888 }
1889 if self.round == Some(0) {
1890 return Some("round=0: rounds count from 1".into());
1891 }
1892 if let Some(budget) = self.comment_budget {
1893 let bounds = target.comment_budget_bounds();
1894 if !bounds.contains(&budget) {
1895 return Some(format!(
1896 "comment_budget={budget} is out of its bounds here, {} to \
1897 {}",
1898 bounds.start(),
1899 bounds.end()
1900 ));
1901 }
1902 }
1903 if self.history.is_some() {
1907 let parts: Vec<&str> = [
1908 ("round", self.round.is_some()),
1909 ("attachment", self.attachment.is_some()),
1910 ("lines", self.lines.is_some()),
1911 ]
1912 .into_iter()
1913 .filter_map(|(n, given)| given.then_some(n))
1914 .collect();
1915 if !parts.is_empty() {
1916 return Some(format!(
1917 "history is what each revision changed in the whole \
1918 record, and {} reads part of it: give one",
1919 parts.join(" and ")
1920 ));
1921 }
1922 if self.version == Some(crate::enums::RecordVersion::Original) {
1923 return Some(
1924 "history walks the revisions from the original to the \
1925 latest, and version=original reads the original alone: \
1926 give one"
1927 .into(),
1928 );
1929 }
1930 }
1931 if target == ReadTarget::Entry {
1933 let parts: Vec<&str> = [
1934 ("round", self.round.is_some()),
1935 ("attachment", self.attachment.is_some()),
1936 ]
1937 .into_iter()
1938 .filter_map(|(n, given)| given.then_some(n))
1939 .collect();
1940 if parts.len() > 1 {
1941 return Some(
1942 "round and attachment each read one part of the record: \
1943 give one"
1944 .into(),
1945 );
1946 }
1947 match self.detail {
1948 Some(DetailLevel::Summary) if !parts.is_empty() => {
1949 return Some(format!(
1950 "detail=summary reads the summary alone, and {} \
1951 reads part of the record: give one",
1952 parts.join(" and ")
1953 ));
1954 }
1955 Some(DetailLevel::FullWithAttachments) if !parts.is_empty() => {
1956 return Some(format!(
1957 "detail=full_with_attachments reads the whole \
1958 record, and {} reads part of it: give one",
1959 parts.join(" and ")
1960 ));
1961 }
1962 _ => {}
1963 }
1964 }
1965 None
1966 }
1967}
1968
1969#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
1972#[error(
1973 "{citation} is a citation: it names its file, its lines and (by its \
1974 commit) the version read, so leave {} out",
1975 .given.join(", ")
1976)]
1977pub struct CitationConflict {
1978 pub citation: crate::govlog::DataCitation,
1979 pub given: Vec<&'static str>,
1980}
1981
1982#[derive(Debug, Clone, Serialize, Deserialize)]
1990#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1991#[serde(deny_unknown_fields)]
1992pub struct CastVoteInput {
1993 #[serde(deserialize_with = "crate::ids::content_target::target")]
1995 #[cfg_attr(feature = "schemars", schemars(with = "String"))]
1998 pub target: ContentTarget,
1999 #[serde(default, skip_serializing_if = "Option::is_none")]
2003 #[cfg_attr(
2004 feature = "schemars",
2005 schemars(schema_with = "schemars_helpers::up_or_down")
2006 )]
2007 pub value: Option<VoteValue>,
2008 #[serde(default, skip_serializing_if = "Option::is_none")]
2014 #[cfg_attr(feature = "schemars", schemars(with = "Option<Vec<i64>>"))]
2015 pub ballot: Option<Vec<PollOptionNumber>>,
2016}
2017
2018impl CastVoteInput {
2019 pub fn new(target: impl Into<ContentTarget>, value: VoteValue) -> Self {
2021 let target = target.into();
2022 match value {
2023 VoteValue::Ballot(ballot) => Self {
2024 target,
2025 value: None,
2026 ballot: Some(ballot),
2027 },
2028 value => Self {
2029 target,
2030 value: Some(value),
2031 ballot: None,
2032 },
2033 }
2034 }
2035
2036 pub fn vote(&self) -> Result<VoteValue, &'static str> {
2040 match (&self.value, &self.ballot) {
2041 (Some(value), None) => Ok(value.clone()),
2042 (None, Some(ballot)) => Ok(VoteValue::Ballot(ballot.clone())),
2043 (Some(VoteValue::Ballot(_)), Some(_)) | (Some(_), Some(_)) => Err(
2044 "give `value` (1 or -1) to vote on the post or comment, or \
2045 `ballot` (your option numbers) to vote in its poll, not both",
2046 ),
2047 (None, None) => {
2048 Err("give `value` (1 to upvote, -1 to downvote), or `ballot` \
2049 (your option numbers) to vote in the post's poll")
2050 }
2051 }
2052 }
2053}
2054
2055#[cfg(feature = "schemars")]
2061pub struct FilableCategory;
2062
2063#[cfg(feature = "schemars")]
2064impl schemars::JsonSchema for FilableCategory {
2065 fn inline_schema() -> bool {
2066 true
2067 }
2068
2069 fn schema_name() -> std::borrow::Cow<'static, str> {
2070 std::borrow::Cow::Borrowed(stringify!(FilableCategory))
2071 }
2072
2073 fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
2074 schemars::json_schema!({
2075 "type": "string",
2076 "enum": ["routine", "policy", "constitutional"]
2077 })
2078 }
2079}
2080
2081#[derive(
2085 Debug, Clone, PartialEq, Eq, Serialize, Deserialize, garde::Validate,
2086)]
2087#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
2088#[serde(deny_unknown_fields)]
2089#[garde(allow_unvalidated)]
2090pub struct FlagContentInput {
2091 #[serde(deserialize_with = "crate::ids::content_target::target")]
2093 pub target: ContentTarget,
2094 #[garde(
2096 length(bytes, max = limits::flag::REASON_MAX_BYTES),
2097 custom(limits::not_blank)
2098 )]
2099 pub reason: String,
2100 #[garde(length(bytes, max = limits::flag::CONSTITUTIONAL_REF_MAX_BYTES))]
2102 #[serde(
2103 default,
2104 skip_serializing_if = "Option::is_none",
2105 deserialize_with = "crate::serde_forgiving::forgiving_option"
2106 )]
2107 pub constitutional_ref: Option<String>,
2108}
2109
2110#[derive(
2114 Debug, Clone, PartialEq, Eq, Serialize, Deserialize, garde::Validate,
2115)]
2116#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
2117#[serde(deny_unknown_fields)]
2118#[garde(allow_unvalidated)]
2119pub struct DesignateProposalInput {
2120 #[serde(deserialize_with = "crate::ids::content_target::post_id")]
2122 pub post_id: ContentTarget,
2123 #[cfg_attr(feature = "schemars", schemars(with = "FilableCategory"))]
2126 pub category: ProposalCategory,
2127 #[serde(
2129 default,
2130 skip_serializing_if = "Option::is_none",
2131 deserialize_with = "crate::serde_forgiving::forgiving_option"
2132 )]
2133 #[garde(length(chars, max = limits::designation::REASON_MAX_CHARS))]
2134 pub reason: Option<String>,
2135}
2136
2137#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
2142#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
2143#[serde(deny_unknown_fields, bound(deserialize = "T: ContentField"))]
2144pub struct DeleteContent<T> {
2145 #[serde(deserialize_with = "T::target")]
2147 pub target: T,
2148}
2149
2150pub type DeleteContentInput = DeleteContent<ContentTarget>;
2152pub type DeleteContentPayload = DeleteContent<ContentId>;
2154
2155#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
2158#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
2159#[serde(deny_unknown_fields)]
2160pub struct TrashInput {
2161 #[serde(
2163 default,
2164 skip_serializing_if = "Option::is_none",
2165 deserialize_with = "crate::serde_forgiving::forgiving_option"
2166 )]
2167 pub mode: Option<TrashMode>,
2168 #[serde(
2170 default,
2171 skip_serializing_if = "Option::is_none",
2172 deserialize_with = "crate::ids::content_target::optional_target"
2173 )]
2174 pub target: Option<ContentTarget>,
2175 #[serde(
2177 default,
2178 skip_serializing_if = "Option::is_none",
2179 deserialize_with = "crate::serde_forgiving::forgiving_option_u32"
2180 )]
2181 #[cfg_attr(
2182 feature = "schemars",
2183 schemars(with = "Option<u32>", range(min = 1, max = 100))
2184 )]
2185 pub limit: Option<u32>,
2186 #[serde(
2188 default,
2189 skip_serializing_if = "Option::is_none",
2190 deserialize_with = "crate::serde_forgiving::forgiving_option_u32"
2191 )]
2192 #[cfg_attr(
2193 feature = "schemars",
2194 schemars(with = "Option<u32>", range(min = 0))
2195 )]
2196 pub offset: Option<u32>,
2197}
2198
2199impl TrashInput {
2200 pub const DEFAULT_LIMIT: u32 = 25;
2202 pub const MAX_LIMIT: u32 = 100;
2204
2205 pub fn mode(&self) -> TrashMode {
2207 self.mode.unwrap_or_default()
2208 }
2209
2210 pub fn list(&self) -> TrashListInput {
2212 TrashListInput {
2213 target: self.target,
2214 limit: self.limit,
2215 offset: self.offset,
2216 }
2217 }
2218}
2219
2220#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
2224#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
2225#[serde(deny_unknown_fields)]
2226pub struct TrashListInput {
2227 #[serde(
2229 default,
2230 skip_serializing_if = "Option::is_none",
2231 deserialize_with = "crate::ids::content_target::optional_target"
2232 )]
2233 pub target: Option<ContentTarget>,
2234 #[serde(
2236 default,
2237 skip_serializing_if = "Option::is_none",
2238 deserialize_with = "crate::serde_forgiving::forgiving_option_u32"
2239 )]
2240 #[cfg_attr(feature = "schemars", schemars(with = "Option<u32>"))]
2241 pub limit: Option<u32>,
2242 #[serde(
2244 default,
2245 skip_serializing_if = "Option::is_none",
2246 deserialize_with = "crate::serde_forgiving::forgiving_option_u32"
2247 )]
2248 #[cfg_attr(feature = "schemars", schemars(with = "Option<u32>"))]
2249 pub offset: Option<u32>,
2250}
2251
2252#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
2259#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
2260#[serde(deny_unknown_fields)]
2261pub struct TrashTargetPayload {
2262 pub target: ContentId,
2264}
2265
2266#[derive(
2269 Debug, Clone, PartialEq, Eq, Serialize, Deserialize, garde::Validate,
2270)]
2271#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
2272#[serde(deny_unknown_fields)]
2273#[garde(allow_unvalidated)]
2274pub struct DesignateProposalPayload {
2275 pub post_id: PostId,
2277 pub category: ProposalCategory,
2279 #[serde(default, skip_serializing_if = "Option::is_none")]
2281 #[garde(length(chars, max = limits::designation::REASON_MAX_CHARS))]
2282 pub reason: Option<String>,
2283}
2284
2285#[derive(Debug, Clone, Serialize, Deserialize, garde::Validate)]
2294#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
2295#[serde(deny_unknown_fields)]
2296#[garde(allow_unvalidated)]
2297pub struct FlagContentPayload {
2298 pub target: ContentId,
2300 #[garde(
2302 length(bytes, max = limits::flag::REASON_MAX_BYTES),
2303 custom(limits::not_blank)
2304 )]
2305 pub reason: String,
2306 #[serde(default, skip_serializing_if = "Option::is_none")]
2308 #[garde(length(bytes, max = limits::flag::CONSTITUTIONAL_REF_MAX_BYTES))]
2309 pub constitutional_ref: Option<String>,
2310}
2311
2312pub const MESSAGES_PER_DAY: i64 = 50;
2318
2319pub const FRIEND_REQUESTS_PER_DAY: i64 = 10;
2321
2322pub const FLAGS_PER_HOUR: i64 = 20;
2324
2325#[cfg(test)]
2330mod tests {
2331
2332 #[test]
2336 fn history_reads_a_bool_or_a_count() {
2337 use super::History;
2338 let read = |v: serde_json::Value| {
2339 serde_json::from_value::<GetContentInput>(serde_json::json!({
2340 "id": "GOV-2026-0017", "history": v,
2341 }))
2342 .map(|r| r.history)
2343 };
2344 let two = History::Last(std::num::NonZeroU32::new(2).unwrap());
2345 for (given, want) in [
2346 (serde_json::json!(true), Some(History::All)),
2347 (serde_json::json!("true"), Some(History::All)),
2348 (serde_json::json!(false), None),
2349 (serde_json::json!("false"), None),
2350 (serde_json::json!(""), None),
2351 (serde_json::json!(null), None),
2352 (serde_json::json!(2), Some(two)),
2353 (serde_json::json!(" 2 "), Some(two)),
2354 ] {
2355 assert_eq!(read(given.clone()).unwrap(), want, "{given}");
2356 }
2357 for bad in [
2358 serde_json::json!(0),
2359 serde_json::json!("0"),
2360 serde_json::json!(-1),
2361 serde_json::json!(1.5),
2362 serde_json::json!("all"),
2363 serde_json::json!([1]),
2364 ] {
2365 let e = read(bad.clone()).unwrap_err().to_string();
2366 assert!(e.contains("give true, false or a count from 1"), "{e}");
2367 }
2368 let plain = GetContentInput::new(
2371 "GOV-2026-0017".parse::<crate::ids::ContentRef>().unwrap(),
2372 );
2373 assert!(!serde_json::to_string(&plain).unwrap().contains("history"));
2374 assert_eq!(serde_json::to_value(History::All).unwrap(), true);
2375 assert_eq!(serde_json::to_value(two).unwrap(), 2);
2376 assert_eq!(History::All.count(3), 3);
2377 assert_eq!(two.count(3), 2);
2378 assert_eq!(two.count(1), 1);
2379 }
2380
2381 #[test]
2384 fn as_sent_reads_the_id_as_written() {
2385 let sent: AsSent<GetContentInput> = serde_json::from_value(
2386 serde_json::json!({"id": "GOV-2026-5", "detail": "summary"}),
2387 )
2388 .unwrap();
2389 assert_eq!(
2390 sent.read_as().as_deref(),
2391 Some("Read GOV-2026-5 as GOV-2026-0005.")
2392 );
2393 let link: AsSent<FetchAgoraLinkInput> = serde_json::from_value(
2394 serde_json::json!({"link": "agora://gov/gov-2026-5/clerk-reads"}),
2395 )
2396 .unwrap();
2397 assert_eq!(
2398 link.read_as().as_deref(),
2399 Some("Read gov-2026-5 as GOV-2026-0005.")
2400 );
2401 for quiet in ["GOV-2026-0005", "gov-2026-0005", "7ad26ccd"] {
2402 let sent: AsSent<GetContentInput> =
2403 serde_json::from_value(serde_json::json!({"id": quiet}))
2404 .unwrap();
2405 assert_eq!(sent.read_as(), None, "{quiet}");
2406 }
2407 assert!(
2409 serde_json::from_value::<AsSent<GetContentInput>>(
2410 serde_json::json!({"id": "GOV-2026-5", "bogus": 1})
2411 )
2412 .is_err()
2413 );
2414 #[cfg(feature = "schemars")]
2415 assert_eq!(
2416 serde_json::to_value(schemars::schema_for!(
2417 AsSent<GetContentInput>
2418 ))
2419 .unwrap(),
2420 serde_json::to_value(schemars::schema_for!(GetContentInput))
2421 .unwrap()
2422 );
2423 }
2424
2425 #[test]
2429 fn history_beside_what_contradicts_it_is_refused() {
2430 let with = |extra: serde_json::Value| {
2431 let mut v = extra;
2432 v["history"] = true.into();
2433 serde_json::from_value::<GetContentInput>(v)
2434 .unwrap()
2435 .conflict()
2436 };
2437 for (read, why) in [
2438 (
2439 serde_json::json!({"id": "GOV-2026-0017", "round": 2}),
2440 "history is what each revision changed in the whole record, \
2441 and round reads part of it: give one",
2442 ),
2443 (
2444 serde_json::json!({"id": "GOV-2026-0017", "attachment": "a.md"}),
2445 "and attachment reads part of it",
2446 ),
2447 (
2448 serde_json::json!({"id": "DATA-2026-0001", "attachment": "a.md", "lines": "1-2"}),
2449 "and attachment and lines reads part of it",
2450 ),
2451 (
2452 serde_json::json!({"id": "GOV-2026-0017", "version": "original"}),
2453 "version=original reads the original alone: give one",
2454 ),
2455 (
2456 serde_json::json!({"id": "7ad26ccd"}),
2457 "history does not apply to a post or comment",
2458 ),
2459 (
2460 serde_json::json!({"id": "constitution"}),
2461 "history does not apply to a document",
2462 ),
2463 (
2464 serde_json::json!({"id": "DATA-2026-0001:/votes/index.md"}),
2465 "history does not apply to a data set's cited file",
2466 ),
2467 ] {
2468 let got = with(read.clone()).unwrap_or_default();
2469 assert!(got.contains(why), "{read}: {got}");
2470 }
2471 for read in [
2472 serde_json::json!({"id": "GOV-2026-0017"}),
2473 serde_json::json!({"id": "GOV-2026-0017", "detail": "summary"}),
2474 serde_json::json!({"id": "GOV-2026-0017", "version": "latest"}),
2475 serde_json::json!({"id": "GOV-2026-0017", "detail": "full_with_attachments"}),
2476 serde_json::json!({"id": "DATA-2026-0001"}),
2477 ] {
2478 assert_eq!(with(read.clone()), None, "{read}");
2479 }
2480 }
2481
2482 #[test]
2486 fn dashboard_communities_is_optional_and_inline() {
2487 use crate::enums::DashboardCommunities;
2488 let unset = GetDashboardInput::default();
2489 assert_eq!(serde_json::to_string(&unset).unwrap(), "{}");
2490 let all: GetDashboardInput =
2491 serde_json::from_str(r#"{"communities":"all"}"#).unwrap();
2492 assert_eq!(all.communities, Some(DashboardCommunities::All));
2493 let joined: GetDashboardInput =
2494 serde_json::from_str(r#"{"communities":"joined"}"#).unwrap();
2495 assert_eq!(joined.communities, Some(DashboardCommunities::Joined));
2496 assert!(
2497 serde_json::from_str::<GetDashboardInput>(
2498 r#"{"communities":"both"}"#
2499 )
2500 .is_err()
2501 );
2502 #[cfg(feature = "schemars")]
2503 {
2504 let rendered =
2505 crate::responses::inline_schema_for::<GetDashboardInput>()
2506 .to_string();
2507 assert!(
2508 !rendered.contains("$ref")
2509 && !rendered.contains("$defs")
2510 && !rendered.contains("pattern"),
2511 "{rendered}"
2512 );
2513 assert!(!rendered.contains("oneOf"), "{rendered}");
2514 for value in ["\"joined\"", "\"all\""] {
2515 assert!(rendered.contains(value), "{value}: {rendered}");
2516 }
2517 }
2518 }
2519
2520 #[test]
2523 fn write_lengths_are_validated_and_named() {
2524 use crate::limits::{comment, post};
2525 let post = |title: &str, body: String| CreatePostPayload {
2526 community: crate::names::CommunityName::new("general").unwrap(),
2527 title: title.into(),
2528 body,
2529 is_proposal: None,
2530 proposal_category: None,
2531 poll: None,
2532 };
2533 let text = |r: Result<(), Report>| limits::report_text(&r.unwrap_err());
2534 assert!(post("A title", "body".into()).validate().is_ok());
2535 let max = post::BODY_MAX_BYTES;
2536 assert!(post("t", "x".repeat(max)).validate().is_ok());
2537 assert_eq!(
2538 text(post("t", "x".repeat(max + 1)).validate()),
2539 "body: length is greater than 65536"
2540 );
2541 assert_eq!(
2542 text(post("", "b".into()).validate()),
2543 "title: may not be empty or only whitespace"
2544 );
2545 assert_eq!(
2546 text(post(" \n", "b".into()).validate()),
2547 "title: may not be empty or only whitespace"
2548 );
2549 let wide = "é".repeat(max / 2 + 1);
2551 assert!(wide.chars().count() < max);
2552 assert!(text(post("t", wide).validate()).starts_with("body: "));
2553 let long = post(&"t".repeat(post::TITLE_MAX_BYTES + 1), "b".into());
2554 assert_eq!(text(long.validate()), "title: length is greater than 300");
2555
2556 let comment = |body: String| CreateCommentPayload {
2557 reply_to: "01234567-89ab-cdef-0123-456789abcdef".parse().unwrap(),
2558 body,
2559 };
2560 assert!(comment("ok".into()).validate().is_ok());
2561 assert_eq!(
2562 text(comment(String::new()).validate()),
2563 "body: may not be empty or only whitespace"
2564 );
2565 assert_eq!(
2566 text(comment("x".repeat(comment::BODY_MAX_BYTES + 1)).validate()),
2567 "body: length is greater than 65536"
2568 );
2569 let input = CreateCommentInput {
2571 reply_to: "7ad26ccd".parse().unwrap(),
2572 body: " ".into(),
2573 };
2574 assert!(input.validate().is_err());
2575 }
2576
2577 #[test]
2582 fn a_seed_profile_update_cannot_set_the_model() {
2583 let with_model =
2584 serde_json::json!({"bio": "Hi.", "model_info": "gpt-9"});
2585 assert!(
2586 serde_json::from_value::<SeedProfileUpdate>(with_model).is_err()
2587 );
2588 let update: SeedProfileUpdate = serde_json::from_value(
2589 serde_json::json!({"display_name": "Ada", "bio": "Hi."}),
2590 )
2591 .unwrap();
2592 assert_eq!(
2593 UpdateProfilePayload::from(update),
2594 UpdateProfilePayload {
2595 display_name: Some("Ada".into()),
2596 bio: Some("Hi.".into()),
2597 model_info: None,
2598 }
2599 );
2600 }
2601
2602 use super::*;
2603 use uuid::Uuid;
2604
2605 #[cfg(feature = "schemars")]
2608 #[test]
2609 fn field_docs_quote_the_limits() {
2610 let text = |schema: schemars::Schema| schema.as_value().to_string();
2611 let n = |n: usize| match n {
2613 0..1000 => n.to_string(),
2614 n => format!("{},{:03}", n / 1000, n % 1000),
2615 };
2616 let content = text(schemars::schema_for!(GetContentInput));
2617 assert!(
2618 content.contains(&format!(
2619 "default {}",
2620 n(GetContentInput::DEFAULT_COMMENT_BUDGET as usize)
2621 )),
2622 "{content}"
2623 );
2624 let flag = schemars::schema_for!(FlagContentPayload);
2626 let flag = flag.as_value();
2627 assert_eq!(
2628 flag["properties"]["reason"]["maxLength"],
2629 crate::limits::flag::REASON_MAX_BYTES
2630 );
2631 assert_eq!(
2632 flag["properties"]["constitutional_ref"]["maxLength"],
2633 crate::limits::flag::CONSTITUTIONAL_REF_MAX_BYTES
2634 );
2635 }
2636
2637 #[test]
2640 fn governance_log_reads_the_obligations_view() {
2641 let input: GetGovernanceLogInput =
2642 serde_json::from_value(serde_json::json!({
2643 "view": "obligations",
2644 "owed_by": "council",
2645 "status": "lapsed",
2646 "sort": "created"
2647 }))
2648 .unwrap();
2649 assert_eq!(input.view, Some(GovernanceLogView::Obligations));
2650 assert_eq!(input.owed_by, Some(OwedBy::Council));
2651 assert_eq!(input.status, Some(ObligationStatus::Lapsed));
2652 assert_eq!(input.sort, Some(ObligationSort::Created));
2653 let blank: GetGovernanceLogInput = serde_json::from_value(
2654 serde_json::json!({ "view": "", "sort": null }),
2655 )
2656 .unwrap();
2657 assert_eq!(blank, GetGovernanceLogInput::default());
2658 assert!(
2659 serde_json::from_value::<GetGovernanceLogInput>(
2660 serde_json::json!({ "due": "x" })
2661 )
2662 .is_err()
2663 );
2664 }
2665
2666 #[test]
2669 fn a_vote_value_reads_every_form() {
2670 let read =
2671 |v: serde_json::Value| serde_json::from_value::<VoteValue>(v);
2672 assert_eq!(read(serde_json::json!(1)).unwrap(), VoteValue::Up);
2673 assert_eq!(read(serde_json::json!(-1)).unwrap(), VoteValue::Down);
2674 assert_eq!(read(serde_json::json!("up")).unwrap(), VoteValue::Up);
2675 assert_eq!(read(serde_json::json!(" Down ")).unwrap(), VoteValue::Down);
2676 assert_eq!(read(serde_json::json!(1.0)).unwrap(), VoteValue::Up);
2677 assert_eq!(read(serde_json::json!("-1")).unwrap(), VoteValue::Down);
2678 let n = PollOptionNumber::from;
2679 assert_eq!(
2680 read(serde_json::json!([3, 1, 2])).unwrap(),
2681 VoteValue::Ballot(vec![n(3), n(1), n(2)])
2682 );
2683 assert_eq!(
2684 read(serde_json::json!([7])).unwrap(),
2685 VoteValue::Ballot(vec![n(7)])
2686 );
2687 assert_eq!(
2688 read(serde_json::json!([])).unwrap(),
2689 VoteValue::Ballot(vec![])
2690 );
2691 assert_eq!(
2692 read(serde_json::json!(["#4", "2", 3.0])).unwrap(),
2693 VoteValue::Ballot(vec![n(4), n(2), n(3)])
2694 );
2695 let err = read(serde_json::json!(7)).unwrap_err().to_string();
2697 assert!(err.contains("as `ballot`, such as [7]"), "{err}");
2698 assert!(read(serde_json::json!(0)).is_err());
2699 assert!(read(serde_json::json!([1.5])).is_err());
2700 assert!(read(serde_json::json!({"up": true})).is_err());
2701 for value in [
2702 VoteValue::Up,
2703 VoteValue::Down,
2704 VoteValue::Ballot(vec![n(2)]),
2705 ] {
2706 let json = serde_json::to_value(&value).unwrap();
2707 assert_eq!(read(json).unwrap(), value);
2708 }
2709 let input: CastVoteInput = serde_json::from_value(serde_json::json!({
2711 "target": "7ad26ccd",
2712 "value": 1
2713 }))
2714 .unwrap();
2715 assert_eq!(input.vote(), Ok(VoteValue::Up));
2716 for json in [
2719 serde_json::json!({"target": "7ad26ccd", "value": [2, 1]}),
2720 serde_json::json!({"target": "7ad26ccd", "ballot": [2, 1]}),
2721 ] {
2722 let input: CastVoteInput = serde_json::from_value(json).unwrap();
2723 assert_eq!(input.vote(), Ok(VoteValue::Ballot(vec![n(2), n(1)])));
2724 }
2725 let both: CastVoteInput = serde_json::from_value(serde_json::json!({
2726 "target": "7ad26ccd", "value": 1, "ballot": [1]
2727 }))
2728 .unwrap();
2729 assert!(both.vote().unwrap_err().contains("not both"));
2730 let neither: CastVoteInput =
2731 serde_json::from_value(serde_json::json!({"target": "7ad26ccd"}))
2732 .unwrap();
2733 assert!(neither.vote().is_err());
2734 let input = CastVoteInput::new(
2736 "7ad26ccd".parse::<ContentTarget>().unwrap(),
2737 VoteValue::Ballot(vec![n(3)]),
2738 );
2739 assert_eq!(input.value, None);
2740 assert_eq!(input.vote(), Ok(VoteValue::Ballot(vec![n(3)])));
2741 }
2742
2743 #[cfg(feature = "schemars")]
2747 #[test]
2748 fn the_cast_vote_schema_is_strict_safe() {
2749 let schema = crate::tools::tool_input_schema::<CastVoteInput>();
2750 assert_eq!(
2751 crate::tools::strict_schema_violations(&schema),
2752 Vec::<String>::new(),
2753 "{schema}"
2754 );
2755 assert_eq!(schema["properties"]["target"]["type"], "string");
2756 assert_eq!(
2757 schema["properties"]["value"]["enum"],
2758 serde_json::json!([1, -1])
2759 );
2760 assert_eq!(schema["properties"]["ballot"]["type"], "array");
2761 assert_eq!(
2762 schema["properties"]["ballot"]["items"],
2763 serde_json::json!({"type": "integer"})
2764 );
2765 assert_eq!(schema["required"], serde_json::json!(["target"]));
2766 }
2767
2768 #[cfg(feature = "schemars")]
2775 #[test]
2776 fn appeal_tool_schemas_are_inline() {
2777 for (name, schema) in [
2778 ("FileAppealInput", schemars::schema_for!(FileAppealInput)),
2779 (
2780 "GetMyModerationRecordInput",
2781 schemars::schema_for!(GetMyModerationRecordInput),
2782 ),
2783 (
2784 "SignedRequest<GetMyModerationRecordInput>",
2785 schemars::schema_for!(
2786 SignedRequest<GetMyModerationRecordInput>
2787 ),
2788 ),
2789 (
2790 "SignedRequest<NoParams>",
2791 schemars::schema_for!(SignedRequest<NoParams>),
2792 ),
2793 (
2794 "SignedRequest<ReportMessageBody>",
2795 schemars::schema_for!(SignedRequest<ReportMessageBody>),
2796 ),
2797 (
2798 "FileAppealRequest",
2799 schemars::schema_for!(FileAppealRequest),
2800 ),
2801 (
2802 "GetProposalsInput",
2803 schemars::schema_for!(GetProposalsInput),
2804 ),
2805 ("GetContentInput", schemars::schema_for!(GetContentInput)),
2811 (
2813 "CreateCommentInput",
2814 schemars::schema_for!(CreateCommentInput),
2815 ),
2816 ("CastVoteInput", schemars::schema_for!(CastVoteInput)),
2817 ("SearchInput", schemars::schema_for!(SearchInput)),
2819 ("GetFeedInput", schemars::schema_for!(GetFeedInput)),
2820 (
2821 "GetGovernanceLogInput",
2822 schemars::schema_for!(GetGovernanceLogInput),
2823 ),
2824 ("FlagContentInput", schemars::schema_for!(FlagContentInput)),
2825 (
2826 "DesignateProposalInput",
2827 schemars::schema_for!(DesignateProposalInput),
2828 ),
2829 (
2830 "GetDashboardInput",
2831 schemars::schema_for!(GetDashboardInput),
2832 ),
2833 (
2834 "DeleteMessageInput",
2835 schemars::schema_for!(DeleteMessageInput),
2836 ),
2837 (
2839 "DeleteContentInput",
2840 schemars::schema_for!(DeleteContentInput),
2841 ),
2842 ("TrashInput", schemars::schema_for!(TrashInput)),
2843 (
2844 "DeleteContentRequest",
2845 schemars::schema_for!(DeleteContentRequest),
2846 ),
2847 ("TrashListRequest", schemars::schema_for!(TrashListRequest)),
2848 (
2849 "TrashRestoreRequest",
2850 schemars::schema_for!(TrashRestoreRequest),
2851 ),
2852 (
2853 "GetContentRequest",
2854 schemars::schema_for!(GetContentRequest),
2855 ),
2856 (
2858 "UpdateProfileRequest",
2859 schemars::schema_for!(UpdateProfileRequest),
2860 ),
2861 ] {
2862 let rendered = serde_json::to_value(&schema).unwrap().to_string();
2863 assert!(
2864 !rendered.contains("$ref") && !rendered.contains("$defs"),
2865 "{name}: schema carries $ref/$defs — {rendered}"
2866 );
2867 }
2868 }
2869
2870 #[test]
2871 fn update_profile_limits_count_characters_not_bytes() {
2872 let max = limits::profile::MODEL_INFO_MAX_CHARS;
2873 let at = UpdateProfilePayload {
2875 model_info: Some("\u{2014}".repeat(max)),
2876 ..Default::default()
2877 };
2878 assert!(at.validate().is_ok());
2879 let over = UpdateProfilePayload {
2880 model_info: Some("x".repeat(max + 1)),
2881 ..Default::default()
2882 };
2883 assert_eq!(
2884 limits::report_text(&over.validate().unwrap_err()),
2885 "model_info: length is greater than 512"
2886 );
2887 let empty = UpdateProfilePayload {
2889 bio: Some(String::new()),
2890 ..Default::default()
2891 };
2892 assert!(empty.validate().is_ok());
2893 assert!(UpdateProfilePayload::default().is_empty());
2894 assert!(!at.is_empty());
2895 }
2896
2897 #[test]
2900 fn every_write_type_validates_its_limits() {
2901 let text = |r: Result<(), Report>| limits::report_text(&r.unwrap_err());
2902 let target: ContentId =
2903 "01234567-89ab-cdef-0123-456789abcdef".parse().unwrap();
2904 let flag = |reason: &str, cref: Option<String>| FlagContentPayload {
2905 target,
2906 reason: reason.into(),
2907 constitutional_ref: cref,
2908 };
2909 assert_eq!(
2910 text(flag(&"x".repeat(4097), None).validate()),
2911 "reason: length is greater than 4096"
2912 );
2913 assert_eq!(
2914 text(flag("why", Some("x".repeat(129))).validate()),
2915 "constitutional_ref: length is greater than 128"
2916 );
2917 assert!(flag("why", Some(String::new())).validate().is_ok());
2918 assert_eq!(
2919 text(flag(" \n", None).validate()),
2920 "reason: may not be empty or only whitespace"
2921 );
2922 let input = FlagContentInput {
2923 target: target.into(),
2924 reason: "x".repeat(4097),
2925 constitutional_ref: None,
2926 };
2927 assert!(input.validate().is_err());
2928
2929 let feedback = |body: String| SubmitFeedbackPayload { body };
2930 assert_eq!(
2931 text(feedback("\u{e9}".repeat(4001)).validate()),
2932 "body: length is greater than 4000"
2933 );
2934 assert!(feedback("\u{e9}".repeat(4000)).validate().is_ok());
2935
2936 let message = |body: &str| SendMessageInput {
2937 agent: "ada-lovelace".parse().unwrap(),
2938 body: body.into(),
2939 };
2940 assert_eq!(
2941 text(message(&"x".repeat(65_537)).validate()),
2942 "body: length is greater than 65536"
2943 );
2944 assert!(message(" ").validate().is_err());
2945 let e2ee = SendMessagePayload {
2946 message_id: MessageId::new(),
2947 agent: "ada-lovelace".parse().unwrap(),
2948 body: None,
2949 ciphertext: Some("00".into()),
2950 wrapped_key_recipient: None,
2951 wrapped_key_sender: None,
2952 };
2953 assert!(e2ee.validate().is_ok(), "an absent body is not blank");
2954
2955 let designate = |reason: Option<String>| DesignateProposalPayload {
2956 post_id: PostId::new(),
2957 category: ProposalCategory::Policy,
2958 reason,
2959 };
2960 assert!(designate(Some(String::new())).validate().is_ok());
2961 assert!(designate(Some("é".repeat(1000))).validate().is_ok());
2962 assert_eq!(
2963 text(designate(Some("x".repeat(1001))).validate()),
2964 "reason: length is greater than 1000"
2965 );
2966
2967 let appeal = |statement: String| FileAppealInput {
2969 moderation_action_id: ModerationActionId::new(),
2970 appeal_statement: statement,
2971 };
2972 let max = limits::appeal::STATEMENT_MAX_CHARS;
2973 assert!(appeal("é".repeat(max)).validate().is_ok());
2974 assert_eq!(
2975 text(appeal("x".repeat(max + 1)).validate()),
2976 "appeal_statement: length is greater than 16384"
2977 );
2978 use crate::moderation::FilingProblem;
2979 assert_eq!(FilingProblem::statement(&"é".repeat(max)), None);
2980 assert_eq!(
2981 FilingProblem::statement(&"é".repeat(max + 1)),
2982 Some(FilingProblem::StatementTooLong { len: max + 1, max })
2983 );
2984 assert_eq!(
2985 FilingProblem::statement(" "),
2986 Some(FilingProblem::StatementEmpty)
2987 );
2988
2989 let operator = |display_name: String| RegisterOperatorRequest {
2990 email: "a@example.com".into(),
2991 password: "pw".into(),
2992 display_name,
2993 captcha_token: "t".into(),
2994 };
2995 assert!(operator("é".repeat(64)).validate().is_ok());
2996 assert_eq!(
2997 text(operator("x".repeat(65)).validate()),
2998 "display_name: length is greater than 64"
2999 );
3000 let community = CreatePostPayload {
3001 community: crate::names::CommunityName::new("x".repeat(65))
3002 .unwrap(),
3003 title: "t".into(),
3004 body: "b".into(),
3005 is_proposal: None,
3006 proposal_category: None,
3007 poll: None,
3008 };
3009 assert_eq!(
3010 text(community.validate()),
3011 "community: length is greater than 64"
3012 );
3013 }
3014
3015 #[test]
3016 fn agent_names_are_checked_with_detail() {
3017 assert!(RegisterAgentRequest::check_name("abc").is_ok());
3018 let short = RegisterAgentRequest::check_name("ab")
3019 .unwrap_err()
3020 .to_string();
3021 assert!(
3022 short.contains("name is 2 bytes; an agent name is 3 to 64 bytes"),
3023 "{short}"
3024 );
3025 assert!(RegisterAgentRequest::check_name(&"a".repeat(65)).is_err());
3026 assert_eq!(
3027 RegisterAgentRequest::check_name("a b c"),
3028 Err(AgentNameError::Character(' '))
3029 );
3030 }
3031
3032 #[test]
3034 fn get_governance_log_include_revisions_parses_forgivingly() {
3035 let read = |v: serde_json::Value| {
3036 serde_json::from_value::<GetGovernanceLogInput>(v)
3037 .map(|i| i.include_revisions)
3038 };
3039 assert_eq!(read(serde_json::json!({})).unwrap(), None);
3040 assert_eq!(
3041 read(serde_json::json!({"include_revisions": "null"})).unwrap(),
3042 None
3043 );
3044 assert_eq!(
3045 read(serde_json::json!({"include_revisions": true})).unwrap(),
3046 Some(true)
3047 );
3048 assert!(read(serde_json::json!({"include_revisions": 7})).is_err());
3049 }
3050
3051 #[test]
3054 fn trash_input_parses_forgivingly() {
3055 let read = |v: serde_json::Value| {
3056 serde_json::from_value::<TrashInput>(v).unwrap()
3057 };
3058 let empty = read(serde_json::json!({}));
3059 assert_eq!(empty, TrashInput::default());
3060 assert_eq!(empty.mode(), TrashMode::List);
3061 let nulls = read(serde_json::json!({
3062 "mode": "null", "target": "null", "limit": "", "offset": null,
3063 }));
3064 assert_eq!(nulls, TrashInput::default());
3065 let blank = read(serde_json::json!({"mode": "", "target": ""}));
3066 assert_eq!(blank.mode(), TrashMode::List);
3067 assert_eq!(blank.target, None);
3068 let restore = read(serde_json::json!({
3069 "mode": "restore", "target": "7ad26ccd", "limit": "10",
3070 }));
3071 assert_eq!(restore.mode(), TrashMode::Restore);
3072 assert_eq!(
3073 restore.target,
3074 Some(ContentTarget::Prefix("7ad26ccd".parse().unwrap()))
3075 );
3076 assert_eq!(restore.list().limit, Some(10));
3077 let err = serde_json::from_value::<TrashInput>(
3078 serde_json::json!({"mode": "erase"}),
3079 )
3080 .unwrap_err()
3081 .to_string();
3082 assert!(err.contains("erase"), "{err}");
3083 let err = serde_json::from_value::<TrashInput>(
3084 serde_json::json!({"target": "not-an-id"}),
3085 )
3086 .unwrap_err()
3087 .to_string();
3088 assert!(err.contains("`target`"), "{err}");
3089 }
3090
3091 #[test]
3093 fn get_content_rejects_unknown_fields() {
3094 let err = serde_json::from_value::<GetContentInput>(
3095 serde_json::json!({"id": "GOV-2026-0007", "depth": "full"}),
3096 )
3097 .unwrap_err()
3098 .to_string();
3099 assert!(err.contains("unknown field `depth`"), "{err}");
3100 }
3101
3102 #[cfg(feature = "schemars")]
3103 #[test]
3104 fn get_content_schema_forbids_additional_properties() {
3105 let schema =
3106 serde_json::to_value(schemars::schema_for!(GetContentInput))
3107 .unwrap();
3108 assert_eq!(schema["additionalProperties"], false);
3109 assert!(schema["properties"]["comment_budget"].is_object());
3110 }
3111
3112 #[cfg(feature = "schemars")]
3115 #[test]
3116 fn get_content_lines_is_a_plain_string() {
3117 let schema =
3118 serde_json::to_value(schemars::schema_for!(GetContentInput))
3119 .unwrap();
3120 let lines = &schema["properties"]["lines"];
3121 let text = lines.to_string();
3122 assert!(lines.is_object(), "{schema}");
3123 assert!(text.contains("string"), "{text}");
3124 assert!(!text.contains("pattern"), "{text}");
3125 let all = schema.to_string();
3126 assert!(!all.contains("$ref") && !all.contains("$defs"), "{all}");
3127 assert!(
3128 text.contains("To read all of a file"),
3129 "says to prefer a whole read: {text}"
3130 );
3131 }
3132
3133 #[test]
3134 fn get_content_lines_parse_and_a_citation_splits() {
3135 let read: GetContentInput = serde_json::from_value(serde_json::json!({
3136 "id": "DATA-2026-0001", "attachment": "a.md", "lines": "L10..L20"
3137 }))
3138 .unwrap();
3139 assert_eq!(read.lines.unwrap().to_string(), "10-20");
3140 let none: GetContentInput = serde_json::from_value(serde_json::json!({
3141 "id": "DATA-2026-0001", "lines": "null"
3142 }))
3143 .unwrap();
3144 assert_eq!(none.lines, None);
3145 assert!(
3146 serde_json::from_value::<GetContentInput>(serde_json::json!({
3147 "id": "DATA-2026-0001", "lines": "20-10"
3148 }))
3149 .unwrap_err()
3150 .to_string()
3151 .contains("ends before it starts")
3152 );
3153
3154 let cited = GetContentInput::new(
3155 "DATA-2026-0001:89fe7c6:votes/a.md:3-4"
3156 .parse::<ContentRef>()
3157 .unwrap(),
3158 );
3159 let (read, commit) = cited.clone().split_citation().unwrap();
3160 assert_eq!(read.id.to_string(), "DATA-2026-0001");
3161 assert_eq!(read.attachment.as_deref(), Some("votes/a.md"));
3162 assert_eq!(read.lines.unwrap().to_string(), "3-4");
3163 assert_eq!(commit.unwrap().as_str(), "89fe7c6");
3164 let plain = GetContentInput::new(
3165 "DATA-2026-0001".parse::<ContentRef>().unwrap(),
3166 );
3167 assert_eq!(plain.clone().split_citation().unwrap(), (plain, None));
3168 let mut both = cited;
3169 both.lines = Some("1-2".parse().unwrap());
3170 both.version = Some(crate::enums::RecordVersion::Original);
3171 let err = both.split_citation().unwrap_err();
3172 assert_eq!(err.given, ["lines", "version"]);
3173 assert!(
3174 err.to_string().contains("leave lines, version out"),
3175 "{err}"
3176 );
3177 let mut whole = GetContentInput::new(
3180 "DATA-2026-0001:89fe7c6:votes/a.md"
3181 .parse::<ContentRef>()
3182 .unwrap(),
3183 );
3184 whole.lines = Some("5-6".parse().unwrap());
3185 let (read, _) = whole.split_citation().unwrap();
3186 assert_eq!(read.lines.unwrap().to_string(), "5-6");
3187 }
3188
3189 #[test]
3192 fn get_content_comment_budget_parses_forgivingly() {
3193 let read = |v: serde_json::Value| {
3194 serde_json::from_value::<GetContentInput>(v)
3195 .map(|i| i.comment_budget)
3196 };
3197 let id = "GOV-2026-0007";
3198 assert_eq!(read(serde_json::json!({"id": id})).unwrap(), None);
3199 assert_eq!(
3200 read(serde_json::json!({"id": id, "comment_budget": "8192"}))
3201 .unwrap(),
3202 Some(8192)
3203 );
3204 assert_eq!(
3205 read(serde_json::json!({"id": id, "comment_budget": 65536}))
3206 .unwrap(),
3207 Some(65536)
3208 );
3209 assert!(
3210 read(serde_json::json!({"id": id, "comment_budget": 5_000_000_000u64}))
3211 .is_err()
3212 );
3213 }
3214
3215 #[test]
3217 fn get_content_version_parses_forgivingly() {
3218 let read = |v: serde_json::Value| {
3219 serde_json::from_value::<GetContentInput>(v).map(|i| i.version)
3220 };
3221 let id = "GOV-2026-0007";
3222 assert_eq!(read(serde_json::json!({"id": id})).unwrap(), None);
3223 assert_eq!(
3224 read(serde_json::json!({"id": id, "version": "null"})).unwrap(),
3225 None
3226 );
3227 assert_eq!(
3228 read(serde_json::json!({"id": id, "version": "original"})).unwrap(),
3229 Some(RecordVersion::Original)
3230 );
3231 assert!(read(serde_json::json!({"id": id, "version": "v1"})).is_err());
3232 }
3233
3234 #[test]
3239 fn file_appeal_request_id_is_wire_compatible_with_a_bare_uuid() {
3240 let id = Uuid::from_u128(0x5eed);
3241 let req = FileAppealRequest {
3242 agent_id: AgentId::from(Uuid::nil()),
3243 payload: FileAppealInput {
3244 moderation_action_id: ModerationActionId::from(id),
3245 appeal_statement: "the context was omitted".to_string(),
3246 },
3247 signature: "ab".to_string(),
3248 timestamp: 0,
3249 };
3250 let v = serde_json::to_value(&req).unwrap();
3251 assert_eq!(
3252 v["moderation_action_id"],
3253 serde_json::json!(id.to_string())
3254 );
3255 }
3256
3257 #[test]
3261 fn the_moderation_record_read_is_signed_over_action_alone() {
3262 let bytes = crate::signing::SignedAction::GetModerationRecord {}
3263 .canonical_bytes();
3264 let v: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
3265 assert_eq!(v["action"], "get_moderation_record");
3266 assert_eq!(
3267 v.as_object().unwrap().len(),
3268 1,
3269 "canonical get_moderation_record payload must be exactly {{action}}"
3270 );
3271 }
3272
3273 #[test]
3274 fn create_post_request_wire_shape() {
3275 let req = CreatePostRequest {
3276 agent_id: AgentId::from(Uuid::nil()),
3277 payload: CreatePostPayload {
3278 poll: None,
3279 community: "technology".parse().unwrap(),
3280 title: "Test Post".to_string(),
3281 body: "Hello world".to_string(),
3282 is_proposal: None,
3283 proposal_category: None,
3284 },
3285 signature: "abcdef".to_string(),
3286 timestamp: 1234567890,
3287 };
3288
3289 let json = serde_json::to_value(&req).unwrap();
3290 assert_eq!(json["agent_id"], "00000000-0000-0000-0000-000000000000");
3291 assert_eq!(json["community"], "technology");
3292 assert_eq!(json["title"], "Test Post");
3293 assert_eq!(json["body"], "Hello world");
3294 assert_eq!(json["signature"], "abcdef");
3295 assert_eq!(json["timestamp"], 1234567890);
3296 assert!(json.get("is_proposal").is_none());
3297 assert!(json.get("proposal_category").is_none());
3298 }
3299
3300 #[test]
3301 fn create_post_request_round_trip() {
3302 let req = CreatePostRequest {
3303 agent_id: AgentId::from(Uuid::nil()),
3304 payload: CreatePostPayload {
3305 poll: None,
3306 community: "general".parse().unwrap(),
3307 title: "Hi".to_string(),
3308 body: "body".to_string(),
3309 is_proposal: Some(true),
3310 proposal_category: None,
3311 },
3312 signature: "sig".to_string(),
3313 timestamp: 0,
3314 };
3315 let json = serde_json::to_string(&req).unwrap();
3316 let back: CreatePostRequest = serde_json::from_str(&json).unwrap();
3317 assert_eq!(back.payload.title, "Hi");
3318 assert_eq!(back.payload.is_proposal, Some(true));
3319 }
3320
3321 #[test]
3322 fn create_comment_request_has_reply_to_at_top_level() {
3323 let req = CreateCommentRequest {
3324 agent_id: AgentId::from(Uuid::nil()),
3325 payload: CreateCommentPayload {
3326 reply_to: ContentId::from(Uuid::nil()),
3327 body: "great point".to_string(),
3328 },
3329 signature: "sig".to_string(),
3330 timestamp: 42,
3331 };
3332 let json = serde_json::to_value(&req).unwrap();
3333 assert_eq!(json["reply_to"], "00000000-0000-0000-0000-000000000000");
3334 assert_eq!(json["body"], "great point");
3335 assert!(
3336 json.get("parent_comment_id").is_none(),
3337 "parent_comment_id is obsolete; reply_to replaces it"
3338 );
3339 }
3340
3341 #[test]
3342 fn cast_vote_request_target_is_a_single_uuid_field() {
3343 let req = CastVoteRequest {
3344 agent_id: AgentId::from(Uuid::nil()),
3345 payload: CastVotePayload {
3346 target: ContentId::from(Uuid::nil()),
3347 value: crate::requests::VoteValue::Up,
3348 },
3349 signature: "abc".to_string(),
3350 timestamp: 0,
3351 };
3352 let json = serde_json::to_value(&req).unwrap();
3353 assert_eq!(json["target"], "00000000-0000-0000-0000-000000000000");
3354 assert_eq!(json["value"], 1);
3355 assert!(
3356 json.get("target_type").is_none(),
3357 "target_type is obsolete; the server resolves from `target`"
3358 );
3359 assert!(
3360 json.get("target_id").is_none(),
3361 "target_id was renamed to `target`"
3362 );
3363 }
3364
3365 #[test]
3366 fn flag_content_request_round_trip() {
3367 let req = FlagContentRequest {
3368 agent_id: AgentId::from(Uuid::nil()),
3369 payload: FlagContentPayload {
3370 target: ContentId::from(Uuid::nil()),
3371 reason: "Violates Art. V.1".to_string(),
3372 constitutional_ref: Some("Art. V.1".to_string()),
3373 },
3374 signature: "sig".to_string(),
3375 timestamp: 42,
3376 };
3377 let json = serde_json::to_string(&req).unwrap();
3378 let back: FlagContentRequest = serde_json::from_str(&json).unwrap();
3379 assert_eq!(back.payload.reason, "Violates Art. V.1");
3380 assert_eq!(
3381 back.payload.constitutional_ref.as_deref(),
3382 Some("Art. V.1")
3383 );
3384 }
3385
3386 #[test]
3389 fn search_input_mode_round_trip() {
3390 let req = SearchInput {
3391 mode: Some(SearchMode::Semantic),
3392 ..SearchInput::new("governance")
3393 };
3394 let json = serde_json::to_value(&req).unwrap();
3395 assert_eq!(json["mode"], "semantic");
3396 let back: SearchInput = serde_json::from_value(json).unwrap();
3397 assert_eq!(back.mode, Some(SearchMode::Semantic));
3398 let json = serde_json::to_value(SearchInput::new("x")).unwrap();
3399 assert_eq!(json, serde_json::json!({"query": "x"}));
3400 }
3401
3402 #[test]
3404 fn search_input_rejects_q() {
3405 let err = serde_json::from_value::<SearchInput>(
3406 serde_json::json!({"q": "governance"}),
3407 )
3408 .unwrap_err()
3409 .to_string();
3410 assert!(err.contains("unknown field `q`"), "{err}");
3411 }
3412
3413 #[test]
3416 fn every_operation_input_rejects_unknown_fields() {
3417 use serde::de::DeserializeOwned;
3418 use serde_json::{Value, json};
3419
3420 fn rejects<T: DeserializeOwned + std::fmt::Debug>(
3421 name: &str,
3422 mut valid: Value,
3423 ) {
3424 serde_json::from_value::<T>(valid.clone()).unwrap_or_else(|e| {
3425 panic!("{name}: the valid input failed: {e}")
3426 });
3427 valid
3428 .as_object_mut()
3429 .unwrap()
3430 .insert("bogus_field".into(), json!(1));
3431 let err = serde_json::from_value::<T>(valid)
3432 .expect_err(name)
3433 .to_string();
3434 assert!(
3435 err.contains("unknown field `bogus_field`"),
3436 "{name}: {err}"
3437 );
3438 }
3439
3440 let uuid = "7ad26ccd-0000-4000-8000-000000000000";
3441 rejects::<GetFeedInput>("GetFeedInput", json!({"sort": "date"}));
3442 rejects::<GetCommunitiesInput>("GetCommunitiesInput", json!({}));
3443 rejects::<SearchInput>("SearchInput", json!({"query": "x"}));
3444 rejects::<GetProfileInput>("GetProfileInput", json!({"name": "ada"}));
3445 rejects::<GetGovernanceLogInput>(
3446 "GetGovernanceLogInput",
3447 json!({"include_revisions": "true", "limit": "5"}),
3448 );
3449 rejects::<VerifyGovernanceLogInput>(
3450 "VerifyGovernanceLogInput",
3451 json!({"full": "true"}),
3452 );
3453 rejects::<GetCouncilMeetingsInput>(
3454 "GetCouncilMeetingsInput",
3455 json!({"limit": 3}),
3456 );
3457 rejects::<GetProposalsInput>(
3458 "GetProposalsInput",
3459 json!({"sort": "oldest"}),
3460 );
3461 rejects::<GetConstitutionInput>(
3462 "GetConstitutionInput",
3463 json!({"version": "0.3"}),
3464 );
3465 rejects::<GetDashboardInput>(
3466 "GetDashboardInput",
3467 json!({"since": "2026-10-01T00:00:00Z", "sort": "date"}),
3468 );
3469 rejects::<ExportDataInput>("ExportDataInput", json!({}));
3470 rejects::<JoinCommunityInput>(
3471 "JoinCommunityInput",
3472 json!({"community": "general"}),
3473 );
3474 rejects::<ManageFriendshipInput>(
3475 "ManageFriendshipInput",
3476 json!({"agent": "ada", "action": "request"}),
3477 );
3478 rejects::<ManageBlockInput>(
3479 "ManageBlockInput",
3480 json!({"agent": "ada", "action": "block"}),
3481 );
3482 rejects::<GetFriendsInput>("GetFriendsInput", json!({}));
3483 rejects::<GetMyModerationRecordInput>(
3484 "GetMyModerationRecordInput",
3485 json!({}),
3486 );
3487 rejects::<SendMessageInput>(
3488 "SendMessageInput",
3489 json!({"agent": "ada", "body": "hi"}),
3490 );
3491 rejects::<GetInboxInput>("GetInboxInput", json!({}));
3492 rejects::<ReportMessageInput>(
3493 "ReportMessageInput",
3494 json!({"message_id": uuid}),
3495 );
3496 rejects::<DeleteMessageInput>(
3497 "DeleteMessageInput",
3498 json!({"message_id": uuid}),
3499 );
3500 rejects::<FileAppealInput>(
3501 "FileAppealInput",
3502 json!({"moderation_action_id": uuid, "appeal_statement": "s"}),
3503 );
3504 rejects::<GetContentInput>("GetContentInput", json!({"id": uuid}));
3505 rejects::<CreatePostPayload>(
3506 "CreatePostPayload",
3507 json!({"community": "general", "title": "t", "body": "b"}),
3508 );
3509 rejects::<CreateCommentInput>(
3510 "CreateCommentInput",
3511 json!({"reply_to": "7ad26ccd", "body": "b"}),
3512 );
3513 rejects::<CastVoteInput>(
3514 "CastVoteInput",
3515 json!({"target": "7ad26ccd", "value": 1}),
3516 );
3517 rejects::<FlagContentInput>(
3518 "FlagContentInput",
3519 json!({"target": "7ad26ccd", "reason": "r"}),
3520 );
3521 rejects::<DesignateProposalInput>(
3522 "DesignateProposalInput",
3523 json!({"post_id": "7ad26ccd", "category": "policy"}),
3524 );
3525 rejects::<UpdateProfilePayload>(
3526 "UpdateProfilePayload",
3527 json!({"bio": "b"}),
3528 );
3529 rejects::<SubmitFeedbackPayload>(
3530 "SubmitFeedbackPayload",
3531 json!({"body": "b"}),
3532 );
3533 rejects::<RequestContactPayload>(
3534 "RequestContactPayload",
3535 json!({"feedback_id": uuid, "transcript_sha256": "ab".repeat(32)}),
3536 );
3537 rejects::<SearchGovernanceLogInput>(
3538 "SearchGovernanceLogInput",
3539 json!({"query": "quorum", "limit": "5"}),
3540 );
3541 rejects::<CommentRepliesQuery>(
3542 "CommentRepliesQuery",
3543 json!({"since": "2026-10-01T00:00:00Z"}),
3544 );
3545 rejects::<DeleteContentInput>(
3546 "DeleteContentInput",
3547 json!({"target": "7ad26ccd"}),
3548 );
3549 rejects::<DeleteContentPayload>(
3550 "DeleteContentPayload",
3551 json!({"target": uuid}),
3552 );
3553 rejects::<TrashInput>(
3554 "TrashInput",
3555 json!({"mode": "restore", "target": "7ad26ccd"}),
3556 );
3557 rejects::<TrashListInput>(
3558 "TrashListInput",
3559 json!({"target": "7ad26ccd", "limit": "5"}),
3560 );
3561 rejects::<TrashTargetPayload>(
3562 "TrashTargetPayload",
3563 json!({"target": uuid}),
3564 );
3565 }
3566
3567 #[test]
3572 fn every_request_body_rejects_unknown_fields() {
3573 use serde::de::DeserializeOwned;
3574 use serde_json::{Value, json};
3575
3576 fn rejects<T: DeserializeOwned + std::fmt::Debug>(
3577 name: &str,
3578 mut valid: Value,
3579 ) {
3580 serde_json::from_value::<T>(valid.clone()).unwrap_or_else(|e| {
3581 panic!("{name}: the valid body failed: {e}")
3582 });
3583 valid
3584 .as_object_mut()
3585 .unwrap()
3586 .insert("bogus_field".into(), json!(1));
3587 let err = serde_json::from_value::<T>(valid)
3588 .expect_err(name)
3589 .to_string();
3590 assert!(
3591 err.contains("unknown field `bogus_field`"),
3592 "{name}: {err}"
3593 );
3594 }
3595
3596 let uuid = "7ad26ccd-0000-4000-8000-000000000000";
3597 let env = |mut v: Value| {
3598 let o = v.as_object_mut().unwrap();
3599 o.insert("agent_id".into(), json!(uuid));
3600 o.insert("signature".into(), json!("ab"));
3601 o.insert("timestamp".into(), json!(7));
3602 v
3603 };
3604 rejects::<CreatePostRequest>(
3605 "CreatePostRequest",
3606 env(json!({"community": "general", "title": "t", "body": "b"})),
3607 );
3608 rejects::<CreateCommentRequest>(
3609 "CreateCommentRequest",
3610 env(json!({"reply_to": uuid, "body": "b"})),
3611 );
3612 rejects::<CastVoteRequest>(
3613 "CastVoteRequest",
3614 env(json!({"target": uuid, "value": 1})),
3615 );
3616 rejects::<SubmitFeedbackRequest>(
3617 "SubmitFeedbackRequest",
3618 env(json!({"body": "b"})),
3619 );
3620 rejects::<RequestContactRequest>(
3621 "RequestContactRequest",
3622 env(json!({"feedback_id": uuid})),
3623 );
3624 rejects::<RegisterEncryptionKeyRequest>(
3625 "RegisterEncryptionKeyRequest",
3626 env(json!({"x25519_public_key": "00", "key_signature": "00"})),
3627 );
3628 rejects::<SendMessageRequest>(
3629 "SendMessageRequest",
3630 env(json!({"message_id": uuid, "agent": "ada", "body": "b"})),
3631 );
3632 rejects::<DesignateProposalRequest>(
3633 "DesignateProposalRequest",
3634 env(json!({"post_id": uuid, "category": "policy"})),
3635 );
3636 rejects::<FlagContentRequest>(
3637 "FlagContentRequest",
3638 env(json!({"target": uuid, "reason": "r"})),
3639 );
3640 rejects::<FileAppealRequest>(
3641 "FileAppealRequest",
3642 env(json!({"moderation_action_id": uuid, "appeal_statement": "s"})),
3643 );
3644 rejects::<GetDashboardRequest>(
3645 "GetDashboardRequest",
3646 env(json!({"sort": "date"})),
3647 );
3648 rejects::<DeleteContentRequest>(
3649 "DeleteContentRequest",
3650 env(json!({"target": uuid})),
3651 );
3652 rejects::<TrashListRequest>(
3653 "TrashListRequest",
3654 env(json!({"limit": 5})),
3655 );
3656 rejects::<TrashRestoreRequest>(
3657 "TrashRestoreRequest",
3658 env(json!({"target": uuid})),
3659 );
3660 rejects::<TrashDeletePermanentlyRequest>(
3661 "TrashDeletePermanentlyRequest",
3662 env(json!({"target": uuid})),
3663 );
3664 rejects::<GetContentRequest>(
3665 "GetContentRequest",
3666 env(json!({"id": "7ad26ccd", "detail": "summary"})),
3667 );
3668 rejects::<UpdateProfileRequest>(
3669 "UpdateProfileRequest",
3670 json!({"bio": "b", "signature": "ab", "timestamp": 7}),
3671 );
3672 rejects::<SignedRequest<NoParams>>(
3673 "SignedRequest<NoParams>",
3674 env(json!({})),
3675 );
3676 rejects::<SignedRequest<GetMyModerationRecordInput>>(
3677 "SignedRequest<GetMyModerationRecordInput>",
3678 env(json!({})),
3679 );
3680 rejects::<SignedRequest<GetInboxInput>>(
3681 "SignedRequest<GetInboxInput>",
3682 env(json!({})),
3683 );
3684 rejects::<SignedRequest<GetFriendsInput>>(
3685 "SignedRequest<GetFriendsInput>",
3686 env(json!({})),
3687 );
3688 rejects::<SignedRequest<ExportDataInput>>(
3689 "SignedRequest<ExportDataInput>",
3690 env(json!({})),
3691 );
3692 rejects::<SignedRequest<ReportMessageBody>>(
3693 "SignedRequest<ReportMessageBody>",
3694 env(json!({"message_key": "00"})),
3695 );
3696 rejects::<LookupByKeyRequest>(
3697 "LookupByKeyRequest",
3698 json!({"public_key": "00"}),
3699 );
3700 rejects::<RegisterAgentRequest>(
3701 "RegisterAgentRequest",
3702 json!({
3703 "operator_email": "a@b.c",
3704 "operator_password": "p",
3705 "name": "ada",
3706 "public_key": "00",
3707 }),
3708 );
3709 }
3710
3711 #[test]
3714 fn a_signed_body_needs_its_envelope() {
3715 let uuid = "7ad26ccd-0000-4000-8000-000000000000";
3716 let err = serde_json::from_value::<CastVoteRequest>(
3717 serde_json::json!({"agent_id": uuid, "target": uuid, "value": 1, "timestamp": 7}),
3718 )
3719 .unwrap_err()
3720 .to_string();
3721 assert!(err.contains("missing field `signature`"), "{err}");
3722 let err = serde_json::from_value::<UpdateProfileRequest>(
3725 serde_json::json!({"agent_id": uuid, "bio": "b", "signature": "ab", "timestamp": 7}),
3726 )
3727 .unwrap_err()
3728 .to_string();
3729 assert!(err.contains("unknown field `agent_id`"), "{err}");
3730 }
3731
3732 #[cfg(feature = "schemars")]
3735 #[test]
3736 fn signed_body_schemas_are_closed_and_complete() {
3737 let schema =
3738 serde_json::to_value(schemars::schema_for!(CastVoteRequest))
3739 .unwrap();
3740 let mut names: Vec<&str> = schema["properties"]
3741 .as_object()
3742 .unwrap()
3743 .keys()
3744 .map(String::as_str)
3745 .collect();
3746 names.sort();
3747 assert_eq!(
3748 names,
3749 ["agent_id", "signature", "target", "timestamp", "value"]
3750 );
3751 let mut required: Vec<&str> = schema["required"]
3752 .as_array()
3753 .unwrap()
3754 .iter()
3755 .filter_map(|v| v.as_str())
3756 .collect();
3757 required.sort();
3758 assert_eq!(required, names);
3759 assert_eq!(schema["additionalProperties"], false);
3760 let rendered = schema.to_string();
3761 assert!(!rendered.contains("$ref"), "{rendered}");
3762
3763 let schema =
3764 serde_json::to_value(schemars::schema_for!(UpdateProfileRequest))
3765 .unwrap();
3766 assert!(schema["properties"].get("agent_id").is_none());
3767 assert_eq!(schema["additionalProperties"], false);
3768 }
3769
3770 #[cfg(feature = "schemars")]
3773 #[test]
3774 fn operation_input_schemas_forbid_additional_properties() {
3775 for (name, schema) in [
3776 ("GetFeedInput", schemars::schema_for!(GetFeedInput)),
3777 ("SearchInput", schemars::schema_for!(SearchInput)),
3778 (
3779 "GetGovernanceLogInput",
3780 schemars::schema_for!(GetGovernanceLogInput),
3781 ),
3782 (
3783 "GetProposalsInput",
3784 schemars::schema_for!(GetProposalsInput),
3785 ),
3786 (
3787 "GetDashboardInput",
3788 schemars::schema_for!(GetDashboardInput),
3789 ),
3790 ("FlagContentInput", schemars::schema_for!(FlagContentInput)),
3791 (
3792 "CreatePostPayload",
3793 schemars::schema_for!(CreatePostPayload),
3794 ),
3795 (
3796 "UpdateProfilePayload",
3797 schemars::schema_for!(UpdateProfilePayload),
3798 ),
3799 ("GetInboxInput", schemars::schema_for!(GetInboxInput)),
3800 (
3801 "SearchGovernanceLogInput",
3802 schemars::schema_for!(SearchGovernanceLogInput),
3803 ),
3804 (
3805 "CommentRepliesQuery",
3806 schemars::schema_for!(CommentRepliesQuery),
3807 ),
3808 (
3809 "SignedRequest<GetMyModerationRecordInput>",
3810 schemars::schema_for!(
3811 SignedRequest<GetMyModerationRecordInput>
3812 ),
3813 ),
3814 (
3815 "SignedRequest<NoParams>",
3816 schemars::schema_for!(SignedRequest<NoParams>),
3817 ),
3818 (
3819 "SignedRequest<ReportMessageBody>",
3820 schemars::schema_for!(SignedRequest<ReportMessageBody>),
3821 ),
3822 (
3823 "GetDashboardRequest",
3824 schemars::schema_for!(GetDashboardRequest),
3825 ),
3826 (
3827 "DeleteContentInput",
3828 schemars::schema_for!(DeleteContentInput),
3829 ),
3830 ("TrashInput", schemars::schema_for!(TrashInput)),
3831 ("TrashListRequest", schemars::schema_for!(TrashListRequest)),
3832 ] {
3833 let schema = serde_json::to_value(&schema).unwrap();
3834 assert_eq!(
3835 schema["additionalProperties"], false,
3836 "{name}: {schema}"
3837 );
3838 }
3839 }
3840
3841 #[cfg(feature = "schemars")]
3844 #[test]
3845 fn update_profile_docs_state_the_enforced_limits() {
3846 let schema =
3847 serde_json::to_value(schemars::schema_for!(UpdateProfilePayload))
3848 .unwrap();
3849 for (field, max) in [
3850 ("display_name", limits::profile::DISPLAY_NAME_MAX_CHARS),
3851 ("bio", limits::profile::BIO_MAX_CHARS),
3852 ("model_info", limits::profile::MODEL_INFO_MAX_CHARS),
3853 ] {
3854 assert_eq!(
3855 schema["properties"][field]["maxLength"], max,
3856 "{field}: {schema}"
3857 );
3858 }
3859 }
3860}
3861
3862#[cfg(test)]
3863mod entry_type_filter_tests {
3864 use super::GetGovernanceLogInput;
3865 use crate::enums::GovernanceLogEntryType;
3866
3867 #[test]
3868 fn a_filter_names_a_known_type_or_is_refused_by_name() {
3869 let ok: GetGovernanceLogInput = serde_json::from_value(
3870 serde_json::json!({"entry_type": "data_set"}),
3871 )
3872 .unwrap();
3873 assert_eq!(ok.entry_type, Some(GovernanceLogEntryType::DataSet));
3874 let none: GetGovernanceLogInput =
3875 serde_json::from_value(serde_json::json!({"entry_type": null}))
3876 .unwrap();
3877 assert_eq!(none.entry_type, None);
3878 let err = serde_json::from_value::<GetGovernanceLogInput>(
3879 serde_json::json!({"entry_type": "nope"}),
3880 )
3881 .unwrap_err()
3882 .to_string();
3883 assert!(err.contains("nope") && err.contains("data_set"), "{err}");
3884 }
3885}