generated from n8n-io/n8n-nodes-starter
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d5b033
commit 97c1166
Showing
4 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { ImapFlow } from "imapflow"; | ||
import { IExecuteFunctions, INodeExecutionData, NodeApiError } from "n8n-workflow"; | ||
import { IResourceOperationDef } from "../../../utils/CommonDefinitions"; | ||
import { getMailboxPathFromNodeParameter, parameterSelectMailbox } from '../../../utils/SearchFieldParameters'; | ||
|
||
|
||
enum ImapFlags { | ||
Answered = '\\Answered', | ||
Flagged = '\\Flagged', | ||
Deleted = '\\Deleted', | ||
Seen = '\\Seen', | ||
Draft = '\\Draft', | ||
} | ||
|
||
export const setEmailFlagsOperation: IResourceOperationDef = { | ||
operation: { | ||
name: 'Set Flags', | ||
value: 'setEmailFlags', | ||
description: 'Set flags on an email like "Seen" or "Flagged"', | ||
}, | ||
parameters: [ | ||
{ | ||
...parameterSelectMailbox, | ||
description: 'Select the mailbox', | ||
}, | ||
{ | ||
displayName: 'Email UID', | ||
name: 'emailUid', | ||
type: 'string', | ||
default: '', | ||
description: 'UID of the email to set flags', | ||
hint: 'You can use comma separated list of UIDs', | ||
}, | ||
{ | ||
displayName: 'Flags', | ||
name: 'flags', | ||
type: 'collection', | ||
default: [], | ||
required: true, | ||
placeholder: 'Add Flag', | ||
options: [ | ||
{ | ||
displayName: 'Answered', | ||
name: ImapFlags.Answered, | ||
type: 'boolean', | ||
default: false, | ||
description: 'Whether email is answered', | ||
}, | ||
{ | ||
displayName: 'Flagged', | ||
name: ImapFlags.Flagged, | ||
type: 'boolean', | ||
default: false, | ||
description: 'Whether email is flagged', | ||
}, | ||
{ | ||
displayName: 'Deleted', | ||
name: ImapFlags.Deleted, | ||
type: 'boolean', | ||
default: false, | ||
description: 'Whether email is deleted', | ||
}, | ||
{ | ||
displayName: 'Seen', | ||
name: ImapFlags.Seen, | ||
type: 'boolean', | ||
default: false, | ||
description: 'Whether email is seen', | ||
}, | ||
{ | ||
displayName: 'Draft', | ||
name: ImapFlags.Draft, | ||
type: 'boolean', | ||
default: false, | ||
description: 'Whether email is draft', | ||
}, | ||
], | ||
}, | ||
], | ||
async executeImapAction(context: IExecuteFunctions, client: ImapFlow) { | ||
var returnData: INodeExecutionData[] = []; | ||
|
||
const mailboxPath = getMailboxPathFromNodeParameter(context); | ||
const emailUid = context.getNodeParameter('emailUid', 0) as string; | ||
const flags = context.getNodeParameter('flags', 0) as unknown as { [key: string]: boolean }; | ||
|
||
var flagsToSet : string[] = []; | ||
var flagsToRemove : string[] = []; | ||
for (const flagName in flags) { | ||
if (flags[flagName]) { | ||
flagsToSet.push(flagName); | ||
} else { | ||
flagsToRemove.push(flagName); | ||
} | ||
} | ||
|
||
context.logger?.info(`Setting flags "${flagsToSet.join(',')}" and removing flags "${flagsToRemove.join(',')}" on email "${emailUid}"`); | ||
|
||
await client.mailboxOpen(mailboxPath, { readOnly: false }); | ||
|
||
if (flagsToSet.length > 0) { | ||
const isSuccess : boolean = await client.messageFlagsAdd(emailUid, flagsToSet, { | ||
uid: true, | ||
}); | ||
if (!isSuccess) { | ||
throw new NodeApiError(context.getNode(), {}, { | ||
message: "Unable to set flags, unknown error", | ||
}); | ||
} | ||
} | ||
if (flagsToRemove.length > 0) { | ||
const isSuccess : boolean = await client.messageFlagsRemove(emailUid, flagsToRemove, { | ||
uid: true, | ||
}); | ||
if (!isSuccess) { | ||
throw new NodeApiError(context.getNode(), {}, { | ||
message: "Unable to remove flags, unknown error", | ||
}); | ||
} | ||
} | ||
|
||
client.close(); | ||
return [returnData]; | ||
}, | ||
}; |