use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::addr::{Handle, Slug};
use crate::error::{
Validate, ValidationError, check_chars, check_known, check_opt_chars, check_unique_ids,
};
use crate::http::Patch;
use crate::id::Id;
use crate::limits::{GROUP_DM_MAX_OTHERS, NAME_MAX_CHARS, PURPOSE_MAX_CHARS, TOPIC_MAX_CHARS};
use crate::perms::Perms;
use crate::time::Timestamp;
unit_enum! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum ChannelType {
Text = "text",
Voice = "voice",
Announcement = "announcement",
Dm = "dm",
GroupDm = "group_dm",
}
}
impl ChannelType {
pub const fn is_dm(self) -> bool {
matches!(self, ChannelType::Dm | ChannelType::GroupDm)
}
}
unit_enum! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum Visibility {
Public = "public",
Private = "private",
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct Channel {
pub id: Id,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub space_id: Option<Id>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub category_id: Option<Id>,
#[serde(rename = "type")]
pub kind: ChannelType,
pub visibility: Visibility,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub slug: Option<Slug>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub topic: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub purpose: Option<String>,
pub position: u32,
pub synced: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub archived_at: Option<Timestamp>,
pub head_seq: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub last_message_id: Option<Id>,
pub dm_member_ids: Vec<Id>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub my_perms: Option<Perms>,
pub created_at: Timestamp,
#[serde(default)]
pub member_count: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub created_by: Option<Id>,
#[serde(default)]
pub slow_mode_s: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub retention_days: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub file_retention_days: Option<u32>,
#[serde(default)]
pub has_guests: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub assistant_reader_ids: Vec<Id>,
#[serde(default, skip_serializing_if = "is_zero_u8")]
pub user_limit: u8,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub recording_auto: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub recording_audio_days: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub recording_require_consent: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub recording_required: Option<bool>,
#[serde(
default,
skip_serializing_if = "crate::recording::RecordingRule::is_none"
)]
pub recording_rule: crate::recording::RecordingRule,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub meeting: Option<crate::api::meetings::ChannelMeeting>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub closing: bool,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub members_can_leave: bool,
}
impl Channel {
pub fn new(id: Id, kind: ChannelType, visibility: Visibility, created_at: Timestamp) -> Self {
Channel {
id,
space_id: None,
category_id: None,
kind,
visibility,
slug: None,
name: None,
topic: None,
purpose: None,
position: 0,
synced: false,
archived_at: None,
head_seq: 0,
last_message_id: None,
dm_member_ids: Vec::new(),
my_perms: None,
created_at,
member_count: 0,
created_by: None,
slow_mode_s: 0,
retention_days: None,
file_retention_days: None,
has_guests: false,
assistant_reader_ids: Vec::new(),
user_limit: 0,
recording_auto: false,
recording_audio_days: None,
recording_require_consent: None,
recording_required: None,
recording_rule: crate::recording::RecordingRule::None,
meeting: None,
closing: false,
members_can_leave: false,
}
}
}
pub const RECORDING_AUDIO_DAYS_MAX: u16 = 36_500;
fn is_zero_u8(v: &u8) -> bool {
*v == 0
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct CreateChannelRequest {
pub slug: Slug,
pub name: String,
#[serde(rename = "type")]
pub kind: ChannelType,
pub visibility: Visibility,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub category_id: Option<Id>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub topic: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub purpose: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub position: Option<u32>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub member_ids: Vec<Id>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub user_limit: Option<u8>,
}
impl Validate for CreateChannelRequest {
fn validate(&self) -> Result<(), ValidationError> {
check_unique_ids("member_ids", &self.member_ids, 0, 100)?;
crate::voice::check_user_limit(self.user_limit)?;
if self.user_limit.is_some() && self.kind != ChannelType::Voice {
return Err(ValidationError::invalid(
"user_limit",
"user_limit is for voice channels only",
));
}
check_chars("name", &self.name, 1, NAME_MAX_CHARS)?;
match self.kind {
ChannelType::Text | ChannelType::Voice | ChannelType::Announcement => {}
_ => {
return Err(ValidationError::invalid(
"type",
"type must be text, voice or announcement",
));
}
}
check_known("visibility", self.visibility)?;
check_opt_chars("topic", self.topic.as_deref(), 0, TOPIC_MAX_CHARS)?;
check_opt_chars("purpose", self.purpose.as_deref(), 0, PURPOSE_MAX_CHARS)
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct UpdateChannelRequest {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub slug: Option<Slug>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "name_or_clear"
)]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "Patch::is_keep")]
pub topic: Patch<String>,
#[serde(default, skip_serializing_if = "Patch::is_keep")]
pub purpose: Patch<String>,
#[serde(default, skip_serializing_if = "Patch::is_keep")]
pub category_id: Patch<Id>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub position: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub user_limit: Option<u8>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub recording_auto: Option<bool>,
#[serde(default, skip_serializing_if = "Patch::is_keep")]
pub recording_audio_days: Patch<u16>,
#[serde(default, skip_serializing_if = "Patch::is_keep")]
pub recording_require_consent: Patch<bool>,
#[serde(default, skip_serializing_if = "Patch::is_keep")]
pub recording_required: Patch<bool>,
}
fn name_or_clear<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Option<String>, D::Error> {
Ok(Some(Option::<String>::deserialize(d)?.unwrap_or_default()))
}
impl Validate for UpdateChannelRequest {
fn validate(&self) -> Result<(), ValidationError> {
crate::voice::check_user_limit(self.user_limit)?;
if let Patch::Set(d) = self.recording_audio_days
&& d > RECORDING_AUDIO_DAYS_MAX
{
return Err(ValidationError::invalid(
"recording_audio_days",
format!("recording_audio_days must be 0–{RECORDING_AUDIO_DAYS_MAX}"),
));
}
check_opt_chars("name", self.name.as_deref(), 0, NAME_MAX_CHARS)?;
check_opt_chars(
"topic",
self.topic.as_set().map(String::as_str),
0,
TOPIC_MAX_CHARS,
)?;
check_opt_chars(
"purpose",
self.purpose.as_set().map(String::as_str),
0,
PURPOSE_MAX_CHARS,
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct ChannelMember {
pub channel_id: Id,
pub member_id: Id,
pub joined_at: Timestamp,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct AddChannelMembersRequest {
pub member_ids: Vec<Id>,
}
impl Validate for AddChannelMembersRequest {
fn validate(&self) -> Result<(), ValidationError> {
check_unique_ids("member_ids", &self.member_ids, 1, 100)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct RemoveChannelMemberRequest {
pub member_id: Id,
}
impl Validate for RemoveChannelMemberRequest {
fn validate(&self) -> Result<(), ValidationError> {
Ok(())
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ChannelTypingRequest {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub thread_id: Option<Id>,
}
impl Validate for ChannelTypingRequest {
fn validate(&self) -> Result<(), ValidationError> {
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct OpenDmRequest {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub member_ids: Vec<Id>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub usernames: Vec<Handle>,
}
impl Validate for OpenDmRequest {
fn validate(&self) -> Result<(), ValidationError> {
let total = self.member_ids.len() + self.usernames.len();
if total == 0 || total > GROUP_DM_MAX_OTHERS {
return Err(ValidationError::invalid(
if self.member_ids.is_empty() {
"usernames"
} else {
"member_ids"
},
format!(
"member_ids and usernames must name 1–{GROUP_DM_MAX_OTHERS} members in all, got {total}"
),
));
}
check_unique_ids("member_ids", &self.member_ids, 0, GROUP_DM_MAX_OTHERS)?;
let mut seen = std::collections::HashSet::with_capacity(self.usernames.len());
for h in &self.usernames {
if !seen.insert(h.as_str()) {
return Err(ValidationError::invalid(
"usernames",
format!("usernames holds {h} twice"),
));
}
}
Ok(())
}
}
pub type ChannelPage = crate::http::Page<Channel>;
unit_enum! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum ChannelListScope {
Browse = "browse",
Joined = "joined",
Admin = "admin",
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ChannelListQuery {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub scope: Option<ChannelListScope>,
#[serde(default)]
pub include_archived: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub q: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub after: Option<Id>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
}
impl Validate for ChannelListQuery {
fn validate(&self) -> Result<(), ValidationError> {
if let Some(s) = self.scope {
check_known("scope", s)?;
}
check_opt_chars("q", self.q.as_deref(), 1, 80)?;
crate::api::spaces::check_page_limit(self.limit)
}
}
unit_enum! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum MyChannelsKind {
Dm = "dm",
Space = "space",
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct MyChannelsQuery {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub kind: Option<MyChannelsKind>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub after: Option<Id>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
}
impl Validate for MyChannelsQuery {
fn validate(&self) -> Result<(), ValidationError> {
if let Some(k) = self.kind {
check_known("kind", k)?;
}
crate::api::spaces::check_page_limit(self.limit)
}
}
impl From<MyChannelsQuery> for MyChannelsPageQuery {
fn from(q: MyChannelsQuery) -> MyChannelsPageQuery {
MyChannelsPageQuery {
kind: q.kind,
after: q.after,
limit: q.limit,
..MyChannelsPageQuery::default()
}
}
}
unit_enum! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum MyChannelsOrder {
Id = "id",
Activity = "activity",
}
}
pub const FEATURE_GROUP_DM_NAMES: &str = "group_dm_names";
pub const FEATURE_DM_ORDER_ACTIVITY: &str = "dm_order_activity";
pub const FEATURE_HTTP_TYPING: &str = "http_typing";
pub const FEATURE_CHANNEL_MEMBERS_CAN_LEAVE: &str = "channel_members_can_leave";
pub const MY_CHANNELS_ACTIVITY_LIMIT_MAX: u32 = 200;
pub const MY_CHANNELS_CURSOR_MAX_BYTES: usize = 64;
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct MyChannelsPageQuery {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub kind: Option<MyChannelsKind>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub order: Option<MyChannelsOrder>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub after: Option<Id>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub before: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
}
impl MyChannelsPageQuery {
pub fn by_activity(&self) -> bool {
self.order == Some(MyChannelsOrder::Activity)
}
}
impl Validate for MyChannelsPageQuery {
fn validate(&self) -> Result<(), ValidationError> {
if let Some(k) = self.kind {
check_known("kind", k)?;
}
if let Some(o) = self.order {
check_known("order", o)?;
}
if !self.by_activity() {
if self.before.is_some() {
return Err(ValidationError::invalid(
"before",
"before is for order=activity",
));
}
return crate::api::spaces::check_page_limit(self.limit);
}
if self.kind != Some(MyChannelsKind::Dm) {
return Err(ValidationError::invalid(
"kind",
"order=activity needs kind=dm",
));
}
if self.after.is_some() {
return Err(ValidationError::invalid(
"after",
"order=activity pages with before",
));
}
if let Some(b) = &self.before
&& (b.is_empty() || b.len() > MY_CHANNELS_CURSOR_MAX_BYTES)
{
return Err(ValidationError::invalid("before", "before is not a cursor"));
}
if let Some(l) = self.limit
&& !(1..=MY_CHANNELS_ACTIVITY_LIMIT_MAX).contains(&l)
{
return Err(ValidationError::invalid(
"limit",
format!("limit must be 1–{MY_CHANNELS_ACTIVITY_LIMIT_MAX}"),
));
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct MyChannelsPage {
pub items: Vec<Channel>,
pub has_more: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub next: Option<String>,
}
impl MyChannelsPage {
pub fn new(items: Vec<Channel>, has_more: bool) -> MyChannelsPage {
MyChannelsPage {
items,
has_more,
next: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct AddChannelMembersResponse {
pub added: Vec<ChannelMember>,
pub already: Vec<Id>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub skipped: Vec<Id>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub banned: Vec<Id>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ReorderChannelItem {
pub channel_id: Id,
#[serde(default, skip_serializing_if = "Patch::is_keep")]
pub category_id: Patch<Id>,
pub position: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ReorderChannelsRequest {
pub items: Vec<ReorderChannelItem>,
}
impl Validate for ReorderChannelsRequest {
fn validate(&self) -> Result<(), ValidationError> {
let ids: Vec<Id> = self.items.iter().map(|i| i.channel_id).collect();
check_unique_ids("items", &ids, 1, 500)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct ReorderChannelsResponse {
pub channels: Vec<Channel>,
}