mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-31 06:23:08 +02:00
Minor license handling tweaks
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
use log::debug;
|
use mime_guess::{mime, Mime};
|
||||||
use mime_guess::{Mime, mime};
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
@@ -7,14 +6,8 @@ use tokio::fs;
|
|||||||
pub async fn read_response_body(body_path: impl AsRef<Path>, content_type: &str) -> Option<String> {
|
pub async fn read_response_body(body_path: impl AsRef<Path>, content_type: &str) -> Option<String> {
|
||||||
let body = fs::read(body_path).await.ok()?;
|
let body = fs::read(body_path).await.ok()?;
|
||||||
let body_charset = parse_charset(content_type).unwrap_or("utf-8".to_string());
|
let body_charset = parse_charset(content_type).unwrap_or("utf-8".to_string());
|
||||||
debug!("body_charset: {}", body_charset);
|
|
||||||
if let Some(decoder) = charset::Charset::for_label(body_charset.as_bytes()) {
|
if let Some(decoder) = charset::Charset::for_label(body_charset.as_bytes()) {
|
||||||
debug!("Using decoder for charset: {}", body_charset);
|
let (cow, _real_encoding, _exist_replace) = decoder.decode(&body);
|
||||||
let (cow, real_encoding, exist_replace) = decoder.decode(&body);
|
|
||||||
debug!(
|
|
||||||
"Decoded body with charset: {}, real_encoding: {:?}, exist_replace: {}",
|
|
||||||
body_charset, real_encoding, exist_replace
|
|
||||||
);
|
|
||||||
return cow.into_owned().into();
|
return cow.into_owned().into();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,22 +1,18 @@
|
|||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use crate::{LicenseCheckStatus, activate_license, check_license, deactivate_license};
|
use crate::{LicenseCheckStatus, activate_license, check_license, deactivate_license};
|
||||||
use log::{debug, info};
|
|
||||||
use tauri::{Runtime, WebviewWindow, command};
|
use tauri::{Runtime, WebviewWindow, command};
|
||||||
|
|
||||||
#[command]
|
#[command]
|
||||||
pub async fn check<R: Runtime>(window: WebviewWindow<R>) -> Result<LicenseCheckStatus> {
|
pub async fn check<R: Runtime>(window: WebviewWindow<R>) -> Result<LicenseCheckStatus> {
|
||||||
debug!("Checking license");
|
|
||||||
check_license(&window).await
|
check_license(&window).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[command]
|
#[command]
|
||||||
pub async fn activate<R: Runtime>(license_key: &str, window: WebviewWindow<R>) -> Result<()> {
|
pub async fn activate<R: Runtime>(license_key: &str, window: WebviewWindow<R>) -> Result<()> {
|
||||||
info!("Activating license {}", license_key);
|
|
||||||
activate_license(&window, license_key).await
|
activate_license(&window, license_key).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[command]
|
#[command]
|
||||||
pub async fn deactivate<R: Runtime>(window: WebviewWindow<R>) -> Result<()> {
|
pub async fn deactivate<R: Runtime>(window: WebviewWindow<R>) -> Result<()> {
|
||||||
info!("Deactivating activation");
|
|
||||||
deactivate_license(&window).await
|
deactivate_license(&window).await
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ pub async fn activate_license<R: Runtime>(
|
|||||||
window: &WebviewWindow<R>,
|
window: &WebviewWindow<R>,
|
||||||
license_key: &str,
|
license_key: &str,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
info!("Activating license {}", license_key);
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let payload = ActivateLicenseRequestPayload {
|
let payload = ActivateLicenseRequestPayload {
|
||||||
license_key: license_key.to_string(),
|
license_key: license_key.to_string(),
|
||||||
@@ -103,6 +104,7 @@ pub async fn activate_license<R: Runtime>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn deactivate_license<R: Runtime>(window: &WebviewWindow<R>) -> Result<()> {
|
pub async fn deactivate_license<R: Runtime>(window: &WebviewWindow<R>) -> Result<()> {
|
||||||
|
info!("Deactivating activation");
|
||||||
let app_handle = window.app_handle();
|
let app_handle = window.app_handle();
|
||||||
let activation_id = get_activation_id(app_handle).await;
|
let activation_id = get_activation_id(app_handle).await;
|
||||||
|
|
||||||
@@ -159,8 +161,6 @@ pub async fn check_license<R: Runtime>(window: &WebviewWindow<R>) -> Result<Lice
|
|||||||
let settings = window.db().get_settings();
|
let settings = window.db().get_settings();
|
||||||
let trial_end = settings.created_at.add(Duration::from_secs(TRIAL_SECONDS));
|
let trial_end = settings.created_at.add(Duration::from_secs(TRIAL_SECONDS));
|
||||||
|
|
||||||
debug!("Trial ending at {trial_end:?}");
|
|
||||||
|
|
||||||
let has_activation_id = !activation_id.is_empty();
|
let has_activation_id = !activation_id.is_empty();
|
||||||
let trial_period_active = Utc::now().naive_utc() < trial_end;
|
let trial_period_active = Utc::now().naive_utc() < trial_end;
|
||||||
|
|
||||||
@@ -185,11 +185,13 @@ pub async fn check_license<R: Runtime>(window: &WebviewWindow<R>) -> Result<Lice
|
|||||||
}
|
}
|
||||||
|
|
||||||
if response.status().is_server_error() {
|
if response.status().is_server_error() {
|
||||||
|
warn!("Failed to check license {}", response.status());
|
||||||
return Err(ServerError);
|
return Err(ServerError);
|
||||||
}
|
}
|
||||||
|
|
||||||
let body: CheckActivationResponsePayload = response.json().await?;
|
let body: CheckActivationResponsePayload = response.json().await?;
|
||||||
if !body.active {
|
if !body.active {
|
||||||
|
info!("Inactive License {:?}", body);
|
||||||
return Ok(LicenseCheckStatus::InvalidLicense);
|
return Ok(LicenseCheckStatus::InvalidLicense);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -75,16 +75,16 @@ function SettingsLicenseCmp() {
|
|||||||
{check.error && <Banner color="danger">{check.error}</Banner>}
|
{check.error && <Banner color="danger">{check.error}</Banner>}
|
||||||
{activate.error && <Banner color="danger">{activate.error}</Banner>}
|
{activate.error && <Banner color="danger">{activate.error}</Banner>}
|
||||||
|
|
||||||
|
{check.data?.type === 'invalid_license' && (
|
||||||
|
<Banner color="danger">
|
||||||
|
Your license is invalid. Please <Link href="https://yaak.app/dashboard">Sign In</Link> for
|
||||||
|
more details
|
||||||
|
</Banner>
|
||||||
|
)}
|
||||||
|
|
||||||
{check.data?.type === 'commercial_use' ? (
|
{check.data?.type === 'commercial_use' ? (
|
||||||
<HStack space={2}>
|
<HStack space={2}>
|
||||||
<Button
|
<Button variant="border" color="secondary" size="sm" onClick={() => deactivate.mutate()}>
|
||||||
variant="border"
|
|
||||||
color="secondary"
|
|
||||||
size="sm"
|
|
||||||
onClick={() => {
|
|
||||||
deactivate.mutate();
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
Deactivate License
|
Deactivate License
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
@@ -104,12 +104,12 @@ function SettingsLicenseCmp() {
|
|||||||
<Button
|
<Button
|
||||||
size="sm"
|
size="sm"
|
||||||
color="primary"
|
color="primary"
|
||||||
|
rightSlot={<Icon icon="external_link" />}
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
openUrl(
|
openUrl(
|
||||||
`https://yaak.app/pricing?s=purchase&ref=app.yaak.desktop&t=${check.data?.type ?? ''}`,
|
`https://yaak.app/pricing?s=purchase&ref=app.yaak.desktop&t=${check.data?.type ?? ''}`,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
rightSlot={<Icon icon="external_link" />}
|
|
||||||
>
|
>
|
||||||
Purchase License
|
Purchase License
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
Reference in New Issue
Block a user