add: ability to join/leave/be banned from communities

This commit is contained in:
trisua 2025-03-31 15:39:49 -04:00
parent f3c2157dfc
commit 619184d02e
28 changed files with 618 additions and 197 deletions

View file

@ -15,11 +15,9 @@ pub async fn login_request(jar: CookieJar, Extension(data): Extension<State>) ->
}
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &user).await;
let context = initial_context(&data.0.0, lang, &user).await;
Ok(Html(
data.1.render("auth/login.html", &mut context).unwrap(),
))
Ok(Html(data.1.render("auth/login.html", &context).unwrap()))
}
/// `/auth/register`
@ -35,9 +33,7 @@ pub async fn register_request(
}
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &user).await;
let context = initial_context(&data.0.0, lang, &user).await;
Ok(Html(
data.1.render("auth/register.html", &mut context).unwrap(),
))
Ok(Html(data.1.render("auth/register.html", &context).unwrap()))
}

View file

@ -34,6 +34,38 @@ macro_rules! check_permissions {
};
}
macro_rules! community_context_bools {
($data:ident, $user:ident, $community:ident) => {{
let is_owner = if let Some(ref ua) = $user {
ua.id == $community.owner
} else {
false
};
let is_joined = if let Some(ref ua) = $user {
if let Ok(membership) = $data
.0
.get_membership_by_owner_community(ua.id, $community.id)
.await
{
membership.role.check_member()
} else {
false
}
} else {
false
};
let can_post = if let Some(ref ua) = $user {
$data.0.check_can_post(&$community, ua.id).await
} else {
false
};
(is_owner, is_joined, can_post)
}};
}
/// `/communities`
pub async fn list_request(jar: CookieJar, Extension(data): Extension<State>) -> impl IntoResponse {
let data = data.read().await;
@ -65,9 +97,7 @@ pub async fn list_request(jar: CookieJar, Extension(data): Extension<State>) ->
// return
Ok(Html(
data.1
.render("communities/list.html", &mut context)
.unwrap(),
data.1.render("communities/list.html", &context).unwrap(),
))
}
@ -76,10 +106,12 @@ pub fn community_context(
community: &Community,
is_owner: bool,
is_joined: bool,
can_post: bool,
) {
context.insert("community", &community);
context.insert("is_owner", &is_owner);
context.insert("is_joined", &is_joined);
context.insert("can_post", &can_post);
}
/// `/community/{title}`
@ -117,29 +149,14 @@ pub async fn feed_request(
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &user).await;
let is_owner = if let Some(ref ua) = user {
ua.id == community.owner
} else {
false
};
let is_joined = if let Some(ref ua) = user {
data.0
.get_membership_by_owner_community(ua.id, community.id)
.await
.is_ok()
} else {
false
};
let (is_owner, is_joined, can_post) = community_context_bools!(data, user, community);
context.insert("feed", &feed);
community_context(&mut context, &community, is_owner, is_joined);
community_context(&mut context, &community, is_owner, is_joined, can_post);
// return
Ok(Html(
data.1
.render("communities/feed.html", &mut context)
.unwrap(),
data.1.render("communities/feed.html", &context).unwrap(),
))
}
@ -185,7 +202,7 @@ pub async fn settings_request(
// return
Ok(Html(
data.1
.render("communities/settings.html", &mut context)
.render("communities/settings.html", &context)
.unwrap(),
))
}
@ -226,20 +243,7 @@ pub async fn post_request(
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &user).await;
let is_owner = if let Some(ref ua) = user {
ua.id == community.owner
} else {
false
};
let is_joined = if let Some(ref ua) = user {
data.0
.get_membership_by_owner_community(ua.id, community.id)
.await
.is_ok()
} else {
false
};
let (is_owner, is_joined, can_post) = community_context_bools!(data, user, community);
context.insert("post", &post);
context.insert("replies", &feed);
@ -251,12 +255,10 @@ pub async fn post_request(
.await
.unwrap_or(User::deleted()),
);
community_context(&mut context, &community, is_owner, is_joined);
community_context(&mut context, &community, is_owner, is_joined, can_post);
// return
Ok(Html(
data.1
.render("communities/post.html", &mut context)
.unwrap(),
data.1.render("communities/post.html", &context).unwrap(),
))
}

View file

@ -27,9 +27,9 @@ pub async fn index_request(jar: CookieJar, Extension(data): Extension<State>) ->
let user = get_user_from_token!(jar, data.0);
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &user).await;
let context = initial_context(&data.0.0, lang, &user).await;
Html(data.1.render("misc/index.html", &mut context).unwrap())
Html(data.1.render("misc/index.html", &context).unwrap())
}
/// `/notifs`
@ -58,8 +58,6 @@ pub async fn notifications_request(
// return
Ok(Html(
data.1
.render("misc/notifications.html", &mut context)
.unwrap(),
data.1.render("misc/notifications.html", &context).unwrap(),
))
}

View file

@ -42,9 +42,9 @@ pub async fn render_error(
user: &Option<User>,
) -> String {
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &user).await;
let mut context = initial_context(&data.0.0, lang, user).await;
context.insert("error_text", &e.to_string());
data.1.render("misc/error.html", &mut context).unwrap()
data.1.render("misc/error.html", &context).unwrap()
}
#[derive(Deserialize)]

View file

@ -45,9 +45,7 @@ pub async fn settings_request(
// return
Ok(Html(
data.1
.render("profile/settings.html", &mut context)
.unwrap(),
data.1.render("profile/settings.html", &context).unwrap(),
))
}
@ -143,7 +141,5 @@ pub async fn posts_request(
);
// return
Ok(Html(
data.1.render("profile/posts.html", &mut context).unwrap(),
))
Ok(Html(data.1.render("profile/posts.html", &context).unwrap()))
}