sew-proto 0.11.6

Sew wire contracts: ids, addresses, errors, HTTP and gateway bodies, permissions, policies, audit.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//! Spaces, categories, roles and overwrites.

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::addr::Slug;
use crate::error::{
    Validate, ValidationError, check_chars, check_known, check_opt_chars, check_unique_ids,
};
use crate::http::Page;
use crate::http::Patch;
use crate::id::Id;
use crate::limits::{
    NAME_MAX_CHARS, PAGE_LIMIT_MAX, SPACE_DEFAULT_CHANNELS_MAX, SPACE_DESCRIPTION_MAX_CHARS,
};
use crate::perms::Perms;
use crate::time::Timestamp;

pub(crate) fn check_space_slug(field: &str, slug: &Slug) -> Result<(), ValidationError> {
    if slug.is_reserved_for_space() {
        return Err(ValidationError::invalid(
            field,
            "the space slug \"dm\" is reserved",
        ));
    }
    Ok(())
}

/// A space: a group of channels with its own roles.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct Space {
    /// The space id.
    pub id: Id,
    /// The slug, unique in the install.
    pub slug: Slug,
    /// The name.
    pub name: String,
    /// Signed icon URL.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub icon_url: Option<String>,
    /// Signed 64 px icon URL.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub icon_thumb_url: Option<String>,
    /// The space owner's member id.
    pub owner_id: Id,
    /// Number of members.
    pub member_count: u32,
    /// When the space was created.
    pub created_at: Timestamp,
    /// Who may join by themself. Old payloads without it read as `invite`.
    #[serde(default = "SpaceJoinPolicy::default_invite")]
    pub join_policy: SpaceJoinPolicy,
    /// The viewer's base permissions in the space; absent for non-members.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub my_perms: Option<Perms>,
    /// The channels a joining member lands in (the open join and an add), in the
    /// order MANAGE_SPACE set them; at most 10, absent when none. Guests never see it.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub default_channel_ids: Vec<Id>,
    /// What the space is for, 1–300 characters; absent when unset.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// The channel holding the space's rules (a public text or announcement
    /// channel of the space); absent when unset. Guests never see it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rules_channel_id: Option<Id>,
    /// Where a newcomer starts (a public text or announcement channel of the
    /// space); absent when unset. Guests never see it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub welcome_channel_id: Option<Id>,
    /// The App the space belongs to (`[a-z0-9][a-z0-9._-]{0,63}`); absent when the space
    /// is not bound. A bound space gets its members only through mgmt calls for that App.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub app_id: Option<String>,
    /// The display name K2 gave the Space's App (`/mgmt/v1/apps/{app_id}/settings`), for
    /// labels; absent when the Space is not bound or the App has no name.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub app_display_name: Option<String>,
    /// Since 0.8.0 (flag `app_participant_links`): the participant Apps, whose sessions see
    /// the Space and act in it as the person, as the governing App's do (`app_id` stays the
    /// governing App). Sorted; at most 8; absent when none. Unscoped and home-client sessions
    /// get the whole list; an App session gets only its own id, when its App is a participant,
    /// and nothing otherwise.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub participant_app_ids: Vec<String>,
    /// The App the Space was unbound from while it is a read-only orphan (posts, joins,
    /// calls and member adds are `422 invalid_state` `space_orphaned`); absent otherwise.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub orphaned_from: Option<String>,
    /// A hidden Space (the Meetings Space) is left out of `GET /spaces`,
    /// browse and the directory, and out of READY unless IDENTIFY declares `hidden_spaces`;
    /// clients read it by id. Absent when `false`.
    #[serde(default, skip_serializing_if = "is_false")]
    pub hidden: bool,
    /// A private Space is hidden from everyone outside it (lists, the joinable
    /// directory, search, mentionables, previews and GET answer the masked 404 of an unknown
    /// Space) and is invite-only (`join_policy` is `invite`). Absent when `false`.
    #[serde(default, skip_serializing_if = "is_false")]
    pub private: bool,
    /// Who governs the Space's structure; absent for `sew` (today's model). `app`:
    /// the governing App (`app_id`) decides membership, private channels and levels.
    /// `server`: the server-governed Space, governed by K2's daemon over mgmt.
    #[serde(
        default = "SpaceGovernance::default_sew",
        skip_serializing_if = "SpaceGovernance::is_sew"
    )]
    pub governance: SpaceGovernance,
}

