blatherskite

a toy discord-like chat app backend written for a swe class
Log | Files | Refs | README

responses.rs (7717B)


      1 use poem_openapi::{
      2     payload::{Json, PlainText},
      3     ApiResponse, Object,
      4 };
      5 use serde::{Deserialize, Serialize};
      6 
      7 use crate::models;
      8 
      9 #[derive(Object, Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
     10 /// Object representing a user
     11 pub struct User {
     12     pub id: i64,
     13     pub username: String,
     14     pub email: String,
     15 }
     16 
     17 #[derive(Object, Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
     18 /// Object representing a group.
     19 /// No guarantees are made for the order of any vector in this struct.
     20 pub struct Group {
     21     pub id: i64,
     22     pub name: String,
     23     // The IDs of the group members
     24     pub members: Vec<i64>,
     25     // The IDs of the group's channels
     26     pub channels: Vec<i64>,
     27 	// The IDs of the group's users with admin permissions
     28 	pub admin: Vec<i64>,
     29 	// The ID of the owner of the group
     30 	pub owner: i64,
     31 	// Whether or not the group is a DM
     32 	pub is_dm: bool,
     33 }
     34 
     35 #[derive(Object, Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
     36 /// Object representing a group's channel.
     37 /// No guarantees are made for the order of any vector in this struct.
     38 pub struct ChannelOrig {
     39     pub id: i64,
     40     pub name: String,
     41 	// ID of the group the channel is in
     42 	pub group: i64,
     43 	// The IDs of the group members
     44     pub members: Vec<i64>,
     45 	// Whether or not the channel is private
     46 	pub private: bool,
     47 }
     48 
     49 #[derive(Object, Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
     50 /// Object representing a message from a user.
     51 pub struct Message {
     52     pub id: i64,
     53     pub channel: i64,
     54     pub author: i64,
     55     pub content: String,
     56 	// The (optional) thread associated with the message
     57 	pub thread: Option<i64>
     58 }
     59 
     60 #[derive(ApiResponse)]
     61 pub enum LoginResponse {
     62 	/// Returns a JWT encoding the user's ID and the token expiration date
     63 	/// (1 day from now) that can be used to authenticate future requests
     64     #[oai(status = 200)]
     65     Success(PlainText<String>),
     66     /// User ID not found
     67     #[oai(status = 404)]
     68     NotFound,
     69     /// Incorrect hash provided
     70     #[oai(status = 401)]
     71     Unauthorized,
     72     /// Hash provided is of incorrect length
     73     #[oai(status = 400)]
     74     BadRequest,
     75     /// Internal server error when attempting to access database/sign key
     76     #[oai(status = 500)]
     77     InternalError(PlainText<String>),
     78 }
     79 
     80 #[derive(ApiResponse)]
     81 pub enum UserResponse {
     82     /// Returns the user requested.
     83     #[oai(status = 200)]
     84     Success(Json<User>),
     85     /// Invalid ID.
     86     #[oai(status = 404)]
     87     NotFound,
     88     /// Internal server error: likely due to a database operation failing
     89     #[oai(status = 500)]
     90     InternalError(PlainText<String>),
     91 }
     92 
     93 #[derive(ApiResponse)]
     94 pub enum CreateUserResponse {
     95     /// Returns the user requested.
     96     #[oai(status = 200)]
     97     Success(Json<User>),
     98     /// Recieved a bad argument when specifying the user. Returns error type, such as:
     99     /// - found empty string for any of the arguments
    100     /// - invalid email
    101     #[oai(status = 400)]
    102     BadRequest(PlainText<String>),
    103     /// Internal server error: likely due to a database operation failing
    104     #[oai(status = 500)]
    105     InternalError(PlainText<String>),
    106 }
    107 
    108 #[derive(ApiResponse)]
    109 pub enum DeleteResponse {
    110     /// The delete operation succeeded
    111     #[oai(status = 200)]
    112     Success,
    113     /// You are not authorized to perform the action
    114     #[oai(status = 401)]
    115     Unauthorized,
    116     /// Invalid ID. Content specifies which of the IDs passed is invalid.
    117     #[oai(status = 404)]
    118     NotFound(PlainText<String>),
    119     /// Internal server error: likely due to a database operation failing
    120     #[oai(status = 500)]
    121     InternalError(PlainText<String>),
    122 }
    123 
    124 #[derive(ApiResponse)]
    125 pub enum GroupResponse {
    126     /// Returns the group requested
    127     #[oai(status = 200)]
    128     Success(Json<Group>),
    129     /// Invalid ID or user is not a member of specified group.
    130     #[oai(status = 404)]
    131     NotFound,
    132     /// Internal server error when attempting to access database
    133     #[oai(status = 500)]
    134     InternalError(PlainText<String>),
    135 }
    136 
    137 #[derive(ApiResponse)]
    138 pub enum CreateGroupResponse {
    139     /// Returns the group requested
    140     #[oai(status = 200)]
    141     Success(Json<Group>),
    142 	/// Invalid User ID (only possible when making a DM).
    143     #[oai(status = 404)]
    144     NotFound,
    145     /// Invalid parameter, such as:
    146     /// - empty string for name
    147     /// - bad string
    148     #[oai(status = 400)]
    149     BadRequest(PlainText<String>),
    150     /// Internal server error: likely due to a database operation failing
    151     #[oai(status = 500)]
    152     InternalError(PlainText<String>),
    153 }
    154 
    155 #[derive(ApiResponse)]
    156 pub enum ChannelResponse {
    157     /// Returns the channel requested
    158     #[oai(status = 200)]
    159     Success(Json<ChannelOrig>),
    160 	/// Invalid ID or user is not a member of specified channel.
    161     #[oai(status = 404)]
    162     NotFound,
    163     /// Internal server error: likely due to a database operation failing
    164     #[oai(status = 500)]
    165     InternalError(PlainText<String>),
    166 }
    167 
    168 
    169 #[derive(ApiResponse)]
    170 pub enum ChannelResponse2 {
    171     /// Returns the channel requested
    172     #[oai(status = 200)]
    173     Success(Json<models::Channel>),
    174 	/// Invalid ID or user is not a member of specified channel.
    175     #[oai(status = 404)]
    176     NotFound,
    177     /// Internal server error: likely due to a database operation failing
    178     #[oai(status = 500)]
    179     InternalError(PlainText<String>),
    180 }
    181 
    182 
    183 #[derive(ApiResponse)]
    184 pub enum CreateChannelResponse {
    185     /// Returns the channel requested
    186     #[oai(status = 200)]
    187     Success(Json<ChannelOrig>),
    188 	/// You are not authorized to perform the action
    189     #[oai(status = 401)]
    190     Unauthorized,
    191     /// Invalid parameter, such as:
    192     /// - empty string for name
    193     /// - bad string
    194     #[oai(status = 400)]
    195     BadRequest(PlainText<String>),
    196     /// Invalid ID. Content specifies which of the IDs passed is invalid.
    197     #[oai(status = 404)]
    198     NotFound(PlainText<String>),
    199     /// Internal server error: likely due to a database operation failing
    200     #[oai(status = 500)]
    201     InternalError(PlainText<String>),
    202 }
    203 
    204 #[derive(ApiResponse)]
    205 pub enum GenericResponse {
    206     /// Action succeeded.
    207     #[oai(status = 200)]
    208     Success,
    209 	/// You are not authorized to perform the action
    210     #[oai(status = 401)]
    211     Unauthorized,
    212     /// Recieved a bad argument.    
    213     #[oai(status = 400)]
    214     BadRequest(PlainText<String>),
    215     /// Invalid ID. Content specifies which of the IDs passed is invalid.
    216     #[oai(status = 404)]
    217     NotFound(PlainText<String>),
    218     /// Internal server error: likely due to a database operation failing
    219     #[oai(status = 500)]
    220     InternalError(PlainText<String>),
    221 }
    222 
    223 #[derive(ApiResponse)]
    224 pub enum MessagesResponse {
    225     /// Returns the messages requested
    226     #[oai(status = 200)]
    227     Success(Json<Vec<Message>>),
    228     /// Invalid ID, no messages found, or user is not a member of specified channel.
    229     #[oai(status = 404)]
    230     NotFound(PlainText<String>),
    231     /// Offset or number of messages requested is bad. Content specifies which error occured.
    232     #[oai(status = 400)]
    233     BadRequest(PlainText<String>),
    234 }
    235 
    236 #[derive(ApiResponse)]
    237 pub enum MembersResponse {
    238     /// Returns the members of current channel/group
    239     #[oai(status = 200)]
    240     Success(Json<Vec<User>>),
    241     /// Invalid ID
    242     #[oai(status = 404)]
    243     NotFound,
    244 }
    245 
    246 #[derive(ApiResponse)]
    247 pub enum GroupsResponse {
    248     /// Returns the groups the user is a memmber of
    249     #[oai(status = 200)]
    250     Success(Json<Vec<Group>>),
    251     /// Invalid user ID
    252     #[oai(status = 404)]
    253     NotFound,
    254 }
    255 
    256 #[derive(ApiResponse)]
    257 pub enum ChannelsResponse {
    258     /// Returns the channels in a group
    259     #[oai(status = 200)]
    260     Success(Json<Vec<ChannelOrig>>),
    261     /// Invalid group ID
    262     #[oai(status = 404)]
    263     NotFound,
    264     /// Internal server error: likely due to a database operation failing
    265     #[oai(status = 500)]
    266     InternalError(PlainText<String>),
    267 }