robespierre_models/
events.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    channels::{Channel, ChannelField, Message, PartialChannel, PartialMessage},
5    id::{ChannelId, MemberId, MessageId, RoleId, ServerId, UserId},
6    servers::{
7        Member, MemberField, PartialMember, PartialRole, PartialServer, RoleField, Server,
8        ServerField,
9    },
10    users::{RelationshipStatus, User, UserField, UserPatch},
11};
12
13/// Any message the client can send to the server.
14#[derive(Serialize, Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
15#[serde(tag = "type")]
16pub enum ClientToServerEvent {
17    Authenticate { token: String },
18    BeginTyping { channel: ChannelId },
19    EndTyping { channel: ChannelId },
20    Ping { data: u32 },
21}
22
23/// Event received after authentication.
24#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
25pub struct ReadyEvent {
26    pub users: Vec<User>,
27    pub servers: Vec<Server>,
28    pub channels: Vec<Channel>,
29    pub members: Vec<Member>,
30}
31
32/// Any message that the server can send to the client.
33#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
34#[serde(tag = "type")]
35#[serde(deny_unknown_fields)]
36pub enum ServerToClientEvent {
37    Error {
38        error: String,
39    },
40    Authenticated,
41    Pong {
42        data: u32,
43    },
44    Ready {
45        #[serde(flatten)]
46        event: ReadyEvent,
47    },
48    Message {
49        #[serde(flatten)]
50        message: Message,
51    },
52    MessageUpdate {
53        id: MessageId,
54        channel: ChannelId,
55        data: PartialMessage,
56    },
57    MessageDelete {
58        id: MessageId,
59        channel: ChannelId,
60    },
61    ChannelCreate {
62        #[serde(flatten)]
63        channel: Channel,
64    },
65    ChannelUpdate {
66        id: ChannelId,
67        data: PartialChannel,
68        #[serde(default)]
69        clear: Option<ChannelField>,
70    },
71    ChannelDelete {
72        id: ChannelId,
73    },
74    ChannelGroupJoin {
75        id: ChannelId,
76        user: UserId,
77    },
78    ChannelGroupLeave {
79        id: ChannelId,
80        user: UserId,
81    },
82    ChannelStartTyping {
83        id: ChannelId,
84        user: UserId,
85    },
86    ChannelStopTyping {
87        id: ChannelId,
88        user: UserId,
89    },
90    ChannelAck {
91        id: ChannelId,
92        user: UserId,
93        message_id: MessageId,
94    },
95    ServerUpdate {
96        id: ServerId,
97        data: PartialServer,
98        #[serde(default)]
99        clear: Option<ServerField>,
100    },
101    ServerDelete {
102        id: ServerId,
103    },
104    ServerMemberUpdate {
105        id: MemberId,
106        data: PartialMember,
107        #[serde(default)]
108        clear: Option<MemberField>,
109    },
110    ServerMemberJoin {
111        id: ServerId,
112        user: UserId,
113    },
114    ServerMemberLeave {
115        id: ServerId,
116        user: UserId,
117    },
118    ServerRoleUpdate {
119        id: ServerId,
120        role_id: RoleId,
121        data: PartialRole,
122        #[serde(default)]
123        clear: Option<RoleField>,
124    },
125    ServerRoleDelete {
126        id: ServerId,
127        role_id: RoleId,
128    },
129    UserUpdate {
130        id: UserId,
131        data: UserPatch,
132        #[serde(default)]
133        clear: Option<UserField>,
134    },
135    UserRelationship {
136        id: UserId,
137        user: UserId,
138        status: RelationshipStatus,
139    },
140}
141
142pub trait HasWsUrl {
143    fn get_ws_url(&self) -> &str;
144}