]> Untitled Git - lemmy.git/commitdiff
Adding and activitypub output for user.
authorDessalines <tyhou13@gmx.com>
Tue, 5 Mar 2019 03:52:09 +0000 (19:52 -0800)
committerDessalines <tyhou13@gmx.com>
Tue, 5 Mar 2019 03:52:09 +0000 (19:52 -0800)
API.md
server/migrations/2019-02-26-002946_create_user/up.sql
server/migrations/2019-02-27-170003_create_community/up.sql
server/migrations/2019-03-03-163336_create_post/up.sql
server/src/actions/community.rs
server/src/actions/post.rs
server/src/actions/user.rs
server/src/apub.rs [new file with mode: 0644]
server/src/lib.rs
server/src/models.rs [deleted file]
server/src/schema.rs

diff --git a/API.md b/API.md
index 8438cb9e7cdf4c1c3b9b81f89854469f8b2851b6..fb2335424269f60e26a81170da3fe164fa518f38 100644 (file)
--- a/API.md
+++ b/API.md
@@ -52,7 +52,6 @@
   "inbox": "https://rust-reddit-fediverse/api/v1/user/sally_smith/inbox",
   "outbox": "https://rust-reddit-fediverse/api/v1/user/sally_smith/outbox",
   "liked": "https://rust-reddit-fediverse/api/v1/user/sally_smith/liked",
-  "disliked": "https://rust-reddit-fediverse/api/v1/user/sally_smith/disliked",
   "following": "https://rust-reddit-fediverse/api/v1/user/sally_smith/following",
   "name": "sally_smith", 
   "preferredUsername": "Sally",
index a4ba1e8804aa8cdf77d37ce279079fe65a4571f3..577ff136ab34fc230fe2c002f23ba6979f7c4dc1 100644 (file)
@@ -5,6 +5,6 @@ create table user_ (
   password_encrypted text not null,
   email text,
   icon bytea,
-  start_time timestamp not null default now()
-);
-
+  published timestamp not null default now(),
+  updated timestamp
+)
index 692a5f06b7fd9e8c884c8c819e0eb1ea7a4587a4..30deec5b8e1e1b547c781e7d36eb943e5f07fecc 100644 (file)
@@ -1,19 +1,20 @@
 create table community (
   id serial primary key,
   name varchar(20) not null,
-  start_time timestamp not null default now()
+  published timestamp not null default now(),
+  updated timestamp
 );
 
 create table community_user (
   id serial primary key,
   community_id int references community on update cascade on delete cascade not null,
   fedi_user_id text not null,
-  start_time timestamp not null default now()
+  published timestamp not null default now()
 );
 
 create table community_follower (
   id serial primary key,
   community_id int references community on update cascade on delete cascade not null,
   fedi_user_id text not null,
-  start_time timestamp not null default now()
+  published timestamp not null default now()
 );
index 4a811fa2dd5fa64ba9269c58cab4446e2b594d9f..16ef545e83ac6af39627d35e8a322f7bcc88feb6 100644 (file)
@@ -3,20 +3,21 @@ create table post (
   name varchar(100) not null,
   url text not null,
   attributed_to text not null,
-  start_time timestamp not null default now()
+  published timestamp not null default now(),
+  updated timestamp
 );
 
 create table post_like (
   id serial primary key,
   fedi_user_id text not null,
   post_id int references post on update cascade on delete cascade,
-  start_time timestamp not null default now()
+  published timestamp not null default now()
 );
 
 create table post_dislike (
   id serial primary key,
   fedi_user_id text not null,
   post_id int references post on update cascade on delete cascade,
-  start_time timestamp not null default now()
+  published timestamp not null default now()
 );
 
index 6b0bea8d5d4c5b2efaa8b602610cd12313532429..03490369c49d3dda82124ff0d7b8574d19b0727e 100644 (file)
@@ -9,13 +9,15 @@ use {Crud, Followable, Joinable};
 pub struct Community {
   pub id: i32,
   pub name: String,
-  pub start_time: chrono::NaiveDateTime
+  pub published: chrono::NaiveDateTime,
+  pub updated: Option<chrono::NaiveDateTime>
 }
 
 #[derive(Insertable, AsChangeset, Clone, Copy)]
 #[table_name="community"]
 pub struct CommunityForm<'a> {
-    pub name: &'a str,
+  pub name: &'a str,
+  pub updated: Option<&'a chrono::NaiveDateTime>
 }
 
 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
@@ -25,7 +27,7 @@ pub struct CommunityUser {
   pub id: i32,
   pub community_id: i32,
   pub fedi_user_id: String,
-  pub start_time: chrono::NaiveDateTime
+  pub published: chrono::NaiveDateTime,
 }
 
 #[derive(Insertable, AsChangeset, Clone, Copy)]
