Skip to content
Merged
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
12 changes: 9 additions & 3 deletions apps/web/src/lib/core/util/whenSettled.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import type { UseQueryResult } from '@tanstack/solid-query';
import type { Resource } from 'solid-js';
import { createEffect, createRoot, onCleanup, untrack } from 'solid-js';

type Suspendable<T> = Resource<T> | UseQueryResult<T>;
type QueryLike<T> = {
readonly data: T | undefined;
readonly error: Error | null;
readonly isError: boolean;
readonly isSuccess: boolean;
};

type Suspendable<T> = Resource<T> | QueryLike<T>;

type SettledState<T> =
| { status: 'pending' }
Expand All @@ -21,7 +27,7 @@ function getSettledState<T>(suspendable: Suspendable<T>): SettledState<T> {
return { status: 'pending' };
}

const query = suspendable as UseQueryResult<T>;
const query = suspendable as QueryLike<T>;
if (query.isError && query.error) {
return { status: 'error', error: query.error as Error };
}
Expand Down
174 changes: 174 additions & 0 deletions apps/web/src/lib/queries/email/graphql/mapper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import type {
EmailThreadMessageFieldsFragment,
EmailThreadPageFieldsFragment,
} from '@service-storage/graphql/generated/graphql';
import { describe, expect, it } from 'vitest';
import { mapGraphqlEmailThreadPage } from './mapper';

const message: EmailThreadMessageFieldsFragment = {
__typename: 'GraphqlSoupEmailMessage',
id: 'message-1',
providerId: 'provider-message-1',
threadId: 'thread-1',
replyingToId: 'message-0',
linkId: 'link-1',
subject: 'Subject',
snippet: 'Snippet',
internalDateTs: '2026-08-06T12:00:00Z',
sentAt: '2026-08-06T12:01:00Z',
isRead: true,
isStarred: false,
isSent: false,
isDraft: true,
hasAttachments: true,
scheduledSendTime: '2026-08-07T12:00:00Z',
from: { email: 'from@example.com', name: 'From', photoUrl: 'from-photo' },
to: [{ email: 'to@example.com', name: 'To', photoUrl: null }],
cc: [],
bcc: [],
labels: [{ providerLabelId: 'INBOX', name: 'Inbox' }],
bodyText: 'Plain text',
bodyHtmlSanitized: '<p>HTML</p>',
bodyMacro: 'Macro',
bodyReplyless: '<p>Replyless</p>',
attachments: [
{
__typename: 'GraphqlSoupEmailMessageAttachment',
id: 'attachment-1',
providerId: 'provider-attachment-1',
filename: 'image.png',
mimeType: 'image/png',
sizeBytes: 10,
sfsId: 'sfs-1',
contentId: 'cid-1',
},
],
attachmentsDraft: [
{
__typename: 'GraphqlSoupEmailDraftAttachment',
id: 'draft-attachment-1',
draftId: 'message-1',
fileName: 'draft.txt',
contentType: 'text/plain',
sha: 'sha',
size: 20,
s3Key: 'draft-key',
},
],
attachmentsForwarded: [
{
__typename: 'GraphqlSoupEmailForwardedAttachment',
attachmentId: 'forwarded-attachment-1',
draftId: 'message-1',
providerAttachmentId: 'provider-forwarded-1',
messageProviderId: 'provider-message-0',
filename: 'forwarded.pdf',
mimeType: 'application/pdf',
sizeBytes: 30,
},
],
createdAt: '2026-08-06T11:00:00Z',
updatedAt: '2026-08-06T12:02:00Z',
};

function thread(
overrides: Partial<EmailThreadPageFieldsFragment> = {}
): EmailThreadPageFieldsFragment {
return {
__typename: 'GraphqlSoupEmailThread',
id: 'thread-1',
providerId: 'provider-thread-1',
linkId: 'link-1',
inboxVisible: true,
isRead: false,
projectId: 'project-1',
latestInboundMessageTs: '2026-08-06T12:00:00Z',
createdAt: '2026-08-01T00:00:00Z',
updatedAt: '2026-08-06T12:02:00Z',
viewerPermission: {
__typename: 'GraphqlAccessLevelPermission',
accessLevel: 'OWNER',
},
labels: [
{
__typename: 'GraphqlSoupEmailLabel',
id: 'label-1',
linkId: 'link-1',
providerLabelId: 'INBOX',
name: 'Inbox',
createdAt: '2026-08-01T00:00:00Z',
messageListVisibility: 'show',
labelListVisibility: 'labelShow',
type: 'system',
},
],
messages: [message],
...overrides,
};
}

describe('mapGraphqlEmailThreadPage', () => {
it('maps thread metadata, permissions, messages, and attachments', () => {
const mapped = mapGraphqlEmailThreadPage(thread());

expect(mapped).toMatchObject({
access_level: 'owner',
db_id: 'thread-1',
provider_id: 'provider-thread-1',
link_id: 'link-1',
inbox_visible: true,
latest_inbound_message_ts: '2026-08-06T12:00:00Z',
});
expect(mapped.messages[0]).toMatchObject({
db_id: 'message-1',
provider_id: 'provider-message-1',
replying_to_id: 'message-0',
scheduled_send_time: '2026-08-07T12:00:00Z',
from: { email: 'from@example.com', photo_url: 'from-photo' },
labels: [
{
id: 'label-1',
link_id: 'link-1',
provider_label_id: 'INBOX',
},
],
attachments: [
{
db_id: 'attachment-1',
sfs_id: 'sfs-1',
content_id: 'cid-1',
},
],
attachments_draft: [{ id: 'draft-attachment-1', s3_key: 'draft-key' }],
attachments_forwarded: [
{
attachment_id: 'forwarded-attachment-1',
message_provider_id: 'provider-message-0',
},
],
});
});

it('defaults unknown permission shapes to view-only', () => {
const mapped = mapGraphqlEmailThreadPage(
thread({
viewerPermission: {
__typename: 'GraphqlTeamRolePermission',
},
})
);

expect(mapped.access_level).toBe('view');
});

it('uses message metadata when a canonical label is unavailable', () => {
const mapped = mapGraphqlEmailThreadPage(thread({ labels: [] }));

expect(mapped.messages[0]?.labels[0]).toMatchObject({
created_at: message.createdAt,
link_id: message.linkId,
name: 'Inbox',
provider_label_id: 'INBOX',
});
});
});
154 changes: 154 additions & 0 deletions apps/web/src/lib/queries/email/graphql/mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import type {
AccessLevel,
ApiContactInfo,
ApiMessage,
ApiMessageLabel,
ApiThread,
} from '@service-email/generated/schemas';
import type {
EmailThreadMessageFieldsFragment,
EmailThreadPageFieldsFragment,
GraphqlEntityAccessLevel,
} from '@service-storage/graphql/generated/graphql';
import { match } from 'ts-pattern';

