Skip to content

Commit

Permalink
feat: store single message as PDF to GMail
Browse files Browse the repository at this point in the history
  • Loading branch information
ahochsteger committed Jan 24, 2023
1 parent 6029e2d commit c9abd9c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/Gmail2GDrive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
describe("Features", () => {
describe("Store email thread as PDF", () => {
describe("Store as PDF", () => {
test.todo("should be able to store the thread as PDF (PR #2)")
test.todo("should be able to store an individual message as PDF (PR #73)")
test.todo(
"should be able to store attachments and email PDF in the same folder (issue #36, PR #40)",
)
Expand Down
1 change: 1 addition & 0 deletions src/actions/MessageActions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ it("should provide actions in the action registry", () => {
"message.markUnread",
"message.moveToTrash",
"message.star",
"message.storeAsPdfToGDrive",
"message.unstar",
])
})
Expand Down
19 changes: 19 additions & 0 deletions src/actions/MessageActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AbstractActions } from "./AbstractActions"
import { action } from "./ActionRegistry"
import { MessageContext } from "../context/MessageContext"
import { GmailAdapter } from "../adapter/GmailAdapter"
import { ConflictStrategy } from "../adapter/GDriveAdapter"

export class MessageActions extends AbstractActions {
private gmailAdapter: GmailAdapter
Expand Down Expand Up @@ -48,4 +49,22 @@ export class MessageActions extends AbstractActions {
public unstar() {
this.gmailAdapter.messageUnstar(this.message)
}

/**
* Generate a PDF document from the message and store it to GDrive.
*/
@action("message.storeAsPdfToGDrive")
public storeAsPdfToGDrive(
location: string,
conflictStrategy: ConflictStrategy,
skipHeader = false,
) {
return this.processingContext.gdriveAdapter.createFile(
location,
this.gmailAdapter.messageAsPdf(this.messageContext.message, skipHeader),
"application/pdf",
"",
conflictStrategy,
)
}
}
5 changes: 3 additions & 2 deletions src/actions/ThreadActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,17 @@ export class ThreadActions extends AbstractActions {
}

/**
* Generate a PDF document for the whole thread using HTML from .
* Generate a PDF document for the whole thread and store it to GDrive.
*/
@action("thread.storeAsPdfToGDrive")
public storeAsPdfToGDrive(
location: string,
conflictStrategy: ConflictStrategy,
skipHeader = false,
) {
return this.processingContext.gdriveAdapter.createFile(
location,
this.gmailAdapter.threadAsPdf(this.threadContext.thread),
this.gmailAdapter.threadAsPdf(this.threadContext.thread, skipHeader),
"application/pdf",
"",
conflictStrategy,
Expand Down
22 changes: 13 additions & 9 deletions src/adapter/GmailAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ export class GmailAdapter extends BaseAdapter {
return htmlBlob.getAs("application/pdf").getDataAsString()
}

public messageAsHtml(message: GoogleAppsScript.Gmail.GmailMessage): string {
const html = `From: ${message.getFrom()}<br />
public messageAsHtml(message: GoogleAppsScript.Gmail.GmailMessage, skipHeader = false): string {
let html = ""
if (!skipHeader) {
html += `From: ${message.getFrom()}<br />
To: ${message.getTo()}<br />
Date: ${message.getDate()}<br />
Subject: ${message.getSubject()}<br />
<hr />
${message.getBody()}
`
}
html += `${message.getBody()}
<hr />
`
return html
}

public messageAsPdf(message: GoogleAppsScript.Gmail.GmailMessage) {
return this.convertHtmlToPdf(this.messageAsHtml(message))
public messageAsPdf(message: GoogleAppsScript.Gmail.GmailMessage, skipHeader = false) {
return this.convertHtmlToPdf(this.messageAsHtml(message, skipHeader))
}

@skipOnDryRun()
Expand Down Expand Up @@ -113,7 +117,7 @@ ${message.getBody()}
/**
* Generate HTML code for one message of a thread.
*/
public threadAsHtml(thread: GoogleAppsScript.Gmail.GmailThread) {
public threadAsHtml(thread: GoogleAppsScript.Gmail.GmailThread, skipHeader = false) {
this.logger.info(
" Generating HTML code of thread '" +
thread.getFirstMessageSubject() +
Expand All @@ -122,13 +126,13 @@ ${message.getBody()}
const messages = thread.getMessages()
let html = ""
for (const message of messages) {
html += this.messageAsHtml(message)
html += this.messageAsHtml(message, skipHeader)
}
return html
}

public threadAsPdf(thread: GoogleAppsScript.Gmail.GmailThread) {
return this.convertHtmlToPdf(this.threadAsHtml(thread))
public threadAsPdf(thread: GoogleAppsScript.Gmail.GmailThread, skipHeader = false) {
return this.convertHtmlToPdf(this.threadAsHtml(thread, skipHeader))
}

@skipOnDryRun()
Expand Down
11 changes: 11 additions & 0 deletions src/config/v1/V1Rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ export class V1Rule {
parentFolderId = ""
/** Add the given label to the processed thread */
ruleLabel = ""
/** Save the message to PDF */
saveMessagePDF = false
/** Save the thread to PDF */
saveThreadPDF = false
/** Skip header for PDF */
skipPDFHeader = false
}

/*
Expand Down Expand Up @@ -57,6 +61,13 @@ export class V1Rule {
"saveThreadPDF": true,
"folder": "PDF Emails"
},
{
// Store each INDIVIDUAL email as "PDF" instead of an entire thread, in the folder "PDF Emails"
"filter": "from:no_reply@email-invoice.example.com",
"saveMessagePDF": true,
"skipPDFHeader": true, // Skip Email Header
"folder": "PDF Emails"
},
{
// Store threads marked with label "PDF" in the folder "PDF Emails" als PDF document.
// while renaming the PDFs to the pattern defined in 'filenameTo'.
Expand Down

0 comments on commit c9abd9c

Please sign in to comment.