API change: deprecate Gravatar.
Include avatar information in user object.
This commit is contained in:
@@ -1,5 +1,9 @@
|
|||||||
|
use mongodb::bson::{doc, from_document};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::util::result::{Error, Result};
|
||||||
|
use crate::database::*;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
enum Metadata {
|
enum Metadata {
|
||||||
@@ -32,3 +36,54 @@ pub struct File {
|
|||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
object_id: Option<String>,
|
object_id: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl File {
|
||||||
|
pub async fn find_and_use(attachment_id: &str, tag: &str, parent_type: &str, parent_id: &str) -> Result<File> {
|
||||||
|
let attachments = get_collection("attachments");
|
||||||
|
let key = format!("{}_id", parent_type);
|
||||||
|
if let Some(doc) = attachments
|
||||||
|
.find_one(
|
||||||
|
doc! {
|
||||||
|
"_id": attachment_id,
|
||||||
|
"tag": &tag,
|
||||||
|
key.clone(): {
|
||||||
|
"$exists": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map_err(|_| Error::DatabaseError {
|
||||||
|
operation: "find_one",
|
||||||
|
with: "attachment",
|
||||||
|
})?
|
||||||
|
{
|
||||||
|
let attachment = from_document::<File>(doc).map_err(|_| Error::DatabaseError {
|
||||||
|
operation: "from_document",
|
||||||
|
with: "attachment",
|
||||||
|
})?;
|
||||||
|
|
||||||
|
attachments
|
||||||
|
.update_one(
|
||||||
|
doc! {
|
||||||
|
"_id": &attachment.id
|
||||||
|
},
|
||||||
|
doc! {
|
||||||
|
"$set": {
|
||||||
|
key: &parent_id
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map_err(|_| Error::DatabaseError {
|
||||||
|
operation: "update_one",
|
||||||
|
with: "attachment",
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(attachment)
|
||||||
|
} else {
|
||||||
|
Err(Error::UnknownAttachment)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -63,6 +63,8 @@ pub struct User {
|
|||||||
pub id: String,
|
pub id: String,
|
||||||
pub username: String,
|
pub username: String,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub avatar: Option<File>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub relations: Option<Vec<Relationship>>,
|
pub relations: Option<Vec<Relationship>>,
|
||||||
|
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use crate::database::*;
|
|||||||
use crate::util::result::{Error, Result};
|
use crate::util::result::{Error, Result};
|
||||||
|
|
||||||
use mongodb::{
|
use mongodb::{
|
||||||
bson::{doc, from_document},
|
bson::{doc},
|
||||||
options::FindOneOptions,
|
options::FindOneOptions,
|
||||||
};
|
};
|
||||||
use rocket_contrib::json::{Json, JsonValue};
|
use rocket_contrib::json::{Json, JsonValue};
|
||||||
@@ -61,52 +61,8 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal
|
|||||||
}
|
}
|
||||||
|
|
||||||
let id = Ulid::new().to_string();
|
let id = Ulid::new().to_string();
|
||||||
let attachments = get_collection("attachments");
|
|
||||||
let attachment = if let Some(attachment_id) = &message.attachment {
|
let attachment = if let Some(attachment_id) = &message.attachment {
|
||||||
if let Some(doc) = attachments
|
Some(File::find_and_use(attachment_id, "attachments", "message", &id).await?)
|
||||||
.find_one(
|
|
||||||
doc! {
|
|
||||||
"_id": attachment_id,
|
|
||||||
"tag": "attachments",
|
|
||||||
"message_id": {
|
|
||||||
"$exists": false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|_| Error::DatabaseError {
|
|
||||||
operation: "find_one",
|
|
||||||
with: "attachment",
|
|
||||||
})?
|
|
||||||
{
|
|
||||||
let attachment = from_document::<File>(doc).map_err(|_| Error::DatabaseError {
|
|
||||||
operation: "from_document",
|
|
||||||
with: "attachment",
|
|
||||||
})?;
|
|
||||||
|
|
||||||
attachments
|
|
||||||
.update_one(
|
|
||||||
doc! {
|
|
||||||
"_id": &attachment.id
|
|
||||||
},
|
|
||||||
doc! {
|
|
||||||
"$set": {
|
|
||||||
"message_id": &id
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|_| Error::DatabaseError {
|
|
||||||
operation: "update_one",
|
|
||||||
with: "attachment",
|
|
||||||
})?;
|
|
||||||
|
|
||||||
Some(attachment)
|
|
||||||
} else {
|
|
||||||
return Err(Error::UnknownAttachment);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -15,17 +15,35 @@ pub struct Data {
|
|||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
#[validate]
|
#[validate]
|
||||||
profile: Option<UserProfile>,
|
profile: Option<UserProfile>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
#[validate(length(min = 1, max = 128))]
|
||||||
|
avatar: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[patch("/<_ignore_id>", data = "<data>")]
|
#[patch("/<_ignore_id>", data = "<data>")]
|
||||||
pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()> {
|
pub async fn req(user: User, mut data: Json<Data>, _ignore_id: String) -> Result<()> {
|
||||||
|
if data.0.status.is_none() && data.0.profile.is_none() && data.0.avatar.is_none() {
|
||||||
|
return Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
data.validate()
|
data.validate()
|
||||||
.map_err(|error| Error::FailedValidation { error })?;
|
.map_err(|error| Error::FailedValidation { error })?;
|
||||||
|
|
||||||
|
let mut set = to_document(&data.0).map_err(|_| Error::DatabaseError { operation: "to_document", with: "data" })?;
|
||||||
|
|
||||||
|
let avatar = std::mem::replace(&mut data.0.avatar, None);
|
||||||
|
let attachment = if let Some(attachment_id) = avatar {
|
||||||
|
let attachment = File::find_and_use(&attachment_id, "avatars", "user", &user.id).await?;
|
||||||
|
set.insert("avatar", to_document(&attachment).map_err(|_| Error::DatabaseError { operation: "to_document", with: "attachment" })?);
|
||||||
|
Some(attachment)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
get_collection("users")
|
get_collection("users")
|
||||||
.update_one(
|
.update_one(
|
||||||
doc! { "_id": &user.id },
|
doc! { "_id": &user.id },
|
||||||
doc! { "$set": to_document(&data.0).map_err(|_| Error::DatabaseError { operation: "to_document", with: "data" })? },
|
doc! { "$set": set },
|
||||||
None
|
None
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
@@ -41,5 +59,15 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()>
|
|||||||
.ok();
|
.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(avatar) = attachment {
|
||||||
|
ClientboundNotification::UserUpdate {
|
||||||
|
id: user.id.clone(),
|
||||||
|
data: json!({ "avatar": avatar }),
|
||||||
|
}
|
||||||
|
.publish(user.id.clone())
|
||||||
|
.await
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,49 +0,0 @@
|
|||||||
use md5;
|
|
||||||
use mongodb::bson::doc;
|
|
||||||
use mongodb::options::FindOneOptions;
|
|
||||||
use rocket::response::Redirect;
|
|
||||||
use urlencoding;
|
|
||||||
|
|
||||||
use crate::database::*;
|
|
||||||
use crate::util::result::{Error, Result};
|
|
||||||
use crate::util::variables::PUBLIC_URL;
|
|
||||||
|
|
||||||
#[get("/<target>/avatar")]
|
|
||||||
pub async fn req(target: Ref) -> Result<Redirect> {
|
|
||||||
let doc = get_collection("accounts")
|
|
||||||
.find_one(
|
|
||||||
doc! {
|
|
||||||
"_id": &target.id
|
|
||||||
},
|
|
||||||
FindOneOptions::builder()
|
|
||||||
.projection(doc! { "email": 1 })
|
|
||||||
.build(),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|_| Error::DatabaseError {
|
|
||||||
operation: "find_one",
|
|
||||||
with: "user",
|
|
||||||
})?
|
|
||||||
.ok_or_else(|| Error::UnknownUser)?;
|
|
||||||
|
|
||||||
let email = doc
|
|
||||||
.get_str("email")
|
|
||||||
.map_err(|_| Error::DatabaseError {
|
|
||||||
operation: "get_str(email)",
|
|
||||||
with: "user",
|
|
||||||
})?
|
|
||||||
.to_lowercase();
|
|
||||||
|
|
||||||
let url = format!(
|
|
||||||
"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))
|
|
||||||
}
|
|
||||||
@@ -10,7 +10,6 @@ mod fetch_relationship;
|
|||||||
mod fetch_relationships;
|
mod fetch_relationships;
|
||||||
mod fetch_user;
|
mod fetch_user;
|
||||||
mod find_mutual;
|
mod find_mutual;
|
||||||
mod get_avatar;
|
|
||||||
mod get_default_avatar;
|
mod get_default_avatar;
|
||||||
mod open_dm;
|
mod open_dm;
|
||||||
mod remove_friend;
|
mod remove_friend;
|
||||||
@@ -23,7 +22,6 @@ pub fn routes() -> Vec<Route> {
|
|||||||
edit_user::req,
|
edit_user::req,
|
||||||
change_username::req,
|
change_username::req,
|
||||||
get_default_avatar::req,
|
get_default_avatar::req,
|
||||||
get_avatar::req,
|
|
||||||
fetch_profile::req,
|
fetch_profile::req,
|
||||||
// Direct Messaging
|
// Direct Messaging
|
||||||
fetch_dms::req,
|
fetch_dms::req,
|
||||||
|
|||||||
Reference in New Issue
Block a user