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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SearchInput } from "../components/SearchInput";
import {
ConversationHeaderActions,
type ConversationArchiveAction,
type ConversationPublishAction,
} from "./ConversationHeaderActions";
import { ConversationDetailsDrawer } from "./ConversationDetailsDrawer";
import type { TranscriptViewMode } from "./transcriptRenderModel";
Expand All @@ -20,6 +21,7 @@ export function ConversationHeader(props: {
onSearchChange(value: string): void;
onViewChange(value: TranscriptViewMode): void;
privacy: ReactNode;
publish?: ConversationPublishAction;
search: string;
stats: ReactNode;
title: string;
Expand Down Expand Up @@ -70,6 +72,7 @@ export function ConversationHeader(props: {
setSearchOpen(true);
}}
onViewChange={props.onViewChange}
publish={props.publish}
searchOpen={searchOpenVisible}
view={props.view}
/>
Expand All @@ -95,6 +98,12 @@ export function ConversationHeader(props: {
</div>
) : null}

{props.publish?.error ? (
<div className="mt-1.5 text-xs text-red-300/80">
Could not make this conversation public.
</div>
) : null}

{props.meta ? (
<div className="mt-1.5 hidden min-w-0 font-sans text-xs leading-snug text-dashboard-text-muted md:block">
{props.meta}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ReactNode } from "react";
import {
Archive,
ArchiveRestore,
Globe2,
Info,
MessagesSquare,
ScrollText,
Expand All @@ -22,6 +23,14 @@ export type ConversationArchiveAction = {
pending: boolean;
};

export type ConversationPublishAction = {
disabled: boolean;
error: boolean;
onClick(): void;
pending: boolean;
visible: boolean;
};

/** Render the compact icon controls for one conversation header. */
export function ConversationHeaderActions(props: {
archive: ConversationArchiveAction;
Expand All @@ -30,6 +39,7 @@ export function ConversationHeaderActions(props: {
onDetailsClick(): void;
onSearchClick(): void;
onViewChange(value: TranscriptViewMode): void;
publish?: ConversationPublishAction;
searchOpen: boolean;
view: TranscriptViewMode;
}) {
Expand All @@ -41,6 +51,9 @@ export function ConversationHeaderActions(props: {
/>
<TranscriptViewToggle onChange={props.onViewChange} value={props.view} />
{props.copyAction}
{props.publish?.visible ? (
<PublishConversationButton {...props.publish} />
) : null}
<ArchiveConversationButton {...props.archive} />
<HeaderIconButton
label="Conversation details"
Expand Down Expand Up @@ -174,3 +187,20 @@ function ArchiveConversationButton(props: ConversationArchiveAction) {
</IconButtonTooltip>
);
}

function PublishConversationButton(props: ConversationPublishAction) {
const label = props.pending ? "Making public" : "Make public";
return (
<IconButtonTooltip label={label}>
<Button
aria-label={label}
className="hidden shrink-0 text-dashboard-text-muted md:grid"
disabled={props.disabled}
onClick={props.onClick}
size="icon"
>
<Globe2 aria-hidden="true" size={15} strokeWidth={2} />
</Button>
</IconButtonTooltip>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
useArchiveConversation,
useCancelConversationPendingMessages,
useConversationData,
usePublishConversation,
type PendingArchiveConversationUpdate,
} from "./queries";
import type { ConversationMailboxMessage } from "./conversationOutbox";
Expand Down Expand Up @@ -63,6 +64,7 @@ export function ConversationPage(props: {
const conversations = buildConversations(summaries);
const detail = useConversationData(conversationId);
const archive = useArchiveConversation(conversationId);
const publish = usePublishConversation(conversationId);
const feedConversation = conversations.find(
(item) => item.id === conversationId,
);
Expand Down Expand Up @@ -121,6 +123,25 @@ export function ConversationPage(props: {
}),
pending: archive.isPending,
}}
publish={{
disabled: publish.isPending,
error: Boolean(publish.error),
onClick: () => {
if (
!window.confirm(
"Make this conversation public? Anyone in this workspace can read it. You cannot undo this.",
)
) {
return;
}
publish.mutate();
},
pending: publish.isPending,
visible: Boolean(
Comment thread
sentry-warden[bot] marked this conversation as resolved.
detail.data?.isParticipant &&
conversation?.visibility === "private",
),
}}
identity={
Comment thread
sentry-warden[bot] marked this conversation as resolved.
hasConversationIdentity({
conversation,
Expand Down
58 changes: 58 additions & 0 deletions packages/junior-dashboard/src/client/conversations/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
conversationDetailReportSchema,
conversationEventPageSchema,
conversationPendingMessagesReportSchema,
publishConversationResponseSchema,
} from "@sentry/junior/api/schema";

import {
Expand Down Expand Up @@ -282,6 +283,63 @@ export function useCancelConversationPendingMessages(conversationId: string) {
});
}

/** One-way private→public publish for a conversation the viewer participates in. */
export function usePublishConversation(conversationId: string) {
const queryClient = useQueryClient();
return useMutation({
mutationKey: ["dashboard", "publish-conversation", conversationId],
mutationFn: () =>
post(
publishConversationResponseSchema,
`/api/conversations/${encodeURIComponent(conversationId)}/publish`,
{},
),
onMutate: async () => {
await Promise.all([
queryClient.cancelQueries({
queryKey: ["dashboard", "conversations"],
}),
queryClient.cancelQueries({
exact: true,
queryKey: conversationDetailQueryKey(conversationId),
}),
]);
},
onSuccess: () => {
const detailQueryKey = conversationDetailQueryKey(conversationId);
queryClient.setQueryData<ConversationDetailReport>(
detailQueryKey,
(detail) => (detail ? { ...detail, visibility: "public" } : detail),
);
const conversationQueries = { queryKey: ["dashboard", "conversations"] };
const feeds =
queryClient.getQueriesData<ConversationFeed>(conversationQueries);
for (const [queryKey, feed] of feeds) {
if (!feed) continue;
queryClient.setQueryData<ConversationFeed>(queryKey, {
...feed,
conversations: feed.conversations.map((conversation) =>
conversation.conversationId === conversationId
? { ...conversation, visibility: "public" }
: conversation,
),
});
}
},
onSettled: async () => {
await Promise.all([
queryClient.invalidateQueries({
queryKey: ["dashboard", "conversations"],
}),
queryClient.invalidateQueries({
exact: true,
queryKey: conversationDetailQueryKey(conversationId),
}),
]);
},
});
}

/** Archive or restore one conversation with an immediate reversible cache update. */
export function useArchiveConversation(
conversationId: string,
Expand Down
110 changes: 110 additions & 0 deletions packages/junior/src/api/conversations/publish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { and, eq, isNull, ne, sql } from "drizzle-orm";
import type { User } from "@sentry/junior-plugin-api";
import { getDb, getSqlExecutor } from "@/chat/db";
import { resolveRootVisibility } from "@/chat/conversations/sql/privacy";
import { juniorConversations, juniorDestinations } from "@/db/schema";
import { throwApiError } from "../http";
import type { PublishConversationResponse } from "../schema/conversation";
import { readConversationAccessFromSql } from "./access";

/**
* Make one conversation public by flipping its root destination visibility.
* One-way only: non-public becomes public; already-public stays public.
*
* Refuses destinations shared by other roots so one publish cannot expose
* unrelated private conversations on the same channel.
*/
export async function publishConversationForViewer(
viewer: User,
conversationId: string,
): Promise<PublishConversationResponse> {
const access = (
await readConversationAccessFromSql(getDb(), [conversationId], viewer)
).get(conversationId);
if (!access) {
throwApiError(404, "Conversation not found.");
}
if (!access.isParticipant) {
throwApiError(403, "Only conversation participants can make this public.");
}

const executor = getSqlExecutor();
const root = await resolveRootVisibility(executor, conversationId);
if (root.visibility === null) {
throwApiError(409, "Conversation has no destination to publish.");
}

// Resolve the root destination id under the same privacy authority used by
// access and retention. Child conversations publish the parent root only.
const [destination] = await executor
.db()
.select({ destinationId: juniorConversations.destinationId })
.from(juniorConversations)
.where(
and(
eq(juniorConversations.conversationId, root.rootConversationId),
eq(
juniorConversations.rootConversationId,
juniorConversations.conversationId,
),
),
)
.limit(1);

if (!destination?.destinationId) {
throwApiError(409, "Conversation has no destination to publish.");
}
const destinationId = destination.destinationId;

await executor.transaction(async () => {
// Root creation upserts the destination before it inserts the conversation.
// Locking this row makes the shared-root check and visibility update atomic
// against a concurrent root that targets the same destination.
const [lockedDestination] = await executor
.db()
.select({ visibility: juniorDestinations.visibility })
.from(juniorDestinations)
.where(eq(juniorDestinations.id, destinationId))
.for("update");
if (!lockedDestination) {
throwApiError(404, "Conversation not found.");
}

// Already public is success without re-checking shared roots: the flip is
// one-way and the destination is already exposed.
if (lockedDestination.visibility === "public") {
return;
}

const [shared] = await executor
.db()
.select({
count: sql<number>`count(*)::int`,
})
.from(juniorConversations)
.where(
and(
eq(juniorConversations.destinationId, destinationId),
isNull(juniorConversations.parentConversationId),
ne(juniorConversations.conversationId, root.rootConversationId),
),
);
if ((shared?.count ?? 0) > 0) {
throwApiError(
409,
"This destination is shared by other conversations, so it cannot be made public from one conversation.",
);
}

await executor
.db()
.update(juniorDestinations)
.set({
updatedAt: sql`now()`,
visibility: "public",
})
.where(eq(juniorDestinations.id, destinationId));
});

return { visibility: "public" };
}
20 changes: 20 additions & 0 deletions packages/junior/src/api/conversations/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
acceptedConversationMessageSchema,
archiveConversationBodySchema,
archiveConversationResponseSchema,
publishConversationResponseSchema,
cancelConversationPendingMessagesBodySchema,
cancelConversationPendingMessagesResponseSchema,
conversationAttachmentParamsSchema,
Expand All @@ -28,6 +29,7 @@ import {
conversationAttachmentHeaders,
requireConversationAttachment,
} from "./attachments";
import { publishConversationForViewer } from "./publish";
import {
appendConversationMessageForViewer,
createConversationForViewer,
Expand Down Expand Up @@ -133,6 +135,24 @@ export function createConversationRoutes(options: {
},
);

app.post(
"/:conversationId/publish",
requireViewer,
validateRequest(
"param",
conversationParamsSchema,
"Invalid route parameters.",
),
async (context) => {
const viewer = context.get("viewer");
const { conversationId } = context.req.valid("param");
return jsonResponse(
publishConversationResponseSchema,
await publishConversationForViewer(viewer, conversationId),
);
},
);

app.get(
"/:conversationId/events",
validateRequest(
Expand Down
2 changes: 2 additions & 0 deletions packages/junior/src/api/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {
acceptedConversationMessageSchema,
archiveConversationBodySchema,
archiveConversationResponseSchema,
publishConversationResponseSchema,
cancelConversationPendingMessagesBodySchema,
cancelConversationPendingMessagesResponseSchema,
conversationAuxiliaryCostsSchema,
Expand All @@ -30,6 +31,7 @@ export type {
AcceptedConversationMessage,
ArchiveConversationBody,
ArchiveConversationResponse,
PublishConversationResponse,
CancelConversationPendingMessagesBody,
CancelConversationPendingMessagesResponse,
ActorIdentity,
Expand Down
Loading
Loading