-
Notifications
You must be signed in to change notification settings - Fork 0
[PB-6431]: feat/implement RecipientKeysService for managing public keys and add lookup functionality in MailService #54
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2f4efb7
feat(recipient-keys): implement RecipientKeysService for managing pub…
jzunigax2 a7685f1
feat(mail): enhance ComposeMessageDialog with email encryption, recip…
jzunigax2 c203985
feat(mail): implement email preview decryption and enhance ComposeMes…
jzunigax2 c1b7544
feat(mail): integrate decrypted previews in email formatting and enha…
jzunigax2 7b4f50d
chore: add unit tests for useDecryptedMail and useDecryptedPreviews h…
jzunigax2 1df2887
feat(recipient-keys): implement RecipientKeysService for managing pub…
jzunigax2 0cc0eb2
chore(deps): bump SDK version
xabg2 31ee18f
feat(mail): refactor email decryption logic in useDecryptedMail and u…
jzunigax2 da9b73c
chore: update sdk and crypto
jzunigax2 a79dcd0
chore: refactor email encryption handling by introducing MailEncrypti…
jzunigax2 ef5cb17
feat(mail): refactor ComposeMessageDialog and introduce useComposeSen…
jzunigax2 b6ddff7
test: add unit tests for useComposeSend hook to validate email sendin…
jzunigax2 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,155 @@ | ||
| import { useCallback, useMemo } from 'react'; | ||
| import type { Editor } from '@tiptap/react'; | ||
| import type { EmailAddress, SendEmailRequest } from '@internxt/sdk/dist/mail/types'; | ||
| import { | ||
| useGetActiveDomainsQuery, | ||
| useGetMailAccountKeysQuery, | ||
| useLazyLookupRecipientKeysQuery, | ||
| useSendEmailMutation, | ||
| } from '@/store/api/mail'; | ||
| import { classifyRecipients, uniqueEmailAddresses } from '@/utils/domain'; | ||
| import { MailEncryptionService, type RecipientPublicKey } from '@/services/mail-encryption'; | ||
| import notificationsService, { ToastType } from '@/services/notifications'; | ||
| import { useTranslationContext } from '@/i18n'; | ||
| import type { Recipient } from '../types'; | ||
|
|
||
| export type EncryptionState = 'none' | 'encrypted' | 'cleartext'; | ||
|
|
||
| const toEmailAddress = (r: Recipient): EmailAddress => (r.name ? { name: r.name, email: r.email } : { email: r.email }); | ||
|
|
||
| interface UseComposeSendParams { | ||
| toRecipients: Recipient[]; | ||
| ccRecipients: Recipient[]; | ||
| bccRecipients: Recipient[]; | ||
| subject: string; | ||
| editor: Editor | null; | ||
| onSent: () => void; | ||
| } | ||
|
|
||
| interface UseComposeSendResult { | ||
| encryptionState: EncryptionState; | ||
| isSending: boolean; | ||
| send: () => Promise<void>; | ||
| } | ||
|
|
||
| /** | ||
| * Owns the compose dialog's send pipeline: recipient classification, recipient | ||
| * key lookup, body/preview encryption and dispatch of the send mutation. Keeps | ||
| * `ComposeMessageDialog` focused on rendering and wiring callbacks. | ||
| */ | ||
| export const useComposeSend = ({ | ||
| toRecipients, | ||
| ccRecipients, | ||
| bccRecipients, | ||
| subject, | ||
| editor, | ||
| onSent, | ||
| }: UseComposeSendParams): UseComposeSendResult => { | ||
| const { translate } = useTranslationContext(); | ||
|
|
||
| const { data: activeDomains } = useGetActiveDomainsQuery(); | ||
| const { data: senderKeys } = useGetMailAccountKeysQuery(); | ||
| const [triggerLookup] = useLazyLookupRecipientKeysQuery(); | ||
| const [sendEmail, { isLoading: isSending }] = useSendEmailMutation(); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| const allRecipients = useMemo( | ||
| () => [...toRecipients, ...ccRecipients, ...bccRecipients], | ||
| [toRecipients, ccRecipients, bccRecipients], | ||
| ); | ||
|
|
||
| const encryptionState = useMemo<EncryptionState>(() => { | ||
| if (allRecipients.length === 0) return 'none'; | ||
| if (!activeDomains) return 'none'; | ||
| return classifyRecipients( | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| allRecipients.map((r) => r.email), | ||
| activeDomains, | ||
| ).allInternxt | ||
| ? 'encrypted' | ||
| : 'cleartext'; | ||
| }, [allRecipients, activeDomains]); | ||
|
|
||
| const send = useCallback(async () => { | ||
| if (allRecipients.length === 0) { | ||
| notificationsService.show({ | ||
| text: translate('errors.mail.noRecipients'), | ||
| type: ToastType.Warning, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const htmlBody = editor?.getHTML() ?? ''; | ||
| const textBody = editor?.getText() ?? ''; | ||
| const cleartextPayload: SendEmailRequest = { | ||
| to: toRecipients.map(toEmailAddress), | ||
| cc: ccRecipients.length ? ccRecipients.map(toEmailAddress) : undefined, | ||
| bcc: bccRecipients.length ? bccRecipients.map(toEmailAddress) : undefined, | ||
| subject, | ||
| textBody: textBody || undefined, | ||
| htmlBody: htmlBody || undefined, | ||
| }; | ||
|
|
||
| try { | ||
| if (encryptionState === 'encrypted') { | ||
| if (!senderKeys?.address || !senderKeys.publicKey) { | ||
| notificationsService.show({ | ||
| text: translate('errors.mail.keyLookupFailed'), | ||
| type: ToastType.Error, | ||
| }); | ||
| return; | ||
| } | ||
| const uniqueAddresses = uniqueEmailAddresses(allRecipients.map((r) => r.email)); | ||
| const lookup = await triggerLookup({ addresses: uniqueAddresses }).unwrap(); | ||
| const usable = lookup.filter((r): r is { address: string; publicKey: string } => Boolean(r.publicKey)); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (usable.length !== uniqueAddresses.length) { | ||
| notificationsService.show({ | ||
| text: translate('errors.mail.keyLookupFailed'), | ||
| type: ToastType.Error, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const recipientsWithKeys: RecipientPublicKey[] = [ | ||
| ...usable, | ||
| { address: senderKeys.address, publicKey: senderKeys.publicKey }, | ||
| ]; | ||
| const encryption = await MailEncryptionService.instance.buildEncryptionBlock( | ||
| { body: htmlBody || textBody, previewText: textBody }, | ||
| recipientsWithKeys, | ||
| ); | ||
| await sendEmail({ | ||
| to: toRecipients.map(toEmailAddress), | ||
| cc: ccRecipients.length ? ccRecipients.map(toEmailAddress) : undefined, | ||
| bcc: bccRecipients.length ? bccRecipients.map(toEmailAddress) : undefined, | ||
| subject, | ||
| encryption, | ||
| }).unwrap(); | ||
| } else { | ||
| await sendEmail(cleartextPayload).unwrap(); | ||
| } | ||
| onSent(); | ||
| } catch { | ||
| notificationsService.show({ | ||
| text: translate('errors.mail.sendFailed'), | ||
| type: ToastType.Error, | ||
| }); | ||
| } | ||
| }, [ | ||
| allRecipients, | ||
| editor, | ||
| toRecipients, | ||
| ccRecipients, | ||
| bccRecipients, | ||
| subject, | ||
| encryptionState, | ||
| senderKeys, | ||
| triggerLookup, | ||
| sendEmail, | ||
| onSent, | ||
| translate, | ||
| ]); | ||
|
|
||
| return { encryptionState, isSending, send }; | ||
| }; | ||
|
|
||
| export default useComposeSend; | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win
Replace relative internal import with
@/*alias.Use the project alias for
Recipientimport to keep module imports consistent.Proposed patch
As per coding guidelines, "Use the path alias
@/*→src/*when importing internal modules."📝 Committable suggestion
🤖 Prompt for AI Agents