-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
186 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,4 +61,4 @@ app.onError((err, ctx) => { | |
) | ||
}) | ||
|
||
export default app | ||
export default app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import { AppHandler } from "../handler" | ||
import { getConnection } from "@/v2/db/turso" | ||
import { | ||
userCollection, | ||
userCollectionCollaborators, | ||
selectUserCollectionSchema, | ||
selectUserSchema, | ||
} from "@/v2/db/schema" | ||
import { and, eq } from "drizzle-orm" | ||
import { createRoute } from "@hono/zod-openapi" | ||
import { GenericResponses } from "@/v2/lib/response-schemas" | ||
import { z } from "@hono/zod-openapi" | ||
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager" | ||
|
||
const paramsSchema = z.object({ | ||
id: z.string().openapi({ | ||
param: { | ||
name: "id", | ||
in: "path", | ||
description: "The ID of the collection to retrieve.", | ||
required: true, | ||
}, | ||
}), | ||
}) | ||
|
||
const responseSchema = z.object({ | ||
success: z.literal(true), | ||
collection: selectUserCollectionSchema | ||
.pick({ | ||
id: true, | ||
name: true, | ||
accentColour: true, | ||
isPublic: true, | ||
userId: true, | ||
}) | ||
.extend({ | ||
authUser: selectUserSchema.pick({ | ||
id: true, | ||
avatarUrl: true, | ||
displayName: true, | ||
username: true, | ||
usernameColour: true, | ||
plan: true, | ||
role: true, | ||
}), | ||
}), | ||
}) | ||
|
||
const openRoute = createRoute({ | ||
path: "/{id}", | ||
method: "get", | ||
summary: "Get a collection", | ||
description: | ||
"Get a collection by its ID. If you do not have access to the collection (it is private/you do not have edit permission), it will not be returned.", | ||
tags: ["Asset"], | ||
request: { | ||
params: paramsSchema, | ||
}, | ||
responses: { | ||
200: { | ||
description: "Basic information about the collection is returned.", | ||
content: { | ||
"application/json": { | ||
schema: responseSchema, | ||
}, | ||
}, | ||
}, | ||
...GenericResponses, | ||
}, | ||
}) | ||
|
||
export const GetCollectionByIdRoute = (handler: AppHandler) => { | ||
handler.openapi(openRoute, async (ctx) => { | ||
const { id } = ctx.req.valid("param") | ||
|
||
const { drizzle } = await getConnection(ctx.env) | ||
|
||
const [validCollection] = await drizzle | ||
.select({ | ||
id: userCollection.id, | ||
userId: userCollection.userId, | ||
isPublic: userCollection.isPublic, | ||
}) | ||
.from(userCollection) | ||
.where(eq(userCollection.id, id)) | ||
|
||
if (!validCollection) { | ||
return ctx.json( | ||
{ | ||
success: false, | ||
message: "Collection not found", | ||
}, | ||
404 | ||
) | ||
} | ||
|
||
const authSessionManager = new AuthSessionManager(ctx) | ||
|
||
const { user } = await authSessionManager.validateSession() | ||
|
||
if (!validCollection.isPublic) { | ||
if (!user) { | ||
return ctx.json( | ||
{ | ||
success: false, | ||
message: "Unauthorized", | ||
}, | ||
401 | ||
) | ||
} | ||
|
||
const [collaborator] = await drizzle | ||
.select() | ||
.from(userCollectionCollaborators) | ||
.where( | ||
and( | ||
eq(userCollectionCollaborators.collectionId, id), | ||
eq(userCollectionCollaborators.collaboratorId, user.id) | ||
) | ||
) | ||
|
||
if (!collaborator && validCollection.userId != user.id) { | ||
return ctx.json( | ||
{ | ||
success: false, | ||
message: "Unauthorized", | ||
}, | ||
401 | ||
) | ||
} | ||
} | ||
|
||
const collectionInfo = await drizzle.query.userCollection.findFirst({ | ||
columns: { | ||
id: true, | ||
name: true, | ||
accentColour: true, | ||
isPublic: true, | ||
userId: true, | ||
}, | ||
where: (collection, { eq }) => eq(collection.id, id), | ||
with: { | ||
authUser: { | ||
columns: { | ||
id: true, | ||
avatarUrl: true, | ||
displayName: true, | ||
username: true, | ||
usernameColour: true, | ||
plan: true, | ||
role: true, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
return ctx.json( | ||
{ | ||
success: true, | ||
collection: collectionInfo, | ||
}, | ||
200 | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { OpenAPIHono } from "@hono/zod-openapi"; | ||
import { CreateCollectionRoute } from "./create-collection"; | ||
import { DeleteCollectionRoute } from "./delete-collection"; | ||
import { OpenAPIHono } from "@hono/zod-openapi" | ||
import { CreateCollectionRoute } from "./create-collection" | ||
import { DeleteCollectionRoute } from "./delete-collection" | ||
|
||
const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>(); | ||
const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>() | ||
|
||
CreateCollectionRoute(handler); | ||
DeleteCollectionRoute(handler); | ||
CreateCollectionRoute(handler) | ||
DeleteCollectionRoute(handler) | ||
|
||
export default handler; | ||
export default handler |