Skip to main content

post_archiver/
post.rs

1use std::{hash::Hash, path::PathBuf};
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5#[cfg(feature = "typescript")]
6use ts_rs::TS;
7
8use crate::{
9    comment::Comment,
10    id::{FileMetaId, PostId},
11    Content, PlatformId,
12};
13
14/// A content entry
15#[cfg_attr(feature = "typescript", derive(TS))]
16#[cfg_attr(feature = "typescript", ts(export))]
17#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Hash)]
18pub struct Post {
19    pub id: PostId,
20    pub source: Option<String>,
21    pub title: String,
22    pub content: Vec<Content>,
23    pub thumb: Option<FileMetaId>,
24    pub comments: Vec<Comment>,
25    pub updated: DateTime<Utc>,
26    pub published: DateTime<Utc>,
27    pub platform: Option<PlatformId>,
28}
29
30impl Post {
31    /// The number of posts in one chunk.
32    pub const POSTS_PRE_CHUNK: u32 = 2048;
33
34    pub fn directory(post_id: PostId) -> PathBuf {
35        let id = post_id.raw();
36        let chunk = id / Self::POSTS_PRE_CHUNK;
37        let index = id % Self::POSTS_PRE_CHUNK;
38        PathBuf::from(chunk.to_string()).join(index.to_string())
39    }
40}
41
42#[cfg(feature = "utils")]
43mod definitions {
44    use crate::utils::macros::as_table;
45
46    use super::*;
47
48    as_table! {
49        "posts" => Post {
50            id: "id",
51            source: "source",
52            title: "title",
53            content: "content" => json,
54            thumb: "thumb",
55            comments: "comments" => json,
56            updated: "updated",
57            published: "published",
58            platform: "platform",
59        }
60    }
61}