UBERF-10408: Use tags for mails

Signed-off-by: Artem Savchenko <armisav@gmail.com>
This commit is contained in:
Artem Savchenko 2025-05-07 10:44:04 +07:00
parent 9fd47e04be
commit fdb9cc7cb1
15 changed files with 318 additions and 647 deletions

View File

@ -424,6 +424,9 @@ importers:
'@rush-temp/mail-assets':
specifier: file:./projects/mail-assets.tgz
version: file:projects/mail-assets.tgz(@babel/core@7.23.9)(@jest/types@29.6.3)(babel-jest@29.7.0(@babel/core@7.23.9))(esbuild@0.24.2)(ts-node@10.9.2(@types/node@20.11.19)(typescript@5.3.3))
'@rush-temp/mail-common':
specifier: file:./projects/mail-common.tgz
version: file:projects/mail-common.tgz(@babel/core@7.23.9)(@jest/types@29.6.3)(babel-jest@29.7.0(@babel/core@7.23.9))
'@rush-temp/mail-resources':
specifier: file:./projects/mail-resources.tgz
version: file:projects/mail-resources.tgz(@babel/core@7.23.9)(@jest/types@29.6.3)(@types/node@20.11.19)(babel-jest@29.7.0(@babel/core@7.23.9))(esbuild@0.24.2)(postcss-load-config@4.0.2(postcss@8.5.3)(ts-node@10.9.2(@types/node@20.11.19)(typescript@5.3.3)))(postcss@8.5.3)(ts-node@10.9.2(@types/node@20.11.19)(typescript@5.3.3))
@ -4404,6 +4407,10 @@ packages:
resolution: {integrity: sha512-EY+m2n2E2Sm4zD0/n7psqlOwOH/zSinZE0zDfPLKZCFMF++7wuZ7cpkEz9NbL5zELQwqA7sk/G/IwUVIEXB/Kg==, tarball: file:projects/mail-assets.tgz}
version: 0.0.0
'@rush-temp/mail-common@file:projects/mail-common.tgz':
resolution: {integrity: sha512-BWp0nl30Ff2fIpHrH1ZXi+f8pOFvIyun57kkxiApAu6uOuF/A4qkfHLPV8dpPZWMfkpT86XwQlZVucNw4zJ0Jg==, tarball: file:projects/mail-common.tgz}
version: 0.0.0
'@rush-temp/mail-resources@file:projects/mail-resources.tgz':
resolution: {integrity: sha512-495MjItvq3ep8/hMbrw8ITDPZQ8oNC+5h1ZLTU0uCLUAOVgu1BFVpRtBZ8nFAIxK2Aks5JpKg9No2070QDjDaQ==, tarball: file:projects/mail-resources.tgz}
version: 0.0.0
@ -4885,7 +4892,7 @@ packages:
version: 0.0.0
'@rush-temp/pod-gmail@file:projects/pod-gmail.tgz':
resolution: {integrity: sha512-0mw6Nvng0HK7Aftm21TzY6nHBhUURxKoaU6ASuWik+eSzhsJzocplksOQuVItxSDfKcNR+5Kjbj7Udg/dvLFjA==, tarball: file:projects/pod-gmail.tgz}
resolution: {integrity: sha512-mWpD7TVaoiHBcGFgqt2KoPioV7Uc/9Y7DXu3LKiwZBrUX6X+WR/77RcKq/gNGPbwNxCKOc+dDsscF5NKMhIqSg==, tarball: file:projects/pod-gmail.tgz}
version: 0.0.0
'@rush-temp/pod-inbound-mail@file:projects/pod-inbound-mail.tgz':
@ -19925,6 +19932,44 @@ snapshots:
- supports-color
- ts-node
'@rush-temp/mail-common@file:projects/mail-common.tgz(@babel/core@7.23.9)(@jest/types@29.6.3)(babel-jest@29.7.0(@babel/core@7.23.9))':
dependencies:
'@hcengineering/communication-rest-client': 0.1.174(typescript@5.7.3)
'@hcengineering/communication-types': 0.1.174(typescript@5.7.3)
'@tsconfig/node16': 1.0.4
'@types/express': 4.17.21
'@types/jest': 29.5.12
'@types/node': 20.11.19
'@types/sanitize-html': 2.15.0
'@types/turndown': 5.0.5
'@types/uuid': 8.3.4
'@typescript-eslint/eslint-plugin': 6.21.0(@typescript-eslint/parser@6.21.0(eslint@8.56.0)(typescript@5.3.3))(eslint@8.56.0)(typescript@5.7.3)
'@typescript-eslint/parser': 6.21.0(eslint@8.56.0)(typescript@5.7.3)
esbuild: 0.24.2
eslint: 8.56.0
eslint-config-standard-with-typescript: 40.0.0(@typescript-eslint/eslint-plugin@6.21.0(@typescript-eslint/parser@6.21.0(eslint@8.56.0)(typescript@5.3.3))(eslint@8.56.0)(typescript@5.3.3))(eslint-plugin-import@2.29.1(eslint@8.56.0))(eslint-plugin-n@15.7.0(eslint@8.56.0))(eslint-plugin-promise@6.1.1(eslint@8.56.0))(eslint@8.56.0)(typescript@5.7.3)
eslint-plugin-import: 2.29.1(eslint@8.56.0)
eslint-plugin-n: 15.7.0(eslint@8.56.0)
eslint-plugin-node: 11.1.0(eslint@8.56.0)
eslint-plugin-promise: 6.1.1(eslint@8.56.0)
jest: 29.7.0(@types/node@20.11.19)(ts-node@10.9.2(@types/node@20.11.19)(typescript@5.3.3))
prettier: 3.2.5
sanitize-html: 2.16.0
ts-jest: 29.1.2(@babel/core@7.23.9)(@jest/types@29.6.3)(babel-jest@29.7.0(@babel/core@7.23.9))(esbuild@0.24.2)(jest@29.7.0(@types/node@20.11.19)(ts-node@10.9.2(@types/node@20.11.19)(typescript@5.3.3)))(typescript@5.7.3)
ts-node: 10.9.2(@types/node@20.11.19)(typescript@5.7.3)
turndown: 7.2.0
typescript: 5.7.3
uuid: 8.3.2
transitivePeerDependencies:
- '@babel/core'
- '@jest/types'
- '@swc/core'
- '@swc/wasm'
- babel-jest
- babel-plugin-macros
- node-notifier
- supports-color
'@rush-temp/mail-resources@file:projects/mail-resources.tgz(@babel/core@7.23.9)(@jest/types@29.6.3)(@types/node@20.11.19)(babel-jest@29.7.0(@babel/core@7.23.9))(esbuild@0.24.2)(postcss-load-config@4.0.2(postcss@8.5.3)(ts-node@10.9.2(@types/node@20.11.19)(typescript@5.3.3)))(postcss@8.5.3)(ts-node@10.9.2(@types/node@20.11.19)(typescript@5.3.3))':
dependencies:
'@types/jest': 29.5.12
@ -22400,7 +22445,6 @@ snapshots:
ts-jest: 29.1.2(@babel/core@7.23.9)(@jest/types@29.6.3)(babel-jest@29.7.0(@babel/core@7.23.9))(esbuild@0.24.2)(jest@29.7.0(@types/node@20.11.19)(ts-node@10.9.2(@types/node@20.11.19)(typescript@5.3.3)))(typescript@5.3.3)
ts-node: 10.9.2(@types/node@20.11.19)(typescript@5.3.3)
ts-node-dev: 2.0.0(@types/node@20.11.19)(typescript@5.3.3)
turndown: 7.2.0
typescript: 5.3.3
uuid: 8.3.2
transitivePeerDependencies:

