blatherskite

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

commit 422c4fe5ae18daa4866e3033bf70034555d49dfb
parent 0a36cddf2af0adcd1f67830446f8d644a8fd1e1a
Author: quantumish <freifeld.david@gmail.com>
Date:   Sat, 24 Sep 2022 18:22:32 -0700

Split out responses/datatypes into other module

Diffstat:
Mscuttlebutt/src/main.rs | 167++-----------------------------------------------------------------------------
Ascuttlebutt/src/responses.rs | 169+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 172 insertions(+), 164 deletions(-)

diff --git a/scuttlebutt/src/main.rs b/scuttlebutt/src/main.rs @@ -10,39 +10,8 @@ use poem_openapi::{ use serde::{Deserialize, Serialize}; use sha2::Sha256; -struct Api; - -#[derive(Object, Serialize, Deserialize)] -struct User { - id: i64, - username: String, - email: String, -} - -#[derive(Object)] -struct Group { - id: i64, - name: String, - // The IDs of the group members - members: Vec<i64>, - // The IDs of the group's channels - channels: Vec<i64>, -} - -#[derive(Object)] -struct Channel { - id: i64, - name: String, - members: Vec<i64>, -} - -#[derive(Object)] -struct Message { - id: i64, - channel: i64, - author: i64, - content: String, -} +pub mod responses; +pub use responses::*; type ServerKey = Hmac<Sha256>; @@ -67,137 +36,7 @@ async fn api_checker(req: &Request, api_key: ApiKey) -> Option<User> { VerifyWithKey::<User>::verify_with_key(api_key.key.as_str(), server_key).ok() } -#[derive(ApiResponse)] -enum UserResponse { - /// Returns the user requested. - #[oai(status = 200)] - User(Json<User>), - /// Invalid ID. Content specifies which of the IDs passed is invalid. - #[oai(status = 404)] - NotFound(PlainText<String>), -} - -#[derive(ApiResponse)] -enum CreateUserResponse { - /// Returns the user requested. - #[oai(status = 200)] - User(Json<User>), - /// Recieved a bad argument when specifying the user. Returns error type, such as: - /// - found empty string for any of the arguments - /// - invalid email - #[oai(status = 400)] - BadRequest, -} - -#[derive(ApiResponse)] -enum DeleteResponse { - /// The delete operation succeeded - #[oai(status = 200)] - Success, - /// You are not authorized to perform the action - #[oai(status = 401)] - Unauthorized, - /// Invalid ID. Content specifies which of the IDs passed is invalid. - #[oai(status = 404)] - NotFound(PlainText<String>), -} - -#[derive(ApiResponse)] -enum GroupResponse { - /// Returns the group requested - #[oai(status = 200)] - Group(Json<Group>), - /// Invalid ID. - #[oai(status = 404)] - NotFound, -} - -#[derive(ApiResponse)] -enum CreateGroupResponse { - /// Returns the group requested - #[oai(status = 200)] - Group(Json<Group>), - /// Invalid parameter, such as: - /// - empty string for name - /// - bad string - #[oai(status = 400)] - BadRequest(PlainText<String>), -} - -#[derive(ApiResponse)] -enum ChannelResponse { - /// Returns the channel requested - #[oai(status = 200)] - Channel(Json<Channel>), - /// Invalid ID. Content specifies which of the IDs passed is invalid. - #[oai(status = 404)] - NotFound(PlainText<String>), -} - -#[derive(ApiResponse)] -enum CreateChannelResponse { - /// Returns the channel requested - #[oai(status = 200)] - Channel(Json<Channel>), - /// Invalid parameter, such as: - /// - empty string for name - /// - bad string - #[oai(status = 400)] - BadRequest(PlainText<String>), -} - -#[derive(ApiResponse)] -enum GenericResponse { - /// Action succeeded - #[oai(status = 200)] - Success, - /// Invalid ID. Content specifies which of the IDs passed is invalid. - #[oai(status = 404)] - NotFound(PlainText<String>), -} - -#[derive(ApiResponse)] -enum MessagesResponse { - /// Returns the messages requested - #[oai(status = 200)] - Messages(Json<Vec<Message>>), - /// Invalid ID, or no messages found. Content specifies which error occured. - #[oai(status = 404)] - NotFound(PlainText<String>), - /// Offset or number of messages requested is bad. Content specifies which error occured. - #[oai(status = 400)] - BadRequest(PlainText<String>), -} - -#[derive(ApiResponse)] -enum MembersResponse { - /// Returns the members of current channel/group - #[oai(status = 200)] - Messages(Json<Vec<User>>), - /// Invalid ID - #[oai(status = 404)] - NotFound, -} - -#[derive(ApiResponse)] -enum GroupsResponse { - /// Returns the groups the user is a memmber of - #[oai(status = 200)] - Messages(Json<Vec<Group>>), - /// Invalid user ID - #[oai(status = 404)] - NotFound, -} - -#[derive(ApiResponse)] -enum ChannelsResponse { - /// Returns the channels in a group - #[oai(status = 200)] - Messages(Json<Vec<Channel>>), - /// Invalid group ID - #[oai(status = 404)] - NotFound, -} +struct Api; #[OpenApi] impl Api { diff --git a/scuttlebutt/src/responses.rs b/scuttlebutt/src/responses.rs @@ -0,0 +1,169 @@ +use poem_openapi::{ + payload::{Json, PlainText}, + ApiResponse, Object, +}; +use serde::{Deserialize, Serialize}; + +#[derive(Object, Serialize, Deserialize)] +pub struct User { + pub id: i64, + pub username: String, + pub email: String, +} + +#[derive(Object)] +pub struct Group { + pub id: i64, + pub name: String, + // The IDs of the group members + pub members: Vec<i64>, + // The IDs of the group's channels + pub channels: Vec<i64>, +} + +#[derive(Object)] +pub struct Channel { + pub id: i64, + pub name: String, + pub members: Vec<i64>, +} + +#[derive(Object)] +pub struct Message { + pub id: i64, + pub channel: i64, + pub author: i64, + pub content: String, +} + +#[derive(ApiResponse)] +pub enum UserResponse { + /// Returns the user requested. + #[oai(status = 200)] + User(Json<User>), + /// Invalid ID. Content specifies which of the IDs passed is invalid. + #[oai(status = 404)] + NotFound(PlainText<String>), +} + +#[derive(ApiResponse)] +pub enum CreateUserResponse { + /// Returns the user requested. + #[oai(status = 200)] + User(Json<User>), + /// Recieved a bad argument when specifying the user. Returns error type, such as: + /// - found empty string for any of the arguments + /// - invalid email + #[oai(status = 400)] + BadRequest, +} + +#[derive(ApiResponse)] +pub enum DeleteResponse { + /// The delete operation succeeded + #[oai(status = 200)] + Success, + /// You are not authorized to perform the action + #[oai(status = 401)] + Unauthorized, + /// Invalid ID. Content specifies which of the IDs passed is invalid. + #[oai(status = 404)] + NotFound(PlainText<String>), +} + +#[derive(ApiResponse)] +pub enum GroupResponse { + /// Returns the group requested + #[oai(status = 200)] + Group(Json<Group>), + /// Invalid ID. + #[oai(status = 404)] + NotFound, +} + +#[derive(ApiResponse)] +pub enum CreateGroupResponse { + /// Returns the group requested + #[oai(status = 200)] + Group(Json<Group>), + /// Invalid parameter, such as: + /// - empty string for name + /// - bad string + #[oai(status = 400)] + BadRequest(PlainText<String>), +} + +#[derive(ApiResponse)] +pub enum ChannelResponse { + /// Returns the channel requested + #[oai(status = 200)] + Channel(Json<Channel>), + /// Invalid ID. Content specifies which of the IDs passed is invalid. + #[oai(status = 404)] + NotFound(PlainText<String>), +} + +#[derive(ApiResponse)] +pub enum CreateChannelResponse { + /// Returns the channel requested + #[oai(status = 200)] + Channel(Json<Channel>), + /// Invalid parameter, such as: + /// - empty string for name + /// - bad string + #[oai(status = 400)] + BadRequest(PlainText<String>), +} + +#[derive(ApiResponse)] +pub enum GenericResponse { + /// Action succeeded + #[oai(status = 200)] + Success, + /// Invalid ID. Content specifies which of the IDs passed is invalid. + #[oai(status = 404)] + NotFound(PlainText<String>), +} + +#[derive(ApiResponse)] +pub enum MessagesResponse { + /// Returns the messages requested + #[oai(status = 200)] + Messages(Json<Vec<Message>>), + /// Invalid ID, or no messages found. Content specifies which error occured. + #[oai(status = 404)] + NotFound(PlainText<String>), + /// Offset or number of messages requested is bad. Content specifies which error occured. + #[oai(status = 400)] + BadRequest(PlainText<String>), +} + +#[derive(ApiResponse)] +pub enum MembersResponse { + /// Returns the members of current channel/group + #[oai(status = 200)] + Messages(Json<Vec<User>>), + /// Invalid ID + #[oai(status = 404)] + NotFound, +} + +#[derive(ApiResponse)] +pub enum GroupsResponse { + /// Returns the groups the user is a memmber of + #[oai(status = 200)] + Messages(Json<Vec<Group>>), + /// Invalid user ID + #[oai(status = 404)] + NotFound, +} + +#[derive(ApiResponse)] +pub enum ChannelsResponse { + /// Returns the channels in a group + #[oai(status = 200)] + Messages(Json<Vec<Channel>>), + /// Invalid group ID + #[oai(status = 404)] + NotFound, +}