mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 21:47:02 +00:00
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 crate::util::result::{Error, Result};
|
||||
use crate::database::*;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[serde(tag = "type")]
|
||||
enum Metadata {
|
||||
@@ -32,3 +36,54 @@ pub struct File {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
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 username: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub avatar: Option<File>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub relations: Option<Vec<Relationship>>,
|
||||
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
|
||||
Reference in New Issue
Block a user