Skip to content

Commit

Permalink
user spotify visibility setting now respected in friend and suer endp…
Browse files Browse the repository at this point in the history
…oint.
  • Loading branch information
Likqez committed Dec 7, 2024
1 parent 07b5924 commit 0d80041
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 11 deletions.
18 changes: 11 additions & 7 deletions DB/users.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
CREATE TABLE users
(
id uuid not null references auth.users on delete cascade,
avatar_url text,
username text not null,
spotify_id text not null,
spotify_visibility boolean not null default false,
id uuid not null references auth.users on delete cascade,
avatar_url text,
username text not null,
spotify_id text not null,
spotify_visibility boolean not null default false,
created_at timestamp with time zone default now(),
daily_streak integer not null default 0,
daily_streak_updated_at timestamp with time zone not null default now(),
primary key (id)
);

ALTER TABLE users
ADD CONSTRAINT unique_username UNIQUE (username),
ADD CONSTRAINT valid_username check (username <> '' AND length(trim(username)) >= 4 AND username ~ '^[a-zA-Z0-9_]+$');
ADD CONSTRAINT valid_username check (username <> '' AND length(trim(username)) >= 4 AND
username ~ '^[a-zA-Z0-9_]+$');

CREATE INDEX idx_username ON users(username);
CREATE INDEX idx_username ON users (username);
40 changes: 38 additions & 2 deletions server/api/v1/user/[uid].get.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
export default defineEventHandler((event) => {
import {isValidUUID} from "~/server/utils/data-validation";
import {serverSupabaseServiceRole, serverSupabaseUser} from "#supabase/server";
import type {GetUserResponse} from "~/types/api/users";

export default defineEventHandler(async (event) => {
const userId = getRouterParam(event, 'uid')

return;
// Require user to be authenticated
const user = await serverSupabaseUser(event);
if (!user?.id) {
setResponseStatus(event, 401);
return {error: 'unauthenticated'};
}

if (!userId || !isValidUUID(userId)) {
setResponseStatus(event, 400);
return {error: 'Invalid user ID'};
}

// Send request
const client = serverSupabaseServiceRole(event);
const {data, error}:{ data: GetUserResponse|null, error: any} = await client.from('users').select('*').eq('id', userId).maybeSingle();

Check failure on line 22 in server/api/v1/user/[uid].get.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type

Check failure on line 22 in server/api/v1/user/[uid].get.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type

if (!data) {
setResponseStatus(event, 404);
return {error: 'User not found'};
}

// Handle errors
if (error) {
setResponseStatus(event, 500);
return {error: error.message};
}

// Hide spotify id if not visible
if (!data.spotify_visibility) {
delete data.spotify_id;
}

return data;
})
10 changes: 10 additions & 0 deletions server/api/v1/user/friends/index.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,15 @@ export default defineEventHandler(async (event) => {
return {error: error.message};
}

if(data === null) return [];

// Hide spotify id if not visible
// Yes this works even when the IDE marks it as an error
data!.forEach(friend => {
if (!friend.friend_spotify_visibility) {
delete friend.friend_spotify_id;
}
});

return data;
});
12 changes: 11 additions & 1 deletion server/utils/data-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,15 @@ export const spotifyIDRegex = /^[a-zA-Z0-9]+$/; // base62
* @returns Whether the string is a valid Spotify ID
*/
export function isValidSpotifyID(spotifyID: string): boolean {
return spotifyID.match(spotifyIDRegex) !== null; // base62
return spotifyID.match(spotifyIDRegex) !== null; // base62
}

/**
* Check if a string is a valid UUID
* @param uuid - The string to check
* @returns Whether the string is a valid UUID
*/
export function isValidUUID(uuid: string): boolean {
if(uuid.length !== 36) return false;
return uuid.match(/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/) !== null;
}
2 changes: 1 addition & 1 deletion types/api/user.friends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface GetFriendsResponse {
friend_id: string,
friend_username: string,
friend_avatar: url.URL | null,
friend_spotify_id: string,
friend_spotify_id?: string,
friend_spotify_visibility: boolean,
created_at: string
updated_at: string
Expand Down
11 changes: 11 additions & 0 deletions types/api/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type url from "node:url";

export interface GetUserResponse {
id: string,
avatar_url?: url.URL | null,
username: string,
spotify_id?: string,
spotify_visibility: boolean,
daily_streak: number,
daily_streak_updated_at: string
}

0 comments on commit 0d80041

Please sign in to comment.