diff --git a/crates/app/src/assets.rs b/crates/app/src/assets.rs index 06c27fb..347fb65 100644 --- a/crates/app/src/assets.rs +++ b/crates/app/src/assets.rs @@ -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); diff --git a/crates/app/src/langs/en-US.toml b/crates/app/src/langs/en-US.toml index 52a1c6a..bac4974 100644 --- a/crates/app/src/langs/en-US.toml +++ b/crates/app/src/langs/en-US.toml @@ -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" diff --git a/crates/app/src/public/css/style.css b/crates/app/src/public/css/style.css index d458df1..6ec1761 100644 --- a/crates/app/src/public/css/style.css +++ b/crates/app/src/public/css/style.css @@ -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; } diff --git a/crates/app/src/public/html/communities/base.html b/crates/app/src/public/html/communities/base.html index f52c898..f189f26 100644 --- a/crates/app/src/public/html/communities/base.html +++ b/crates/app/src/public/html/communities/base.html @@ -165,7 +165,8 @@ }); }; - {% endif %} {% endif %} {% if is_owner or is_manager %} + {% endif %} {% endif %} {% if can_manage_community or + is_manager %} Members - {{ community.member_count }} + {{ community.member_count }}
diff --git a/crates/app/src/public/html/communities/members.html b/crates/app/src/public/html/communities/members.html new file mode 100644 index 0000000..3ff15c5 --- /dev/null +++ b/crates/app/src/public/html/communities/members.html @@ -0,0 +1,38 @@ +{% import "macros.html" as macros %} {% import "components.html" as components +%} {% extends "communities/base.html" %} {% block content %} +
+
+
+ {{ icon "users-round" }} + {{ text "communities:tab.members" }} +
+ +
+ + {% for item in list %} +
+
+ + Since + {{ item[0].created }} + + + {% if can_manage_roles %} + + {{ icon "pencil" }} + {{ text "general:action.manage" }} + + {% endif %} +
+ + {{ components::user_card(user=item[1]) }} +
+ {% endfor %} {{ components::pagination(page=page, items=list|length) + }} +
+
+
+{% endblock %} diff --git a/crates/app/src/public/html/communities/settings.html b/crates/app/src/public/html/communities/settings.html index 0901084..f30cc8b 100644 --- a/crates/app/src/public/html/communities/settings.html +++ b/crates/app/src/public/html/communities/settings.html @@ -335,6 +335,7 @@ BANNED: 1 << 5, REQUESTED: 1 << 6, MANAGE_PINS: 1 << 7, + MANAGE_COMMUNITY: 1 << 8, }; function all_matching_permissions(role) { diff --git a/crates/app/src/public/html/components.html b/crates/app/src/public/html/components.html index a2d869e..5a8d4a1 100644 --- a/crates/app/src/public/html/components.html +++ b/crates/app/src/public/html/components.html @@ -115,7 +115,14 @@ show_community %} > {{ components::community_avatar(id=post.community, community=community) }} - {{ community.title }} + + + {% if community.context.display_name %} + {{ community.context.display_name }} + {% else %} + {{ community.title }} + {% endif %} + {% if post.context.is_pinned %} {{ icon "pin" }} {% endif %} diff --git a/crates/app/src/public/html/misc/notifications.html b/crates/app/src/public/html/misc/notifications.html index 70f4fbb..7b7bfb9 100644 --- a/crates/app/src/public/html/misc/notifications.html +++ b/crates/app/src/public/html/misc/notifications.html @@ -2,12 +2,26 @@ Notifications - {{ config.name }} {% endblock %} {% block body %} {{ macros::nav(selected="notifications") }}
- +
+
+ + {{ icon "bell" }} + {{ text "notifs:label.notifications" }} + - {% for notification in notifications %} {{ - components::notification(notification=notification) }} {% endfor %} + +
+ +
+ {% for notification in notifications %} {{ + components::notification(notification=notification) }} {% endfor %} +
+
{% endblock %} diff --git a/crates/app/src/routes/pages/communities.rs b/crates/app/src/routes/pages/communities.rs index 1ca61a9..b9e5026 100644 --- a/crates/app/src/routes/pages/communities.rs +++ b/crates/app/src/routes/pages/communities.rs @@ -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, + Query(props): Query, + Extension(data): Extension, +) -> 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(), + )) +} diff --git a/crates/app/src/routes/pages/mod.rs b/crates/app/src/routes/pages/mod.rs index 5d0e977..6079e69 100644 --- a/crates/app/src/routes/pages/mod.rs +++ b/crates/app/src/routes/pages/mod.rs @@ -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)) } diff --git a/crates/core/src/database/memberships.rs b/crates/core/src/database/memberships.rs index 2c183e2..68b503a 100644 --- a/crates/core/src/database/memberships.rs +++ b/crates/core/src/database/memberships.rs @@ -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, + ) -> Result> { + 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> { + 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 diff --git a/crates/core/src/model/communities_permissions.rs b/crates/core/src/model/communities_permissions.rs index 929dfdd..656dd96 100644 --- a/crates/core/src/model/communities_permissions.rs +++ b/crates/core/src/model/communities_permissions.rs @@ -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; }