Talon/src/model.rs
ThetaDev 166e6c1738
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
feat: add website icons
2023-04-01 23:18:46 +02:00

260 lines
7.1 KiB
Rust

use std::collections::BTreeMap;
use hex::ToHex;
use poem_openapi::{Enum, Object};
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
use crate::{api::ApiError, build, db, util};
/// Website
#[derive(Debug, Clone, Object, Serialize, Deserialize)]
pub struct Website {
/// Website subdomain
pub subdomain: String,
/// Website name
pub name: String,
/// Website creation date
#[serde(with = "time::serde::rfc3339")]
pub created_at: OffsetDateTime,
/// Latest version ID
pub latest_version: Option<u32>,
/// Color of the page icon
///
/// Format: `#7935df`
pub color: Option<String>,
/// Visibility of the page in the sidebar menu
pub visibility: Visibility,
/// Link to the source of the page
pub source_url: Option<String>,
/// Icon for the source link
pub source_icon: Option<SourceIcon>,
/// Does the website have an icon?
pub has_icon: bool,
}
/// Create a new website
#[derive(Debug, Clone, Object, Serialize, Deserialize)]
pub struct WebsiteNew {
/// Website name
pub name: String,
/// Color of the page icon
pub color: Option<String>,
/// Visibility of the page in the sidebar menu
#[serde(default)]
#[oai(default)]
pub visibility: Visibility,
/// Link to the source of the page
pub source_url: Option<String>,
/// Icon for the source link
pub source_icon: Option<SourceIcon>,
}
/// Update a website with the contained values
///
/// Values set to `None` remain unchanged.
#[derive(Debug, Clone, Object, Serialize, Deserialize)]
pub struct WebsiteUpdate {
/// Website name
pub name: Option<String>,
/// Color of the page icon
pub color: Option<Option<String>>,
/// Visibility of the page in the sidebar menu
pub visibility: Option<Visibility>,
/// Link to the source of the page
pub source_url: Option<Option<String>>,
/// Icon for the source link
pub source_icon: Option<Option<SourceIcon>>,
}
/// Website version
#[derive(Debug, Clone, Object, Serialize, Deserialize)]
pub struct Version {
/// Version ID
pub id: u32,
/// Version creation date
#[serde(with = "time::serde::rfc3339")]
pub created_at: OffsetDateTime,
/// Associated version data
///
/// This is an arbitrary string map that can hold build information and other stuff
/// and will be displayed in the site info dialog.
pub data: BTreeMap<String, String>,
}
/// Website file
#[derive(Debug, Clone, Object, Serialize, Deserialize)]
pub struct VersionFile {
/// File path
pub path: String,
/// File hash
pub hash: String,
/// MIME file type
pub mime: Option<String>,
}
/// Server information
#[derive(Debug, Clone, Object, Serialize, Deserialize)]
pub struct Info {
/// Stats about your instance
pub stats: Stats,
/// Version information
pub version: VersionInfo,
/// Start time of the server
#[serde(with = "time::serde::rfc3339")]
pub start_time: OffsetDateTime,
}
/// Information about a Talon version
#[derive(Debug, Clone, Object, Serialize, Deserialize)]
pub struct VersionInfo {
/// Talon version
pub version: String,
/// Commit hash
pub commit: String,
/// Commit date
#[serde(with = "time::serde::rfc3339")]
pub commit_date: OffsetDateTime,
/// Rust version
pub rust_version: String,
/// Build target (OS and architecture)
pub build_target: String,
/// Rust build mode (`debug` / `release`)
pub build_mode: String,
}
/// Stats about your Talon instance
#[derive(Debug, Clone, Object, Serialize, Deserialize)]
pub struct Stats {
/// Number of websites
pub n_websites: usize,
/// Number of unique files
pub n_files: usize,
/// Amount of used storage space (in bytes)
pub storage_used: u64,
}
/// Embedded site config
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TalonConfig<'a> {
pub internal: &'a str,
pub version: &'a str,
pub root_domain: &'a str,
}
#[derive(
Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Enum, Serialize, Deserialize,
)]
#[oai(rename_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum Visibility {
Featured,
Searchable,
#[default]
Hidden,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Enum, Serialize, Deserialize)]
#[oai(rename_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum SourceIcon {
Link,
Git,
Github,
Gitlab,
Gitea,
Bitbucket,
}
impl From<(String, db::model::Website)> for Website {
fn from(value: (String, db::model::Website)) -> Self {
let w = value.1;
Self {
subdomain: value.0,
name: w.name,
created_at: w.created_at,
latest_version: w.latest_version,
color: w.color.map(|c| format!("#{c:06x}")),
visibility: w.visibility,
source_url: w.source_url,
source_icon: w.source_icon,
has_icon: w.has_icon,
}
}
}
impl TryFrom<WebsiteNew> for db::model::Website {
type Error = ApiError;
fn try_from(value: WebsiteNew) -> Result<Self, Self::Error> {
Ok(Self {
name: value.name,
color: match value.color {
Some(color) => Some(util::parse_color(&color).ok_or(ApiError::InvalidColor)?),
None => None,
},
visibility: value.visibility,
source_url: value.source_url,
source_icon: value.source_icon,
..Default::default()
})
}
}
impl TryFrom<WebsiteUpdate> for db::model::WebsiteUpdate {
type Error = ApiError;
fn try_from(value: WebsiteUpdate) -> Result<Self, Self::Error> {
Ok(Self {
name: value.name,
latest_version: None,
color: match value.color {
Some(Some(color)) => Some(Some(
util::parse_color(&color).ok_or(ApiError::InvalidColor)?,
)),
Some(None) => Some(None),
None => None,
},
visibility: value.visibility,
source_url: value.source_url,
source_icon: value.source_icon,
has_icon: None,
})
}
}
impl From<(u32, db::model::Version)> for Version {
fn from(value: (u32, db::model::Version)) -> Self {
let v = value.1;
Self {
id: value.0,
created_at: v.created_at,
data: v.data,
}
}
}
impl From<(String, Vec<u8>)> for VersionFile {
fn from(value: (String, Vec<u8>)) -> Self {
Self {
mime: mime_guess::from_path(&value.0)
.first()
.map(|m| m.essence_str().to_owned()),
path: value.0,
hash: value.1.encode_hex(),
}
}
}
impl VersionInfo {
pub fn get() -> Self {
Self {
version: build::PKG_VERSION.to_owned(),
commit: build::COMMIT_HASH.to_owned(),
commit_date: OffsetDateTime::from(*crate::LAST_COMMIT_DATE),
rust_version: build::RUST_VERSION.to_owned(),
build_target: build::BUILD_TARGET.to_owned(),
build_mode: build::BUILD_RUST_CHANNEL.to_owned(),
}
}
}