Skip to content
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
2 changes: 1 addition & 1 deletion client/src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ beforeEach(() => {
(global.fetch as any).mockResolvedValue({
ok: true,
text: async () => 'Hello from server',
json: async () => [],
json: async () => {},
});
});

Expand Down
46 changes: 27 additions & 19 deletions client/src/MapView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 () => {
Expand Down
16 changes: 14 additions & 2 deletions client/src/MapView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ type FeedPost = {
} | null;
};

type UsersNames = {
id: string;
username: string;
};

function MapUpdater({ position }: { position: [number, number] | null }) {
const map = useMap();

Expand Down Expand Up @@ -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<FeedPost[]>([]);
const [names, setNames] = useState<UsersNames[]>([]);

// Grab user's current location (for centering map)
useEffect(() => {
Expand All @@ -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);
Expand All @@ -115,13 +124,16 @@ function MapView({ latestPost, feedRefresh }: PostProp) {
<MapUpdater position={position} />

{/* Show posts from feed */}

{feedPosts.map((post) => {
const coords = parseLocationPoint(post.location);
if (!coords) return null;

return (
<Marker key={post.postid} position={coords}>
<Popup>
<b>{names.find(user => user.id === post.userid)?.username ?? "anonymous"}</b>
<br />
<b>{post.comment?.songTitle ?? "Unknown Song"}</b>
<br />
{post.comment?.artistName ?? ""}
Expand Down
30 changes: 3 additions & 27 deletions server/sql/schema.sql
Original file line number Diff line number Diff line change
@@ -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
);
Expand All @@ -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
);
49 changes: 26 additions & 23 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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");
Expand Down Expand Up @@ -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;
}
Expand Down