-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0eb5d31
commit ba0ac20
Showing
17 changed files
with
326 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// NOTE: The functions in this module ignore `Err` values because failing to log a visit is ok | ||
use actix_web::{web, HttpRequest, Result}; | ||
use awc::Client; | ||
use chrono::prelude::*; | ||
use diesel::RunQueryDsl; | ||
use model::{ | ||
schema::users, | ||
user::{Device, Location, NewUser, User}, | ||
}; | ||
use std::sync::Arc; | ||
|
||
use crate::{DbConn, DbPool}; | ||
|
||
async fn get_location(ip: &str) -> Result<Location, ()> { | ||
actix_web::rt::System::new().block_on(async { | ||
let client = Client::default(); | ||
client | ||
.get(format!("http://ip-api.com/json/{ip}")) | ||
.send() | ||
.await | ||
.map_err(|_| ())? | ||
.json::<Location>() | ||
.await | ||
.map_err(|_| ()) | ||
}) | ||
} | ||
|
||
async fn get_user(req: HttpRequest) -> Result<User, String> { | ||
let time = Utc::now().naive_utc(); | ||
let ip = req.peer_addr().map(|addr| addr.ip().to_string()); | ||
|
||
let location = if let Some(ref ip) = ip { | ||
get_location(ip).await.unwrap_or_default() | ||
} else { | ||
Location::default() | ||
}; | ||
|
||
let ip = ip.unwrap_or_default(); | ||
|
||
let user_agent = req | ||
.headers() | ||
.get("User-Agent") | ||
.and_then(|field| field.to_str().ok()) | ||
.map(|s| s.to_owned()); | ||
|
||
let device = user_agent.as_ref().map(Device::from).unwrap_or_default(); | ||
|
||
let Location { | ||
country, | ||
state, | ||
city, | ||
} = location; | ||
|
||
Ok(User { | ||
device, | ||
ip, | ||
user_agent, | ||
time, | ||
country, | ||
state, | ||
city, | ||
}) | ||
} | ||
|
||
async fn insert_user(user: User, mut conn: DbConn) { | ||
let _ = web::block(move || { | ||
diesel::insert_into(users::table) | ||
.values(NewUser::from(&user)) | ||
.execute(&mut conn) | ||
}) | ||
.await; | ||
} | ||
|
||
pub async fn log_user(req: HttpRequest, conn: DbConn) { | ||
if let Ok(user) = get_user(req).await { | ||
insert_user(user, conn).await | ||
} // Otherwise do nothing! missing a visit is fine :) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod metrics; | ||
pub mod order; | ||
pub mod stock; | ||
pub mod stripe; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ pub async fn send_confirmation(user: UserData, mailer: Arc<Mailer>) -> Result<() | |
|
||
let email = Message::builder() | ||
.from("Kiggyshop <[email protected]>".parse().unwrap()) | ||
.to("Kiggy <kristencrankin@gmail.com>".parse().unwrap()) | ||
.to("Fay <faycarsons23@gmail.com>".parse().unwrap()) | ||
.subject("Thank you for your order!") | ||
.multipart( | ||
MultiPart::alternative() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.