Skip to content
Open
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
23 changes: 22 additions & 1 deletion apps/realtime/src/routes/http.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { IncomingMessage, ServerResponse } from 'http'
import { FILE_DOC_EVENTS, type FileDocInvalidated } from '@sim/realtime-protocol/file-doc'
import {
FILE_DOC_EVENTS,
type FileDocCommentsChanged,
type FileDocInvalidated,
} from '@sim/realtime-protocol/file-doc'
import {
fileDocAdmissionRoom,
fileDocRoom,
Expand Down Expand Up @@ -282,6 +286,23 @@ export function createHttpHandler(roomManager: IRoomManager, logger: Logger) {
return
}

if (req.method === 'POST' && req.url === '/api/file-doc/comments-changed') {
try {
const body = await readRequestBody(req)
const target = parseFileDocTarget(JSON.parse(body))
if (!target) return sendError(res, 'Invalid file target', 400)
const payload: FileDocCommentsChanged = { fileId: target.fileId }
roomManager.io
.to([roomName(fileDocRoom(target)), fileDocAdmissionRoom(target)])
.emit(FILE_DOC_EVENTS.COMMENTS_CHANGED, payload)
sendSuccess(res)
} catch (error) {
logger.error('Error relaying file comment change:', error)
sendError(res, 'Failed to relay comment change')
}
return
}

res.writeHead(404, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: 'Not found' }))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
deleteWorkspaceFileCommentContract,
editWorkspaceFileCommentContract,
} from '@/lib/api/contracts/workspace-file-comments'
import { defineInternalJsonRoute, internalSessionAuth } from '@/lib/api/server/routes'
import {
fileCommentRateLimits,
internalFileErrorPolicies,
toFileCommentThread,
} from '@/lib/workspace-files/api'
import {
deleteWorkspaceFileComment,
editWorkspaceFileComment,
} from '@/lib/workspace-files/application/file-comments'
import { fileOperations } from '@/lib/workspace-files/application/operations'

export const PATCH = defineInternalJsonRoute({
contract: editWorkspaceFileCommentContract,
auth: internalSessionAuth,
operation: fileOperations.changeComment,
rateLimit: fileCommentRateLimits.write,
errorPolicy: internalFileErrorPolicies.concealResourceAuthorization,
mapInput: ({ params, body }) => ({
fileId: params.fileId,
assertedWorkspaceId: params.id,
threadId: params.threadId,
commentId: params.commentId,
body: body.body,
}),
useCase: editWorkspaceFileComment,
present: ({ thread }) => ({ thread: toFileCommentThread(thread) }),
})

