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(); 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)] #[cfg(test)]
mod tests { mod tests {
use super::fuzzy_search; use super::fuzzy_search;

View file

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

View file

@ -42,10 +42,10 @@ as $$
begin begin
IF (TG_OP = 'INSERT') THEN IF (TG_OP = 'INSERT') THEN
update user_aggregates 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 ELSIF (TG_OP = 'DELETE') THEN
update user_aggregates 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; END IF;
return null; return null;
end $$; end $$;
@ -86,10 +86,10 @@ as $$
begin begin
IF (TG_OP = 'INSERT') THEN IF (TG_OP = 'INSERT') THEN
update user_aggregates 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 ELSIF (TG_OP = 'DELETE') THEN
update user_aggregates 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; END IF;
return null; return null;
end $$; end $$;

View file

@ -59,10 +59,10 @@ as $$
begin begin
IF (TG_OP = 'INSERT') THEN IF (TG_OP = 'INSERT') THEN
update community_aggregates 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 ELSIF (TG_OP = 'DELETE') THEN
update community_aggregates 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; END IF;
return null; return null;
end $$; end $$;