Skip to content

Commit

Permalink
✔ 😥
Browse files Browse the repository at this point in the history
  • Loading branch information
bifeldy committed Jul 15, 2023
1 parent 5a507f2 commit d841530
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 38 deletions.
2 changes: 1 addition & 1 deletion dist/fansubid/browser/ngsw.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"configVersion": 1,
"timestamp": 1689444992789,
"timestamp": 1689447587108,
"index": "/index.html",
"assetGroups": [
{
Expand Down
2 changes: 1 addition & 1 deletion dist/fansubid/server/main.js

Large diffs are not rendered by default.

76 changes: 44 additions & 32 deletions src/api/controllers/mail-/mail-webhook.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ export class MailWebhookController {
//
}

async updateLater(mailId: string): Promise<void> {
if (this.ms.webhook[mailId].timeout) {
this.sr.deleteTimeout(mailId);
}
this.ms.webhook[mailId].timeout = true;
this.sr.addTimeout(
mailId,
setTimeout(async () => {
try {
const mailbox = await this.mailboxRepo.findOneOrFail({
where: [
{ mail: Equal(mailId) }
]
});
for (const [key, value] of Object.entries(this.ms.webhook[mailId].col)) {
mailbox[key] = value;
}
await this.mailboxRepo.save(mailbox);
delete this.ms.webhook[mailId];
} catch (err) {
this.gs.log('[MAIL_WEBHOOK_TIMEOUT-ERROR] 📧', err, 'error');
}
}, CONSTANTS.timeoutMailWebhookTime)
);
}

@Post('/')
@HttpCode(201)
@FilterApiKeyAccess()
Expand Down Expand Up @@ -130,7 +156,13 @@ export class MailWebhookController {
mailbox.date = new Date(req.body.Date);
mailboxSave = await this.mailboxRepo.insert(mailbox);
if (req.files?.length > 0) {
let attachments = [];
if (!this.ms.webhook[req.body['Message-Id']]) {
this.ms.webhook[req.body['Message-Id']] = {};
}
if (!this.ms.webhook[req.body['Message-Id']].col) {
this.ms.webhook[req.body['Message-Id']].col = {};
}
this.ms.webhook[req.body['Message-Id']].col.attachments = [];
for (const file of req.files as any) {
const fileExt = file.originalname.split('.').pop().toLowerCase();
const files = readdirSync(`${environment.uploadFolder}`, { withFileTypes: true });
Expand All @@ -143,7 +175,7 @@ export class MailWebhookController {
attachment.size = file.size;
attachment.mime = file.mimetype;
const resAttachmentSave = await this.attachmentRepo.save(attachment);
attachments.push(resAttachmentSave);
this.ms.webhook[req.body['Message-Id']].col.attachments.push(resAttachmentSave);
// Upload Attachment -- Jpg, Png, etc
if (environment.production) {
this.gdrive.gDrive(true).then(async (gdrive) => {
Expand Down Expand Up @@ -171,46 +203,26 @@ export class MailWebhookController {
}
}
}
await this.mailboxRepo.update({
mail: Equal(req.body['Message-Id'])
}, {
attachment_: attachments
});
await this.updateLater(req.body['Message-Id']);
}
} else {
mailboxSave = mailboxs[0];
if (addressBcc.length > 0) {
if (!this.ms.webhook[mailboxSave.mail]) {
this.ms.webhook[mailboxSave.mail] = {};
}
if (!this.ms.webhook[mailboxSave.mail].bcc) {
this.ms.webhook[mailboxSave.mail].bcc = '';
if (!this.ms.webhook[mailboxSave.mail].col) {
this.ms.webhook[mailboxSave.mail].col = {};
}
this.ms.webhook[mailboxSave.mail].col.bcc = '';
if (mailboxSave.bcc) {
this.ms.webhook[mailboxSave.mail].bcc += mailboxSave.bcc + ', ';
}
this.ms.webhook[mailboxSave.mail].bcc += addressBcc.join(', ');
const bccUniq = [...new Set(this.ms.webhook[mailboxSave.mail].bcc)];
this.ms.webhook[mailboxSave.mail].bcc = bccUniq;
if (this.ms.webhook[mailboxSave.mail].timeout) {
this.sr.deleteTimeout(mailboxSave.mail);
this.ms.webhook[mailboxSave.mail].col.bcc += mailboxSave.bcc + ', ';
}
this.ms.webhook[mailboxSave.mail].timeout = true;
this.sr.addTimeout(
mailboxSave.mail,
setTimeout(async () => {
try {
await this.mailboxRepo.update({
mail: Equal(req.body['Message-Id'])
}, {
bcc: this.ms.webhook[mailboxSave.mail].bcc
});
delete this.ms.webhook[mailboxSave.mail];
} catch (err) {
this.gs.log('[MAIL_WEBHOOK_TIMEOUT-ERROR] 📧', err, 'error');
}
}, CONSTANTS.timeoutMailWebhookTime)
);
this.ms.webhook[mailboxSave.mail].col.bcc += addressBcc.join(', ');
const bcc = this.ms.webhook[mailboxSave.mail].col.bcc.split(',').map(v => v.trim());
const bccUniq = [...new Set(bcc)];
this.ms.webhook[mailboxSave.mail].col.bcc = bccUniq.join(', ');
await this.updateLater(mailboxSave.mail);
}
if (req.files?.length > 0) {
for (const file of req.files as any) {
Expand Down
11 changes: 7 additions & 4 deletions src/api/services/mail.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ export class MailService {
});

webhook = {
// mail_id: {
// bcc: '',
// timeout: true
// }
mail_id: {
col: {
bcc: '',
attachments: [],
},
timeout: true
}
};

constructor(
Expand Down

0 comments on commit d841530

Please sign in to comment.