more post expiry functions

new-idea
Avril 4 years ago
parent d72df0c9b2
commit a85286edb9
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -352,7 +352,7 @@ mod tests
#[macro_export] macro_rules! id_type {
($name:ident $(; $doc:literal)?) => {
$(#[doc(comment=$doc)])?
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, ::serde::Serialize, ::serde::Deserialize)]
#[derive(Copy, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, ::serde::Serialize, ::serde::Deserialize)]
pub struct $name(uuid::Uuid);
impl $name

@ -26,7 +26,7 @@ pub type PostBodyString = hard_format::FormattedString<PostBodyFormat>;
pub type PostBodyStr = hard_format::FormattedStr<PostBodyFormat>;
/// Identifiers for a post
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default)]
pub struct Ident
{
name: Option<IDMaxString>,
@ -66,13 +66,40 @@ pub struct Post
expires_in: Option<tokio::time::Duration>,
}
// Time based functions
impl Post
{
/// Time since this post was created as a Chrono `Duration`.
/// The timestamp when the post should expire.
///
/// # Notes
/// If the conversion overflows, then the expiry time is treated as 0. (i.e. the post expires immediately).
pub fn expires_at(&self) -> PostTimestamp
{
self.created + chrono::Duration::from_std(self.expires_in.unwrap_or(defaults::POST_EXPIRE))
.unwrap_or(chrono::Duration::zero())
}
/// How long until the post has until it reaches its expiry time from its creation time.
///
/// If this value is *lower* than the value of `time_since_creation`, then the post has expired.
pub fn expires_in(&self) -> chrono::Duration
{
self.expires_at() - self.created
}
/// Time passed since this post was created as a Chrono `Duration`.
pub fn time_since_creation(&self) -> chrono::Duration
{
defaults::Timezone::now() - self.created
}
/// The timestamp for when this post was created
pub fn created(&self) -> PostTimestamp
{
self.created
}
/// A slice of timestamps showing when this post was edited, in order of those edits.
pub fn edited(&self) -> &[PostTimestamp]
{
&self.edited[..]
}
/// Has this post expired?
///
/// Expired posts should be removed
@ -86,6 +113,15 @@ impl Post
true
}
}
}
// Ident based functions
impl Post
{
/// This post's unique identifier
#[inline] pub fn post_id(&self) -> &PostID
{
&self.id
}
/// The user-set name for this post if there is one.
#[inline] pub fn own_name(&self) -> Option<&str>
{
@ -137,7 +173,7 @@ mod tests
email: None,
tripcode: Some(super::Tripcode::generate("uhh hello").unwrap()),
},
body: unsafe { super::PostBodyStr::new_unchecked("test").to_owned() }, //temporary
body: unsafe { super::PostBodyStr::new_unchecked("test").to_owned() }, //TODO: temporary
signature: None,
hash: Default::default(),
@ -145,10 +181,11 @@ mod tests
edited: Default::default(),
expires_in: None,
};
println!("Post is: {:?}", post);
let post_json = serde_json::to_vec(&post).expect("Serialise");
println!("Post json: {}", std::str::from_utf8(&post_json[..]).unwrap());
let post2: super::Post = serde_json::from_slice(&post_json[..]).expect("Deserialise");
assert_eq!(post, post2);
println!("Post was: {:?}", post);
println!("Post was: {:?}", post2);
}
}

Loading…
Cancel
Save