add: community member list

This commit is contained in:
trisua 2025-04-03 20:05:21 -04:00
parent 1a2efdba1f
commit ca0c4b9e0b
12 changed files with 264 additions and 16 deletions

View file

@ -50,6 +50,7 @@ pub const COMMUNITIES_BASE: &str = include_str!("./public/html/communities/base.
pub const COMMUNITIES_FEED: &str = include_str!("./public/html/communities/feed.html");
pub const COMMUNITIES_POST: &str = include_str!("./public/html/communities/post.html");
pub const COMMUNITIES_SETTINGS: &str = include_str!("./public/html/communities/settings.html");
pub const COMMUNITIES_MEMBERS: &str = include_str!("./public/html/communities/members.html");
pub const TIMELINES_HOME: &str = include_str!("./public/html/timelines/home.html");
pub const TIMELINES_POPULAR: &str = include_str!("./public/html/timelines/popular.html");
@ -174,6 +175,7 @@ pub(crate) async fn write_assets(config: &Config) -> PathBufD {
write_template!(html_path->"communities/feed.html"(crate::assets::COMMUNITIES_FEED) --config=config);
write_template!(html_path->"communities/post.html"(crate::assets::COMMUNITIES_POST) --config=config);
write_template!(html_path->"communities/settings.html"(crate::assets::COMMUNITIES_SETTINGS) --config=config);
write_template!(html_path->"communities/members.html"(crate::assets::COMMUNITIES_MEMBERS) --config=config);
write_template!(html_path->"timelines/home.html"(crate::assets::TIMELINES_HOME) -d "timelines" --config=config);
write_template!(html_path->"timelines/popular.html"(crate::assets::TIMELINES_POPULAR) --config=config);

View file

@ -75,6 +75,7 @@ version = "1.0.0"
"notifs:action.mark_as_read" = "Mark as read"
"notifs:action.mark_as_unread" = "Mark as unread"
"notifs:action.clear" = "Clear"
"notifs:label.notifications" = "Notifications"
"settings:tab.general" = "General"
"settings:tab.account" = "Account"

View file

@ -447,7 +447,7 @@ button,
button.small,
.button.small {
min-height: max-content;
padding: 0.25rem;
padding: 0.25rem 0.5rem;
height: 24px;
font-size: 16px;
}

View file

@ -165,7 +165,8 @@
});
};
</script>
{% endif %} {% endif %} {% if is_owner or is_manager %}
{% endif %} {% endif %} {% if can_manage_community or
is_manager %}
<a
href="/community/{{ community.title }}/manage"
class="button primary"
@ -204,7 +205,9 @@
<div class="w-full flex justify-between items-center">
<span class="notification chip">Members</span>
<span>{{ community.member_count }}</span>
<a href="/community/{{ community.title }}/members"
>{{ community.member_count }}</a
>
</div>
<div class="w-full flex justify-between items-center">

View file

@ -0,0 +1,38 @@
{% import "macros.html" as macros %} {% import "components.html" as components
%} {% extends "communities/base.html" %} {% block content %}
<div class="flex flex-col gap-4 w-full">
<div class="card-nest">
<div class="card small flex gap-2 items-center">
{{ icon "users-round" }}
<span>{{ text "communities:tab.members" }}</span>
</div>
<div class="card flex flex-col gap-4">
<!-- prettier-ignore -->
{% for item in list %}
<div class="card-nest">
<div class="card small flex items-center gap-2 justify-between">
<span>
Since
<span class="date">{{ item[0].created }}</span>
</span>
{% if can_manage_roles %}
<a
href="/community/{{ community.title }}/manage?uid={{ item[1].id }}#/members"
class="button small quaternary"
>
{{ icon "pencil" }}
<span>{{ text "general:action.manage" }}</span>
</a>
{% endif %}
</div>
{{ components::user_card(user=item[1]) }}
</div>
{% endfor %} {{ components::pagination(page=page, items=list|length)
}}
</div>
</div>
</div>
{% endblock %}

View file

@ -335,6 +335,7 @@
BANNED: 1 << 5,
REQUESTED: 1 << 6,
MANAGE_PINS: 1 << 7,
MANAGE_COMMUNITY: 1 << 8,
};
function all_matching_permissions(role) {

View file

@ -115,7 +115,14 @@ show_community %}
>
{{ components::community_avatar(id=post.community,
community=community) }}
<b>{{ community.title }}</b>
<b>
<!-- prettier-ignore -->
{% if community.context.display_name %}
{{ community.context.display_name }}
{% else %}
{{ community.title }}
{% endif %}
</b>
{% if post.context.is_pinned %} {{ icon "pin" }} {% endif %}
</a>

View file

@ -2,12 +2,26 @@
<title>Notifications - {{ config.name }}</title>
{% endblock %} {% block body %} {{ macros::nav(selected="notifications") }}
<main class="flex flex-col gap-2">
<button onclick="trigger('me::clear_notifs')">
<div class="card-nest">
<div class="card small flex items-center justify-between gap-2">
<span class="flex items-center gap-2">
{{ icon "bell" }}
<span>{{ text "notifs:label.notifications" }}</span>
</span>
<button
onclick="trigger('me::clear_notifs')"
class="small quaternary"
>
{{ icon "bomb" }}
<span>{{ text "notifs:action.clear" }}</span>
</button>
</div>
<div class="card tertiary flex flex-col gap-4">
{% for notification in notifications %} {{
components::notification(notification=notification) }} {% endfor %}
</div>
</div>
</main>
{% endblock %}

View file

