| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| export const SECTION_NAME = 'Email_Plugin'; | ||
| export const EXPORT_TYPE = 'Export_Type'; | ||
| export const ATTACHMENTS = 'Attachments'; | ||
| export const PLUGIN_ICON = 'fa fa-envelope'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,35 @@ | ||
| import * as Imap from 'imap'; | ||
| import {ImapConfig} from '../model/imapConfig.model'; | ||
| import {Query} from '../model/Query.model'; | ||
|
|
||
| export class IMAP { | ||
| private imap = null; | ||
| private monitorId = null; | ||
| private query: Query = null; | ||
| private delayTime = 1000 * 5; | ||
|
|
||
| constructor(config: ImapConfig) { | ||
| this.imap = new Imap({ | ||
| ...config, | ||
| authTimeout: 10000, | ||
| connTimeout: 30000, | ||
| tlsOptions: { | ||
| rejectUnauthorized: false, | ||
| }, | ||
|
|
||
| }); | ||
| } | ||
|
|
||
| init() { | ||
| return new Promise((resolve, reject) => { | ||
| this.imap.connect(); | ||
|
|
||
| this.imap.once('ready', ()=> { | ||
| console.log('%c--------------------- SUCCESSFUL IMAP CONNECTION --------------------', 'color: Green'); | ||
| resolve(this.imap); | ||
| }); | ||
|
|
||
| this.imap.once('error', function(err: Error) { | ||
| reject(err); | ||
| }); | ||
| }); | ||
|
|
@@ -44,12 +41,10 @@ export class IMAP { | |
|
|
||
| openBox(mailBox, readOnly = true) { | ||
| return new Promise((resolve, reject) => { | ||
| this.imap.openBox(mailBox, readOnly, (err, box) => { | ||
| if (err) { | ||
| reject(err); | ||
| } else { | ||
| resolve(box); | ||
| } | ||
| }); | ||
|
|
@@ -61,66 +56,56 @@ export class IMAP { | |
| this.imap.search(criteria, (err, messages) => { | ||
| if (err) { | ||
| reject(err); | ||
| } else { | ||
| resolve(messages); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| state() { | ||
| return new Promise(async (resolve, reject) => { | ||
| if (!navigator.onLine) { | ||
| reject(new Error('No internet Connection')); | ||
| } else if (!this.imap) { | ||
| reject(new Error('Please Re-login')); | ||
| } else if (this.imap.state === 'authenticated') { | ||
| resolve('authenticated'); | ||
| } else { | ||
| try { | ||
| await this.init(); | ||
| resolve('Reconnected successfully'); | ||
| } catch (err) { | ||
| reject(err); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| monitor() { | ||
| let mailBox = null; | ||
| let criteria = null; | ||
|
|
||
| this.monitorId = setInterval(async () => { | ||
| // | ||
| if (this.query) { | ||
| ({mailBox, criteria} = this.query); | ||
|
|
||
| try { | ||
| // Check if the connection is still stable. | ||
| await this.state(); | ||
|
|
||
| await this.openBox(mailBox); | ||
|
There was a problem hiding this comment. Same here, this returns There was a problem hiding this comment. how does the change look like I cannot find anything in the latest commit? There was a problem hiding this comment.
Briefly, Dealing with operations in imap is asynchronous because we are waiting for a response from the mail service provider with approval or rejection, so this is not an instantaneous process. That is why you will notice the word 'await' beside the function. When this data is available from the email service provider, we use the 'resolve' and can also return the box or message from the email provider depending on what the function does. But we don't need this data now, so what I did was just If there is anything unclear, please tell me. There was a problem hiding this comment. thx I found the change in the code, eventually, in e8784fe, thx for pushing me in the right direction thx for the nice explanation :), can you show me where resolve() is defined? There was a problem hiding this comment. You’re welcome,
Inside promise functionResolve and reject are callback functions, and calling the resolve function if the job is finished successfully. If you are interested in the promise, this website is very nice. |
||
|
|
||
| const messages = await this.search(criteria); | ||
|
|
||
| console.log(messages); | ||
| } catch (err) { | ||
| // Revoke the query | ||
| this.query = null; | ||
| alert(err); | ||
| } | ||
| } | ||
| }, this.delayTime); | ||
| } | ||
|
|
||
|
|
@@ -129,5 +114,4 @@ export class IMAP { | |
| this.imap.end(); | ||
| console.log('%c--------------------- Close IMAP CONNECTION --------------------', 'color: Red'); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,4 @@ export interface Query { | |
| mailBox: string, | ||
| criteria: string[] | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,4 @@ export interface EmailProvider { | |
| host: string, | ||
| port: number, | ||
| tls?: boolean, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,4 +7,4 @@ export interface ImapConfig { | |
| port: number, | ||
| tls?: boolean, | ||
| tlsOptions?: object, | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You resolve this passing string but never use the return value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is where having type annotations could help, then it'd be immediately clear that you don't use the return type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to make the function more readable without comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I annotated the promises by void type.