From 2b1e125c79d11ce33b803a37caf74117b61e1e77 Mon Sep 17 00:00:00 2001 From: Asone Date: Mon, 16 Jan 2023 22:05:40 +0100 Subject: [PATCH 1/2] feat(rss): draft rss feature --- Cargo.lock | 129 ++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 5 +- src/routes/mod.rs | 1 + src/routes/rss.rs | 32 ++++++++++++ 5 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 src/routes/rss.rs diff --git a/Cargo.lock b/Cargo.lock index f44b9dd..ca8e222 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -108,6 +108,19 @@ dependencies = [ "syn", ] +[[package]] +name = "atom_syndication" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91a85f2ee28cbd1ecf91288460f6dc74661fd99b4e9a559836a667ccf63aa38c" +dependencies = [ + "chrono", + "derive_builder", + "diligent-date-parser", + "never", + "quick-xml", +] + [[package]] name = "atomic" version = "0.5.1" @@ -424,6 +437,72 @@ dependencies = [ "syn", ] +[[package]] +name = "darling" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0dd3cd20dc6b5a876612a6e5accfe7f3dd883db6d07acfbf14c128f61550dfa" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a784d2ccaf7c98501746bf0be29b2022ba41fd62a2e622af997a03e9f972859f" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7618812407e9402654622dd402b0a89dff9ba93badd6540781526117b92aab7e" +dependencies = [ + "darling_core", + "quote", + "syn", +] + +[[package]] +name = "derive_builder" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d67778784b508018359cbc8696edb3db78160bab2c2a28ba7f56ef6932997f8" +dependencies = [ + "derive_builder_macro", +] + +[[package]] +name = "derive_builder_core" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c11bdc11a0c47bc7d37d582b5285da6849c96681023680b906673c5707af7b0f" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "derive_builder_macro" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebcda35c7a396850a55ffeac740804b40ffec779b98fffbb1738f4033f0ee79e" +dependencies = [ + "derive_builder_core", + "syn", +] + [[package]] name = "derive_more" version = "0.99.17" @@ -540,6 +619,15 @@ dependencies = [ "subtle", ] +[[package]] +name = "diligent-date-parser" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6cf7fe294274a222363f84bcb63cdea762979a0443b4cf1f4f8fd17c86b1182" +dependencies = [ + "chrono", +] + [[package]] name = "dotenv" version = "0.15.0" @@ -944,6 +1032,12 @@ dependencies = [ "cxx-build", ] +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "idna" version = "0.2.3" @@ -1205,6 +1299,7 @@ dependencies = [ "rocket", "rocket-multipart-form-data", "rocket_sync_db_pools", + "rss", "serde", "serde_json", "tokio-util 0.7.2", @@ -1333,6 +1428,12 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a" +[[package]] +name = "never" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c96aba5aa877601bb3f6dd6a63a969e1f82e60646e81e71b14496995e9853c91" + [[package]] name = "num-bigint" version = "0.4.3" @@ -1669,6 +1770,16 @@ dependencies = [ "prost 0.8.0", ] +[[package]] +name = "quick-xml" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffc053f057dd768a56f62cd7e434c42c831d296968997e9ac1f76ea7c2d14c41" +dependencies = [ + "encoding_rs", + "memchr", +] + [[package]] name = "quickcheck" version = "1.0.3" @@ -1968,6 +2079,18 @@ dependencies = [ "quote", ] +[[package]] +name = "rss" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14816ec8b4e58b34a36adba66dcadd3e1e221dcb0fb2fd83e7c5129ea1a72458" +dependencies = [ + "atom_syndication", + "derive_builder", + "never", + "quick-xml", +] + [[package]] name = "rustc_version" version = "0.4.0" @@ -2235,6 +2358,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "subtle" version = "2.4.1" diff --git a/Cargo.toml b/Cargo.toml index 4b34633..360e59f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ juniper_rocket_multipart_handler = "0.1.0" infer = "0.8.1" base64 = "0.13.0" juniper_relay_connection = "0.1.1" +rss = "2.0" [dependencies.tokio-util] version = "0.7.1" diff --git a/src/main.rs b/src/main.rs index 712fefb..d8a9640 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,7 +35,7 @@ use dotenv::dotenv; use juniper::EmptySubscription; use rocket::Rocket; use rocket::{fairing::AdHoc, Route}; -use routes::{auth::login, file::get_file, utils::graphiql, utils::static_index}; +use routes::{auth::login, file::get_file, rss::get_rss, utils::graphiql, utils::static_index}; use std::env; use app::{ @@ -92,7 +92,8 @@ fn routes_builder() -> Vec { payable_post_graphql_handler, upload, login, - get_file + get_file, + get_rss ]; let enable_dev_tools = env::var("ENABLE_DEV_TOOLS").unwrap_or("false".to_string()); diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 2b3fa05..abe0aa4 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -1,3 +1,4 @@ pub mod auth; pub mod file; +pub mod rss; pub mod utils; diff --git a/src/routes/rss.rs b/src/routes/rss.rs new file mode 100644 index 0000000..345663f --- /dev/null +++ b/src/routes/rss.rs @@ -0,0 +1,32 @@ +use crate::db::{models::media::Media, PostgresConn}; +use rocket::http::ContentType; +use rocket::response::{content, Response}; +use rss::{Channel, ChannelBuilder, Item, ItemBuilder}; +use std::io::Cursor; + +/// A route to retrieve files behind the paywall. +#[rocket::get("/rss")] +pub async fn get_rss(db: PostgresConn) -> content::RawXml { + let results = db.run(move |c| Media::find_all_published(c)).await; + let items: Vec = results + .iter() + .map(|media| { + let mut item = Item::default(); + item.set_title(media.title.clone()); + item.set_description(media.description.clone()); + // .set_description(media.description) + // .link(item.link.as_str()) + item + }) + .collect(); + + let channel = ChannelBuilder::default() + .title("LNFileStore RSS Feed".to_string()) + .description("This RSS feed provides the list of available medias".to_string()) + // .link("https://example.com".to_string()) + .items(items) + .build() + .to_string(); + + content::RawXml(channel) +} From 019925c0d921b62d9dd7b4a75b953dacff603355 Mon Sep 17 00:00:00 2001 From: Asone Date: Tue, 17 Jan 2023 20:54:09 +0100 Subject: [PATCH 2/2] chore(multipart): remove old code for multipart exported to external crate --- .../src/graphql_upload_operations_request.rs | 77 ------------------- juniper_rocket_multipart_handler/src/lib.rs | 3 - 2 files changed, 80 deletions(-) delete mode 100644 juniper_rocket_multipart_handler/src/graphql_upload_operations_request.rs delete mode 100644 juniper_rocket_multipart_handler/src/lib.rs diff --git a/juniper_rocket_multipart_handler/src/graphql_upload_operations_request.rs b/juniper_rocket_multipart_handler/src/graphql_upload_operations_request.rs deleted file mode 100644 index 83de319..0000000 --- a/juniper_rocket_multipart_handler/src/graphql_upload_operations_request.rs +++ /dev/null @@ -1,77 +0,0 @@ -use juniper::{ - http::GraphQLBatchRequest, DefaultScalarValue, GraphQLSubscriptionType, RootNode, ScalarValue, -}; -use juniper::{GraphQLType, GraphQLTypeAsync}; -use juniper_rocket::GraphQLResponse; -use rocket::http::Status; - -/// A GraphQL operations request. -/// -/// This struct replicates the [`GraphQLRequest`](https://github.com/graphql-rust/juniper/blob/master/juniper_rocket/src/lib.rs#L64) original behavior. -/// It is provided and used with the upload wrapper as the original struct -/// does not provide any constructor and the tuple constructor is private. -#[derive(Debug, PartialEq)] -pub struct GraphQLOperationsRequest(pub GraphQLBatchRequest) -where - S: ScalarValue; - -impl GraphQLOperationsRequest -where - S: ScalarValue, -{ - /// Asynchronously execute an incoming GraphQL query. - pub async fn execute( - &self, - root_node: &RootNode<'_, QueryT, MutationT, SubscriptionT, S>, - context: &CtxT, - ) -> GraphQLResponse - where - QueryT: GraphQLTypeAsync, - QueryT::TypeInfo: Sync, - MutationT: GraphQLTypeAsync, - MutationT::TypeInfo: Sync, - SubscriptionT: GraphQLSubscriptionType, - SubscriptionT::TypeInfo: Sync, - CtxT: Sync, - S: Send + Sync, - { - let response = self.0.execute(root_node, context).await; - let status = if response.is_ok() { - Status::Ok - } else { - Status::BadRequest - }; - let json = serde_json::to_string(&response).unwrap(); - - GraphQLResponse(status, json) - } - - /// Synchronously execute an incoming GraphQL query. - pub fn execute_sync( - &self, - root_node: &RootNode, - context: &CtxT, - ) -> GraphQLResponse - where - QueryT: GraphQLType, - MutationT: GraphQLType, - SubscriptionT: GraphQLType, - { - let response = self.0.execute_sync(root_node, context); - let status = if response.is_ok() { - Status::Ok - } else { - Status::BadRequest - }; - let json = serde_json::to_string(&response).unwrap(); - - GraphQLResponse(status, json) - } - - /// Returns the operation names associated with this request. - /// - /// For batch requests there will be multiple names. - pub fn operation_names(&self) -> Vec> { - self.0.operation_names() - } -} diff --git a/juniper_rocket_multipart_handler/src/lib.rs b/juniper_rocket_multipart_handler/src/lib.rs deleted file mode 100644 index e0ad8a4..0000000 --- a/juniper_rocket_multipart_handler/src/lib.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod graphql_upload_operations_request; -pub mod graphql_upload_wrapper; -pub mod temp_file;