Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mdcpp/mdoj
Browse files Browse the repository at this point in the history
  • Loading branch information
KAIYOHUGO committed Aug 22, 2024
2 parents 174487a + c341b6c commit bb64862
Show file tree
Hide file tree
Showing 46 changed files with 337 additions and 610 deletions.
465 changes: 63 additions & 402 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ version = "0.11.0"
default-features = false

[workspace.dependencies.sea-orm]
version = "0.12.11"
version = "1.0.0"
default-features = false

[workspace.dependencies.sea-orm-migration]
version = "0.12.11"
version = "1.0.0"
default-features = false

[profile.wasm-release]
Expand Down
14 changes: 7 additions & 7 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ futures = "0.3.29"
bincode = "1.3.3"
base64 = "0.21.5"
tonic-web = { workspace = true }
quick_cache = "0.4.0"
quick_cache = "0.6.2"
hickory-resolver = "0.24.0"
crossbeam-queue = "0.3.8"
dashmap = "5.5.3"
Expand All @@ -36,7 +36,6 @@ opentelemetry-semantic-conventions = "0.16.0"
tracing-opentelemetry = { version = "0.24.0", features = ["metrics"] }
tracing-core = "0.1.32"
migration = { path = "./migration", optional = true }
sea-orm-cli = { version = "0.12.12", optional = true }
governor = "0.6.0"
http = "^0.2"
lazy_static = "1.5.0"
Expand Down Expand Up @@ -69,7 +68,7 @@ version = "^0.4"
features = ["cors", "trace"]

[dependencies.sea-query]
version = "0.30.4"
version = "0.31.0"
features = ["thread-safe", "with-chrono", "backend-sqlite"]

[dependencies.chrono]
Expand Down Expand Up @@ -102,7 +101,8 @@ workspace = true
features = ["macros", "rt-multi-thread", "full", "time"]

