webhooks: add "user created" webhook

Webhooks send a POST to an admin-supplied URL when something happens,
with relevant information sent in JSON. One has been added for creating
users in Settings > Webhooks > User Created.

Lazily, the portion of GetUsers which generates a respUser has been
factored out, and is called to send the JSON payload.

A stripped-down common.Req method has been added, which is used by the
barebones WebhookSender struct.
This commit is contained in:
Harvey Tindall
2024-08-20 21:33:43 +01:00
parent 8307d3da90
commit e5f79c60ae
12 changed files with 226 additions and 63 deletions
+16 -2
View File
@@ -1,6 +1,7 @@
package main
import (
"sync"
"time"
"github.com/gin-gonic/gin"
@@ -49,7 +50,8 @@ type NewUserData struct {
}
// Called after a new-user-creating route has done pre-steps (veryfing contact methods for example).
func (app *appContext) NewUserPostVerification(p NewUserParams) (out NewUserData) {
func (app *appContext) NewUserPostVerification(p NewUserParams) (out NewUserData, pendingTasks *sync.WaitGroup) {
pendingTasks = &sync.WaitGroup{}
// Some helper functions which will behave as our app.info/error/debug
deferLogInfo := func(s string, args ...any) {
out.Log = func() {
@@ -124,7 +126,19 @@ func (app *appContext) NewUserPostVerification(p NewUserParams) (out NewUserData
}
}
// Welcome email is sent by each user of this method separately..
webhookURIs := app.config.Section("webhooks").Key("created").StringsWithShadows("|")
if len(webhookURIs) != 0 {
summary := app.userSummary(out.User)
for _, uri := range webhookURIs {
go func() {
pendingTasks.Add(1)
app.webhooks.Send(uri, summary)
pendingTasks.Done()
}()
}
}
// Welcome email is sent by each user of this method separately.
out.Status = 200
out.Success = true