impl Space {
    /// A `Space` with its required fields: every count 0, every flag `false`, every list
    /// empty, the other fields at their serde defaults and every optional field unset.
    pub fn new(
        id: Id,
        slug: Slug,
        name: impl Into<String>,
        owner_id: Id,
        created_at: Timestamp,
    ) -> Self {
        Space {
            id,
            slug,
            name: name.into(),
            icon_url: None,
            icon_thumb_url: None,
            owner_id,
            member_count: 0,
            created_at,
            join_policy: SpaceJoinPolicy::default_invite(),
            my_perms: None,
            default_channel_ids: Vec::new(),
            description: None,
            rules_channel_id: None,
            welcome_channel_id: None,
            app_id: None,
            app_display_name: None,
            participant_app_ids: Vec::new(),
            orphaned_from: None,
            hidden: false,
            private: false,
            governance: SpaceGovernance::default_sew(),
        }
    }
}

#[allow(clippy::trivially_copy_pass_by_ref)]
fn is_false(v: &bool) -> bool {
    !*v
}

unit_enum! {
    /// Who governs a Space's structure.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
    pub enum SpaceGovernance {
        /// Sew's own model: roles, overwrites and membership are edited in Sew (the default).
        Sew = "sew",
        /// The governing App decides membership, private channels and levels (K2's sync).
        App = "app",
        /// The server-governed Space (named `K5 Space`), one per K2 install, owned by `k2:server`:
        /// K2's daemon decides its structure over mgmt; no App session ever reaches it.
        Server = "server",
    }
}

impl SpaceGovernance {
    /// `sew`, the default (also the serde default for old payloads).
    pub const fn default_sew() -> SpaceGovernance {
        SpaceGovernance::Sew
    }

    /// Whether this is `sew` (the omitted value).
    #[allow(clippy::trivially_copy_pass_by_ref)]
    pub fn is_sew(&self) -> bool {
        *self == SpaceGovernance::Sew
    }
}

unit_enum! {
    /// A person's level in an App-governed Space (K2's sync sets it), and an App's
    /// `max_level`. `moderator` and `admin` are the Space's managed roles; `member` is
    /// `@everyone`.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, JsonSchema)]
    pub enum SpaceLevel {
        /// `@everyone` only.
        Member = "member",
        /// The managed Moderator role.
        Moderator = "moderator",
        /// The managed Admin role (the Moderator bits plus MENTION_EVERYONE).
        Admin = "admin",
    }
}

/// Body of the create-space route.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct CreateSpaceRequest {
    /// The slug; `dm` is reserved.
    pub slug: Slug,
    /// The name, 1–100 characters.
    pub name: String,
    /// Who may join by themself; default `invite`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub join_policy: Option<SpaceJoinPolicy>,
}

impl Validate for CreateSpaceRequest {
    fn validate(&self) -> Result<(), ValidationError> {
        check_space_slug("slug", &self.slug)?;
        check_chars("name", &self.name, 1, NAME_MAX_CHARS)?;
        if let Some(p) = self.join_policy {
            check_known("join_policy", p)?;
        }
        Ok(())
    }
}

/// Body of the update-space route.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct UpdateSpaceRequest {
    /// A new slug.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub slug: Option<Slug>,
    /// A new name, 1–100 characters.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// A new join policy.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub join_policy: Option<SpaceJoinPolicy>,
    /// New default channels: 0–10 distinct public, unarchived channels of the
    /// space; `[]` clears them.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_channel_ids: Option<Vec<Id>>,
    /// Set or clear the description (≤ 300 characters; a blank one clears).
    #[serde(default, skip_serializing_if = "Patch::is_keep")]
    pub description: Patch<String>,
    /// Set or clear the rules channel: a public, unarchived text or announcement
    /// channel of the space.
    #[serde(default, skip_serializing_if = "Patch::is_keep")]
    pub rules_channel_id: Patch<Id>,
    /// Set or clear the welcome channel, with the rules channel's rules.
    #[serde(default, skip_serializing_if = "Patch::is_keep")]
    pub welcome_channel_id: Patch<Id>,
    /// Make the Space private (`true`: hidden from everyone outside it, `join_policy`
    /// becomes `invite`; `open` with it is refused) or public again (`false`: `join_policy`
    /// becomes the request's, default `open`; `invite` for an App's Space).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub private: Option<bool>,
}

impl Validate for UpdateSpaceRequest {
    fn validate(&self) -> Result<(), ValidationError> {
        if let Some(slug) = &self.slug {
            check_space_slug("slug", slug)?;
        }
        if let Some(p) = self.join_policy {
            check_known("join_policy", p)?;
        }
        if self.private == Some(true) && self.join_policy == Some(SpaceJoinPolicy::Open) {
            return Err(ValidationError::invalid(
                "join_policy",
                "a private space is invite-only",
            ));
        }
        if let Some(ids) = &self.default_channel_ids {
            check_unique_ids("default_channel_ids", ids, 0, SPACE_DEFAULT_CHANNELS_MAX)?;
        }
        check_opt_chars(
            "description",
            self.description.as_set().map(String::as_str),
            0,
            SPACE_DESCRIPTION_MAX_CHARS,
        )?;
        check_opt_chars("name", self.name.as_deref(), 1, NAME_MAX_CHARS)
    }
}

