post timestamps

new-idea
Avril 4 years ago
parent 5b3aeadb6b
commit b07a320234
Signed by: flanchan
GPG Key ID: 284488987C31F630

34
Cargo.lock generated

@ -155,6 +155,20 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
dependencies = [
"libc",
"num-integer",
"num-traits",
"serde",
"time",
"winapi 0.3.9",
]
[[package]]
name = "cloudabi"
version = "0.0.3"
@ -888,6 +902,25 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "num-integer"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
dependencies = [
"autocfg 1.0.1",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
dependencies = [
"autocfg 1.0.1",
]
[[package]]
name = "num_cpus"
version = "1.13.0"
@ -1864,6 +1897,7 @@ name = "yuurei"
version = "0.1.0"
dependencies = [
"cfg-if 1.0.0",
"chrono",
"color-eyre",
"cryptohelpers",
"difference",

@ -12,6 +12,7 @@ nightly = ["smallvec/const_generics"]
[dependencies]
cfg-if = "1.0.0"
chrono = {version = "0.4", features=["serde"]}
color-eyre = {version = "0.5.10", default-features=false}
cryptohelpers = {version = "1.7.2", features=["full"]}
difference = "2.0.0"

@ -7,3 +7,19 @@ pub const POST_ID_MAX_LEN: usize = 32;
pub const TRIPCODE_SALT: khash::salt::Salt = khash::salt::Salt::internal();
/// The tripcode algorithm to use
pub const TRIPCODE_ALGO: khash::ctx::Algorithm = khash::ctx::Algorithm::Sha256;
/// What timezone to represent data in.
pub type Timezone = chrono::offset::Utc;
/// Default post expiry duration (120 days)
///
/// # Notes
/// This is specified as a std `Duration`, the chrono `Duration` calculated from the post's offset must be converted into this type to be used for anything.
/// This conversion can fail.
/// If it does fail, then the post should just be treated as expired.
pub const POST_EXPIRE: tokio::time::Duration = tokio::time::Duration::from_secs(
60 // Minute
* 60 // Hour
* 24 // Day
* 120
);

@ -802,4 +802,3 @@ pub use cancel::{
}
}
}

@ -16,6 +16,9 @@ const ID_MAX_LEN: usize = defaults::POST_ID_MAX_LEN;
/// String type that limits its bytes to the ID string max limit.
pub type IDMaxString = MaxLenString<ID_MAX_LEN>;
/// The timestamp type used in posts
pub type PostTimestamp = chrono::DateTime<defaults::Timezone>;
/// A single completed post
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Post
@ -34,10 +37,39 @@ pub struct Post
/// Hash of the body
hash: Sha256Hash,
/// When the post was created
created: PostTimestamp,
/// When the post was last edited.
///
/// # Notes
/// Each new edit is pushed to the end of the vec, creation does not count as an edit.
edited: Vec<PostTimestamp>,
/// Optional dynamic expiry duration.
expires_in: Option<tokio::time::Duration>,
}
impl Post
{
/// Time since this post was created as a Chrono `Duration`.
pub fn time_since_creation(&self) -> chrono::Duration
{
defaults::Timezone::now() - self.created
}
/// Has this post expired?
///
/// Expired posts should be removed
pub fn expired(&self) -> bool
{
if let Ok(dur) = self.time_since_creation().to_std()
{
dur >= *self.expires_in.as_ref().unwrap_or(&defaults::POST_EXPIRE)
} else {
// Conversion failed. Expire the post
true
}
}
/// The user-set name for this post if there is one.
#[inline] pub fn own_name(&self) -> Option<&str>
{
@ -85,6 +117,10 @@ mod tests
body: unsafe { super::PEMFormattedStr::new_unchecked("test").to_owned() }, //temporary
signature: None,
hash: Default::default(),
created: crate::defaults::Timezone::now(),
edited: Default::default(),
expires_in: None,
};
let post_json = serde_json::to_vec(&post).expect("Serialise");
println!("Post json: {}", std::str::from_utf8(&post_json[..]).unwrap());

Loading…
Cancel
Save