~sircmpwn/meta.sr.ht

ref: 0.56.17 meta.sr.ht/api/webhooks/webhooks.go -rw-r--r-- 1.6 KiB
b1deacf0Nicolas Schodet Fix field name when validating the PGP key in RegisterAccount 9 months ago
                                                                                
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
package webhooks

import (
	"context"
	"log"
	"time"

	"git.sr.ht/~sircmpwn/core-go/auth"
	"git.sr.ht/~sircmpwn/core-go/webhooks"
	"github.com/google/uuid"
	sq "github.com/Masterminds/squirrel"

	"git.sr.ht/~sircmpwn/meta.sr.ht/api/graph/model"
)

func deliverProfileWebhook(ctx context.Context, event model.WebhookEvent,
	payload model.WebhookPayload, payloadUUID uuid.UUID) {
	q, ok := ctx.Value(profileWebhooksCtxKey).(*webhooks.WebhookQueue)
	if !ok {
		log.Fatalf("No webhooks worker for this context")
	}
	userID := auth.ForContext(ctx).UserID
	query := sq.
		Select().
		From("gql_profile_wh_sub sub").
		Where("sub.user_id = ?", userID)
	q.Schedule(ctx, query, "profile", event.String(),
		payloadUUID, payload)
}

func DeliverProfileUpdate(ctx context.Context, user *model.User) {
	payloadUUID := uuid.New()
	payload := model.ProfileUpdateEvent{
		UUID:    payloadUUID.String(),
		Event:   model.WebhookEventProfileUpdate,
		Date:    time.Now().UTC(),
		Profile: user,
	}
	event := model.WebhookEventProfileUpdate
	deliverProfileWebhook(ctx, event, &payload, payloadUUID)
}

func DeliverPGPKeyEvent(ctx context.Context,
	event model.WebhookEvent, key *model.PGPKey) {
	payloadUUID := uuid.New()
	payload := model.PGPKeyEvent{
		UUID:  payloadUUID.String(),
		Event: event,
		Date:  time.Now().UTC(),
		Key:   key,
	}
	deliverProfileWebhook(ctx, event, &payload, payloadUUID)
}

func DeliverSSHKeyEvent(ctx context.Context,
	event model.WebhookEvent, key *model.SSHKey) {
	payloadUUID := uuid.New()
	payload := model.SSHKeyEvent{
		UUID:  payloadUUID.String(),
		Event: event,
		Date:  time.Now().UTC(),
		Key:   key,
	}
	deliverProfileWebhook(ctx, event, &payload, payloadUUID)
}