Skip to content

Commit

Permalink
Updated project
Browse files Browse the repository at this point in the history
  • Loading branch information
myOmikron committed Dec 28, 2024
1 parent fa82123 commit bc69ae6
Show file tree
Hide file tree
Showing 21 changed files with 1,534 additions and 1,107 deletions.
2,281 changes: 1,348 additions & 933 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ repository = "https://github.com/hopfenspace/runciv"
# Web framework
actix-web = { version = "~4" }
# Extensions for actix-web
actix-toolbox = { version = "~0.11", features = ["logging", "ws", "session"] }
actix-toolbox = { version = "~0.13", features = ["logging", "ws", "session-postgres-only"] }

# openapi swagger
utoipa = { version = "~3", features = ["actix_extras", "repr", "chrono", "uuid", "openapi_extensions", "preserve_order"] }
utoipa = { version = "~5", features = ["actix_extras", "repr", "chrono", "uuid", "openapi_extensions", "preserve_order"] }
# openapi swagger boilerplat generation
utoipa-swagger-ui = { version = "3", features = ["actix-web"] }
utoipa-swagger-ui = { version = "8", features = ["actix-web"] }

# Hashing
argon2 = { version = "~0.5" }
Expand All @@ -34,7 +34,7 @@ clap = { version = "~4", features = ["derive"] }
serde = { version = "~1", features = ["derive"] }
serde_repr = { version = "~0.1" }
serde_json = { version = "~1", features = ["raw_value"] }
toml = { version = "~0.7" }
toml = { version = "~0.8" }
# Time library
chrono = { version = ">=0.4.20", default-features = false, features = ["serde"] }
# Bytes abstractions for network usage
Expand All @@ -44,19 +44,19 @@ bytestring = { version = "~1" }
# UUID generation
uuid = { version = "~1", features = ["v4", "serde"] }
# Base64 encoding and decoding library
base64 = { version = "~0.21" }
base64 = { version = "~0.22" }

# ORM
rorm = { version = "~0.5", features = ["tokio-rustls", "cli", "uuid"] }
rorm = { version = "~0.6", default-features = false, features = ["postgres-only", "time", "chrono", "cli", "uuid"] }

# Async runtime
tokio = { version = ">=1.23.1", features = ["rt-multi-thread", "sync", "macros"] }
tokio = { version = ">=1.23.1", features = ["rt-multi-thread", "sync", "macros", "fs"] }
# Async abstractions
futures = { version = "~0.3" }
futures-util = "0.3"

# More iterators
itertools = { version = "~0.11" }
itertools = { version = "~0.13" }