[dependencies.sea-orm]
version = "0.12.11"
workspace = true
default-features = false
features = [
"runtime-tokio-rustls",
"macros",
Expand All @@ -124,12 +124,12 @@ version = "0.9.8"
features = ["mutex", "spin_mutex", "rwlock"]

[dependencies.sea-orm-migration]
# workspace = true
version = "0.12.11"
workspace = true
optional = true
default-features = false
features = ["runtime-tokio-rustls", "sqlx-sqlite", "with-chrono"]

[features]
default = ["insecure-print"]
standalone = ["dep:migration", "dep:sea-orm-migration", "dep:sea-orm-cli"]
standalone = ["dep:migration", "dep:sea-orm-migration"]
insecure-print = ["sea-orm/debug-print"]
5 changes: 2 additions & 3 deletions backend/migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ version = "1"
features = ["attributes", "tokio1"]

[dependencies.sea-orm]
version = "0.12.11"
workspace = true
default-features = false
features = [
"runtime-async-std-rustls",
Expand All @@ -34,8 +34,7 @@ features = [
]

[dependencies.sea-orm-migration]
# workspace = true
version = "0.12.11"
workspace = true
default-features = true
features = [
# Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI.
Expand Down
8 changes: 8 additions & 0 deletions backend/migration/src/m20231207_000001_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,17 +668,25 @@ impl MigrationTrait for Migration {
index!(manager, Problem, AcceptCount);
index!(manager, Problem, Difficulty);
index!(manager, Problem, Order);
index!(manager, Problem, Content);
index!(manager, Problem, Title);

index!(manager, Submit, Committed);
index!(manager, Submit, Time);
index!(manager, Submit, Memory);
index!(manager, Submit, UserId);

index!(manager, Contest, Hoster);
index!(manager, Contest, Public);
index!(manager, Contest, End);
index!(manager, Contest, Begin);

index!(manager, User, Score);
index!(manager, User, Username);

index!(manager, Token, Rand);
index!(manager, Token, Expiry);

index!(manager, Chat, CreateAt);

manager
Expand Down
6 changes: 3 additions & 3 deletions backend/src/endpoint/announcement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl Announcement for ArcServer {

let id = *model.id.as_ref();

tracing::info!(count.announcement = 1, id = id);
info!(count.announcement = 1, id = id);

Ok(id.into())
})
Expand All @@ -197,7 +197,7 @@ impl Announcement for ArcServer {
req.bound_check()?;

req.get_or_insert(|req| async move {
tracing::trace!(id = req.id);
trace!(id = req.id);

let mut model = Entity::find_by_id(req.id)
.with_auth(&auth)
Expand Down Expand Up @@ -242,7 +242,7 @@ impl Announcement for ArcServer {
if result.rows_affected == 0 {
Err(Error::NotInDB)
} else {
tracing::info!(counter.announcement = -1, id = req.id);
info!(counter.announcement = -1, id = req.id);
Ok(())
}
})
Expand Down
4 changes: 2 additions & 2 deletions backend/src/endpoint/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Chat for ArcServer {
.map_err(Into::<Error>::into)?;

let id = *model.id.as_ref();
tracing::debug!(id = id, "chat_created");
debug!(id = id, "chat_created");

Ok(id.into())
})
Expand Down Expand Up @@ -80,7 +80,7 @@ impl Chat for ArcServer {
if result.rows_affected == 0 {
Err(Error::NotInDB)
} else {
tracing::info!(counter.chat = -1, id = req.id);
info!(counter.chat = -1, id = req.id);
Ok(())
}
})
Expand Down
114 changes: 62 additions & 52 deletions backend/src/endpoint/contest.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::*;

use crate::entity::{
contest::{Paginator, *},
*,
problem, *,
};
use sea_orm::sea_query::Expr;

use grpc::backend::contest_server::*;

Expand Down Expand Up @@ -164,7 +164,7 @@ impl Contest for ArcServer {

let id = *model.id.as_ref();

tracing::info!(count.contest = 1, id = id);
info!(count.contest = 1, id = id);

Ok(id.into())
})
Expand All @@ -185,7 +185,7 @@ impl Contest for ArcServer {
let (_, perm) = auth.assume_login()?;

req.get_or_insert(|req| async move {
tracing::trace!(id = req.id);
trace!(id = req.id);

let mut model = Entity::find_by_id(req.id).with_auth(&auth).write()?
.one(self.db.deref())
Expand Down Expand Up @@ -238,6 +238,8 @@ impl Contest for ArcServer {
let (auth, req) = self.rate_limit(req).in_current_span().await?;

req.get_or_insert(|req| async move {
let txn = self.db.begin().await?;

let result = Entity::delete_by_id(req.id)
.with_auth(&auth)
.write()?
Expand All @@ -246,12 +248,20 @@ impl Contest for ArcServer {
.await
.map_err(Into::<Error>::into)?;

problem::Entity::update_many()
.col_expr(problem::Column::ContestId, Expr::value(Value::Int(None)))
.filter(crate::entity::testcase::Column::ProblemId.eq(req.id))
.exec(&txn)
.instrument(info_span!("remove_child"))
.await?;

txn.commit().await.map_err(|_| Error::Retry)?;

if result.rows_affected == 0 {
Err(Error::NotInDB)
} else {
tracing::info!(counter.contest = -1, id = req.id);
Ok(())
return Err(Error::NotInDB);
}
info!(counter.contest = -1, id = req.id);
Ok(())
})
.await
.with_grpc()
Expand All @@ -260,47 +270,30 @@ impl Contest for ArcServer {
#[instrument(
skip_all,
level = "info",
name = "oj.backend.Contest/join",
name = "oj.backend.Contest/publish",
err(level = "debug", Display)
)]
async fn join(&self, req: Request<JoinContestRequest>) -> Result<Response<()>, Status> {
async fn publish(&self, req: Request<PublishRequest>) -> Result<Response<()>, Status> {
let (auth, req) = self.rate_limit(req).in_current_span().await?;
let (user_id, _) = auth.assume_login()?;

req.get_or_insert(|req| async move {
let model = Entity::find_by_id(req.id)
let mut model = Entity::find_by_id(req.id)
.with_auth(&auth)
.read()?
.write()?
.columns([Column::Id])
.one(self.db.deref())
.instrument(info_span!("fetch").or_current())
.await
.map_err(Into::<Error>::into)?
.ok_or(Error::NotInDB)?;
// FIXME: abstract away password checking logic

if let Some(tar) = model.password {
let password = req
.password
.as_ref()
.ok_or(Error::NotInPayload("password"))?;
if !self.crypto.hash_eq(password, &tar) {
return Err(Error::PermissionDeny("mismatched password"));
}
}

let pivot = user_contest::ActiveModel {
user_id: ActiveValue::Set(user_id),
contest_id: ActiveValue::Set(model.id),
..Default::default()
};
.ok_or(Error::NotInDB)?
.into_active_model();

pivot
.save(self.db.deref())
.instrument(info_span!("insert_pviot").or_current())
.await
.map_err(Into::<Error>::into)?;
model.public = ActiveValue::Set(true);

tracing::debug!(user_id = user_id, contest_id = req.id);
model
.update(self.db.deref())
.instrument(info_span!("update").or_current())
.await?;
Ok(())
})
.await
Expand All @@ -310,10 +303,10 @@ impl Contest for ArcServer {
#[instrument(
skip_all,
level = "info",
name = "oj.backend.Contest/publish",
name = "oj.backend.Contest/unpublish",
err(level = "debug", Display)
)]
async fn publish(&self, req: Request<PublishRequest>) -> Result<Response<()>, Status> {
async fn unpublish(&self, req: Request<PublishRequest>) -> Result<Response<()>, Status> {
let (auth, req) = self.rate_limit(req).in_current_span().await?;

req.get_or_insert(|req| async move {
Expand All @@ -328,7 +321,7 @@ impl Contest for ArcServer {
.ok_or(Error::NotInDB)?
.into_active_model();

model.public = ActiveValue::Set(true);
model.public = ActiveValue::Set(false);

model
.update(self.db.deref())
Expand All @@ -343,30 +336,47 @@ impl Contest for ArcServer {
#[instrument(
skip_all,
level = "info",
name = "oj.backend.Contest/unpublish",
name = "oj.backend.Contest/join",
err(level = "debug", Display)
)]
async fn unpublish(&self, req: Request<PublishRequest>) -> Result<Response<()>, Status> {
async fn join(&self, req: Request<JoinContestRequest>) -> Result<Response<()>, Status> {
let (auth, req) = self.rate_limit(req).in_current_span().await?;
let (user_id, _) = auth.assume_login()?;

req.get_or_insert(|req| async move {
let mut model = Entity::find_by_id(req.id)
let model = Entity::find_by_id(req.id)
.with_auth(&auth)
.write()?
.columns([Column::Id])
.read()?
.one(self.db.deref())
.instrument(info_span!("fetch").or_current())
.await
.map_err(Into::<Error>::into)?
.ok_or(Error::NotInDB)?
.into_active_model();
.ok_or(Error::NotInDB)?;
// FIXME: abstract away password checking logic

model.public = ActiveValue::Set(false);
if let Some(tar) = model.password {
let password = req
.password
.as_ref()
.ok_or(Error::NotInPayload("password"))?;
if !self.crypto.hash_eq(password, &tar) {
return Err(Error::PermissionDeny("mismatched password"));
}
}

model
.update(self.db.deref())
.instrument(info_span!("update").or_current())
.await?;
let pivot = user_contest::ActiveModel {
user_id: ActiveValue::Set(user_id),
contest_id: ActiveValue::Set(model.id),
..Default::default()
};

pivot
.save(self.db.deref())
.instrument(info_span!("insert_pviot").or_current())
.await
.map_err(Into::<Error>::into)?;

debug!(user_id = user_id, contest_id = req.id);
Ok(())
})
.await
Expand Down
6 changes: 3 additions & 3 deletions backend/src/endpoint/education.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl Education for ArcServer {

let id = *model.id.as_ref();

tracing::info!(count.education = 1, id = id);
info!(count.education = 1, id = id);

Ok(id.into())
})
Expand All @@ -123,7 +123,7 @@ impl Education for ArcServer {
req.bound_check()?;

req.get_or_insert(|req| async move {
tracing::trace!(id = req.id);
trace!(id = req.id);
let mut model = Entity::find_by_id(req.id)
.with_auth(&auth)
.write()?
Expand Down Expand Up @@ -167,7 +167,7 @@ impl Education for ArcServer {
if result.rows_affected == 0 {
Err(Error::NotInDB)
} else {
tracing::info!(counter.education = -1, id = req.id);
info!(counter.education = -1, id = req.id);
Ok(())
}
})
Expand Down
2 changes: 1 addition & 1 deletion backend/src/endpoint/imgur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Image for ArcServer {
req.get_or_insert(|req| async move {
let url = self.imgur.upload(req.data).await?;

tracing::debug!(counter.image = 1, uri = url);
debug!(counter.image = 1, uri = url);
Ok(UploadResponse { url })
})
.await
Expand Down
Loading

0 comments on commit bb64862

Please sign in to comment.