tab_spaces = 2
edition="2018"
imports_layout="HorizontalVertical"
-merge_imports=true
+imports_granularity="Crate"
reorder_imports=true
pub downvotes: i64,
pub stickied: bool,
pub published: chrono::NaiveDateTime,
+ pub newest_comment_time_necro: chrono::NaiveDateTime, // A newest comment time, limited to 2 days, to prevent necrobumping
pub newest_comment_time: chrono::NaiveDateTime,
}
TopYear,
TopAll,
MostComments,
+ NewComments,
}
#[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone)]
downvotes -> Int8,
stickied -> Bool,
published -> Timestamp,
+ newest_comment_time_necro -> Timestamp,
newest_comment_time -> Timestamp,
}
}
SortType::Hot | SortType::Active => query
.order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
.then_order_by(comment_aggregates::published.desc()),
- SortType::New | SortType::MostComments => query.order_by(comment::published.desc()),
+ SortType::New | SortType::MostComments | SortType::NewComments => {
+ query.order_by(comment::published.desc())
+ }
SortType::TopAll => query.order_by(comment_aggregates::score.desc()),
SortType::TopYear => query
.filter(comment::published.gt(now - 1.years()))
query = match self.sort {
SortType::Active => query
.then_order_by(
- hot_rank(post_aggregates::score, post_aggregates::newest_comment_time).desc(),
+ hot_rank(
+ post_aggregates::score,
+ post_aggregates::newest_comment_time_necro,
+ )
+ .desc(),
)
- .then_order_by(post_aggregates::newest_comment_time.desc()),
+ .then_order_by(post_aggregates::newest_comment_time_necro.desc()),
SortType::Hot => query
.then_order_by(hot_rank(post_aggregates::score, post_aggregates::published).desc())
.then_order_by(post_aggregates::published.desc()),
SortType::New => query.then_order_by(post_aggregates::published.desc()),
SortType::MostComments => query.then_order_by(post_aggregates::comments.desc()),
+ SortType::NewComments => query.then_order_by(post_aggregates::newest_comment_time.desc()),
SortType::TopAll => query.then_order_by(post_aggregates::score.desc()),
SortType::TopYear => query
.filter(post::published.gt(now - 1.years()))
downvotes: 0,
stickied: false,
published: agg.published,
+ newest_comment_time_necro: inserted_post.published,
newest_comment_time: inserted_post.published,
},
subscribed: false,
SortType::Hot | SortType::Active => query
.order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
.then_order_by(comment_aggregates::published.desc()),
- SortType::New | SortType::MostComments => query.order_by(comment::published.desc()),
+ SortType::New | SortType::MostComments | SortType::NewComments => {
+ query.order_by(comment::published.desc())
+ }
SortType::TopAll => query.order_by(comment_aggregates::score.desc()),
SortType::TopYear => query
.filter(comment::published.gt(now - 1.years()))
SortType::Active => query
.order_by(user_aggregates::comment_score.desc())
.then_order_by(user_::published.desc()),
- SortType::New | SortType::MostComments => query.order_by(user_::published.desc()),
+ SortType::New | SortType::MostComments | SortType::NewComments => {
+ query.order_by(user_::published.desc())
+ }
SortType::TopAll => query.order_by(user_aggregates::comment_score.desc()),
SortType::TopYear => query
.filter(user_::published.gt(now - 1.years()))
--- /dev/null
+drop index idx_post_aggregates_newest_comment_time,
+idx_post_aggregates_stickied_newest_comment_time,
+idx_post_aggregates_stickied_comments;
+
+alter table post_aggregates drop column newest_comment_time;
+
+alter table post_aggregates rename column newest_comment_time_necro to newest_comment_time;
+
+create or replace function post_aggregates_comment_count()
+returns trigger language plpgsql
+as $$
+begin
+ IF (TG_OP = 'INSERT') THEN
+ update post_aggregates pa
+ set comments = comments + 1
+ where pa.post_id = NEW.post_id;
+
+ -- A 2 day necro-bump limit
+ update post_aggregates pa
+ set newest_comment_time = NEW.published
+ where pa.post_id = NEW.post_id
+ and published > ('now'::timestamp - '2 days'::interval);
+ ELSIF (TG_OP = 'DELETE') THEN
+ -- Join to post because that post may not exist anymore
+ update post_aggregates pa
+ set comments = comments - 1
+ from post p
+ where pa.post_id = p.id
+ and pa.post_id = OLD.post_id;
+ END IF;
+ return null;
+end $$;
+
--- /dev/null
+-- First rename current newest comment time to newest_comment_time_necro
+-- necro means that time is limited to 2 days, whereas newest_comment_time ignores that.
+alter table post_aggregates rename column newest_comment_time to newest_comment_time_necro;
+
+-- Add the newest_comment_time column
+alter table post_aggregates add column newest_comment_time timestamp not null default now();
+
+-- Set the current newest_comment_time based on the old ones
+update post_aggregates set newest_comment_time = newest_comment_time_necro;
+
+-- Add the indexes for this new column
+create index idx_post_aggregates_newest_comment_time on post_aggregates (newest_comment_time desc);
+create index idx_post_aggregates_stickied_newest_comment_time on post_aggregates (stickied desc, newest_comment_time desc);
+
+-- Forgot to add index w/ stickied first for most comments:
+create index idx_post_aggregates_stickied_comments on post_aggregates (stickied desc, comments desc);
+
+-- Alter the comment trigger to set the newest_comment_time, and newest_comment_time_necro
+create or replace function post_aggregates_comment_count()
+returns trigger language plpgsql
+as $$
+begin
+ IF (TG_OP = 'INSERT') THEN
+ update post_aggregates pa
+ set comments = comments + 1,
+ newest_comment_time = NEW.published
+ where pa.post_id = NEW.post_id;
+
+ -- A 2 day necro-bump limit
+ update post_aggregates pa
+ set newest_comment_time_necro = NEW.published
+ where pa.post_id = NEW.post_id
+ and published > ('now'::timestamp - '2 days'::interval);
+ ELSIF (TG_OP = 'DELETE') THEN
+ -- Join to post because that post may not exist anymore
+ update post_aggregates pa
+ set comments = comments - 1
+ from post p
+ where pa.post_id = p.id
+ and pa.post_id = OLD.post_id;
+ END IF;
+ return null;
+end $$;