2022-12-19 21:58:55 +00:00
|
|
|
use uuid::Uuid;
|
2021-12-31 16:43:14 +00:00
|
|
|
|
2023-04-25 13:49:35 +00:00
|
|
|
use fedimovies_config::Instance;
|
|
|
|
use fedimovies_models::{
|
2023-03-30 20:27:17 +00:00
|
|
|
database::{DatabaseClient, DatabaseError},
|
2023-01-21 22:45:25 +00:00
|
|
|
emojis::types::DbEmoji,
|
2022-12-19 21:58:55 +00:00
|
|
|
posts::{
|
|
|
|
helpers::{add_related_posts, add_user_actions},
|
|
|
|
types::Post,
|
|
|
|
},
|
|
|
|
users::types::User,
|
2022-05-06 15:03:43 +00:00
|
|
|
};
|
2023-03-30 20:27:17 +00:00
|
|
|
|
2023-03-23 18:35:05 +00:00
|
|
|
use super::microsyntax::{
|
|
|
|
emojis::find_emojis,
|
|
|
|
hashtags::{find_hashtags, replace_hashtags},
|
2023-04-24 15:35:32 +00:00
|
|
|
links::{find_linked_posts, replace_object_links},
|
2023-03-23 18:35:05 +00:00
|
|
|
mentions::{find_mentioned_profiles, replace_mentions},
|
|
|
|
};
|
2022-05-06 15:03:43 +00:00
|
|
|
use super::types::Status;
|
2021-12-31 16:43:14 +00:00
|
|
|
|
2022-12-19 21:58:55 +00:00
|
|
|
pub struct PostContent {
|
|
|
|
pub content: String,
|
|
|
|
pub mentions: Vec<Uuid>,
|
2023-01-30 23:04:56 +00:00
|
|
|
pub hashtags: Vec<String>,
|
2022-12-19 21:58:55 +00:00
|
|
|
pub links: Vec<Uuid>,
|
|
|
|
pub linked: Vec<Post>,
|
2023-01-21 22:45:25 +00:00
|
|
|
pub emojis: Vec<DbEmoji>,
|
2022-12-19 21:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn parse_microsyntaxes(
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &impl DatabaseClient,
|
2022-12-19 21:58:55 +00:00
|
|
|
instance: &Instance,
|
|
|
|
mut content: String,
|
|
|
|
) -> Result<PostContent, DatabaseError> {
|
|
|
|
// Mentions
|
2023-04-24 15:35:32 +00:00
|
|
|
let mention_map = find_mentioned_profiles(db_client, &instance.hostname(), &content).await?;
|
2022-12-19 21:58:55 +00:00
|
|
|
content = replace_mentions(
|
|
|
|
&mention_map,
|
|
|
|
&instance.hostname(),
|
|
|
|
&instance.url(),
|
|
|
|
&content,
|
|
|
|
);
|
|
|
|
let mentions = mention_map.values().map(|profile| profile.id).collect();
|
|
|
|
// Hashtags
|
2023-01-30 23:04:56 +00:00
|
|
|
let hashtags = find_hashtags(&content);
|
2023-04-24 15:35:32 +00:00
|
|
|
content = replace_hashtags(&instance.url(), &content, &hashtags);
|
2022-12-19 21:58:55 +00:00
|
|
|
// Links
|
2023-04-24 15:35:32 +00:00
|
|
|
let link_map = find_linked_posts(db_client, &instance.url(), &content).await?;
|
|
|
|
content = replace_object_links(&link_map, &content);
|
2022-12-19 21:58:55 +00:00
|
|
|
let links = link_map.values().map(|post| post.id).collect();
|
|
|
|
let linked = link_map.into_values().collect();
|
2023-01-21 00:23:15 +00:00
|
|
|
// Emojis
|
2023-04-24 15:35:32 +00:00
|
|
|
let emoji_map = find_emojis(db_client, &content).await?;
|
2023-01-21 22:45:25 +00:00
|
|
|
let emojis = emoji_map.into_values().collect();
|
2023-04-24 15:35:32 +00:00
|
|
|
Ok(PostContent {
|
|
|
|
content,
|
|
|
|
mentions,
|
|
|
|
hashtags,
|
|
|
|
links,
|
|
|
|
linked,
|
|
|
|
emojis,
|
|
|
|
})
|
2022-12-19 21:58:55 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 15:03:43 +00:00
|
|
|
/// Load related objects and build status for API response
|
|
|
|
pub async fn build_status(
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &impl DatabaseClient,
|
2023-02-21 21:39:42 +00:00
|
|
|
base_url: &str,
|
2022-05-06 15:03:43 +00:00
|
|
|
instance_url: &str,
|
|
|
|
user: Option<&User>,
|
|
|
|
mut post: Post,
|
|
|
|
) -> Result<Status, DatabaseError> {
|
2022-08-21 20:08:45 +00:00
|
|
|
add_related_posts(db_client, vec![&mut post]).await?;
|
2022-05-06 15:03:43 +00:00
|
|
|
if let Some(user) = user {
|
|
|
|
add_user_actions(db_client, &user.id, vec![&mut post]).await?;
|
|
|
|
};
|
2023-02-21 21:39:42 +00:00
|
|
|
let status = Status::from_post(base_url, instance_url, post);
|
2022-05-06 15:03:43 +00:00
|
|
|
Ok(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn build_status_list(
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &impl DatabaseClient,
|
2023-02-21 21:39:42 +00:00
|
|
|
base_url: &str,
|
2022-05-06 15:03:43 +00:00
|
|
|
instance_url: &str,
|
|
|
|
user: Option<&User>,
|
|
|
|
mut posts: Vec<Post>,
|
|
|
|
) -> Result<Vec<Status>, DatabaseError> {
|
2022-08-21 20:08:45 +00:00
|
|
|
add_related_posts(db_client, posts.iter_mut().collect()).await?;
|
2022-05-06 15:03:43 +00:00
|
|
|
if let Some(user) = user {
|
|
|
|
add_user_actions(db_client, &user.id, posts.iter_mut().collect()).await?;
|
|
|
|
};
|
|
|
|
let statuses: Vec<Status> = posts
|
|
|
|
.into_iter()
|
2023-02-21 21:39:42 +00:00
|
|
|
.map(|post| Status::from_post(base_url, instance_url, post))
|
2022-05-06 15:03:43 +00:00
|
|
|
.collect();
|
|
|
|
Ok(statuses)
|
|
|
|
}
|