-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core/events): legacy handler to read posts and replies from boards
The naming `legacy` refers to code that will remain compatible with `commune-server` once I introduce breaking changes in the future. Note that this is a draft since I'm not sure what the idiomatic way of selecting rows from multiple tables without embedding them as JSON (which I think is an anti-pattern for queries).
- Loading branch information
Showing
5 changed files
with
191 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use matrix::events::space::BoardPostEvent; | ||
use serde::{Deserialize, Serialize}; | ||
use tokio_postgres::{types::Json, Error, Row}; | ||
|
||
#[derive(Deserialize)] | ||
pub struct LegacyEntity { | ||
pub event_id: String, | ||
pub json: BoardPostEvent, | ||
pub room_alias: String, | ||
pub display_name: Option<String>, | ||
pub avatar_url: Option<String>, | ||
pub replies: u32, | ||
pub slug: String, | ||
pub reactions: Vec<()>, | ||
pub edited_on: Option<u32>, | ||
|
||
// only for replies | ||
pub in_reply_to: Option<String>, | ||
pub downvotes: Option<u32>, | ||
pub upvotes: Option<u32>, | ||
#[serde(skip_deserializing, flatten)] | ||
pub metadata: Option<Metadata>, | ||
} | ||
#[derive(Deserialize)] | ||
pub struct ReactionEntity { | ||
key: String, | ||
url: String, | ||
senders: Vec<String>, | ||
} | ||
|
||
#[derive(Default, Serialize, Deserialize)] | ||
#[serde(from = "LegacyEntity")] | ||
pub struct Metadata { | ||
pub edited: bool, | ||
pub downvoted: bool, | ||
pub upvoted: bool, | ||
} | ||
|
||
impl From<LegacyEntity> for Metadata { | ||
fn from(inner: LegacyEntity) -> Self { | ||
let edited = inner.edited_on.is_some(); | ||
let downvoted = inner.downvotes.unwrap_or(0) > 0; | ||
let upvoted = inner.upvotes.unwrap_or(0) > 0; | ||
|
||
Self { | ||
edited, | ||
downvoted, | ||
upvoted, | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<Row> for LegacyEntity { | ||
type Error = Error; | ||
|
||
fn try_from(row: Row) -> Result<Self, Self::Error> { | ||
Ok(Self { | ||
event_id: row.try_get(0)?, | ||
json: row.try_get(1).map(|j: Json<BoardPostEvent>| j.0)?, | ||
room_alias: row.try_get(5)?, | ||
display_name: row.try_get(3)?, | ||
avatar_url: row.try_get(4)?, | ||
replies: 0, | ||
slug: row.try_get(6)?, | ||
reactions: vec![], | ||
edited_on: row.try_get(7).map(|n: Option<i64>| n.map(|m| m as u32))?, | ||
|
||
in_reply_to: None, | ||
downvotes: None, | ||
upvotes: None, | ||
metadata: None, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod legacy; | ||
pub mod post; | ||
pub mod reply; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use http::StatusCode; | ||
use thiserror::Error; | ||
|
||
use crate::error::HttpStatusCode; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum EventsErrorCode { | ||
#[error("Not found")] | ||
NotFound(u64), | ||
#[error("Malformed body")] | ||
MalformedBody, | ||
} | ||
|
||
impl HttpStatusCode for EventsErrorCode { | ||
fn status_code(&self) -> StatusCode { | ||
match self { | ||
EventsErrorCode::NotFound(_) => StatusCode::NOT_FOUND, | ||
EventsErrorCode::MalformedBody => StatusCode::BAD_REQUEST, | ||
} | ||
} | ||
|
||
fn error_code(&self) -> &'static str { | ||
match self { | ||
EventsErrorCode::NotFound(_) => "NOT_FOUND", | ||
EventsErrorCode::MalformedBody => "BAD_REQUEST", | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub mod service; | ||
pub mod error; | ||
|
||
/// This is a re-export of `matrix::events`. | ||
pub use matrix::events as ruma; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters