2018-04-14 16:40:56 +02:00
|
|
|
// Copyright (C) 2018 Vincent Ambo <mail@tazj.in>
|
|
|
|
//
|
2018-04-17 14:42:11 +02:00
|
|
|
// This file is part of Converse.
|
|
|
|
//
|
2018-04-14 16:40:56 +02:00
|
|
|
// Converse is free software: you can redistribute it and/or modify it
|
2018-04-17 14:42:11 +02:00
|
|
|
// under the terms of the GNU Affero General Public License as
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
2018-04-14 16:40:56 +02:00
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2018-04-17 14:42:11 +02:00
|
|
|
// Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public
|
|
|
|
// License along with this program. If not, see
|
2018-04-14 16:40:56 +02:00
|
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-04-08 15:49:19 +02:00
|
|
|
use chrono::prelude::{DateTime, Utc};
|
2018-05-01 20:49:19 +02:00
|
|
|
use schema::{users, threads, posts, simple_posts};
|
2018-04-14 20:28:30 +02:00
|
|
|
use diesel::sql_types::{Text, Integer};
|
2018-04-08 15:49:19 +02:00
|
|
|
|
2018-05-01 20:49:19 +02:00
|
|
|
/// Represents a single user in the Converse database. Converse does
|
|
|
|
/// not handle logins itself, but rather looks them up based on the
|
|
|
|
/// email address received from an OIDC provider.
|
2018-04-08 18:27:15 +02:00
|
|
|
#[derive(Identifiable, Queryable, Serialize)]
|
2018-05-01 20:49:19 +02:00
|
|
|
pub struct User {
|
|
|
|
pub id: i32,
|
|
|
|
pub name: String,
|
|
|
|
pub email: String,
|
|
|
|
pub admin: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Identifiable, Queryable, Serialize, Associations)]
|
|
|
|
#[belongs_to(User)]
|
2018-04-08 15:49:19 +02:00
|
|
|
pub struct Thread {
|
|
|
|
pub id: i32,
|
|
|
|
pub title: String,
|
|
|
|
pub posted: DateTime<Utc>,
|
2018-04-14 17:47:31 +02:00
|
|
|
pub sticky: bool,
|
2018-05-01 20:49:19 +02:00
|
|
|
pub user_id: i32,
|
2018-05-25 18:11:46 +02:00
|
|
|
pub closed: bool,
|
2018-04-08 15:49:19 +02:00
|
|
|
}
|
|
|
|
|
2018-05-01 20:49:19 +02:00
|
|
|
#[derive(Identifiable, Queryable, Serialize, Associations)]
|
|
|
|
#[belongs_to(Thread)]
|
|
|
|
#[belongs_to(User)]
|
|
|
|
pub struct Post {
|
|
|
|
pub id: i32,
|
2018-04-14 17:15:27 +02:00
|
|
|
pub thread_id: i32,
|
2018-05-01 20:49:19 +02:00
|
|
|
pub body: String,
|
2018-04-14 17:47:31 +02:00
|
|
|
pub posted: DateTime<Utc>,
|
2018-05-01 20:49:19 +02:00
|
|
|
pub user_id: i32,
|
2018-04-14 17:15:27 +02:00
|
|
|
}
|
|
|
|
|
2018-05-01 20:49:19 +02:00
|
|
|
/// This struct is used as the query result type for the simplified
|
|
|
|
/// post view, which already joins user information in the database.
|
2018-04-08 18:27:15 +02:00
|
|
|
#[derive(Identifiable, Queryable, Serialize, Associations)]
|
|
|
|
#[belongs_to(Thread)]
|
2018-05-01 20:49:19 +02:00
|
|
|
pub struct SimplePost {
|
2018-04-08 15:49:19 +02:00
|
|
|
pub id: i32,
|
2018-04-08 18:27:15 +02:00
|
|
|
pub thread_id: i32,
|
2018-04-08 15:49:19 +02:00
|
|
|
pub body: String,
|
|
|
|
pub posted: DateTime<Utc>,
|
2018-05-01 20:49:19 +02:00
|
|
|
pub user_id: i32,
|
2018-04-08 22:37:21 +02:00
|
|
|
pub author_name: String,
|
|
|
|
pub author_email: String,
|
2018-04-08 15:49:19 +02:00
|
|
|
}
|
2018-04-08 19:41:28 +02:00
|
|
|
|
2018-05-01 20:49:19 +02:00
|
|
|
/// This struct is used as the query result type for the thread index
|
|
|
|
/// view, which lists the index of threads ordered by the last post in
|
|
|
|
/// each thread.
|
|
|
|
#[derive(Queryable, Serialize)]
|
|
|
|
pub struct ThreadIndex {
|
|
|
|
pub thread_id: i32,
|
|
|
|
pub title: String,
|
|
|
|
pub thread_author: String,
|
|
|
|
pub created: DateTime<Utc>,
|
|
|
|
pub sticky: bool,
|
|
|
|
pub post_id: i32,
|
|
|
|
pub post_author: String,
|
|
|
|
pub posted: DateTime<Utc>,
|
|
|
|
}
|
|
|
|
|
2018-04-08 20:01:32 +02:00
|
|
|
#[derive(Deserialize, Insertable)]
|
2018-04-08 19:41:28 +02:00
|
|
|
#[table_name="threads"]
|
|
|
|
pub struct NewThread {
|
|
|
|
pub title: String,
|
2018-05-01 20:49:19 +02:00
|
|
|
pub user_id: i32,
|
2018-04-08 19:41:28 +02:00
|
|
|
}
|
2018-04-08 20:19:46 +02:00
|
|
|
|
2018-05-01 21:37:37 +02:00
|
|
|
#[derive(Deserialize, Insertable)]
|
|
|
|
#[table_name="users"]
|
|
|
|
pub struct NewUser {
|
|
|
|
pub email: String,
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
2018-04-08 20:19:46 +02:00
|
|
|
#[derive(Deserialize, Insertable)]
|
|
|
|
#[table_name="posts"]
|
|
|
|
pub struct NewPost {
|
|
|
|
pub thread_id: i32,
|
|
|
|
pub body: String,
|
2018-05-01 20:49:19 +02:00
|
|
|
pub user_id: i32,
|
2018-04-08 20:19:46 +02:00
|
|
|
}
|
2018-04-14 20:28:30 +02:00
|
|
|
|
|
|
|
/// This struct models the response of a full-text search query. It
|
|
|
|
/// does not use a table/schema definition struct like the other
|
|
|
|
/// tables, as no table of this type actually exists.
|
2018-04-14 22:06:30 +02:00
|
|
|
#[derive(QueryableByName, Debug, Serialize)]
|
2018-04-14 20:28:30 +02:00
|
|
|
pub struct SearchResult {
|
|
|
|
#[sql_type = "Integer"]
|
|
|
|
pub post_id: i32,
|
|
|
|
#[sql_type = "Integer"]
|
|
|
|
pub thread_id: i32,
|
|
|
|
#[sql_type = "Text"]
|
|
|
|
pub author: String,
|
|
|
|
#[sql_type = "Text"]
|
|
|
|
pub title: String,
|
|
|
|
|
|
|
|
/// Headline represents the result of Postgres' ts_headline()
|
|
|
|
/// function, which highlights search terms in the search results.
|
|
|
|
#[sql_type = "Text"]
|
|
|
|
pub headline: String,
|
|
|
|
}
|