Skip to content
Draft
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
2 changes: 2 additions & 0 deletions apps/web/app/(ee)/api/cron/messages/notify-partner/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createId } from "@/lib/api/create-id";
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
import { messageAttachmentsOrderBy } from "@/lib/messages/utils";
import { prisma } from "@/lib/prisma";
import { sendBatchEmail } from "@dub/email";
import NewMessageFromProgram from "@dub/email/templates/new-message-from-program";
Expand Down Expand Up @@ -55,6 +56,7 @@ export async function POST(req: Request) {
include: {
senderUser: true,
attachments: {
orderBy: messageAttachmentsOrderBy,
select: {
name: true,
size: true,
Expand Down
2 changes: 2 additions & 0 deletions apps/web/app/(ee)/api/cron/messages/notify-program/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createId } from "@/lib/api/create-id";
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
import { messageAttachmentsOrderBy } from "@/lib/messages/utils";
import { prisma } from "@/lib/prisma";
import { sendBatchEmail } from "@dub/email";
import NewMessageFromPartner from "@dub/email/templates/new-message-from-partner";
Expand Down Expand Up @@ -57,6 +58,7 @@ export async function POST(req: Request) {
include: {
senderPartner: true,
attachments: {
orderBy: messageAttachmentsOrderBy,
select: {
name: true,
size: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
intercomCredentialsSchema,
} from "@/lib/integrations/intercom/schema";
import { PROGRAM_ALLOWED_ATTACHMENT_TYPES } from "@/lib/messages/constants";
import { sanitizeFileName } from "@/lib/messages/utils";
import {
mapMessageAttachmentsForCreate,
sanitizeFileName,
} from "@/lib/messages/utils";
import { prisma } from "@/lib/prisma";
import { storage } from "@/lib/storage";
import {
Expand Down Expand Up @@ -112,13 +115,7 @@ export async function handleConversationAdminReplied({
text: originalMessage,
...(storedAttachments.length > 0 && {
attachments: {
create: storedAttachments.map((attachment) => ({
id: createId({ prefix: "msa_" }),
storageKey: attachment.storageKey,
name: attachment.name,
size: attachment.size,
type: attachment.type,
})),
create: mapMessageAttachmentsForCreate(storedAttachments),
},
}),
},
Expand Down
5 changes: 4 additions & 1 deletion apps/web/app/(ee)/api/messages/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
PartnerMessagesSchema,
getPartnerMessagesQuerySchema,
} from "@/lib/messages/schemas";
import { messageAttachmentsOrderBy } from "@/lib/messages/utils";
import { prisma } from "@/lib/prisma";
import { NextResponse } from "next/server";

Expand Down Expand Up @@ -47,7 +48,9 @@ export const GET = withWorkspace(
include: {
senderPartner: true,
senderUser: true,
attachments: true,
attachments: {
orderBy: messageAttachmentsOrderBy,
},
},
orderBy: {
[sortBy]: sortOrder,
Expand Down
5 changes: 4 additions & 1 deletion apps/web/app/(ee)/api/partner-profile/messages/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ProgramMessagesSchema,
getProgramMessagesQuerySchema,
} from "@/lib/messages/schemas";
import { messageAttachmentsOrderBy } from "@/lib/messages/utils";
import { prisma } from "@/lib/prisma";
import { NextResponse } from "next/server";

Expand Down Expand Up @@ -78,7 +79,9 @@ export const GET = withPartnerProfile(async ({ partner, searchParams }) => {
include: {
senderPartner: true,
senderUser: true,
attachments: true,
attachments: {
orderBy: messageAttachmentsOrderBy,
},
},
orderBy: {
[sortBy]: sortOrder,
Expand Down
5 changes: 4 additions & 1 deletion apps/web/lib/messages/enrich.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { sortMessageAttachments } from "@/lib/messages/utils";
import { storage } from "@/lib/storage";
import { Message, MessageAttachment } from "@prisma/client";

export async function enrichMessage(
message: Message & { attachments: MessageAttachment[] },
) {
const attachments = sortMessageAttachments(message.attachments);

return {
...message,
attachments: await Promise.all(message.attachments.map(enrichAttachment)),
attachments: await Promise.all(attachments.map(enrichAttachment)),
};
}

Expand Down
16 changes: 8 additions & 8 deletions apps/web/lib/messages/message-partner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import * as z from "zod/v4";
import { authActionClient } from "../actions/safe-action";
import { throwIfNoPermission } from "../actions/throw-if-no-permission";
import { MessageSchema, messagePartnerSchema } from "./schemas";
import {
mapMessageAttachmentsForCreate,
messageAttachmentsOrderBy,
} from "./utils";

const schema = messagePartnerSchema
.extend({
Expand Down Expand Up @@ -136,20 +140,16 @@ export const messagePartnerAction = authActionClient
text,
...(attachments.length > 0 && {
attachments: {
create: attachments.map((att) => ({
id: createId({ prefix: "msa_" }),
storageKey: att.storageKey,
name: att.name,
size: att.size,
type: att.type,
})),
create: mapMessageAttachmentsForCreate(attachments),
},
}),
},
include: {
senderUser: true,
senderPartner: true,
attachments: true,
attachments: {
orderBy: messageAttachmentsOrderBy,
},
},
});

Expand Down
16 changes: 8 additions & 8 deletions apps/web/lib/messages/message-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { APP_DOMAIN_WITH_NGROK, INTERCOM_INTEGRATION_ID } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { authPartnerActionClient } from "../actions/safe-action";
import { MessageSchema, messageProgramSchema } from "./schemas";
import {
mapMessageAttachmentsForCreate,
messageAttachmentsOrderBy,
} from "./utils";

const schema = messageProgramSchema.refine(
(data) => data.text.trim().length > 0 || data.attachments.length > 0,
Expand Down Expand Up @@ -81,20 +85,16 @@ export const messageProgramAction = authPartnerActionClient
text,
...(attachments.length > 0 && {
attachments: {
create: attachments.map((att) => ({
id: createId({ prefix: "msa_" }),
storageKey: att.storageKey,
name: att.name,
size: att.size,
type: att.type,
})),
create: mapMessageAttachmentsForCreate(attachments),
},
}),
},
include: {
senderUser: true,
senderPartner: true,
attachments: true,
attachments: {
orderBy: messageAttachmentsOrderBy,
},
},
});

Expand Down
34 changes: 34 additions & 0 deletions apps/web/lib/messages/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
import { createId } from "@/lib/api/create-id";
import {
ATTACHMENT_MIME_TYPE_LABELS,
PREVIEWABLE_IMAGE_TYPES,
} from "./constants";

export const messageAttachmentsOrderBy = { createdAt: "asc" } as const;

type MessageAttachmentInput = {
storageKey: string;
name: string;
size: number;
type: string;
};

export function mapMessageAttachmentsForCreate(
attachments: MessageAttachmentInput[],
) {
const base = Date.now();

return attachments.map((att, index) => ({
id: createId({ prefix: "msa_" }),
storageKey: att.storageKey,
name: att.name,
size: att.size,
type: att.type,
createdAt: new Date(base + index),
}));
}

export function sortMessageAttachments<
T extends { createdAt: Date; id: string },
>(attachments: T[]) {
return [...attachments].sort(
(a, b) =>
a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id),
);
}

// Normalize a user-supplied file name
export function sanitizeFileName(name: string): string {
return name
Expand Down
Loading
Loading