diff --git a/auth/Cargo.toml b/auth/Cargo.toml index a892163..d4ee8c7 100644 --- a/auth/Cargo.toml +++ b/auth/Cargo.toml @@ -18,3 +18,4 @@ hex = "0.4.3" rand = "0.8.5" mail-send = "0.4.8" regex = "1.10.5" +realm_shared = { path = "../shared" } diff --git a/auth/src/server.rs b/auth/src/server.rs index 1a9376b..7c68ec0 100644 --- a/auth/src/server.rs +++ b/auth/src/server.rs @@ -11,8 +11,9 @@ use sha3::digest::Update; use sqlx::{MySql, Pool, Row}; use tarpc::context::Context; -use crate::types::{AuthEmail, AuthUser, ErrorCode, RealmAuth}; -use crate::types::ErrorCode::*; +use crate::types::{AuthEmail, AuthUser, RealmAuth}; +use realm_shared::types::ErrorCode; +use realm_shared::types::ErrorCode::*; #[derive(Clone)] pub struct RealmAuthServer { diff --git a/auth/src/types.rs b/auth/src/types.rs index b800ab3..511ea47 100644 --- a/auth/src/types.rs +++ b/auth/src/types.rs @@ -1,4 +1,5 @@ use serde::{Deserialize, Serialize}; +use realm_shared::types::ErrorCode; #[tarpc::service] pub trait RealmAuth { @@ -21,21 +22,6 @@ pub trait RealmAuth { // TODO: OAuth login, check against email, store token, take avatar: Google, Apple, GitHub, Discord } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub enum ErrorCode { - Error, - Unauthorized, - EmailTaken, - UsernameTaken, - InvalidLoginCode, - InvalidImage, - InvalidUsername, - InvalidEmail, - InvalidToken, - UnableToConnectToMail, - UnableToSendMail, -} - #[derive(Debug, Clone, Serialize, Deserialize)] pub struct AuthUser { pub id: u32, diff --git a/server/Cargo.toml b/server/Cargo.toml index 672fc30..c096d48 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -14,4 +14,5 @@ emojis = "0.6.2" chrono = { version = "0.4.24", features = ["serde"] } sqlx = { version = "0.7", features = [ "runtime-tokio", "tls-rustls", "mysql", "chrono" ] } dotenvy = "0.15" -realm_auth = { path = "../auth" } \ No newline at end of file +realm_auth = { path = "../auth" } +realm_shared = { path = "../shared" } \ No newline at end of file diff --git a/server/src/server.rs b/server/src/server.rs index 2fbea6f..66c129c 100644 --- a/server/src/server.rs +++ b/server/src/server.rs @@ -3,8 +3,9 @@ use chrono::{DateTime, Utc}; use sqlx::{Error, MySql, Pool, Row}; use sqlx::mysql::MySqlRow; use tarpc::context::Context; -use crate::types::{Edit, ErrorCode, Message, MessageData, Reaction, RealmChat, Redaction, Reply, Room, User}; -use crate::types::ErrorCode::*; +use crate::types::{Edit, Message, MessageData, Reaction, RealmChat, Redaction, Reply, Room, User}; +use realm_shared::types::ErrorCode::*; +use realm_shared::types::ErrorCode; #[derive(Clone)] pub struct RealmChatServer { @@ -84,7 +85,7 @@ impl RealmChat for RealmChatServer { self.dbmessage_to_message(row) }, Err(_) => { - Err(NotFound) + Err(MessageNotFound) }, } } @@ -122,7 +123,7 @@ impl RealmChat for RealmChatServer { match result { Ok(row) => { self.dbroom_to_room(row) }, - Err(_) => Err(NotFound), + Err(_) => Err(RoomNotFound), } } @@ -131,7 +132,7 @@ impl RealmChat for RealmChatServer { match result { Ok(row) => { self.dbuser_to_user(row) }, - Err(_) => Err(NotFound), + Err(_) => Err(UserNotFound), } } @@ -180,7 +181,7 @@ impl RealmChatServer { let admin_only_view: Result = row.try_get("admin_only_view"); if id.is_err() { - return Err(FailedToUnwrapDB) + return Err(MalformedDBResponse) } Ok(Room { @@ -200,7 +201,7 @@ impl RealmChatServer { let admin: Result = row.try_get("admin"); if id.is_err() { - return Err(FailedToUnwrapDB) + return Err(MalformedDBResponse) } Ok(User { @@ -220,7 +221,7 @@ impl RealmChatServer { }; if type_enum == "" { - return Err(FailedToUnwrapDB) + return Err(MalformedDBResponse) } let id: u32 = row.try_get("message.id").unwrap(); @@ -295,7 +296,7 @@ impl RealmChatServer { }), }) } - _ => { Err(FailedToUnwrapDB) } + _ => { Err(MalformedDBResponse) } } } } \ No newline at end of file diff --git a/server/src/types.rs b/server/src/types.rs index 1e95148..af3823e 100644 --- a/server/src/types.rs +++ b/server/src/types.rs @@ -1,10 +1,11 @@ use chrono::{DateTime, TimeZone, Utc}; use tarpc::serde::{Deserialize, Serialize}; +use realm_shared::types::ErrorCode; #[tarpc::service] pub trait RealmChat { async fn test(name: String) -> String; - + //TODO: Any user authorized as themselves async fn send_message(auth_token: String, message: Message) -> Result; async fn start_typing(auth_token: String) -> ErrorCode; @@ -28,15 +29,6 @@ pub trait RealmChat { // unban user } -#[derive(Debug, Clone, Serialize, Deserialize)] -pub enum ErrorCode { - None, - Error, - Unauthorized, - NotFound, - FailedToUnwrapDB, -} - #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Message { pub id: u32, diff --git a/shared/Cargo.toml b/shared/Cargo.toml new file mode 100644 index 0000000..1282f0e --- /dev/null +++ b/shared/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "realm_shared" +version = "0.1.0" +edition = "2021" + +[dependencies] +serde = { version = "1.0.203", features = ["derive"] } diff --git a/shared/src/lib.rs b/shared/src/lib.rs new file mode 100644 index 0000000..dd198c6 --- /dev/null +++ b/shared/src/lib.rs @@ -0,0 +1 @@ +pub mod types; \ No newline at end of file diff --git a/shared/src/types.rs b/shared/src/types.rs new file mode 100644 index 0000000..496c0ff --- /dev/null +++ b/shared/src/types.rs @@ -0,0 +1,21 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub enum ErrorCode { + Error, + Unauthorized, + EmailTaken, + UsernameTaken, + InvalidLoginCode, + InvalidImage, + InvalidUsername, + InvalidEmail, + InvalidToken, + UnableToConnectToMail, + UnableToSendMail, + + MessageNotFound, + RoomNotFound, + UserNotFound, + MalformedDBResponse, +} \ No newline at end of file