export const DELETE = defineInternalJsonRoute({
contract: deleteWorkspaceFileCommentContract,
auth: internalSessionAuth,
operation: fileOperations.changeComment,
rateLimit: fileCommentRateLimits.write,
errorPolicy: internalFileErrorPolicies.concealResourceAuthorization,
mapInput: ({ params }) => ({
fileId: params.fileId,
assertedWorkspaceId: params.id,
threadId: params.threadId,
commentId: params.commentId,
}),
useCase: deleteWorkspaceFileComment,
present: ({ thread }) => ({ thread: thread ? toFileCommentThread(thread) : null }),
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { replyToWorkspaceFileCommentThreadContract } from '@/lib/api/contracts/workspace-file-comments'
import { defineInternalJsonRoute, internalSessionAuth } from '@/lib/api/server/routes'
import {
fileCommentRateLimits,
internalFileAnalytics,
internalFileErrorPolicies,
toFileCommentThread,
} from '@/lib/workspace-files/api'
import { replyToWorkspaceFileCommentThread } from '@/lib/workspace-files/application/file-comments'
import { fileOperations } from '@/lib/workspace-files/application/operations'

export const POST = defineInternalJsonRoute({
contract: replyToWorkspaceFileCommentThreadContract,
auth: internalSessionAuth,
operation: fileOperations.postComment,
rateLimit: fileCommentRateLimits.write,
errorPolicy: internalFileErrorPolicies.concealResourceAuthorization,
mapInput: ({ params, body }) => ({
fileId: params.fileId,
assertedWorkspaceId: params.id,
threadId: params.threadId,
body: body.body,
}),
useCase: replyToWorkspaceFileCommentThread,
onSuccess: internalFileAnalytics.commentReplied,
present: ({ thread }) => ({ thread: toFileCommentThread(thread) }),
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { resolveWorkspaceFileCommentThreadContract } from '@/lib/api/contracts/workspace-file-comments'
import { defineInternalJsonRoute, internalSessionAuth } from '@/lib/api/server/routes'
import {
fileCommentRateLimits,
internalFileAnalytics,
internalFileErrorPolicies,
toFileCommentThread,
} from '@/lib/workspace-files/api'
import { resolveWorkspaceFileCommentThread } from '@/lib/workspace-files/application/file-comments'
import { fileOperations } from '@/lib/workspace-files/application/operations'

export const PATCH = defineInternalJsonRoute({
contract: resolveWorkspaceFileCommentThreadContract,
auth: internalSessionAuth,
operation: fileOperations.resolveComment,
rateLimit: fileCommentRateLimits.write,
errorPolicy: internalFileErrorPolicies.concealResourceAuthorization,
mapInput: ({ params, body }) => ({
fileId: params.fileId,
assertedWorkspaceId: params.id,
threadId: params.threadId,
resolved: body.resolved,
}),
useCase: resolveWorkspaceFileCommentThread,
onSuccess: internalFileAnalytics.commentResolved,
present: ({ thread }) => ({ thread: toFileCommentThread(thread) }),
})
44 changes: 44 additions & 0 deletions apps/sim/app/api/workspaces/[id]/files/[fileId]/comments/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
createWorkspaceFileCommentThreadContract,
listWorkspaceFileCommentsContract,
} from '@/lib/api/contracts/workspace-file-comments'
import { defineInternalJsonRoute, internalSessionAuth } from '@/lib/api/server/routes'
import {
fileCommentRateLimits,
internalFileAnalytics,
internalFileErrorPolicies,
toFileCommentThread,
} from '@/lib/workspace-files/api'
import {
createWorkspaceFileCommentThread,
listWorkspaceFileComments,
} from '@/lib/workspace-files/application/file-comments'
import { fileOperations } from '@/lib/workspace-files/application/operations'

export const GET = defineInternalJsonRoute({
contract: listWorkspaceFileCommentsContract,
auth: internalSessionAuth,
operation: fileOperations.listComments,
rateLimit: fileCommentRateLimits.read,
errorPolicy: internalFileErrorPolicies.concealResourceAuthorization,
mapInput: ({ params }) => ({ fileId: params.fileId, assertedWorkspaceId: params.id }),
useCase: listWorkspaceFileComments,
present: ({ threads }) => ({ threads: threads.map(toFileCommentThread) }),
})

export const POST = defineInternalJsonRoute({
contract: createWorkspaceFileCommentThreadContract,
auth: internalSessionAuth,
operation: fileOperations.postComment,
rateLimit: fileCommentRateLimits.write,
errorPolicy: internalFileErrorPolicies.concealResourceAuthorization,
mapInput: ({ params, body }) => ({
fileId: params.fileId,
assertedWorkspaceId: params.id,
anchor: body.anchor,
body: body.body,
}),
useCase: createWorkspaceFileCommentThread,
onSuccess: internalFileAnalytics.commentCreated,
present: ({ thread }) => ({ thread: toFileCommentThread(thread) }),
})
26 changes: 26 additions & 0 deletions apps/sim/app/api/workspaces/[id]/files/[fileId]/mentions/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { notifyWorkspaceFileMentionContract } from '@/lib/api/contracts/workspace-file-comments'
import { defineInternalJsonRoute, internalSessionAuth } from '@/lib/api/server/routes'
import {
fileCommentRateLimits,
internalFileAnalytics,
internalFileErrorPolicies,
} from '@/lib/workspace-files/api'
import { notifyWorkspaceFileMention } from '@/lib/workspace-files/application/file-comments'
import { fileOperations } from '@/lib/workspace-files/application/operations'

export const POST = defineInternalJsonRoute({
contract: notifyWorkspaceFileMentionContract,
auth: internalSessionAuth,
operation: fileOperations.notifyMention,
rateLimit: fileCommentRateLimits.mention,
errorPolicy: internalFileErrorPolicies.concealResourceAuthorization,
mapInput: ({ params, body }) => ({
fileId: params.fileId,
assertedWorkspaceId: params.id,
userId: body.userId,
excerpt: body.excerpt,
}),
useCase: notifyWorkspaceFileMention,
onSuccess: internalFileAnalytics.personMentioned,
present: () => ({ notified: true }),
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client'

import { Avatar, AvatarFallback, AvatarImage, cn, Tooltip } from '@sim/emcn'
import { getUserColor } from '@/lib/workspaces/colors'
import { Avatar, AvatarFallback, cn, Tooltip } from '@sim/emcn'
import { UserAvatar } from '@/app/workspace/[workspaceId]/components/user-avatar/user-avatar'

/** Minimal presence shape the avatar stack renders — shared by workflow and files. */
export interface PresenceAvatarUser {
Expand All @@ -13,32 +13,20 @@ export interface PresenceAvatarUser {
avatarUrl?: string | null
}

interface UserAvatarProps {
interface PresenceAvatarProps {
user: PresenceAvatarUser
index: number
}

/**
* A single collaborator avatar: their image, falling back to a colored circle
* with their initial. Wrapped in a name tooltip when the name is known.
*/
function UserAvatar({ user, index }: UserAvatarProps) {
const color = getUserColor(user.userId)
const initials = user.userName ? user.userName.charAt(0).toUpperCase() : '?'

/** A single collaborator in the stack, wrapped in a name tooltip when the name is known. */
function PresenceAvatar({ user, index }: PresenceAvatarProps) {
const avatarElement = (
<Avatar size='xs' style={{ zIndex: index + 1 }}>
{user.avatarUrl && (
<AvatarImage
src={user.avatarUrl}
alt={user.userName ? `${user.userName}'s avatar` : 'User avatar'}
referrerPolicy='no-referrer'
/>
)}
<AvatarFallback style={{ background: color }} className='border-0 font-semibold text-white'>
{initials}
</AvatarFallback>
</Avatar>
<UserAvatar
userId={user.userId}
name={user.userName}
image={user.avatarUrl}
style={{ zIndex: index + 1 }}
/>
)

if (user.userName) {
Expand Down Expand Up @@ -106,7 +94,7 @@ export function PresenceAvatars({
</Tooltip.Root>
)}
{visibleUsers.map((user, index) => (
<UserAvatar key={user.socketId ?? user.userId} user={user} index={index} />
<PresenceAvatar key={user.socketId ?? user.userId} user={user} index={index} />
))}
</div>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use client'

import type { ComponentProps } from 'react'
import { Avatar, AvatarFallback, AvatarImage } from '@sim/emcn'
import { getUserColor } from '@/lib/workspaces/colors'

interface UserAvatarProps extends Omit<ComponentProps<'span'>, 'children'> {
userId: string
name?: string
image?: string | null
size?: 'xs' | 'sm' | 'md'
}

/**
* A workspace member as they appear across the product: their profile photo, falling back to their
* initial on the color presence and cursors give them, so a person looks the same everywhere.
* Announced by the person's name whether or not the photo loads, and spreads the rest onto the
* avatar so it works as a tooltip or popover trigger.
*/
export function UserAvatar({ userId, name, image, size = 'xs', ...props }: UserAvatarProps) {
return (
<Avatar size={size} role='img' aria-label={name ?? 'User'} {...props}>
{image && <AvatarImage src={image} alt='' referrerPolicy='no-referrer' />}
<AvatarFallback
style={{ background: getUserColor(userId) }}
className='border-0 font-semibold text-white'
>
{name ? name.charAt(0).toUpperCase() : '?'}
</AvatarFallback>
</Avatar>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { ReactNode } from 'react'
import { parseCommentBody } from '@/lib/workspace-files/comments/format'

interface CommentBodyProps {
body: string
/** Marks the viewer's own mentions so being tagged stands out. */
currentUserId: string | null
}

/** A comment's text, with each person mention shown as `@Name`. */
export function CommentBody({ body, currentUserId }: CommentBodyProps) {
const parts: ReactNode[] = []
let offset = 0
for (const segment of parseCommentBody(body)) {
parts.push(
segment.kind === 'mention' ? (
<span
key={offset}
className={
segment.userId === currentUserId
? 'rounded-[3px] bg-[color-mix(in_srgb,var(--selection)_14%,transparent)] px-0.5 text-[var(--selection)]'
: 'text-[var(--selection)]'
}
>
@{segment.text}
</span>
) : (
<span key={offset}>{segment.text}</span>
)
)
offset += segment.text.length + 1
}
return (
<p className='whitespace-pre-wrap break-words text-[var(--text-body)] text-small'>{parts}</p>
)
}
Loading
Loading