unit_enum! {
    /// What an overwrite targets.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
    pub enum OverwriteTarget {
        /// A role (`@everyone`'s role id is the space id).
        Role = "role",
        /// A single member.
        Member = "member",
    }
}

/// A channel or category permission overwrite.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Overwrite {
    /// Role or member.
    pub target_kind: OverwriteTarget,
    /// The role or member id.
    pub target_id: Id,
    /// Bits allowed.
    pub allow: Perms,
    /// Bits denied.
    pub deny: Perms,
}

impl Validate for Overwrite {
    fn validate(&self) -> Result<(), ValidationError> {
        check_known("target_kind", self.target_kind)?;
        self.allow.validate_overwrite("allow")?;
        self.deny.validate_overwrite("deny")
    }
}

/// A category grouping channels in a space.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Category {
    /// The category id.
    pub id: Id,
    /// Its space.
    pub space_id: Id,
    /// The name.
    pub name: String,
    /// Sort position.
    pub position: u32,
    /// Overwrites that synced channels inherit.
    pub overwrites: Vec<Overwrite>,
}

/// Body of the create-category route.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct CreateCategoryRequest {
    /// The name, 1–100 characters.
    pub name: String,
    /// Sort position; default last.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub position: Option<u32>,
}

impl Validate for CreateCategoryRequest {
    fn validate(&self) -> Result<(), ValidationError> {
        check_chars("name", &self.name, 1, NAME_MAX_CHARS)
    }
}

/// Body of the update-category route.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct UpdateCategoryRequest {
    /// A new name, 1–100 characters.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// A new position.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub position: Option<u32>,
}

impl Validate for UpdateCategoryRequest {
    fn validate(&self) -> Result<(), ValidationError> {
        check_opt_chars("name", self.name.as_deref(), 1, NAME_MAX_CHARS)
    }
}

/// A space role.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Role {
    /// The role id; `@everyone`'s equals the space id.
    pub id: Id,
    /// Its space.
    pub space_id: Id,
    /// The name.
    pub name: String,
    /// Color as `0xRRGGBB`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub color: Option<u32>,
    /// Ladder position; `@everyone` is 0.
    pub position: u32,
    /// Bits the role grants.
    pub perms: Perms,
    /// Whether anyone may `@mention` it.
    pub mentionable: bool,
    /// Whether this is the `@everyone` role.
    pub is_everyone: bool,
    /// Whether members with this role are listed separately. Old payloads read as false.
    #[serde(default)]
    pub hoist: bool,
    /// The level of a managed role (`moderator` or `admin`), created by the switch to
    /// App governance and kept by the App's sync; absent for every other role.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub managed: Option<SpaceLevel>,
}

// ---------------------------------------------------------------- listings, members and categories

unit_enum! {
    /// Who may join a space by themself.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
    pub enum SpaceJoinPolicy {
        /// Any active non-guest member may join.
        Open = "open",
        /// Members join only when someone adds them (the default).
        Invite = "invite",
    }
}

impl SpaceJoinPolicy {
    /// `invite`, the default (also the serde default for old payloads).
    pub const fn default_invite() -> SpaceJoinPolicy {
        SpaceJoinPolicy::Invite
    }
}

unit_enum! {
    /// Which spaces `GET /api/v1/spaces` lists.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
    pub enum SpaceListScope {
        /// Spaces the caller belongs to (the default).
        Mine = "mine",
        /// Open spaces the caller is not in (empty for guests).
        Joinable = "joinable",
        /// Every space (install owners and admins only).
        All = "all",
    }
}

/// Query of `GET /api/v1/spaces`: ordered by id ascending with an `after` cursor.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct SpaceListQuery {
    /// Which spaces; default `mine`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub scope: Option<SpaceListScope>,
    /// Spaces with a larger id.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub after: Option<Id>,
    /// Page size, 1–100 (default 50).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,
}

impl Validate for SpaceListQuery {
    fn validate(&self) -> Result<(), ValidationError> {
        if let Some(s) = self.scope {
            check_known("scope", s)?;
        }
        check_page_limit(self.limit)
    }
}

