add: pinned posts

This commit is contained in:
trisua 2025-04-03 12:43:36 -04:00
parent e52733b00c
commit eea3d08e1f
10 changed files with 324 additions and 55 deletions

View file

@ -70,6 +70,7 @@ version = "1.0.0"
"communities:label.delete_community" = "Delete community"
"communities:label.change_title" = "Change title"
"communities:label.new_title" = "New title"
"communities:label.pinned" = "Pinned"
"notifs:action.mark_as_read" = "Mark as read"
"notifs:action.mark_as_unread" = "Mark as unread"

View file

@ -31,6 +31,20 @@
</button>
</form>
</div>
{% endif %} {% if pinned|length != 0 %}
<div class="card-nest">
<div class="card small flex gap-2 items-center">
{{ icon "pin" }}
<span>{{ text "communities:label.pinned" }}</span>
</div>
<div class="card flex flex-col gap-4">
<!-- prettier-ignore -->
{% for post in pinned %}
{{ components::post(post=post[0], owner=post[1], secondary=true, show_community=false, can_manage_post=can_manage_posts) }}
{% endfor %}
</div>
</div>
{% endif %}
<div class="card-nest">
@ -42,7 +56,7 @@
<div class="card flex flex-col gap-4">
<!-- prettier-ignore -->
{% for post in feed %}
{{ components::post(post=post[0], owner=post[1], secondary=true, show_community=false) }}
{{ components::post(post=post[0], owner=post[1], secondary=true, show_community=false, can_manage_post=can_manage_posts) }}
{% endfor %}
{{ components::pagination(page=page, items=feed|length) }}

View file

@ -8,7 +8,8 @@
<span>{{ text "communities:action.continue_thread" }}</span>
</a>
{% endif %} {{ components::post(post=post, owner=owner, community=community,
show_community=true) }} {% if user %}
show_community=true, can_manage_post=can_manage_posts) }} {% if user and
post.context.comments_enabled %}
<div class="card-nest">
<div class="card small">
<b>{{ text "communities:label.create_reply" }}</b>
@ -38,9 +39,91 @@
</button>
</form>
</div>
{% endif %} {% if user and user.id == post.owner or can_manage_posts %}
<div class="pillmenu">
<a href="#/replies" data-tab-button="replies" class="active">
{{ icon "newspaper" }}
<span>{{ text "communities:label.replies" }}</span>
</a>
<a href="#/configure" data-tab-button="configure">
{{ icon "settings" }}
<span>{{ text "communities:action.configure" }}</span>
</a>
</div>
<div class="flex flex-col gap-2 hidden" data-tab="configure">
<div class="card-nest w-full">
<div class="card small flex items-center gap-2">
{{ icon "settings" }}
<span>{{ text "communities:action.configure" }}</span>
</div>
<div
class="card tertiary flex flex-col gap-4"
id="post_context"
></div>
</div>
<button onclick="save_context()">
{{ icon "check" }}
<span>{{ text "general:action.save" }}</span>
</button>
<script>
setTimeout(() => {
const ui = ns("ui");
const element = document.getElementById("post_context");
const settings = JSON.parse("{{ post_context_serde|safe }}");
globalThis.save_context = () => {
fetch("/api/v1/posts/{{ post.id }}/context", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
context: settings,
}),
})
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
});
};
ui.refresh_container(element, []);
const can_manage_pins = "{{ can_manage_pins }}" === "true";
const settings_fields = [
[
[
"comments_enabled",
"Allow people to comment on your post",
],
"{{ post.context.comments_enabled }}",
"checkbox",
],
];
if (can_manage_pins) {
settings_fields.push([
["is_pinned", "Pinned to community wall"],
"{{ post.context.is_pinned }}",
"checkbox",
]);
}
ui.generate_settings_ui(element, settings_fields, settings);
}, 250);
</script>
</div>
{% endif %}
<div class="card-nest w-full">
<div class="card-nest w-full" data-tab="replies">
<div class="card small flex items-center gap-2">
{{ icon "newspaper" }}
<span>{{ text "communities:label.replies" }}</span>

View file

@ -346,7 +346,7 @@
role: (new_role) => {
return update_user_role(
e.target.uid.value,
user_role,
new_role,
);
},
},

View file

