Add Gravatar support.

This commit is contained in:
Paul Makles
2021-02-02 19:09:13 +00:00
parent ea276c3517
commit 38bedbaaea
6 changed files with 94 additions and 28 deletions

16
Cargo.lock generated
View File

@@ -1409,6 +1409,12 @@ dependencies = [
"opaque-debug 0.2.3", "opaque-debug 0.2.3",
] ]
[[package]]
name = "md5"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.3.4" version = "2.3.4"
@@ -2365,7 +2371,7 @@ dependencies = [
[[package]] [[package]]
name = "revolt" name = "revolt"
version = "0.3.3" version = "0.3.3-alpha.0"
dependencies = [ dependencies = [
"async-std", "async-std",
"async-tungstenite", "async-tungstenite",
@@ -2381,6 +2387,7 @@ dependencies = [
"lettre", "lettre",
"log", "log",
"many-to-many", "many-to-many",
"md5",
"mongodb", "mongodb",
"num_enum", "num_enum",
"once_cell", "once_cell",
@@ -2397,6 +2404,7 @@ dependencies = [
"snafu", "snafu",
"time 0.2.25", "time 0.2.25",
"ulid", "ulid",
"urlencoding",
"validator", "validator",
] ]
@@ -3398,6 +3406,12 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "urlencoding"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9232eb53352b4442e40d7900465dfc534e8cb2dc8f18656fcb2ac16112b5593"
[[package]] [[package]]
name = "utf-8" name = "utf-8"
version = "0.7.5" version = "0.7.5"

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "revolt" name = "revolt"
version = "0.3.3" version = "0.3.3-alpha.0"
authors = ["Paul Makles <paulmakles@gmail.com>"] authors = ["Paul Makles <paulmakles@gmail.com>"]
edition = "2018" edition = "2018"
@@ -33,11 +33,13 @@ serde_json = "1.0.57"
bitfield = "0.13.2" bitfield = "0.13.2"
reqwest = { version = "0.10.8", features = ["json"] } reqwest = { version = "0.10.8", features = ["json"] }
urlencoding = "1.1.1"
lazy_static = "1.4.0" lazy_static = "1.4.0"
num_enum = "0.5.1" num_enum = "0.5.1"
chrono = "0.4.15" chrono = "0.4.15"
time = "0.2.16" time = "0.2.16"
rand = "0.7.3" rand = "0.7.3"
md5 = "0.7.0"
regex = "1" regex = "1"
lettre = "0.10.0-alpha.1" lettre = "0.10.0-alpha.1"

View File

@@ -8,7 +8,7 @@ use rocket_contrib::json::JsonValue;
#[get("/")] #[get("/")]
pub async fn root() -> JsonValue { pub async fn root() -> JsonValue {
json!({ json!({
"revolt": "0.3.2", "revolt": "0.3.3-alpha.0",
"features": { "features": {
"registration": !*DISABLE_REGISTRATION, "registration": !*DISABLE_REGISTRATION,
"captcha": { "captcha": {

View File

@@ -1,29 +1,48 @@
use rocket::response::NamedFile; use mongodb::options::FindOneOptions;
use std::path::Path; use rocket::response::Redirect;
use mongodb::bson::doc;
use urlencoding;
use md5;
use crate::database::Ref; use crate::util::result::{Error, Result};
use crate::util::variables::PUBLIC_URL;
use crate::database::*;
#[get("/<target>/avatar")] #[get("/<target>/avatar")]
pub async fn req(target: Ref) -> Option<NamedFile> { pub async fn req(target: Ref) -> Result<Redirect> {
match target.id.chars().nth(25).unwrap() { let doc = get_collection("accounts")
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' => { .find_one(
NamedFile::open(Path::new("assets/user_red.png")).await.ok() doc! {
} "_id": &target.id
'8' | '9' | 'A' | 'C' | 'B' | 'D' | 'E' | 'F' => { },
NamedFile::open(Path::new("assets/user_green.png")) FindOneOptions::builder()
.await .projection(doc! { "email": 1 })
.ok() .build()
} )
'G' | 'H' | 'J' | 'K' | 'M' | 'N' | 'P' | 'Q' => { .await
NamedFile::open(Path::new("assets/user_blue.png")) .map_err(|_| Error::DatabaseError { operation: "find_one", with: "user" })?
.await .ok_or_else(|| Error::UnknownUser)?;
.ok()
} let email = doc
'R' | 'S' | 'T' | 'V' | 'W' | 'X' | 'Y' | 'Z' => { .get_str("email")
NamedFile::open(Path::new("assets/user_yellow.png")) .map_err(|_| Error::DatabaseError { operation: "get_str(email)", with: "user" })?
.await .to_lowercase();
.ok()
} let url = format!(
_ => unreachable!(), "https://www.gravatar.com/avatar/{:x}?s=128&d={}",
} md5::compute(email),
urlencoding::encode(
&format!(
"{}/users/{}/default_avatar",
*PUBLIC_URL,
&target.id
)
)
);
dbg!(&url);
Ok(
Redirect::to(url)
)
} }

View File

@@ -0,0 +1,29 @@
use rocket::response::NamedFile;
use std::path::Path;
use crate::database::Ref;
#[get("/<target>/default_avatar")]
pub async fn req(target: Ref) -> Option<NamedFile> {
match target.id.chars().nth(25).unwrap() {
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' => {
NamedFile::open(Path::new("assets/user_red.png")).await.ok()
}
'8' | '9' | 'A' | 'C' | 'B' | 'D' | 'E' | 'F' => {
NamedFile::open(Path::new("assets/user_green.png"))
.await
.ok()
}
'G' | 'H' | 'J' | 'K' | 'M' | 'N' | 'P' | 'Q' => {
NamedFile::open(Path::new("assets/user_blue.png"))
.await
.ok()
}
'R' | 'S' | 'T' | 'V' | 'W' | 'X' | 'Y' | 'Z' => {
NamedFile::open(Path::new("assets/user_yellow.png"))
.await
.ok()
}
_ => unreachable!(),
}
}

View File

@@ -7,6 +7,7 @@ mod fetch_relationship;
mod fetch_relationships; mod fetch_relationships;
mod fetch_user; mod fetch_user;
mod get_avatar; mod get_avatar;
mod get_default_avatar;
mod open_dm; mod open_dm;
mod remove_friend; mod remove_friend;
mod unblock_user; mod unblock_user;
@@ -15,6 +16,7 @@ pub fn routes() -> Vec<Route> {
routes![ routes![
// User Information // User Information
fetch_user::req, fetch_user::req,
get_default_avatar::req,
get_avatar::req, get_avatar::req,
// Direct Messaging // Direct Messaging
fetch_dms::req, fetch_dms::req,