2.8 KiB · text History 263b40b
pub mod auth;
pub mod avatar;
pub mod bookmarks;
pub mod categories;
pub mod devices;
pub mod focus;
pub mod forum;
pub mod invites;
pub mod landing;
pub mod moderation;
pub mod notifications;
pub mod out;
pub mod post_links;
pub mod posts;
pub mod reactions;
pub mod shaders;
pub mod tags;

pub use avatar::{delete_avatar, serve_avatar, upload_avatar};
pub use bookmarks::{show_bookmarks, toggle_bookmark};
pub use notifications::{
    mark_all_read, mark_read, show_notifications, toggle_watch, unread_badge,
};
pub use reactions::{
    create_reaction, delete_reaction, manage_reactions, show_reactions, toggle_react,
    toggle_reaction_active,
};
pub use tags::{search, tag_show, tags_index};
pub use auth::{
    handle_create_invite, handle_login, handle_logout, handle_register, resend_verification,
    show_invites, show_login, show_profile, show_register, update_settings, verify_email,
};
pub use categories::{
    category_add_slot, create_category, create_category_inline, delete_category, edit_category_form,
    manage_categories, new_category_form, update_category,
};
pub use forum::{
    categories_index, category_show, create_post, create_topic, delete_topic, new_topic_form, restore_topic,
    topic_show, update_topic_tags,
};
pub use invites::{create_invite_inline, invite_add_slot};
pub use landing::{feed_latest, feed_popular, home};
pub use posts::{
    delete_post, edit_post_form, post_history, post_permalink, restore_post, update_post,
};

use axum::http::StatusCode;
use axum::response::Html;
use tracing::error;

use crate::entities::users;
use crate::AppState;

/// Render a named template with the given context, mapping failures to a 500
/// plus a log line.
pub(crate) fn render(state: &AppState, name: &str, ctx: minijinja::Value) -> Result<Html<String>, StatusCode> {
    let template = state.env.get_template(name).map_err(|e| {
        error!(template = name, error = %e, "load template failed");
        StatusCode::INTERNAL_SERVER_ERROR
    })?;
    let body = template.render(ctx).map_err(|e| {
        error!(template = name, error = %e, "render template failed");
        StatusCode::INTERNAL_SERVER_ERROR
    })?;
    Ok(Html(body))
}

/// Whether `user` may write (post, reply, react, bookmark, tag): always, unless
/// `REQUIRE_EMAIL_VERIFICATION` is on and their address isn't verified yet.
pub fn may_write(state: &AppState, user: &users::Model) -> bool {
    !state.config.require_email_verification || user.email_verified_at.is_some()
}

/// 403 for an unverified account (see [`may_write`]). The UI hides the
/// controls and shows a banner explaining why; this is the server-side gate.
pub(crate) fn require_verified(state: &AppState, user: &users::Model) -> Result<(), StatusCode> {
    if may_write(state, user) {
        Ok(())
    } else {
        Err(StatusCode::FORBIDDEN)
    }
}