diff --git a/client/src/App.test.tsx b/client/src/App.test.tsx index 4753b5b..e772ec4 100644 --- a/client/src/App.test.tsx +++ b/client/src/App.test.tsx @@ -24,7 +24,7 @@ beforeEach(() => { (global.fetch as any).mockResolvedValue({ ok: true, text: async () => 'Hello from server', - json: async () => [], + json: async () => {}, }); }); diff --git a/client/src/MapView.test.tsx b/client/src/MapView.test.tsx index b9c8d8b..a829740 100644 --- a/client/src/MapView.test.tsx +++ b/client/src/MapView.test.tsx @@ -23,22 +23,25 @@ Object.defineProperty(global.navigator, 'geolocation', { value: mockGeolocation global.fetch = vi.fn(); -const mockFeedPosts = [ - { - postid: 1, - userid: 'user-1', - time: '2026-01-01', - location: '(44.565,-123.276)', - comment: { songTitle: 'Test Song', artistName: 'Test Artist', text: 'Epic' }, - }, - { - postid: 2, - userid: 'user-2', - time: '2026-01-02', - location: '(44.566,-123.277)', - comment: null, - }, -]; +const mockFeedPosts = { + posts: [ + { + postid: 1, + userid: 'user-1', + time: '2026-01-01', + location: '(44.565,-123.276)', + comment: { songTitle: 'Test Song', artistName: 'Test Artist', text: 'Epic' }, + }, + { + postid: 2, + userid: 'user-2', + time: '2026-01-02', + location: '(44.566,-123.277)', + comment: null, + }, + ], + users: [] +}; beforeEach(() => { vi.clearAllMocks(); @@ -137,9 +140,14 @@ describe('MapView', () => { it('skips render of marker for invalid location', async () => { (global.fetch as any).mockResolvedValue({ ok: true, - json: async () => [ - { postid: 3, userid: 'u', time: '', location: 'invalid', comment: null }, - ], + json: async () => { + return { + posts: [ + { postid: 3, userid: 'u', time: '', location: 'invalid', comment: null }, + ], + users: [] + } + }, }); await act(async () => { diff --git a/client/src/MapView.tsx b/client/src/MapView.tsx index 1557bd0..11891f4 100644 --- a/client/src/MapView.tsx +++ b/client/src/MapView.tsx @@ -42,6 +42,11 @@ type FeedPost = { } | null; }; +type UsersNames = { + id: string; + username: string; +}; + function MapUpdater({ position }: { position: [number, number] | null }) { const map = useMap(); @@ -71,6 +76,7 @@ function parseLocationPoint(point: string): [number, number] | null { function MapView({ latestPost, feedRefresh }: PostProp) { const [position, setPosition] = useState<[number, number] | null>(null); const [feedPosts, setFeedPosts] = useState([]); + const [names, setNames] = useState([]); // Grab user's current location (for centering map) useEffect(() => { @@ -93,9 +99,12 @@ function MapView({ latestPost, feedRefresh }: PostProp) { throw new Error(text); } - const data: FeedPost[] = await res.json(); + const data = await res.json(); console.log("Feed data from backend:", data); - setFeedPosts(data); + const posts = data.posts as FeedPost[]; + const dataNames = data.users as UsersNames[]; + setNames(dataNames); + setFeedPosts(posts); } catch (err) { console.error("Failed to fetch feed:", err); @@ -115,6 +124,7 @@ function MapView({ latestPost, feedRefresh }: PostProp) { {/* Show posts from feed */} + {feedPosts.map((post) => { const coords = parseLocationPoint(post.location); if (!coords) return null; @@ -122,6 +132,8 @@ function MapView({ latestPost, feedRefresh }: PostProp) { return ( + {names.find(user => user.id === post.userid)?.username ?? "anonymous"} +
{post.comment?.songTitle ?? "Unknown Song"}
{post.comment?.artistName ?? ""} diff --git a/server/sql/schema.sql b/server/sql/schema.sql index 297055d..9aabbb2 100644 --- a/server/sql/schema.sql +++ b/server/sql/schema.sql @@ -1,11 +1,8 @@ /* Users Table */ -DROP TABLE IF EXISTS userSettings; -CREATE TABLE userSettings ( +DROP TABLE IF EXISTS userNames; +CREATE TABLE userNames ( id UUID PRIMARY KEY, - username VARCHAR (50), - locationStatus SMALLINT NOT NULL, - nowPlayingStatus SMALLINT NOT NULL, - defaultPostStatus SMALLINT NOT NULL, + username VARCHAR (50) NOT NULL, FOREIGN KEY (id) REFERENCES auth.users ON DELETE CASCADE ); @@ -27,24 +24,3 @@ CREATE TABLE posts ( FOREIGN KEY (userID) REFERENCES auth.users ON DELETE CASCADE ); - -/* Friends Table */ -DROP TABLE IF EXISTS Friends; -CREATE TABLE Friends ( - userID1 UUID NOT NULL, - userID2 UUID NOT NULL, - - PRIMARY KEY (userID1, userID2), - FOREIGN KEY (userID1) REFERENCES auth.users ON DELETE CASCADE, - FOREIGN KEY (userID2) REFERENCES auth.users ON DELETE CASCADE -); - -/* Activity Table */ -DROP TABLE IF EXISTS UserActivity; -CREATE TABLE UserActivity ( - userID UUID NOT NULL PRIMARY KEY, - songID INT, - location POINT, - - FOREIGN KEY (userID) REFERENCES auth.users ON DELETE CASCADE -); diff --git a/server/src/app.ts b/server/src/app.ts index 6cb98a8..1174446 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -73,7 +73,7 @@ app.post("/posts", async (req: Request, res: Response) => { }); // GET feed of posts -// Returns the latest post for each friend +// Returns the latest post for all users app.get("/feed", async (req: Request, res: Response) => { // create supabase client const supabase = createClient(req, res); @@ -93,30 +93,10 @@ app.get("/feed", async (req: Request, res: Response) => { return res.status(400).send("userID query parameter is required"); } - // friends table columns: userid1, userid2 - const { data: friendsRows, error: friendErr } = await supabase - .from("friends") - .select("userid1, userid2") - .or(`userid1.eq.${userID},userid2.eq.${userID}`); - - if (friendErr) { - console.error(friendErr); - return res.status(500).send("Friends query failed"); - } - - // Friends rows gets converted into a list of friend IDs. - const friendsIDs = (friendsRows ?? []).map((row) => - row.userid1 === userID ? row.userid2 : row.userid1 - ); - - // include your own posts too - const feedUserIDs = [...new Set([userID, ...friendsIDs])]; - // Fetch posts where userid is in feedUserIDs by newest first const { data: posts, error: postsErr } = await supabase .from("posts") .select("postid, userid, time, location, comment") - .in("userid", feedUserIDs) .order("time", { ascending: false }) .limit(20); @@ -125,8 +105,25 @@ app.get("/feed", async (req: Request, res: Response) => { return res.status(500).send("Posts query failed"); } + let usersInQuery: string[] = []; + for (const post of posts) { + if (!usersInQuery.includes(post.userid)) { + usersInQuery.push(post.userid); + } + } + + const { data: usernames, error: usernamesErr } = await supabase + .from("usernames") + .select() + .in("id", usersInQuery); + + if (usernamesErr) { + console.error(usernamesErr); + return res.status(500).send("Users query failed"); + } + // Return posts to frontend - return res.status(200).json(posts ?? []); + return res.status(200).json({ posts: posts ?? [], users: usernames ?? [] }); } catch (err) { console.error(err); return res.status(500).send("Server error"); @@ -166,7 +163,13 @@ app.get("/auth/confirm", async (req: Request, res: Response) => { console.log(data, error); - if (!error) { + if (data && data.user && !error) { + const newUserData = data.user.user_metadata; + await supabase.from("usernames").insert({ + id: newUserData.sub, + username: newUserData.display_name, + }); + res.redirect(303, next); return; }