Skip to content

Commit

Permalink
feat/create-room (#31)
Browse files Browse the repository at this point in the history
* feat(backend): add route to create a room, and methods to add it in the database

* feat(frontend) : added a part of the frontend of the creating a room page

* fix(backend) : fixing CORS problem

* fix(backend) : fixing code smell for parameters checking in RoomPost, and reformat code with prettier

* feat(frontend) : end of the frontend before review

* feat(frontend) : Adjustement on CSS and frontend in general

* chore : Adding an endpoint for streaming services and using it in frontend

* chore : Giving directly the streaming service id to the createRoom methods of room.ts instead of the service name

* fix(frontend) : changed backend url to make them dynamic

* chore: deleting assets files of spotify and souncloud logo. Also renaming components file with maj

* fix: Using cookies to create supabase clients and retrieve id

* chore: Adding a method to create supabase client

* refactor: fixed rooms routing

* chore(deps): updated package-lock.json

* refactor(backend): changed endpoint of streaming services

* style(ui): improved style of CustomTextInput

* refactor(frontend): improved ServicesList component

* chore: added .env.local.example file

* chore: improved typing in RoomPOST route

* chore(types): changed any to actual type

---------

Co-authored-by: MAXOUXAX <[email protected]>
  • Loading branch information
TheRealSharKzy and MAXOUXAX authored Jan 15, 2024
1 parent 479b7be commit 492b585
Show file tree
Hide file tree
Showing 16 changed files with 1,683 additions and 179 deletions.
2 changes: 1 addition & 1 deletion backend/.env.local.exemple → backend/.env.local.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SUPABASE_URL=
SUPABASE_ANON_KEY=
SERVICE_ROLE=
SERVICE_ROLE=
2 changes: 1 addition & 1 deletion backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

128 changes: 128 additions & 0 deletions backend/src/room.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { FastifyRequest } from "fastify";
import { supabase } from "./server";
import { createClient } from "@supabase/supabase-js";

export async function getUserFromRequest(req: any) {
if (!process.env.SUPABASE_URL) {
throw new Error("Missing SUPABASE_URL environment variable");
}

const accesToken = req.cookies.accessToken;
const supabaseUrl = process.env.SUPABASE_URL;
const supabase = createClient(supabaseUrl, accesToken);

return await supabase.auth.getUser();
}

export async function createRoom(
name: string,
code: string,
voteSkipping: boolean,
voteSkippingNeeded: number,
maxMusicPerUser: number,
maxMusicPerUserDuration: number,
serviceId: string,
req: FastifyRequest,
) {
let configurationId: string | null = null;
let hostUserProfileId: string | null = null;

getUserFromRequest(req).then((user) => {
if (user) {
if (user.data.user) hostUserProfileId = user.data.user.id;
else return { code: 500, message: "User not found" };
} else {
return { code: 500, message: "User not found" };
}
});

// TODO : review
/*
const userProfileRes = await supabase
.from("user_profile")
.select("user_profile_id");
if (userProfileRes.error) {
return { code: code, message: userProfileRes.error };
} else {
hostUserProfileId = userProfileRes.data[0].user_profile_id;
console.log("Host user profile id retrieved");
}*/

const roomConfigRes = await supabase
.from("active_room_configurations")
.insert([
{
vote_skipping: voteSkipping,
vote_skipping_needed_percentage: voteSkippingNeeded,
max_music_count_in_queue_per_participant: maxMusicPerUser,
max_music_duration: maxMusicPerUserDuration,
},
])
.select("id");

if (roomConfigRes.error) {
return { code: roomConfigRes.status, message: roomConfigRes.error };
} else {
configurationId = roomConfigRes.data[0].id;
console.log("Room configurations created");
}

const roomRes = await supabase
.from("active_rooms")
.insert([
{
name: name,
code: code,
configuration_id: configurationId,
host_user_profile_id: hostUserProfileId,
service_id: serviceId,
},
])
.select("id");

if (roomRes.error) {
return { code: roomRes.status, message: roomRes.error };
} else {
return { code: roomRes.status, message: "Room created" };
}
}

export function endRoom(roomId: string) {
let createdAt: Date | null = null;
let configurationId: string | null = null;
let hostUserProfileId: string | null = null;

supabase
.from("active_rooms")
.delete()
.eq("id", roomId)
.select("*")
.then((res) => {
if (res.error) {
return res.error;
} else {
createdAt = res.data[0].created_at;
configurationId = res.data[0].configuration_id;
hostUserProfileId = res.data[0].host_user_profile_id;
console.log("Room ended");
}
});

supabase
.from("rooms")
.insert([
{
created_at: createdAt,
configuration_id: configurationId,
host_user_profile_id: hostUserProfileId,
},
])
.then((res) => {
if (res.error) {
return res.error;
} else {
console.log("Room added to rooms");
}
});
}
53 changes: 53 additions & 0 deletions backend/src/route/RoomPOST.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { createRoom } from "../room";
import * as repl from "repl";

interface BodyParams {
name: string;
code: string;
service: string;
voteSkipping: boolean;
voteSkippingNeeded: number;
maxMusicPerUser: number;
maxMusicPerUserDuration: number;
}

function extractFromRequest(req: FastifyRequest): BodyParams {
const bodyParams = req.body as BodyParams;
const name = bodyParams.name;
const code = bodyParams.code;
const voteSkipping = bodyParams.voteSkipping;
const voteSkippingNeeded = bodyParams.voteSkippingNeeded;
const maxMusicPerUser = bodyParams.maxMusicPerUser;
const maxMusicPerUserDuration = bodyParams.maxMusicPerUserDuration;
const serviceId = bodyParams.service;
return {
name,
code,
voteSkipping,
voteSkippingNeeded,
maxMusicPerUser,
maxMusicPerUserDuration,
service: serviceId,
};
}

export default async function RoomPOST(
req: FastifyRequest,
reply: FastifyReply
) {
const roomOptions = extractFromRequest(req);

const creationResult = await createRoom(
roomOptions.name,
roomOptions.code,
roomOptions.voteSkipping,
roomOptions.voteSkippingNeeded,
roomOptions.maxMusicPerUser,
roomOptions.maxMusicPerUserDuration,
roomOptions.service,
req
);

reply.send(creationResult);
}
18 changes: 18 additions & 0 deletions backend/src/route/StreamingServicesGET.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { supabase } from "../server";

export default function StreamingServicesGET(
req: FastifyRequest,
reply: FastifyReply
) {
supabase
.from("streaming_services")
.select("*")
.then((res) => {
if (res.error) {
reply.code(500).send({ error: res.error });
} else {
reply.send(res.data);
}
});
}
37 changes: 34 additions & 3 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type { FastifyCookieOptions } from "@fastify/cookie";
import fastifyCors from "@fastify/cors";
import { createClient } from "@supabase/supabase-js";
import { config } from "dotenv";
import fastify from "fastify";
import fastifyIO from "fastify-socket.io";
import path from "path";
import { Server } from "socket.io";
import AuthCallbackGET from "./route/AuthCallbackGET";
import AuthRedirectionGET from "./route/AuthRedirectionGET";
import type { FastifyCookieOptions } from "@fastify/cookie";
import fastifyCors from "@fastify/cors";
import { createClient } from "@supabase/supabase-js";
import RoomsGET from "./route/RoomGET";
import StreamingServicesGET from "./route/StreamingServicesGET";
import { Database } from "./types/dbTypes";

config({ path: path.resolve(__dirname, "../.env.local") });
Expand Down Expand Up @@ -37,6 +39,7 @@ server.register(require("@fastify/cookie"), {
parseOptions: {}, // options for parsing cookies
} as FastifyCookieOptions);

server.get("/rooms", RoomsGET);
server.register(fastifyCors, {
origin: [true], // or true to allow all origins
methods: ["*"], // or just ['*'] for all methods
Expand All @@ -46,6 +49,34 @@ server.register(fastifyCors, {
server.get("/auth/callback", AuthCallbackGET);
server.get("/auth/redirection", AuthRedirectionGET);

server.get("/streaming-services", StreamingServicesGET);

const createRoomSchema = {
body: {
type: "object",
required: [
"name",
"code",
"service",
"voteSkipping",
"voteSkippingNeeded",
"maxMusicPerUser",
"maxMusicPerUserDuration",
],
properties: {
name: { type: "string" },
code: { type: "string" },
service: { type: "string" },
voteSkipping: { type: "boolean" },
voteSkippingNeeded: { type: "number" },
maxMusicPerUser: { type: "number" },
maxMusicPerUserDuration: { type: "number" },
},
},
};

server.post("/createRoom", { schema: createRoomSchema }, RoomPOST);

server.ready().then(() => {
// we need to wait for the server to be ready, else `server.io` is undefined
server.io.on("connection", (socket: any) => {
Expand Down
2 changes: 1 addition & 1 deletion expo/app/(tabs)/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default function TabLayout() {
/>

<Tabs.Screen
name="Rooms"
name="rooms"
options={{
title: "Salles",
tabBarLabel: ({ color, focused, children }) => (
Expand Down
5 changes: 5 additions & 0 deletions expo/app/(tabs)/rooms/_layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Slot } from "expo-router";

export default function RoomsTabLayout() {
return <Slot />;
}
Loading

0 comments on commit 492b585

Please sign in to comment.