diff --git a/src-tauri/src/notifications.rs b/src-tauri/src/notifications.rs index 71e4be47..3eaf30d5 100644 --- a/src-tauri/src/notifications.rs +++ b/src-tauri/src/notifications.rs @@ -1,3 +1,4 @@ +use std::ops::Deref; use std::time::SystemTime; use crate::error::Result; @@ -8,6 +9,7 @@ use reqwest::Method; use serde::{Deserialize, Serialize}; use serde_json::Value; use tauri::{AppHandle, Emitter, Manager, Runtime, WebviewWindow}; +use yaak_license::{check_license, LicenseCheckStatus}; use yaak_models::query_manager::QueryManagerExt; use yaak_models::util::UpdateSource; @@ -70,6 +72,13 @@ impl YaakNotifier { 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 info = app_handle.package_info().clone(); let req = reqwest::Client::default() @@ -77,6 +86,8 @@ impl YaakNotifier { .query(&[ ("version", info.version.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()), ]); let resp = req.send().await?; diff --git a/src-tauri/yaak-license/src/commands.rs b/src-tauri/yaak-license/src/commands.rs index ced67c96..476d39e7 100644 --- a/src-tauri/yaak-license/src/commands.rs +++ b/src-tauri/yaak-license/src/commands.rs @@ -1,60 +1,22 @@ use crate::error::Result; -use crate::{ - activate_license, check_license, deactivate_license, ActivateLicenseRequestPayload, - CheckActivationRequestPayload, DeactivateLicenseRequestPayload, LicenseCheckStatus, -}; +use crate::{LicenseCheckStatus, activate_license, check_license, deactivate_license}; use log::{debug, info}; -use std::string::ToString; -use tauri::{command, Manager, Runtime, WebviewWindow}; +use tauri::{Runtime, WebviewWindow, command}; #[command] pub async fn check(window: WebviewWindow) -> Result { debug!("Checking license"); - check_license( - &window, - CheckActivationRequestPayload { - app_platform: get_os().to_string(), - app_version: window.package_info().version.to_string(), - }, - ) - .await + check_license(&window).await } #[command] pub async fn activate(license_key: &str, window: WebviewWindow) -> Result<()> { info!("Activating license {}", license_key); - activate_license( - &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 + activate_license(&window, license_key).await } #[command] pub async fn deactivate(window: WebviewWindow) -> Result<()> { info!("Deactivating activation"); - deactivate_license( - &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" - } + deactivate_license(&window).await } diff --git a/src-tauri/yaak-license/src/lib.rs b/src-tauri/yaak-license/src/lib.rs index 03b76bfc..dd8fd540 100644 --- a/src-tauri/yaak-license/src/lib.rs +++ b/src-tauri/yaak-license/src/lib.rs @@ -16,3 +16,15 @@ pub fn init() -> TauriPlugin { .invoke_handler(generate_handler![check, activate, deactivate]) .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" + } +} diff --git a/src-tauri/yaak-license/src/license.rs b/src-tauri/yaak-license/src/license.rs index e36caa07..9e22f858 100644 --- a/src-tauri/yaak-license/src/license.rs +++ b/src-tauri/yaak-license/src/license.rs @@ -5,7 +5,7 @@ use log::{debug, info, warn}; use serde::{Deserialize, Serialize}; use std::ops::Add; 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 yaak_models::query_manager::QueryManagerExt; use yaak_models::util::UpdateSource; @@ -63,10 +63,15 @@ pub struct APIErrorResponsePayload { pub async fn activate_license( window: &WebviewWindow, - p: ActivateLicenseRequestPayload, + license_key: &str, ) -> Result<()> { 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() { let body: APIErrorResponsePayload = response.json().await?; @@ -95,16 +100,17 @@ pub async fn activate_license( Ok(()) } -pub async fn deactivate_license( - window: &WebviewWindow, - p: DeactivateLicenseRequestPayload, -) -> Result<()> { +pub async fn deactivate_license(window: &WebviewWindow) -> Result<()> { let app_handle = window.app_handle(); let activation_id = get_activation_id(app_handle).await; let client = reqwest::Client::new(); 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() { let body: APIErrorResponsePayload = response.json().await?; @@ -141,10 +147,11 @@ pub enum LicenseCheckStatus { Trialing { end: NaiveDateTime }, } -pub async fn check_license( - window: &WebviewWindow, - payload: CheckActivationRequestPayload, -) -> Result { +pub async fn check_license(window: &WebviewWindow) -> Result { + let payload = CheckActivationRequestPayload { + app_platform: crate::get_os().to_string(), + app_version: window.package_info().version.to_string(), + }; let activation_id = get_activation_id(window.app_handle()).await; let settings = window.db().get_settings(); 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(app_handle: &AppHandle) -> String { - app_handle.db().get_key_value_string( - KV_ACTIVATION_ID_KEY, - KV_NAMESPACE, - "", - ) + app_handle.db().get_key_value_string(KV_ACTIVATION_ID_KEY, KV_NAMESPACE, "") }