Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly improve the media experience #452

Merged
merged 5 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ trim_trailing_whitespace = true

[*.{js,rs,css,tera,html}]
charset = utf-8
indent_size = 4

[*.{rs,tera,css,html}]
indent_style = space
Expand Down
62 changes: 37 additions & 25 deletions plume-models/src/medias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ pub enum MediaCategory {
Unknown,
}

impl MediaCategory {
pub fn to_string(&self) -> &str {
match *self {
MediaCategory::Image => "image",
MediaCategory::Audio => "audio",
MediaCategory::Video => "video",
MediaCategory::Unknown => "unknown",
}
}
}

impl Media {
insert!(medias, NewMedia);
get!(medias);
Expand All @@ -56,6 +67,23 @@ impl Media {
.map_err(Error::from)
}

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))
.offset(min as i64)
.limit((max - min) as i64)
.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
Expand All @@ -71,41 +99,25 @@ impl Media {
}
}

pub fn preview_html(&self, conn: &Connection) -> Result<SafeString> {
let url = self.url(conn)?;
Ok(match self.category() {
MediaCategory::Image => SafeString::new(&format!(
r#"<img src="{}" alt="{}" title="{}" class=\"preview\">"#,
url, escape(&self.alt_text), escape(&self.alt_text)
)),
MediaCategory::Audio => SafeString::new(&format!(
r#"<audio src="{}" title="{}" class="preview"></audio>"#,
url, escape(&self.alt_text)
)),
MediaCategory::Video => SafeString::new(&format!(
r#"<video src="{}" title="{}" class="preview"></video>"#,
url, escape(&self.alt_text)
)),
MediaCategory::Unknown => SafeString::new(""),
})
}

