hyaenidae/server/src/views.rs

143 lines
3.7 KiB
Rust
Raw Normal View History

use crate::{
error::{Error, OptionExt},
extensions::{ProfileExt, SubmissionExt},
images::{BannerImage, IconImage},
};
use hyaenidae_profiles::store::{File, Profile, Submission};
use hyaenidae_toolkit::Tile;
use uuid::Uuid;
pub struct OwnedProfileView {
pub(crate) profile: Profile,
pub(crate) icon: Option<File>,
pub(crate) banner: Option<File>,
}
pub struct ProfileView<'a> {
pub(crate) profile: &'a Profile,
pub(crate) icon: Option<&'a File>,
pub(crate) banner: Option<&'a File>,
}
pub struct OwnedSubmissionView {
pub(crate) submission: Submission,
pub(crate) files: Vec<File>,
}
impl OwnedSubmissionView {
pub(crate) fn tiles(&self) -> Vec<Tile> {
self.files
.iter()
.filter_map(|file| file.pictrs_key())
.enumerate()
.map(|(index, key)| {
let title = format!("{} file {}", self.submission.title_text(), index + 1);
Tile::new(IconImage::new(key, &title)).title(&title)
})
.collect()
}
}
impl OwnedProfileView {
pub(crate) fn heading(&self) -> hyaenidae_toolkit::Profile {
ProfileView {
profile: &self.profile,
icon: self.icon.as_ref(),
banner: self.banner.as_ref(),
}
.heading()
}
pub(crate) fn icon(&self) -> hyaenidae_toolkit::Icon {
ProfileView {
profile: &self.profile,
icon: self.icon.as_ref(),
banner: self.banner.as_ref(),
}
.icon()
}
pub(crate) fn from_id(
profile_id: Uuid,
store: &hyaenidae_profiles::store::Store,
) -> Result<Self, Error> {
let profile = store.profiles.by_id(profile_id)?.req()?;
let icon = if let Some(icon) = profile.icon() {
store.files.by_id(icon)?
} else {
None
};
let banner = if let Some(banner) = profile.banner() {
store.files.by_id(banner)?
} else {
None
};
Ok(OwnedProfileView {
profile,
icon,
banner,
})
}
}
impl<'a> ProfileView<'a> {
pub(crate) fn heading(&self) -> hyaenidae_toolkit::Profile {
let heading =
hyaenidae_toolkit::Profile::new(self.profile.full_handle(), self.profile.view_path());
let heading = if let Some(display_name) = self.profile.display_name() {
heading.display_name(display_name)
} else {
heading
};
let heading = if let Some(description) = self.profile.description_text() {
heading.description(description)
} else {
heading
};
let heading = if let Some(icon) = self.icon_key() {
heading.icon_image(IconImage::new(
icon,
&format!("{}'s icon", self.profile.name()),
))
} else {
heading
};
if let Some(banner) = self.banner_key() {
heading.banner_image(BannerImage::new(
banner,
&format!("{}'s banner", self.profile.name()),
))
} else {
heading
}
}
pub(crate) fn icon(&self) -> hyaenidae_toolkit::Icon {
let icon = hyaenidae_toolkit::Icon::new(self.profile.view_path());
if let Some(key) = self.icon_key() {
icon.image(IconImage::new(
key,
&format!("{}'s icon", self.profile.name()),
))
} else {
icon
}
}
fn icon_key(&self) -> Option<&str> {
self.icon.and_then(|icon| icon.pictrs_key())
}
fn banner_key(&self) -> Option<&str> {
self.banner.and_then(|banner| banner.pictrs_key())
}
}