Skip to content

Commit

Permalink
Merge pull request funmusicplace#793 from funmusicplace/various-fixes
Browse files Browse the repository at this point in the history
feat: refactor some admin editng thngs
  • Loading branch information
simonv3 authored Aug 7, 2024
2 parents 8046ec5 + 1e4644f commit 1c572e6
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 98 deletions.
20 changes: 5 additions & 15 deletions src/auth/passport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import prisma from "@mirlo/prisma";
import { findArtistIdForURLSlug } from "../utils/artist";
import logger from "../logger";
import { AppError } from "../utils/error";
import { doesTrackGroupBelongToUser } from "../utils/ownership";

const JWTStrategy = passportJWT.Strategy;

Expand Down Expand Up @@ -165,21 +166,7 @@ export const trackGroupBelongsToLoggedInUser = async (
httpCode: 401,
});
} else {
const trackGroup = await prisma.trackGroup.findFirst({
where: {
artist: {
userId: loggedInUser.id,
},
id: Number(trackGroupId),
},
});

if (!trackGroup) {
throw new AppError({
description: "TrackGroup does not exist or does not belong to user",
httpCode: 404,
});
}
await doesTrackGroupBelongToUser(Number(trackGroupId), loggedInUser);
}
} catch (e) {
return next(e);
Expand All @@ -205,6 +192,9 @@ export const trackBelongsToLoggedInUser = async (
httpCode: 401,
});
} else {
if (loggedInUser.isAdmin) {
return next();
}
const track = await prisma.track.findFirst({
where: {
trackGroup: {
Expand Down
2 changes: 1 addition & 1 deletion src/routers/v1/manage/trackGroups/{trackGroupId}/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function () {
try {
const trackGroup = await doesTrackGroupBelongToUser(
Number(trackGroupId),
loggedInUser.id
loggedInUser
);

if (trackGroupId) {
Expand Down
4 changes: 2 additions & 2 deletions src/routers/v1/manage/trackGroups/{trackGroupId}/cover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function () {
const { trackGroupId } = req.params as unknown as Params;
const loggedInUser = req.user as User;
try {
await doesTrackGroupBelongToUser(Number(trackGroupId), loggedInUser.id);
await doesTrackGroupBelongToUser(Number(trackGroupId), loggedInUser);

const jobId = await processTrackGroupCover({ req, res })(
Number(trackGroupId)
Expand Down Expand Up @@ -85,7 +85,7 @@ export default function () {
try {
const trackgroup = await doesTrackGroupBelongToUser(
Number(trackGroupId),
loggedInUser.id
loggedInUser
);

await deleteTrackGroupCover(trackgroup.id);
Expand Down
7 changes: 4 additions & 3 deletions src/routers/v1/manage/trackGroups/{trackGroupId}/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const findNewSlug = async (

export default function () {
const operations = {
PUT: [userAuthenticated, contentBelongsToLoggedInUserArtist, PUT],
PUT: [userAuthenticated, trackGroupBelongsToLoggedInUser, PUT],
DELETE: [userAuthenticated, trackGroupBelongsToLoggedInUser, DELETE],
GET: [userAuthenticated, trackGroupBelongsToLoggedInUser, GET],
};
Expand Down Expand Up @@ -75,15 +75,14 @@ export default function () {
async function PUT(req: Request, res: Response, next: NextFunction) {
const { trackGroupId } = req.params as unknown as Params;
const data = req.body;
const loggedInUser = req.user as User;

try {
const artist = (await prisma.artist.findFirst({
where: {
userId: loggedInUser.id,
id: Number(data.artistId),
},
})) as Artist; // By now we know that the artist exists
// and the user can edit it

const newValues = pick(data, [
"title",
Expand All @@ -95,6 +94,8 @@ export default function () {
"credits",
]);

console.log(newValues);

await prisma.trackGroup.updateMany({
where: { id: Number(trackGroupId), artistId: artist.id },
data: newValues,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function () {
try {
const trackGroup = await doesTrackGroupBelongToUser(
Number(trackGroupId),
Number(loggedInUser.id)
loggedInUser
);
const updatedTrackgroup = await prisma.trackGroup.update({
where: { id: Number(trackGroupId) || undefined },
Expand Down
2 changes: 1 addition & 1 deletion src/routers/v1/manage/trackGroups/{trackGroupId}/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function () {
try {
const trackgroup = await doesTrackGroupBelongToUser(
Number(trackGroupId),
loggedInUser.id
loggedInUser
);

await prisma.trackGroupTag.deleteMany({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function () {
try {
const trackGroup = await doesTrackGroupBelongToUser(
Number(trackGroupId),
loggedInUser.id
loggedInUser
);

await Promise.all(
Expand Down
5 changes: 1 addition & 4 deletions src/routers/v1/manage/tracks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ export default function () {
const { title, trackGroupId, trackArtists, order, metadata, isPreview } =
req.body;
try {
await doesTrackGroupBelongToUser(
Number(trackGroupId),
Number(loggedInUser.id)
);
await doesTrackGroupBelongToUser(Number(trackGroupId), loggedInUser);

const createdTrack = await prisma.track.create({
data: {
Expand Down
2 changes: 1 addition & 1 deletion src/routers/v1/manage/tracks/{trackId}/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function () {
const { trackId } = req.params as unknown as Params;
const loggedInUser = req.user as User;
try {
const track = doesTrackBelongToUser(Number(trackId), loggedInUser.id);
const track = doesTrackBelongToUser(Number(trackId), loggedInUser);

if (!track) {
res.status(400).json({
Expand Down
16 changes: 1 addition & 15 deletions src/routers/v1/manage/tracks/{trackId}/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,11 @@ export default function () {
GET: [userAuthenticated, trackBelongsToLoggedInUser, GET],
};

// FIXME: only allow updating of tracks owned by userId
async function PUT(req: Request, res: Response, next: NextFunction) {
const { trackId } = req.params;
const { title, isPreview, trackArtists } = req.body as TrackBody;
const loggedInUser = req.user as User;

try {
const track = await doesTrackBelongToUser(
Number(trackId),
Number(loggedInUser.id)
);

if (!track) {
res.status(400).json({
error: "Track must belong to user",
});
return next();
}

await updateTrackArtists(Number(trackId), trackArtists);

const newTrack = await prisma.track.update({
Expand Down Expand Up @@ -106,7 +92,7 @@ export default function () {
const loggedInUser = req.user as User;

const trackId = Number(trackIdString);
const track = await doesTrackBelongToUser(trackId, Number(loggedInUser.id));
const track = await doesTrackBelongToUser(trackId, loggedInUser);
if (!track) {
res.status(400).json({
error: "Track must belong to user",
Expand Down
15 changes: 0 additions & 15 deletions src/routers/v1/manage/tracks/{trackId}/trackArtists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import {
trackBelongsToLoggedInUser,
userAuthenticated,
} from "../../../../../auth/passport";
import { doesTrackBelongToUser } from "../../../../../utils/ownership";
import { updateTrackArtists } from "../../../../../utils/tracks";
import { User } from "@mirlo/prisma/client";

interface TrackBody {
title: string;
Expand All @@ -28,22 +26,9 @@ export default function () {
const { trackId } = req.params as {
trackId: string;
};
const loggedInUser = req.user as User;

const { trackArtists } = req.body as TrackBody;
try {
const track = await doesTrackBelongToUser(
Number(trackId),
Number(loggedInUser.id)
);

if (!track) {
res.status(400).json({
error: "Track must belong to user",
});
return next();
}

const newTrackArtists = await updateTrackArtists(
Number(trackId),
trackArtists
Expand Down
6 changes: 3 additions & 3 deletions src/routers/v1/trackGroups/{id}/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ export default function () {
let trackGroup;

if (req.user) {
const { id: userId, isAdmin } = req.user as User;
const user = req.user as User;

if (!isAdmin) {
if (!user.isAdmin) {
const purchase = await findPurchaseAndVoidToken(
Number(trackGroupId),
userId
user
);

trackGroup = purchase.trackGroup;
Expand Down
6 changes: 3 additions & 3 deletions src/routers/v1/trackGroups/{id}/generateDownload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ export default function () {
let trackGroup;

if (req.user) {
const { id: userId, isAdmin } = req.user as User;
const user = req.user as User;

if (!isAdmin) {
if (!user.isAdmin) {
const purchase = await findPurchaseAndVoidToken(
Number(trackGroupId),
userId
user
);

trackGroup = purchase.trackGroup;
Expand Down
2 changes: 1 addition & 1 deletion src/routers/v1/tracks/{id}/stream/{segment}.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default function () {

const isUserAbleToListenToTrack = await canUserListenToTrack(
track?.id,
user?.id
user
);

if (!track || !isUserAbleToListenToTrack) {
Expand Down
63 changes: 34 additions & 29 deletions src/utils/ownership.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ArtistSubscriptionTier, Prisma } from "@mirlo/prisma/client";
import {
ArtistSubscriptionTier,
Prisma,
TrackGroup,
User,
} from "@mirlo/prisma/client";

import prisma from "@mirlo/prisma";
import { AppError } from "./error";
Expand Down Expand Up @@ -27,36 +32,39 @@ export const doesSubscriptionTierBelongToUser = async (

export const doesTrackGroupBelongToUser = async (
trackGroupId: number,
userId: number
user: User
) => {
const artists = await prisma.artist.findMany({
where: {
userId: Number(userId),
},
});

const trackgroup = await prisma.trackGroup.findFirst({
where: {
artistId: { in: artists.map((a) => a.id) },
id: Number(trackGroupId),
},
include: trackGroupSingleInclude({ loggedInUserId: userId }),
});
let trackGroup;
if (user.isAdmin) {
trackGroup = await prisma.trackGroup.findFirst({
where: {
id: Number(trackGroupId),
},
include: trackGroupSingleInclude({ loggedInUserId: user.id }),
});
} else {
trackGroup = await prisma.trackGroup.findFirst({
where: {
artist: {
userId: user.id,
},
id: Number(trackGroupId),
},
include: trackGroupSingleInclude({ loggedInUserId: user.id }),
});
}

if (!trackgroup) {
if (!trackGroup) {
throw new AppError({
description: "TrackGroup does not exist or does not belong to user",
httpCode: 404,
name: "TrackGroup does not exist or does not belong to user",
});
}
return trackgroup;
return trackGroup;
};

export const doesTrackBelongToUser = async (
trackId: number,
userId: number
) => {
export const doesTrackBelongToUser = async (trackId: number, user: User) => {
try {
const track = await prisma.track.findUnique({
where: {
Expand All @@ -67,7 +75,7 @@ export const doesTrackBelongToUser = async (
if (track) {
const trackGroup = await doesTrackGroupBelongToUser(
track?.trackGroupId,
userId
user
);
if (trackGroup) {
return track;
Expand All @@ -80,10 +88,7 @@ export const doesTrackBelongToUser = async (
}
};

export const canUserListenToTrack = async (
trackId?: number,
userId?: number
) => {
export const canUserListenToTrack = async (trackId?: number, user?: User) => {
if (!trackId) {
return false;
}
Expand All @@ -98,16 +103,16 @@ export const canUserListenToTrack = async (
return true;
}

if (track && userId) {
const trackGroup = await doesTrackBelongToUser(track.trackGroupId, userId);
if (track && user) {
const trackGroup = await doesTrackBelongToUser(track.trackGroupId, user);
if (trackGroup) {
return true;
}

const purchase = await prisma.userTrackGroupPurchase.findFirst({
where: {
trackGroupId: track.trackGroupId,
userId: userId,
userId: user.id,
},
});

Expand Down
Loading

0 comments on commit 1c572e6

Please sign in to comment.