Adding hot rank function, possibly fixing views.

This commit is contained in:
Dessalines 2020-12-06 16:44:36 -05:00
parent 36f7b20784
commit f5bef3980a
4 changed files with 17 additions and 8 deletions

View file

@ -217,6 +217,14 @@ lazy_static! {
Regex::new(r"^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
}
pub(crate) mod functions {
use diesel::sql_types::*;
sql_function! {
fn hot_rank(score: BigInt, time: Timestamp) -> Integer;
}
}
#[cfg(test)]
mod tests {
use super::fuzzy_search;

View file

@ -2,6 +2,7 @@ use crate::{
aggregates::community_aggregates::CommunityAggregates,
category::Category,
community::{Community, CommunityFollower, CommunitySafe},
functions::hot_rank,
fuzzy_search,
limit_and_offset,
schema::{category, community, community_aggregates, community_follower, user_},
@ -253,8 +254,8 @@ impl<'a> CommunityQueryBuilder<'a> {
// Covers all other sorts, including hot
_ => {
query = query
// TODO do custom sql function for hot_rank
// .order_by(hot_rank.desc())
// TODO do custom sql function for hot_rank, make sure this works
.order_by(hot_rank(community_aggregates::subscribers, community::published).desc())
.then_order_by(community_aggregates::subscribers.desc())
}
};

View file

@ -42,10 +42,10 @@ as $$
begin
IF (TG_OP = 'INSERT') THEN
update user_aggregates
set post_count = post_count + 1 where user_id = NEW.user_id;
set post_count = post_count + 1 where user_id = NEW.creator_id;
ELSIF (TG_OP = 'DELETE') THEN
update user_aggregates
set post_count = post_count - 1 where user_id = OLD.user_id;
set post_count = post_count - 1 where user_id = OLD.creator_id;
END IF;
return null;
end $$;
@ -86,10 +86,10 @@ as $$
begin
IF (TG_OP = 'INSERT') THEN
update user_aggregates
set comment_count = comment_count + 1 where user_id = NEW.user_id;
set comment_count = comment_count + 1 where user_id = NEW.creator_id;
ELSIF (TG_OP = 'DELETE') THEN
update user_aggregates
set comment_count = comment_count - 1 where user_id = OLD.user_id;
set comment_count = comment_count - 1 where user_id = OLD.creator_id;
END IF;
return null;
end $$;

View file

@ -59,10 +59,10 @@ as $$
begin
IF (TG_OP = 'INSERT') THEN
update community_aggregates
set comments = comments + 1 where community_id = NEW.community_id;
set comments = comments + 1 from comment c join post p on p.id = c.post_id and p.id = NEW.post_id;
ELSIF (TG_OP = 'DELETE') THEN
update community_aggregates
set comments = comments - 1 where community_id = OLD.community_id;
set comments = comments - 1 from comment c join post p on p.id = c.post_id and p.id = OLD.post_id;
END IF;
return null;
end $$;