Skip to content

UBERF-13118: Send emails from Huly mail threads #9669

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 12 commits into from
Aug 13, 2025
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
42 changes: 21 additions & 21 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common/scripts/docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rush docker:build -p 20 \
--to @hcengineering/pod-love \
--to @hcengineering/pod-mail \
--to @hcengineering/pod-datalake \
--to @hcengineering/pod-inbound-mail \
--to @hcengineering/pod-mail-worker \
--to @hcengineering/pod-export \
--to @hcengineering/pod-msg2file \
--to @hcengineering/pod-media \
Expand Down
2 changes: 2 additions & 0 deletions dev/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ services:
- BRANDING_PATH=/var/cfg/branding.json
# - DISABLE_SIGNUP=true
- OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318/v1/traces
- MAILBOX_DOMAINS=huly.dev.local
- MAILBOX_MAX_COUNT_PER_ACCOUNT=2
restart: unless-stopped
stats:
image: hardcoreeng/stats
Expand Down
4 changes: 2 additions & 2 deletions rush.json
Original file line number Diff line number Diff line change
Expand Up @@ -2504,8 +2504,8 @@
"shouldPublish": false
},
{
"packageName": "@hcengineering/pod-inbound-mail",
"projectFolder": "services/mail/pod-inbound-mail",
"packageName": "@hcengineering/pod-mail-worker",
"projectFolder": "services/mail/pod-mail-worker",
"shouldPublish": false
},
{
Expand Down
4 changes: 2 additions & 2 deletions server/account/src/serviceOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ export async function getPersonInfo (
): Promise<PersonInfo> {
const { account } = params
const { extra } = decodeTokenVerbose(ctx, token)
verifyAllowedServices(['workspace', 'tool', 'gmail'], extra)
verifyAllowedServices(['workspace', 'tool', 'gmail', 'huly-mail'], extra)

if (account == null || account === '') {
throw new PlatformError(new Status(Severity.ERROR, platform.status.BadRequest, {}))
Expand Down Expand Up @@ -917,7 +917,7 @@ export async function findFullSocialIds (
): Promise<SocialId[]> {
const { socialIds } = params
const { extra } = decodeTokenVerbose(ctx, token)
verifyAllowedServices(['gmail', 'tool', 'workspace'], extra)
verifyAllowedServices(['gmail', 'tool', 'workspace', 'huly-mail'], extra)

if (socialIds == null || socialIds.length === 0) {
throw new PlatformError(new Status(Severity.ERROR, platform.status.BadRequest, {}))
Expand Down
3 changes: 2 additions & 1 deletion server/account/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,8 @@ export const integrationServices = [
'mailbox',
'caldav',
'gmail',
'google-calendar'
'google-calendar',
'huly-mail'
]

export async function findExistingIntegration (
Expand Down
5 changes: 4 additions & 1 deletion services/gmail/pod-gmail/src/__tests__/gmailClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ jest.mock('@hcengineering/core', () => {
jest.mock('@hcengineering/mail-common', () => ({
createMessages: jest.fn().mockResolvedValue(undefined),
getChannel: jest.fn().mockResolvedValue({ _id: 'test-channel-id' }),
isSyncedMessage: jest.fn().mockReturnValue(false)
isSyncedMessage: jest.fn().mockReturnValue(false),
getMessageExtra: jest.fn().mockReturnValue({}),
getMailHeaders: jest.fn().mockReturnValue([]),
isHulyMessage: jest.fn().mockReturnValue(false)
}))

jest.mock('googleapis', () => ({
Expand Down
22 changes: 10 additions & 12 deletions services/gmail/pod-gmail/src/gmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,20 @@ import {
isWorkspaceLoginInfo,
AccountClient
} from '@hcengineering/account-client'
import { MailRecipient, type SyncOptions, getChannel, isSyncedMessage } from '@hcengineering/mail-common'
import {
MailRecipient,
type SyncOptions,
getChannel,
getMailHeaders,
isSyncedMessage
} from '@hcengineering/mail-common'
import chat from '@hcengineering/chat'

import { encode64 } from './base64'
import config from './config'
import { GmailController } from './gmailController'
import { RateLimiter } from './rateLimiter'
import {
type ProjectCredentials,
type Token,
type User,
type SyncState,
HulyMailHeader,
HulyMessageIdHeader
} from './types'
import { type ProjectCredentials, type Token, type User, type SyncState, GmailMessageType } from './types'
import { addFooter, isToken, serviceToken, getKvsClient, createGmailSearchQuery } from './utils'
import type { WorkspaceClient } from './workspaceClient'
import { getOrCreateSocialId } from './accounts'
Expand All @@ -73,8 +72,7 @@ function makeHTMLBody (message: NewMessage, from: string): string {
'Content-Transfer-Encoding: 7bit\n',
`To: ${message.to} \n`,
`From: ${from} \n`,
`${HulyMailHeader}: true\n`,
`${HulyMessageIdHeader}: ${message._id}\n`
...getMailHeaders(GmailMessageType, message._id)
]

if (message.replyTo != null) {
Expand Down Expand Up @@ -385,7 +383,7 @@ export class GmailClient {
}

this.ctx.info('Sending gmail message', { id: message._id, email })
const gmailBody = await makeHTMLBodyV2(this.accountClient, message, thread, this.socialId._id, email)
const gmailBody = await makeHTMLBodyV2(this.ctx, this.accountClient, message, thread, this.socialId._id, email)
await this.rateLimiter.take(100)
await this.gmail.messages.send({
userId: 'me',
Expand Down
6 changes: 4 additions & 2 deletions services/gmail/pod-gmail/src/message/v2/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import {
EmailMessage,
getProducer,
MailRecipient,
getMessageExtra
getMessageExtra,
HulyMailHeader,
HulyMessageIdHeader
} from '@hcengineering/mail-common'
import { type KeyValueClient } from '@hcengineering/kvs-client'
import { AccountClient, isWorkspaceLoginInfo, WorkspaceLoginInfo } from '@hcengineering/account-client'
Expand All @@ -33,7 +35,7 @@ import { IMessageManager } from '../types'
import config from '../../config'
import { AttachmentHandler } from '../attachments'
import { decode64 } from '../../base64'
import { GmailMessageType, HulyMailHeader, HulyMessageIdHeader } from '../../types'
import { GmailMessageType } from '../../types'

export class MessageManagerV2 implements IMessageManager {
private wsInfo: WorkspaceLoginInfo | undefined = undefined
Expand Down
37 changes: 17 additions & 20 deletions services/gmail/pod-gmail/src/message/v2/send.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,42 @@
import { CreateMessageEvent } from '@hcengineering/communication-sdk-types'
import { type GaxiosResponse } from 'gaxios'
import { gmail_v1 } from 'googleapis'
import { markdownToHtml, getReplySubject } from '@hcengineering/mail-common'
import {
markdownToHtml,
getReplySubject,
getRecipients,
getMailHeaders,
HulyMailHeader,
HulyMessageIdHeader
} from '@hcengineering/mail-common'
import { Card } from '@hcengineering/card'
import { MeasureContext, PersonId } from '@hcengineering/core'
import { AccountClient } from '@hcengineering/account-client'

import { encode64 } from '../../base64'
import { addFooter } from '../../utils'
import { Card } from '@hcengineering/card'
import { PersonId, SocialIdType } from '@hcengineering/core'
import { AccountClient } from '@hcengineering/account-client'
import { HulyMailHeader, HulyMessageIdHeader } from '../../types'
import { GmailMessageType } from '../../types'

export async function makeHTMLBodyV2 (
ctx: MeasureContext,
accountClient: AccountClient,
message: CreateMessageEvent,
thread: Card,
personId: PersonId,
from: string
): Promise<string | undefined> {
const collaborators: PersonId[] = (thread as any).members ?? []
if (collaborators.length === 0) {
const recipients = await getRecipients(ctx, accountClient, thread, personId)
if (recipients === undefined) {
return undefined
}
const recipients = collaborators.length > 1 ? collaborators.filter((c) => c !== personId) : collaborators
const mailSocialIds = (await accountClient.findFullSocialIds(recipients)).filter(
(id) => id.type === SocialIdType.EMAIL
)
if (mailSocialIds.length === 0) {
console.warn('No social IDs found for recipients', { recipients })
return undefined
}
const to = mailSocialIds[0].value
const copy = mailSocialIds.length > 1 ? mailSocialIds.slice(1).map((s) => s.value) : []

const { to, copy } = recipients
const str = [
'Content-Type: text/html; charset="UTF-8"\n',
'MIME-Version: 1.0\n',
'Content-Transfer-Encoding: 7bit\n',
`To: ${to} \n`,
`From: ${from} \n`,
`${HulyMailHeader}: true\n`,
`${HulyMessageIdHeader}: ${message._id}\n`
...getMailHeaders(GmailMessageType, message._id)
]

// TODO: get reply-to from channel
Expand Down
2 changes: 0 additions & 2 deletions services/gmail/pod-gmail/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,3 @@ export interface SyncState {
}

export const GmailMessageType = 'gmail-message'
export const HulyMailHeader = 'X-Huly-Sent'
export const HulyMessageIdHeader = 'X-Huly-Message-Id'
Loading