Skip to content

Commit

Permalink
Refactor code structure and add resource upload and download function…
Browse files Browse the repository at this point in the history
…ality
  • Loading branch information
JieningYu committed Jan 6, 2024
1 parent 6b87c63 commit 34c737f
Show file tree
Hide file tree
Showing 13 changed files with 425 additions and 67 deletions.
3 changes: 3 additions & 0 deletions .test/resources/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Resources directory

This is a placeholder file.
39 changes: 1 addition & 38 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ axum = "0.7"
libaccount = { version = "0.1", git = "https://github.com/subitlab-buf/libaccount.git", branch = "tags" }
dmds = "0.2"
dmds-tokio-fs = "0.2"
tokio = { version = "1.35", features = ["full"] }
tokio = { version = "1.35", features = ["rt", "macros", "sync", "fs"] }
tokio-util = { version = "0.7", features = ["io"] }
tracing = "0.1"
tracing-subscriber = "0.3"
lettre = { version = "0.11", default-features = false, features = [
Expand Down
11 changes: 10 additions & 1 deletion src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,23 @@ pub enum Permission {
ViewFullAccount,
ViewSimpleAccount,

/// Upload resources.
UploadResource,

/// Maintain this system.
Maintain,
}

impl libaccount::Permission for Permission {
#[inline]
fn default_set() -> std::collections::HashSet<Self> {
[Self::Post, Self::GetPubPost].into()
[
Self::Post,
Self::GetPubPost,
Self::ViewSimpleAccount,
Self::UploadResource,
]
.into()
}

#[inline]
Expand Down
5 changes: 4 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ pub struct Config {
/// SMTP configuration.
pub smtp: SMTP,

/// The root path of database.
/// The root path of the database.
pub db_path: PathBuf,
/// The port of HTTP server.
pub port: u16,

/// The root path of resource files.
pub resource_path: PathBuf,
}

/// SMTP mailing configuration.
Expand Down
18 changes: 10 additions & 8 deletions src/handle/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub async fn send_captcha<Io: IoHandle>(
worlds,
config,
test_cx,
..
}): State<Global<Io>>,
Json(SendCaptchaReq { email }): Json<SendCaptchaReq>,
) -> Result<(), Error> {
Expand Down Expand Up @@ -145,6 +146,7 @@ pub async fn send_reset_password_captcha<Io: IoHandle>(
worlds,
config,
test_cx,
..
}): State<Global<Io>>,
Json(SendResetPasswordCaptchaReq { email }): Json<SendResetPasswordCaptchaReq>,
) -> Result<(), Error> {
Expand Down Expand Up @@ -317,7 +319,7 @@ pub async fn set_permissions<Io: IoHandle>(

#[derive(Serialize)]
#[serde(tag = "type")]
pub enum GetInfoRes {
pub enum Info {
Simple {
name: String,
email: String,
Expand All @@ -344,7 +346,7 @@ pub enum GetInfoRes {
},
}

impl GetInfoRes {
impl Info {
fn from_simple(account: &Account) -> Self {
Self::Simple {
name: account.name().to_owned(),
Expand Down Expand Up @@ -392,24 +394,24 @@ pub async fn get_info<Io: IoHandle>(
Path(target): Path<u64>,
auth: Auth,
State(Global { worlds, .. }): State<Global<Io>>,
) -> Result<Json<GetInfoRes>, Error> {
) -> Result<Json<Info>, Error> {
let select = sd!(worlds.account, auth.account);
let this_lazy = va!(auth, select => ViewSimpleAccount);
let select = sd!(worlds.account, target);
let lazy = gd!(select, target).ok_or(Error::TargetAccountNotFound)?;
let account = lazy.get().await?;

if auth.account == account.id() {
Ok(Json(GetInfoRes::from_owned(account)))
Ok(Json(Info::from_owned(account)))
} else if this_lazy
.get()
.await?
.tags()
.contains_permission(&Tag::Permission(Permission::ViewFullAccount))
{
Ok(Json(GetInfoRes::from_full(account)))
Ok(Json(Info::from_full(account)))
} else {
Ok(Json(GetInfoRes::from_simple(account)))
Ok(Json(Info::from_simple(account)))
}
}

Expand All @@ -424,7 +426,7 @@ pub async fn bulk_get_info<Io: IoHandle>(
auth: Auth,
State(Global { worlds, .. }): State<Global<Io>>,
Json(BulkGetInfoReq { accounts }): Json<BulkGetInfoReq>,
) -> Result<Json<HashMap<u64, GetInfoRes>>, Error> {
) -> Result<Json<HashMap<u64, Info>>, Error> {
let select = sd!(worlds.account, auth.account);
va!(auth, select => ViewSimpleAccount);
if let Some(last) = accounts.first().copied() {
Expand All @@ -438,7 +440,7 @@ pub async fn bulk_get_info<Io: IoHandle>(
while let Some(Ok(lazy)) = iter.next().await {
if accounts.contains(&lazy.id()) {
if let Ok(account) = lazy.get().await {
res.insert(account.id(), GetInfoRes::from_simple(account));
res.insert(account.id(), Info::from_simple(account));
}
}
}
Expand Down
Loading

0 comments on commit 34c737f

Please sign in to comment.