-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail-watcher.service.ts
61 lines (51 loc) · 1.76 KB
/
mail-watcher.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { discordConfig } from '../../infra/config/discord';
import { DiscordService } from '../../infra/services/discord.service';
import {
FetchMailService,
MailType
} from '../../infra/services/fetch-mail.service';
import { CheckFilesService } from '../../infra/util/check-files.service';
export class MailWatcherService {
constructor(
private readonly fetchMailService: FetchMailService,
private readonly discordService: DiscordService,
private readonly checkFilesService: CheckFilesService
) {}
listenEvents() {
this.fetchMailService.on(
'finish-read-messages',
this.handleOnFinishReadMailsFounded.bind(this)
);
this.fetchMailService.on(
'nothing-email-founded',
this.handleOnNothingMailsFounded.bind(this)
);
return this;
}
async watch(interval: number) {
setInterval(this.handleWatchMails.bind(this), interval);
}
private async handleOnFinishReadMailsFounded(mails: MailType[]) {
const mailsDomainUFC = mails.filter(
(v) =>
(v.from.address.includes('@ufc.br') ||
v.from.address.includes('@quixada.ufc.br')) &&
!this.checkFilesService.isHtml(v.text)
);
mailsDomainUFC.forEach(this.handleDiscordNotification.bind(this));
this.fetchMailService.disconnect();
}
private async handleDiscordNotification(mail: MailType) {
const notification = `📧 from: ${mail.from.address}, ${mail.from.name}\n📢 subject: ${mail.subject}\n📝 message: ${mail.text}`;
await this.discordService.sendMessage(
discordConfig.channel_focused,
notification
);
}
private async handleOnNothingMailsFounded() {
this.fetchMailService.disconnect();
}
private async handleWatchMails() {
(await this.fetchMailService.connect()).openBox('INBOX');
}
}