mirror of https://github.com/LemmyNet/lemmy
Halfway done with email, not fully working yet.
parent
198b5f10dd
commit
9f35b33dc7
@ -1,3 +1,4 @@
|
||||
/target
|
||||
.env
|
||||
.idea
|
||||
env_setup.sh
|
||||
|
@ -0,0 +1 @@
|
||||
drop table password_reset_request;
|
@ -0,0 +1,6 @@
|
||||
create table password_reset_request (
|
||||
id serial primary key,
|
||||
user_id int references user_ on update cascade on delete cascade not null,
|
||||
token_encrypted text not null,
|
||||
published timestamp not null default now()
|
||||
);
|
@ -0,0 +1,108 @@
|
||||
use super::*;
|
||||
use crate::schema::password_reset_request;
|
||||
use crate::schema::password_reset_request::dsl::*;
|
||||
|
||||
use bcrypt::{hash, DEFAULT_COST};
|
||||
|
||||
#[derive(Queryable, Identifiable, PartialEq, Debug)]
|
||||
#[table_name = "password_reset_request"]
|
||||
pub struct PasswordResetRequest {
|
||||
pub id: i32,
|
||||
pub user_id: i32,
|
||||
pub token_encrypted: String,
|
||||
pub published: chrono::NaiveDateTime,
|
||||
}
|
||||
|
||||
#[derive(Insertable, AsChangeset, Clone)]
|
||||
#[table_name = "password_reset_request"]
|
||||
pub struct PasswordResetRequestForm {
|
||||
pub user_id: i32,
|
||||
pub token_encrypted: String,
|
||||
}
|
||||
|
||||
impl Crud<PasswordResetRequestForm> for PasswordResetRequest {
|
||||
fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result<Self, Error> {
|
||||
use crate::schema::password_reset_request::dsl::*;
|
||||
password_reset_request.find(password_reset_request_id).first::<Self>(conn)
|
||||
}
|
||||
fn delete(conn: &PgConnection, password_reset_request_id: i32) -> Result<usize, Error> {
|
||||
diesel::delete(password_reset_request.find(password_reset_request_id)).execute(conn)
|
||||
}
|
||||
fn create(conn: &PgConnection, form: &PasswordResetRequestForm) -> Result<Self, Error> {
|
||||
insert_into(password_reset_request).values(form).get_result::<Self>(conn)
|
||||
}
|
||||
fn update(conn: &PgConnection, password_reset_request_id: i32, form: &PasswordResetRequestForm) -> Result<Self, Error> {
|
||||
diesel::update(password_reset_request.find(password_reset_request_id))
|
||||
.set(form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
impl PasswordResetRequest {
|
||||
pub fn create_token(conn: &PgConnection, from_user_id: i32, token: &str) -> Result<Self, Error> {
|
||||
let token_hash =
|
||||
hash(token, DEFAULT_COST).expect("Couldn't hash token");
|
||||
|
||||
let form = PasswordResetRequestForm {
|
||||
user_id: from_user_id,
|
||||
token_encrypted: token_hash,
|
||||
};
|
||||
|
||||
Self::create(&conn, &form)
|
||||
}
|
||||
pub fn read_from_token(conn: &PgConnection, token: &str) -> Result<Self, Error> {
|
||||
let token_hash =
|
||||
hash(token, DEFAULT_COST).expect("Couldn't hash token");
|
||||
|
||||
password_reset_request.filter(token_encrypted.eq(token_hash)).first::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use super::super::user::*;
|
||||
|
||||
#[test]
|
||||
fn test_crud() {
|
||||
let conn = establish_connection();
|
||||
|
||||
let new_user = UserForm {
|
||||
name: "thommy prw".into(),
|
||||
fedi_name: "rrf".into(),
|
||||
preferred_username: None,
|
||||
password_encrypted: "nope".into(),
|
||||
email: None,
|
||||
admin: false,
|
||||
banned: false,
|
||||
updated: None,
|
||||
show_nsfw: false,
|
||||
theme: "darkly".into(),
|
||||
default_sort_type: SortType::Hot as i16,
|
||||
default_listing_type: ListingType::Subscribed as i16,
|
||||
};
|
||||
|
||||
let inserted_user = User_::create(&conn, &new_user).unwrap();
|
||||
|
||||
let new_password_reset_request = PasswordResetRequestForm {
|
||||
user_id: inserted_user.id,
|
||||
token_encrypted: "no".into(),
|
||||
};
|
||||
|
||||
let inserted_password_reset_request = PasswordResetRequest::create(&conn, &new_password_reset_request).unwrap();
|
||||
|
||||
let expected_password_reset_request = PasswordResetRequest {
|
||||
id: inserted_password_reset_request.id,
|
||||
user_id: inserted_user.id,
|
||||
token_encrypted: "no".into(),
|
||||
published: inserted_password_reset_request.published,
|
||||
};
|
||||
|
||||
let read_password_reset_request = PasswordResetRequest::read(&conn, inserted_password_reset_request.id).unwrap();
|
||||
let num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
|
||||
|
||||
assert_eq!(expected_password_reset_request, read_password_reset_request);
|
||||
assert_eq!(expected_password_reset_request, inserted_password_reset_request);
|
||||
assert_eq!(1, num_deleted);
|
||||
}
|
||||
}
|
@ -1,345 +0,0 @@
|
||||
table! {
|
||||
category (id) {
|
||||
id -> Int4,
|
||||
name -> Varchar,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
comment (id) {
|
||||
id -> Int4,
|
||||
creator_id -> Int4,
|
||||
post_id -> Int4,
|
||||
parent_id -> Nullable<Int4>,
|
||||
content -> Text,
|
||||
removed -> Bool,
|
||||
read -> Bool,
|
||||
published -> Timestamp,
|
||||
updated -> Nullable<Timestamp>,
|
||||
deleted -> Bool,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
comment_like (id) {
|
||||
id -> Int4,
|
||||
user_id -> Int4,
|
||||
comment_id -> Int4,
|
||||
post_id -> Int4,
|
||||
score -> Int2,
|
||||
published -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
comment_saved (id) {
|
||||
id -> Int4,
|
||||
comment_id -> Int4,
|
||||
user_id -> Int4,
|
||||
published -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
community (id) {
|
||||
id -> Int4,
|
||||
name -> Varchar,
|
||||
title -> Varchar,
|
||||
description -> Nullable<Text>,
|
||||
category_id -> Int4,
|
||||
creator_id -> Int4,
|
||||
removed -> Bool,
|
||||
published -> Timestamp,
|
||||
updated -> Nullable<Timestamp>,
|
||||
deleted -> Bool,
|
||||
nsfw -> Bool,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
community_follower (id) {
|
||||
id -> Int4,
|
||||
community_id -> Int4,
|
||||
user_id -> Int4,
|
||||
published -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
community_moderator (id) {
|
||||
id -> Int4,
|
||||
community_id -> Int4,
|
||||
user_id -> Int4,
|
||||
published -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
community_user_ban (id) {
|
||||
id -> Int4,
|
||||
community_id -> Int4,
|
||||
user_id -> Int4,
|
||||
published -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
mod_add (id) {
|
||||
id -> Int4,
|
||||
mod_user_id -> Int4,
|
||||
other_user_id -> Int4,
|
||||
removed -> Nullable<Bool>,
|
||||
when_ -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
mod_add_community (id) {
|
||||
id -> Int4,
|
||||
mod_user_id -> Int4,
|
||||
other_user_id -> Int4,
|
||||
community_id -> Int4,
|
||||
removed -> Nullable<Bool>,
|
||||
when_ -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
mod_ban (id) {
|
||||
id -> Int4,
|
||||
mod_user_id -> Int4,
|
||||
other_user_id -> Int4,
|
||||
reason -> Nullable<Text>,
|
||||
banned -> Nullable<Bool>,
|
||||
expires -> Nullable<Timestamp>,
|
||||
when_ -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
mod_ban_from_community (id) {
|
||||
id -> Int4,
|
||||
mod_user_id -> Int4,
|
||||
other_user_id -> Int4,
|
||||
community_id -> Int4,
|
||||
reason -> Nullable<Text>,
|
||||
banned -> Nullable<Bool>,
|
||||
expires -> Nullable<Timestamp>,
|
||||
when_ -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
mod_lock_post (id) {
|
||||
id -> Int4,
|
||||
mod_user_id -> Int4,
|
||||
post_id -> Int4,
|
||||
locked -> Nullable<Bool>,
|
||||
when_ -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
mod_remove_comment (id) {
|
||||
id -> Int4,
|
||||
mod_user_id -> Int4,
|
||||
comment_id -> Int4,
|
||||
reason -> Nullable<Text>,
|
||||
removed -> Nullable<Bool>,
|
||||
when_ -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
mod_remove_community (id) {
|
||||
id -> Int4,
|
||||
mod_user_id -> Int4,
|
||||
community_id -> Int4,
|
||||
reason -> Nullable<Text>,
|
||||
removed -> Nullable<Bool>,
|
||||
expires -> Nullable<Timestamp>,
|
||||
when_ -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
mod_remove_post (id) {
|
||||
id -> Int4,
|
||||
mod_user_id -> Int4,
|
||||
post_id -> Int4,
|
||||
reason -> Nullable<Text>,
|
||||
removed -> Nullable<Bool>,
|
||||
when_ -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
mod_sticky_post (id) {
|
||||
id -> Int4,
|
||||
mod_user_id -> Int4,
|
||||
post_id -> Int4,
|
||||
stickied -> Nullable<Bool>,
|
||||
when_ -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
post (id) {
|
||||
id -> Int4,
|
||||
name -> Varchar,
|
||||
url -> Nullable<Text>,
|
||||
body -> Nullable<Text>,
|
||||
creator_id -> Int4,
|
||||
community_id -> Int4,
|
||||
removed -> Bool,
|
||||
locked -> Bool,
|
||||
published -> Timestamp,
|
||||
updated -> Nullable<Timestamp>,
|
||||
deleted -> Bool,
|
||||
nsfw -> Bool,
|
||||
stickied -> Bool,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
post_like (id) {
|
||||
id -> Int4,
|
||||
post_id -> Int4,
|
||||
user_id -> Int4,
|
||||
score -> Int2,
|
||||
published -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
post_read (id) {
|
||||
id -> Int4,
|
||||
post_id -> Int4,
|
||||
user_id -> Int4,
|
||||
published -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
post_saved (id) {
|
||||
id -> Int4,
|
||||
post_id -> Int4,
|
||||
user_id -> Int4,
|
||||
published -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
site (id) {
|
||||
id -> Int4,
|
||||
name -> Varchar,
|
||||
description -> Nullable<Text>,
|
||||
creator_id -> Int4,
|
||||
published -> Timestamp,
|
||||
updated -> Nullable<Timestamp>,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
user_ (id) {
|
||||
id -> Int4,
|
||||
name -> Varchar,
|
||||
fedi_name -> Varchar,
|
||||
preferred_username -> Nullable<Varchar>,
|
||||
password_encrypted -> Text,
|
||||
email -> Nullable<Text>,
|
||||
icon -> Nullable<Bytea>,
|
||||
admin -> Bool,
|
||||
banned -> Bool,
|
||||
published -> Timestamp,
|
||||
updated -> Nullable<Timestamp>,
|
||||
show_nsfw -> Bool,
|
||||
theme -> Varchar,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
user_ban (id) {
|
||||
id -> Int4,
|
||||
user_id -> Int4,
|
||||
published -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
user_mention (id) {
|
||||
id -> Int4,
|
||||
recipient_id -> Int4,
|
||||
comment_id -> Int4,
|
||||
read -> Bool,
|
||||
published -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
joinable!(comment -> post (post_id));
|
||||
joinable!(comment -> user_ (creator_id));
|
||||
joinable!(comment_like -> comment (comment_id));
|
||||
joinable!(comment_like -> post (post_id));
|
||||
joinable!(comment_like -> user_ (user_id));
|
||||
joinable!(comment_saved -> comment (comment_id));
|
||||
joinable!(comment_saved -> user_ (user_id));
|
||||
joinable!(community -> category (category_id));
|
||||
joinable!(community -> user_ (creator_id));
|
||||
joinable!(community_follower -> community (community_id));
|
||||
joinable!(community_follower -> user_ (user_id));
|
||||
joinable!(community_moderator -> community (community_id));
|
||||
joinable!(community_moderator -> user_ (user_id));
|
||||
joinable!(community_user_ban -> community (community_id));
|
||||
joinable!(community_user_ban -> user_ (user_id));
|
||||
joinable!(mod_add_community -> community (community_id));
|
||||
joinable!(mod_ban_from_community -> community (community_id));
|
||||
joinable!(mod_lock_post -> post (post_id));
|
||||
joinable!(mod_lock_post -> user_ (mod_user_id));
|
||||
joinable!(mod_remove_comment -> comment (comment_id));
|
||||
joinable!(mod_remove_comment -> user_ (mod_user_id));
|
||||
joinable!(mod_remove_community -> community (community_id));
|
||||
joinable!(mod_remove_community -> user_ (mod_user_id));
|
||||
joinable!(mod_remove_post -> post (post_id));
|
||||
joinable!(mod_remove_post -> user_ (mod_user_id));
|
||||
joinable!(mod_sticky_post -> post (post_id));
|
||||
joinable!(mod_sticky_post -> user_ (mod_user_id));
|
||||
joinable!(post -> community (community_id));
|
||||
joinable!(post -> user_ (creator_id));
|
||||
joinable!(post_like -> post (post_id));
|
||||
joinable!(post_like -> user_ (user_id));
|
||||
joinable!(post_read -> post (post_id));
|
||||
joinable!(post_read -> user_ (user_id));
|
||||
joinable!(post_saved -> post (post_id));
|
||||
joinable!(post_saved -> user_ (user_id));
|
||||
joinable!(site -> user_ (creator_id));
|
||||
joinable!(user_ban -> user_ (user_id));
|
||||
joinable!(user_mention -> comment (comment_id));
|
||||
joinable!(user_mention -> user_ (recipient_id));
|
||||
|
||||
allow_tables_to_appear_in_same_query!(
|
||||
category,
|
||||
comment,
|
||||
comment_like,
|
||||
comment_saved,
|
||||
community,
|
||||
community_follower,
|
||||
community_moderator,
|
||||
community_user_ban,
|
||||
mod_add,
|
||||
mod_add_community,
|
||||
mod_ban,
|
||||
mod_ban_from_community,
|
||||
mod_lock_post,
|
||||
mod_remove_comment,
|
||||
mod_remove_community,
|
||||
mod_remove_post,
|
||||
mod_sticky_post,
|
||||
post,
|
||||
post_like,
|
||||
post_read,
|
||||
post_saved,
|
||||
site,
|
||||
user_,
|
||||
user_ban,
|
||||
user_mention,
|
||||
);
|
Loading…
Reference in New Issue