-
Notifications
You must be signed in to change notification settings - Fork 95
/
report_form.go
82 lines (69 loc) · 1.97 KB
/
report_form.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package presentator
import (
"fmt"
"net/mail"
"strings"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/tools/mailer"
)
// newReportForm creates new ReportForm request form.
func newReportForm(app core.App, link *models.Record) *ReportForm {
return &ReportForm{
app: app,
link: link,
}
}
// ReportForm handles a design report submission.
type ReportForm struct {
app core.App
link *models.Record
Message string `form:"message" json:"message"`
}
// Validate makes the form validatable by implementing [validation.Validatable] interface.
func (form *ReportForm) Validate() error {
return validation.ValidateStruct(form,
validation.Field(&form.Message, validation.Length(0, 255)),
)
}
// Submit validates the form and send a report email.
func (form *ReportForm) Submit() error {
if err := form.Validate(); err != nil {
return err
}
project, err := form.app.Dao().FindRecordById("projects", form.link.GetString("project"))
if err != nil {
return fmt.Errorf("missing link project: %w", err)
}
previewUrl := strings.TrimRight(form.app.Settings().Meta.AppUrl, "/") + "/#/" + form.link.Username()
data := struct {
*baseMailData
Project *models.Record
PreviewUrl string
Message string
}{
baseMailData: newBaseMailData(form.app),
Project: project,
PreviewUrl: previewUrl,
Message: form.Message,
}
html, err := templates.LoadFS(
templatesFS,
"layout.html",
"report.html",
).Render(data)
if err != nil {
return fmt.Errorf("failed to render report template: %w", err)
}
message := &mailer.Message{
From: mail.Address{
Address: form.app.Settings().Meta.SenderAddress,
Name: form.app.Settings().Meta.SenderName,
},
To: []mail.Address{{Address: form.app.Settings().Meta.SenderAddress}},
Subject: "Design report violation",
HTML: html,
}
return form.app.NewMailClient().Send(message)
}