-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Report btn #241
Report btn #241
Changes from 7 commits
10c3156
a28ac46
1bd7fcc
13f6239
19e3fed
048cfbe
3bef0b4
d1537ab
375b8ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use leptos::*; | ||
|
||
#[component] | ||
pub fn SelectOption(is: &'static str, value: ReadSignal<String>) -> impl IntoView { | ||
view! { | ||
<option | ||
value=is | ||
selected=move || value() == is | ||
> | ||
{is} | ||
</option> | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,14 +8,16 @@ use axum::{ | |
}; | ||
use axum::{routing::get, Router}; | ||
use axum_extra::extract::cookie::Key; | ||
use hot_or_not_web_leptos_ssr::state::canisters::Canisters; | ||
use hot_or_not_web_leptos_ssr::{app::App, state::server::AppState}; | ||
use hot_or_not_web_leptos_ssr::{ | ||
auth::server_impl::store::KVStoreImpl, fallback::file_and_error_handler, | ||
}; | ||
use hot_or_not_web_leptos_ssr::{consts::OFF_CHAIN_AGENT_GRPC_URL, state::canisters::Canisters}; | ||
use leptos::{get_configuration, logging::log, provide_context}; | ||
use leptos_axum::handle_server_fns_with_context; | ||
use leptos_axum::{generate_route_list, LeptosRoutes}; | ||
#[cfg(feature = "ssr")] | ||
use tonic::transport::Channel; | ||
|
||
pub async fn server_fn_handler( | ||
State(app_state): State<AppState>, | ||
|
@@ -35,6 +37,8 @@ pub async fn server_fn_handler( | |
provide_context(app_state.cookie_key.clone()); | ||
#[cfg(feature = "oauth-ssr")] | ||
provide_context(app_state.google_oauth.clone()); | ||
#[cfg(feature = "ssr")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be behind |
||
provide_context(app_state.grpc_offchain_channel.clone()); | ||
}, | ||
request, | ||
) | ||
|
@@ -58,6 +62,8 @@ pub async fn leptos_routes_handler( | |
provide_context(app_state.cookie_key.clone()); | ||
#[cfg(feature = "oauth-ssr")] | ||
provide_context(app_state.google_oauth.clone()); | ||
#[cfg(feature = "ssr")] | ||
provide_context(app_state.grpc_offchain_channel.clone()); | ||
}, | ||
App, | ||
); | ||
|
@@ -136,6 +142,15 @@ fn init_google_oauth() -> openidconnect::core::CoreClient { | |
.set_redirect_uri(RedirectUrl::new(redirect_uri).unwrap()) | ||
} | ||
|
||
#[cfg(feature = "ssr")] | ||
async fn init_grpc_offchain_channel() -> Channel { | ||
let off_chain_agent_url = OFF_CHAIN_AGENT_GRPC_URL.as_ref(); | ||
Channel::from_static(off_chain_agent_url) | ||
.connect() | ||
.await | ||
.expect("Couldn't connect to off-chain agent") | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
simple_logger::init_with_level(log::Level::Debug).expect("couldn't initialize logging"); | ||
|
@@ -163,6 +178,8 @@ async fn main() { | |
cookie_key: init_cookie_key(), | ||
#[cfg(feature = "oauth-ssr")] | ||
google_oauth: init_google_oauth(), | ||
#[cfg(feature = "ssr")] | ||
grpc_offchain_channel: init_grpc_offchain_channel().await, | ||
}; | ||
|
||
// build our application with a route | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ pub mod server { | |
use axum_extra::extract::cookie::Key; | ||
use leptos::LeptosOptions; | ||
use leptos_router::RouteListing; | ||
use tonic::transport::Channel; | ||
|
||
#[derive(FromRef, Clone)] | ||
pub struct AppState { | ||
|
@@ -28,5 +29,6 @@ pub mod server { | |
pub cookie_key: Key, | ||
#[cfg(feature = "oauth-ssr")] | ||
pub google_oauth: openidconnect::core::CoreClient, | ||
pub grpc_offchain_channel: Channel, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::env; | ||
|
||
use leptos::{expect_context, server, ServerFnError}; | ||
|
||
pub enum ReportOption { | ||
Nudity, | ||
Violence, | ||
Offensive, | ||
Spam, | ||
Other, | ||
} | ||
|
||
impl ReportOption { | ||
pub fn as_str(&self) -> &'static str { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: impl Display makes more sense There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. facing issue when I am using it here with impl Display.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. works |
||
match self { | ||
ReportOption::Nudity => "Nudity/Porn", | ||
ReportOption::Violence => "Violence/Gore", | ||
ReportOption::Offensive => "Offensive", | ||
ReportOption::Spam => "Spam/Ad", | ||
ReportOption::Other => "Others", | ||
} | ||
} | ||
} | ||
|
||
#[server] | ||
pub async fn send_report_offchain( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also add a mock (or make reporting no-op locally) |
||
reporter_id: String, | ||
publisher_id: String, | ||
publisher_canister_id: String, | ||
post_id: String, | ||
video_id: String, | ||
reason: String, | ||
video_url: String, | ||
) -> Result<(), ServerFnError> { | ||
use crate::utils::off_chain; | ||
use tonic::metadata::MetadataValue; | ||
use tonic::transport::Channel; | ||
use tonic::Request; | ||
|
||
let channel: Channel = expect_context(); | ||
|
||
let mut off_chain_agent_grpc_auth_token = env::var("GRPC_AUTH_TOKEN").expect("GRPC_AUTH_TOKEN"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we migrate off chain client to AppState instead of constructing every time? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
off_chain_agent_grpc_auth_token.retain(|c| !c.is_whitespace()); | ||
|
||
let token: MetadataValue<_> = format!("Bearer {}", off_chain_agent_grpc_auth_token).parse()?; | ||
|
||
let mut client = off_chain::off_chain_client::OffChainClient::with_interceptor( | ||
channel, | ||
move |mut req: Request<()>| { | ||
req.metadata_mut().insert("authorization", token.clone()); | ||
Ok(req) | ||
}, | ||
); | ||
|
||
let request = tonic::Request::new(off_chain::ReportPostRequest { | ||
reporter_id, | ||
publisher_id, | ||
publisher_canister_id, | ||
post_id, | ||
video_id, | ||
reason, | ||
video_url, | ||
}); | ||
|
||
client.report_post(request).await?; | ||
|
||
Ok(()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I usually put these import inside the scope or use absolute imports so we don't add an extra
#[cfg]
blockSee https://github.com/go-bazzinga/hot-or-not-web-leptos-ssr/blob/main/src/main.rs#L113