mirror of
https://github.com/perstarkse/minne.git
synced 2026-04-23 09:18:36 +02:00
feat: admin panel
This commit is contained in:
@@ -18,7 +18,8 @@ use crate::{
|
|||||||
page_data!(AdminPanelData, "auth/admin_panel.html", {
|
page_data!(AdminPanelData, "auth/admin_panel.html", {
|
||||||
user: User,
|
user: User,
|
||||||
settings: SystemSettings,
|
settings: SystemSettings,
|
||||||
analytics: Analytics
|
analytics: Analytics,
|
||||||
|
users: i64,
|
||||||
});
|
});
|
||||||
|
|
||||||
pub async fn show_admin_panel(
|
pub async fn show_admin_panel(
|
||||||
@@ -27,23 +28,29 @@ pub async fn show_admin_panel(
|
|||||||
) -> Result<impl IntoResponse, HtmlError> {
|
) -> Result<impl IntoResponse, HtmlError> {
|
||||||
// Early return if the user is not authenticated
|
// Early return if the user is not authenticated
|
||||||
let user = match auth.current_user {
|
let user = match auth.current_user {
|
||||||
Some(user) => user,
|
Some(user) if user.admin => user,
|
||||||
None => return Ok(Redirect::to("/").into_response()),
|
_ => return Ok(Redirect::to("/").into_response()),
|
||||||
};
|
};
|
||||||
|
|
||||||
let settings = SystemSettings::get_current(&state.surreal_db_client)
|
let settings = SystemSettings::get_current(&state.surreal_db_client)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||||
|
|
||||||
let analytics = Analytics::get_current(&state.surreal_db_client)
|
let analytics = Analytics::get_current(&state.surreal_db_client)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||||
|
|
||||||
|
let users_count = Analytics::get_users_amount(&state.surreal_db_client)
|
||||||
|
.await
|
||||||
|
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||||
|
|
||||||
let output = render_template(
|
let output = render_template(
|
||||||
AdminPanelData::template_name(),
|
AdminPanelData::template_name(),
|
||||||
AdminPanelData {
|
AdminPanelData {
|
||||||
user,
|
user,
|
||||||
settings,
|
settings,
|
||||||
analytics,
|
analytics,
|
||||||
|
users: users_count,
|
||||||
},
|
},
|
||||||
state.templates.clone(),
|
state.templates.clone(),
|
||||||
)?;
|
)?;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use crate::storage::types::file_info::deserialize_flexible_id;
|
use crate::storage::types::{file_info::deserialize_flexible_id, user::User, StoredObject};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use tracing::info;
|
||||||
|
|
||||||
use crate::{error::AppError, storage::db::SurrealDbClient};
|
use crate::{error::AppError, storage::db::SurrealDbClient};
|
||||||
|
|
||||||
@@ -7,6 +8,7 @@ use crate::{error::AppError, storage::db::SurrealDbClient};
|
|||||||
pub struct Analytics {
|
pub struct Analytics {
|
||||||
#[serde(deserialize_with = "deserialize_flexible_id")]
|
#[serde(deserialize_with = "deserialize_flexible_id")]
|
||||||
pub id: String,
|
pub id: String,
|
||||||
|
pub page_loads: i64,
|
||||||
pub visitors: i64,
|
pub visitors: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,6 +22,7 @@ impl Analytics {
|
|||||||
.content(Analytics {
|
.content(Analytics {
|
||||||
id: "current".to_string(),
|
id: "current".to_string(),
|
||||||
visitors: 0,
|
visitors: 0,
|
||||||
|
page_loads: 0,
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -47,4 +50,30 @@ impl Analytics {
|
|||||||
|
|
||||||
updated.ok_or(AppError::Validation("Failed to update analytics".into()))
|
updated.ok_or(AppError::Validation("Failed to update analytics".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn increment_page_loads(db: &SurrealDbClient) -> Result<Self, AppError> {
|
||||||
|
let updated: Option<Self> = db
|
||||||
|
.client
|
||||||
|
.query("UPDATE type::thing('analytics', 'current') SET page_loads += 1 RETURN AFTER")
|
||||||
|
.await?
|
||||||
|
.take(0)?;
|
||||||
|
|
||||||
|
updated.ok_or(AppError::Validation("Failed to update analytics".into()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_users_amount(db: &SurrealDbClient) -> Result<i64, AppError> {
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct CountResult {
|
||||||
|
count: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
let result: Option<CountResult> = db
|
||||||
|
.client
|
||||||
|
.query("SELECT count() as count FROM type::table($table) GROUP ALL")
|
||||||
|
.bind(("table", User::table_name()))
|
||||||
|
.await?
|
||||||
|
.take(0)?;
|
||||||
|
|
||||||
|
Ok(result.map(|r| r.count).unwrap_or(0))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
<label class="label">
|
<label class="label">
|
||||||
<span class="label-text">Email</span>
|
<span class="label-text">Email</span>
|
||||||
</label>
|
</label>
|
||||||
<input type="email" name="email" value="{{ user.email }}" class="input text-gray-100! input-bordered w-full"
|
<input type="email" name="email" value="{{ user.email }}" class="input text-primary-content input-bordered w-full"
|
||||||
disabled />
|
disabled />
|
||||||
</div>
|
</div>
|
||||||
<div class="form-control">
|
<div class="form-control">
|
||||||
@@ -20,8 +20,8 @@
|
|||||||
</label>
|
</label>
|
||||||
{% block api_key_section %}
|
{% block api_key_section %}
|
||||||
{% if user.api_key %}
|
{% if user.api_key %}
|
||||||
<input type="text" name="api-key" value="{{ user.api_key }}" class="input text-gray-100! input-bordered w-full"
|
<input type="text" name="api-key" value="{{ user.api_key }}"
|
||||||
disabled />
|
class="input text-primary-content input-bordered w-full" disabled />
|
||||||
<a href="https://www.icloud.com/shortcuts/66985f7b98a74aaeac6ba29c3f1f0960"
|
<a href="https://www.icloud.com/shortcuts/66985f7b98a74aaeac6ba29c3f1f0960"
|
||||||
class="btn btn-accent mt-4 w-full">Download
|
class="btn btn-accent mt-4 w-full">Download
|
||||||
iOS
|
iOS
|
||||||
|
|||||||
@@ -1,13 +1,40 @@
|
|||||||
{% extends 'body_base.html' %}
|
{% extends 'body_base.html' %}
|
||||||
{% block main %}
|
{% block main %}
|
||||||
<main class="container justify-center flex-grow flex mx-auto mt-4 p-5">
|
<main class="container flex-grow flex flex-col mx-auto mt-4 p-5 space-y-6">
|
||||||
Hello
|
<h1 class="text-3xl font-bold">Admin Dashboard</h1>
|
||||||
{% if user.admin %}
|
|
||||||
admin
|
|
||||||
{% else %}
|
|
||||||
user
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
{{settings}}
|
<div class="stats stats-vertical lg:stats-horizontal shadow">
|
||||||
|
<div class="stat">
|
||||||
|
<div class="stat-title">Page loads</div>
|
||||||
|
<div class="stat-value text-secondary">{{analytics.page_loads}}</div>
|
||||||
|
<div class="stat-desc">Amount of page loads</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="stat">
|
||||||
|
<div class="stat-title">Unique visitors</div>
|
||||||
|
<div class="stat-value text-primary">{{analytics.visitors}}</div>
|
||||||
|
<div class="stat-desc">Amount of unique visitors</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="stat">
|
||||||
|
<div class="stat-title">Users</div>
|
||||||
|
<div class="stat-value text-accent">{{users}}</div>
|
||||||
|
<div class="stat-desc">Amount of registered users</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Settings in Fieldset -->
|
||||||
|
<div class="grid grid-cols-1 sm:grid-cols-2 gap-6">
|
||||||
|
<fieldset class="fieldset p-4 shadow rounded-box">
|
||||||
|
<legend class="fieldset-legend">Registration</legend>
|
||||||
|
<label class="fieldset-label">
|
||||||
|
<input type="checkbox" class="checkbox" hx-post="/toggle-registrations" hx-target="#registration-status" {% if
|
||||||
|
settings.registrations_enabled %}checked{% endif %} />
|
||||||
|
Enable Registrations
|
||||||
|
</label>
|
||||||
|
<div id="registration-status" class="text-sm mt-2"></div>
|
||||||
|
</fieldset>
|
||||||
|
</div>
|
||||||
</main>
|
</main>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
@@ -37,7 +37,7 @@
|
|||||||
</p>
|
</p>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="mt-2 text-error" id="signup-result"></div>
|
<div class="mt-4 text-error" id="signup-result"></div>
|
||||||
<div class="form-control mt-6">
|
<div class="form-control mt-6">
|
||||||
<button id="submit-btn" class="btn btn-primary w-full">
|
<button id="submit-btn" class="btn btn-primary w-full">
|
||||||
Create Account
|
Create Account
|
||||||
|
|||||||
@@ -19,7 +19,9 @@
|
|||||||
<summary>Account</summary>
|
<summary>Account</summary>
|
||||||
<ul class="bg-base-100 rounded-t-none p-2">
|
<ul class="bg-base-100 rounded-t-none p-2">
|
||||||
<li><a hx-boost="true" class="" href="/account">Account</a></li>
|
<li><a hx-boost="true" class="" href="/account">Account</a></li>
|
||||||
|
{% if user.admin %}
|
||||||
<li><a hx-boost="true" class="" href="/admin">Admin</a></li>
|
<li><a hx-boost="true" class="" href="/admin">Admin</a></li>
|
||||||
|
{% endif %}
|
||||||
<li><a hx-boost="true" href="/signout">Sign out</a></li>
|
<li><a hx-boost="true" href="/signout">Sign out</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</details>
|
</details>
|
||||||
|
|||||||
Reference in New Issue
Block a user