]> Untitled Git - lemmy.git/blob - migrations/2021-08-16-004209_fix_remove_bots_from_aggregates/up.sql
Showing # of unread comments for posts. Fixes #2134 (#2393)
[lemmy.git] / migrations / 2021-08-16-004209_fix_remove_bots_from_aggregates / up.sql
1 -- Make sure bots aren't included in aggregate counts
2
3 create or replace function community_aggregates_activity(i text)
4 returns table(count_ bigint, community_id_ integer)
5 language plpgsql
6 as
7 $$
8 begin
9   return query 
10   select count(*), community_id
11   from (
12     select c.creator_id, p.community_id from comment c
13     inner join post p on c.post_id = p.id
14     inner join person pe on c.creator_id = pe.id
15     where c.published > ('now'::timestamp - i::interval)
16     and pe.bot_account = false
17     union
18     select p.creator_id, p.community_id from post p
19     inner join person pe on p.creator_id = pe.id
20     where p.published > ('now'::timestamp - i::interval)  
21     and pe.bot_account = false
22   ) a
23   group by community_id;
24 end;
25 $$;
26
27 create or replace function site_aggregates_activity(i text) returns integer
28     language plpgsql
29     as $$
30 declare
31    count_ integer;
32 begin
33   select count(*)
34   into count_
35   from (
36     select c.creator_id from comment c
37     inner join person u on c.creator_id = u.id
38     inner join person pe on c.creator_id = pe.id
39     where c.published > ('now'::timestamp - i::interval) 
40     and u.local = true
41     and pe.bot_account = false
42     union
43     select p.creator_id from post p
44     inner join person u on p.creator_id = u.id
45     inner join person pe on p.creator_id = pe.id
46     where p.published > ('now'::timestamp - i::interval)
47     and u.local = true
48     and pe.bot_account = false
49   ) a;
50   return count_;
51 end;
52 $$;