Skip to content

Commit

Permalink
Add post kinds (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
SHAcollision authored Aug 21, 2024
1 parent 23c131b commit 3a714e2
Show file tree
Hide file tree
Showing 6 changed files with 2,735 additions and 2,489 deletions.
5,126 changes: 2,717 additions & 2,409 deletions docker/db-migration/indexer.cypher

Large diffs are not rendered by default.

16 changes: 1 addition & 15 deletions src/models/post/bookmark.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
use crate::db::connectors::neo4j::get_neo4j_graph;
use crate::{queries, RedisOps};
use chrono::Utc;
use neo4rs::Relation;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

use super::PostStream;

#[derive(Serialize, Deserialize, ToSchema)]
#[derive(Serialize, Deserialize, ToSchema, Default)]
pub struct Bookmark {
id: String,
pub indexed_at: i64,
}

impl RedisOps for Bookmark {}

impl Default for Bookmark {
fn default() -> Self {
Self::new()
}
}

impl Bookmark {
pub fn new() -> Self {
Self {
id: String::new(),
indexed_at: Utc::now().timestamp(),
}
}

/// Retrieves counts by user ID, first trying to get from Redis, then from Neo4j if not found.
pub async fn get_by_id(
author_id: &str,
Expand Down
16 changes: 1 addition & 15 deletions src/models/post/counts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use utoipa::ToSchema;
use super::PostStream;

/// Represents total counts of relationships of a user.
#[derive(Serialize, Deserialize, ToSchema)]
#[derive(Serialize, Deserialize, ToSchema, Default)]
pub struct PostCounts {
pub tags: u32,
pub replies: u32,
Expand All @@ -15,21 +15,7 @@ pub struct PostCounts {

impl RedisOps for PostCounts {}

impl Default for PostCounts {
fn default() -> Self {
Self::new()
}
}

impl PostCounts {
pub fn new() -> Self {
Self {
tags: 0,
replies: 0,
reposts: 0,
}
}

/// Retrieves counts by user ID, first trying to get from Redis, then from Neo4j if not found.
pub async fn get_by_id(
author_id: &str,
Expand Down
32 changes: 14 additions & 18 deletions src/models/post/details.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
use crate::db::connectors::neo4j::get_neo4j_graph;
use crate::{queries, RedisOps};
use chrono::Utc;
use neo4rs::Node;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

use super::PostStream;

#[derive(Serialize, Deserialize, ToSchema, Default)]
enum PostKind {
#[default]
Short,
Long,
Image,
Video,
Link,
File,
}

/// Represents post data with content, bio, image, links, and status.
#[derive(Serialize, Deserialize, ToSchema)]
#[derive(Serialize, Deserialize, ToSchema, Default)]
pub struct PostDetails {
content: String,
pub id: String, // TODO: create Crockfordbase32 validator
pub indexed_at: i64,
pub author: String,
kind: PostKind,
uri: String,
}

impl RedisOps for PostDetails {}

impl Default for PostDetails {
fn default() -> Self {
Self::new()
}
}

impl PostDetails {
pub fn new() -> Self {
Self {
content: String::new(),
id: String::new(),
indexed_at: Utc::now().timestamp(),
author: String::new(),
uri: String::new(),
}
}

/// Retrieves post details by author ID and post ID, first trying to get from Redis, then from Neo4j if not found.
pub async fn get_by_id(
author_id: &str,
Expand All @@ -55,6 +50,7 @@ impl PostDetails {
id,
indexed_at: node.get("indexed_at").unwrap_or_default(),
author: String::from(author_id),
kind: node.get("kind").unwrap_or_default(),
}
}

Expand Down
16 changes: 1 addition & 15 deletions src/models/post/relationships.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{queries, RedisOps};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

#[derive(Serialize, Deserialize, ToSchema)]
#[derive(Serialize, Deserialize, ToSchema, Default)]
pub struct PostRelationships {
// URI of the replied post
replied: Option<String>,
Expand All @@ -15,21 +15,7 @@ pub struct PostRelationships {

impl RedisOps for PostRelationships {}

impl Default for PostRelationships {
fn default() -> Self {
Self::new()
}
}

impl PostRelationships {
pub fn new() -> Self {
Self {
replied: None,
reposted: None,
mentioned: None,
}
}

/// Retrieves post relationships by user ID, first trying to get from Redis, then from Neo4j if not found.
pub async fn get_by_id(
author_id: &str,
Expand Down
18 changes: 1 addition & 17 deletions src/models/post/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{Bookmark, PostCounts, PostDetails, PostRelationships};
use crate::models::tag::post::PostTags;

/// Represents a Pubky user with relational data including tags, counts, and relationship with a viewer.
#[derive(Serialize, Deserialize, ToSchema)]
#[derive(Serialize, Deserialize, ToSchema, Default)]
pub struct PostView {
details: PostDetails,
counts: PostCounts,
Expand All @@ -14,23 +14,7 @@ pub struct PostView {
bookmark: Option<Bookmark>,
}

impl Default for PostView {
fn default() -> Self {
Self::new()
}
}

impl PostView {
pub fn new() -> Self {
Self {
details: PostDetails::new(),
counts: PostCounts::new(),
tags: PostTags::default(),
relationships: PostRelationships::new(),
bookmark: Some(Bookmark::new()),
}
}

/// Retrieves a user ID, checking the cache first and then the graph database.
pub async fn get_by_id(
author_id: &str,
Expand Down

0 comments on commit 3a714e2

Please sign in to comment.