Skip to content

Instantly share code, notes, and snippets.

@yawboakye
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yawboakye/fd493429dba05b976a50 to your computer and use it in GitHub Desktop.
Save yawboakye/fd493429dba05b976a50 to your computer and use it in GitHub Desktop.
CREATE OR REPLACE FUNCTION fessup.fetch_posts_for_my_timeline(
fesser_id bigint,
last_id bigint DEFAULT 0,
newer boolean DEFAULT true
)
RETURNS SETOF fessup.all_posts
AS $$
WITH
feeds_i_have_blocked AS (
SELECT * FROM fessup.feed_block WHERE blocker = fesser_id
),
myself_and_other_fessers_whose_posts_i_want_to_see AS (
SELECT fesser_id UNION ALL
SELECT followee FROM fessup.following
WHERE follower = fesser_id
AND during @> now()
AND NOT EXISTS ( SELECT * FROM feeds_i_have_blocked WHERE blockee = followee )
)
SELECT *
FROM fessup.all_posts a
WHERE ((newer AND a.id > last_id) OR (NOT newer AND a.id < last_id))
AND a.author IN ( SELECT * FROM myself_and_other_fessers_whose_posts_i_want_to_see )
ORDER BY a.id DESC
LIMIT 40;
$$ LANGUAGE SQL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment