Set of administration tools for 42l's infrastructure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.5 KiB

use crate::db_structs::*;
use crate::texts::*;
use chrono::prelude::*;
use chrono::Duration;
use chrono::Utc;
use crossterm::style::{style, Colorize, StyledContent, Styler};
pub struct DisplayUser {
pub id: i32,
pub username_plain: StyledContent<String>,
pub password_set: StyledContent<&'static str>,
pub notify_email: Vec<u8>,
pub has_sub_newsletter: StyledContent<&'static str>,
pub has_sub_ag: StyledContent<&'static str>,
pub has_gitea_account: StyledContent<&'static str>,
pub has_nextcloud_account: StyledContent<&'static str>,
pub status: StyledContent<&'static str>,
pub max_emails: i16,
pub max_alias: i16,
pub adh_status: StyledContent<String>,
pub adh_days: Option<i64>,
pub adhesions: Vec<Adhesions>,
}
pub struct DisplayDomain {
pub id: i32,
pub fqdn: StyledContent<String>,
pub free_access: StyledContent<&'static str>,
pub owner: String,
}
impl DisplayDomain {
pub fn from_domain(dom: &EmailDomains) -> Self {
DisplayDomain {
id: dom.id,
fqdn: style(dom.fqdn.clone()).blue().bold(),
free_access: bool_tr(dom.free_access),
owner: User::get_by_id(dom.owner).unwrap().username_plain,
}
}
}
impl DisplayUser {
pub fn from_user(user: &User) -> Self {
let adhlist = Adhesions::adhesions_by_user(user.id);
let adh_days;
let subdata = if adhlist.is_empty() {
adh_days = None;
style("Never subscribed".to_owned()).red().dim()
} else {
adh_days = Some(DisplayUser::adh_calc(adhlist.last().unwrap().adh_date));
DisplayUser::adh_format(adh_days.unwrap())
};
DisplayUser {
id: user.id,
username_plain: style(user.username_plain.clone()).blue().bold(),
password_set: bool_tr(user.password.starts_with('$')),
notify_email: user.notify_email.clone(),
has_sub_newsletter: bool_tr(user.sub_newsletter),
has_sub_ag: bool_tr(user.sub_ag),
has_gitea_account: bool_tr(user.gitea_id.is_some()),
has_nextcloud_account: bool_tr(user.nextcloud_username.is_some()),
status: user_blocked_tr(user.blocked),
max_emails: user.max_emails,
max_alias: user.max_alias,
adh_status: subdata,
adh_days,
adhesions: adhlist,
}
}
fn adh_calc(last_adh: NaiveDate) -> i64 {
let newdate = Utc.from_utc_datetime(&last_adh.and_time(NaiveTime::from_hms(0, 0, 0)))
+ Duration::days(365);
(Utc::now().date() - newdate.date()).num_days()
}
fn adh_format(adh_days: i64) -> StyledContent<String> {
match adh_days {
1 => style("ended yesterday".to_owned()).red().dim(),
0 => style("ends today".to_owned()).red().dim(),
-1 => style("ends tomorrow".to_owned()).red(),
31..=365 => style(format!("ended {} months ago", adh_days / 30))
.red()
.dim(),
2..=30 => style(format!("ended {} days ago", adh_days)).red().dim(),
_ if adh_days > 365 => style(format!("ended {} years ago", adh_days / 365))
.red()
.dim(),
-30..=-2 => style(format!("ends in {} days", adh_days.abs())).yellow(),
-365..=-31 => style(format!("ends in {} months", adh_days.abs() / 30)).green(),
_ => style(format!("ends in {} years", adh_days.abs() / 365)).green(),
}
.bold()
}
}