Skip to content

Commit

Permalink
Wasimify more models
Browse files Browse the repository at this point in the history
  • Loading branch information
SHAcollision committed Jan 24, 2025
1 parent 3fb9143 commit cf03117
Show file tree
Hide file tree
Showing 8 changed files with 420 additions and 4,456 deletions.
4,125 changes: 0 additions & 4,125 deletions a.txt

This file was deleted.

39 changes: 36 additions & 3 deletions src/models/file.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use std::str::FromStr;

use crate::{
common::timestamp,
traits::{HasPath, TimestampId, Validatable},
APP_PATH,
};
use mime::Mime;
use serde::{Deserialize, Serialize};

use std::str::FromStr;
use url::Url;

#[cfg(target_arch = "wasm32")]
use crate::traits::ToJson;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

#[cfg(feature = "openapi")]
use utoipa::ToSchema;

Expand Down Expand Up @@ -43,16 +47,45 @@ const VALID_MIME_TYPES: &[&str] = &[

/// Represents a file uploaded by the user.
/// URI: /pub/pubky.app/files/:file_id
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[derive(Deserialize, Serialize, Debug, Default, Clone)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct PubkyAppFile {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
pub name: String,
pub created_at: i64,
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
pub src: String,
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
pub content_type: String,
pub size: i64,
}

#[cfg(target_arch = "wasm32")]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
impl PubkyAppFile {
// Getters clone the data out because String/JsValue is not Copy.
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
pub fn name(&self) -> String {
self.name.clone()
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
pub fn src(&self) -> String {
self.src.clone()
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
pub fn content_type(&self) -> String {
self.content_type.clone()
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(js_name = toJson))]
pub fn json(&self) -> Result<JsValue, JsValue> {
self.to_json()
}
}

#[cfg(target_arch = "wasm32")]
impl ToJson for PubkyAppFile {}

impl PubkyAppFile {
/// Creates a new `PubkyAppFile` instance.
pub fn new(name: String, src: String, content_type: String, size: i64) -> Self {
Expand Down
2 changes: 0 additions & 2 deletions src/models/follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use serde::{Deserialize, Serialize};
use crate::traits::ToJson;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::JsValue;

#[cfg(feature = "openapi")]
use utoipa::ToSchema;
Expand Down
18 changes: 18 additions & 0 deletions src/models/mute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ use crate::{
};
use serde::{Deserialize, Serialize};

#[cfg(target_arch = "wasm32")]
use crate::traits::ToJson;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

#[cfg(feature = "openapi")]
use utoipa::ToSchema;

Expand All @@ -15,6 +20,7 @@ use utoipa::ToSchema;
///
/// `/pub/pubky.app/mutes/pxnu33x7jtpx9ar1ytsi4yxbp6a5o36gwhffs8zoxmbuptici1jy`
///
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[derive(Serialize, Deserialize, Default, Debug, Clone)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct PubkyAppMute {
Expand All @@ -29,6 +35,18 @@ impl PubkyAppMute {
}
}

#[cfg(target_arch = "wasm32")]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
impl PubkyAppMute {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(js_name = toJson))]
pub fn json(&self) -> Result<JsValue, JsValue> {
self.to_json()
}
}

#[cfg(target_arch = "wasm32")]
impl ToJson for PubkyAppMute {}

impl Validatable for PubkyAppMute {
fn validate(&self, _id: &str) -> Result<(), String> {
// TODO: additional Mute validation? E.g., validate `created_at` ?
Expand Down
56 changes: 55 additions & 1 deletion src/models/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ use url::Url;
const MAX_SHORT_CONTENT_LENGTH: usize = 1000;
const MAX_LONG_CONTENT_LENGTH: usize = 50000;

#[cfg(target_arch = "wasm32")]
use crate::traits::ToJson;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

#[cfg(feature = "openapi")]
use utoipa::ToSchema;

/// Represents the type of pubky-app posted data
/// Used primarily to best display the content in UI
#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[serde(rename_all = "lowercase")]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub enum PubkyAppPostKind {
Expand All @@ -39,11 +45,14 @@ impl fmt::Display for PubkyAppPostKind {
}

/// Represents embedded content within a post
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[derive(Serialize, Deserialize, Default, Clone)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct PubkyAppPostEmbed {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
pub kind: PubkyAppPostKind, // Kind of the embedded content
pub uri: String, // URI of the embedded content
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
pub uri: String, // URI of the embedded content
}

/// Represents raw post in homeserver with content and kind
Expand All @@ -53,18 +62,63 @@ pub struct PubkyAppPostEmbed {
/// Example URI:
///
/// `/pub/pubky.app/posts/00321FCW75ZFY`
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[derive(Serialize, Deserialize, Default, Clone)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct PubkyAppPost {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
pub content: String,
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
pub kind: PubkyAppPostKind,
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
pub parent: Option<String>, // If a reply, the URI of the parent post.
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
pub embed: Option<PubkyAppPostEmbed>,
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
pub attachments: Option<Vec<String>>,
}

#[cfg(target_arch = "wasm32")]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
impl PubkyAppPost {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
pub fn content(&self) -> String {
self.content.clone()
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
pub fn kind(&self) -> PubkyAppPostKind {
self.kind.clone()
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
pub fn parent(&self) -> Option<String> {
self.parent.clone()
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
pub fn embed(&self) -> Option<PubkyAppPostEmbed> {
self.embed.clone()
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
pub fn attachments(&self) -> Option<Vec<String>> {
self.attachments.clone()
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen(js_name = toJson))]
pub fn json(&self) -> Result<JsValue, JsValue> {
self.to_json()
}
}

#[cfg(target_arch = "wasm32")]
impl ToJson for PubkyAppPost {}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
impl PubkyAppPost {
/// Creates a new `PubkyAppPost` instance and sanitizes it.
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(constructor))]
pub fn new(
content: String,
kind: PubkyAppPostKind,
Expand Down
2 changes: 0 additions & 2 deletions src/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use url::Url;
use crate::traits::ToJson;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::JsValue;

#[cfg(feature = "openapi")]
use utoipa::ToSchema;
Expand Down
Loading

0 comments on commit cf03117

Please sign in to comment.