add: request-to-join communities

add: private joined communities setting
add: "void" community
add: ability to delete communities
This commit is contained in:
trisua 2025-04-01 15:03:56 -04:00
parent 3a8af17154
commit d0c1fbcf9a
20 changed files with 669 additions and 122 deletions

View file

@ -16,7 +16,6 @@ version = "1.0.0"
"dialog:action.no" = "No"
"dialog:action.save_and_close" = "Save and close"
"auth:action.login" = "Login"
"auth:action.register" = "Register"
"auth:action.logout" = "Logout"
@ -37,17 +36,22 @@ version = "1.0.0"
"communities:label.create_new" = "Create new community"
"communities:label.name" = "Name"
"communities:action.join" = "Join"
"communities:action.cancel_request" = "Cancel request"
"communities:action.leave" = "Leave"
"communities:action.configure" = "Configure"
"communities:label.create_post" = "Create post"
"communities:label.content" = "Content"
"communities:label.posts" = "Posts"
"communities:label.not_allowed_to_read" = "You're not allowed to view this community's posts"
"communities:label.might_need_to_join" = "You might need to join this community in order to interact with it!"
"communities:label.create_reply" = "Create reply"
"communities:label.replies" = "Replies"
"communities:action.continue_thread" = "Continue thread"
"communities:tab.members" = "Members"
"communities:label.select_member" = "Select member"
"communities:label.user_id" = "User ID"
"communities:label.danger_zone" = "Danger zone"
"communities:label.delete_community" = "Delete community"
"notifs:action.mark_as_read" = "Mark as read"
"notifs:action.mark_as_unread" = "Mark as unread"

View file

@ -880,6 +880,7 @@ dialog::backdrop {
.toast {
box-shadow: 0 0 8px var(--color-shadow);
width: max-content;
max-width: calc(100dvw - 1rem);
border-radius: var(--radius);
padding: 0.75rem 1rem;
animation: popin ease-in-out 1 0.15s running;

View file

@ -20,7 +20,7 @@
{% if community.context.display_name %}
{{ community.context.display_name }}
{% else %}
{{ community.username }}
{{ community.title }}
{% endif %}
</h3>
@ -30,7 +30,8 @@
{% if user %}
<div class="card flex" id="join_or_leave">
{% if not is_owner %} {% if not is_joined %}
{% if not is_owner %} {% if not is_joined %} {% if not
is_pending %}
<button class="primary" onclick="join_community()">
{{ icon "circle-plus" }}
<span>{{ text "communities:action.join" }}</span>
@ -50,10 +51,55 @@
res.ok ? "success" : "error",
res.message,
]);
setTimeout(() => {
window.location.reload();
}, 150);
});
};
</script>
{% else %}
<button
class="quaternary red"
onclick="cancel_request()"
>
{{ icon "x" }}
<span
>{{ text "communities:action.cancel_request"
}}</span
>
</button>
<script>
globalThis.cancel_request = async () => {
if (
!(await trigger("atto::confirm", [
"Are you sure you would like to do this?",
]))
) {
return;
}
fetch(
"/api/v1/communities/{{ community.id }}/memberships/{{ user.id }}",
{
method: "DELETE",
},
)
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
setTimeout(() => {
window.location.reload();
}, 150);
});
};
</script>
{% endif %} {% else %}
<button
class="quaternary red"
onclick="leave_community()"
@ -84,6 +130,10 @@
res.ok ? "success" : "error",
res.message,
]);
setTimeout(() => {
window.location.reload();
}, 150);
});
};
</script>
@ -154,7 +204,25 @@
</div>
</div>
<div class="rhs w-full">{% block content %}{% endblock %}</div>
<div class="rhs w-full">
{% if can_read %} {% block content %}{% endblock %} {% else %}
<div class="card-nest">
<div class="card small flex items-center gap-2">
{{ icon "frown" }}
<b
>{{ text "communities:label.not_allowed_to_read"
}}</b
>
</div>
<div class="card">
<span>
{{ text "communities:label.might_need_to_join" }}
</span>
</div>
</div>
{% endif %}
</div>
</div>
</div>
</article>

View file

