Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Setup some boilerplate, update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwennerberg committed Apr 23, 2020
1 parent a1d2a3d commit 2f0fb22
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 17 deletions.
82 changes: 82 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ reqwest = "0.10.4"
serde = { version = "1.0.106", features = ["derive"] }
serde_json = "1.0.51"
tokio = { version = "0.2.18", features = ["macros"] }
warp = "0.2.2"
warp = {version = "0.2.2", features = ["tls"]}
hyper = "0.13.5"
regex = "1.3.7"
ammonia = "3.1.0"
Expand Down
2 changes: 2 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
A nice big new feature would be event planning

https://github.com/seanmonstar/warp/blob/8b8c9950260ef7312e29c4af72b0753619a5ad5c/examples/tls.rs

sanitize on write to db

parse markdown?
Expand Down
34 changes: 18 additions & 16 deletions src/db/note.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
use maplit::hashset;
use super::schema::notes;
use serde::{Deserialize, Serialize};
use serde::{de::Error, Deserialize, Serialize, Deserializer};
use regex::Regex;
use ammonia;

// Statuses are note activitystream object

#[derive(Queryable, Clone, Deserialize, Serialize)]
pub struct Note {
pub struct Note { // rename RenderedNote
pub id: i32,
pub creator_id: i32,
pub creator_username: String,
pub parent_id: Option<i32>,
// deserialize wiht
pub content: String,
pub created_time: String,
}

/// Content in the DB is stored in plaintext (WILL BE)
/// We want to render it so that it is rendered in HTML
/// This basically just means escaping characters and adding
/// automatic URL parsing
fn render_content<'de, D>(deserializer: D) -> Result<String, D::Error>
where D: Deserializer<'de> {
let s: &str = Deserialize::deserialize(deserializer)?;
return Ok(parse_note_text(s));
}

#[derive(Insertable, Clone)]
#[table_name = "notes"]
pub struct NoteInput {
Expand All @@ -27,23 +37,16 @@ pub struct NoteInput {
// pub published: chrono::NaiveDateTime,
}

/// used when we get content from another server
/// Derived from the big elephant
/// https://github.com/tootsuite/mastodon/blob/master/app/lib/sanitize_config.rb
pub fn sanitize_remote_content(html_string: &str) -> String {
let ok_tags = hashset!["p", "br", "span", "a"];
let html_clean = ammonia::Builder::new()
.tags(ok_tags)
.clean(html_string)
.to_string();
// this is OK for now -- but we want to add microformats like mastodon does
html_clean
impl NoteInput {
// implement a better constructor here
}

/// used for user-input
/// Parse links -- stolen from https://git.cypr.io/oz/autolink-rust/src/branch/master/src/lib.rs
/// TODO -- sanitize before write and then render links on read
pub fn parse_note_text(text: &str) -> String {
// dont hack me
// There shouldn't be any html tags in the db, but
// Let's strip it out just in case
let html_clean = ammonia::clean_text(text);
if text.len() == 0 {
return String::new();
Expand All @@ -67,7 +70,6 @@ pub fn parse_note_text(text: &str) -> String {
let replace_str = "<a href=\"/user/$2\">$0</a>";
let people_parsed = person_regex.replace_all(&notes_parsed, &replace_str as &str).to_string();
// TODO get mentions too
println!("{}", people_parsed);
return people_parsed;
}

Expand Down

0 comments on commit 2f0fb22

Please sign in to comment.