]> ruin.nu Git - ndwebbie.git/blob - database/functions/unread_posts.sql
Only list threads with posts in the last 50 days as new and unread
[ndwebbie.git] / database / functions / unread_posts.sql
1 CREATE OR REPLACE FUNCTION unread_posts(IN uid int, OUT unread int, OUT "new" int) AS
2 $SQL$
3 SELECT count(*)::int AS unread
4         ,count(NULLIF(fp.time > (SELECT max(time) FROM forum_thread_visits WHERE uid = $1),FALSE))::int AS new
5 FROM(
6         SELECT ftid, ftv.time
7         FROM forum_threads ft
8                 LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $1)
9                         ftv USING (ftid)
10         WHERE ft.mtime > NOW() - '50 days'::interval
11                 AND COALESCE(ft.mtime > ftv.time,TRUE)
12                 AND ((fbid > 0 AND
13                                 fbid IN (SELECT fbid FROM forum_access WHERE gid IN (SELECT groups($1)))
14                         ) OR ft.ftid IN (SELECT ftid FROM forum_priv_access WHERE uid = $1)
15                 )
16         ) ft
17         JOIN forum_posts fp USING (ftid)
18 WHERE COALESCE(fp.time > ft.time, TRUE)
19 $SQL$ LANGUAGE sql STABLE;