Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(admin): allow generating lifetime gift codes

Hampus6d6e8e6ae874
diff --git a/fluxer_admin/src/api/codes.rs b/fluxer_admin/src/api/codes.rs
index 80b211d4c..82d07e511 100644
--- a/fluxer_admin/src/api/codes.rs
+++ b/fluxer_admin/src/api/codes.rs
@@ -1,34 +1,25 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
-use crate::api::generated::types as generated_types;
+use reqwest::Method;
-use super::client::{AdminApiClient, ApiError, ApiResult};
+use super::client::{AdminApiClient, ApiResult};
use super::types::CodesResponse;
+pub const GIFT_DURATION_TYPES: [&str; 5] = ["days", "weeks", "months", "years", "lifetime"];
+
impl AdminApiClient {
pub async fn generate_gift_codes(
&self,
count: u32,
- duration_type: generated_types::GiftCodeDurationTypeSchema,
+ duration_type: &str,
duration_quantity: u32,
) -> ApiResult<CodesResponse> {
- let body = generated_types::GenerateGiftCodesRequest {
- count: crate::api::generated::nonzero_u32(count, "count")
- .map_err(ApiError::Parse)?
- .into(),
- duration_quantity: crate::api::generated::nonzero_u32(
- duration_quantity,
- "duration_quantity",
- )
- .map_err(ApiError::Parse)?
- .into(),
- duration_type,
- };
- let response = self
- .generated()
- .create_admin_gift_codes(&body)
+ let body = serde_json::json!({
+ "count": count,
+ "duration_type": duration_type,
+ "duration_quantity": duration_quantity,
+ });
+ self.send(Method::POST, "/admin/gift-codes", Some(&body), None)
.await
- .map_err(|e| self.generated_error(e))?;
- self.generated_value(response.into_inner())
}
}
diff --git a/fluxer_admin/src/routes/codes.rs b/fluxer_admin/src/routes/codes.rs
index af958ecde..db78dce56 100644
--- a/fluxer_admin/src/routes/codes.rs
+++ b/fluxer_admin/src/routes/codes.rs
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
use crate::{
- api::{client::AdminApiClient, generated::types::GiftCodeDurationTypeSchema},
+ api::{client::AdminApiClient, codes::GIFT_DURATION_TYPES},
middleware::{
auth::AuthContext,
csrf::CsrfToken,
@@ -33,7 +33,7 @@ struct GiftCodesForm {
#[serde(default)]
count: Option<u32>,
#[serde(default)]
- duration_type: Option<GiftCodeDurationTypeSchema>,
+ duration_type: Option<String>,
#[serde(default)]
duration_quantity: Option<u32>,
}
@@ -91,10 +91,11 @@ async fn gift_codes_post(
let count = form.count.unwrap_or(1).clamp(1, MAX_GIFT_CODES);
let duration_type = form
.duration_type
- .unwrap_or(GiftCodeDurationTypeSchema::Months);
- let duration_quantity = form.duration_quantity.unwrap_or(1);
+ .as_deref()
+ .filter(|value| GIFT_DURATION_TYPES.contains(value))
+ .unwrap_or("months");
+ let duration_quantity = form.duration_quantity.unwrap_or(1).max(1);
let client = AdminApiClient::new(state.http_client(), config, &auth.0.session);
- let secure_cookies = config.secure_cookies();
match client
.generate_gift_codes(count, duration_type, duration_quantity)
.await
diff --git a/fluxer_admin/src/templates/pages/gift_codes.rs b/fluxer_admin/src/templates/pages/gift_codes.rs
index f89fac948..b134be1db 100644
--- a/fluxer_admin/src/templates/pages/gift_codes.rs
+++ b/fluxer_admin/src/templates/pages/gift_codes.rs
@@ -49,8 +49,7 @@ pub fn gift_codes_page(
let base = &config.base_path;
let codes_value = generated_codes.map(|c| c.join("\n")).unwrap_or_default();
let description = format!(
- "Create one-use {} gift URLs with a fixed positive duration. \
- Lifetime gifts cannot be generated here.",
+ "Create one-use {} gift URLs that last for a set time or for life.",
premium.name
);
@@ -101,8 +100,8 @@ pub fn gift_codes_page(
(form_field_group(
"Duration unit", "gift-duration-type",
true, None,
- Some("Days, weeks, months, or years. \
- No lifetime option is available."),
+ Some("Days, weeks, months, years, or lifetime. \
+ Lifetime ignores the quantity."),
html! {
select id="gift-duration-type" name="duration_type"
required class=(FORM_SELECT_CLASS) {
@@ -110,6 +109,7 @@ pub fn gift_codes_page(
option value="weeks" { "Weeks" }
option value="months" selected { "Months" }
option value="years" { "Years" }
+ option value="lifetime" { "Lifetime" }
}
},
))