mirror of https://github.com/LemmyNet/lemmy
Adding and activitypub output for user.
parent
b1cdbf7154
commit
e8a96999b6
@ -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()
|
||||
);
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
@ -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());
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
Loading…
Reference in New Issue