Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion core/lib/authentication/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ export type AuthorizationOutput<S extends ApiAccessScope, AT extends ApiAccessTy
authorization: true | Exclude<(typeof baseAuthorizationObject)[S][AT], false>;
community: Communities;
lastModifiedBy: LastModifiedBy;
user: User;
// is empty for site builder tokens
// should be empty for other tokens, but isn't.
// we are incorrectly passing the user who minted the token to eg getPubs and getStages
user?: User;
isSiteBuilderToken?: boolean;
};

Expand Down
46 changes: 29 additions & 17 deletions core/lib/db/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,35 @@ import { autoCache } from "../server/cache/autoCache";
import { viewableStagesCte } from "../server/stages";
import { SAFE_USER_SELECT } from "../server/user";

export const getStage = cache((stageId: StagesId, userId: UsersId) => {
return autoCache(
db
.with("viewableStages", (db) => viewableStagesCte({ db, userId }))
.selectFrom("stages")
.innerJoin("viewableStages", "viewableStages.stageId", "stages.id")
.select([
"stages.id",
"communityId",
"stages.name",
"stages.order",
"createdAt",
"updatedAt",
])
.where("stages.id", "=", stageId)
);
});
export const getStage = cache(
(
stageId: StagesId,
userOrAuthorizedStages: XOR<{ userId?: UsersId }, { authorizedStages: StagesId[] | "all" }>
) => {
return autoCache(
db
.with("viewableStages", (db) => {
if (userOrAuthorizedStages.userId) {
return viewableStagesCte({ db, userId: userOrAuthorizedStages.userId });
}

return db.selectFrom("stages").select("stages.id as stageId").where();
where("stages.id", "in", userOrAuthorizedStages.authorizedStages as StagesId[]);
})
.selectFrom("stages")
.innerJoin("viewableStages", "viewableStages.stageId", "stages.id")
.select([
"stages.id",
"communityId",
"stages.name",
"stages.order",
"createdAt",
"updatedAt",
])
.where("stages.id", "=", stageId)
);
}
);

export const getStageActions = cache(
({
Expand Down
31 changes: 27 additions & 4 deletions core/lib/server/stages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
} from "db/public";
import { Capabilities, MembershipType } from "db/public";

import type { AutoReturnType } from "../types";
import type { AutoReturnType, XOR } from "../types";
import { db } from "~/kysely/database";
import { autoCache } from "./cache/autoCache";
import { autoRevalidate } from "./cache/autoRevalidate";
Expand Down Expand Up @@ -130,7 +130,10 @@ export const getStagesViewableByUser = cache(
}
);

type CommunityStageProps = { communityId: CommunitiesId; stageId?: StagesId; userId: UsersId };
type CommunityStageProps = { communityId: CommunitiesId; stageId?: StagesId } & XOR<
{ userId: UsersId },
{ authorizedStages: StagesId[] | "all" }
>;
type CommunityStageOptions = {
withActionInstances?: "count" | "full" | false;
withMembers?: "count" | "full" | false;
Expand All @@ -140,14 +143,34 @@ type CommunityStageOptions = {
* Get all stages the given user has access to
*/
export const getStages = (
{ communityId, stageId, userId }: CommunityStageProps,
{ communityId, stageId, ...userOrAuthorizedStages }: CommunityStageProps,
options: CommunityStageOptions = {}
) => {
const withActionInstances = options.withActionInstances ?? "count";

return autoCache(
db
.with("viewableStages", (db) => viewableStagesCte({ db: db, userId, communityId }))
.with("viewableStages", (db) => {
if (userOrAuthorizedStages.userId) {
return viewableStagesCte({
db: db,
userId: userOrAuthorizedStages.userId,
communityId,
});
}

return db
.selectFrom("stages")
.select("stages.id as stageId")
.where("stages.communityId", "=", communityId)
.$if(userOrAuthorizedStages.authorizedStages !== "all", (qb) =>
qb.where(
"stages.id",
"in",
userOrAuthorizedStages.authorizedStages as StagesId[]
)
);
})
.selectFrom("stages")
.innerJoin("viewableStages", "viewableStages.stageId", "stages.id")
.where("communityId", "=", communityId)
Expand Down
Loading