]> git.ipfire.org Git - thirdparty/paperless-ngx.git/blob
f2d8236bc22995323e25ce4653c9efb45e8b5cc2
[thirdparty/paperless-ngx.git] /
1 import { Component } from '@angular/core'
2 import {
3 FormControl,
4 FormGroup,
5 FormsModule,
6 ReactiveFormsModule,
7 } from '@angular/forms'
8 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
9 import { first } from 'rxjs'
10 import { EditDialogComponent } from 'src/app/components/common/edit-dialog/edit-dialog.component'
11 import { Correspondent } from 'src/app/data/correspondent'
12 import { DocumentType } from 'src/app/data/document-type'
13 import { MailAccount } from 'src/app/data/mail-account'
14 import {
15 MailAction,
16 MailFilterAttachmentType,
17 MailMetadataCorrespondentOption,
18 MailMetadataTitleOption,
19 MailRule,
20 MailRuleConsumptionScope,
21 } from 'src/app/data/mail-rule'
22 import { CorrespondentService } from 'src/app/services/rest/correspondent.service'
23 import { DocumentTypeService } from 'src/app/services/rest/document-type.service'
24 import { MailAccountService } from 'src/app/services/rest/mail-account.service'
25 import { MailRuleService } from 'src/app/services/rest/mail-rule.service'
26 import { UserService } from 'src/app/services/rest/user.service'
27 import { SettingsService } from 'src/app/services/settings.service'
28 import { CheckComponent } from '../../input/check/check.component'
29 import { NumberComponent } from '../../input/number/number.component'
30 import { SelectComponent } from '../../input/select/select.component'
31 import { SwitchComponent } from '../../input/switch/switch.component'
32 import { TagsComponent } from '../../input/tags/tags.component'
33 import { TextComponent } from '../../input/text/text.component'
34
35 const ATTACHMENT_TYPE_OPTIONS = [
36 {
37 id: MailFilterAttachmentType.Attachments,
38 name: $localize`Only process attachments`,
39 },
40 {
41 id: MailFilterAttachmentType.Everything,
42 name: $localize`Process all files, including 'inline' attachments`,
43 },
44 ]
45
46 const CONSUMPTION_SCOPE_OPTIONS = [
47 {
48 id: MailRuleConsumptionScope.Attachments,
49 name: $localize`Only process attachments`,
50 },
51 {
52 id: MailRuleConsumptionScope.EmailOnly,
53 name: $localize`Process message as .eml`,
54 },
55 {
56 id: MailRuleConsumptionScope.Everything,
57 name: $localize`Process message as .eml and attachments separately`,
58 },
59 ]
60
61 const ACTION_OPTIONS = [
62 {
63 id: MailAction.Delete,
64 name: $localize`Delete`,
65 },
66 {
67 id: MailAction.Move,
68 name: $localize`Move to specified folder`,
69 },
70 {
71 id: MailAction.MarkRead,
72 name: $localize`Mark as read, don't process read mails`,
73 },
74 {
75 id: MailAction.Flag,
76 name: $localize`Flag the mail, don't process flagged mails`,
77 },
78 {
79 id: MailAction.Tag,
80 name: $localize`Tag the mail with specified tag, don't process tagged mails`,
81 },
82 ]
83
84 const METADATA_TITLE_OPTIONS = [
85 {
86 id: MailMetadataTitleOption.FromSubject,
87 name: $localize`Use subject as title`,
88 },
89 {
90 id: MailMetadataTitleOption.FromFilename,
91 name: $localize`Use attachment filename as title`,
92 },
93 {
94 id: MailMetadataTitleOption.None,
95 name: $localize`Do not assign title from this rule`,
96 },
97 ]
98
99 const METADATA_CORRESPONDENT_OPTIONS = [
100 {
101 id: MailMetadataCorrespondentOption.FromNothing,
102 name: $localize`Do not assign a correspondent`,
103 },
104 {
105 id: MailMetadataCorrespondentOption.FromEmail,
106 name: $localize`Use mail address`,
107 },
108 {
109 id: MailMetadataCorrespondentOption.FromName,
110 name: $localize`Use name (or mail address if not available)`,
111 },
112 {
113 id: MailMetadataCorrespondentOption.FromCustom,
114 name: $localize`Use correspondent selected below`,
115 },
116 ]
117
118 @Component({
119 selector: 'pngx-mail-rule-edit-dialog',
120 templateUrl: './mail-rule-edit-dialog.component.html',
121 styleUrls: ['./mail-rule-edit-dialog.component.scss'],
122 imports: [
123 SelectComponent,
124 TagsComponent,
125 CheckComponent,
126 TextComponent,
127 NumberComponent,
128 SwitchComponent,
129 FormsModule,
130 ReactiveFormsModule,
131 ],
132 })
133 export class MailRuleEditDialogComponent extends EditDialogComponent<MailRule> {
134 accounts: MailAccount[]
135 correspondents: Correspondent[]
136 documentTypes: DocumentType[]
137
138 constructor(
139 service: MailRuleService,
140 activeModal: NgbActiveModal,
141 accountService: MailAccountService,
142 correspondentService: CorrespondentService,
143 documentTypeService: DocumentTypeService,
144 userService: UserService,
145 settingsService: SettingsService
146 ) {
147 super(service, activeModal, userService, settingsService)
148
149 accountService
150 .listAll()
151 .pipe(first())
152 .subscribe((result) => (this.accounts = result.results))
153
154 correspondentService
155 .listAll()
156 .pipe(first())
157 .subscribe((result) => (this.correspondents = result.results))
158
159 documentTypeService
160 .listAll()
161 .pipe(first())
162 .subscribe((result) => (this.documentTypes = result.results))
163 }
164
165 getCreateTitle() {
166 return $localize`Create new mail rule`
167 }
168
169 getEditTitle() {
170 return $localize`Edit mail rule`
171 }
172
173 getForm(): FormGroup {
174 return new FormGroup({
175 name: new FormControl(null),
176 account: new FormControl(null),
177 enabled: new FormControl(true),
178 folder: new FormControl('INBOX'),
179 filter_from: new FormControl(null),
180 filter_to: new FormControl(null),
181 filter_subject: new FormControl(null),
182 filter_body: new FormControl(null),
183 filter_attachment_filename_include: new FormControl(null),
184 filter_attachment_filename_exclude: new FormControl(null),
185 maximum_age: new FormControl(null),
186 attachment_type: new FormControl(MailFilterAttachmentType.Attachments),
187 consumption_scope: new FormControl(MailRuleConsumptionScope.Attachments),
188 order: new FormControl(null),
189 action: new FormControl(MailAction.MarkRead),
190 action_parameter: new FormControl(null),
191 assign_title_from: new FormControl(MailMetadataTitleOption.FromSubject),
192 assign_tags: new FormControl([]),
193 assign_document_type: new FormControl(null),
194 assign_correspondent_from: new FormControl(
195 MailMetadataCorrespondentOption.FromNothing
196 ),
197 assign_correspondent: new FormControl(null),
198 assign_owner_from_rule: new FormControl(true),
199 })
200 }
201
202 get showCorrespondentField(): boolean {
203 return (
204 this.objectForm?.get('assign_correspondent_from')?.value ==
205 MailMetadataCorrespondentOption.FromCustom
206 )
207 }
208
209 get showActionParamField(): boolean {
210 return (
211 this.objectForm?.get('action')?.value == MailAction.Move ||
212 this.objectForm?.get('action')?.value == MailAction.Tag
213 )
214 }
215
216 get attachmentTypeOptions() {
217 return ATTACHMENT_TYPE_OPTIONS
218 }
219
220 get actionOptions() {
221 return ACTION_OPTIONS
222 }
223
224 get metadataTitleOptions() {
225 return METADATA_TITLE_OPTIONS
226 }
227
228 get metadataCorrespondentOptions() {
229 return METADATA_CORRESPONDENT_OPTIONS
230 }
231
232 get consumptionScopeOptions() {
233 return CONSUMPTION_SCOPE_OPTIONS
234 }
235 }