Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add x username to profile lookup #898

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/snaplet/.snaplet/dataModel.json
Original file line number Diff line number Diff line change
Expand Up @@ -3697,6 +3697,20 @@
"isId": false,
"maxLength": null
},
{
"id": "public.profiles.x_username",
"name": "x_username",
"columnName": "x_username",
"type": "text",
"isRequired": false,
"kind": "scalar",
"isList": false,
"isGenerated": false,
"sequence": false,
"hasDefaultValue": false,
"isId": false,
"maxLength": null
},
{
"name": "users",
"type": "users",
Expand Down
1 change: 1 addition & 0 deletions supabase/database-generated.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,7 @@ export type Database = {
name: string
about: string
refcode: string
x_username: string
tag: string
address: string
chain_id: number
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
drop function if exists "public"."profile_lookup"(lookup_type lookup_type_enum, identifier text);

set check_function_bodies = off;

CREATE OR REPLACE FUNCTION public.profile_lookup(lookup_type lookup_type_enum, identifier text)
RETURNS TABLE(id uuid, avatar_url text, name text, about text, refcode text, x_username text, tag citext, address citext, chain_id integer, is_public boolean, sendid integer, all_tags text[])
LANGUAGE plpgsql
IMMUTABLE SECURITY DEFINER
AS $function$
begin
if identifier is null or identifier = '' then raise exception 'identifier cannot be null or empty'; end if;
if lookup_type is null then raise exception 'lookup_type cannot be null'; end if;
return query --
select case when p.id = ( select auth.uid() ) then p.id end as id,
p.avatar_url::text as avatar_url,
p.name::text as name,
p.about::text as about,
p.referral_code as refcode,
p.x_username as x_username,
t.name as tag,
sa.address as address,
sa.chain_id as chain_id,
case when current_setting('role')::text = 'service_role' then p.is_public
when p.is_public then true
else false end as is_public,
p.send_id as sendid,
( select array_agg(t.name::text)
from tags t
where t.user_id = p.id and t.status = 'confirmed'::tag_status ) as all_tags
from profiles p
join auth.users a on a.id = p.id
left join tags t on t.user_id = p.id and t.status = 'confirmed'::tag_status
left join send_accounts sa on sa.user_id = p.id
where ((lookup_type = 'sendid' and p.send_id::text = identifier) or
(lookup_type = 'tag' and t.name = identifier::citext) or
(lookup_type = 'refcode' and p.referral_code = identifier) or
(lookup_type = 'address' and sa.address = identifier) or
(lookup_type = 'phone' and a.phone::text = identifier))
and (p.is_public -- allow public profiles to be returned
or ( select auth.uid() ) is not null -- allow profiles to be returned if the user is authenticated
or current_setting('role')::text = 'service_role') -- allow public profiles to be returned to service role
limit 1;
end;
$function$
;

29 changes: 16 additions & 13 deletions supabase/tests/profile_lookup_test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ SELECT tests.create_supabase_user('valid_tag_user');
SELECT tests.authenticate_as_service_role();
INSERT INTO tags (user_id, name, status)
VALUES (tests.get_supabase_uid('valid_tag_user'), 'valid_tag', 'confirmed');
UPDATE profiles
SET x_username = 'x_valid_tag_user'
WHERE id = tests.get_supabase_uid('valid_tag_user');
INSERT INTO send_accounts (user_id, address, chain_id, init_code)
VALUES (
tests.get_supabase_uid('valid_tag_user'),
Expand Down Expand Up @@ -41,24 +44,24 @@ LANGUAGE plpgsql;
SELECT tests.authenticate_as('valid_tag_user');
SELECT results_eq($$
SELECT
id::uuid, avatar_url, name, about, tag, address, chain_id, is_public, sendid, all_tags FROM public.profile_lookup('tag', 'valid_tag') $$, $$
VALUES (tests.get_supabase_uid('valid_tag_user'), NULL, NULL, NULL, 'valid_tag'::citext, '0x1234567890abcdef1234567890abcdef12345678'::citext, 1, TRUE,(
id::uuid, avatar_url, name, about, x_username, tag, address, chain_id, is_public, sendid, all_tags FROM public.profile_lookup('tag', 'valid_tag') $$, $$
VALUES (tests.get_supabase_uid('valid_tag_user'), NULL, NULL, NULL, 'x_valid_tag_user', 'valid_tag'::citext, '0x1234567890abcdef1234567890abcdef12345678'::citext, 1, TRUE,(
SELECT
current_setting('vars.send_id')::int),ARRAY['valid_tag']::text[]) $$, 'Test valid tag lookup as authenticated user');
-- Test valid tag lookup as service role
SELECT tests.authenticate_as_service_role();
SELECT results_eq($$
SELECT
id::uuid, avatar_url, name, about, tag, address, chain_id, is_public, sendid, all_tags FROM public.profile_lookup('tag', 'valid_tag') $$, $$
VALUES (NULL::uuid, NULL, NULL, NULL, 'valid_tag'::citext, '0x1234567890abcdef1234567890abcdef12345678'::citext, 1, TRUE,(
id::uuid, avatar_url, name, about, x_username, tag, address, chain_id, is_public, sendid, all_tags FROM public.profile_lookup('tag', 'valid_tag') $$, $$
VALUES (NULL::uuid, NULL, NULL, NULL, 'x_valid_tag_user', 'valid_tag'::citext, '0x1234567890abcdef1234567890abcdef12345678'::citext, 1, TRUE,(
SELECT
current_setting('vars.send_id')::int),ARRAY['valid_tag']::text[]) $$, 'Test valid tag lookup as service role');
-- Test valid tag lookup as anon
SELECT tests.clear_authentication();
SELECT results_eq($$
SELECT
id::uuid, avatar_url, name, about, tag, address, chain_id, is_public, sendid, all_tags FROM public.profile_lookup('tag', 'valid_tag') $$, $$
VALUES (NULL::uuid, NULL, NULL, NULL, 'valid_tag'::citext, '0x1234567890abcdef1234567890abcdef12345678'::citext, 1, TRUE,(
id::uuid, avatar_url, name, about, x_username, tag, address, chain_id, is_public, sendid, all_tags FROM public.profile_lookup('tag', 'valid_tag') $$, $$
VALUES (NULL::uuid, NULL, NULL, NULL, 'x_valid_tag_user', 'valid_tag'::citext, '0x1234567890abcdef1234567890abcdef12345678'::citext, 1, TRUE,(
SELECT
current_setting('vars.send_id')::int),ARRAY['valid_tag']::text[]) $$, 'Test valid tag lookup as anon');
-- Start tests for is_public
Expand All @@ -73,23 +76,23 @@ WHERE
SELECT tests.authenticate_as('valid_tag_user');
SELECT results_eq($$
SELECT
id::uuid, avatar_url, name, about, tag, address, chain_id, is_public, sendid, all_tags FROM public.profile_lookup('tag', 'valid_tag') $$, $$
VALUES (tests.get_supabase_uid('valid_tag_user'), NULL, NULL, NULL, 'valid_tag'::citext, '0x1234567890abcdef1234567890abcdef12345678'::citext, 1, FALSE,(
id::uuid, avatar_url, name, about, x_username, tag, address, chain_id, is_public, sendid, all_tags FROM public.profile_lookup('tag', 'valid_tag') $$, $$
VALUES (tests.get_supabase_uid('valid_tag_user'), NULL, NULL, NULL, 'x_valid_tag_user', 'valid_tag'::citext, '0x1234567890abcdef1234567890abcdef12345678'::citext, 1, FALSE,(
SELECT
current_setting('vars.send_id')::int),ARRAY['valid_tag']::text[]) $$, 'Test valid tag lookup as authenticated user');
-- Test valid tag lookup as service role
SELECT tests.authenticate_as_service_role();
SELECT results_eq($$
SELECT
id::uuid, avatar_url, name, about, tag, address, chain_id, is_public, sendid, all_tags FROM public.profile_lookup('tag', 'valid_tag') $$, $$
VALUES (NULL::uuid, NULL, NULL, NULL, 'valid_tag'::citext, '0x1234567890abcdef1234567890abcdef12345678'::citext, 1, FALSE,(
id::uuid, avatar_url, name, about, x_username, tag, address, chain_id, is_public, sendid, all_tags FROM public.profile_lookup('tag', 'valid_tag') $$, $$
VALUES (NULL::uuid, NULL, NULL, NULL, 'x_valid_tag_user', 'valid_tag'::citext, '0x1234567890abcdef1234567890abcdef12345678'::citext, 1, FALSE,(
SELECT
current_setting('vars.send_id')::int),ARRAY['valid_tag']::text[]) $$, 'Test valid tag lookup as service role');
-- Test invalid tag lookup as anon
SELECT tests.clear_authentication();
SELECT is_empty($$
SELECT
id::uuid, avatar_url, name, about, tag, address, chain_id, is_public, sendid, all_tags FROM public.profile_lookup('tag', 'invalid_tag') $$, 'Test invalid tag lookup as anon');
id::uuid, avatar_url, name, about, x_username, tag, address, chain_id, is_public, sendid, all_tags FROM public.profile_lookup('tag', 'invalid_tag') $$, 'Test invalid tag lookup as anon');
SELECT tests.authenticate_as('valid_tag_user');
-- Test null profile_lookup call
SELECT throws_ok($$
Expand All @@ -115,8 +118,8 @@ SELECT results_eq($$
-- Test profile lookup by tag is case insensitive
SELECT results_eq($$
SELECT
id::uuid, avatar_url, name, about, tag, address, chain_id, is_public, sendid, all_tags FROM public.profile_lookup('tag', 'VALID_TAG') $$, $$
VALUES (NULL::uuid, NULL, NULL, NULL, 'valid_tag'::citext, '0x1234567890abcdef1234567890abcdef12345678'::citext, 1, FALSE,(
id::uuid, avatar_url, name, about, x_username, tag, address, chain_id, is_public, sendid, all_tags FROM public.profile_lookup('tag', 'VALID_TAG') $$, $$
VALUES (NULL::uuid, NULL, NULL, NULL, 'x_valid_tag_user', 'valid_tag'::citext, '0x1234567890abcdef1234567890abcdef12345678'::citext, 1, FALSE,(
SELECT
current_setting('vars.send_id')::int),ARRAY['valid_tag']::text[]) $$, 'Test valid tag lookup is case insensitive');

Expand Down
Loading