mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-25 02:08:28 +02:00
Include license status in notification endpoint
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
use std::ops::Deref;
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
|
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
@@ -8,6 +9,7 @@ use reqwest::Method;
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use tauri::{AppHandle, Emitter, Manager, Runtime, WebviewWindow};
|
use tauri::{AppHandle, Emitter, Manager, Runtime, WebviewWindow};
|
||||||
|
use yaak_license::{check_license, LicenseCheckStatus};
|
||||||
use yaak_models::query_manager::QueryManagerExt;
|
use yaak_models::query_manager::QueryManagerExt;
|
||||||
use yaak_models::util::UpdateSource;
|
use yaak_models::util::UpdateSource;
|
||||||
|
|
||||||
@@ -70,6 +72,13 @@ impl YaakNotifier {
|
|||||||
|
|
||||||
self.last_check = SystemTime::now();
|
self.last_check = SystemTime::now();
|
||||||
|
|
||||||
|
let license_check = match check_license(window).await? {
|
||||||
|
LicenseCheckStatus::PersonalUse { .. } => "personal".to_string(),
|
||||||
|
LicenseCheckStatus::CommercialUse => "commercial".to_string(),
|
||||||
|
LicenseCheckStatus::InvalidLicense => "invalid_license".to_string(),
|
||||||
|
LicenseCheckStatus::Trialing { .. } => "trialing".to_string(),
|
||||||
|
};
|
||||||
|
let settings = window.db().get_settings();
|
||||||
let num_launches = get_num_launches(app_handle).await;
|
let num_launches = get_num_launches(app_handle).await;
|
||||||
let info = app_handle.package_info().clone();
|
let info = app_handle.package_info().clone();
|
||||||
let req = reqwest::Client::default()
|
let req = reqwest::Client::default()
|
||||||
@@ -77,6 +86,8 @@ impl YaakNotifier {
|
|||||||
.query(&[
|
.query(&[
|
||||||
("version", info.version.to_string().as_str()),
|
("version", info.version.to_string().as_str()),
|
||||||
("launches", num_launches.to_string().as_str()),
|
("launches", num_launches.to_string().as_str()),
|
||||||
|
("installed", settings.created_at.format("%Y-%m-%d").to_string().as_str()),
|
||||||
|
("license", &license_check),
|
||||||
("platform", get_os()),
|
("platform", get_os()),
|
||||||
]);
|
]);
|
||||||
let resp = req.send().await?;
|
let resp = req.send().await?;
|
||||||
|
|||||||
@@ -1,60 +1,22 @@
|
|||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use crate::{
|
use crate::{LicenseCheckStatus, activate_license, check_license, deactivate_license};
|
||||||
activate_license, check_license, deactivate_license, ActivateLicenseRequestPayload,
|
|
||||||
CheckActivationRequestPayload, DeactivateLicenseRequestPayload, LicenseCheckStatus,
|
|
||||||
};
|
|
||||||
use log::{debug, info};
|
use log::{debug, info};
|
||||||
use std::string::ToString;
|
use tauri::{Runtime, WebviewWindow, command};
|
||||||
use tauri::{command, Manager, Runtime, WebviewWindow};
|
|
||||||
|
|
||||||
#[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");
|
debug!("Checking license");
|
||||||
check_license(
|
check_license(&window).await
|
||||||
&window,
|
|
||||||
CheckActivationRequestPayload {
|
|
||||||
app_platform: get_os().to_string(),
|
|
||||||
app_version: window.package_info().version.to_string(),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.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);
|
info!("Activating license {}", license_key);
|
||||||
activate_license(
|
activate_license(&window, license_key).await
|
||||||
&window,
|
|
||||||
ActivateLicenseRequestPayload {
|
|
||||||
license_key: license_key.to_string(),
|
|
||||||
app_platform: get_os().to_string(),
|
|
||||||
app_version: window.app_handle().package_info().version.to_string(),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.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");
|
info!("Deactivating activation");
|
||||||
deactivate_license(
|
deactivate_license(&window).await
|
||||||
&window,
|
|
||||||
DeactivateLicenseRequestPayload {
|
|
||||||
app_platform: get_os().to_string(),
|
|
||||||
app_version: window.app_handle().package_info().version.to_string(),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_os() -> &'static str {
|
|
||||||
if cfg!(target_os = "windows") {
|
|
||||||
"windows"
|
|
||||||
} else if cfg!(target_os = "macos") {
|
|
||||||
"macos"
|
|
||||||
} else if cfg!(target_os = "linux") {
|
|
||||||
"linux"
|
|
||||||
} else {
|
|
||||||
"unknown"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,3 +16,15 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
|||||||
.invoke_handler(generate_handler![check, activate, deactivate])
|
.invoke_handler(generate_handler![check, activate, deactivate])
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn get_os() -> &'static str {
|
||||||
|
if cfg!(target_os = "windows") {
|
||||||
|
"windows"
|
||||||
|
} else if cfg!(target_os = "macos") {
|
||||||
|
"macos"
|
||||||
|
} else if cfg!(target_os = "linux") {
|
||||||
|
"linux"
|
||||||
|
} else {
|
||||||
|
"unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use log::{debug, info, warn};
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tauri::{is_dev, AppHandle, Emitter, Manager, Runtime, WebviewWindow};
|
use tauri::{AppHandle, Emitter, Manager, Runtime, WebviewWindow, is_dev};
|
||||||
use ts_rs::TS;
|
use ts_rs::TS;
|
||||||
use yaak_models::query_manager::QueryManagerExt;
|
use yaak_models::query_manager::QueryManagerExt;
|
||||||
use yaak_models::util::UpdateSource;
|
use yaak_models::util::UpdateSource;
|
||||||
@@ -63,10 +63,15 @@ pub struct APIErrorResponsePayload {
|
|||||||
|
|
||||||
pub async fn activate_license<R: Runtime>(
|
pub async fn activate_license<R: Runtime>(
|
||||||
window: &WebviewWindow<R>,
|
window: &WebviewWindow<R>,
|
||||||
p: ActivateLicenseRequestPayload,
|
license_key: &str,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let response = client.post(build_url("/licenses/activate")).json(&p).send().await?;
|
let payload = ActivateLicenseRequestPayload {
|
||||||
|
license_key: license_key.to_string(),
|
||||||
|
app_platform: crate::get_os().to_string(),
|
||||||
|
app_version: window.app_handle().package_info().version.to_string(),
|
||||||
|
};
|
||||||
|
let response = client.post(build_url("/licenses/activate")).json(&payload).send().await?;
|
||||||
|
|
||||||
if response.status().is_client_error() {
|
if response.status().is_client_error() {
|
||||||
let body: APIErrorResponsePayload = response.json().await?;
|
let body: APIErrorResponsePayload = response.json().await?;
|
||||||
@@ -95,16 +100,17 @@ pub async fn activate_license<R: Runtime>(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn deactivate_license<R: Runtime>(
|
pub async fn deactivate_license<R: Runtime>(window: &WebviewWindow<R>) -> Result<()> {
|
||||||
window: &WebviewWindow<R>,
|
|
||||||
p: DeactivateLicenseRequestPayload,
|
|
||||||
) -> Result<()> {
|
|
||||||
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;
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let path = format!("/licenses/activations/{}/deactivate", activation_id);
|
let path = format!("/licenses/activations/{}/deactivate", activation_id);
|
||||||
let response = client.post(build_url(&path)).json(&p).send().await?;
|
let payload = DeactivateLicenseRequestPayload {
|
||||||
|
app_platform: crate::get_os().to_string(),
|
||||||
|
app_version: window.app_handle().package_info().version.to_string(),
|
||||||
|
};
|
||||||
|
let response = client.post(build_url(&path)).json(&payload).send().await?;
|
||||||
|
|
||||||
if response.status().is_client_error() {
|
if response.status().is_client_error() {
|
||||||
let body: APIErrorResponsePayload = response.json().await?;
|
let body: APIErrorResponsePayload = response.json().await?;
|
||||||
@@ -141,10 +147,11 @@ pub enum LicenseCheckStatus {
|
|||||||
Trialing { end: NaiveDateTime },
|
Trialing { end: NaiveDateTime },
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn check_license<R: Runtime>(
|
pub async fn check_license<R: Runtime>(window: &WebviewWindow<R>) -> Result<LicenseCheckStatus> {
|
||||||
window: &WebviewWindow<R>,
|
let payload = CheckActivationRequestPayload {
|
||||||
payload: CheckActivationRequestPayload,
|
app_platform: crate::get_os().to_string(),
|
||||||
) -> Result<LicenseCheckStatus> {
|
app_version: window.package_info().version.to_string(),
|
||||||
|
};
|
||||||
let activation_id = get_activation_id(window.app_handle()).await;
|
let activation_id = get_activation_id(window.app_handle()).await;
|
||||||
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));
|
||||||
@@ -197,9 +204,5 @@ fn build_url(path: &str) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_activation_id<R: Runtime>(app_handle: &AppHandle<R>) -> String {
|
pub async fn get_activation_id<R: Runtime>(app_handle: &AppHandle<R>) -> String {
|
||||||
app_handle.db().get_key_value_string(
|
app_handle.db().get_key_value_string(KV_ACTIVATION_ID_KEY, KV_NAMESPACE, "")
|
||||||
KV_ACTIVATION_ID_KEY,
|
|
||||||
KV_NAMESPACE,
|
|
||||||
"",
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user