-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
483 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#[macro_export] | ||
macro_rules! make_response { | ||
($status:expr, $body:expr) => { | ||
Response::builder() | ||
.status($status) | ||
.header("Content-Type", "application/json") | ||
.body(Body::from($body.to_string())) | ||
.expect("failed to build response") | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use chrono::Utc; | ||
use serde::{Deserialize, Serialize}; | ||
use sqlx::PgPool; | ||
use std::collections::HashMap; | ||
|
||
#[derive(Deserialize)] | ||
pub enum ChatMessage { | ||
Outgoing(OutgoingChatMessage), | ||
Incoming(IncomingChatMessage), | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct OutgoingChatMessage { | ||
// message or info | ||
pub action: String, | ||
pub username: String, | ||
pub message: String, | ||
// nickname color, badges etc | ||
pub metadata: HashMap<String, String>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct IncomingChatMessage { | ||
// auth or message | ||
pub action: String, | ||
pub message: String, | ||
} | ||
|
||
impl IncomingChatMessage { | ||
pub fn to_outgoing(&self, username: String) -> String { | ||
serde_json::to_string(&OutgoingChatMessage { | ||
action: "message".to_string(), | ||
username, | ||
message: self.message.to_string(), | ||
metadata: HashMap::new(), | ||
}) | ||
.unwrap() | ||
} | ||
|
||
pub async fn save_to_db(&self, chat_id: i64, user_id: i64, pool: &PgPool) { | ||
let _ = sqlx::query!( | ||
"INSERT INTO chat_messages (chat_room_id, author_id, message, created_at) VALUES ($1, $2, $3, $4)", | ||
chat_id, | ||
user_id, | ||
self.message, | ||
Utc::now() | ||
) | ||
.execute(pool) | ||
.await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use hyper::Body; | ||
use routerify::Router; | ||
use std::convert::Infallible; | ||
|
||
mod messages; | ||
mod validators; | ||
mod ws; | ||
|
||
pub fn routes() -> Router<Body, Infallible> { | ||
Router::builder() | ||
.any_method("/:chatId", ws::upgrade_request) | ||
.build() | ||
.unwrap() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use super::messages::IncomingChatMessage; | ||
use common::types::{chat_room, user}; | ||
use sqlx::PgPool; | ||
|
||
const MAX_MESSAGE_LENGTH: usize = 255; | ||
|
||
pub fn valid_chat_message(message: String) -> Option<IncomingChatMessage> { | ||
let final_message = serde_json::from_str::<IncomingChatMessage>(&message); | ||
|
||
if let Ok(message_result) = final_message { | ||
if message_result.message.len() <= MAX_MESSAGE_LENGTH { | ||
return Some(message_result); | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
pub async fn valid_user(session_id: i64, pool: &PgPool) -> Option<user::Model> { | ||
let user = sqlx::query_as!( | ||
user::Model, | ||
"SELECT * FROM users WHERE id = (SELECT user_id FROM sessions WHERE id = $1)", | ||
session_id | ||
) | ||
.fetch_optional(pool) | ||
.await | ||
.unwrap_or(None); | ||
|
||
user | ||
} | ||
|
||
pub async fn valid_chat(chat_id: i64, pool: &PgPool) -> Option<chat_room::Model> { | ||
let chat = sqlx::query_as!( | ||
chat_room::Model, | ||
"SELECT * FROM chat_rooms WHERE id = $1", | ||
chat_id | ||
) | ||
.fetch_optional(pool) | ||
.await | ||
.unwrap_or(None); | ||
|
||
chat | ||
} |
Oops, something went wrong.