From a63f65ae73330b4488c47bde63ba994cc3185b50 Mon Sep 17 00:00:00 2001 From: calyptobai Date: Mon, 11 Sep 2023 17:43:09 -0400 Subject: [PATCH] Store `user_id` on all templates Note: this will delete *all* existing templates. --- .../20230911161509_template_user_id.sql | 2 + server/bleep/sqlx-data.json | 226 +++++++++--------- server/bleep/src/webserver/template.rs | 84 +++++-- 3 files changed, 180 insertions(+), 132 deletions(-) create mode 100644 server/bleep/migrations/20230911161509_template_user_id.sql diff --git a/server/bleep/migrations/20230911161509_template_user_id.sql b/server/bleep/migrations/20230911161509_template_user_id.sql new file mode 100644 index 0000000000..35bd2f6b61 --- /dev/null +++ b/server/bleep/migrations/20230911161509_template_user_id.sql @@ -0,0 +1,2 @@ +DELETE FROM templates; +ALTER TABLE templates ADD COLUMN user_id TEXT NOT NULL; diff --git a/server/bleep/sqlx-data.json b/server/bleep/sqlx-data.json index 3ed40d525b..14b5407c1a 100644 --- a/server/bleep/sqlx-data.json +++ b/server/bleep/sqlx-data.json @@ -46,42 +46,6 @@ }, "query": "INSERT INTO studio_snapshots (studio_id, context, messages)\n VALUES (?, ?, ?)" }, - "109cbc6bd18131c59718a575c5e09b78a4d8f28667d347ced1a13f1b4be7e2eb": { - "describe": { - "columns": [ - { - "name": "id", - "ordinal": 0, - "type_info": "Int64" - }, - { - "name": "name", - "ordinal": 1, - "type_info": "Text" - }, - { - "name": "modified_at", - "ordinal": 2, - "type_info": "Datetime" - }, - { - "name": "content", - "ordinal": 3, - "type_info": "Text" - } - ], - "nullable": [ - false, - false, - false, - false - ], - "parameters": { - "Right": 1 - } - }, - "query": "SELECT id, name, modified_at, content FROM templates WHERE id = ?" - }, "11f5e7122d047f87c398cf56470c284e2037203bc4d1506efc85e7431e2e2f5f": { "describe": { "columns": [ @@ -110,24 +74,6 @@ }, "query": "INSERT INTO conversations (user_id, thread_id, repo_ref, title, exchanges, created_at) VALUES (?, ?, ?, ?, ?, strftime('%s', 'now'))" }, - "1650f7ee180dfc3169cd92ec20483663e118831b48274fbea6ca819808bcd9d1": { - "describe": { - "columns": [ - { - "name": "id", - "ordinal": 0, - "type_info": "Int64" - } - ], - "nullable": [ - false - ], - "parameters": { - "Right": 1 - } - }, - "query": "SELECT id FROM templates WHERE id = ?" - }, "359b4d0fa1fcb081767303103b23f0650568cf4e79787c7ddcd21af5bad6761b": { "describe": { "columns": [], @@ -166,6 +112,42 @@ }, "query": "SELECT ss.id\n FROM studio_snapshots ss\n JOIN studios s ON s.id = ss.studio_id AND s.user_id = ?\n WHERE ss.studio_id = ?\n ORDER BY ss.modified_at DESC\n LIMIT 1" }, + "449c90c85dc8afd68e50448549d0a4f3a5cf591c8330e0b9c69c7a2dde8cd97d": { + "describe": { + "columns": [ + { + "name": "id", + "ordinal": 0, + "type_info": "Int64" + }, + { + "name": "name", + "ordinal": 1, + "type_info": "Text" + }, + { + "name": "modified_at", + "ordinal": 2, + "type_info": "Datetime" + }, + { + "name": "content", + "ordinal": 3, + "type_info": "Text" + } + ], + "nullable": [ + false, + false, + false, + false + ], + "parameters": { + "Right": 1 + } + }, + "query": "SELECT id, name, modified_at, content FROM templates WHERE user_id = ?" + }, "476c0b82963b9a2333edec797133770f32c8269a21a17d3165e7785f69e886ab": { "describe": { "columns": [ @@ -266,6 +248,16 @@ }, "query": "DELETE FROM chunk_cache WHERE chunk_hash = ? AND file_hash = ?" }, + "523e2fd0c4f2c3318e894af3537b1c2e503e0865fcaabc4bdda43960ca0ef45c": { + "describe": { + "columns": [], + "nullable": [], + "parameters": { + "Right": 3 + } + }, + "query": "INSERT INTO templates (name, content, user_id) VALUES (?, ?, ?)" + }, "5776008bf71ba2a90bad43c66a6e622ad71a81e1751c00b62aafa70840997999": { "describe": { "columns": [], @@ -276,51 +268,33 @@ }, "query": "UPDATE templates SET content = ? WHERE id = ?" }, - "5840a627d06f7e3fe4681954fd6d36c4b33924f97803a988c2b3a9e31dfee402": { + "5bdd10bd3029a70911749c200c1680224e2fb9f4ce1bf463a6ebfcdf3de10ad6": { + "describe": { + "columns": [], + "nullable": [], + "parameters": { + "Right": 1 + } + }, + "query": "UPDATE templates SET modified_at = datetime('now') WHERE id = ?" + }, + "623d5ba93bde27df707f2b7807cca5d853e2a9f5cd8c9b8ddc681776af50fdcc": { "describe": { "columns": [ { "name": "id", "ordinal": 0, "type_info": "Int64" - }, - { - "name": "name", - "ordinal": 1, - "type_info": "Text" - }, - { - "name": "modified_at", - "ordinal": 2, - "type_info": "Datetime" - }, - { - "name": "content", - "ordinal": 3, - "type_info": "Text" } ], "nullable": [ - false, - false, - false, false ], "parameters": { - "Right": 0 - } - }, - "query": "SELECT id, name, modified_at, content FROM templates" - }, - "5bdd10bd3029a70911749c200c1680224e2fb9f4ce1bf463a6ebfcdf3de10ad6": { - "describe": { - "columns": [], - "nullable": [], - "parameters": { - "Right": 1 + "Right": 2 } }, - "query": "UPDATE templates SET modified_at = datetime('now') WHERE id = ?" + "query": "SELECT id FROM templates WHERE id = ? AND user_id = ?" }, "63938812eb2374512bb6dd750e14f363f67471ca85c2ed27f7c4b9404f5a875f": { "describe": { @@ -392,16 +366,6 @@ }, "query": "INSERT INTO studios(name, user_id) VALUES (?, ?) RETURNING id" }, - "69e7fcbe274e645ac8dece40ddad39b5f594731e4647a683f4537ede7e20b3d4": { - "describe": { - "columns": [], - "nullable": [], - "parameters": { - "Right": 2 - } - }, - "query": "INSERT INTO templates (name, content) VALUES (?, ?)" - }, "6e3bfe277ca4506bc389597db418b311c4faabf5af132f81699997e4d766e40d": { "describe": { "columns": [ @@ -432,6 +396,24 @@ }, "query": "SELECT title, repo_ref, exchanges\n FROM conversations\n WHERE user_id = ? AND thread_id = ?" }, + "76464f75732fee5c742a23d7a0b95de1def360bae7943a2389944b0177106033": { + "describe": { + "columns": [ + { + "name": "id", + "ordinal": 0, + "type_info": "Int64" + } + ], + "nullable": [ + true + ], + "parameters": { + "Right": 2 + } + }, + "query": "DELETE FROM templates WHERE id = ? AND user_id = ? RETURNING id" + }, "7ca39c2d8aebd7ebe1dbda00b4fb5fce6f4ea17894ce64c3d9786585c61739f0": { "describe": { "columns": [ @@ -488,24 +470,6 @@ }, "query": "UPDATE chunk_cache SET branches = ? WHERE chunk_hash = ?" }, - "9abe5ab6b9b3ccbcea0eab3750f3a7cfa00e9fe3eaf77db18c639fec8061800f": { - "describe": { - "columns": [ - { - "name": "id", - "ordinal": 0, - "type_info": "Int64" - } - ], - "nullable": [ - false - ], - "parameters": { - "Right": 1 - } - }, - "query": "DELETE FROM templates WHERE id = ? RETURNING id" - }, "9f862a56e79cc9ae6e9b896064a0057335b40225be0a8c8d29d9227de12ae364": { "describe": { "columns": [], @@ -764,6 +728,42 @@ }, "query": "SELECT repo_ref, exchanges FROM conversations WHERE user_id = ? AND thread_id = ?" }, + "e807398025b2b80405d3653e9ffc339d29d69d40dc2a57c4f1824822ab9da298": { + "describe": { + "columns": [ + { + "name": "id", + "ordinal": 0, + "type_info": "Int64" + }, + { + "name": "name", + "ordinal": 1, + "type_info": "Text" + }, + { + "name": "modified_at", + "ordinal": 2, + "type_info": "Datetime" + }, + { + "name": "content", + "ordinal": 3, + "type_info": "Text" + } + ], + "nullable": [ + false, + false, + false, + false + ], + "parameters": { + "Right": 2 + } + }, + "query": "SELECT id, name, modified_at, content FROM templates WHERE id = ? AND user_id = ?" + }, "ec193a038eb7fc3aaca3c3adebcc4dbde01b47ae34ac2df2227c5e5459617182": { "describe": { "columns": [], diff --git a/server/bleep/src/webserver/template.rs b/server/bleep/src/webserver/template.rs index c85b288538..c01a501efb 100644 --- a/server/bleep/src/webserver/template.rs +++ b/server/bleep/src/webserver/template.rs @@ -1,4 +1,4 @@ -use super::{Error, ErrorKind}; +use super::{middleware::User, Error, ErrorKind}; use crate::{webserver, Application}; use axum::extract::{Extension, Json, Path}; use chrono::NaiveDateTime; @@ -12,12 +12,19 @@ pub struct Create { pub async fn create( app: Extension, + user: Extension, params: Json, ) -> webserver::Result { + let user_id = user + .login() + .ok_or_else(|| super::Error::user("didn't have user ID"))? + .to_string(); + let id = sqlx::query!( - "INSERT INTO templates (name, content) VALUES (?, ?)", + "INSERT INTO templates (name, content, user_id) VALUES (?, ?, ?)", params.name, - params.content + params.content, + user_id, ) .execute(&*app.sql) .await @@ -35,10 +42,19 @@ pub struct Template { content: String, } -pub async fn list(app: Extension) -> webserver::Result>> { +pub async fn list( + app: Extension, + user: Extension, +) -> webserver::Result>> { + let user_id = user + .login() + .ok_or_else(|| super::Error::user("didn't have user ID"))? + .to_string(); + let templates = sqlx::query_as!( Template, - "SELECT id, name, modified_at, content FROM templates" + "SELECT id, name, modified_at, content FROM templates WHERE user_id = ?", + user_id, ) .fetch_all(&*app.sql) .await @@ -49,12 +65,19 @@ pub async fn list(app: Extension) -> webserver::Result, + user: Extension, Path(id): Path, ) -> webserver::Result> { + let user_id = user + .login() + .ok_or_else(|| super::Error::user("didn't have user ID"))? + .to_string(); + let template = sqlx::query_as!( Template, - "SELECT id, name, modified_at, content FROM templates WHERE id = ?", - id + "SELECT id, name, modified_at, content FROM templates WHERE id = ? AND user_id = ?", + id, + user_id, ) .fetch_optional(&*app.sql) .await @@ -72,17 +95,27 @@ pub struct Patch { pub async fn patch( app: Extension, + user: Extension, Path(id): Path, Json(patch): Json, ) -> webserver::Result<()> { + let user_id = user + .login() + .ok_or_else(|| super::Error::user("didn't have user ID"))? + .to_string(); + let mut transaction = app.sql.begin().await.map_err(Error::internal)?; // Ensure the ID is valid first. - sqlx::query!("SELECT id FROM templates WHERE id = ?", id) - .fetch_optional(&mut transaction) - .await - .map_err(Error::internal)? - .ok_or_else(|| Error::new(ErrorKind::NotFound, "unknown template ID"))?; + sqlx::query!( + "SELECT id FROM templates WHERE id = ? AND user_id = ?", + id, + user_id + ) + .fetch_optional(&mut transaction) + .await + .map_err(Error::internal)? + .ok_or_else(|| Error::new(ErrorKind::NotFound, "unknown template ID"))?; if let Some(name) = patch.name { sqlx::query!("UPDATE templates SET name = ? WHERE id = ?", name, id) @@ -111,11 +144,24 @@ pub async fn patch( Ok(()) } -pub async fn delete(app: Extension, Path(id): Path) -> webserver::Result<()> { - sqlx::query!("DELETE FROM templates WHERE id = ? RETURNING id", id) - .fetch_optional(&*app.sql) - .await - .map_err(Error::internal)? - .ok_or_else(|| Error::new(ErrorKind::NotFound, "unknown template ID")) - .map(|_| ()) +pub async fn delete( + app: Extension, + user: Extension, + Path(id): Path, +) -> webserver::Result<()> { + let user_id = user + .login() + .ok_or_else(|| super::Error::user("didn't have user ID"))? + .to_string(); + + sqlx::query!( + "DELETE FROM templates WHERE id = ? AND user_id = ? RETURNING id", + id, + user_id + ) + .fetch_optional(&*app.sql) + .await + .map_err(Error::internal)? + .ok_or_else(|| Error::new(ErrorKind::NotFound, "unknown template ID")) + .map(|_| ()) }