function optional<T>(value: T | null): T | undefined {
return value ?? undefined;
}

function mapAccessLevel(
permission: EmailThreadPageFieldsFragment['viewerPermission']
): AccessLevel {
if (permission?.__typename !== 'GraphqlAccessLevelPermission') {
// An accessible email thread should always use access-level permissions.
// Default to view-only if permission hydration fails so compose actions
// remain disabled rather than granting more access than the viewer has.
return 'view';
}

return match<GraphqlEntityAccessLevel, AccessLevel>(permission.accessLevel)
.with('VIEW', () => 'view')
.with('COMMENT', () => 'comment')
.with('EDIT', () => 'edit')
.with('OWNER', () => 'owner')
.exhaustive();
}

function mapContact(
contact: NonNullable<EmailThreadMessageFieldsFragment['from']>
): ApiContactInfo {
return {
email: contact.email,
name: contact.name,
photo_url: contact.photoUrl,
};
}

function mapMessageLabel(
message: EmailThreadMessageFieldsFragment,
label: EmailThreadMessageFieldsFragment['labels'][number],
labelsByProviderId: ReadonlyMap<
string,
EmailThreadPageFieldsFragment['labels'][number]
>
): ApiMessageLabel {
const canonical = labelsByProviderId.get(label.providerLabelId);
return {
created_at: canonical?.createdAt ?? message.createdAt,
id: canonical?.id,
link_id: canonical?.linkId ?? message.linkId,
name: label.name,
provider_label_id: label.providerLabelId,
};
}

function mapMessage(
message: EmailThreadMessageFieldsFragment,
labelsByProviderId: ReadonlyMap<
string,
EmailThreadPageFieldsFragment['labels'][number]
>
): ApiMessage {
return {
attachments: message.attachments.map((attachment) => ({
db_id: attachment.id,
provider_id: optional(attachment.providerId),
filename: optional(attachment.filename),
mime_type: optional(attachment.mimeType),
size_bytes: optional(attachment.sizeBytes),
sfs_id: optional(attachment.sfsId),
content_id: optional(attachment.contentId),
})),
attachments_draft: message.attachmentsDraft.map((attachment) => ({
id: attachment.id,
draft_id: attachment.draftId,
file_name: attachment.fileName,
content_type: attachment.contentType,
sha: attachment.sha,
size: attachment.size,
s3_key: attachment.s3Key,
})),
attachments_forwarded: message.attachmentsForwarded.map((attachment) => ({
attachment_id: attachment.attachmentId,
draft_id: attachment.draftId,
provider_attachment_id: optional(attachment.providerAttachmentId),
message_provider_id: attachment.messageProviderId,
filename: optional(attachment.filename),
mime_type: optional(attachment.mimeType),
size_bytes: optional(attachment.sizeBytes),
})),
bcc: message.bcc.map(mapContact),
body_html_sanitized: message.bodyHtmlSanitized,
body_macro: message.bodyMacro,
body_replyless: message.bodyReplyless,
body_text: message.bodyText,
cc: message.cc.map(mapContact),
created_at: message.createdAt,
db_id: message.id,
from: message.from ? mapContact(message.from) : undefined,
has_attachments: message.hasAttachments,
internal_date_ts: message.internalDateTs,
is_draft: message.isDraft,
is_read: message.isRead,
is_sent: message.isSent,
is_starred: message.isStarred,
labels: message.labels.map((label) =>
mapMessageLabel(message, label, labelsByProviderId)
),
link_id: message.linkId,
provider_id: optional(message.providerId),
replying_to_id: optional(message.replyingToId),
scheduled_send_time: message.scheduledSendTime,
sent_at: message.sentAt,
snippet: message.snippet,
subject: message.subject,
thread_db_id: message.threadId,
to: message.to.map(mapContact),
updated_at: message.updatedAt,
};
}

/** Maps one GraphQL email-thread page to the REST-compatible viewer model. */
export function mapGraphqlEmailThreadPage(
thread: EmailThreadPageFieldsFragment
): ApiThread {
const labelsByProviderId = new Map(
thread.labels.map((label) => [label.providerLabelId, label])
);

return {
access_level: mapAccessLevel(thread.viewerPermission),
created_at: thread.createdAt,
db_id: thread.id,
inbox_visible: thread.inboxVisible,
is_read: thread.isRead,
latest_inbound_message_ts: thread.latestInboundMessageTs,
link_id: thread.linkId,
messages: thread.messages.map((message) =>
mapMessage(message, labelsByProviderId)
),
project_id: optional(thread.projectId),
provider_id: optional(thread.providerId),
updated_at: thread.updatedAt,
};
}
Loading
Loading