mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-11 03:26:58 +02:00
Dynamic plugins (#68)
This commit is contained in:
@@ -655,6 +655,7 @@ pub struct GrpcEvent {
|
||||
pub request_id: String,
|
||||
pub connection_id: String,
|
||||
pub created_at: NaiveDateTime,
|
||||
pub updated_at: NaiveDateTime,
|
||||
pub content: String,
|
||||
pub event_type: GrpcEventType,
|
||||
pub metadata: HashMap<String, String>,
|
||||
@@ -693,6 +694,7 @@ impl<'s> TryFrom<&Row<'s>> for GrpcEvent {
|
||||
request_id: r.get("request_id")?,
|
||||
connection_id: r.get("connection_id")?,
|
||||
created_at: r.get("created_at")?,
|
||||
updated_at: r.get("updated_at")?,
|
||||
content: r.get("content")?,
|
||||
event_type: serde_json::from_str(event_type.as_str()).unwrap_or_default(),
|
||||
metadata: serde_json::from_str(metadata.as_str()).unwrap_or_default(),
|
||||
@@ -702,6 +704,51 @@ impl<'s> TryFrom<&Row<'s>> for GrpcEvent {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default, TS)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct Plugin {
|
||||
pub id: String,
|
||||
#[ts(type = "\"plugin\"")]
|
||||
pub model: String,
|
||||
pub created_at: NaiveDateTime,
|
||||
pub updated_at: NaiveDateTime,
|
||||
pub checked_at: Option<NaiveDateTime>,
|
||||
pub directory: String,
|
||||
pub url: Option<String>,
|
||||
pub enabled: bool,
|
||||
}
|
||||
|
||||
#[derive(Iden)]
|
||||
pub enum PluginIden {
|
||||
#[iden = "plugins"]
|
||||
Table,
|
||||
Id,
|
||||
Model,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
CheckedAt,
|
||||
Directory,
|
||||
Url,
|
||||
Enabled,
|
||||
}
|
||||
|
||||
impl<'s> TryFrom<&Row<'s>> for Plugin {
|
||||
type Error = rusqlite::Error;
|
||||
|
||||
fn try_from(r: &Row<'s>) -> Result<Self, Self::Error> {
|
||||
Ok(Plugin {
|
||||
id: r.get("id")?,
|
||||
model: r.get("model")?,
|
||||
created_at: r.get("created_at")?,
|
||||
updated_at: r.get("updated_at")?,
|
||||
checked_at: r.get("checked_at")?,
|
||||
url: r.get("url")?,
|
||||
directory: r.get("directory")?,
|
||||
enabled: r.get("enabled")?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default, TS)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct KeyValue {
|
||||
@@ -758,6 +805,7 @@ pub enum ModelType {
|
||||
TypeGrpcRequest,
|
||||
TypeHttpRequest,
|
||||
TypeHttpResponse,
|
||||
TypePlugin,
|
||||
TypeWorkspace,
|
||||
}
|
||||
|
||||
@@ -772,6 +820,7 @@ impl ModelType {
|
||||
ModelType::TypeGrpcRequest => "gr",
|
||||
ModelType::TypeHttpRequest => "rq",
|
||||
ModelType::TypeHttpResponse => "rs",
|
||||
ModelType::TypePlugin => "pg",
|
||||
ModelType::TypeWorkspace => "wk",
|
||||
}
|
||||
.to_string()
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
use std::fs;
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::models::{
|
||||
CookieJar, CookieJarIden, Environment, EnvironmentIden, Folder, FolderIden, GrpcConnection,
|
||||
GrpcConnectionIden, GrpcEvent, GrpcEventIden, GrpcRequest, GrpcRequestIden, HttpRequest,
|
||||
HttpRequestIden, HttpResponse, HttpResponseHeader, HttpResponseIden, KeyValue, KeyValueIden,
|
||||
ModelType, Settings, SettingsIden, Workspace, WorkspaceIden,
|
||||
};
|
||||
use crate::models::{CookieJar, CookieJarIden, Environment, EnvironmentIden, Folder, FolderIden, GrpcConnection, GrpcConnectionIden, GrpcEvent, GrpcEventIden, GrpcRequest, GrpcRequestIden, HttpRequest, HttpRequestIden, HttpResponse, HttpResponseHeader, HttpResponseIden, KeyValue, KeyValueIden, ModelType, Plugin, PluginIden, Settings, SettingsIden, Workspace, WorkspaceIden};
|
||||
use crate::plugin::SqliteConnection;
|
||||
use log::{debug, error};
|
||||
use rand::distributions::{Alphanumeric, DistString};
|
||||
@@ -848,7 +843,7 @@ pub async fn upsert_environment<R: Runtime>(
|
||||
serde_json::to_string(&environment.variables)?.into(),
|
||||
])
|
||||
.on_conflict(
|
||||
OnConflict::column(GrpcEventIden::Id)
|
||||
OnConflict::column(EnvironmentIden::Id)
|
||||
.update_columns([
|
||||
EnvironmentIden::UpdatedAt,
|
||||
EnvironmentIden::Name,
|
||||
@@ -877,6 +872,88 @@ pub async fn get_environment<R: Runtime>(mgr: &impl Manager<R>, id: &str) -> Res
|
||||
Ok(stmt.query_row(&*params.as_params(), |row| row.try_into())?)
|
||||
}
|
||||
|
||||
pub async fn get_plugin<R: Runtime>(
|
||||
mgr: &impl Manager<R>,
|
||||
id: &str
|
||||
) -> Result<Plugin> {
|
||||
let dbm = &*mgr.state::<SqliteConnection>();
|
||||
let db = dbm.0.lock().await.get().unwrap();
|
||||
|
||||
let (sql, params) = Query::select()
|
||||
.from(PluginIden::Table)
|
||||
.column(Asterisk)
|
||||
.cond_where(Expr::col(EnvironmentIden::Id).eq(id))
|
||||
.build_rusqlite(SqliteQueryBuilder);
|
||||
let mut stmt = db.prepare(sql.as_str())?;
|
||||
Ok(stmt.query_row(&*params.as_params(), |row| row.try_into())?)
|
||||
}
|
||||
|
||||
pub async fn list_plugins<R: Runtime>(
|
||||
mgr: &impl Manager<R>,
|
||||
) -> Result<Vec<Plugin>> {
|
||||
let dbm = &*mgr.state::<SqliteConnection>();
|
||||
let db = dbm.0.lock().await.get().unwrap();
|
||||
|
||||
let (sql, params) = Query::select()
|
||||
.from(PluginIden::Table)
|
||||
.column(Asterisk)
|
||||
.order_by(PluginIden::CreatedAt, Order::Desc)
|
||||
.build_rusqlite(SqliteQueryBuilder);
|
||||
let mut stmt = db.prepare(sql.as_str())?;
|
||||
let items = stmt.query_map(&*params.as_params(), |row| row.try_into())?;
|
||||
Ok(items.map(|v| v.unwrap()).collect())
|
||||
}
|
||||
|
||||
pub async fn upsert_plugin<R: Runtime>(
|
||||
window: &WebviewWindow<R>,
|
||||
plugin: Plugin,
|
||||
) -> Result<Plugin> {
|
||||
let id = match plugin.id.as_str() {
|
||||
"" => generate_model_id(ModelType::TypePlugin),
|
||||
_ => plugin.id.to_string(),
|
||||
};
|
||||
let dbm = &*window.app_handle().state::<SqliteConnection>();
|
||||
let db = dbm.0.lock().await.get().unwrap();
|
||||
|
||||
let (sql, params) = Query::insert()
|
||||
.into_table(PluginIden::Table)
|
||||
.columns([
|
||||
PluginIden::Id,
|
||||
PluginIden::CreatedAt,
|
||||
PluginIden::UpdatedAt,
|
||||
PluginIden::CheckedAt,
|
||||
PluginIden::Directory,
|
||||
PluginIden::Url,
|
||||
PluginIden::Enabled,
|
||||
])
|
||||
.values_panic([
|
||||
id.as_str().into(),
|
||||
CurrentTimestamp.into(),
|
||||
CurrentTimestamp.into(),
|
||||
plugin.checked_at.into(),
|
||||
plugin.directory.into(),
|
||||
plugin.url.into(),
|
||||
plugin.enabled.into(),
|
||||
])
|
||||
.on_conflict(
|
||||
OnConflict::column(PluginIden::Id)
|
||||
.update_columns([
|
||||
PluginIden::UpdatedAt,
|
||||
PluginIden::CheckedAt,
|
||||
PluginIden::Directory,
|
||||
PluginIden::Url,
|
||||
PluginIden::Enabled,
|
||||
])
|
||||
.to_owned(),
|
||||
)
|
||||
.returning_all()
|
||||
.build_rusqlite(SqliteQueryBuilder);
|
||||
|
||||
let mut stmt = db.prepare(sql.as_str())?;
|
||||
let m = stmt.query_row(&*params.as_params(), |row| row.try_into())?;
|
||||
Ok(emit_upserted_model(window, m))
|
||||
}
|
||||
|
||||
pub async fn get_folder<R: Runtime>(mgr: &impl Manager<R>, id: &str) -> Result<Folder> {
|
||||
let dbm = &*mgr.state::<SqliteConnection>();
|
||||
let db = dbm.0.lock().await.get().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user