@ -105,7 +105,8 @@ community %}
{% endif %}
</div>
{%- endmacro %} {% macro post(post, owner, secondary=false, community=false,
show_community=true) -%} {% if community and show_community %}
show_community=true, can_manage_post=false) -%} {% if community and
show_community %}
<div class="card-nest">
<div class="card small">
<a
@ -115,6 +116,8 @@ show_community=true) -%} {% if community and show_community %}
{{ components::community_avatar(id=post.community,
community=community) }}
<b>{{ community.title }}</b>
{% if post.context.is_pinned %} {{ icon "pin" }} {% endif %}
</a>
</div>
{% endif %}
@ -197,7 +200,7 @@ show_community=true) -%} {% if community and show_community %}
<span>{{ text "general:action.report" }}</span>
</button>
{% endif %} {% if (user.id == post.owner) or is_helper
%}
or can_manage_post %}
<b class="title">{{ text "general:action.manage" }}</b>
<button
class="red"

View file

@ -736,12 +736,17 @@ media_theme_pref();
const self = reg_ns("ui");
self.define("refresh_container", (_, element, keep) => {
if (keep.length === 0) {
element.innerHTML = "";
return;
}
for (const child of element.children) {
if (keep.includes(child.getAttribute("ui_ident"))) {
continue;
}
child.remove();
child.outerHTML = "";
}
});

View file

@ -18,6 +18,7 @@ use tetratto_core::model::{
macro_rules! check_permissions {
($community:ident, $jar:ident, $data:ident, $user:ident) => {{
let mut is_member: bool = false;
let mut can_manage_pins: bool = false;
if let Some(ref ua) = $user {
if let Ok(membership) = $data
@ -32,18 +33,24 @@ macro_rules! check_permissions {
} else if membership.role.check_member() {
is_member = true;
}
if membership.role.check(
tetratto_core::model::communities_permissions::CommunityPermission::MANAGE_PINS,
) {
can_manage_pins = true;
}
}
}
match $community.read_access {
CommunityReadAccess::Joined => {
if !is_member {
false
(false, can_manage_pins)
} else {
true
(true, can_manage_pins)
}
}
_ => true,
_ => (true, can_manage_pins),
}
}};
}
@ -87,7 +94,13 @@ macro_rules! community_context_bools {
false
};
(is_owner, is_joined, is_pending, can_post)
let can_manage_posts = if let Some(ref membership) = membership {
membership.role.check(CommunityPermission::MANAGE_POSTS)
} else {
false
};
(is_owner, is_joined, is_pending, can_post, can_manage_posts)
}};
}
@ -141,6 +154,7 @@ pub fn community_context(
is_pending: bool,
can_post: bool,
can_read: bool,
can_manage_posts: bool,
) {
context.insert("community", &community);
context.insert("is_owner", &is_owner);
@ -148,6 +162,7 @@ pub fn community_context(
context.insert("is_pending", &is_pending);
context.insert("can_post", &can_post);
context.insert("can_read", &can_read);
context.insert("can_manage_posts", &can_manage_posts);
}
/// `/community/{title}`
@ -179,7 +194,7 @@ pub async fn feed_request(
}
// check permissions
let can_read = check_permissions!(community, jar, data, user);
let (can_read, _) = check_permissions!(community, jar, data, user);
// ...
let feed = match data
@ -194,14 +209,23 @@ pub async fn feed_request(
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
};
let pinned = match data.0.get_pinned_posts_by_community(community.id).await {
Ok(p) => match data.0.fill_posts(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) =
let (is_owner, is_joined, is_pending, can_post, can_manage_posts) =
community_context_bools!(data, user, community);
context.insert("feed", &feed);
context.insert("pinned", &pinned);
context.insert("page", &props.page);
community_context(
&mut context,
@ -211,6 +235,7 @@ pub async fn feed_request(
is_pending,
can_post,
can_read,
can_manage_posts,
);
// return
@ -287,7 +312,7 @@ pub async fn post_request(
};
// check permissions
let can_read = check_permissions!(community, jar, data, user);
let (can_read, can_manage_pins) = check_permissions!(community, jar, data, user);
// ...
let feed = match data.0.get_post_comments(post.id, 12, props.page).await {
@ -302,7 +327,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, is_joined, is_pending, can_post) =
let (is_owner, is_joined, is_pending, can_post, can_manage_posts) =
community_context_bools!(data, user, community);
context.insert("post", &post);
@ -316,6 +341,14 @@ pub async fn post_request(
.await
.unwrap_or(User::deleted()),
);
context.insert(
"post_context_serde",
&serde_json::to_string(&post.context)
.unwrap()
.replace("\"", "\\\""),
);
context.insert("can_manage_pins", &can_manage_pins);
community_context(
&mut context,
&community,
@ -324,6 +357,7 @@ pub async fn post_request(
is_pending,
can_post,
can_read,
can_manage_posts,
);
// return