You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.5 KiB
JavaScript

import { SessionStore, UserStore } from "../db/schemas.mjs";
import { getUserResource } from "../resources/user.mjs";
export const applyUserRoutes = (router) => {
applyUserRoutesPublic(router);
router.get('/me/currentlyPlaying', async (req, res) => {
const userLocal = await res.locals.user.spotify.local;
if (!userLocal.player) {
res.status(500);
res.send({ message: 'player is unavailable' });
}
const currentlyPlaying = await userLocal.player.getCurrentlyPlaying('track');
res.status(200);
res.send({ currentlyPlaying });
});
router.get('/me/role', (req, res) => {
res.status(200);
res.send({ role: res.locals.user?.role });
});
};
export const applyUserRoutesPublic = (router) => {
router.get('/users/:userId/info', async (req, res) => {
if (!req.params.userId) {
res.status(400);
res.send({ message: 'userId is missing' });
}
const { userId } = req.params;
const userStore = await UserStore.findOne().bySpotifyId(userId);
if (!userStore) {
res.status(400);
res.send({ message: 'user is not registered' });
return;
}
const host = await userStore?.spotify?.local;
try {
const currentlyPlaying = await host.player.getCurrentlyPlaying('track');
res.status(200);
res.send({
currentlyPlaying,
user: await getUserResource(userStore),
});
} catch (e) {
console.log(e);
res.status(500);
res.send({ message: 'server error' });
}
});
};