tauri-plugin-deeprespond-im 1.0.0-alpha

深应智能 IM 的 Tauri 插件:本地存储、安全存储、窗口间的锁与消息、系统事件、通知、角标、文件
//! 通知的命令(引擎和界面窗口都可以调用:自动模式由引擎显示,手动模式由 App 在负责提醒的窗口中显示)。

use std::sync::Arc;

use serde_json::{Map, Value};
use tauri::{Runtime, State, Webview};

use super::{Action, Clear, Request};
use crate::commands::caller;
use crate::error::{Error, Result};
use crate::Shared;

#[allow(clippy::too_many_arguments)]
#[tauri::command(rename_all = "snake_case")]
pub async fn notify_show<R: Runtime>(
    webview: Webview<R>,
    state: State<'_, Arc<Shared>>,
    id: String,
    title: String,
    body: String,
    kind: String,
    conversation_id: Option<String>,
    seq: Option<i64>,
    data: Option<Map<String, Value>>,
    sound: Option<bool>,
    actions: Option<Vec<Action>>,
    group: Option<String>,
) -> Result<()> {
    caller(&state, &webview)?;
    let req = Request { id, title, body, kind, conversation_id, seq, data, sound: sound.unwrap_or(true), actions, group };
    state.notify.show(&req).await
}

/// 清除本应用显示的通知:只给出 id、conversation_id、all 其中之一。
#[tauri::command(rename_all = "snake_case")]
pub async fn notify_clear<R: Runtime>(webview: Webview<R>, state: State<'_, Arc<Shared>>, id: Option<String>, conversation_id: Option<String>, all: Option<bool>) -> Result<()> {
    caller(&state, &webview)?;
    let target = match (id, conversation_id, all.unwrap_or(false)) {
        (Some(id), None, false) => Clear::Id(id),
        (None, Some(conversation_id), false) => Clear::Conversation(conversation_id),
        (None, None, true) => Clear::All,
        _ => return Err(Error::validation("invalid_target", "notify_clear 只能给出 id、conversation_id、all 其中之一")),
    };
    state.notify.clear(target).await
}

/// macOS 为系统授权;Windows 读取用户或组策略是否关闭了本应用的通知;Linux 总是 granted。
#[tauri::command(rename_all = "snake_case")]
pub async fn notify_permission<R: Runtime>(webview: Webview<R>, state: State<'_, Arc<Shared>>) -> Result<&'static str> {
    caller(&state, &webview)?;
    Ok(state.notify.permission().await)
}

#[tauri::command(rename_all = "snake_case")]
pub async fn notify_request_permission<R: Runtime>(webview: Webview<R>, state: State<'_, Arc<Shared>>) -> Result<&'static str> {
    caller(&state, &webview)?;
    Ok(state.notify.request_permission().await)
}

/// 认领一次通知点击:第一个认领的页面得到 true,由它处理(打开会话等)。
#[tauri::command(rename_all = "snake_case")]
pub fn notify_claim<R: Runtime>(webview: Webview<R>, state: State<'_, Arc<Shared>>, click_id: String) -> Result<bool> {
    caller(&state, &webview)?;
    Ok(state.notify.claim(&click_id))
}