/// `limit` must be 1–100 when set.
pub(crate) fn check_page_limit(limit: Option<u32>) -> Result<(), ValidationError> {
    if let Some(l) = limit
        && !(1..=PAGE_LIMIT_MAX).contains(&l)
    {
        return Err(ValidationError::invalid(
            "limit",
            format!("limit must be 1–{PAGE_LIMIT_MAX}"),
        ));
    }
    Ok(())
}

/// A space membership. `role_ids` never includes `@everyone`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct SpaceMember {
    /// The space.
    pub space_id: Id,
    /// The member.
    pub member_id: Id,
    /// When they joined.
    pub joined_at: Timestamp,
    /// Their roles, ascending; never `@everyone`.
    pub role_ids: Vec<Id>,
    /// End of an active timeout; present only while it is in the future.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub timeout_until: Option<Timestamp>,
}

/// A page of space members (`GET /api/v1/spaces/{id}/members`).
pub type SpaceMemberPage = Page<SpaceMember>;

/// Query of `GET /api/v1/spaces/{id}/members`: a page forward by member id (`after`, `limit`),
/// or a batch lookup (`ids`). With `ids` the answer holds, ascending, only the ids
/// that are space members the caller may list (the page's view rules), with `has_more: false`;
/// other ids are left out silently. `ids` with any cursor or `limit` is the server's
/// `400 validation_failed` (field `ids`), so a full fixture can set every field.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct SpaceMembersQuery {
    /// Refused (`400`): this list pages forward with `after` only.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub before: Option<Id>,
    /// Members with a larger id.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub after: Option<Id>,
    /// Refused (`400`): this list pages forward with `after` only.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub around: Option<Id>,
    /// Page size, 1–100 (default 50).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,
    /// 1–100 distinct member ids.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ids: Option<crate::id::IdList>,
}

impl SpaceMembersQuery {
    /// The page part of the query.
    pub fn page(&self) -> crate::http::PageQuery {
        crate::http::PageQuery {
            before: self.before,
            after: self.after,
            around: self.around,
            limit: self.limit,
        }
    }
}

impl Validate for SpaceMembersQuery {
    fn validate(&self) -> Result<(), ValidationError> {
        // Cursor and `ids` combinations are the server's checks (see the type docs).
        check_page_limit(self.limit)
    }
}

/// A page of spaces (`GET /api/v1/spaces`).
pub type SpacePage = Page<Space>;

/// Body of `POST /api/v1/spaces/{id}/members/add`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct AddSpaceMembersRequest {
    /// 1–100 distinct member ids.
    pub member_ids: Vec<Id>,
}

impl Validate for AddSpaceMembersRequest {
    fn validate(&self) -> Result<(), ValidationError> {
        check_unique_ids("member_ids", &self.member_ids, 1, 100)
    }
}

/// Response of `POST /api/v1/spaces/{id}/members/add`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct AddSpaceMembersResponse {
    /// The memberships created.
    pub added: Vec<SpaceMember>,
    /// Ids that already were members.
    pub already: Vec<Id>,
    /// Ids that were not added and cannot join (a banned target never fails
    /// the batch); no reason is given here.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub skipped: Vec<Id>,
    /// The ids of `skipped` that are banned: present only for BAN_MEMBERS holders and install
    /// owners and admins.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub banned: Vec<Id>,
}

/// Body of `POST /api/v1/spaces/{id}/members/remove`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct RemoveSpaceMemberRequest {
    /// The member to remove.
    pub member_id: Id,
}

impl Validate for RemoveSpaceMemberRequest {
    fn validate(&self) -> Result<(), ValidationError> {
        Ok(())
    }
}

/// Body of `POST /api/v1/spaces/{id}/transfer`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct TransferSpaceRequest {
    /// The new owner.
    pub member_id: Id,
}

impl Validate for TransferSpaceRequest {
    fn validate(&self) -> Result<(), ValidationError> {
        Ok(())
    }
}

/// One category's new position.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ReorderCategoryItem {
    /// The category.
    pub category_id: Id,
    /// Its new position.
    pub position: u32,
}

/// Body of `POST /api/v1/spaces/{id}/categories/reorder`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ReorderCategoriesRequest {
    /// 1–200 items with distinct category ids.
    pub items: Vec<ReorderCategoryItem>,
}

impl Validate for ReorderCategoriesRequest {
    fn validate(&self) -> Result<(), ValidationError> {
        let ids: Vec<Id> = self.items.iter().map(|i| i.category_id).collect();
        check_unique_ids("items", &ids, 1, 200)
    }
}

/// The whole ordered category list of a space.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct CategoriesResponse {
    /// Every category, by position.
    pub categories: Vec<Category>,
}