imap-modify.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * @node imap-modify
  3. * @name IMAP Modify Email
  4. * @category email
  5. * @version 1.0.1
  6. * @description Modify emails on an IMAP server - mark as read/unread, flag, delete, or move.
  7. * @icon edit
  8. */
  9. const configSchema = {
  10. type: 'object',
  11. properties: {
  12. credentialId: {
  13. type: 'string',
  14. title: 'IMAP Credential',
  15. description: 'Select the IMAP credential to use for authentication',
  16. dynamicOptions: {
  17. source: 'credentials',
  18. filter: { type: 'imap' }
  19. }
  20. },
  21. mailbox: {
  22. type: 'string',
  23. title: 'Mailbox',
  24. description: 'The mailbox containing the email',
  25. default: 'INBOX'
  26. },
  27. uid: {
  28. type: 'string',
  29. title: 'Email UID',
  30. description: 'The unique identifier of the email to modify (can be provided from input)'
  31. },
  32. action: {
  33. type: 'string',
  34. title: 'Action',
  35. description: 'The action to perform on the email',
  36. enum: ['markRead', 'markUnread', 'flag', 'unflag', 'delete', 'move'],
  37. default: 'markRead'
  38. },
  39. targetMailbox: {
  40. type: 'string',
  41. title: 'Target Mailbox',
  42. description: 'The target mailbox for move action (e.g., Archive, Trash)'
  43. }
  44. },
  45. required: ['credentialId', 'action']
  46. };
  47. const inputSchema = {
  48. type: 'object',
  49. properties: {
  50. uid: {
  51. type: 'string',
  52. description: 'Email UID to modify'
  53. },
  54. uids: {
  55. type: 'array',
  56. items: { type: 'string' },
  57. description: 'Array of email UIDs to modify (batch operation)'
  58. },
  59. mailbox: {
  60. type: 'string',
  61. description: 'Override mailbox from input'
  62. },
  63. action: {
  64. type: 'string',
  65. description: 'Override action from input'
  66. }
  67. }
  68. };
  69. const outputSchema = {
  70. type: 'object',
  71. properties: {
  72. success: { type: 'boolean' },
  73. action: { type: 'string' },
  74. mailbox: { type: 'string' },
  75. processed: { type: 'integer' },
  76. succeeded: { type: 'integer' },
  77. failed: { type: 'integer' },
  78. results: { type: 'array' }
  79. }
  80. };
  81. const outputs = [
  82. { name: 'main', displayName: 'Result', type: 'object', color: '#f59e0b' }
  83. ];
  84. module.exports = {
  85. configSchema,
  86. inputSchema,
  87. outputSchema,
  88. outputs,
  89. async execute(config, input, context) {
  90. const credentialId = config.credentialId;
  91. const mailbox = input.mailbox || config.mailbox || 'INBOX';
  92. const action = input.action || config.action;
  93. const targetMailbox = config.targetMailbox;
  94. if (!credentialId) {
  95. throw new Error('IMAP credential is required');
  96. }
  97. if (!action) {
  98. throw new Error('Action is required');
  99. }
  100. // Support both single UID and array of UIDs
  101. let uids = [];
  102. if (input.uids && Array.isArray(input.uids)) {
  103. uids = input.uids;
  104. } else if (input.uid) {
  105. uids = [input.uid];
  106. } else if (config.uid) {
  107. uids = [config.uid];
  108. }
  109. if (uids.length === 0) {
  110. throw new Error('At least one email UID is required');
  111. }
  112. // Validate move action has target
  113. if (action === 'move' && !targetMailbox) {
  114. throw new Error('Target mailbox is required for move action');
  115. }
  116. const results = [];
  117. let successCount = 0;
  118. // Process each UID
  119. for (const uid of uids) {
  120. const modifyOptions = {
  121. credentialId,
  122. mailbox,
  123. uid,
  124. action
  125. };
  126. if (action === 'move') {
  127. modifyOptions.targetMailbox = targetMailbox;
  128. }
  129. const modifyResult = smartbotic.imap.modify(modifyOptions);
  130. results.push({
  131. uid,
  132. success: modifyResult.success,
  133. error: modifyResult.error
  134. });
  135. if (modifyResult.success) {
  136. successCount++;
  137. }
  138. }
  139. return {
  140. success: successCount === uids.length,
  141. action,
  142. mailbox,
  143. processed: uids.length,
  144. succeeded: successCount,
  145. failed: uids.length - successCount,
  146. results
  147. };
  148. }
  149. };