add: user achievements

This commit is contained in:
trisua 2025-06-27 03:45:50 -04:00
parent e7c4cf14aa
commit b860f74124
15 changed files with 318 additions and 11 deletions

View file

@ -51,6 +51,7 @@ pub const MISC_ERROR: &str = include_str!("./public/html/misc/error.lisp");
pub const MISC_NOTIFICATIONS: &str = include_str!("./public/html/misc/notifications.lisp");
pub const MISC_MARKDOWN: &str = include_str!("./public/html/misc/markdown.lisp");
pub const MISC_REQUESTS: &str = include_str!("./public/html/misc/requests.lisp");
pub const MISC_ACHIEVEMENTS: &str = include_str!("./public/html/misc/achievements.lisp");
pub const AUTH_BASE: &str = include_str!("./public/html/auth/base.lisp");
pub const AUTH_LOGIN: &str = include_str!("./public/html/auth/login.lisp");
@ -349,6 +350,7 @@ pub(crate) async fn write_assets(config: &Config) -> PathBufD {
write_template!(html_path->"misc/notifications.html"(crate::assets::MISC_NOTIFICATIONS) --config=config --lisp plugins);
write_template!(html_path->"misc/markdown.html"(crate::assets::MISC_MARKDOWN) --config=config --lisp plugins);
write_template!(html_path->"misc/requests.html"(crate::assets::MISC_REQUESTS) --config=config --lisp plugins);
write_template!(html_path->"misc/achievements.html"(crate::assets::MISC_ACHIEVEMENTS) --config=config --lisp plugins);
write_template!(html_path->"auth/base.html"(crate::assets::AUTH_BASE) -d "auth" --config=config --lisp plugins);
write_template!(html_path->"auth/login.html"(crate::assets::AUTH_LOGIN) --config=config --lisp plugins);

View file

@ -17,6 +17,7 @@ version = "1.0.0"
"general:link.stats" = "Stats"
"general:link.search" = "Search"
"general:link.journals" = "Journals"
"general:link.achievements" = "Achievements"
"general:action.save" = "Save"
"general:action.delete" = "Delete"
"general:action.purge" = "Purge"

View file

@ -1083,6 +1083,10 @@
("href" "/journals/0/0")
(icon (text "notebook"))
(str (text "general:link.journals")))
(a
("href" "/achievements")
(icon (text "award"))
(str (text "general:link.achievements")))
(a
("href" "/settings")
(text "{{ icon \"settings\" }}")

View file

@ -0,0 +1,42 @@
(text "{% extends \"root.html\" %} {% block head %}")
(title
(text "Achievements - {{ config.name }}"))
(text "{% endblock %} {% block body %} {{ macros::nav(selected=\"achievements\") }}")
(main
("class" "flex flex-col gap-2")
(div
("class" "card-nest")
(div
("class" "card small flex items-center gap-2")
(icon (text "coffee"))
(span (text "Welcome to {{ config.name }}!")))
(div
("class" "card no_p_margin")
(p (text "To help you move in, you'll be rewarded with small achievements for completing simple tasks on {{ config.name }}!"))
(p (text "You'll find out what each achievement is when you get it, so look around!"))))
(div
("class" "card-nest")
(div
("class" "card small flex items-center justify-between gap-2")
(span
("class" "flex items-center gap-2")
(icon (text "award"))
(span (str (text "general:link.achievements")))))
(div
("class" "card lowered flex flex-col gap-4")
(text "{% for achievement in achievements %}")
(div
("class" "w-full card-nest")
(div
("class" "card small flex items-center gap-2 {% if achievement[2] == 'Uncommon' -%} green {%- elif achievement[2] == 'Rare' -%} purple {%- endif %}")
(icon (text "award"))
(text "{{ achievement[0] }}"))
(div
("class" "card flex flex-col gap-2")
(span ("class" "no_p_margin") (text "{{ achievement[1]|markdown|safe }}"))
(hr)
(span ("class" "fade") (text "Unlocked: ") (span ("class" "date") (text "{{ achievement[3].unlocked }}")))))
(text "{% endfor %}"))))
(text "{% endblock %}")

View file

@ -21,7 +21,7 @@ use futures_util::{sink::SinkExt, stream::StreamExt};
use tetratto_core::{
cache::Cache,
model::{
auth::{InviteCode, Token, UserSettings},
auth::{AchievementName, InviteCode, Token, UserSettings},
moderation::AuditLogEntry,
oauth,
permissions::FinePermission,
@ -151,6 +151,14 @@ pub async fn update_user_settings_request(
req.theme_lit = format!("{}%", req.theme_lit)
}
// award achievement
if let Err(e) = data
.add_achievement(&user, AchievementName::EditSettings.into())
.await
{
return Json(e.into());
}
// ...
match data.update_user_settings(id, req).await {
Ok(_) => Json(ApiReturn {

View file

@ -11,7 +11,7 @@ use axum::{
};
use axum_extra::extract::CookieJar;
use tetratto_core::model::{
auth::{FollowResult, IpBlock, Notification, UserBlock, UserFollow},
auth::{AchievementName, FollowResult, IpBlock, Notification, UserBlock, UserFollow},
oauth,
};
@ -59,6 +59,13 @@ pub async fn follow_request(
return Json(e.into());
};
if let Err(e) = data
.add_achievement(&user, AchievementName::FollowUser.into())
.await
{
return Json(e.into());
}
Json(ApiReturn {
ok: true,
message: "User followed".to_string(),

View file

@ -7,6 +7,7 @@ use axum::{
use axum_extra::extract::CookieJar;
use tetratto_core::model::{
addr::RemoteAddr,
auth::AchievementName,
communities::{Poll, PollVote, Post},
oauth,
permissions::FinePermission,
@ -178,6 +179,41 @@ pub async fn create_request(
}
}
// achievements
if let Err(e) = data
.add_achievement(&user, AchievementName::CreatePost.into())
.await
{
return Json(e.into());
}
if user.post_count >= 49 {
if let Err(e) = data
.add_achievement(&user, AchievementName::Create50Posts.into())
.await
{
return Json(e.into());
}
}
if user.post_count >= 99 {
if let Err(e) = data
.add_achievement(&user, AchievementName::Create100Posts.into())
.await
{
return Json(e.into());
}
}
if user.post_count >= 999 {
if let Err(e) = data
.add_achievement(&user, AchievementName::Create1000Posts.into())
.await
{
return Json(e.into());
}
}
// return
Json(ApiReturn {
ok: true,

View file

@ -7,7 +7,7 @@ use axum::{
use axum_extra::extract::CookieJar;
use tetratto_core::model::{
addr::RemoteAddr,
auth::IpBlock,
auth::{AchievementName, IpBlock},
communities::{CommunityReadAccess, Question},
oauth,
permissions::FinePermission,
@ -50,6 +50,16 @@ pub async fn create_request(
return Json(Error::NotAllowed.into());
}
// award achievement
if let Some(ref user) = user {
if let Err(e) = data
.add_achievement(user, AchievementName::CreateQuestion.into())
.await
{
return Json(e.into());
}
}
// ...
let mut props = Question::new(
if let Some(ref ua) = user { ua.id } else { 0 },

View file

@ -15,6 +15,7 @@ use crate::{
use tetratto_core::{
database::NAME_REGEX,
model::{
auth::AchievementName,
journals::{Journal, JournalPrivacyPermission},
oauth,
permissions::FinePermission,
@ -106,11 +107,22 @@ pub async fn create_request(
.create_journal(Journal::new(user.id, props.title))
.await
{
Ok(x) => Json(ApiReturn {
ok: true,
message: "Journal created".to_string(),
payload: Some(x.id.to_string()),
}),
Ok(x) => {
// award achievement
if let Err(e) = data
.add_achievement(&user, AchievementName::CreateJournal.into())
.await
{
return Json(e.into());
}
// ...
Json(ApiReturn {
ok: true,
message: "Journal created".to_string(),
payload: Some(x.id.to_string()),
})
}
Err(e) => Json(e.into()),
}
}

View file

@ -441,6 +441,34 @@ pub async fn requests_request(
Ok(Html(data.1.render("misc/requests.html", &context).unwrap()))
}
/// `/achievements`
pub async fn achievements_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 achievements = data.0.fill_achievements(user.achievements.clone());
// ...
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0.0, lang, &Some(user)).await;
context.insert("achievements", &achievements);
// return
Ok(Html(
data.1.render("misc/achievements.html", &context).unwrap(),
))
}
/// `/doc/{file_name}`
pub async fn markdown_document_request(
jar: CookieJar,

View file

@ -45,6 +45,7 @@ pub fn routes() -> Router {
// misc
.route("/notifs", get(misc::notifications_request))
.route("/requests", get(misc::requests_request))
.route("/achievements", get(misc::achievements_request))
.route("/doc/{*file_name}", get(misc::markdown_document_request))
.fallback_service(get(misc::not_found))
// mod