The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
feat(slack): render task links as Slack's typed task entity (#25303)
Abdul Rahman07c3a9390c1c
diff --git a/packages/twenty-apps/public/slack/SETUP.md b/packages/twenty-apps/public/slack/SETUP.mdindex 7b320f8fbe1..2c6cbbe4235 100644--- a/packages/twenty-apps/public/slack/SETUP.md+++ b/packages/twenty-apps/public/slack/SETUP.md@@ -161,7 +161,10 @@ mentions and DMs, at the cost of un-mentioned thread follow-ups.> `entity_details_requested` plus the `links:read` / `links:write` scopes for the> record link previews; that one does need a reconnect, and on an app created> from an older manifest the Work Object Previews toggle has to be enabled by-> hand as well (see Record link previews).+> hand as well (see Record link previews). Upgrading from any version before+> 0.9.0 adds the `slack#/entities/task` entity type, which has to be selected+> under Work Object Previews by hand; until it is, task links stop rendering a+> card at all. No new scopes or events, so no reconnect.### Interactivity@@ -305,11 +308,15 @@ On an existing install, add them and reconnect (disconnect, then **Addconnection** again) so the token picks up the scopes.The manifest turns the previews themselves on, through `features.rich_previews`-with the `slack#/entities/item` entity type they use. On an app created by hand,-or from a manifest older than 0.7.0, open **Work Object Previews** in the app-settings, enable the toggle and select the **Item** entity type. Without it Slack-ignores the unfurl metadata and no card ever renders, with nothing in the logs to-say why.+with the two entity types they use: `slack#/entities/item` for people,+companies, opportunities and notes, and `slack#/entities/task` for tasks, which+Slack renders with native status, due date and assignee fields. On an app+created by hand, or from a manifest older than 0.7.0, open **Work Object+Previews** in the app settings, enable the toggle and select both the **Item**+and **Task** entity types. An app created from a 0.7.x or 0.8.x manifest already+has the toggle on with **Item** selected, so it only needs **Task** added. A+type you leave unselected makes Slack ignore the unfurl metadata for those+records, so no card renders and nothing in the logs says why.Slack stamps your Slack app's icon on the corner of every record card forattribution, and an app without one gets Slack's generic placeholder. Upload thediff --git a/packages/twenty-apps/public/slack/package.json b/packages/twenty-apps/public/slack/package.jsonindex ae7ff46a37f..39ac01e2139 100644--- a/packages/twenty-apps/public/slack/package.json+++ b/packages/twenty-apps/public/slack/package.json@@ -1,6 +1,6 @@{"name": "@twentyhq/slack",- "version": "0.8.0",+ "version": "0.9.0","description": "Slack assistant and workflow steps for Twenty","license": "MIT","engines": {diff --git a/packages/twenty-apps/public/slack/slack-app-manifest.json b/packages/twenty-apps/public/slack/slack-app-manifest.jsonindex 5e2277b979e..696b07b3916 100644--- a/packages/twenty-apps/public/slack/slack-app-manifest.json+++ b/packages/twenty-apps/public/slack/slack-app-manifest.json@@ -19,7 +19,7 @@"unfurl_domains": ["<your-twenty-front-host>"],"rich_previews": {"is_active": true,- "entity_types": ["slack#/entities/item"]+ "entity_types": ["slack#/entities/item", "slack#/entities/task"]}},"oauth_config": {diff --git a/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-entity-field-type.ts b/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-entity-field-type.tsindex a1168706d8e..ba40e272ba5 100644--- a/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-entity-field-type.ts+++ b/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-entity-field-type.ts@@ -4,4 +4,5 @@ export const SLACK_ENTITY_FIELD_TYPE = {LINK: 'slack#/types/link',EMAIL: 'slack#/types/email',ENTITY_REF: 'slack#/types/entity_ref',+ USER: 'slack#/types/user',} as const;diff --git a/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-entity-type.ts b/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-entity-type.tsnew file mode 100644index 00000000000..b8975276591--- /dev/null+++ b/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-entity-type.ts@@ -0,0 +1,4 @@+export const SLACK_ENTITY_TYPE = {+ ITEM: 'slack#/entities/item',+ TASK: 'slack#/entities/task',+} as const;diff --git a/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-task-description-max-length.ts b/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-task-description-max-length.tsnew file mode 100644index 00000000000..54acc56406c--- /dev/null+++ b/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-task-description-max-length.ts@@ -0,0 +1 @@+export const SLACK_TASK_DESCRIPTION_MAX_LENGTH = 3000;diff --git a/packages/twenty-apps/public/slack/src/logic-functions/types/slack-unfurl-content.type.ts b/packages/twenty-apps/public/slack/src/logic-functions/types/slack-unfurl-content.type.tsnew file mode 100644index 00000000000..ab4ee4b95ca--- /dev/null+++ b/packages/twenty-apps/public/slack/src/logic-functions/types/slack-unfurl-content.type.ts@@ -0,0 +1,20 @@+import { type EntityCustomField, type TaskEntityFields } from '@slack/web-api';++import { type SLACK_ENTITY_TYPE } from 'src/logic-functions/constants/slack-entity-type';++type SlackUnfurlContentAttributes = {+ title: string;+ iconUrl?: string;+};++export type SlackUnfurlContent = SlackUnfurlContentAttributes &+ (+ | {+ entityType: (typeof SLACK_ENTITY_TYPE)['ITEM'];+ customFields: (EntityCustomField | undefined)[];+ }+ | {+ entityType: (typeof SLACK_ENTITY_TYPE)['TASK'];+ fields: TaskEntityFields;+ }+ );diff --git a/packages/twenty-apps/public/slack/src/logic-functions/utils/__tests__/build-slack-record-unfurl-entity.test.ts b/packages/twenty-apps/public/slack/src/logic-functions/utils/__tests__/build-slack-record-unfurl-entity.test.tsindex a71b7701d7d..c7a6b94d236 100644--- a/packages/twenty-apps/public/slack/src/logic-functions/utils/__tests__/build-slack-record-unfurl-entity.test.ts+++ b/packages/twenty-apps/public/slack/src/logic-functions/utils/__tests__/build-slack-record-unfurl-entity.test.ts@@ -53,6 +53,26 @@ const buildOpportunityRecord = () => ({updatedAt: '2026-02-01T00:00:00.000Z',});+const TASK_ID = '20202020-0713-4b29-8f43-4444e2f6a4b4';++const buildTaskRecordLink = () => ({+ sharedUrl: `${WORKSPACE_BASE_URL}/object/task/${TASK_ID}`,+ canonicalUrl: `${WORKSPACE_BASE_URL}/object/task/${TASK_ID}`,+ objectNameSingular: 'task' as const,+ recordId: TASK_ID,+});++const buildTaskRecord = () => ({+ id: TASK_ID,+ title: 'Follow up with ACME',+ status: 'IN_PROGRESS',+ dueAt: '2026-03-01T00:00:00.000Z',+ assignee: { name: { firstName: 'Ada', lastName: 'Lovelace' } },+ bodyV2: { markdown: 'Ping them about the renewal' },+ createdAt: '2026-01-01T00:00:00.000Z',+ updatedAt: '2026-02-01T00:00:00.000Z',+});+describe('buildSlackRecordUnfurlEntity', () => {it('should build an item entity for a person card', () => {const entity = buildSlackRecordUnfurlEntity({@@ -218,4 +238,90 @@ describe('buildSlackRecordUnfurlEntity', () => {entity?.entity_payload.custom_fields?.map((field) => field.key),).toContain('amount');});++ it('should build a typed task entity rather than a generic item', () => {+ const entity = buildSlackRecordUnfurlEntity({+ recordLink: buildTaskRecordLink(),+ record: buildTaskRecord(),+ workspaceBaseUrls: [WORKSPACE_BASE_URL],+ });++ expect(entity?.entity_type).toBe('slack#/entities/task');+ expect(entity?.entity_payload.custom_fields).toBeUndefined();+ });++ it('should keep the task card to status and due date', () => {+ const entity = buildSlackRecordUnfurlEntity({+ recordLink: buildTaskRecordLink(),+ record: buildTaskRecord(),+ workspaceBaseUrls: [WORKSPACE_BASE_URL],+ });++ expect(entity?.entity_payload.fields).toEqual({+ status: { label: 'Status', value: 'In progress' },+ due_date: {+ label: 'Due date',+ type: 'slack#/types/timestamp',+ value: Math.floor(Date.parse('2026-03-01T00:00:00.000Z') / 1000),+ },+ });+ });++ it('should add the assignee and body to the task flexpane', () => {+ const entity = buildSlackRecordUnfurlEntity({+ recordLink: buildTaskRecordLink(),+ record: buildTaskRecord(),+ workspaceBaseUrls: [WORKSPACE_BASE_URL],+ includeDetails: true,+ });++ expect(entity?.entity_payload.fields).toMatchObject({+ assignee: {+ label: 'Assignee',+ type: 'slack#/types/user',+ user: { text: 'Ada Lovelace' },+ },+ description: {+ label: 'Body',+ value: 'Ping them about the renewal',+ long: true,+ },+ date_created: { label: 'Created' },+ date_updated: { label: 'Updated' },+ });+ });++ it('should omit the fields payload for a task with nothing to show', () => {+ const entity = buildSlackRecordUnfurlEntity({+ recordLink: buildTaskRecordLink(),+ record: { id: TASK_ID, title: 'Follow up with ACME' },+ workspaceBaseUrls: [WORKSPACE_BASE_URL],+ });++ expect(entity?.entity_payload.fields).toBeUndefined();+ });++ it('should still build a generic item entity for a person', () => {+ const entity = buildSlackRecordUnfurlEntity({+ recordLink: buildPersonRecordLink(),+ record: buildPersonRecord(),+ workspaceBaseUrls: [WORKSPACE_BASE_URL],+ });++ expect(entity?.entity_type).toBe('slack#/entities/item');+ expect(entity?.entity_payload.fields).toBeUndefined();+ });++ it('should not clip a task body at the card preview length', () => {+ const entity = buildSlackRecordUnfurlEntity({+ recordLink: buildTaskRecordLink(),+ record: { ...buildTaskRecord(), bodyV2: { markdown: 'a'.repeat(1000) } },+ workspaceBaseUrls: [WORKSPACE_BASE_URL],+ includeDetails: true,+ });++ expect(entity?.entity_payload.fields).toMatchObject({+ description: { value: 'a'.repeat(1000) },+ });+ });});diff --git a/packages/twenty-apps/public/slack/src/logic-functions/utils/__tests__/read-slack-body-preview.test.ts b/packages/twenty-apps/public/slack/src/logic-functions/utils/__tests__/read-slack-body-preview.test.tsnew file mode 100644index 00000000000..188f809fb62--- /dev/null+++ b/packages/twenty-apps/public/slack/src/logic-functions/utils/__tests__/read-slack-body-preview.test.ts@@ -0,0 +1,61 @@+import { describe, expect, it } from 'vitest';++import { readSlackBodyPreview } from 'src/logic-functions/utils/read-slack-body-preview';++describe('readSlackBodyPreview', () => {+ it('should read the markdown body', () => {+ expect(+ readSlackBodyPreview({ bodyValue: { markdown: ' Call them back ' } }),+ ).toBe('Call them back');+ });++ it('should truncate a long body', () => {+ const preview = readSlackBodyPreview({+ bodyValue: { markdown: 'a'.repeat(400) },+ });++ expect(preview).toBe(`${'a'.repeat(299)}β¦`);+ });++ it('should keep composite emoji whole when truncating', () => {+ const preview = readSlackBodyPreview({+ bodyValue: { markdown: 'π¨βπ©βπ§βπ¦'.repeat(400) },+ });++ expect(preview).toBe(`${'π¨βπ©βπ§βπ¦'.repeat(27)}β¦`);+ });++ it('should keep a truncated emoji body within the maximum length', () => {+ const preview = readSlackBodyPreview({+ bodyValue: { markdown: 'π¨βπ©βπ§βπ¦'.repeat(400) },+ maxLength: 3000,+ });++ expect(preview?.length).toBeLessThanOrEqual(3000);+ });++ it('should honour a larger maximum length', () => {+ const preview = readSlackBodyPreview({+ bodyValue: { markdown: 'a'.repeat(400) },+ maxLength: 3000,+ });++ expect(preview).toBe('a'.repeat(400));+ });++ it.each([undefined, {}, { markdown: ' ' }, { markdown: 42 }])(+ 'should return undefined for %s',+ (bodyValue) => {+ expect(readSlackBodyPreview({ bodyValue })).toBeUndefined();+ },+ );++ it('should count the ellipsis inside the maximum length', () => {+ const preview = readSlackBodyPreview({+ bodyValue: { markdown: 'a'.repeat(5000) },+ maxLength: 3000,+ });++ expect(preview).toBe(`${'a'.repeat(2999)}β¦`);+ });+});diff --git a/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-body-preview-field.ts b/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-body-preview-field.tsindex 7d26a1ee1b4..018d6607154 100644--- a/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-body-preview-field.ts+++ b/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-body-preview-field.ts@@ -1,33 +1,21 @@import { type EntityCustomField } from '@slack/web-api';-import { isNonEmptyString } from '@sniptt/guards';+import { isDefined } from 'twenty-sdk/utils';import { SLACK_ENTITY_FIELD_TYPE } from 'src/logic-functions/constants/slack-entity-field-type';-import { asRecord } from 'src/logic-functions/utils/as-record.util';-import { readOptionalString } from 'src/logic-functions/utils/read-optional-string.util';--const BODY_PREVIEW_MAX_LENGTH = 300;+import { readSlackBodyPreview } from 'src/logic-functions/utils/read-slack-body-preview';export const buildSlackBodyPreviewField = (bodyValue: unknown,): EntityCustomField | undefined => {- const markdown = readOptionalString(asRecord(bodyValue)?.markdown)?.trim();-- if (!isNonEmptyString(markdown)) {- return undefined;- }-- const codePoints = [...markdown];-- const preview =- codePoints.length > BODY_PREVIEW_MAX_LENGTH- ? `${codePoints.slice(0, BODY_PREVIEW_MAX_LENGTH).join('')}β¦`- : markdown;+ const preview = readSlackBodyPreview({ bodyValue });- return {- key: 'body',- label: 'Body',- type: SLACK_ENTITY_FIELD_TYPE.STRING,- value: preview,- long: true,- };+ return isDefined(preview)+ ? {+ key: 'body',+ label: 'Body',+ type: SLACK_ENTITY_FIELD_TYPE.STRING,+ value: preview,+ long: true,+ }+ : undefined;};diff --git a/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-entity-payload-fields.ts b/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-entity-payload-fields.tsnew file mode 100644index 00000000000..b640886f357--- /dev/null+++ b/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-entity-payload-fields.ts@@ -0,0 +1,22 @@+import { type EntityMetadata } from '@slack/web-api';+import { isNonEmptyArray } from '@sniptt/guards';+import { isDefined } from 'twenty-sdk/utils';++import { SLACK_ENTITY_TYPE } from 'src/logic-functions/constants/slack-entity-type';+import { type SlackUnfurlContent } from 'src/logic-functions/types/slack-unfurl-content.type';++export const buildSlackEntityPayloadFields = (+ content: SlackUnfurlContent,+): Pick<EntityMetadata['entity_payload'], 'fields' | 'custom_fields'> => {+ if (content.entityType === SLACK_ENTITY_TYPE.TASK) {+ return isNonEmptyArray(Object.keys(content.fields))+ ? { fields: content.fields }+ : {};+ }++ const definedCustomFields = content.customFields.filter(isDefined);++ return isNonEmptyArray(definedCustomFields)+ ? { custom_fields: definedCustomFields }+ : {};+};diff --git a/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-record-content.ts b/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-record-content.tsindex 85cf62ace5c..648ad4e93d2 100644--- a/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-record-content.ts+++ b/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-record-content.ts@@ -1,7 +1,8 @@-import { type EntityCustomField } from '@slack/web-api';import { isDefined } from 'twenty-sdk/utils';import { SLACK_ENTITY_FIELD_TYPE } from 'src/logic-functions/constants/slack-entity-field-type';+import { SLACK_ENTITY_TYPE } from 'src/logic-functions/constants/slack-entity-type';+import { type SlackUnfurlContent } from 'src/logic-functions/types/slack-unfurl-content.type';import { type SlackUnfurlObjectName } from 'src/logic-functions/types/slack-unfurl-object-name.type';import { asRecord } from 'src/logic-functions/utils/as-record.util';import { buildFullName } from 'src/logic-functions/utils/build-full-name';@@ -10,6 +11,7 @@ import { buildSlackCompanyRefField } from 'src/logic-functions/utils/build-slackimport { buildSlackLinkedinField } from 'src/logic-functions/utils/build-slack-linkedin-field';import { buildSlackRecordRefField } from 'src/logic-functions/utils/build-slack-record-ref-field';import { buildSlackStringField } from 'src/logic-functions/utils/build-slack-string-field';+import { buildSlackTaskEntityFields } from 'src/logic-functions/utils/build-slack-task-entity-fields';import { buildSlackTimestampDetailFields } from 'src/logic-functions/utils/build-slack-timestamp-detail-fields';import { buildSlackTimestampField } from 'src/logic-functions/utils/build-slack-timestamp-field';import { formatAmount } from 'src/logic-functions/utils/format-amount';@@ -25,12 +27,6 @@ type SlackUnfurlContentArgs = {includeDetails: boolean;};-type SlackUnfurlContent = {- title: string;- customFields: (EntityCustomField | undefined)[];- iconUrl?: string;-};-const buildPersonContent = ({record,workspaceBaseUrls,@@ -44,6 +40,7 @@ const buildPersonContent = ({: undefined;return {+ entityType: SLACK_ENTITY_TYPE.ITEM,title: buildFullName(record.name) ?? '',iconUrl: getPublicAvatarUrl({avatarUrl: record.avatarUrl,@@ -92,6 +89,7 @@ const buildCompanyContent = ({const address = asRecord(record.address);return {+ entityType: SLACK_ENTITY_TYPE.ITEM,title: readOptionalString(record.name) ?? '',iconUrl: getCompanyLogoUrl(domainUrl),customFields: [@@ -143,6 +141,7 @@ const buildOpportunityContent = ({const pointOfContactName = buildFullName(pointOfContact?.name);return {+ entityType: SLACK_ENTITY_TYPE.ITEM,title: readOptionalString(record.name) ?? '',iconUrl: getCompanyLogoUrl(readOptionalString(asRecord(company?.domainName)?.primaryLinkUrl),@@ -192,6 +191,7 @@ const buildNoteContent = ({record,includeDetails,}: SlackUnfurlContentArgs): SlackUnfurlContent => ({+ entityType: SLACK_ENTITY_TYPE.ITEM,title: readOptionalString(record.title) ?? '',customFields: includeDetails? [@@ -210,36 +210,11 @@ const buildNoteContent = ({const buildTaskContent = ({record,includeDetails,-}: SlackUnfurlContentArgs): SlackUnfurlContent => {- const status = readOptionalString(record.status);-- return {- title: readOptionalString(record.title) ?? '',- customFields: [- buildSlackStringField({- key: 'status',- label: 'Status',- value: isDefined(status) ? humanizeSelectValue(status) : undefined,- }),- buildSlackTimestampField({- key: 'dueAt',- label: 'Due date',- value: record.dueAt,- }),- ...(includeDetails- ? [- buildSlackStringField({- key: 'assignee',- label: 'Assignee',- value: buildFullName(asRecord(record.assignee)?.name),- }),- buildSlackBodyPreviewField(record.bodyV2),- ...buildSlackTimestampDetailFields(record),- ]- : []),- ],- };-};+}: SlackUnfurlContentArgs): SlackUnfurlContent => ({+ entityType: SLACK_ENTITY_TYPE.TASK,+ title: readOptionalString(record.title) ?? '',+ fields: buildSlackTaskEntityFields({ record, includeDetails }),+});export const SLACK_RECORD_CONTENT_BUILDERS: Record<SlackUnfurlObjectName,diff --git a/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-record-unfurl-entity.ts b/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-record-unfurl-entity.tsindex 0221a2fed19..9bdededaab2 100644--- a/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-record-unfurl-entity.ts+++ b/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-record-unfurl-entity.ts@@ -5,10 +5,9 @@ import { isDefined } from 'twenty-sdk/utils';import { type SlackUnfurlObjectName } from 'src/logic-functions/types/slack-unfurl-object-name.type';import { type SlackRecordLink } from 'src/logic-functions/types/slack-record-link.type';import { SLACK_RECORD_CONTENT_BUILDERS } from 'src/logic-functions/utils/build-slack-record-content';+import { buildSlackEntityPayloadFields } from 'src/logic-functions/utils/build-slack-entity-payload-fields';import { toEpochSeconds } from 'src/logic-functions/utils/to-epoch-seconds';-const ITEM_ENTITY_TYPE = 'slack#/entities/item';-const TWENTY_PRODUCT_ICON_URL ='https://raw.githubusercontent.com/twentyhq/twenty/main/packages/twenty-front/public/images/icons/ios/192.png';@@ -31,34 +30,33 @@ export const buildSlackRecordUnfurlEntity = ({workspaceBaseUrls: string[];includeDetails?: boolean;}): EntityMetadata | undefined => {- const { title, customFields, iconUrl } = SLACK_RECORD_CONTENT_BUILDERS[- recordLink.objectNameSingular- ]({ record, workspaceBaseUrls, includeDetails });+ const content = SLACK_RECORD_CONTENT_BUILDERS[recordLink.objectNameSingular]({+ record,+ workspaceBaseUrls,+ includeDetails,+ });- if (!isNonEmptyString(title)) {+ if (!isNonEmptyString(content.title)) {return undefined;}- const definedCustomFields = customFields.filter(isDefined);const metadataLastModified = toEpochSeconds(record.updatedAt);return {- entity_type: ITEM_ENTITY_TYPE,+ entity_type: content.entityType,entity_payload: {attributes: {- title: { text: title },+ title: { text: content.title },display_type: DISPLAY_TYPE_BY_OBJECT[recordLink.objectNameSingular],product_name: 'Twenty',- product_icon: isDefined(iconUrl)- ? { alt_text: title, url: iconUrl }+ product_icon: isDefined(content.iconUrl)+ ? { alt_text: content.title, url: content.iconUrl }: { alt_text: 'Twenty', url: TWENTY_PRODUCT_ICON_URL },...(isDefined(metadataLastModified)? { metadata_last_modified: metadataLastModified }: {}),},- ...(definedCustomFields.length > 0- ? { custom_fields: definedCustomFields }- : {}),+ ...buildSlackEntityPayloadFields(content),},external_ref: {id: recordLink.recordId,diff --git a/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-task-entity-fields.ts b/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-task-entity-fields.tsnew file mode 100644index 00000000000..713820db33a--- /dev/null+++ b/packages/twenty-apps/public/slack/src/logic-functions/utils/build-slack-task-entity-fields.ts@@ -0,0 +1,71 @@+import { type TaskEntityFields } from '@slack/web-api';+import { isDefined } from 'twenty-sdk/utils';++import { SLACK_ENTITY_FIELD_TYPE } from 'src/logic-functions/constants/slack-entity-field-type';+import { SLACK_TASK_DESCRIPTION_MAX_LENGTH } from 'src/logic-functions/constants/slack-task-description-max-length';+import { asRecord } from 'src/logic-functions/utils/as-record.util';+import { buildFullName } from 'src/logic-functions/utils/build-full-name';+import { humanizeSelectValue } from 'src/logic-functions/utils/humanize-select-value';+import { readOptionalString } from 'src/logic-functions/utils/read-optional-string.util';+import { readSlackBodyPreview } from 'src/logic-functions/utils/read-slack-body-preview';+import { toEpochSeconds } from 'src/logic-functions/utils/to-epoch-seconds';++export const buildSlackTaskEntityFields = ({+ record,+ includeDetails,+}: {+ record: Record<string, unknown>;+ includeDetails: boolean;+}): TaskEntityFields => {+ const status = readOptionalString(record.status);+ const dueDate = toEpochSeconds(record.dueAt);++ const cardFields: TaskEntityFields = {+ ...(isDefined(status)+ ? { status: { label: 'Status', value: humanizeSelectValue(status) } }+ : {}),+ ...(isDefined(dueDate)+ ? {+ due_date: {+ label: 'Due date',+ type: SLACK_ENTITY_FIELD_TYPE.TIMESTAMP,+ value: dueDate,+ },+ }+ : {}),+ };++ if (!includeDetails) {+ return cardFields;+ }++ const assignee = buildFullName(asRecord(record.assignee)?.name);+ const description = readSlackBodyPreview({+ bodyValue: record.bodyV2,+ maxLength: SLACK_TASK_DESCRIPTION_MAX_LENGTH,+ });+ const dateCreated = toEpochSeconds(record.createdAt);+ const dateUpdated = toEpochSeconds(record.updatedAt);++ return {+ ...cardFields,+ ...(isDefined(assignee)+ ? {+ assignee: {+ label: 'Assignee',+ type: SLACK_ENTITY_FIELD_TYPE.USER,+ user: { text: assignee },+ },+ }+ : {}),+ ...(isDefined(description)+ ? { description: { label: 'Body', value: description, long: true } }+ : {}),+ ...(isDefined(dateCreated)+ ? { date_created: { label: 'Created', value: dateCreated } }+ : {}),+ ...(isDefined(dateUpdated)+ ? { date_updated: { label: 'Updated', value: dateUpdated } }+ : {}),+ };+};diff --git a/packages/twenty-apps/public/slack/src/logic-functions/utils/read-slack-body-preview.ts b/packages/twenty-apps/public/slack/src/logic-functions/utils/read-slack-body-preview.tsnew file mode 100644index 00000000000..a44f60bc4f2--- /dev/null+++ b/packages/twenty-apps/public/slack/src/logic-functions/utils/read-slack-body-preview.ts@@ -0,0 +1,37 @@+import { isNonEmptyString } from '@sniptt/guards';++import { asRecord } from 'src/logic-functions/utils/as-record.util';+import { readOptionalString } from 'src/logic-functions/utils/read-optional-string.util';++const BODY_PREVIEW_MAX_LENGTH = 300;++const GRAPHEME_SEGMENTER = new Intl.Segmenter(undefined, {+ granularity: 'grapheme',+});++export const readSlackBodyPreview = ({+ bodyValue,+ maxLength = BODY_PREVIEW_MAX_LENGTH,+}: {+ bodyValue: unknown;+ maxLength?: number;+}): string | undefined => {+ const markdown = readOptionalString(asRecord(bodyValue)?.markdown)?.trim();++ if (!isNonEmptyString(markdown)) {+ return undefined;+ }++ if (markdown.length <= maxLength) {+ return markdown;+ }++ // Slack counts the characters it stores, so the budget is code units, cut on+ // grapheme boundaries to keep sequences such as π¨βπ©βπ§βπ¦ whole+ const truncated = [...GRAPHEME_SEGMENTER.segment(markdown)]+ .filter(({ index, segment }) => index + segment.length <= maxLength - 1)+ .map(({ segment }) => segment)+ .join('');++ return `${truncated}β¦`;+};diff --git a/packages/twenty-apps/public/slack/tsconfig.json b/packages/twenty-apps/public/slack/tsconfig.jsonindex abacf4dcf6d..5063445b1ea 100644--- a/packages/twenty-apps/public/slack/tsconfig.json+++ b/packages/twenty-apps/public/slack/tsconfig.json@@ -18,7 +18,7 @@"strictBindCallApply": false,"target": "es2020","module": "esnext",- "lib": ["es2020", "dom"],+ "lib": ["es2020", "es2022.intl", "dom"],"skipLibCheck": true,"skipDefaultLibCheck": true,"resolveJsonModule": true,