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

feat/create-room #31

Merged
merged 21 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
3a32076
feat(backend): add route to create a room, and methods to add it in t…
TheRealSharKzy Jan 9, 2024
7d28516
feat(frontend) : added a part of the frontend of the creating a room …
TheRealSharKzy Jan 9, 2024
d2db4f6
fix(backend) : fixing CORS problem
TheRealSharKzy Jan 10, 2024
f7a86d3
fix(backend) : fixing code smell for parameters checking in RoomPost,…
TheRealSharKzy Jan 10, 2024
f62dbf5
feat(frontend) : end of the frontend before review
TheRealSharKzy Jan 10, 2024
b110fa2
feat(frontend) : Adjustement on CSS and frontend in general
TheRealSharKzy Jan 11, 2024
af00c9f
chore : Adding an endpoint for streaming services and using it in fro…
TheRealSharKzy Jan 13, 2024
561d20b
chore : Giving directly the streaming service id to the createRoom me…
TheRealSharKzy Jan 13, 2024
93341b4
fix(frontend) : changed backend url to make them dynamic
TheRealSharKzy Jan 13, 2024
7af4876
chore: deleting assets files of spotify and souncloud logo. Also rena…
TheRealSharKzy Jan 14, 2024
a8e3b87
fix: Using cookies to create supabase clients and retrieve id
TheRealSharKzy Jan 15, 2024
bfbab95
chore: Adding a method to create supabase client
TheRealSharKzy Jan 15, 2024
0ef3493
refactor: fixed rooms routing
MAXOUXAX Jan 15, 2024
1f44fa5
chore(deps): updated package-lock.json
MAXOUXAX Jan 15, 2024
4f74868
refactor(backend): changed endpoint of streaming services
MAXOUXAX Jan 15, 2024
7a0aa30
style(ui): improved style of CustomTextInput
MAXOUXAX Jan 15, 2024
b6af2d4
refactor(frontend): improved ServicesList component
MAXOUXAX Jan 15, 2024
15f11eb
chore: added .env.local.example file
MAXOUXAX Jan 15, 2024
75df51e
chore: improved typing in RoomPOST route
MAXOUXAX Jan 15, 2024
df4dc03
chore(types): changed any to actual type
MAXOUXAX Jan 15, 2024
7b7347d
Merge branch 'main' of github.com:datsmysong/app into feat/create-room
MAXOUXAX Jan 15, 2024
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 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");
MAXOUXAX marked this conversation as resolved.
Show resolved Hide resolved
}
});
}
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
Loading