feat(db): Add support for stickies in database

Adds a 'sticky' column to threads and rewrites the thread index to
take sticky markings into account when ordering threads.

Stickies are not yet highlighted in any way in the forum overview.
This commit is contained in:
Vincent Ambo 2018-04-14 17:47:31 +02:00
parent c136d34e79
commit d1c45159b9
7 changed files with 36 additions and 21 deletions

View file

@ -1 +0,0 @@
DROP VIEW thread_index;

View file

@ -1,15 +0,0 @@
-- Create a simple view that returns the list of threads ordered by
-- the last post that occured in the thread.
CREATE VIEW thread_index AS
SELECT t.id AS thread_id,
t.title AS title,
t.author_name AS author_name,
t.posted AS posted,
p.id AS post_id
FROM threads t
JOIN (SELECT DISTINCT ON (thread_id) id, thread_id
FROM posts
ORDER BY thread_id, id DESC) AS p
ON t.id = p.thread_id
ORDER BY p.id DESC;

View file

@ -0,0 +1,2 @@
DROP VIEW thread_index;
ALTER TABLE threads DROP COLUMN sticky;

View file

@ -0,0 +1,21 @@
-- Add support for stickies in threads
ALTER TABLE threads ADD COLUMN sticky BOOLEAN NOT NULL DEFAULT FALSE;
-- CREATE a simple view that returns the list of threads ordered by
-- the last post that occured in the thread.
CREATE VIEW thread_index AS
SELECT t.id AS thread_id,
t.title AS title,
t.author_name AS thread_author,
t.posted AS created,
t.sticky AS sticky,
p.id AS post_id,
p.author_name AS post_author,
p.posted AS posted
FROM threads t
JOIN (SELECT DISTINCT ON (thread_id)
id, thread_id, author_name, posted
FROM posts
ORDER BY thread_id, id DESC) AS p
ON t.id = p.thread_id
ORDER BY t.sticky DESC, p.id DESC;

View file

@ -24,6 +24,7 @@ pub struct Thread {
pub posted: DateTime<Utc>,
pub author_name: String,
pub author_email: String,
pub sticky: bool,
}
/// This struct is used as the query type for the thread index view,
@ -33,9 +34,12 @@ pub struct Thread {
pub struct ThreadIndex {
pub thread_id: i32,
pub title: String,
pub author_name: String,
pub posted: DateTime<Utc>,
pub thread_author: String,
pub created: DateTime<Utc>,
pub sticky: bool,
pub post_id: i32,
pub post_author: String,
pub posted: DateTime<Utc>,
}
#[derive(Identifiable, Queryable, Serialize, Associations)]

View file

@ -72,7 +72,7 @@ impl Handler<IndexPage> for Renderer {
id: thread.thread_id,
title: escape_html(&thread.title),
posted: thread.posted.into(),
author_name: thread.author_name,
author_name: thread.thread_author,
})
.collect();

View file

@ -16,6 +16,7 @@ table! {
posted -> Timestamptz,
author_name -> Varchar,
author_email -> Varchar,
sticky -> Bool,
}
}
@ -24,9 +25,12 @@ table! {
thread_index (thread_id){
thread_id -> Integer,
title -> Text,
author_name -> Text,
posted -> Timestamptz,
thread_author -> Text,
created -> Timestamptz,
sticky -> Bool,
post_id -> Integer,
post_author -> Text,
posted -> Timestamptz,
}
}