@@ -42,7 +44,7 @@ pub struct CommunityFollower {
   pub id: i32,
   pub community_id: i32,
   pub fedi_user_id: String,
-  pub start_time: chrono::NaiveDateTime
+  pub published: chrono::NaiveDateTime,
 }
 
 #[derive(Insertable, AsChangeset, Clone, Copy)]
@@ -130,6 +132,7 @@ mod tests {
     
     let new_community = CommunityForm {
       name: "TIL".into(),
+      updated: None
     };
 
     let inserted_community = Community::create(&conn, new_community).unwrap();
@@ -137,14 +140,16 @@ mod tests {
     let expected_community = Community {
       id: inserted_community.id,
       name: "TIL".into(),
-      start_time: inserted_community.start_time
+      published: inserted_community.published,
+      updated: None
     };
 
     let new_user = UserForm {
       name: "thom".into(),
       preferred_username: None,
       password_encrypted: "nope".into(),
-      email: None
+      email: None,
+      updated: None
     };
 
     let inserted_user = User_::create(&conn, new_user).unwrap();
@@ -160,7 +165,7 @@ mod tests {
       id: inserted_community_follower.id,
       community_id: inserted_community.id,
       fedi_user_id: "test".into(),
-      start_time: inserted_community_follower.start_time
+      published: inserted_community_follower.published
     };
     
     let community_user_form = CommunityUserForm {
@@ -174,7 +179,7 @@ mod tests {
       id: inserted_community_user.id,
       community_id: inserted_community.id,
       fedi_user_id: "test".into(),
-      start_time: inserted_community_user.start_time
+      published: inserted_community_user.published
     };
 
     let read_community = Community::read(&conn, inserted_community.id);
index f939fc7c2b01df8fc18dde3b0b9c3405e3cd0879..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 (file)
@@ -1,95 +0,0 @@
-extern crate diesel;
-use schema::user_;
-use diesel::*;
-use diesel::result::Error;
-use schema::user_::dsl::*;
-
-#[derive(Queryable, PartialEq, Debug)]
-pub struct User_ {
-  pub id: i32,
-  pub name: String,
-  pub preferred_username: Option<String>,
-  pub password_encrypted: String,
-  pub email: Option<String>,
-  pub icon: Option<Vec<u8>>,
-  pub start_time: chrono::NaiveDateTime
-}
-
-#[derive(Insertable, AsChangeset, Clone, Copy)]
-#[table_name="user_"]
-pub struct NewUser<'a> {
-    pub name: &'a str,
-    pub preferred_username: Option<&'a str>,
-    pub password_encrypted: &'a str,
-    pub email: Option<&'a str>,
-}
-
-pub fn read(conn: &PgConnection, user_id: i32) -> User_ {
-  user_.find(user_id)
-    .first::<User_>(conn)
-    .expect("Error in query")
-}
-
-pub fn delete(conn: &PgConnection, user_id: i32) -> usize {
-  diesel::delete(user_.find(user_id))
-    .execute(conn)
-    .expect("Error deleting.")
-}
-
-pub fn create(conn: &PgConnection, new_user: &NewUser) -> Result<User_, Error> {
-  let mut edited_user = new_user.clone();
-  // Add the rust crypt
-  edited_user.password_encrypted = "here";
-    // edited_user.password_encrypted;
-    insert_into(user_)
-      .values(edited_user)
-      .get_result::<User_>(conn)
-}
-
-pub fn update(conn: &PgConnection, user_id: i32, new_user: &NewUser) -> User_ {
-  let mut edited_user = new_user.clone();
-  edited_user.password_encrypted = "here";
-  diesel::update(user_.find(user_id))
-    .set(edited_user)
-    .get_result::<User_>(conn)
-    .expect(&format!("Unable to find user {}", user_id))
-}
-
-#[cfg(test)]
-mod tests {
-  use establish_connection;
-  use super::*;
- #[test]
-  fn test_crud() {
-    let conn = establish_connection();
-    
-    let new_user = NewUser {
-      name: "thom".into(),
-      preferred_username: None,
-      password_encrypted: "nope".into(),
-      email: None
-    };
-
-    let inserted_user = create(&conn, &new_user).unwrap();
-
-    let expected_user = User_ {
-      id: inserted_user.id,
-      name: "thom".into(),
-      preferred_username: None,
-      password_encrypted: "here".into(),
-      email: None,
-      icon: None,
-      start_time: inserted_user.start_time
-    };
-    
-    let read_user = read(&conn, inserted_user.id);
-    let updated_user = update(&conn, inserted_user.id, &new_user);
-    let num_deleted = delete(&conn, inserted_user.id);
-
-    assert_eq!(expected_user, read_user);
-    assert_eq!(expected_user, inserted_user);
-    assert_eq!(expected_user, updated_user);
-    assert_eq!(1, num_deleted);
-
-  }
-}
index 36222f834eb21a2e10a5537014223f150aa36482..8556525f4b6e2c726a1d86048d03e697664b62ee 100644 (file)
@@ -1,10 +1,8 @@
 extern crate diesel;
-extern crate activitypub;
 use schema::user_;
 use diesel::*;
 use diesel::result::Error;
 use schema::user_::dsl::*;
-// use self::activitypub::{context, actor::Person};
 use Crud;
 
 #[derive(Queryable, Identifiable, PartialEq, Debug)]
@@ -16,7 +14,8 @@ pub struct User_ {
   pub password_encrypted: String,
   pub email: Option<String>,
   pub icon: Option<Vec<u8>>,
-  pub start_time: chrono::NaiveDateTime
+  pub published: chrono::NaiveDateTime,
+  pub updated: Option<chrono::NaiveDateTime>
 }
 
 #[derive(Insertable, AsChangeset, Clone, Copy)]
@@ -26,6 +25,7 @@ pub struct UserForm<'a> {
     pub preferred_username: Option<&'a str>,
     pub password_encrypted: &'a str,
     pub email: Option<&'a str>,
+    pub updated: Option<&'a chrono::NaiveDateTime>
 }
 
 impl<'a> Crud<UserForm<'a>> for User_ {
@@ -58,16 +58,6 @@ impl<'a> Crud<UserForm<'a>> for User_ {
   }
 }
 
-
-// TODO
-// pub fn person(user: &User_) -> Person {
-//   let mut person  = Person::default();
-//   person.object_props.set_context_object(context());
-//   person.ap_actor_props.set_preferred_username_string("set".into());
-
-//   person
-// }
-
 #[cfg(test)]
 mod tests {
   use establish_connection;
@@ -81,7 +71,8 @@ mod tests {
       name: "thom".into(),
       preferred_username: None,
       password_encrypted: "nope".into(),
-      email: None
+      email: None,
+      updated: None
     };
 
     let inserted_user = User_::create(&conn, new_user).unwrap();
@@ -93,7 +84,8 @@ mod tests {
       password_encrypted: "here".into(),
       email: None,
       icon: None,
-      start_time: inserted_user.start_time
+      published: inserted_user.published,
+      updated: None
     };
     
     let read_user = User_::read(&conn, inserted_user.id);
diff --git a/server/src/apub.rs b/server/src/apub.rs
new file mode 100644 (file)
index 0000000..4cfd108
--- /dev/null
@@ -0,0 +1,59 @@
+extern crate activitypub;
+use self::activitypub::{context, actor::Person};
+use actions::user::User_;
+
+impl User_ {
+  pub fn person(&self) -> Person {
+    use {Settings, to_datetime_utc};
+    let base_url = &format!("{}/user/{}", Settings::get().api_endpoint(), self.name);
+    let mut person  = Person::default();
+    person.object_props.set_context_object(context()).ok();
+    person.object_props.set_id_string(base_url.to_string()).ok();
+    person.object_props.set_name_string(self.name.to_owned()).ok();
+    person.object_props.set_published_utctime(to_datetime_utc(self.published)).ok();
+    if let Some(i) = self.updated {
+      person.object_props.set_updated_utctime(to_datetime_utc(i)).ok();
+    }
+    // person.object_props.summary = self.summary;
+
+    person.ap_actor_props.set_inbox_string(format!("{}/inbox", &base_url)).ok();
+    person.ap_actor_props.set_outbox_string(format!("{}/outbox", &base_url)).ok();
+    person.ap_actor_props.set_following_string(format!("{}/following", &base_url)).ok();
+    person.ap_actor_props.set_liked_string(format!("{}/liked", &base_url)).ok();
+    if let Some(i) = &self.preferred_username {
+      person.ap_actor_props.set_preferred_username_string(i.to_string()).ok();
+    }
+
+    person
+  }
+}
+
+#[cfg(test)]
+mod tests {
+  use super::activitypub::{context, actor::Person};
+  use super::User_;
+  use naive_now;
+
+ #[test]
+  fn test_person() {
+    let expected_user = User_ {
+      id: 52,
+      name: "thom".into(),
+      preferred_username: None,
+      password_encrypted: "here".into(),
+      email: None,
+      icon: None,
+      published: naive_now(),
+      updated: None
+    };
+
+    let person = expected_user.person();
+
+    // let expected_person = Person {
+    // };
+
+    assert_eq!("http://0.0.0.0/api/v1/user/thom", person.object_props.id_string().unwrap());
+
+  }
+}
+
index e8a67c3de78e30bbaf7d465448a34ecf65520ec5..e78989711a22e751ab975803f49d47fbe9f63636 100644 (file)
@@ -1,6 +1,7 @@
 #[macro_use]
 extern crate diesel;
 extern crate dotenv;
+extern crate chrono;
 
 use diesel::*;
 use diesel::pg::PgConnection;
@@ -9,7 +10,7 @@ use dotenv::dotenv;
 use std::env;
 
 pub mod schema;
-pub mod models;
+pub mod apub;
 pub mod actions;
 
 // pub trait Likeable;
@@ -30,13 +31,45 @@ pub trait Joinable<T> {
   fn leave(conn: &PgConnection, form: T) -> usize;
 }
 
-
 pub fn establish_connection() -> PgConnection {
-  dotenv().ok();
+  let db_url = Settings::get().db_url;
+  PgConnection::establish(&db_url)
+    .expect(&format!("Error connecting to {}", db_url))
+}
+
+pub struct Settings {
+  db_url: String,
+  hostname: String
+}
+
+impl Settings {
+  fn get() -> Self {
+    dotenv().ok();
+    Settings {
+      db_url: env::var("DATABASE_URL")
+        .expect("DATABASE_URL must be set"),
+      hostname: env::var("HOSTNAME").unwrap_or("http://0.0.0.0".to_string())
+    }
+  }
+  fn api_endpoint(&self) -> String {
+    format!("{}/api/v1", self.hostname)
+  }
+}
+
+use chrono::{DateTime, NaiveDateTime, Utc};
+pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime<Utc> {
+  DateTime::<Utc>::from_utc(ndt, Utc)
+}
 
-  let database_url = env::var("DATABASE_URL")
-    .expect("DATABASE_URL must be set");
-  PgConnection::establish(&database_url)
-    .expect(&format!("Error connecting to {}", database_url))
+pub fn naive_now() -> NaiveDateTime {
+  chrono::prelude::Utc::now().naive_utc()
 }
 
+#[cfg(test)]
+mod tests {
+  use Settings;
+ #[test]
+  fn test_api() {
+    assert_eq!(Settings::get().api_endpoint(), "http://0.0.0.0/api/v1");
+  }
+} 
diff --git a/server/src/models.rs b/server/src/models.rs
deleted file mode 100644 (file)
index c895f3e..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-
-// enum CommunityUserType {
-//   CREATOR = 0,
-//   MODERATOR = 1,
-//   USER = 2
-// }
-
-// impl CommunityUserType {
-//   fn from_u32(value: u32) -> CommunityUserType {
-//     match value {
-//       0 => CommunityUserType::CREATOR,
-//       1 => CommunityUserType::MODERATOR,
-//       2 => CommunityUserType::USER,
-//       _ => panic!("Unknown value: {}", value),
-//     }
-//   }
-// }
-
-
index cf90c04754dfa14ae9a6055e179e8769e69ddb64..75cbad5ba21ee257b8deadeecadc7fe5402a57db 100644 (file)
@@ -2,7 +2,8 @@ table! {
     community (id) {
         id -> Int4,
         name -> Varchar,
-        start_time -> Timestamp,
+        published -> Timestamp,
+        updated -> Nullable<Timestamp>,
     }
 }
 
@@ -11,7 +12,7 @@ table! {
         id -> Int4,
         community_id -> Int4,
         fedi_user_id -> Text,
-        start_time -> Timestamp,
+        published -> Timestamp,
     }
 }
 
@@ -20,7 +21,7 @@ table! {
         id -> Int4,
         community_id -> Int4,
         fedi_user_id -> Text,
-        start_time -> Timestamp,
+        published -> Timestamp,
     }
 }
 
@@ -30,7 +31,8 @@ table! {
         name -> Varchar,
         url -> Text,
         attributed_to -> Text,
-        start_time -> Timestamp,
+        published -> Timestamp,
+        updated -> Nullable<Timestamp>,
     }
 }
 
@@ -39,7 +41,7 @@ table! {
         id -> Int4,
         fedi_user_id -> Text,
         post_id -> Nullable<Int4>,
-        start_time -> Timestamp,
+        published -> Timestamp,
     }
 }
 
@@ -48,7 +50,7 @@ table! {
         id -> Int4,
         fedi_user_id -> Text,
         post_id -> Nullable<Int4>,
-        start_time -> Timestamp,
+        published -> Timestamp,
     }
 }
 
@@ -60,7 +62,8 @@ table! {
         password_encrypted -> Text,
         email -> Nullable<Text>,
         icon -> Nullable<Bytea>,
-        start_time -> Timestamp,
+        published -> Timestamp,
+        updated -> Nullable<Timestamp>,
     }
 }