Skip to content

Commit 841b135

Browse files
authored
fix profile lookup to use tag case insensitive (#883)
* fix profile lookup to use tag case insensitive * shutup graphite
1 parent de28c23 commit 841b135

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
set check_function_bodies = off;
2+
3+
CREATE OR REPLACE FUNCTION public.profile_lookup(lookup_type lookup_type_enum, identifier text)
4+
RETURNS TABLE(id uuid, avatar_url text, name text, about text, refcode text, tag citext, address citext, chain_id integer, is_public boolean, sendid integer, all_tags text[])
5+
LANGUAGE plpgsql
6+
IMMUTABLE SECURITY DEFINER
7+
AS $function$
8+
begin
9+
if identifier is null or identifier = '' then raise exception 'identifier cannot be null or empty'; end if;
10+
if lookup_type is null then raise exception 'lookup_type cannot be null'; end if;
11+
return query --
12+
select case when p.id = ( select auth.uid() ) then p.id end as id,
13+
p.avatar_url::text as avatar_url,
14+
p.name::text as name,
15+
p.about::text as about,
16+
p.referral_code as refcode,
17+
t.name as tag,
18+
sa.address as address,
19+
sa.chain_id as chain_id,
20+
case when current_setting('role')::text = 'service_role' then p.is_public
21+
when p.is_public then true
22+
else false end as is_public,
23+
p.send_id as sendid,
24+
( select array_agg(t.name::text)
25+
from tags t
26+
where t.user_id = p.id and t.status = 'confirmed'::tag_status ) as all_tags
27+
from profiles p
28+
join auth.users a on a.id = p.id
29+
left join tags t on t.user_id = p.id and t.status = 'confirmed'::tag_status
30+
left join send_accounts sa on sa.user_id = p.id
31+
where ((lookup_type = 'sendid' and p.send_id::text = identifier) or
32+
(lookup_type = 'tag' and t.name = identifier::citext) or
33+
(lookup_type = 'refcode' and p.referral_code = identifier) or
34+
(lookup_type = 'address' and sa.address = identifier) or
35+
(lookup_type = 'phone' and a.phone::text = identifier))
36+
and (p.is_public -- allow public profiles to be returned
37+
or ( select auth.uid() ) is not null -- allow profiles to be returned if the user is authenticated
38+
or current_setting('role')::text = 'service_role') -- allow public profiles to be returned to service role
39+
limit 1;
40+
end;
41+
$function$
42+
;
43+

supabase/tests/profile_lookup_test.sql

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
BEGIN;
2-
SELECT plan(10);
2+
SELECT plan(11);
33
CREATE EXTENSION "basejump-supabase_test_helpers";
44
SELECT tests.create_supabase_user('valid_tag_user');
55
SELECT tests.authenticate_as_service_role();
@@ -112,6 +112,14 @@ SELECT results_eq($$
112112
0::bigint
113113
) $$, 'Test looking up kenny_ does not return kennyl');
114114

115+
-- Test profile lookup by tag is case insensitive
116+
SELECT results_eq($$
117+
SELECT
118+
id::uuid, avatar_url, name, about, tag, address, chain_id, is_public, sendid, all_tags FROM public.profile_lookup('tag', 'VALID_TAG') $$, $$
119+
VALUES (NULL::uuid, NULL, NULL, NULL, 'valid_tag'::citext, '0x1234567890abcdef1234567890abcdef12345678'::citext, 1, FALSE,(
120+
SELECT
121+
current_setting('vars.send_id')::int),ARRAY['valid_tag']::text[]) $$, 'Test valid tag lookup is case insensitive');
122+
115123
SELECT *
116124
FROM
117125
finish();

0 commit comments

Comments
 (0)