Plume/plume-models/src/medias.rs

409 lines
12 KiB
Rust
Raw Normal View History

2020-01-21 07:02:03 +01:00
use crate::{
ap_url, instance::Instance, safe_string::SafeString, schema::medias, users::User, Connection,
Error, PlumeRocket, Result,
};
2018-10-31 10:40:20 +01:00
use activitypub::object::Image;
use askama_escape::escape;
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
2018-10-31 10:40:20 +01:00
use guid_create::GUID;
Big refactoring of the Inbox (#443) * Big refactoring of the Inbox We now have a type that routes an activity through the registered handlers until one of them matches. Each Actor/Activity/Object combination is represented by an implementation of AsObject These combinations are then registered on the Inbox type, which will try to deserialize the incoming activity in the requested types. Advantages: - nicer syntax: the final API is clearer and more idiomatic - more generic: only two traits (`AsActor` and `AsObject`) instead of one for each kind of activity - it is easier to see which activities we handle and which one we don't * Small fixes - Avoid panics - Don't search for AP ID infinitely - Code style issues * Fix tests * Introduce a new trait: FromId It should be implemented for any AP object. It allows to look for an object in database using its AP ID, or to dereference it if it was not present in database Also moves the inbox code to plume-models to test it (and write a basic test for each activity type we handle) * Use if let instead of match * Don't require PlumeRocket::intl for tests * Return early and remove a forgotten dbg! * Add more tests to try to understand where the issues come from * Also add a test for comment federation * Don't check creation_date is the same for blogs * Make user and blog federation more tolerant to errors/missing fields * Make clippy happy * Use the correct Accept header when dereferencing * Fix follow approval with Mastodon * Add spaces to characters that should not be in usernames And validate blog names too * Smarter dereferencing: only do it once for each actor/object * Forgot some files * Cargo fmt * Delete plume_test * Delete plume_tests * Update get_id docs + Remove useless : Sized * Appease cargo fmt * Remove dbg! + Use as_ref instead of clone when possible + Use and_then instead of map when possible * Remove .po~ * send unfollow to local instance * read cover from update activity * Make sure "cc" and "to" are never empty and fix a typo in a constant name * Cargo fmt
2019-04-17 19:31:47 +02:00
use plume_common::{
activity_pub::{inbox::FromId, Id},
utils::MediaProcessor,
};
2020-01-21 07:02:03 +01:00
use reqwest;
use std::{fs, path::Path};
2018-09-02 13:34:48 +02:00
#[derive(Clone, Identifiable, Queryable)]
2018-09-02 13:34:48 +02:00
pub struct Media {
pub id: i32,
pub file_path: String,
pub alt_text: String,
pub is_remote: bool,
pub remote_url: Option<String>,
pub sensitive: bool,
2018-09-02 22:55:42 +02:00
pub content_warning: Option<String>,
pub owner_id: i32,
2018-09-02 13:34:48 +02:00
}
#[derive(Insertable)]
#[table_name = "medias"]
pub struct NewMedia {
pub file_path: String,
pub alt_text: String,
pub is_remote: bool,
pub remote_url: Option<String>,
pub sensitive: bool,
2018-09-02 22:55:42 +02:00
pub content_warning: Option<String>,
pub owner_id: i32,
2018-09-02 13:34:48 +02:00
}
#[derive(PartialEq)]
pub enum MediaCategory {
Image,
Audio,
Video,
Unknown,
}
impl MediaCategory {
pub fn to_string(&self) -> &str {
match *self {
MediaCategory::Image => "image",
MediaCategory::Audio => "audio",
MediaCategory::Video => "video",
MediaCategory::Unknown => "unknown",
}
}
}
2018-09-02 13:34:48 +02:00
impl Media {
insert!(medias, NewMedia);
get!(medias);
pub fn for_user(conn: &Connection, owner: i32) -> Result<Vec<Media>> {
medias::table
.filter(medias::owner_id.eq(owner))
.order(medias::id.desc())
.load::<Self>(conn)
.map_err(Error::from)
}
2018-09-02 22:55:42 +02:00
pub fn list_all_medias(conn: &Connection) -> Result<Vec<Media>> {
2019-03-20 17:56:17 +01:00
medias::table.load::<Media>(conn).map_err(Error::from)
}
2019-03-20 17:56:17 +01:00
pub fn page_for_user(
conn: &Connection,
user: &User,
(min, max): (i32, i32),
) -> Result<Vec<Media>> {
medias::table
.filter(medias::owner_id.eq(user.id))
.order(medias::id.desc())
.offset(i64::from(min))
.limit(i64::from(max - min))
.load::<Media>(conn)
.map_err(Error::from)
}
pub fn count_for_user(conn: &Connection, user: &User) -> Result<i64> {
medias::table
.filter(medias::owner_id.eq(user.id))
.count()
.get_result(conn)
.map_err(Error::from)
}
pub fn category(&self) -> MediaCategory {
match &*self
.file_path
.rsplitn(2, '.')
.next()
.expect("Media::category: extension error")
.to_lowercase()
{
"png" | "jpg" | "jpeg" | "gif" | "svg" => MediaCategory::Image,
"mp3" | "wav" | "flac" => MediaCategory::Audio,
"mp4" | "avi" | "webm" | "mov" => MediaCategory::Video,
_ => MediaCategory::Unknown,
}
}
pub fn html(&self) -> Result<SafeString> {
let url = self.url()?;
Ok(match self.category() {
MediaCategory::Image => SafeString::trusted(&format!(
r#"<img src="{}" alt="{}" title="{}">"#,
url,
escape(&self.alt_text),
escape(&self.alt_text)
)),
MediaCategory::Audio => SafeString::trusted(&format!(
r#"<div class="media-preview audio"></div><audio src="{}" title="{}" controls></audio>"#,
url,
escape(&self.alt_text)
)),
MediaCategory::Video => SafeString::trusted(&format!(
r#"<video src="{}" title="{}" controls></video>"#,
url,
escape(&self.alt_text)
)),
MediaCategory::Unknown => SafeString::trusted(&format!(
r#"<a href="{}" class="media-preview unknown"></a>"#,
url,
)),
})
}
pub fn markdown(&self) -> Result<SafeString> {
Ok(match self.category() {
2019-03-20 17:56:17 +01:00
MediaCategory::Image => {
SafeString::new(&format!("![{}]({})", escape(&self.alt_text), self.id))
2019-03-20 17:56:17 +01:00
}
MediaCategory::Audio | MediaCategory::Video => self.html()?,
MediaCategory::Unknown => SafeString::new(""),
})
2018-09-02 22:55:42 +02:00
}
pub fn url(&self) -> Result<String> {
if self.is_remote {
Ok(self.remote_url.clone().unwrap_or_default())
} else {
let p = Path::new(&self.file_path);
let filename: String = p.file_name().unwrap().to_str().unwrap().to_owned();
Ok(ap_url(&format!(
"{}/static/media/{}",
Instance::get_local()?.public_domain,
&filename
)))
}
2018-09-02 22:55:42 +02:00
}
2018-09-02 23:10:15 +02:00
pub fn delete(&self, conn: &Connection) -> Result<()> {
if !self.is_remote {
fs::remove_file(self.file_path.as_str())?;
}
diesel::delete(self)
.execute(conn)
.map(|_| ())
.map_err(Error::from)
2018-09-02 23:10:15 +02:00
}
pub fn save_remote(conn: &Connection, url: String, user: &User) -> Result<Media> {
2018-12-02 19:07:36 +01:00
if url.contains(&['<', '>', '"'][..]) {
Err(Error::Url)
2018-12-02 19:07:36 +01:00
} else {
Media::insert(
2018-12-02 19:07:36 +01:00
conn,
NewMedia {
file_path: String::new(),
alt_text: String::new(),
is_remote: true,
remote_url: Some(url),
sensitive: false,
content_warning: None,
owner_id: user.id,
},
)
2018-12-02 19:07:36 +01:00
}
}
pub fn set_owner(&self, conn: &Connection, user: &User) -> Result<()> {
diesel::update(self)
.set(medias::owner_id.eq(user.id))
.execute(conn)
.map(|_| ())
.map_err(Error::from)
}
2018-10-31 10:40:20 +01:00
// TODO: merge with save_remote?
Big refactoring of the Inbox (#443) * Big refactoring of the Inbox We now have a type that routes an activity through the registered handlers until one of them matches. Each Actor/Activity/Object combination is represented by an implementation of AsObject These combinations are then registered on the Inbox type, which will try to deserialize the incoming activity in the requested types. Advantages: - nicer syntax: the final API is clearer and more idiomatic - more generic: only two traits (`AsActor` and `AsObject`) instead of one for each kind of activity - it is easier to see which activities we handle and which one we don't * Small fixes - Avoid panics - Don't search for AP ID infinitely - Code style issues * Fix tests * Introduce a new trait: FromId It should be implemented for any AP object. It allows to look for an object in database using its AP ID, or to dereference it if it was not present in database Also moves the inbox code to plume-models to test it (and write a basic test for each activity type we handle) * Use if let instead of match * Don't require PlumeRocket::intl for tests * Return early and remove a forgotten dbg! * Add more tests to try to understand where the issues come from * Also add a test for comment federation * Don't check creation_date is the same for blogs * Make user and blog federation more tolerant to errors/missing fields * Make clippy happy * Use the correct Accept header when dereferencing * Fix follow approval with Mastodon * Add spaces to characters that should not be in usernames And validate blog names too * Smarter dereferencing: only do it once for each actor/object * Forgot some files * Cargo fmt * Delete plume_test * Delete plume_tests * Update get_id docs + Remove useless : Sized * Appease cargo fmt * Remove dbg! + Use as_ref instead of clone when possible + Use and_then instead of map when possible * Remove .po~ * send unfollow to local instance * read cover from update activity * Make sure "cc" and "to" are never empty and fix a typo in a constant name * Cargo fmt
2019-04-17 19:31:47 +02:00
pub fn from_activity(c: &PlumeRocket, image: &Image) -> Result<Media> {
let conn = &*c.conn;
2018-10-31 10:40:20 +01:00
let remote_url = image.object_props.url_string().ok()?;
let ext = remote_url
.rsplit('.')
.next()
.map(ToOwned::to_owned)
.unwrap_or_else(|| String::from("png"));
let path = Path::new(&super::CONFIG.media_directory).join(format!(
"{}.{}",
GUID::rand().to_string(),
ext
));
2018-10-31 10:40:20 +01:00
let mut dest = fs::File::create(path.clone()).ok()?;
reqwest::get(remote_url.as_str())
.ok()?
.copy_to(&mut dest)
.ok()?;
Media::insert(
conn,
NewMedia {
file_path: path.to_str()?.to_string(),
alt_text: image.object_props.content_string().ok()?,
2018-12-02 19:07:36 +01:00
is_remote: false,
remote_url: None,
sensitive: image.object_props.summary_string().is_ok(),
content_warning: image.object_props.summary_string().ok(),
Big refactoring of the Inbox (#443) * Big refactoring of the Inbox We now have a type that routes an activity through the registered handlers until one of them matches. Each Actor/Activity/Object combination is represented by an implementation of AsObject These combinations are then registered on the Inbox type, which will try to deserialize the incoming activity in the requested types. Advantages: - nicer syntax: the final API is clearer and more idiomatic - more generic: only two traits (`AsActor` and `AsObject`) instead of one for each kind of activity - it is easier to see which activities we handle and which one we don't * Small fixes - Avoid panics - Don't search for AP ID infinitely - Code style issues * Fix tests * Introduce a new trait: FromId It should be implemented for any AP object. It allows to look for an object in database using its AP ID, or to dereference it if it was not present in database Also moves the inbox code to plume-models to test it (and write a basic test for each activity type we handle) * Use if let instead of match * Don't require PlumeRocket::intl for tests * Return early and remove a forgotten dbg! * Add more tests to try to understand where the issues come from * Also add a test for comment federation * Don't check creation_date is the same for blogs * Make user and blog federation more tolerant to errors/missing fields * Make clippy happy * Use the correct Accept header when dereferencing * Fix follow approval with Mastodon * Add spaces to characters that should not be in usernames And validate blog names too * Smarter dereferencing: only do it once for each actor/object * Forgot some files * Cargo fmt * Delete plume_test * Delete plume_tests * Update get_id docs + Remove useless : Sized * Appease cargo fmt * Remove dbg! + Use as_ref instead of clone when possible + Use and_then instead of map when possible * Remove .po~ * send unfollow to local instance * read cover from update activity * Make sure "cc" and "to" are never empty and fix a typo in a constant name * Cargo fmt
2019-04-17 19:31:47 +02:00
owner_id: User::from_id(
c,
image
.object_props
.attributed_to_link_vec::<Id>()
.ok()?
.into_iter()
.next()?
.as_ref(),
Big refactoring of the Inbox (#443) * Big refactoring of the Inbox We now have a type that routes an activity through the registered handlers until one of them matches. Each Actor/Activity/Object combination is represented by an implementation of AsObject These combinations are then registered on the Inbox type, which will try to deserialize the incoming activity in the requested types. Advantages: - nicer syntax: the final API is clearer and more idiomatic - more generic: only two traits (`AsActor` and `AsObject`) instead of one for each kind of activity - it is easier to see which activities we handle and which one we don't * Small fixes - Avoid panics - Don't search for AP ID infinitely - Code style issues * Fix tests * Introduce a new trait: FromId It should be implemented for any AP object. It allows to look for an object in database using its AP ID, or to dereference it if it was not present in database Also moves the inbox code to plume-models to test it (and write a basic test for each activity type we handle) * Use if let instead of match * Don't require PlumeRocket::intl for tests * Return early and remove a forgotten dbg! * Add more tests to try to understand where the issues come from * Also add a test for comment federation * Don't check creation_date is the same for blogs * Make user and blog federation more tolerant to errors/missing fields * Make clippy happy * Use the correct Accept header when dereferencing * Fix follow approval with Mastodon * Add spaces to characters that should not be in usernames And validate blog names too * Smarter dereferencing: only do it once for each actor/object * Forgot some files * Cargo fmt * Delete plume_test * Delete plume_tests * Update get_id docs + Remove useless : Sized * Appease cargo fmt * Remove dbg! + Use as_ref instead of clone when possible + Use and_then instead of map when possible * Remove .po~ * send unfollow to local instance * read cover from update activity * Make sure "cc" and "to" are never empty and fix a typo in a constant name * Cargo fmt
2019-04-17 19:31:47 +02:00
None,
)
.map_err(|(_, e)| e)?
2019-03-20 17:56:17 +01:00
.id,
},
)
}
pub fn get_media_processor<'a>(conn: &'a Connection, user: Vec<&User>) -> MediaProcessor<'a> {
let uid = user.iter().map(|u| u.id).collect::<Vec<_>>();
Box::new(move |id| {
let media = Media::get(conn, id).ok()?;
// if owner is user or check is disabled
if uid.contains(&media.owner_id) || uid.is_empty() {
Some((media.url().ok()?, media.content_warning))
} else {
None
}
})
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
2020-01-21 07:02:03 +01:00
use crate::{tests::db, users::tests as usersTests, Connection as Conn};
use diesel::Connection;
use std::env::{current_dir, set_current_dir};
use std::fs;
use std::path::Path;
pub(crate) fn fill_database(conn: &Conn) -> (Vec<User>, Vec<Media>) {
let mut wd = current_dir().unwrap().to_path_buf();
while wd.pop() {
if wd.join(".git").exists() {
set_current_dir(wd).unwrap();
break;
}
}
let users = usersTests::fill_database(conn);
let user_one = users[0].id;
let user_two = users[1].id;
let f1 = "static/media/1.png".to_owned();
let f2 = "static/media/2.mp3".to_owned();
fs::write(f1.clone(), []).unwrap();
fs::write(f2.clone(), []).unwrap();
2019-03-20 17:56:17 +01:00
(
users,
vec![
NewMedia {
file_path: f1,
alt_text: "some alt".to_owned(),
is_remote: false,
remote_url: None,
sensitive: false,
content_warning: None,
owner_id: user_one,
},
NewMedia {
file_path: f2,
alt_text: "alt message".to_owned(),
is_remote: false,
remote_url: None,
sensitive: true,
content_warning: Some("Content warning".to_owned()),
owner_id: user_one,
},
NewMedia {
file_path: "".to_owned(),
alt_text: "another alt".to_owned(),
is_remote: true,
remote_url: Some("https://example.com/".to_owned()),
sensitive: false,
content_warning: None,
owner_id: user_two,
},
]
.into_iter()
.map(|nm| Media::insert(conn, nm).unwrap())
2019-03-20 17:56:17 +01:00
.collect(),
)
}
pub(crate) fn clean(conn: &Conn) {
//used to remove files generated by tests
for media in Media::list_all_medias(conn).unwrap() {
media.delete(conn).unwrap();
}
}
#[test]
fn delete() {
let conn = &db();
conn.test_transaction::<_, (), _>(|| {
let user = fill_database(conn).0[0].id;
let path = "static/media/test_deletion".to_owned();
fs::write(path.clone(), []).unwrap();
let media = Media::insert(
conn,
NewMedia {
file_path: path.clone(),
alt_text: "alt message".to_owned(),
is_remote: false,
remote_url: None,
sensitive: false,
content_warning: None,
owner_id: user,
},
2019-03-20 17:56:17 +01:00
)
.unwrap();
assert!(Path::new(&path).exists());
media.delete(conn).unwrap();
assert!(!Path::new(&path).exists());
clean(conn);
Ok(())
});
}
#[test]
fn set_owner() {
let conn = &db();
conn.test_transaction::<_, (), _>(|| {
let (users, _) = fill_database(conn);
let u1 = &users[0];
let u2 = &users[1];
let path = "static/media/test_set_owner".to_owned();
fs::write(path.clone(), []).unwrap();
let media = Media::insert(
conn,
NewMedia {
file_path: path.clone(),
alt_text: "alt message".to_owned(),
is_remote: false,
remote_url: None,
sensitive: false,
content_warning: None,
owner_id: u1.id,
},
2019-03-20 17:56:17 +01:00
)
.unwrap();
assert!(Media::for_user(conn, u1.id)
.unwrap()
.iter()
.any(|m| m.id == media.id));
assert!(!Media::for_user(conn, u2.id)
.unwrap()
.iter()
.any(|m| m.id == media.id));
media.set_owner(conn, u2).unwrap();
2019-03-20 17:56:17 +01:00
assert!(!Media::for_user(conn, u1.id)
.unwrap()
.iter()
.any(|m| m.id == media.id));
assert!(Media::for_user(conn, u2.id)
.unwrap()
.iter()
.any(|m| m.id == media.id));
clean(conn);
Ok(())
});
2018-10-31 10:40:20 +01:00
}
2018-09-02 13:34:48 +02:00
}