2022-04-13 17:45:47 +00:00
|
|
|
use ammonia::clean_text;
|
|
|
|
|
2022-07-15 17:31:02 +00:00
|
|
|
use crate::activitypub::identifiers::{local_actor_id, local_object_id};
|
2022-04-13 17:45:47 +00:00
|
|
|
use crate::config::Instance;
|
|
|
|
use crate::models::posts::types::Post;
|
|
|
|
use crate::models::profiles::types::DbActorProfile;
|
2023-01-17 00:09:09 +00:00
|
|
|
use crate::utils::{
|
|
|
|
datetime::get_min_datetime,
|
|
|
|
html::clean_html_all,
|
|
|
|
};
|
2022-04-13 17:45:47 +00:00
|
|
|
|
|
|
|
const ENTRY_TITLE_MAX_LENGTH: usize = 75;
|
|
|
|
|
|
|
|
fn make_entry(
|
|
|
|
instance_url: &str,
|
|
|
|
post: &Post,
|
|
|
|
) -> String {
|
2022-07-15 17:31:02 +00:00
|
|
|
let object_id = local_object_id(instance_url, &post.id);
|
2022-04-13 17:45:47 +00:00
|
|
|
let content_escaped = clean_text(&post.content);
|
|
|
|
let content_cleaned = clean_html_all(&post.content);
|
|
|
|
// Use trimmed content for title
|
|
|
|
let mut title: String = content_cleaned.chars()
|
|
|
|
.take(ENTRY_TITLE_MAX_LENGTH)
|
|
|
|
.collect();
|
|
|
|
if title.len() == ENTRY_TITLE_MAX_LENGTH &&
|
|
|
|
content_cleaned.len() != ENTRY_TITLE_MAX_LENGTH {
|
|
|
|
title += "...";
|
|
|
|
};
|
|
|
|
format!(
|
2023-01-25 22:09:27 +00:00
|
|
|
r#"<entry>
|
|
|
|
<id>{url}</id>
|
|
|
|
<title>{title}</title>
|
|
|
|
<updated>{updated_at}</updated>
|
|
|
|
<author><name>{author}</name></author>
|
|
|
|
<content type="html">{content}</content>
|
2023-01-25 22:24:45 +00:00
|
|
|
<link rel="alternate" href="{url}"/>
|
2023-01-25 22:09:27 +00:00
|
|
|
</entry>"#,
|
2022-04-13 17:45:47 +00:00
|
|
|
url=object_id,
|
|
|
|
title=title,
|
|
|
|
updated_at=post.created_at.to_rfc3339(),
|
|
|
|
author=post.author.username,
|
|
|
|
content=content_escaped,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-01-25 22:22:56 +00:00
|
|
|
pub fn get_feed_url(instance_url: &str, username: &str) -> String {
|
|
|
|
format!("{}/feeds/{}", instance_url, username)
|
|
|
|
}
|
|
|
|
|
2022-04-13 17:45:47 +00:00
|
|
|
pub fn make_feed(
|
|
|
|
instance: &Instance,
|
|
|
|
profile: &DbActorProfile,
|
|
|
|
posts: Vec<Post>,
|
|
|
|
) -> String {
|
2023-01-25 22:22:56 +00:00
|
|
|
let actor_id = local_actor_id(&instance.url(), &profile.username);
|
2022-04-13 17:45:47 +00:00
|
|
|
let actor_name = profile.display_name.as_ref()
|
|
|
|
.unwrap_or(&profile.username);
|
2022-11-24 01:51:43 +00:00
|
|
|
let actor_address = profile.actor_address(&instance.hostname());
|
2023-01-25 22:22:56 +00:00
|
|
|
let feed_url = get_feed_url(&instance.url(), &profile.username);
|
2022-04-13 17:45:47 +00:00
|
|
|
let feed_title = format!("{} (@{})", actor_name, actor_address);
|
|
|
|
let mut entries = vec![];
|
|
|
|
let mut feed_updated_at = get_min_datetime();
|
|
|
|
for post in posts {
|
|
|
|
let entry = make_entry(&instance.url(), &post);
|
|
|
|
entries.push(entry);
|
|
|
|
if post.created_at > feed_updated_at {
|
|
|
|
feed_updated_at = post.created_at;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
format!(
|
|
|
|
r#"<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<feed xmlns="http://www.w3.org/2005/Atom">
|
2023-01-25 22:22:56 +00:00
|
|
|
<id>{id}</id>
|
|
|
|
<link rel="self" href="{url}"/>
|
2023-01-25 22:09:27 +00:00
|
|
|
<title>{title}</title>
|
|
|
|
<updated>{updated_at}</updated>
|
|
|
|
{entries}
|
2022-04-13 17:45:47 +00:00
|
|
|
</feed>"#,
|
2023-01-25 22:22:56 +00:00
|
|
|
id=actor_id,
|
|
|
|
url=feed_url,
|
2022-04-13 17:45:47 +00:00
|
|
|
title=feed_title,
|
|
|
|
updated_at=feed_updated_at.to_rfc3339(),
|
2023-01-25 22:09:27 +00:00
|
|
|
entries=entries.join("\n"),
|
2022-04-13 17:45:47 +00:00
|
|
|
)
|
|
|
|
}
|