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;
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_},
// 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())
}
};
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 $$;
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 $$;
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 $$;