From 8930bb11fce19c773201b4f499d99c1c3c28efa6 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Wed, 30 Apr 2025 00:20:23 +0800 Subject: HALF WORK!: 2025-5-14 2 --- services/docker/mail-server/aws-sendmail/db.ts | 30 +++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'services/docker/mail-server/aws-sendmail/db.ts') diff --git a/services/docker/mail-server/aws-sendmail/db.ts b/services/docker/mail-server/aws-sendmail/db.ts index e239e72..e5307be 100644 --- a/services/docker/mail-server/aws-sendmail/db.ts +++ b/services/docker/mail-server/aws-sendmail/db.ts @@ -3,6 +3,11 @@ import { SQLocalKysely } from "sqlocal/kysely"; import { Generated, Insertable, Kysely, Migration, Migrator } from "kysely"; +import { Mail } from "./mail.ts"; + +export class DbError extends Error { +} + const tableNames = { mail: { table: "mail", @@ -10,6 +15,7 @@ const tableNames = { id: "id", messageId: "message_id", awsMessageId: "aws_message_id", + date: "date", raw: "raw", }, }, @@ -19,6 +25,7 @@ interface MailTable { [tableNames.mail.columns.id]: Generated; [tableNames.mail.columns.messageId]: string; [tableNames.mail.columns.awsMessageId]: string | null; + [tableNames.mail.columns.date]: number | null; [tableNames.mail.columns.raw]: string; } @@ -45,6 +52,7 @@ const migrations: Record = { (col) => col.notNull().unique(), ) .addColumn(names.columns.awsMessageId, "text", (col) => col.unique()) + .addColumn(names.columns.date, "integer") .addColumn(names.columns.raw, "text", (col) => col.notNull()) .execute(); @@ -96,7 +104,27 @@ export class DbService { await this._migrator.migrateToLatest(); } - async addMail(mail: Insertable): Promise { + async addMail(mail: Insertable | Mail, options?: { + allowNullAwsMessageId?: boolean; + }): Promise { + if (mail instanceof Mail) { + if (mail.messageId == null) { + throw new DbError("Mail object has no message id."); + } + mail = { + message_id: mail.messageId, + aws_message_id: mail.awsMessageId, + date: mail.simpleGetDate()?.getTime(), + raw: mail.raw, + }; + } + if ( + mail.aws_message_id == null && + !(options?.allowNullAwsMessageId === true) + ) { + throw new DbError("Aws message id is missing but it is required."); + } + await this._db.insertInto(tableNames.mail.table).values(mail) .executeTakeFirstOrThrow(); } -- cgit v1.2.3