Skip to content

Commit

Permalink
plugin search
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Nov 4, 2022
1 parent bb21ec5 commit 8bb0afa
Show file tree
Hide file tree
Showing 18 changed files with 3,665 additions and 116 deletions.
62 changes: 33 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions migrations/2022-10-27-193452_create_plugins_table/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ create table plugins (
description VARCHAR NOT NULL,
downloads INTEGER NOT NULL DEFAULT 0,
repository VARCHAR,
wasm bool NOT NULL DEFAULT true,
CONSTRAINT "plugins_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id")
);

Expand Down
8 changes: 4 additions & 4 deletions nginx/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<html>
<head>
<title>Lapce Plugins</title>
<link rel="stylesheet" href="/static/main.css?id=da26a5870bfeb909">
<link rel="preload" href="/static/main.wasm?id=da26a5870bfeb909" as="fetch" type="application/wasm" crossorigin="">
<link rel="modulepreload" href="/static/main.js?id=da26a5870bfeb909">
<link rel="stylesheet" href="/static/main.css?id=da26a5870bfeb90d">
<link rel="preload" href="/static/main.wasm?id=da26a5870bfeb90d" as="fetch" type="application/wasm" crossorigin="">
<link rel="modulepreload" href="/static/main.js?id=da26a5870bfeb90d">
<meta charset="utf-8">
</head>
<body class = "bg-body text-body font-body">
<script type="module">import init from '/static/main.js?id=da26a5870bfeb909';init('/static/main.wasm?id=da26a5870bfeb909');</script>
<script type="module">import init from '/static/main.js?id=da26a5870bfeb90d';init('/static/main.wasm?id=da26a5870bfeb90d');</script>
</body>
</html>
2 changes: 1 addition & 1 deletion volts-back/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ tokio = { version = "1.21.2", features = ["full"] }
dotenvy = "0.15.6"
volts-core = { path = "../volts-core" }
toml_edit = { version = "0.14.4", features = ["easy"] }
lapce-rpc = { git = "https://github.com/lapce/lapce" }
lapce-rpc = "0.2.1"
15 changes: 14 additions & 1 deletion volts-back/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,28 @@ pub struct NewPlugin<'a> {
pub user_id: i32,
pub display_name: &'a str,
pub description: &'a str,
pub repository: Option<&'a str>,
pub downloads: i32,
pub wasm: bool,
}