# Lazy initialization
once_cell = { version = "~1" }
Expand Down
26 changes: 10 additions & 16 deletions src/chan/ws_manager_chan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::iter;
use actix_toolbox::ws;
use actix_toolbox::ws::{MailboxError, Message};
use log::{debug, error, info, warn};
use rorm::{and, delete, query, Database, Model};
use rorm::{and, delete, query, Database, FieldAccess, Model};
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc::Sender;
use tokio::sync::{mpsc, oneshot};
Expand Down Expand Up @@ -246,7 +246,7 @@ pub async fn start_ws_manager(db: Database) -> Result<WsManagerChan, String> {

let (username, display_name) =
match query!(&mut tx, (Account::F.username, Account::F.display_name))
.condition(Account::F.uuid.equals(uuid.as_ref()))
.condition(Account::F.uuid.equals(uuid))
.one()
.await
{
Expand Down Expand Up @@ -278,17 +278,15 @@ pub async fn start_ws_manager(db: Database) -> Result<WsManagerChan, String> {
}

if let Err(err) = delete!(&mut tx, ChatRoom)
.condition(
ChatRoom::F.uuid.equals(lobby.chat_room.key().as_ref()),
)
.condition(ChatRoom::F.uuid.equals(*lobby.chat_room.key()))
.await
{
error!("Database error: {err}");
return;
}

if let Err(err) = delete!(&mut tx, Lobby)
.condition(Lobby::F.uuid.equals(lobby.uuid.as_ref()))
.condition(Lobby::F.uuid.equals(lobby.uuid))
.await
{
error!("Database error: {err}");
Expand Down Expand Up @@ -319,18 +317,14 @@ pub async fn start_ws_manager(db: Database) -> Result<WsManagerChan, String> {
}

match query!(&mut tx, LobbyAccount)
.condition(LobbyAccount::F.player.equals(uuid.as_ref()))
.condition(LobbyAccount::F.player.equals(uuid))
.all()
.await
{
Ok(lobby_accounts) => {
for lobby_account in lobby_accounts {
let mut lobby = match query!(&mut tx, Lobby)
.condition(
Lobby::F
.uuid
.equals(lobby_account.lobby.key().as_ref()),
)
.condition(Lobby::F.uuid.equals(*lobby_account.lobby.key()))
.one()
.await
{
Expand All @@ -350,10 +344,10 @@ pub async fn start_ws_manager(db: Database) -> Result<WsManagerChan, String> {

if let Err(err) = delete!(&mut tx, ChatRoomMember)
.condition(and!(
ChatRoomMember::F.member.equals(uuid.as_ref()),
ChatRoomMember::F.member.equals(uuid),
ChatRoomMember::F
.chat_room
.equals(lobby.chat_room.key().as_ref())
.equals(lobby.chat_room.key())
))
.await
{
Expand All @@ -363,8 +357,8 @@ pub async fn start_ws_manager(db: Database) -> Result<WsManagerChan, String> {

if let Err(err) = delete!(&mut tx, LobbyAccount)
.condition(and!(
LobbyAccount::F.player.equals(uuid.as_ref()),
LobbyAccount::F.lobby.equals(lobby.uuid.as_ref())
LobbyAccount::F.player.equals(uuid),
LobbyAccount::F.lobby.equals(lobby.uuid)
))
.await
{
Expand Down
2 changes: 1 addition & 1 deletion src/models/account.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rorm::fields::BackRef;
use rorm::fields::types::BackRef;
use rorm::{field, Model, Patch};
use uuid::Uuid;

Expand Down
2 changes: 1 addition & 1 deletion src/models/chat.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rorm::fields::{BackRef, ForeignModel};
use rorm::fields::types::{BackRef, ForeignModel};
use rorm::{field, Model, Patch};
use uuid::Uuid;

Expand Down
2 changes: 1 addition & 1 deletion src/models/friend.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rorm::fields::ForeignModel;
use rorm::fields::types::ForeignModel;
use rorm::{Model, Patch};
use uuid::Uuid;

Expand Down
2 changes: 1 addition & 1 deletion src/models/game.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rorm::fields::{BackRef, ForeignModel};
use rorm::fields::types::{BackRef, ForeignModel};
use rorm::{field, Model, Patch};
use uuid::Uuid;

Expand Down
2 changes: 1 addition & 1 deletion src/models/invite.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rorm::fields::ForeignModel;
use rorm::fields::types::ForeignModel;
use rorm::{Model, Patch};
use uuid::Uuid;

Expand Down
2 changes: 1 addition & 1 deletion src/models/lobby.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rorm::fields::{BackRef, ForeignModel};
use rorm::fields::types::{BackRef, ForeignModel};
use rorm::{field, Model, Patch};
use uuid::Uuid;

Expand Down
41 changes: 23 additions & 18 deletions src/server/handler/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use argon2::password_hash::{Error, SaltString};
use argon2::{Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
use log::{error, warn};
use rand::thread_rng;
use rorm::{insert, query, update, Database, Model};
use rorm::{insert, query, update, Database, FieldAccess, Model};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use uuid::Uuid;

use crate::chan::{WsManagerChan, WsManagerMessage, WsMessage};
use crate::models::{Account, AccountInsert};
use crate::server::handler::{ApiError, ApiResult, PathUuid};
use crate::server::handler::{ApiError, ApiErrorResponse, ApiResult, PathUuid};

/// The content to register a new account
#[derive(Debug, Deserialize, ToSchema)]
Expand Down Expand Up @@ -119,7 +119,7 @@ pub async fn get_me(db: Data<Database>, session: Session) -> ApiResult<Json<Acco
let uuid: Uuid = session.get("uuid")?.ok_or(ApiError::SessionCorrupt)?;

let account = query!(db.as_ref(), Account)
.condition(Account::F.uuid.equals(uuid.as_ref()))
.condition(Account::F.uuid.equals(uuid))
.optional()
.await?
.ok_or(ApiError::SessionCorrupt)?;
Expand Down Expand Up @@ -150,8 +150,10 @@ pub async fn delete_me(
) -> ApiResult<HttpResponse> {
let uuid: Uuid = session.get("uuid")?.ok_or(ApiError::SessionCorrupt)?;

rorm::delete!(db.as_ref(), Account)
.condition(Account::F.uuid.equals(uuid.as_ref()))
let db = db.into_inner();

rorm::delete!(&*db, Account)
.condition(Account::F.uuid.equals(uuid))
.await?;

// Clear the current session
Expand Down Expand Up @@ -206,7 +208,7 @@ pub async fn set_password(
let mut tx = db.start_transaction().await?;

let (pw_hash,) = query!(&mut tx, (Account::F.password_hash,))
.condition(Account::F.uuid.equals(uuid.as_ref()))
.condition(Account::F.uuid.equals(uuid))
.optional()
.await?
.ok_or(ApiError::SessionCorrupt)?;
Expand All @@ -224,8 +226,8 @@ pub async fn set_password(
.to_string();

update!(&mut tx, Account)
.condition(Account::F.uuid.equals(uuid.as_ref()))
.set(Account::F.password_hash, &password_hash)
.condition(Account::F.uuid.equals(uuid))
.set(Account::F.password_hash, password_hash)
.exec()
.await?;

Expand Down Expand Up @@ -264,7 +266,10 @@ pub struct UpdateAccountRequest {
)]
#[put("/accounts/me")]
pub async fn update_me(
req: Json<UpdateAccountRequest>,
Json(UpdateAccountRequest {
username,
display_name,
}): Json<UpdateAccountRequest>,
db: Data<Database>,
session: Session,
ws_manager_chan: Data<WsManagerChan>,
Expand All @@ -273,7 +278,7 @@ pub async fn update_me(

let mut tx = db.start_transaction().await?;

if let Some(username) = &req.username {
if let Some(username) = &username {
if username.is_empty() {
return Err(ApiError::InvalidUsername);
}
Expand All @@ -288,17 +293,17 @@ pub async fn update_me(
}
}

if let Some(display_name) = &req.display_name {
if let Some(display_name) = &display_name {
if display_name.is_empty() {
return Err(ApiError::InvalidDisplayName);
}
}

update!(&mut tx, Account)
.condition(Account::F.uuid.equals(uuid.as_ref()))
.condition(Account::F.uuid.equals(uuid))
.begin_dyn_set()
.set_if(Account::F.username, req.username.as_ref())
.set_if(Account::F.display_name, req.display_name.as_ref())
.set_if(Account::F.username, username)
.set_if(Account::F.display_name, display_name)
.finish_dyn_set()
.map_err(|_| ApiError::EmptyJson)?
.exec()
Expand All @@ -312,7 +317,7 @@ pub async fn update_me(
Account::F.display_name
)
)
.condition(Account::F.uuid.equals(uuid.as_ref()))
.condition(Account::F.uuid.equals(uuid))
.optional()
.await?
.ok_or(ApiError::SessionCorrupt)?;
Expand Down Expand Up @@ -359,8 +364,8 @@ pub async fn lookup_account_by_uuid(
req: Path<PathUuid>,
db: Data<Database>,
) -> ApiResult<Json<AccountResponse>> {
let account = query!(db.as_ref(), Account)
.condition(Account::F.uuid.equals(req.uuid.as_ref()))
let account = query!(&**db, Account)
.condition(Account::F.uuid.equals(req.uuid))
.optional()
.await?
.ok_or(ApiError::InvalidUuid)?;
Expand Down Expand Up @@ -404,7 +409,7 @@ pub async fn lookup_account_by_username(
req: Json<LookupAccountUsernameRequest>,
db: Data<Database>,
) -> ApiResult<Json<AccountResponse>> {
let account = query!(db.as_ref(), Account)
let account = query!(&**db, Account)
.condition(Account::F.username.equals(&req.username))
.optional()
.await?
Expand Down
6 changes: 3 additions & 3 deletions src/server/handler/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use argon2::password_hash::Error;
use argon2::{Argon2, PasswordHash, PasswordVerifier};
use chrono::Utc;
use log::error;
use rorm::{query, update, Database, Model};
use rorm::{query, update, Database, FieldAccess, Model};
use serde::Deserialize;
use utoipa::ToSchema;
use uuid::Uuid;

use crate::chan::{WsManagerChan, WsManagerMessage};
use crate::models::Account;
use crate::server::handler::{ApiError, ApiResult};
use crate::server::handler::{ApiError, ApiErrorResponse, ApiResult};

/// The request data of a login request
#[derive(ToSchema, Deserialize)]
Expand Down Expand Up @@ -63,7 +63,7 @@ pub(crate) async fn login(
})?;

update!(&mut tx, Account)
.condition(Account::F.uuid.equals(user.uuid.as_ref()))
.condition(Account::F.uuid.equals(user.uuid))
.set(Account::F.last_login, Some(Utc::now().naive_utc()))
.exec()
.await?;
Expand Down
Loading

0 comments on commit bc69ae6

Please sign in to comment.