add: policy achievements
This commit is contained in:
parent
d90b08720a
commit
973373426a
7 changed files with 83 additions and 13 deletions
|
@ -3,8 +3,9 @@ use crate::{
|
|||
get_user_from_token,
|
||||
model::{ApiReturn, Error},
|
||||
routes::api::v1::{
|
||||
AppendAssociations, DeleteUser, DisableTotp, RefreshGrantToken, UpdateSecondaryUserRole,
|
||||
UpdateUserIsVerified, UpdateUserPassword, UpdateUserRole, UpdateUserUsername,
|
||||
AppendAssociations, AwardAchievement, DeleteUser, DisableTotp, RefreshGrantToken,
|
||||
UpdateSecondaryUserRole, UpdateUserIsVerified, UpdateUserPassword, UpdateUserRole,
|
||||
UpdateUserUsername,
|
||||
},
|
||||
State,
|
||||
};
|
||||
|
@ -21,7 +22,7 @@ use futures_util::{sink::SinkExt, stream::StreamExt};
|
|||
use tetratto_core::{
|
||||
cache::Cache,
|
||||
model::{
|
||||
auth::{AchievementName, InviteCode, Token, UserSettings},
|
||||
auth::{AchievementName, InviteCode, Token, UserSettings, SELF_SERVE_ACHIEVEMENTS},
|
||||
moderation::AuditLogEntry,
|
||||
oauth,
|
||||
permissions::FinePermission,
|
||||
|
@ -920,3 +921,31 @@ pub async fn generate_invite_codes_request(
|
|||
payload: Some((out_string, errors_string)),
|
||||
})
|
||||
}
|
||||
|
||||
/// Award an achievement to the current user.
|
||||
/// Only works with specific "self-serve" achievements.
|
||||
pub async fn self_serve_achievement_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Json(req): Json<AwardAchievement>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let mut user = match get_user_from_token!(jar, data) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
if !SELF_SERVE_ACHIEVEMENTS.contains(&req.name) {
|
||||
return Json(Error::MiscError("Cannot grant this achievement manually".to_string()).into());
|
||||
}
|
||||
|
||||
// award achievement
|
||||
match data.add_achievement(&mut user, req.name.into()).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Achievement granted".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ use axum::{
|
|||
use serde::Deserialize;
|
||||
use tetratto_core::model::{
|
||||
apps::AppQuota,
|
||||
auth::AchievementName,
|
||||
communities::{
|
||||
CommunityContext, CommunityJoinAccess, CommunityReadAccess, CommunityWriteAccess,
|
||||
PollOption, PostContext,
|
||||
|
@ -387,6 +388,10 @@ pub fn routes() -> Router {
|
|||
"/auth/user/{id}/grants/{app}/refresh",
|
||||
post(auth::profile::refresh_grant_request),
|
||||
)
|
||||
.route(
|
||||
"/auth/user/me/achievement",
|
||||
post(auth::profile::self_serve_achievement_request),
|
||||
)
|
||||
// apps
|
||||
.route("/apps", post(apps::create_request))
|
||||
.route("/apps/{id}/title", post(apps::update_title_request))
|
||||
|
@ -976,7 +981,13 @@ pub struct AddJournalDir {
|
|||
pub struct RemoveJournalDir {
|
||||
pub dir: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateNoteTags {
|
||||
pub tags: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct AwardAchievement {
|
||||
pub name: AchievementName,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue