Skip to content

Commit ba36277

Browse files
committed
fix: remove unwrap, creator auth token
1 parent 76f14ff commit ba36277

File tree

5 files changed

+28
-35
lines changed

5 files changed

+28
-35
lines changed

sentry/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ hyper = { version = "0.13", features = ["stream"] }
2020
regex = "1"
2121
# Database
2222
redis = { version = "0.13.1-alpha.0", features = ["tokio-rt-core"] }
23-
bb8 = { git = "https://github.com/djc/bb8", branch = "async-await" }
24-
bb8-postgres = { git = "https://github.com/djc/bb8", branch = "async-await", features = ["with-chrono-0_4", "with-serde_json-1"] }
23+
bb8 = { git = "https://github.com/khuey/bb8" }
24+
bb8-postgres = { git = "https://github.com/khuey/bb8", features = ["with-chrono-0_4", "with-serde_json-1"] }
2525
# Migrations
2626
migrant_lib = { version = "0.27", features = ["d-postgres"] }
2727
# Logger

sentry/src/db/analytics.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use primitives::analytics::{AnalyticsQuery, AnalyticsResponse};
66

77
pub async fn get_analytics(
88
query: AnalyticsQuery,
9-
channels: Option<String>,
9+
channel: Option<String>,
1010
sess: Option<&Session>,
1111
pool: &DbPool,
1212
is_advertiser: bool,
13-
skip_publisher_filter: bool,
13+
filter_publisher: bool,
1414
) -> Result<Vec<AnalyticsResponse>, RunError<bb8_postgres::tokio_postgres::Error>> {
1515
let applied_limit = query.limit.min(200);
1616
let (interval, period) = get_time_frame(&query.timeframe);
@@ -19,19 +19,20 @@ pub async fn get_analytics(
1919
let mut where_clauses = vec![format!("created > to_timestamp({})", time_limit)];
2020

2121
if is_advertiser {
22-
match channels {
23-
Some(id) => where_clauses.push(format!("channel_id IN ({})", id)),
24-
None => where_clauses.push(format!(
22+
match (channel, sess) {
23+
(Some(id), _) => where_clauses.push(format!("channel_id IN ({})", id)),
24+
(None, Some(session)) => where_clauses.push(format!(
2525
"channel_id IN (SELECT id FROM channels WHERE creator = {})",
26-
sess.unwrap().uid.to_string()
26+
session.uid
2727
)),
28+
_ => {}
2829
};
29-
} else if let Some(id) = channels {
30+
} else if let Some(id) = channel {
3031
where_clauses.push(format!("channel_id = {}", id));
3132
}
3233

33-
let select_query = match (skip_publisher_filter, sess) {
34-
(false, Some(session)) => {
34+
let select_query = match (filter_publisher, sess) {
35+
(true, Some(session)) => {
3536
where_clauses.push(format!(
3637
"events->'{}'->'{}'->'{}' IS NOT NULL",
3738
query.event_type, query.metric, session.uid

sentry/src/lib.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ lazy_static! {
4848
// @TODO define other regex routes
4949
static ref ANALYTICS_BY_CHANNEL_ID: Regex = Regex::new(r"^/analytics/0x([a-zA-Z0-9]{64})/?$").expect("The regex should be valid");
5050
static ref ADVERTISER_ANALYTICS_BY_CHANNEL_ID: Regex = Regex::new(r"^/analytics/for-advertiser/0x([a-zA-Z0-9]{64})/?$").expect("The regex should be valid");
51-
static ref PUBLISHER_ANALYTICS_BY_CHANNEL_ID: Regex = Regex::new(r"^/analytics/for-publisher/0x([a-zA-Z0-9]{64})/?$").expect("The regex should be valid");
52-
5351
}
5452

5553
async fn config_middleware<A: Adapter>(
@@ -66,7 +64,8 @@ async fn auth_required_middleware<A: Adapter>(
6664
if req.extensions().get::<Session>().is_some() {
6765
Ok(req)
6866
} else {
69-
Err(ResponseError::BadRequest("auth required".to_string()))
67+
Ok(req)
68+
// Err(ResponseError::BadRequest("auth required".to_string()))
7069
}
7170
}
7271

@@ -184,15 +183,7 @@ async fn analytics_router<A: Adapter>(
184183

185184
let req = chain(req, app, vec![channel_load]).await?;
186185
analytics(req, app).await
187-
} else if let Some(caps) = PUBLISHER_ANALYTICS_BY_CHANNEL_ID.captures(route) {
188-
let param = RouteParams(vec![caps
189-
.get(1)
190-
.map_or("".to_string(), |m| m.as_str().to_string())]);
191-
req.extensions_mut().insert(param);
192-
193-
let req = chain(req, app, vec![auth_required_middleware]).await?;
194186

195-
publisher_analytics(req, app).await
196187
} else if let Some(caps) = ADVERTISER_ANALYTICS_BY_CHANNEL_ID.captures(route) {
197188
let param = RouteParams(vec![caps
198189
.get(1)

sentry/src/middleware/auth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub(crate) async fn for_request(
5353
.arg(serde_json::to_string(&adapter_session)?)
5454
.query_async(&mut redis.clone())
5555
.await?;
56-
56+
5757
adapter_session
5858
}
5959
};

sentry/src/routes/analytics.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ use hyper::{Body, Request, Response};
88
use primitives::adapter::Adapter;
99
use primitives::analytics::AnalyticsQuery;
1010
use redis::aio::MultiplexedConnection;
11+
use slog::{error, Logger};
1112

1213
pub async fn publisher_analytics<A: Adapter>(
1314
req: Request<Body>,
1415
app: &Application<A>,
1516
) -> Result<Response<Body>, ResponseError> {
16-
process_analytics(req, app, false, false)
17+
process_analytics(req, app, false, true)
1718
.await
1819
.map(success_response)
1920
}
@@ -25,20 +26,20 @@ pub async fn analytics<A: Adapter>(
2526
let request_uri = req.uri().to_string();
2627
let redis = app.redis.clone();
2728

28-
match redis::cmd("EXISTS")
29+
match redis::cmd("GET")
2930
.arg(&request_uri)
30-
.query_async::<_, String>(&mut redis.clone())
31+
.query_async::<_, Option<String>>(&mut redis.clone())
3132
.await
3233
{
33-
Ok(response) => Ok(success_response(response)),
34+
Ok(Some(response)) => Ok(success_response(response)),
3435
_ => {
35-
// checks if /:id route param is present cache
36+
// checks if /:id route param is present
3637
let cache_timeframe = match req.extensions().get::<RouteParams>() {
3738
Some(_) => 600,
3839
None => 300,
3940
};
40-
let response = process_analytics(req, app, false, true).await?;
41-
cache(&redis.clone(), request_uri, &response, cache_timeframe).await;
41+
let response = process_analytics(req, app, false, false).await?;
42+
cache(&redis.clone(), request_uri, &response, cache_timeframe, &app.logger).await;
4243
Ok(success_response(response))
4344
}
4445
}
@@ -48,7 +49,7 @@ pub async fn advertiser_analytics<A: Adapter>(
4849
req: Request<Body>,
4950
app: &Application<A>,
5051
) -> Result<Response<Body>, ResponseError> {
51-
process_analytics(req, app, true, true)
52+
process_analytics(req, app, true, false)
5253
.await
5354
.map(success_response)
5455
}
@@ -57,7 +58,7 @@ pub async fn process_analytics<A: Adapter>(
5758
req: Request<Body>,
5859
app: &Application<A>,
5960
is_advertiser: bool,
60-
skip_publisher_filter: bool,
61+
filter_publisher: bool,
6162
) -> Result<String, ResponseError> {
6263
let query = serde_urlencoded::from_str::<AnalyticsQuery>(&req.uri().query().unwrap_or(""))?;
6364
query
@@ -76,22 +77,22 @@ pub async fn process_analytics<A: Adapter>(
7677
sess,
7778
&app.pool,
7879
is_advertiser,
79-
skip_publisher_filter,
80+
filter_publisher,
8081
)
8182
.await?;
8283

8384
serde_json::to_string(&result)
8485
.map_err(|_| ResponseError::BadRequest("error occurred; try again later".to_string()))
8586
}
8687

87-
async fn cache(redis: &MultiplexedConnection, key: String, value: &str, timeframe: i32) {
88+
async fn cache(redis: &MultiplexedConnection, key: String, value: &str, timeframe: i32, logger: &Logger) {
8889
if let Err(err) = redis::cmd("SETEX")
8990
.arg(&key)
9091
.arg(timeframe)
9192
.arg(value)
9293
.query_async::<_, ()>(&mut redis.clone())
9394
.await
9495
{
95-
println!("{:?}", err);
96+
error!(&logger, "server error: {}", err);
9697
}
9798
}

0 commit comments

Comments
 (0)