From 0b5eb2c94617949bbaca9a57d836743286ac11a3 Mon Sep 17 00:00:00 2001 From: Bat Date: Sun, 2 Sep 2018 12:34:48 +0100 Subject: [PATCH] Add a Media model --- .../2018-09-02-111458_create_medias/down.sql | 2 ++ .../2018-09-02-111458_create_medias/up.sql | 10 +++++++ plume-models/src/lib.rs | 5 ++-- plume-models/src/medias.rs | 29 +++++++++++++++++++ plume-models/src/schema.rs | 13 +++++++++ 5 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 migrations/2018-09-02-111458_create_medias/down.sql create mode 100644 migrations/2018-09-02-111458_create_medias/up.sql create mode 100644 plume-models/src/medias.rs diff --git a/migrations/2018-09-02-111458_create_medias/down.sql b/migrations/2018-09-02-111458_create_medias/down.sql new file mode 100644 index 00000000..3ba01786 --- /dev/null +++ b/migrations/2018-09-02-111458_create_medias/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE medias; diff --git a/migrations/2018-09-02-111458_create_medias/up.sql b/migrations/2018-09-02-111458_create_medias/up.sql new file mode 100644 index 00000000..f89f448a --- /dev/null +++ b/migrations/2018-09-02-111458_create_medias/up.sql @@ -0,0 +1,10 @@ +-- Your SQL goes here +CREATE TABLE medias ( + id SERIAL PRIMARY KEY, + file_path TEXT NOT NULL DEFAULT '', + alt_text TEXT NOT NULL DEFAULT '', + is_remote BOOLEAN NOT NULL DEFAULT 'f', + remote_url TEXT, + sensitive BOOLEAN NOT NULL DEFAULT 'f', + content_warning TEXT +) diff --git a/plume-models/src/lib.rs b/plume-models/src/lib.rs index e0bd5b20..f2c80c62 100644 --- a/plume-models/src/lib.rs +++ b/plume-models/src/lib.rs @@ -78,7 +78,7 @@ fn get_next_id(conn: &PgConnection, seq: &str) -> i32 { // We cant' use currval because it may fail if nextval have never been called before let next = select(nextval(seq)).get_result::(conn).expect("Next ID fail"); if next > 1 { - select(setval(seq, next - 1)).get_result::(conn).expect("Reset ID fail"); + select(setval(seq, next - 1)).get_result::(conn).expect("Reset ID fail"); } next as i32 } @@ -87,7 +87,7 @@ fn get_next_id(conn: &PgConnection, seq: &str) -> i32 { lazy_static! { pub static ref BASE_URL: String = env::var("BASE_URL") .unwrap_or(format!("127.0.0.1:{}", env::var("ROCKET_PORT").unwrap_or(String::from("8000")))); - + pub static ref DB_URL: String = env::var("DB_URL") .unwrap_or(format!("postgres://plume:plume@localhost/{}", env::var("DB_NAME").unwrap_or(String::from("plume")))); @@ -111,6 +111,7 @@ pub mod db_conn; pub mod follows; pub mod instance; pub mod likes; +pub mod medias; pub mod mentions; pub mod notifications; pub mod post_authors; diff --git a/plume-models/src/medias.rs b/plume-models/src/medias.rs new file mode 100644 index 00000000..23c28904 --- /dev/null +++ b/plume-models/src/medias.rs @@ -0,0 +1,29 @@ +use diesel::{self, PgConnection, QueryDsl, ExpressionMethods, RunQueryDsl}; +use schema::medias; + +#[derive(Queryable)] +pub struct Media { + pub id: i32, + pub file_path: String, + pub alt_text: String, + pub is_remote: bool, + pub remote_url: Option, + pub sensitive: bool, + pub content_warning: Option +} + +#[derive(Insertable)] +#[table_name = "medias"] +pub struct NewMedia { + pub file_path: String, + pub alt_text: String, + pub is_remote: bool, + pub remote_url: Option, + pub sensitive: bool, + pub content_warning: Option +} + +impl Media { + insert!(medias, NewMedia); + get!(medias); +} diff --git a/plume-models/src/schema.rs b/plume-models/src/schema.rs index 79807097..f2967174 100644 --- a/plume-models/src/schema.rs +++ b/plume-models/src/schema.rs @@ -72,6 +72,18 @@ table! { } } +table! { + medias (id) { + id -> Int4, + file_path -> Text, + alt_text -> Text, + is_remote -> Bool, + remote_url -> Nullable, + sensitive -> Bool, + content_warning -> Nullable, + } +} + table! { mentions (id) { id -> Int4, @@ -170,6 +182,7 @@ allow_tables_to_appear_in_same_query!( follows, instances, likes, + medias, mentions, notifications, post_authors,