impl<'a> NewPlugin<'a> {
pub fn new(name: &'a str, user_id: i32, display_name: &'a str, description: &'a str) -> Self {
pub fn new(
name: &'a str,
user_id: i32,
display_name: &'a str,
description: &'a str,
repository: Option<&'a str>,
wasm: bool,
) -> Self {
NewPlugin {
name,
user_id,
display_name,
description,
downloads: 0,
repository,
wasm,
}
}

Expand All @@ -180,6 +191,8 @@ impl<'a> NewPlugin<'a> {
.set((
display_name.eq(excluded(display_name)),
description.eq(excluded(description)),
repository.eq(excluded(repository)),
wasm.eq(excluded(wasm)),
updated_at.eq(chrono::Utc::now().naive_utc()),
))
.get_result(conn)
Expand Down
28 changes: 22 additions & 6 deletions volts-back/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ pub struct SearchQuery {
q: Option<String>,
sort: Option<String>,
limit: Option<usize>,
page: Option<usize>,
offset: Option<usize>,
}

pub async fn search(
Query(query): Query<SearchQuery>,
State(db_pool): State<DbPool>,
) -> impl IntoResponse {
let limit = query.limit.unwrap_or(10).min(100);
let page = query.page.unwrap_or(0);
let offset = page * limit;
let offset = query.offset.unwrap_or(0);
let mut conn = db_pool.read.get().await.unwrap();
let mut sql_query = plugins::table.inner_join(users::dsl::users).into_boxed();
let mut total: i64 = 0;
let mut had_query = false;
if let Some(q) = query.q.as_ref() {
if !q.is_empty() {
let q = format!("%{q}%");
Expand All @@ -74,6 +74,7 @@ pub async fn search(
.or(plugins::description.ilike(q));
sql_query = sql_query.filter(filter.clone());

had_query = true;
total = plugins::table
.filter(filter)
.count()
Expand All @@ -82,6 +83,9 @@ pub async fn search(
.unwrap();
}
}
if !had_query {
total = plugins::table.count().get_result(&mut conn).await.unwrap();
}

sql_query = sql_query.offset(offset as i64).limit(limit as i64);
match query.sort.as_deref() {
Expand Down Expand Up @@ -117,6 +121,7 @@ pub async fn search(
.filter_map(|(v, (p, u))| {
let version = v?.1;
Some(EncodePlugin {
id: p.id,
name: p.name,
author: u.gh_login,
version: version.num,
Expand All @@ -127,14 +132,15 @@ pub async fn search(
updated_at_ts: p.updated_at.timestamp(),
updated_at: p.updated_at.format("%Y-%m-%d %H:%M:%S").to_string(),
released_at: version.created_at.format("%Y-%m-%d %H:%M:%S").to_string(),
wasm: p.wasm,
})
})
.collect();

Json(PluginList {
total,
limit,
page,
offset,
plugins,
})
}
Expand Down Expand Up @@ -172,6 +178,7 @@ pub async fn meta(
};

Json(EncodePlugin {
id: plugin.id,
name,
author,
version: version.num,
Expand All @@ -182,6 +189,7 @@ pub async fn meta(
updated_at_ts: plugin.updated_at.timestamp(),
updated_at: plugin.updated_at.format("%Y-%m-%d %H:%M:%S").to_string(),
released_at: version.created_at.format("%Y-%m-%d %H:%M:%S").to_string(),
wasm: plugin.wasm,
})
}

Expand Down Expand Up @@ -378,6 +386,7 @@ pub async fn publish(

let s3_folder = format!("{}/{}/{}", user.gh_login, volt.name, volt.version);

let mut is_wasm = false;
if let Some(wasm) = volt.wasm.as_ref() {
let wasm_path = dir.path().join(wasm);
if !wasm_path.exists() {
Expand All @@ -389,6 +398,7 @@ pub async fn publish(
.await
.unwrap();
tokio::fs::copy(wasm_path, dest_wasm).await.unwrap();
is_wasm = true;
} else if let Some(themes) = volt.color_themes.as_ref() {
if themes.is_empty() {
return (StatusCode::BAD_REQUEST, "no color theme provided").into_response();
Expand Down Expand Up @@ -545,8 +555,14 @@ pub async fn publish(
.build_transaction()
.run(|conn| {
async move {
let new_plugin =
NewPlugin::new(&volt.name, user.id, &volt.display_name, &volt.description);
let new_plugin = NewPlugin::new(
&volt.name,
user.id,
&volt.display_name,
&volt.description,
volt.repository.as_deref(),
is_wasm,
);
let plugin = new_plugin.create_or_update(conn).await?;
let new_version = NewVersion::new(plugin.id, &volt.version);
new_version.create_or_update(conn).await?;
Expand Down
1 change: 1 addition & 0 deletions volts-core/src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub struct Plugin {
pub description: String,
pub downloads: i32,
pub repository: Option<String>,
pub wasm: bool,
}

#[derive(Queryable, Debug, Identifiable, Associations)]
Expand Down
1 change: 1 addition & 0 deletions volts-core/src/db/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ diesel::table! {
description -> Varchar,
downloads -> Int4,
repository -> Nullable<Varchar>,
wasm -> Bool,
}
}

Expand Down
4 changes: 3 additions & 1 deletion volts-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct NewTokenPayload {

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct EncodePlugin {
pub id: i32,
pub name: String,
pub author: String,
pub version: String,
Expand All @@ -47,12 +48,13 @@ pub struct EncodePlugin {
pub updated_at_ts: i64,
pub updated_at: String,
pub released_at: String,
pub wasm: bool,
}

#[derive(Serialize, Deserialize)]
pub struct PluginList {
pub total: i64,
pub limit: usize,
pub page: usize,
pub offset: usize,
pub plugins: Vec<EncodePlugin>,
}
1 change: 1 addition & 0 deletions volts-front/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
pulldown-cmark = "0.9.2"
url = "2.3.1"
gloo-net = "0.2.4"
web-sys = { version = "0.3.60", features = ["HtmlImageElement"] }
Expand Down
Loading

0 comments on commit 8bb0afa

Please sign in to comment.