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/user-track #118

Merged
merged 12 commits into from
Apr 9, 2024
3 changes: 1 addition & 2 deletions backend/src/socketio/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@
}
}

async add(rawUrl: string) {
async add(rawUrl: string, accountId: string) {
Fixed Show fixed Hide fixed
if (!this.remote) return;

const trackMetadata = this.trackFactory.fromUrl(rawUrl);
if (trackMetadata === null) return;

Expand Down
9 changes: 2 additions & 7 deletions backend/src/socketio/RoomIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,11 @@ export default function onRoomWSConnection(socket: TypedSocket) {
*/
sendQueue(socket, room);

socket.on("queue:add", async (params: string) => {
await room.add(params);
socket.on("queue:add", async (rawUrl: string, accountId: string) => {
await room.add(rawUrl, accountId);
sendQueue(socket, room);
});

socket.on("queue:add", async (rawUrl: string) => {
await room.add(rawUrl);
roomSocket.emit("queue:update", Room.toJSON(room));
});

// We should check the origin of the request to prevent anyone that isn't the host from removing anything
socket.on("queue:remove", async (index: number) => {
if (Number.isSafeInteger(index)) {
Expand Down
2 changes: 2 additions & 0 deletions commons/backend-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface JSONTrack {
/** An array of all genres of this song */
genres: string[];
id: string;
votes?: string[]; // array of user ids
addedBy: string;
}

export interface RoomJSONTrack extends JSONTrack {
Expand Down
2 changes: 1 addition & 1 deletion commons/socket.io-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export interface ClientToServerEvents
extends LocalPlayerClientToServerEvents,
PlayerClientToServerEvents {
/** Add a track to the queue. */
"queue:add": (rawUrl: string) => void;
"queue:add": (rawUrl: string, accountId: string) => void;
/** Remove a track from the queue by its index. */
"queue:remove": (index: number) => void;
/** Remove a track from the queue by its link. */
Expand Down
14 changes: 10 additions & 4 deletions expo/app/(tabs)/rooms/[id]/add.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import { Text } from "../../../../components/Themed";
import Library from "../../../../components/room/Library";
import SearchedTrackItem from "../../../../components/room/SearchedTrackItem";
import { useDebounce } from "../../../../lib/useDebounce";
import { useUserProfile } from "../../../../lib/userProfile";

export default function AddTrack() {
const { id } = useLocalSearchParams();
const userProfile = useUserProfile();

const [searchBar, setSearchBar] = useState("");
const [result, setResult] = useState<JSONTrack[] | null>(null);
Expand All @@ -24,18 +26,22 @@ export default function AddTrack() {
if (debouncedSearchMusic) searchMusic();
}, [debouncedSearchMusic]);

const addMusic = (value: string) => {
const addMusic = (url: string) => {
if (!socket) return;
if (!userProfile)
return Alert.alert(
"Les utilisateurs anonymes ne sont pas autorisés à accéder à cette fonctionnalité. Veuillez vous connecter."
);
try {
socket.emit("queue:add", new URL(value).toString());
socket.emit("queue:add", new URL(url).toString(), userProfile.account_id);
} catch {
setSearchBar("");
setResult(null);
Alert.alert("Erreur, impossible d'ajouter la musique");
}
if (router.canGoBack()) return router.back();
const url = ("/(tabs)/rooms/" + id) as any;
router.push(url);
const path = ("/(tabs)/rooms/" + id) as any;
router.push(path);
};

const searchMusic = () => {
Expand Down
5 changes: 3 additions & 2 deletions expo/components/ActiveRoomView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ const ActiveRoomView: React.FC<ActiveRoomViewProps> = ({ room }) => {
setLiveRoom(data);
});

socket.on("queue:update", (data: RoomJSON) => {
setLiveRoom(data);
socket.on("queue:update", async (room: RoomJSON) => {
setLiveRoom(room);
});
}, [socket]);

Expand Down Expand Up @@ -223,6 +223,7 @@ const ActiveRoomView: React.FC<ActiveRoomViewProps> = ({ room }) => {
)) ||
false
}
addedBy={item.addedBy}
/>
)}
/>
Expand Down
19 changes: 13 additions & 6 deletions expo/components/room/TrackItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ import { Menu, MenuOptions, MenuTrigger } from "react-native-popup-menu";
import MinimalistTrackItem from "./MinimalistTrackItem";
import SocketIo from "../../lib/socketio";
import CustomMenuOption from "../CustomMenuOption";
import Avatar from "../profile/Avatar";

export default function TrackItem(prop: {
track: JSONTrack;
index: number;
roomId: string;
isMenuDisabled: boolean;
handleDislike?: () => void;
disliked?: boolean;
handleDislike: () => void;
disliked: boolean;
addedBy: string;
accountId: string;
}) {
const {
title,
Expand Down Expand Up @@ -45,10 +48,14 @@ export default function TrackItem(prop: {
artistsName={artists}
imgUrl={rawImageUrl}
profilePictureImage={
<Image
source={require("../../assets/images/album-cover.jpg")}
style={itemStyles.profileImage}
/>
prop.accountId ? (
<Avatar id={prop.accountId} style={itemStyles.profileImage} />
) : (
<Image
source={require("../../assets/images/album-cover.jpg")}
style={itemStyles.profileImage}
/>
)
}
>
<Pressable onPress={handleDislike}>
Expand Down
Loading