abstract duration parsing into utils
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
4d42a29c51
commit
dea5fee6a3
6 changed files with 24 additions and 12 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -692,6 +692,7 @@ dependencies = [
|
|||
"conduit_macros",
|
||||
"const-str",
|
||||
"ctor",
|
||||
"cyborgtime",
|
||||
"either",
|
||||
"figment",
|
||||
"hardened_malloc-rs",
|
||||
|
@ -790,7 +791,6 @@ dependencies = [
|
|||
"conduit_core",
|
||||
"conduit_database",
|
||||
"const-str",
|
||||
"cyborgtime",
|
||||
"futures-util",
|
||||
"hickory-resolver",
|
||||
"http",
|
||||
|
|
|
@ -176,6 +176,7 @@ pub(super) async fn delete_list(&self) -> Result<RoomMessageEventContent> {
|
|||
|
||||
#[admin_command]
|
||||
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
|
||||
.services
|
||||
.media
|
||||
|
|
|
@ -68,6 +68,7 @@ clap.workspace = true
|
|||
conduit-macros.workspace = true
|
||||
const-str.workspace = true
|
||||
ctor.workspace = true
|
||||
cyborgtime.workspace = true
|
||||
either.workspace = true
|
||||
figment.workspace = true
|
||||
http-body-util.workspace = true
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use crate::{err, Result};
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[allow(clippy::as_conversions, clippy::cast_possible_truncation)]
|
||||
|
@ -10,6 +12,22 @@ pub fn now_millis() -> 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]
|
||||
pub fn rfc2822_from_seconds(epoch: i64) -> String {
|
||||
use chrono::{DateTime, Utc};
|
||||
|
|
|
@ -46,7 +46,6 @@ bytes.workspace = true
|
|||
conduit-core.workspace = true
|
||||
conduit-database.workspace = true
|
||||
const-str.workspace = true
|
||||
cyborgtime.workspace = true
|
||||
futures-util.workspace = true
|
||||
hickory-resolver.workspace = true
|
||||
http.workspace = true
|
||||
|
|
|
@ -196,14 +196,7 @@ impl Service {
|
|||
|
||||
/// Deletes all remote only media files in the given at or after
|
||||
/// 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> {
|
||||
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")))?,
|
||||
};
|
||||
|
||||
pub async fn delete_all_remote_media_at_after_time(&self, time: SystemTime, force: bool) -> Result<usize> {
|
||||
let all_keys = self.db.get_all_media_keys();
|
||||
|
||||
let mut remote_mxcs = Vec::with_capacity(all_keys.len());
|
||||
|
@ -271,7 +264,7 @@ impl Service {
|
|||
};
|
||||
|
||||
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.");
|
||||
remote_mxcs.push(mxc.to_string());
|
||||
}
|
||||
|
@ -284,7 +277,7 @@ impl Service {
|
|||
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;
|
||||
for mxc in remote_mxcs {
|
||||
let mxc: Mxc<'_> = mxc.as_str().try_into()?;
|
||||
|
|
Loading…
Add table
Reference in a new issue