add: communities list page

TODO(ui): implement community creation, community viewing, community posting
TODO(ui): implement profile following, followers, and posts feed
This commit is contained in:
trisua 2025-03-27 18:10:47 -04:00
parent 5cfca49793
commit d6fbfc3cd6
28 changed files with 497 additions and 313 deletions

View file

@ -40,6 +40,8 @@ pub const AUTH_REGISTER: &str = include_str!("./public/html/auth/register.html")
pub const PROFILE_BASE: &str = include_str!("./public/html/profile/base.html");
pub const PROFILE_POSTS: &str = include_str!("./public/html/profile/posts.html");
pub const COMMUNITIES_LIST: &str = include_str!("./public/html/communities/list.html");
// langs
pub const LANG_EN_US: &str = include_str!("./langs/en-US.toml");
@ -145,6 +147,8 @@ pub(crate) async fn write_assets(config: &Config) -> PathBufD {
write_template!(html_path->"profile/base.html"(crate::assets::PROFILE_BASE) -d "profile" --config=config);
write_template!(html_path->"profile/posts.html"(crate::assets::PROFILE_POSTS) --config=config);
write_template!(html_path->"communities/list.html"(crate::assets::COMMUNITIES_LIST) -d "communities" --config=config);
html_path
}
@ -179,7 +183,11 @@ pub(crate) async fn init_dirs(config: &Config) {
pub(crate) static CACHE_BREAKER: LazyLock<String> = LazyLock::new(|| salt());
/// Create the initial template context.
pub(crate) fn initial_context(config: &Config, lang: &LangFile, user: &Option<User>) -> Context {
pub(crate) async fn initial_context(
config: &Config,
lang: &LangFile,
user: &Option<User>,
) -> Context {
let mut ctx = Context::new();
ctx.insert("config", &config);
ctx.insert("user", &user);

View file

@ -3,6 +3,7 @@ version = "1.0.0"
[data]
"general:link.home" = "Home"
"general:link.communities" = "Communities"
"dialog:action.okay" = "Ok"
"dialog:action.continue" = "Continue"
@ -13,8 +14,14 @@ version = "1.0.0"
"auth:action.login" = "Login"
"auth:action.register" = "Register"
"auth:action.logout" = "Logout"
"auto:action.follow" = "Follow"
"auto:action.unfollow" = "Unfollow"
"auth:link.my_profile" = "My profile"
"auth:link.settings" = "Settings"
"auth:label.followers" = "Followers"
"auth:label.following" = "Following"
"auth:label.joined_journals" = "Joined Journals"
"communities:action.create" = "Create"
"communities:label.create_new" = "Create new community"
"communities:label.name" = "Name"

View file

@ -0,0 +1,30 @@
{% import "macros.html" as macros %} {% extends "root.html" %} {% block head %}
<title>My communities - {{ config.name }}</title>
{% endblock %} {% block body %} {{ macros::nav(selected="communities") }}
<main class="flex flex-col gap-2">
<div class="card-nest">
<div class="card">
<b>{{ text "communities:label.create_new" }}</b>
</div>
<form class="card flex flex-col gap-2">
<div class="flex flex-col gap-1">
<label for="">{{ text "communities:label.name" }}</label>
<input
type="text"
name="title"
id="title"
placeholder="name"
required
minlength="2"
maxlength="32"
/>
</div>
<button class="primary">
{{ text "communities:action.create" }}
</button>
</form>
</div>
</main>
{% endblock %}

View file

@ -14,11 +14,29 @@
{{ icon "house" }}
<span class="desktop">{{ text "general:link.home" }}</span>
</a>
<a
href="/communities"
class="button {% if selected == 'communities' %}active{% endif %}"
>
{{ icon "book-heart" }}
<span class="desktop"
>{{ text "general:link.communities" }}</span
>
</a>
{% endif %}
</div>
<div class="flex nav_side">
{% if user %}
<a href="/notifs" class="button" title="Notifications">
{{ icon "bell" }} {% if user.notification_count > 0 %}
<span class="notification tr"
>{{ user.notification_count }}</span
>
{% endif %}
</a>
<div class="dropdown">
<!-- prettier-ignore -->
<button
@ -34,7 +52,7 @@
<div class="inner">
<b class="title">{{ user.username }}</b>
<a href="/user/{{ user.username }}">
{{ icon "book-heart" }}
{{ icon "circle-user-round" }}
<span>{{ text "auth:link.my_profile" }}</span>
</a>

View file

@ -1,6 +1,6 @@
{% import "macros.html" as macros %} {% extends "root.html" %} {% block head %}
<title>{{ error_text }} - Tetratto</title>
{% endblock %} {% block body %} {{ macros::nav(selected="home") }}
<title>{{ error_text }} - {{ config.name }}</title>
{% endblock %} {% block body %} {{ macros::nav() }}
<main class="flex flex-col gap-2">
<div class="card-nest">
<div class="card">

View file

@ -1,11 +1,11 @@
use axum::{Extension, Json, extract::Path, response::IntoResponse};
use axum_extra::extract::CookieJar;
use tetratto_core::model::{ApiReturn, Error, journal::Journal};
use tetratto_core::model::{ApiReturn, Error, communities::Community};
use crate::{
State, get_user_from_token,
routes::api::v1::{
CreateJournal, UpdateJournalPrompt, UpdateJournalReadAccess, UpdateJournalTitle,
CreateCommunity, UpdateCommunityContext, UpdateJournalReadAccess, UpdateJournalTitle,
UpdateJournalWriteAccess,
},
};
@ -13,7 +13,7 @@ use crate::{
pub async fn create_request(
jar: CookieJar,
Extension(data): Extension<State>,
Json(req): Json<CreateJournal>,
Json(req): Json<CreateCommunity>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data) {
@ -22,12 +22,12 @@ pub async fn create_request(
};
match data
.create_page(Journal::new(req.title, req.prompt, user.id))
.create_community(Community::new(req.title, user.id))
.await
{
Ok(_) => Json(ApiReturn {
ok: true,
message: "Page created".to_string(),
message: "Community created".to_string(),
payload: (),
}),
Err(e) => return Json(e.into()),
@ -45,10 +45,10 @@ pub async fn delete_request(
None => return Json(Error::NotAllowed.into()),
};
match data.delete_page(id, user).await {
match data.delete_community(id, user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Page deleted".to_string(),
message: "Community deleted".to_string(),
payload: (),
}),
Err(e) => return Json(e.into()),
@ -67,21 +67,21 @@ pub async fn update_title_request(
None => return Json(Error::NotAllowed.into()),
};
match data.update_page_title(id, user, req.title).await {
match data.update_community_title(id, user, req.title).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Page updated".to_string(),
message: "Community updated".to_string(),
payload: (),
}),
Err(e) => return Json(e.into()),
}
}
pub async fn update_prompt_request(
pub async fn update_context_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path(id): Path<usize>,
Json(req): Json<UpdateJournalPrompt>,
Json(req): Json<UpdateCommunityContext>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data) {
@ -89,10 +89,10 @@ pub async fn update_prompt_request(
None => return Json(Error::NotAllowed.into()),
};
match data.update_page_prompt(id, user, req.prompt).await {
match data.update_community_context(id, user, req.context).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Page updated".to_string(),
message: "Community updated".to_string(),
payload: (),
}),
Err(e) => return Json(e.into()),
@ -111,10 +111,13 @@ pub async fn update_read_access_request(
None => return Json(Error::NotAllowed.into()),
};
match data.update_page_read_access(id, user, req.access).await {
match data
.update_community_read_access(id, user, req.access)
.await
{
Ok(_) => Json(ApiReturn {
ok: true,
message: "Page updated".to_string(),
message: "Community updated".to_string(),
payload: (),
}),
Err(e) => return Json(e.into()),
@ -133,10 +136,13 @@ pub async fn update_write_access_request(
None => return Json(Error::NotAllowed.into()),
};
match data.update_page_write_access(id, user, req.access).await {
match data
.update_community_write_access(id, user, req.access)
.await
{
Ok(_) => Json(ApiReturn {
ok: true,
message: "Page updated".to_string(),
message: "Community updated".to_string(),
payload: (),
}),
Err(e) => return Json(e.into()),

View file

@ -0,0 +1,2 @@
pub mod communities;
pub mod posts;

View file

@ -1,6 +1,6 @@
use axum::{Extension, Json, extract::Path, response::IntoResponse};
use axum_extra::extract::CookieJar;
use tetratto_core::model::{ApiReturn, Error, journal::JournalPost};
use tetratto_core::model::{ApiReturn, Error, communities::Post};
use crate::{
State, get_user_from_token,
@ -19,7 +19,7 @@ pub async fn create_request(
};
match data
.create_post(JournalPost::new(
.create_post(Post::new(
req.content,
req.journal,
req.replying_to,
@ -29,7 +29,7 @@ pub async fn create_request(
{
Ok(_) => Json(ApiReturn {
ok: true,
message: "Entry created".to_string(),
message: "Post created".to_string(),
payload: (),
}),
Err(e) => return Json(e.into()),
@ -50,7 +50,7 @@ pub async fn delete_request(
match data.delete_post(id, user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Entry deleted".to_string(),
message: "Post deleted".to_string(),
payload: (),
}),
Err(e) => return Json(e.into()),
@ -72,7 +72,7 @@ pub async fn update_content_request(
match data.update_post_content(id, user, req.content).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Entry updated".to_string(),
message: "Post updated".to_string(),
payload: (),
}),
Err(e) => return Json(e.into()),
@ -94,7 +94,7 @@ pub async fn update_context_request(
match data.update_post_context(id, user, req.context).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Entry updated".to_string(),
message: "Post updated".to_string(),
payload: (),
}),
Err(e) => return Json(e.into()),

View file

@ -1,2 +0,0 @@
pub mod journals;
pub mod posts;

View file

@ -1,5 +1,5 @@
pub mod auth;
pub mod journal;
pub mod communities;
pub mod reactions;
use axum::{
@ -8,7 +8,7 @@ use axum::{
};
use serde::Deserialize;
use tetratto_core::model::{
journal::{JournalPostContext, JournalReadAccess, JournalWriteAccess},
communities::{CommunityContext, CommunityReadAccess, CommunityWriteAccess, PostContext},
reactions::AssetType,
};
@ -19,34 +19,40 @@ pub fn routes() -> Router {
.route("/reactions/{id}", get(reactions::get_request))
.route("/reactions/{id}", delete(reactions::delete_request))
// journal journals
.route("/journals", post(journal::journals::create_request))
.route("/journals/{id}", delete(journal::journals::delete_request))
.route(
"/journals/{id}/title",
post(journal::journals::update_title_request),
"/communities",
post(communities::communities::create_request),
)
.route(
"/journals/{id}/prompt",
post(journal::journals::update_prompt_request),
"/communities/{id}",
delete(communities::communities::delete_request),
)
.route(
"/communities/{id}/title",
post(communities::communities::update_title_request),
)
.route(
"/communities/{id}/context",
post(communities::communities::update_context_request),
)
.route(
"/journals/{id}/access/read",
post(journal::journals::update_read_access_request),
post(communities::communities::update_read_access_request),
)
.route(
"/journals/{id}/access/write",
post(journal::journals::update_write_access_request),
post(communities::communities::update_write_access_request),
)
// journal posts
.route("/posts", post(journal::posts::create_request))
.route("/posts/{id}", delete(journal::posts::delete_request))
// posts
.route("/posts", post(communities::posts::create_request))
.route("/posts/{id}", delete(communities::posts::delete_request))
.route(
"/posts/{id}/content",
post(journal::posts::update_content_request),
post(communities::posts::update_content_request),
)
.route(
"/posts/{id}/context",
post(journal::posts::update_context_request),
post(communities::posts::update_context_request),
)
// auth
// global
@ -99,9 +105,8 @@ pub struct AuthProps {
}
#[derive(Deserialize)]
pub struct CreateJournal {
pub struct CreateCommunity {
pub title: String,
pub prompt: String,
}
#[derive(Deserialize)]
@ -110,18 +115,18 @@ pub struct UpdateJournalTitle {
}
#[derive(Deserialize)]
pub struct UpdateJournalPrompt {
pub prompt: String,
pub struct UpdateCommunityContext {
pub context: CommunityContext,
}
#[derive(Deserialize)]
pub struct UpdateJournalReadAccess {
pub access: JournalReadAccess,
pub access: CommunityReadAccess,
}
#[derive(Deserialize)]
pub struct UpdateJournalWriteAccess {
pub access: JournalWriteAccess,
pub access: CommunityWriteAccess,
}
#[derive(Deserialize)]
@ -139,7 +144,7 @@ pub struct UpdateJournalEntryContent {
#[derive(Deserialize)]
pub struct UpdateJournalEntryContext {
pub context: JournalPostContext,
pub context: PostContext,
}
#[derive(Deserialize)]

View file

@ -15,7 +15,7 @@ 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);
let mut context = initial_context(&data.0.0, lang, &user).await;
Ok(Html(
data.1.render("auth/login.html", &mut context).unwrap(),
@ -35,7 +35,7 @@ pub async fn register_request(
}
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &user);
let mut context = initial_context(&data.0.0, lang, &user).await;
Ok(Html(
data.1.render("auth/register.html", &mut context).unwrap(),

View file

@ -0,0 +1,37 @@
use super::render_error;
use crate::{State, assets::initial_context, get_lang, get_user_from_token};
use axum::{
Extension,
response::{Html, IntoResponse},
};
use axum_extra::extract::CookieJar;
use tetratto_core::model::Error;
/// `/communities`
pub async fn list_request(jar: CookieJar, Extension(data): Extension<State>) -> impl IntoResponse {
let data = data.read().await;
let user = match get_user_from_token!(jar, data.0) {
Some(ua) => ua,
None => {
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &None).await,
));
}
};
let posts = match data.0.get_memberships_by_owner(user.id).await {
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
};
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &Some(user)).await;
context.insert("posts", &posts);
// return
Ok(Html(
data.1
.render("communities/list.html", &mut context)
.unwrap(),
))
}

View file

@ -11,7 +11,7 @@ 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);
let mut context = initial_context(&data.0.0, lang, &user).await;
Html(data.1.render("misc/index.html", &mut context).unwrap())
}

View file

@ -1,4 +1,5 @@
pub mod auth;
pub mod communities;
pub mod misc;
pub mod profile;
@ -21,16 +22,18 @@ pub fn routes() -> Router {
.route("/auth/login", get(auth::login_request))
// profile
.route("/user/{username}", get(profile::posts_request))
// communities
.route("/communities", get(communities::list_request))
}
pub fn render_error(
pub async fn render_error(
e: Error,
jar: &CookieJar,
data: &(DataManager, tera::Tera),
user: &Option<User>,
) -> String {
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &user);
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()
}

View file

@ -6,6 +6,14 @@ use axum::{
response::{Html, IntoResponse},
};
use axum_extra::extract::CookieJar;
use tera::Context;
use tetratto_core::model::{Error, auth::User};
pub fn profile_context(context: &mut Context, profile: &User, is_self: bool, is_following: bool) {
context.insert("profile", &profile);
context.insert("is_self", &is_self);
context.insert("is_following", &is_following);
}
/// `/user/{username}`
pub async fn posts_request(
@ -19,7 +27,7 @@ pub async fn posts_request(
let other_user = match data.0.get_user_by_username(&username).await {
Ok(ua) => ua,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user))),
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
};
let posts = match data
@ -28,15 +36,46 @@ pub async fn posts_request(
.await
{
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user))),
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
};
// check if we're blocked
if let Some(ref ua) = user {
if data
.0
.get_userblock_by_initiator_receiver(other_user.id, ua.id)
.await
.is_ok()
{
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &user).await,
));
}
}
// init context
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &user);
let mut context = initial_context(&data.0.0, lang, &user).await;
let is_self = if let Some(ref ua) = user {
ua.id == other_user.id
} else {
false
};
let is_following = if let Some(ref ua) = user {
data.0
.get_userfollow_by_initiator_receiver(ua.id, other_user.id)
.await
.is_ok()
} else {
false
};
context.insert("profile", &other_user);
context.insert("posts", &posts);
profile_context(&mut context, &other_user, is_self, is_following);
// return
Ok(Html(
data.1.render("profile/posts.html", &mut context).unwrap(),
))