@ -32,16 +32,39 @@
Everybody
</option>
<option
value="Unlisted"
selected="{% if community.read_access == 'Unlisted' %}true{% else %}false{% endif %}"
value="Joined"
selected="{% if community.read_access == 'Joined' %}true{% else %}false{% endif %}"
>
Unlisted
Joined
</option>
</select>
</div>
</div>
<div class="card-nest" ui_ident="join_access">
<div class="card small">
<b>Join access</b>
</div>
<div class="card">
<select onchange="save_access(event, 'join')">
<option
value="Everybody"
selected="{% if community.join_access == 'Everybody' %}true{% else %}false{% endif %}"
>
Everybody
</option>
<option
value="Private"
selected="{% if community.read_access == 'Private' %}true{% else %}false{% endif %}"
value="Request"
selected="{% if community.join_access == 'Request' %}true{% else %}false{% endif %}"
>
Private
Request
</option>
<option
value="Nobody"
selected="{% if community.join_access == 'Nobody' %}true{% else %}false{% endif %}"
>
Nobody
</option>
</select>
</div>
@ -77,6 +100,20 @@
</div>
</div>
<div class="card-nest" ui_ident="danger_zone">
<div class="card small flex gap-1 items-center red">
{{ icon "skull" }}
<b> {{ text "communities:label.danger_zone" }} </b>
</div>
<div class="card flex flex-wrap gap-2">
<button class="red quaternary" onclick="delete_community()">
{{ icon "trash" }}
<span>{{ text "communities:label.delete_community" }}</span>
</button>
</div>
</div>
<div class="flex gap-2 flex-wrap">
<button onclick="save_context()">
{{ icon "check" }}
@ -185,6 +222,11 @@
const element = document.getElementById("membership_info");
const ui = ns("ui");
const uid = new URLSearchParams(window.location.search).get("uid");
if (uid) {
document.getElementById("uid").value = uid;
}
globalThis.ban_user = async (uid) => {
if (
!(await trigger("atto::confirm", [
@ -245,6 +287,57 @@
});
};
globalThis.update_user_role = async (uid, new_role) => {
if (
!(await trigger("atto::confirm", [
"Are you sure you would like to do this?",
]))
) {
return;
}
fetch(
`/api/v1/communities/{{ community.id }}/memberships/${uid}/role`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
role: Number.parseInt(new_role),
}),
},
)
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
});
};
globalThis.kick_user = async (uid, new_role) => {
if (
!(await trigger("atto::confirm", [
"Are you sure you would like to do this?",
]))
) {
return;
}
fetch(`/api/v1/communities/{{ community.id }}/memberships/${uid}`, {
method: "DELETE",
})
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
});
};
globalThis.select_user_from_form = (e) => {
e.preventDefault();
fetch(
@ -261,9 +354,11 @@
return;
}
element.innerHTML = `<div class="flex gap-2" ui_ident="actions">
element.innerHTML = `<div class="flex gap-2 flex-wrap" ui_ident="actions">
<a target="_blank" class="button" href="/api/v1/auth/profile/find/${e.target.uid.value}">Open user profile</a>
${res.payload.role !== 33 ? `<button class="red quaternary" onclick="ban_user('${e.target.uid.value}')">Ban</button>` : `<button class="quaternary" onclick="unban_user('${e.target.uid.value}')">Unban</button>`}
${res.payload.role !== 65 ? `<button class="red quaternary" onclick="update_user_role('${e.target.uid.value}', 65)">Send to review</button>` : `<button class="green quaternary" onclick="update_user_role('${e.target.uid.value}', 5)">Accept join request</button>`}
<button class="red quaternary" onclick="kick_user('${e.target.uid.value}')">Kick</button>
</div>`;
ui.refresh_container(element, ["actions"]);
@ -278,34 +373,11 @@
],
null,
{
role: async (new_role) => {
if (
!(await trigger("atto::confirm", [
"Are you sure you would like to do this?",
]))
) {
return;
}
fetch(
`/api/v1/communities/{{ community.id }}/memberships/${e.target.uid.value}/role`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
role: Number.parseInt(new_role),
}),
},
)
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
});
role: (new_role) => {
return update_user_role(
e.target.uid.value,
user_role,
);
},
},
);
@ -400,8 +472,30 @@
});
};
globalThis.delete_community = async () => {
if (
!(await trigger("atto::confirm", [
"Are you sure you would like to do this? This action is permanent.",
]))
) {
return;
}
fetch(`/api/v1/communities/{{ community.id }}`, {
method: "DELETE",
})
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
});
};
ui.refresh_container(document.getElementById("manage_fields"), [
"read_access",
"join_access",
"write_access",
"change_avatar",
"change_banner",

View file

@ -75,7 +75,7 @@
</div>
</div>
{% if not is_self %}
{% if not is_self and user %}
<div class="card-nest">
<div class="card small">
<b>{{ text "auth:label.relationship" }}</b>
@ -157,8 +157,8 @@
</script>
</div>
</div>
{% endif %}
{% endif %} {% if not profile.settings.private_communities or
is_self %}
<div class="card-nest">
<div class="card small flex gap-2 items-center">
{{ icon "users-round" }}
@ -174,6 +174,7 @@
{% endfor %}
</div>
</div>
{% endif %}
</div>
<div class="rhs w-full">{% block content %}{% endblock %}</div>

View file

@ -378,10 +378,21 @@
profile_settings,
[
[
["private_profile", "Private profile"],
[
"private_profile",
"Only allow users I'm following to view my profile",
],
"{{ user.settings.private_profile }}",
"checkbox",
],
[
[
"private_communities",
"Keep my joined communities private",
],
"{{ user.settings.private_communities }}",
"checkbox",
],
],
settings,
);

View file

@ -14,8 +14,9 @@ use tetratto_core::model::{
use crate::{
State, get_user_from_token,
routes::api::v1::{
CreateCommunity, UpdateCommunityContext, UpdateCommunityReadAccess, UpdateCommunityTitle,
UpdateCommunityWriteAccess, UpdateMembershipRole,
CreateCommunity, UpdateCommunityContext, UpdateCommunityJoinAccess,
UpdateCommunityReadAccess, UpdateCommunityTitle, UpdateCommunityWriteAccess,
UpdateMembershipRole,
},
};
@ -175,6 +176,31 @@ pub async fn update_write_access_request(
}
}
pub async fn update_join_access_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path(id): Path<usize>,
Json(req): Json<UpdateCommunityJoinAccess>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
match data
.update_community_join_access(id, user, req.access)
.await
{
Ok(_) => Json(ApiReturn {
ok: true,
message: "Community updated".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}
pub async fn get_membership(
jar: CookieJar,
Extension(data): Extension<State>,
@ -225,9 +251,9 @@ pub async fn create_membership(
))
.await
{
Ok(_) => Json(ApiReturn {
Ok(m) => Json(ApiReturn {
ok: true,
message: "Community joined".to_string(),
message: m,
payload: (),
}),
Err(e) => Json(e.into()),
@ -329,6 +355,31 @@ pub async fn update_membership_role(
return Json(e.into());
};
if let Err(e) = data.incr_community_member_count(community.id).await {
return Json(e.into());
}
} else if req.role.check(CommunityPermission::REQUESTED) {
// user was demoted to a request again
if let Err(e) = data.decr_community_member_count(community.id).await {
return Json(e.into());
}
} else if membership.role.check(CommunityPermission::REQUESTED) {
// user was accepted to community
if let Err(e) = data
.create_notification(Notification::new(
"You have been accepted into a community you requested to join!"
.to_string(),
format!(
"You have been accepted into [{}](/community/{}).",
community.title, community.title
),
membership.owner,
))
.await
{
return Json(e.into());
};
if let Err(e) = data.incr_community_member_count(community.id).await {
return Json(e.into());
}

View file

@ -10,7 +10,10 @@ use axum::{
};
use serde::Deserialize;
use tetratto_core::model::{
communities::{CommunityContext, CommunityReadAccess, CommunityWriteAccess, PostContext},
communities::{
CommunityContext, CommunityJoinAccess, CommunityReadAccess, CommunityWriteAccess,
PostContext,
},
communities_permissions::CommunityPermission,
reactions::AssetType,
};
@ -53,6 +56,10 @@ pub fn routes() -> Router {
"/communities/{id}/access/write",
post(communities::communities::update_write_access_request),
)
.route(
"/communities/{id}/access/join",
post(communities::communities::update_join_access_request),
)
.route(
"/communities/{id}/upload/avatar",
post(communities::images::upload_avatar_request),
@ -194,6 +201,11 @@ pub struct UpdateCommunityWriteAccess {
pub access: CommunityWriteAccess,
}
#[derive(Deserialize)]
pub struct UpdateCommunityJoinAccess {
pub access: CommunityJoinAccess,
}
#[derive(Deserialize)]
pub struct CreatePost {
pub content: String,

View file

@ -11,26 +11,12 @@ use tetratto_core::model::{
Error,
auth::User,
communities::{Community, CommunityReadAccess},
communities_permissions::CommunityPermission,
};
macro_rules! check_permissions {
($community:ident, $jar:ident, $data:ident, $user:ident) => {
match $community.read_access {
CommunityReadAccess::Private => {
if let Some(ref ua) = $user {
if ua.id != $community.owner {
return Err(Html(
render_error(Error::NotAllowed, &$jar, &$data, &$user).await,
));
}
} else {
return Err(Html(
render_error(Error::NotAllowed, &$jar, &$data, &$user).await,
));
}
}
_ => (),
};
($community:ident, $jar:ident, $data:ident, $user:ident) => {{
let mut is_member: bool = false;
if let Some(ref ua) = $user {
if let Ok(membership) = $data
@ -42,30 +28,54 @@ macro_rules! check_permissions {
return Err(Html(
render_error(Error::NotAllowed, &$jar, &$data, &$user).await,
));
} else if membership.role.check_member() {
is_member = true;
}
}
}
};
match $community.read_access {
CommunityReadAccess::Joined => {
if !is_member {
false
} else {
true
}
}
_ => true,
}
}};
}
macro_rules! community_context_bools {
($data:ident, $user:ident, $community:ident) => {{
let membership = if let Some(ref ua) = $user {
match $data
.0
.get_membership_by_owner_community(ua.id, $community.id)
.await
{
Ok(m) => Some(m),
Err(_) => None,
}
} else {
None
};
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
}
let is_joined = if let Some(ref membership) = membership {
membership.role.check_member()
} else {
false
};
let is_pending = if let Some(ref membership) = membership {
membership.role.check(CommunityPermission::REQUESTED)
} else {
false
};
@ -76,7 +86,7 @@ macro_rules! community_context_bools {
false
};
(is_owner, is_joined, can_post)
(is_owner, is_joined, is_pending, can_post)
}};
}
@ -120,12 +130,16 @@ pub fn community_context(
community: &Community,
is_owner: bool,
is_joined: bool,
is_pending: bool,
can_post: bool,
can_read: bool,
) {
context.insert("community", &community);
context.insert("is_owner", &is_owner);
context.insert("is_joined", &is_joined);
context.insert("is_pending", &is_pending);
context.insert("can_post", &can_post);
context.insert("can_read", &can_read);
}
/// `/community/{title}`
@ -143,8 +157,21 @@ pub async fn feed_request(
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
};
if community.id == 0 {
// don't show page for void community
return Err(Html(
render_error(
Error::GeneralNotFound("community".to_string()),
&jar,
&data,
&user,
)
.await,
));
}
// check permissions
check_permissions!(community, jar, data, user);
let can_read = check_permissions!(community, jar, data, user);
// ...
let feed = match data
@ -163,10 +190,19 @@ 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, is_joined, can_post) = community_context_bools!(data, user, community);
let (is_owner, is_joined, is_pending, can_post) =
community_context_bools!(data, user, community);
context.insert("feed", &feed);
community_context(&mut context, &community, is_owner, is_joined, can_post);
community_context(
&mut context,
&community,
is_owner,
is_joined,
is_pending,
can_post,
can_read,
);
// return
Ok(Html(
@ -242,7 +278,7 @@ pub async fn post_request(
};
// check permissions
check_permissions!(community, jar, data, user);
let can_read = check_permissions!(community, jar, data, user);
// ...
let feed = match data.0.get_post_comments(post.id, 12, props.page).await {
@ -257,7 +293,8 @@ 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, is_joined, can_post) = community_context_bools!(data, user, community);
let (is_owner, is_joined, is_pending, can_post) =
community_context_bools!(data, user, community);
context.insert("post", &post);
context.insert("replies", &feed);
@ -269,7 +306,15 @@ pub async fn post_request(
.await
.unwrap_or(User::deleted()),
);
community_context(&mut context, &community, is_owner, is_joined, can_post);
community_context(
&mut context,
&community,
is_owner,
is_joined,
is_pending,
can_post,
can_read,
);
// return
Ok(Html(