View File

@ -2502,6 +2502,11 @@
"packageName": "@hcengineering/pod-gmail-next",
"projectFolder": "services/gmail/pod-gmail-next",
"shouldPublish": false
},
{
"packageName": "@hcengineering/mail-common",
"projectFolder": "services/mail/mail-common",
"shouldPublish": false
}
]
}

View File

@ -64,10 +64,8 @@
"@hcengineering/chat": "^0.6.0",
"@hcengineering/client": "^0.6.18",
"@hcengineering/client-resources": "^0.6.27",
"@hcengineering/communication-rest-client": "0.1.172",
"@hcengineering/communication-types": "0.1.172",
"@hcengineering/contact": "^0.6.24",
"@hcengineering/mail": "^0.6.0",
"@hcengineering/mail-common": "^0.6.0",
"@hcengineering/core": "^0.6.32",
"@hcengineering/gmail": "^0.6.22",
"@hcengineering/platform": "^0.6.11",

View File

@ -1,113 +0,0 @@
import { parseNameFromEmailHeader } from '../message/message'
import { EmailContact } from '../types'
describe('parseNameFromEmailHeader', () => {
it('should parse email with name in double quotes', () => {
const input = '"John Doe" <john.doe@example.com>'
const expected: EmailContact = {
email: 'john.doe@example.com',
firstName: 'John',
lastName: 'Doe'
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
it('should parse email with name without quotes', () => {
const input = 'Jane Smith <jane.smith@example.com>'
const expected: EmailContact = {
email: 'jane.smith@example.com',
firstName: 'Jane',
lastName: 'Smith'
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
it('should parse email without name', () => {
const input = 'no-reply@example.com'
const expected: EmailContact = {
email: 'no-reply@example.com',
firstName: 'no-reply',
lastName: 'example.com'
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
it('should parse email with angle brackets only', () => {
const input = '<support@example.com>'
const expected: EmailContact = {
email: 'support@example.com',
firstName: 'support',
lastName: 'example.com'
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
it('should parse email with multi-word last name', () => {
const input = 'Maria Van Der Berg <maria@example.com>'
const expected: EmailContact = {
email: 'maria@example.com',
firstName: 'Maria',
lastName: 'Van Der Berg'
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
it('should handle undefined input', () => {
const expected: EmailContact = {
email: '',
firstName: '',
lastName: ''
}
expect(parseNameFromEmailHeader(undefined)).toEqual(expected)
})
it('should handle empty string input', () => {
const input = ''
const expected: EmailContact = {
email: '',
firstName: '',
lastName: ''
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
it('should handle malformed email formats', () => {
const input = 'John Doe john.doe@example.com'
const expected: EmailContact = {
email: 'John Doe john.doe@example.com',
firstName: 'John Doe john.doe',
lastName: 'example.com'
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
it('should parse single-name format', () => {
const input = 'Support <help@example.com>'
const expected: EmailContact = {
email: 'help@example.com',
firstName: 'Support',
lastName: 'example.com'
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
it('should handle name with special characters', () => {
const input = '"O\'Neill, James" <james.oneill@example.com>'
const expected: EmailContact = {
email: 'james.oneill@example.com',
firstName: "O'Neill,",
lastName: 'James'
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
})

View File

@ -13,21 +13,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
import { BaseConfig } from '@hcengineering/mail-common'
import { config as dotenvConfig } from 'dotenv'
dotenvConfig()
interface Config {
interface Config extends BaseConfig {
Port: number
AccountsURL: string
ServiceID: string
Secret: string
Credentials: string
WATCH_TOPIC_NAME: string
FooterMessage: string
InitLimit: number
KvsUrl: string
StorageConfig: string
}
const envMap: { [key in keyof Config]: string } = {

View File

@ -12,15 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
import { SocialId, type MeasureContext } from '@hcengineering/core'
import { type GaxiosResponse } from 'gaxios'
import { gmail_v1 } from 'googleapis'
import sanitizeHtml from 'sanitize-html'
import { SocialId, type MeasureContext } from '@hcengineering/core'
import { createMessages, parseNameFromEmailHeader } from '@hcengineering/mail-common'
import config from '../config'
import { AttachmentHandler } from './attachments'
import { decode64 } from '../base64'
import { createMessages } from './messageCard'
import { EmailMessage, EmailContact } from '../types'
import { EmailMessage } from '../types'
export class MessageManager {
constructor (
@ -34,7 +36,7 @@ export class MessageManager {
const res = convertMessage(message, me)
const attachments = await this.attachmentHandler.getPartFiles(message.data.payload, message.data.id ?? '')
await createMessages(this.ctx, this.token, res, attachments, me, this.socialId)
await createMessages(config, this.ctx, this.token, res, attachments, me, this.socialId)
}
}
@ -99,57 +101,3 @@ function convertMessage (message: GaxiosResponse<gmail_v1.Schema$Message>, me: s
sendOn: date.getTime()
}
}
export function parseNameFromEmailHeader (headerValue: string | undefined): EmailContact {
if (headerValue == null || headerValue.trim() === '') {
return {
email: '',
firstName: '',
lastName: ''
}
}
// Match pattern like: "Name" <email@example.com> or Name <email@example.com>
const nameEmailPattern = /^(?:"?([^"<]+)"?\s*)?<([^>]+)>$/
const match = headerValue.trim().match(nameEmailPattern)
if (match == null) {
const address = headerValue.trim()
const parts = address.split('@')
return {
email: address,
firstName: parts[0],
lastName: parts[1]
}
}
const displayName = match[1]?.trim()
const email = match[2].trim()
if (displayName == null || displayName === '') {
const parts = email.split('@')
return {
email,
firstName: parts[0],
lastName: parts[1]
}
}
const nameParts = displayName.split(/\s+/)
let firstName: string | undefined
let lastName: string | undefined
if (nameParts.length === 1) {
firstName = nameParts[0]
} else if (nameParts.length > 1) {
firstName = nameParts[0]
lastName = nameParts.slice(1).join(' ')
}
const parts = email.split('@')
return {
email,
firstName: firstName ?? parts[0],
lastName: lastName ?? parts[1]
}
}

View File

@ -1,323 +0,0 @@
//
// Copyright © 2025 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
import { getClient as getAccountClient, isWorkspaceLoginInfo } from '@hcengineering/account-client'
import { createRestTxOperations, createRestClient } from '@hcengineering/api-client'
import { type Card } from '@hcengineering/card'
import {
type RestClient as CommunicationClient,
createRestClient as getCommunicationClient
} from '@hcengineering/communication-rest-client'
import { MessageType } from '@hcengineering/communication-types'
import contact, { PersonSpace } from '@hcengineering/contact'
import {
type Blob,
type MeasureContext,
type PersonId,
type Ref,
type TxOperations,
type Doc,
generateId,
PersonUuid,
RateLimiter,
SocialIdType,
SocialId
} from '@hcengineering/core'
import mail from '@hcengineering/mail'
import chat from '@hcengineering/chat'
import { buildStorageFromConfig, storageConfigFromEnv } from '@hcengineering/server-storage'
import config from '../config'
import { type AttachedFile } from './types'
import { EmailMessage } from '../types'
import { getMdContent } from './utils'
export async function createMessages (
ctx: MeasureContext,
token: string,
message: EmailMessage,
attachments: AttachedFile[],
me: string,
socialId: SocialId
): Promise<void> {
const { mailId, from, subject, replyTo } = message
const tos = [message.to, ...(message.copy ?? [])]
ctx.info('Sending message', { mailId, from, to: tos.join(',') })
const accountClient = getAccountClient(config.AccountsURL, token)
const wsInfo = await accountClient.getLoginInfoByToken()
if (!isWorkspaceLoginInfo(wsInfo)) {
ctx.error('Unable to get workspace info', { mailId, from, tos })
return
}
const transactorUrl = wsInfo.endpoint.replace('ws://', 'http://').replace('wss://', 'https://')
const txClient = await createRestTxOperations(transactorUrl, wsInfo.workspace, wsInfo.token)
const msgClient = getCommunicationClient(wsInfo.endpoint, wsInfo.workspace, wsInfo.token)
const restClient = createRestClient(transactorUrl, wsInfo.workspace, wsInfo.token)
const fromPerson = await restClient.ensurePerson(SocialIdType.EMAIL, from.email, from.firstName, from.lastName)
const toPersons: { address: string, uuid: PersonUuid, socialId: PersonId }[] = []
for (const to of tos) {
const toPerson = await restClient.ensurePerson(SocialIdType.EMAIL, to.email, to.firstName, to.lastName)
if (toPerson === undefined) {
continue
}
toPersons.push({ address: to.email, ...toPerson })
}
if (toPersons.length === 0) {
ctx.error('Unable to create message without a proper TO', { mailId, from })
return
}
const modifiedBy = fromPerson.socialId
const participants = [fromPerson.socialId, ...toPersons.map((p) => p.socialId)]
const content = getMdContent(ctx, message)
const attachedBlobs: AttachedFile[] = []
if (config.StorageConfig !== undefined) {
const storageConfig = storageConfigFromEnv(config.StorageConfig)
const storageAdapter = buildStorageFromConfig(storageConfig)
try {
for (const a of attachments ?? []) {
try {
await storageAdapter.put(
ctx,
{
uuid: wsInfo.workspace,
url: wsInfo.workspaceUrl,
dataId: wsInfo.workspaceDataId
},
a.id,
a.data,
a.contentType
)
attachedBlobs.push(a)
ctx.info('Uploaded attachment', { mailId, blobId: a.id, name: a.name, contentType: a.contentType })
} catch (error) {
ctx.error('Failed to upload attachment', { name: a.name, error, mailId })
}
}
} finally {
await storageAdapter.close()
}
}
try {
const spaces = await getPersonSpaces(ctx, txClient, mailId, fromPerson.uuid, from.email)
if (spaces.length > 0) {
await saveMessageToSpaces(
ctx,
txClient,
msgClient,
mailId,
spaces,
participants,
modifiedBy,
subject,
content,
attachedBlobs,
me,
socialId,
message.sendOn,
replyTo
)
}
} catch (error) {
ctx.error('Failed to save message to personal spaces', {
error,
mailId,
personUuid: fromPerson.uuid,
email: from
})
}
for (const to of toPersons) {
try {
const spaces = await getPersonSpaces(ctx, txClient, mailId, to.uuid, to.address)
if (spaces.length > 0) {
await saveMessageToSpaces(
ctx,
txClient,
msgClient,
mailId,
spaces,
participants,
modifiedBy,
subject,
content,
attachedBlobs,
me,
socialId,
message.sendOn,
replyTo
)
}
} catch (error) {
ctx.error('Failed to save message spaces', { error, mailId, personUuid: to.uuid, email: to.address })
}
}
}
async function getPersonSpaces (
ctx: MeasureContext,
client: TxOperations,
mailId: string,
personUuid: PersonUuid,
email: string
): Promise<PersonSpace[]> {
const persons = await client.findAll(contact.class.Person, { personUuid }, { projection: { _id: 1 } })
const personRefs = persons.map((p) => p._id)
const spaces = await client.findAll(contact.class.PersonSpace, { person: { $in: personRefs } })
if (spaces.length === 0) {
ctx.warn('No personal space found, skip', { mailId, personUuid, email })
}
return spaces
}
async function saveMessageToSpaces (
ctx: MeasureContext,
client: TxOperations,
msgClient: CommunicationClient,
mailId: string,
spaces: PersonSpace[],
participants: PersonId[],
modifiedBy: PersonId,
subject: string,
content: string,
attachments: AttachedFile[],
me: string,
socialId: SocialId,
createdDate: number,
inReplyTo?: string
): Promise<void> {
const rateLimiter = new RateLimiter(10)
for (const space of spaces) {
const spaceId = space._id
await rateLimiter.add(async () => {
ctx.info('Saving message to space', { mailId, space: spaceId })
const route = await client.findOne(mail.class.MailRoute, { mailId, space: spaceId })
if (route !== undefined) {
ctx.info('Message is already in the thread, skip', { mailId, threadId: route.threadId, spaceId })
return
}
let threadId: Ref<Card> | undefined
if (inReplyTo !== undefined) {
const route = await client.findOne(mail.class.MailRoute, { mailId: inReplyTo, space: spaceId })
if (route !== undefined) {
threadId = route.threadId as Ref<Card>
ctx.info('Found existing thread', { mailId, threadId, spaceId })
}
}
if (threadId === undefined) {
const channel = await getOrCreateChannel(ctx, client, spaceId, participants, me, socialId)
const newThreadId = await client.createDoc(
chat.masterTag.Thread,
space._id,
{
title: subject,
description: content,
private: true,
members: participants,
archived: false,
createdBy: modifiedBy,
modifiedBy,
parent: channel
},
generateId(),
undefined,
modifiedBy
)
threadId = newThreadId as Ref<Card>
ctx.info('Created new thread', { mailId, threadId, spaceId })
}
const { id: messageId, created: messageCreated } = await msgClient.createMessage(
threadId,
chat.masterTag.Thread,
content,
modifiedBy,
MessageType.Message,
{
created: createdDate
}
)
ctx.info('Created message', { mailId, messageId, threadId, content })
for (const a of attachments) {
await msgClient.createFile(
threadId,
messageId,
messageCreated,
a.id as Ref<Blob>,
a.contentType,
a.name,
a.data.length,
modifiedBy
)
}
await client.createDoc(
mail.class.MailRoute,
space._id,
{
mailId,
threadId
},
generateId(),
undefined,
modifiedBy
)
})
}
await rateLimiter.waitProcessing()
}
async function getOrCreateChannel (
ctx: MeasureContext,
client: TxOperations,
space: Ref<PersonSpace>,
participants: PersonId[],
me: string,
socialId: SocialId
): Promise<Ref<Doc> | undefined> {
try {
const channel = await client.findOne(chat.masterTag.Channel, { title: me })
ctx.info('Existing channel', { me, space, channel })
if (channel != null) return channel._id
ctx.info('Creating new channel', { me, space })
return await client.createDoc(
chat.masterTag.Channel,
space,
{
title: me,
private: true,
members: participants,
archived: false,
createdBy: socialId._id
},
generateId(),
undefined,
socialId._id
)
} catch (err: any) {
ctx.error('Failed to create channel', { me, space })
return undefined
}
}

View File

@ -1,127 +0,0 @@
//
// Copyright © 2025 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
import {
type MeasureContext,
buildSocialIdString,
generateId,
PersonId,
PersonUuid,
SocialIdType,
TxOperations
} from '@hcengineering/core'
import contact, { AvatarType, combineName, SocialIdentityRef } from '@hcengineering/contact'
import { AccountClient } from '@hcengineering/account-client'
import { EmailContact } from '../types'
export async function ensureGlobalPerson (
ctx: MeasureContext,
client: AccountClient,
mailId: string,
contact: EmailContact
): Promise<{ socialId: PersonId, uuid: PersonUuid, firstName: string, lastName: string } | undefined> {
const socialKey = buildSocialIdString({ type: SocialIdType.EMAIL, value: contact.email })
const socialId = await client.findSocialIdBySocialKey(socialKey)
const uuid = await client.findPersonBySocialKey(socialKey)
if (socialId !== undefined && uuid !== undefined) {
return { socialId, uuid, firstName: contact.firstName, lastName: contact.lastName }
}
try {
const globalPerson = await client.ensurePerson(
SocialIdType.EMAIL,
contact.email,
contact.firstName,
contact.lastName
)
ctx.info('Created global person', { mailId, email: contact, personUuid: globalPerson.uuid })
return { ...globalPerson, firstName: contact.firstName, lastName: contact.lastName }
} catch (error) {
ctx.error('Failed to create global person', { mailId, error, email: contact })
}
return undefined
}
export async function ensureLocalPerson (
ctx: MeasureContext,
client: TxOperations,
mailId: string,
personUuid: PersonUuid,
personId: PersonId,
email: string,
firstName: string,
lastName: string
): Promise<void> {
let person = await client.findOne(contact.class.Person, { personUuid })
if (person === undefined) {
const newPersonId = await client.createDoc(
contact.class.Person,
contact.space.Contacts,
{
avatarType: AvatarType.COLOR,
name: combineName(firstName, lastName),
personUuid
},
generateId()
)
person = await client.findOne(contact.class.Person, { _id: newPersonId })
if (person === undefined) {
throw new Error(`Failed to create local person for ${personUuid}`)
} else {
ctx.info('Created local person', { mailId, personUuid, _id: person._id })
}
}
const socialId = await client.findOne(contact.class.SocialIdentity, {
attachedTo: person._id,
type: SocialIdType.EMAIL,
value: email
})
if (socialId === undefined) {
await client.addCollection(
contact.class.SocialIdentity,
contact.space.Contacts,
person._id,
contact.class.Person,
'socialIds',
{
key: buildSocialIdString({ type: SocialIdType.EMAIL, value: email }),
type: SocialIdType.EMAIL,
value: email
},
personId as SocialIdentityRef
)
ctx.info('Created local socialId', { mailId, personUuid, email })
}
const channel = await client.findOne(contact.class.Channel, {
attachedTo: person._id,
attachedToClass: contact.class.Person,
provider: contact.channelProvider.Email,
value: email
})
if (channel === undefined) {
await client.addCollection(
contact.class.Channel,
contact.space.Contacts,
person._id,
contact.class.Person,
'channels',
{
provider: contact.channelProvider.Email,
value: email
},
generateId()
)
ctx.info('Created channel', { mailId, personUuid, email })
}
}

View File

@ -23,7 +23,6 @@
"devDependencies": {
"@hcengineering/platform-rig": "^0.6.0",
"@tsconfig/node16": "^1.0.4",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/jest": "^29.5.5",
"@types/node": "~20.11.16",
@ -32,7 +31,6 @@
"@types/uuid": "^8.3.1",
"@typescript-eslint/eslint-plugin": "^6.11.0",
"@typescript-eslint/parser": "^6.11.0",
"cross-env": "~7.0.3",
"esbuild": "^0.24.2",
"eslint": "^8.54.0",
"eslint-config-standard-with-typescript": "^40.0.0",
@ -48,22 +46,15 @@
},
"dependencies": {
"@hcengineering/account-client": "^0.6.0",
"@hcengineering/analytics-service": "^0.6.0",
"@hcengineering/api-client": "^0.6.0",
"@hcengineering/card": "^0.6.0",
"@hcengineering/chat": "^0.6.0",
"@hcengineering/communication-rest-client": "0.1.174",
"@hcengineering/communication-types": "0.1.174",
"@hcengineering/contact": "^0.6.24",
"@hcengineering/core": "^0.6.32",
"@hcengineering/mail": "^0.6.0",
"@hcengineering/platform": "^0.6.11",
"@hcengineering/server-core": "^0.6.1",
"@hcengineering/server-storage": "^0.6.0",
"@hcengineering/server-token": "^0.6.11",
"cors": "^2.8.5",
"dotenv": "~16.0.0",
"eml-parse-js": "^1.2.0-beta.0",
"express": "^4.21.2",
"sanitize-html": "^2.15.0",
"turndown": "^7.2.0",
"uuid": "^8.3.2"

View File

@ -0,0 +1,113 @@
import { parseNameFromEmailHeader } from '../utils'
import { EmailContact } from '../types'
describe('parseNameFromEmailHeader', () => {
it('should parse email with name in double quotes', () => {
const input = '"John Doe" <john.doe@example.com>'
const expected: EmailContact = {
email: 'john.doe@example.com',
firstName: 'John',
lastName: 'Doe'
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
it('should parse email with name without quotes', () => {
const input = 'Jane Smith <jane.smith@example.com>'
const expected: EmailContact = {
email: 'jane.smith@example.com',
firstName: 'Jane',
lastName: 'Smith'
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
it('should parse email without name', () => {
const input = 'no-reply@example.com'
const expected: EmailContact = {
email: 'no-reply@example.com',
firstName: 'no-reply',
lastName: 'example.com'
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
it('should parse email with angle brackets only', () => {
const input = '<support@example.com>'
const expected: EmailContact = {
email: 'support@example.com',
firstName: 'support',
lastName: 'example.com'
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
it('should parse email with multi-word last name', () => {
const input = 'Maria Van Der Berg <maria@example.com>'
const expected: EmailContact = {
email: 'maria@example.com',
firstName: 'Maria',
lastName: 'Van Der Berg'
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
it('should handle undefined input', () => {
const expected: EmailContact = {
email: '',
firstName: '',
lastName: ''
}
expect(parseNameFromEmailHeader(undefined)).toEqual(expected)
})
it('should handle empty string input', () => {
const input = ''
const expected: EmailContact = {
email: '',
firstName: '',
lastName: ''
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
it('should handle malformed email formats', () => {
const input = 'John Doe john.doe@example.com'
const expected: EmailContact = {
email: 'John Doe john.doe@example.com',
firstName: 'John Doe john.doe',
lastName: 'example.com'
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
it('should parse single-name format', () => {
const input = 'Support <help@example.com>'
const expected: EmailContact = {
email: 'help@example.com',
firstName: 'Support',
lastName: 'example.com'
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
it('should handle name with special characters', () => {
const input = '"O\'Neill, James" <james.oneill@example.com>'
const expected: EmailContact = {
email: 'james.oneill@example.com',
firstName: "O'Neill,",
lastName: 'James'
}
expect(parseNameFromEmailHeader(input)).toEqual(expected)
})
})

View File

@ -0,0 +1,19 @@
//
// Copyright © 2025 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
export * from './base64'
export * from './message'
export * from './types'
export * from './utils'

View File

@ -20,6 +20,7 @@ import {
createRestClient as getCommunicationClient
} from '@hcengineering/communication-rest-client'
import { MessageType } from '@hcengineering/communication-types'
import chat from '@hcengineering/chat'
import contact, { PersonSpace } from '@hcengineering/contact'
import {
type Blob,
@ -35,7 +36,6 @@ import {
SocialId
} from '@hcengineering/core'
import mail from '@hcengineering/mail'
import chat from '@hcengineering/chat'
import { buildStorageFromConfig, storageConfigFromEnv } from '@hcengineering/server-storage'
@ -244,6 +244,15 @@ async function saveMessageToSpaces (
undefined,
modifiedBy
)
await client.createMixin(
newThreadId,
chat.masterTag.Thread,
space._id,
mail.tag.MailThread,
{},
Date.now(),
socialId._id
)
threadId = newThreadId as Ref<Card>
ctx.info('Created new thread', { mailId, threadId, spaceId })
}
@ -298,11 +307,11 @@ async function getOrCreateChannel (
socialId: SocialId
): Promise<Ref<Doc> | undefined> {
try {
const channel = await client.findOne(chat.masterTag.Channel, { title: me })
const channel = await client.findOne(mail.tag.MailChannel, { title: me })
ctx.info('Existing channel', { me, space, channel })
if (channel != null) return channel._id
ctx.info('Creating new channel', { me, space })
return await client.createDoc(
ctx.info('Creating new channel', { me, space, personId: socialId._id })
const channelId = await client.createDoc(
chat.masterTag.Channel,
space,
{
@ -310,10 +319,21 @@ async function getOrCreateChannel (
private: true,
members: participants,
archived: false,
createdBy: socialId._id
createdBy: socialId._id,
modifiedBy: socialId._id
},
generateId(),
undefined,
Date.now(),
socialId._id
)
ctx.info('Creating mixin', { me, space, personId: socialId._id, channelId })
await client.createMixin(
channelId,
chat.masterTag.Channel,
space,
mail.tag.MailChannel,
{},
Date.now(),
socialId._id
)
} catch (err: any) {

View File

@ -0,0 +1,42 @@
//
// Copyright © 2025 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
export class SyncMutex {
private readonly locks = new Map<string, Promise<void>>()
async lock (key: string): Promise<() => void> {
// Wait for any existing lock to be released
const currentLock = this.locks.get(key)
if (currentLock != null) {
await currentLock
}
// Create a new lock
let releaseFn!: () => void
const newLock = new Promise<void>((resolve) => {
releaseFn = resolve
})
// Store the lock
this.locks.set(key, newLock)
// Return the release function
return () => {
if (this.locks.get(key) === newLock) {
this.locks.delete(key)
}
releaseFn()
}
}
}

View File

@ -43,5 +43,6 @@ export interface EmailMessage {
export interface BaseConfig {
AccountsURL: string
KvsUrl: string
StorageConfig: string
}

View File

@ -14,8 +14,9 @@
//
import TurndownService from 'turndown'
import sanitizeHtml from 'sanitize-html'
import { EmailMessage } from './types'
import { MeasureContext } from '@hcengineering/core'
import { EmailContact, EmailMessage } from './types'
export function getMdContent (ctx: MeasureContext, email: EmailMessage): string {
if (email.content !== undefined) {
@ -29,3 +30,57 @@ export function getMdContent (ctx: MeasureContext, email: EmailMessage): string
}
return email.textContent
}
export function parseNameFromEmailHeader (headerValue: string | undefined): EmailContact {
if (headerValue == null || headerValue.trim() === '') {
return {
email: '',
firstName: '',
lastName: ''
}
}
// Match pattern like: "Name" <email@example.com> or Name <email@example.com>
const nameEmailPattern = /^(?:"?([^"<]+)"?\s*)?<([^>]+)>$/
const match = headerValue.trim().match(nameEmailPattern)
if (match == null) {
const address = headerValue.trim()
const parts = address.split('@')
return {
email: address,
firstName: parts[0],
lastName: parts[1]
}
}
const displayName = match[1]?.trim()
const email = match[2].trim()
if (displayName == null || displayName === '') {
const parts = email.split('@')
return {
email,
firstName: parts[0],
lastName: parts[1]
}
}
const nameParts = displayName.split(/\s+/)
let firstName: string | undefined
let lastName: string | undefined
if (nameParts.length === 1) {
firstName = nameParts[0]
} else if (nameParts.length > 1) {
firstName = nameParts[0]
lastName = nameParts.slice(1).join(' ')
}
const parts = email.split('@')
return {
email,
firstName: firstName ?? parts[0],
lastName: lastName ?? parts[1]
}
}