pub fn html(&self, conn: &Connection) -> Result<SafeString> {
let url = self.url(conn)?;
Ok(match self.category() {
MediaCategory::Image => SafeString::new(&format!(
MediaCategory::Image => SafeString::trusted(&format!(
r#"<img src="{}" alt="{}" title="{}">"#,
url, escape(&self.alt_text), escape(&self.alt_text)
)),
MediaCategory::Audio => SafeString::new(&format!(
r#"<audio src="{}" title="{}"></audio>"#,
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::new(&format!(
r#"<video src="{}" title="{}"></video>"#,
MediaCategory::Video => SafeString::trusted(&format!(
r#"<video src="{}" title="{}" controls></video>"#,
url, escape(&self.alt_text)
)),
MediaCategory::Unknown => SafeString::new(""),
MediaCategory::Unknown => SafeString::trusted(&format!(
r#"<a href="{}" class="media-preview unknown"></a>"#,
url,
)),
})
}

Expand Down
24 changes: 22 additions & 2 deletions plume-models/src/safe_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ lazy_static! {
static ref CLEAN: Builder<'static> = {
let mut b = Builder::new();
b.add_generic_attributes(iter::once("id"))
.add_tags(iter::once("iframe"))
.add_tags(&[ "iframe", "video", "audio" ])
.id_prefix(Some("postcontent-"))
.url_relative(UrlRelative::Custom(Box::new(url_add_prefix)))
.add_tag_attributes(
"iframe",
["width", "height", "src", "frameborder"].iter().map(|&v| v),
[ "width", "height", "src", "frameborder" ].iter().map(|&v| v),
)
.add_tag_attributes(
"video",
[ "src", "title", "controls" ].iter(),
)
.add_tag_attributes(
"audio",
[ "src", "title", "controls" ].iter(),
);
b
};
Expand Down Expand Up @@ -53,6 +61,18 @@ impl SafeString {
value: CLEAN.clean(&value).to_string(),
}
}

/// Creates a new `SafeString`, but without escaping the given value.
///
/// Only use when you are sure you can trust the input (when the HTML
/// is entirely generated by Plume, not depending on user-inputed data).
/// Prefer `SafeString::new` as much as possible.
pub fn trusted(value: impl AsRef<str>) -> Self {
SafeString {
value: value.as_ref().to_string()
}
}

pub fn set(&mut self, value: &str) {
self.value = CLEAN.clean(value).to_string();
}
Expand Down
24 changes: 24 additions & 0 deletions po/plume/ar.po
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ msgstr "تعديل حسابك"
msgid "Your Profile"
msgstr "ملفك الشخصي"

msgid "To change your avatar, upload it in your gallery and select from there."
msgstr ""

#, fuzzy
msgid "Upload an avatar"
msgstr "استخدمها كصورة رمزية"

#, fuzzy
msgid "Display name"
msgstr "الاسم العلني"
Expand Down Expand Up @@ -480,6 +487,15 @@ msgstr "العنوان الثانوي"
msgid "Content"
msgstr "المحتوى"

msgid ""
"You can upload medias to your gallery, and copy their Markdown code in your "
"articles to insert them."
msgstr ""

#, fuzzy
msgid "Upload media"
msgstr "إرسال"

# src/template_utils.rs:144
msgid "Tags, separated by commas"
msgstr ""
Expand Down Expand Up @@ -673,9 +689,17 @@ msgstr "إرسال"
msgid "You don't have any media yet."
msgstr "ليس لديك أية وسائط بعد."

#, fuzzy
msgid "Content warning: {0}"
msgstr "تحذير عن المحتوى"

msgid "Delete"
msgstr "حذف"

#, fuzzy
msgid "Details"
msgstr "تفاصيل الصورة"

msgid "Media upload"
msgstr "إرسال الوسائط"

Expand Down
23 changes: 23 additions & 0 deletions po/plume/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ msgstr "Ändere deinen Account"
msgid "Your Profile"
msgstr "Dein Profil"

msgid "To change your avatar, upload it in your gallery and select from there."
msgstr ""

#, fuzzy
msgid "Upload an avatar"
msgstr "Als Avatar verwenden"

#, fuzzy
msgid "Display name"
msgstr "Anzeigename"
Expand Down Expand Up @@ -495,6 +502,15 @@ msgstr "Untertitel"
msgid "Content"
msgstr "Inhalt"

msgid ""
"You can upload medias to your gallery, and copy their Markdown code in your "
"articles to insert them."
msgstr ""

#, fuzzy
msgid "Upload media"
msgstr "Hochladen"

# src/template_utils.rs:143
msgid "Tags, separated by commas"
msgstr ""
Expand Down Expand Up @@ -690,9 +706,16 @@ msgstr "Hochladen"
msgid "You don't have any media yet."
msgstr "Derzeit sind noch keine Mediendateien hochgeladen."

#, fuzzy
msgid "Content warning: {0}"
msgstr "Warnhinweis zum Inhalt"

msgid "Delete"
msgstr "Löschen"

msgid "Details"
msgstr ""

msgid "Media upload"
msgstr "Hochladen von Mediendateien"

Expand Down
20 changes: 20 additions & 0 deletions po/plume/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ msgstr ""
msgid "Your Profile"
msgstr ""

msgid "To change your avatar, upload it in your gallery and select from there."
msgstr ""

msgid "Upload an avatar"
msgstr ""

# src/template_utils.rs:144
msgid "Display name"
msgstr ""
Expand Down Expand Up @@ -473,6 +479,14 @@ msgstr ""
msgid "Content"
msgstr ""

msgid ""
"You can upload medias to your gallery, and copy their Markdown code in your "
"articles to insert them."
msgstr ""

msgid "Upload media"
msgstr ""

# src/template_utils.rs:144
msgid "Tags, separated by commas"
msgstr ""
Expand Down Expand Up @@ -638,9 +652,15 @@ msgstr ""
msgid "You don't have any media yet."
msgstr ""

msgid "Content warning: {0}"
msgstr ""

msgid "Delete"
msgstr ""

msgid "Details"
msgstr ""

msgid "Media upload"
msgstr ""

Expand Down
20 changes: 20 additions & 0 deletions po/plume/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ msgstr "Edita tu cuenta"
msgid "Your Profile"
msgstr "Tu perfil"

msgid "To change your avatar, upload it in your gallery and select from there."
msgstr ""

msgid "Upload an avatar"
msgstr ""

# src/template_utils.rs:144
msgid "Display name"
msgstr ""
Expand Down Expand Up @@ -457,6 +463,14 @@ msgstr ""
msgid "Content"
msgstr "Contenido"

msgid ""
"You can upload medias to your gallery, and copy their Markdown code in your "
"articles to insert them."
msgstr ""

msgid "Upload media"
msgstr ""

# src/template_utils.rs:144
msgid "Tags, separated by commas"
msgstr ""
Expand Down Expand Up @@ -626,9 +640,15 @@ msgstr ""
msgid "You don't have any media yet."
msgstr ""

msgid "Content warning: {0}"
msgstr ""

msgid "Delete"
msgstr ""

msgid "Details"
msgstr ""

msgid "Media upload"
msgstr ""

Expand Down
23 changes: 23 additions & 0 deletions po/plume/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ msgstr "Modifier votre compte"
msgid "Your Profile"
msgstr "Votre profil"

msgid "To change your avatar, upload it in your gallery and select from there."
msgstr ""

#, fuzzy
msgid "Upload an avatar"
msgstr "Utiliser comme avatar"

#, fuzzy
msgid "Display name"
msgstr "Nom affiché"
Expand Down Expand Up @@ -490,6 +497,15 @@ msgstr "Sous-titre"
msgid "Content"
msgstr "Contenu"

msgid ""
"You can upload medias to your gallery, and copy their Markdown code in your "
"articles to insert them."
msgstr ""

#, fuzzy
msgid "Upload media"
msgstr "Téléverser"

# src/template_utils.rs:143
msgid "Tags, separated by commas"
msgstr ""
Expand Down Expand Up @@ -684,9 +700,16 @@ msgstr "Téléverser"
msgid "You don't have any media yet."
msgstr "Vous n’avez pas encore de média."

#, fuzzy
msgid "Content warning: {0}"
msgstr "Avertissement"

msgid "Delete"
msgstr "Supprimer"

msgid "Details"
msgstr ""

msgid "Media upload"
msgstr "Téléversement de média"

Expand Down
Loading