-
Notifications
You must be signed in to change notification settings - Fork 38
feat(conversations): allow one-way private to public publish #1545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sentry-junior
wants to merge
5
commits into
main
Choose a base branch
from
feat/publish-private-conversation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2ede951
feat(conversations): allow one-way private to public publish
sentry-junior[bot] ba43935
fix(conversations): Prevent stale visibility after publish
sentry-junior[bot] c8955e1
fix(conversations): block publish on shared destinations
sentry-junior[bot] 6b56cf3
fix(conversations): Serialize publish with root creation
sentry-junior[bot] b0f9193
fix(conversations): correct public publish confirm copy
sentry-junior[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,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" }; | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.