Add emojis to post previews

This commit is contained in:
silverpill 2023-01-21 22:45:25 +00:00
parent 97145efad9
commit be67972760
3 changed files with 26 additions and 5 deletions

View file

@ -3,6 +3,7 @@ use uuid::Uuid;
use crate::config::Instance;
use crate::database::{DatabaseClient, DatabaseError};
use crate::models::{
emojis::types::DbEmoji,
posts::{
emojis::find_emojis,
hashtags::{find_hashtags, replace_hashtags},
@ -21,7 +22,7 @@ pub struct PostContent {
pub tags: Vec<String>,
pub links: Vec<Uuid>,
pub linked: Vec<Post>,
pub emojis: Vec<Uuid>,
pub emojis: Vec<DbEmoji>,
}
pub async fn parse_microsyntaxes(
@ -66,7 +67,7 @@ pub async fn parse_microsyntaxes(
db_client,
&content,
).await?;
let emojis = emoji_map.values().map(|emoji| emoji.id).collect();
let emojis = emoji_map.into_values().collect();
Ok(PostContent { content, mentions, tags, links, linked, emojis })
}

View file

@ -187,6 +187,20 @@ pub struct StatusPreviewData {
#[derive(Serialize)]
pub struct StatusPreview {
pub content: String,
pub emojis: Vec<CustomEmoji>
}
impl StatusPreview {
pub fn new(
instance_url: &str,
content: String,
emojis: Vec<DbEmoji>,
) -> Self {
let emojis: Vec<CustomEmoji> = emojis.into_iter()
.map(|emoji| CustomEmoji::from_db(instance_url, emoji))
.collect();
Self { content, emojis }
}
}
#[derive(Deserialize)]

View file

@ -117,6 +117,7 @@ async fn create_status(
};
// Emoji validation
let emojis: Vec<_> = emojis.iter().map(|emoji| emoji.id).collect();
if emojis.len() > EMOJIS_MAX_NUM {
return Err(ValidationError("too many emojis").into());
};
@ -192,6 +193,7 @@ async fn preview_status(
) -> Result<HttpResponse, HttpError> {
let db_client = &**get_database_client(&db_pool).await?;
get_current_user(db_client, auth.token()).await?;
let instance = config.instance();
let status_data = status_data.into_inner();
let content = match status_data.content_type.as_str() {
"text/html" => status_data.status,
@ -201,15 +203,19 @@ async fn preview_status(
},
_ => return Err(ValidationError("unsupported content type").into()),
};
let PostContent { mut content, .. } = parse_microsyntaxes(
let PostContent { mut content, emojis, .. } = parse_microsyntaxes(
db_client,
&config.instance(),
&instance,
content,
).await?;
// Clean content
content = clean_content(&content)?;
// Return preview
let preview = StatusPreview { content };
let preview = StatusPreview::new(
&instance.url(),
content,
emojis,
);
Ok(HttpResponse::Ok().json(preview))
}