Skip to content

Commit

Permalink
Select contracts with unseen liked comments (manifoldmarkets#1710)
Browse files Browse the repository at this point in the history
* Select contracts with unseen comments

* Change other divisors

* Speed up unseen comments sql function

* Convert to sql
  • Loading branch information
IanPhilips authored Apr 26, 2023
1 parent add6679 commit a4f66ae
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 12 deletions.
145 changes: 143 additions & 2 deletions backend/supabase/functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ or replace function get_recommended_contracts_embeddings_from (
and close_time > (now() + interval '1 day')
and relative_dist < max_dist
order by score desc
limit n / 5
limit n / 6
), closing_soon_contracts as (
select *,
row_number() over (
Expand All @@ -162,7 +162,7 @@ or replace function get_recommended_contracts_embeddings_from (
where close_time < (now() + interval '1 day')
and relative_dist < max_dist
order by score desc
limit n / 5
limit n / 6
), combined_new_closing_soon as (
select *,
1 as result_id
Expand Down Expand Up @@ -240,6 +240,30 @@ or replace function get_recommended_contracts_embeddings_from (
order by row_num,
result_id
),
excluded_contracts as (
select contract_id
from combined_trending
union all
select contract_id
from combined_new_closing_soon
union all
select unnest(excluded_contract_ids) as contract_id
),
contracts_with_liked_comments as (
select
ac.*,
3 as result_id,
row_number() over (
order by score desc
) as row_num
from get_contracts_with_unseen_liked_comments(
array(select contract_id from available_contracts),
array(select contract_id from excluded_contracts),
uid,
n / 6
) as cwc
join available_contracts ac on ac.contract_id = cwc.contract_id
),
combined_results as (
select *,
1 as result_id2,
Expand All @@ -256,6 +280,15 @@ or replace function get_recommended_contracts_embeddings_from (
result_id
) as row_num2
from combined_new_closing_soon
union all
select
*,
3 as result_id2,
row_number() over (
order by row_num,
result_id
) as row_num2
from contracts_with_liked_comments
order by row_num2,
result_id2
)
Expand Down Expand Up @@ -608,3 +641,111 @@ select nullif(
''
)::text;
$$;


CREATE OR REPLACE FUNCTION get_reply_chain_comments_matching_contracts(contract_ids TEXT[], past_time_ms BIGINT)
RETURNS TABLE (
id text,
contract_id text,
data JSONB
) AS $$
BEGIN
RETURN QUERY
WITH matching_comments AS (
SELECT
(c1.data ->> 'id') AS id,
c1.contract_id,
c1.data
FROM
contract_comments c1
WHERE
c1.contract_id = ANY(contract_ids)
AND (c1.data -> 'createdTime')::BIGINT >= past_time_ms
),
reply_chain_comments AS (
SELECT
(c2.data ->> 'id') AS id,
c2.contract_id,
c2.data
FROM
contract_comments c2
JOIN matching_comments mc
ON c2.contract_id = mc.contract_id
AND c2.data ->> 'replyToCommentId' = mc.data ->> 'replyToCommentId'
AND c2.data->>'id' != mc.id
),
parent_comments AS (
SELECT
(c3.data ->> 'id') AS id,
c3.contract_id,
c3.data
FROM
contract_comments c3
JOIN matching_comments mc
ON c3.contract_id = mc.contract_id
AND c3.data ->> 'id' = mc.data ->> 'replyToCommentId'
)
SELECT * FROM matching_comments
UNION ALL
SELECT * FROM parent_comments
UNION ALL
SELECT * FROM reply_chain_comments;
END;
$$ LANGUAGE plpgsql;


create or replace function get_contracts_with_unseen_liked_comments(
available_contract_ids text[],
excluded_contract_ids text[],
current_user_id text,
limit_count integer
)
returns table (
contract_id text,
comment_id text,
user_id text,
data jsonb
) as $$
select
filtered_comments.contract_id,
filtered_comments.comment_id,
filtered_comments.user_id,
filtered_comments.data
from (
select distinct on (comments.contract_id)
comments.contract_id,
comments.comment_id,
comments.user_id,
comments.data,
(comments.data->>'createdTime')::bigint as created_time
from
liked_sorted_comments comments
where
comments.contract_id = any (available_contract_ids) and
comments.contract_id <> all (excluded_contract_ids)
and
not (
exists (
select 1
from user_events ue
where
ue.user_id = current_user_id and
ue.data->>'name' = 'view comment thread' and
ue.data->>'commentId' = comments.comment_id
)
or exists (
select 1
from user_events ue
where
ue.user_id = current_user_id and
ue.data->>'name' = 'view comment thread' and
ue.data->>'commentId' = comments.data->>'replyToCommentId'
)
)
order by
comments.contract_id,
created_time desc) as filtered_comments
order by
filtered_comments.created_time desc
limit limit_count;
$$ language sql;
3 changes: 3 additions & 0 deletions backend/supabase/seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,9 @@ select
using (true);

create index if not exists contract_comments_data_gin on contract_comments using GIN (data);
CREATE INDEX contract_comments_contract_id_idx ON contract_comments (contract_id);
CREATE INDEX contract_comments_data_likes_idx ON contract_comments (((data -> 'likes')::numeric));
CREATE INDEX contract_comments_data_created_time_idx ON contract_comments (((data ->> 'createdTime')::bigint));

alter table contract_comments
cluster on contract_comments_pkey;
Expand Down
14 changes: 14 additions & 0 deletions backend/supabase/views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,17 @@ create or replace view
where
visibility = 'public'
);


create view liked_sorted_comments as
SELECT
cc.contract_id,
cc.comment_id,
cc.data->>'userId' AS user_id,
cc.data
FROM
contract_comments cc
WHERE
(cc.data->'likes')::numeric >= 1
ORDER BY
(cc.data->>'createdTime')::bigint DESC;
20 changes: 10 additions & 10 deletions web/components/feed/feed-comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,8 @@ export function FeedCommentThread(props: {
const [collapseToIndex, setCollapseToIndex] = useState<number>(
collapseMiddle && threadComments.length > 2 ? threadComments.length - 2 : -1
)
const { ref } = useIsVisible(
() =>
track('view comment thread', {
contractId: contract.id,
commentId: parentComment.id,
} as CommentView),
true
)
return (
<Col className="w-full items-stretch gap-3 pb-2" ref={ref}>
<Col className="w-full items-stretch gap-3 pb-2">
<ParentFeedComment
key={parentComment.id}
contract={contract}
Expand Down Expand Up @@ -215,7 +207,14 @@ export const ParentFeedComment = memo(function ParentFeedComment(props: {
numReplies,
} = props
const { userUsername } = comment

const { ref } = useIsVisible(
() =>
track('view comment thread', {
contractId: contract.id,
commentId: comment.id,
} as CommentView),
true
)
const commentKind = userUsername === 'ManifoldDream' ? 'ub-dream-comment' : ''
return (
<FeedComment
Expand All @@ -226,6 +225,7 @@ export const ParentFeedComment = memo(function ParentFeedComment(props: {
showLike={showLike}
className={clsx('gap-2', commentKind)}
>
<div ref={ref} />
<ReplyToggle
seeReplies={seeReplies}
numComments={numReplies}
Expand Down

0 comments on commit a4f66ae

Please sign in to comment.