Skip to content

Commit

Permalink
Merge pull request #686 from bpatrik/top-pick-sending
Browse files Browse the repository at this point in the history
Improving e-mail sending #683
  • Loading branch information
bpatrik authored Aug 1, 2023
2 parents 065c57f + e9f9a3a commit 5bfd384
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/backend/model/jobs/jobs/Job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export abstract class Job<T extends Record<string, any> = Record<string, any>> i
this.run();
} catch (e) {
Logger.error(LOG_TAG, e);
this.Progress.log('Failed with: ' + (typeof e.toString === 'function') ? e.toString() : JSON.stringify(e));
this.Progress.State = JobProgressStates.failed;
}
});
Expand Down
13 changes: 3 additions & 10 deletions src/backend/model/jobs/jobs/TopPickSendJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,11 @@ export class TopPickSendJob extends Job<{
defaultValue: 5,
}, {
id: 'emailTo',
type: 'email',
type: 'string-array',
name: backendTexts.emailTo.name,
description: backendTexts.emailTo.description,
defaultValue: '',
}, {
id: 'emailFrom',
type: 'email',
name: backendTexts.emailFrom.name,
description: backendTexts.emailFrom.description,
defaultValue: '[email protected]',
}, {
defaultValue: [],
}, {
id: 'emailSubject',
type: 'string',
name: backendTexts.emailSubject.name,
Expand Down Expand Up @@ -105,7 +99,6 @@ export class TopPickSendJob extends Job<{
this.Progress.log('Sending emails');
const messenger = new EmailMediaMessenger();
await messenger.sendMedia({
from: this.config.emailFrom,
to: this.config.emailTo,
subject: this.config.emailSubject,
text: this.config.emailText
Expand Down
13 changes: 6 additions & 7 deletions src/backend/model/mediamessengers/EmailMediaMessenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export class EmailMediaMessenger {
}

public async sendMedia(mailSettings: {
from: string,
to: string,
subject: string,
text: string
Expand All @@ -60,11 +59,11 @@ export class EmailMediaMessenger {
for (let i = 0; i < media.length; ++i) {
const thPath = await this.getThumbnail(media[i]);
const linkUrl = Utils.concatUrls(Config.Server.publicUrl, '/gallery/', path.join(media[i].directory.path, media[i].directory.name));
const location = (media[0].metadata as PhotoMetadata).positionData?.country ?
(media[0].metadata as PhotoMetadata).positionData?.country :
((media[0].metadata as PhotoMetadata).positionData?.city ?
(media[0].metadata as PhotoMetadata).positionData?.city : '');
const caption = (new Date(media[0].metadata.creationDate)).getFullYear() + (location ? ', ' + location : '');
const location = (media[i].metadata as PhotoMetadata).positionData?.country ?
(media[i].metadata as PhotoMetadata).positionData?.country :
((media[i].metadata as PhotoMetadata).positionData?.city ?
(media[i].metadata as PhotoMetadata).positionData?.city : '');
const caption = (new Date(media[i].metadata.creationDate)).getFullYear() + (location ? ', ' + location : '');
attachments.push({
filename: media[i].name,
path: thPath,
Expand All @@ -84,7 +83,7 @@ export class EmailMediaMessenger {
}

return await this.transporter.sendMail({
from: mailSettings.from,
from: Config.Messaging.Email.emailFrom,
to: mailSettings.to,
subject: mailSettings.subject,
html: htmlStart + htmlMiddle + htmlEnd,
Expand Down
1 change: 0 additions & 1 deletion src/common/BackendTexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export const backendTexts = {
sortBy: {name: 50, description: 52},
pickAmount: {name: 60, description: 62},
emailTo: {name: 70, description: 72},
emailFrom: {name: 80, description: 82},
emailSubject: {name: 90, description: 92},
emailText: {name: 100, description: 102}

Expand Down
12 changes: 12 additions & 0 deletions src/common/config/private/MessagingConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import {SubConfigClass} from '../../../../node_modules/typeconfig/src/decorators
import {ConfigPriority, TAGS} from '../public/ClientConfig';
import {ConfigProperty} from '../../../../node_modules/typeconfig/src/decorators/property/ConfigPropoerty';
import {ServerConfig} from './PrivateConfig';

declare let $localize: (s: TemplateStringsArray) => string;

if (typeof $localize === 'undefined') {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
global.$localize = (s) => s;
}

export enum EmailMessagingType {
sendmail = 1,
SMTP = 2,
Expand Down Expand Up @@ -91,6 +93,16 @@ export class EmailMessagingConfig {
})
type: EmailMessagingType = EmailMessagingType.sendmail;

@ConfigProperty<EmailMessagingType, EmailMessagingConfig>({
tags:
{
name: $localize`Sender email`,
priority: ConfigPriority.advanced,
} as TAGS,
description: $localize`Some services do not allow sending from random e-mail addresses. Set this accordingly.`
})
emailFrom: string = '[email protected]';

@ConfigProperty({
tags:
{
Expand Down
2 changes: 1 addition & 1 deletion src/common/entities/job/JobDTO.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {backendText} from '../../BackendTexts';

export type fieldType = 'string' | 'email' | 'number' | 'boolean' | 'number-array' | 'SearchQuery' | 'sort-array';
export type fieldType = 'string' | 'string-array' | 'number' | 'boolean' | 'number-array' | 'SearchQuery' | 'sort-array';

export enum DefaultsJobs {
Indexing = 1,
Expand Down
4 changes: 0 additions & 4 deletions src/frontend/app/model/backendtext.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ export class BackendtextService {
return $localize`E-mail to`;
case backendTexts.emailTo.description:
return $localize`E-mail address of the recipient.`;
case backendTexts.emailFrom.name:
return $localize`E-mail From`;
case backendTexts.emailFrom.description:
return $localize`E-mail sender address.`;
case backendTexts.emailSubject.name:
return $localize`Subject`;
case backendTexts.emailSubject.description:
Expand Down
12 changes: 6 additions & 6 deletions src/frontend/app/ui/settings/workflow/workflow.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,12 @@
[(ngModel)]="schedule.config[configEntry.id]" required>
</ng-container>

<ng-container *ngSwitchCase="'email'">
<input type="email" class="form-control" [name]="configEntry.id+'_'+i"
<ng-container *ngSwitchCase="'string-array'">
<input type="text" class="form-control"
[name]="configEntry.id+'_'+i"
[id]="configEntry.id+'_'+i"
placeholder="[email protected]"
(ngModelChange)="onChange($event)"
[(ngModel)]="schedule.config[configEntry.id]" required>
(ngModelChange)="setEmailArray(schedule.config,configEntry.id,$event); onChange($event);"
[ngModel]="getArray($any(schedule.config),configEntry.id)" required>
</ng-container>

<ng-container *ngSwitchCase="'number'">
Expand All @@ -196,7 +196,7 @@
[name]="configEntry.id+'_'+i"
[id]="configEntry.id+'_'+i"
(ngModelChange)="setNumberArray(schedule.config,configEntry.id,$event); onChange($event);"
[ngModel]="getNumberArray($any(schedule.config),configEntry.id)" required>
[ngModel]="getArray($any(schedule.config),configEntry.id)" required>
</ng-container>

<app-gallery-search-field
Expand Down
17 changes: 12 additions & 5 deletions src/frontend/app/ui/settings/workflow/workflow.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ export class WorkflowComponent implements ControlValueAccessor, Validator, OnIni
}
}

setEmailArray(configElement: any, id: string, value: string): void {
value = value.replace(new RegExp(',', 'g'), ';');
value = value.replace(new RegExp(' ', 'g'), ';');
configElement[id] = value
.split(';').filter((i: string) => i != '');
}

getArray(configElement: Record<string, number[]>, id: string): string {
return configElement[id] && Array.isArray(configElement[id]) ? configElement[id].join('; ') : '';
}

setNumberArray(configElement: any, id: string, value: string): void {
value = value.replace(new RegExp(',', 'g'), ';');
value = value.replace(new RegExp(' ', 'g'), ';');
Expand All @@ -168,9 +179,6 @@ export class WorkflowComponent implements ControlValueAccessor, Validator, OnIni
.filter((i: number) => !isNaN(i) && i > 0);
}

getNumberArray(configElement: Record<string, number[]>, id: string): string {
return configElement[id] ? configElement[id].join('; ') : '';
}

public shouldIdent(curr: JobScheduleDTO, prev: JobScheduleDTO): boolean {
return (
Expand Down Expand Up @@ -284,7 +292,6 @@ export class WorkflowComponent implements ControlValueAccessor, Validator, OnIni
}



AsSortArray(configElement: string | number | string[] | number[]): SortingMethods[] {
return configElement as SortingMethods[];
}
Expand All @@ -294,6 +301,6 @@ export class WorkflowComponent implements ControlValueAccessor, Validator, OnIni
}

AddNewSorting(configElement: string | number | string[] | number[]): void {
(configElement as SortingMethods[]).push(SortingMethods.ascDate)
(configElement as SortingMethods[]).push(SortingMethods.ascDate);
}
}

0 comments on commit 5bfd384

Please sign in to comment.