-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.go
161 lines (146 loc) · 5.15 KB
/
hooks.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package wom
import (
"errors"
"fmt"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/tools/filesystem"
"github.com/pocketbase/pocketbase/tools/mailer"
"github.com/pocketbase/pocketbase/tools/security"
"strings"
"time"
)
func ConfigurePocketBase(app *pocketbase.PocketBase, db *daos.Dao, mailClient mailer.Mailer, contactEmail, siteURL, senderName, senderAddress, hcaptchaSecretKey, hcaptchaSiteKey, mailingListSecret, webhookURL string, dev bool) {
app.OnBeforeServe().Add(createWomRoutesHook(app, app, db, mailClient, webhookURL, contactEmail, siteURL, senderName, senderAddress, hcaptchaSecretKey, hcaptchaSiteKey, mailingListSecret, dev))
app.OnRecordBeforeUpdateRequest("adventures").Add(preserveFilenameUpdateHook)
app.OnRecordBeforeCreateRequest("adventures").Add(preserveFilenameCreateHook)
app.OnRecordBeforeCreateRequest("guesses").Add(createBeforeGuessCreatedHook(db))
app.OnRecordAfterCreateRequest("guesses").Add(createGuessCreatedHook(db, webhookURL))
app.OnRecordBeforeAuthWithPasswordRequest("users").Add(emailValidationLoginCheck)
app.OnRecordBeforeAuthWithOAuth2Request("users").Add(createOauthSignupHook(db, webhookURL))
app.OnRecordBeforeAuthWithPasswordRequest().Add(rateLimitingPasswordHook)
app.OnRecordBeforeAuthWithOAuth2Request().Add(rateLimitingOAuthHook)
}
func rateLimitingPasswordHook(e *core.RecordAuthWithPasswordEvent) error {
return rateLimit(e.HttpContext.Request())
}
func rateLimitingOAuthHook(e *core.RecordAuthWithOAuth2Event) error {
return rateLimit(e.HttpContext.Request())
}
func createOauthSignupHook(db *daos.Dao, webhookURL string) func(e *core.RecordAuthWithOAuth2Event) error {
return func(e *core.RecordAuthWithOAuth2Event) error {
if !validEmail(e.OAuth2User.Email) {
return errors.New("invalid email")
}
if _, err := db.FindAuthRecordByEmail("users", e.OAuth2User.Email); err == nil {
return nil
}
users, err := db.FindCollectionByNameOrId("users")
if err != nil {
return err
}
record := models.NewRecord(users)
err = record.SetEmail(e.OAuth2User.Email)
if err != nil {
return err
}
err = record.SetPassword(security.RandomString(30))
if err != nil {
return err
}
err = record.SetUsername(e.OAuth2User.Email)
if err != nil {
return err
}
if err = db.SaveRecord(record); err != nil {
return err
}
e.Record = record
sendWebhook(webhookURL, fmt.Sprintf("New user signup: `%s`", record.Id))
return nil
}
}
func emailValidationLoginCheck(e *core.RecordAuthWithPasswordEvent) error {
if !e.Record.ValidatePassword(e.Password) {
return nil
}
if e.Record.Verified() {
return nil
}
return apis.NewBadRequestError("Email address pending validation.", nil)
}
func preserveOriginalFilenames(uploadedFiles map[string][]*filesystem.File, record *models.Record) error {
oldNames := map[string]map[string]string{}
for field, files := range uploadedFiles {
if len(files) == 0 {
continue
}
oldNames[field] = make(map[string]string, len(files))
for _, f := range files {
oldNames[field][f.Name] = f.OriginalName
f.Name = f.OriginalName
}
}
for field, filenames := range oldNames {
files := record.GetStringSlice(field)
for i, old := range files {
if newName, ok := filenames[old]; ok {
files[i] = newName
}
}
record.Set(field, files)
}
return nil
}
func createBeforeGuessCreatedHook(db *daos.Dao) func(e *core.RecordCreateEvent) error {
return func(e *core.RecordCreateEvent) error {
guess := strings.ToLower(e.Record.GetString("content"))
puzzle := e.Record.GetString("puzzle")
e.Record.Set("correct", checkGuess(db, puzzle, guess))
return nil
}
}
func createGuessCreatedHook(db *daos.Dao, webhookURL string) func(e *core.RecordCreateEvent) error {
return func(e *core.RecordCreateEvent) error {
game, err := db.FindRecordById("games", e.Record.Get("game").(string))
if err != nil {
return err
}
puzzle, err := db.FindRecordById("puzzles", e.Record.Get("puzzle").(string))
if err != nil {
return err
}
guessCorrect := e.Record.GetBool("correct")
nextPuzzle := puzzle.Get("next")
go func(gameCode, puzzleTitle, guessText string) {
if guessCorrect {
sendWebhook(webhookURL, fmt.Sprintf(":tada: %s/%s: %s", gameCode, puzzleTitle, guessText))
if nextPuzzle == "" {
sendWebhook(webhookURL, fmt.Sprintf(":checkered_flag: %s finished", gameCode))
}
} else {
sendWebhook(webhookURL, fmt.Sprintf(":x: %s/%s: %s", gameCode, puzzleTitle, guessText))
}
}(game.GetString("username"), puzzle.GetString("title"), e.Record.GetString("content"))
if guessCorrect {
if nextPuzzle == "" {
game.Set("puzzle", nil)
game.Set("status", "EXPIRED")
game.Set("end", time.Now())
} else {
game.Set("puzzle", nextPuzzle)
}
return db.SaveRecord(game)
}
return nil
}
}
func preserveFilenameCreateHook(e *core.RecordCreateEvent) error {
return preserveOriginalFilenames(e.UploadedFiles, e.Record)
}
func preserveFilenameUpdateHook(e *core.RecordUpdateEvent) error {
return preserveOriginalFilenames(e.UploadedFiles, e.Record)
}