abstract duration parsing into utils

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-08-27 01:23:44 +00:00
parent 4d42a29c51
commit dea5fee6a3
6 changed files with 24 additions and 12 deletions

2
Cargo.lock generated
View File

@ -692,6 +692,7 @@ dependencies = [
"conduit_macros", "conduit_macros",
"const-str", "const-str",
"ctor", "ctor",
"cyborgtime",
"either", "either",
"figment", "figment",
"hardened_malloc-rs", "hardened_malloc-rs",
@ -790,7 +791,6 @@ dependencies = [
"conduit_core", "conduit_core",
"conduit_database", "conduit_database",
"const-str", "const-str",
"cyborgtime",
"futures-util", "futures-util",
"hickory-resolver", "hickory-resolver",
"http", "http",

View File

@ -176,6 +176,7 @@ pub(super) async fn delete_list(&self) -> Result<RoomMessageEventContent> {
#[admin_command] #[admin_command]
pub(super) async fn delete_past_remote_media(&self, duration: String, force: bool) -> Result<RoomMessageEventContent> { pub(super) async fn delete_past_remote_media(&self, duration: String, force: bool) -> Result<RoomMessageEventContent> {
let duration = parse_timepoint_ago(&duration)?;
let deleted_count = self let deleted_count = self
.services .services
.media .media

View File

@ -68,6 +68,7 @@ clap.workspace = true
conduit-macros.workspace = true conduit-macros.workspace = true
const-str.workspace = true const-str.workspace = true
ctor.workspace = true ctor.workspace = true
cyborgtime.workspace = true
either.workspace = true either.workspace = true
figment.workspace = true figment.workspace = true
http-body-util.workspace = true http-body-util.workspace = true

View File

@ -1,5 +1,7 @@
use std::time::{Duration, SystemTime, UNIX_EPOCH}; use std::time::{Duration, SystemTime, UNIX_EPOCH};
use crate::{err, Result};
#[inline] #[inline]
#[must_use] #[must_use]
#[allow(clippy::as_conversions, clippy::cast_possible_truncation)] #[allow(clippy::as_conversions, clippy::cast_possible_truncation)]
@ -10,6 +12,22 @@ pub fn now_millis() -> u64 {
.as_millis() as u64 .as_millis() as u64
} }
#[inline]
pub fn parse_timepoint_ago(ago: &str) -> Result<SystemTime> { timepoint_ago(parse_duration(ago)?) }
#[inline]
pub fn timepoint_ago(duration: Duration) -> Result<SystemTime> {
SystemTime::now()
.checked_sub(duration)
.ok_or_else(|| err!(Arithmetic("Duration {duration:?} is too large")))
}
#[inline]
pub fn parse_duration(duration: &str) -> Result<Duration> {
cyborgtime::parse_duration(duration)
.map_err(|error| err!("'{duration:?}' is not a valid duration string: {error:?}"))
}
#[must_use] #[must_use]
pub fn rfc2822_from_seconds(epoch: i64) -> String { pub fn rfc2822_from_seconds(epoch: i64) -> String {
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};

View File

@ -46,7 +46,6 @@ bytes.workspace = true
conduit-core.workspace = true conduit-core.workspace = true
conduit-database.workspace = true conduit-database.workspace = true
const-str.workspace = true const-str.workspace = true
cyborgtime.workspace = true
futures-util.workspace = true futures-util.workspace = true
hickory-resolver.workspace = true hickory-resolver.workspace = true
http.workspace = true http.workspace = true

View File

@ -196,14 +196,7 @@ impl Service {
/// Deletes all remote only media files in the given at or after /// Deletes all remote only media files in the given at or after
/// time/duration. Returns a u32 with the amount of media files deleted. /// time/duration. Returns a u32 with the amount of media files deleted.
pub async fn delete_all_remote_media_at_after_time(&self, time: String, force: bool) -> Result<usize> { pub async fn delete_all_remote_media_at_after_time(&self, time: SystemTime, force: bool) -> Result<usize> {
let user_duration: SystemTime = match cyborgtime::parse_duration(&time) {
Err(e) => return Err!(Database(error!("Failed to parse specified time duration: {e}"))),
Ok(duration) => SystemTime::now()
.checked_sub(duration)
.ok_or(err!(Arithmetic("Duration {duration:?} is too large")))?,
};
let all_keys = self.db.get_all_media_keys(); let all_keys = self.db.get_all_media_keys();
let mut remote_mxcs = Vec::with_capacity(all_keys.len()); let mut remote_mxcs = Vec::with_capacity(all_keys.len());
@ -271,7 +264,7 @@ impl Service {
}; };
debug!("File created at: {file_created_at:?}"); debug!("File created at: {file_created_at:?}");
if file_created_at <= user_duration { if file_created_at <= time {
debug!("File is within user duration, pushing to list of file paths and keys to delete."); debug!("File is within user duration, pushing to list of file paths and keys to delete.");
remote_mxcs.push(mxc.to_string()); remote_mxcs.push(mxc.to_string());
} }
@ -284,7 +277,7 @@ impl Service {
return Err!(Database("Did not found any eligible MXCs to delete.")); return Err!(Database("Did not found any eligible MXCs to delete."));
} }
debug_info!("Deleting media now in the past {user_duration:?}."); debug_info!("Deleting media now in the past {time:?}.");
let mut deletion_count: usize = 0; let mut deletion_count: usize = 0;
for mxc in remote_mxcs { for mxc in remote_mxcs {
let mxc: Mxc<'_> = mxc.as_str().try_into()?; let mxc: Mxc<'_> = mxc.as_str().try_into()?;