@ -100,7 +100,27 @@ macro_rules! community_context_bools {
false
};
(is_owner, is_joined, is_pending, can_post, can_manage_posts)
let can_manage_community = if let Some(ref membership) = membership {
membership.role.check(CommunityPermission::MANAGE_COMMUNITY)
} else {
false
};
let can_manage_roles = if let Some(ref membership) = membership {
membership.role.check(CommunityPermission::MANAGE_ROLES)
} else {
false
};
(
is_owner,
is_joined,
is_pending,
can_post,
can_manage_posts,
can_manage_community,
can_manage_roles,
)
}};
}
@ -155,6 +175,8 @@ pub fn community_context(
can_post: bool,
can_read: bool,
can_manage_posts: bool,
can_manage_community: bool,
can_manage_roles: bool,
) {
context.insert("community", &community);
context.insert("is_owner", &is_owner);
@ -163,6 +185,8 @@ pub fn community_context(
context.insert("can_post", &can_post);
context.insert("can_read", &can_read);
context.insert("can_manage_posts", &can_manage_posts);
context.insert("can_manage_community", &can_manage_community);
context.insert("can_manage_roles", &can_manage_roles);
}
/// `/community/{title}`
@ -221,8 +245,15 @@ 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, is_pending, can_post, can_manage_posts) =
community_context_bools!(data, user, community);
let (
is_owner,
is_joined,
is_pending,
can_post,
can_manage_posts,
can_manage_community,
can_manage_roles,
) = community_context_bools!(data, user, community);
context.insert("feed", &feed);
context.insert("pinned", &pinned);
@ -236,6 +267,8 @@ pub async fn feed_request(
can_post,
can_read,
can_manage_posts,
can_manage_community,
can_manage_roles,
);
// return
@ -265,7 +298,19 @@ pub async fn settings_request(
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
};
if user.id != community.owner && !user.permissions.check(FinePermission::MANAGE_COMMUNITIES) {
let membership = match data
.0
.get_membership_by_owner_community(user.id, community.id)
.await
{
Ok(m) => m,
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
};
if user.id != community.owner
&& !user.permissions.check(FinePermission::MANAGE_COMMUNITIES)
&& !membership.role.check(CommunityPermission::MANAGE_COMMUNITY)
{
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &None).await,
));
@ -327,8 +372,15 @@ 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, is_pending, can_post, can_manage_posts) =
community_context_bools!(data, user, community);
let (
is_owner,
is_joined,
is_pending,
can_post,
can_manage_posts,
can_manage_community,
can_manage_roles,
) = community_context_bools!(data, user, community);
context.insert("post", &post);
context.insert("replies", &feed);
@ -358,6 +410,8 @@ pub async fn post_request(
can_post,
can_read,
can_manage_posts,
can_manage_community,
can_manage_roles,
);
// return
@ -365,3 +419,82 @@ pub async fn post_request(
data.1.render("communities/post.html", &context).unwrap(),
))
}
/// `/community/{title}/members`
pub async fn members_request(
jar: CookieJar,
Path(title): Path<String>,
Query(props): Query<PaginatedQuery>,
Extension(data): Extension<State>,
) -> impl IntoResponse {
let data = data.read().await;
let user = get_user_from_token!(jar, data.0);
let community = match data.0.get_community_by_title(&title.to_lowercase()).await {
Ok(ua) => ua,
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
let (can_read, _) = check_permissions!(community, jar, data, user);
// ...
let list = match data
.0
.get_memberships_by_community(community.id, 12, props.page)
.await
{
Ok(p) => match data.0.fill_users(p).await {
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
},
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
};
// init context
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &user).await;
let (
is_owner,
is_joined,
is_pending,
can_post,
can_manage_posts,
can_manage_community,
can_manage_roles,
) = community_context_bools!(data, user, community);
context.insert("list", &list);
context.insert("page", &props.page);
community_context(
&mut context,
&community,
is_owner,
is_joined,
is_pending,
can_post,
can_read,
can_manage_posts,
can_manage_community,
can_manage_roles,
);
// return
Ok(Html(
data.1.render("communities/members.html", &context).unwrap(),
))
}

View file

@ -44,6 +44,10 @@ pub fn routes() -> Router {
"/community/{title}/manage",
get(communities::settings_request),
)
.route(
"/community/{title}/members",
get(communities::members_request),
)
.route("/post/{id}", get(communities::post_request))
}

View file

@ -43,6 +43,19 @@ impl DataManager {
Ok(communities)
}
/// Replace a list of community memberships with the proper user.
pub async fn fill_users(
&self,
list: Vec<CommunityMembership>,
) -> Result<Vec<(CommunityMembership, User)>> {
let mut users: Vec<(CommunityMembership, User)> = Vec::new();
for membership in list {
let owner = membership.owner.clone();
users.push((membership, self.get_user_by_id(owner).await?));
}
Ok(users)
}
/// Get a community membership by `owner` and `community`.
pub async fn get_membership_by_owner_community(
&self,
@ -90,6 +103,37 @@ impl DataManager {
Ok(res.unwrap())
}
/// Get all community memberships by `community`.
pub async fn get_memberships_by_community(
&self,
community: usize,
batch: usize,
page: usize,
) -> Result<Vec<CommunityMembership>> {
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_rows!(
&conn,
// 33 = banned, 65 = pending membership
"SELECT * FROM memberships WHERE community = $1 AND NOT role = 33 AND NOT role = 65 ORDER BY created DESC LIMIT $2 OFFSET $3",
&[
&(community as i64),
&(batch as i64),
&((page * batch) as i64)
],
|x| { Self::get_membership_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("community membership".to_string()));
}
Ok(res.unwrap())
}
/// Create a new community membership in the database.
///
/// # Arguments

View file

@ -16,6 +16,7 @@ bitflags! {
const BANNED = 1 << 5;
const REQUESTED = 1 << 6;
const MANAGE_PINS = 1 << 7;
const MANAGE_COMMUNITY = 1 << 8;
const _ = !0;
}