~evanj/cms

ref: a1db405a36ebf4c89a8f98602af20c37bc6ff381 cms/internal/c/hook/hook.go -rw-r--r-- 3.0 KiB
a1db405aEvan J Fix(s/stripe): Resolved stripe transaction error: misconfiguration in 1 year, 2 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
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
package hook

import (
	"context"
	"errors"
	"fmt"
	"log"
	"net/http"
	"strings"

	"git.sr.ht/~evanj/cms/internal/c"
	"git.sr.ht/~evanj/cms/internal/m/hook"
	"git.sr.ht/~evanj/cms/internal/m/space"
	"git.sr.ht/~evanj/cms/internal/m/user"
	"git.sr.ht/~evanj/cms/internal/v"
)

var (
	hookHTML = v.MustParse("html/hook.html")

	ErrNoSpace      = errors.New("failed to find required space")
	ErrNoHook       = errors.New("failed to find desired webhook")
	ErrFailedCreate = errors.New("failed to create webhook")
	ErrFailedDelete = errors.New("failed to delete webhook")
)

type Content struct {
	*c.Controller
	log *log.Logger
	db  dber
}

type dber interface {
	UserGet(ctx context.Context, username, password string) (user.User, error)
	UserGetFromToken(ctx context.Context, token string) (user.User, error)
	SpaceGet(ctx context.Context, user user.User, spaceID string) (space.Space, error)
	HookNew(ctx context.Context, user user.User, space space.Space, url string) (hook.Hook, error)
	HookGet(ctx context.Context, user user.User, space space.Space, id string) (hook.Hook, error)
	HookDelete(ctx context.Context, user user.User, space space.Space, hook hook.Hook) error
}

func New(c *c.Controller, log *log.Logger, db dber) *Content {
	return &Content{
		c,
		log,
		db,
	}
}

func (h *Content) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	user, err := h.GetCookieUser(w, r)
	if err != nil {
		h.Error(w, r, http.StatusBadRequest, err)
		return
	}

	switch h.Method(r) {

	case "POST":
		space, err := h.db.SpaceGet(r.Context(), user, r.FormValue("space"))
		if err != nil {
			h.Error(w, r, http.StatusBadRequest, ErrNoSpace)
			return
		}

		hook, err := h.db.HookNew(r.Context(), user, space, r.FormValue("url"))
		if err != nil {
			h.Error(w, r, http.StatusInternalServerError, ErrFailedCreate)
			return
		}

		h.Redirect(w, r, fmt.Sprintf("/hook/%s/%s", space.ID(), hook.ID()))
		return

	case "DELETE":
		space, err := h.db.SpaceGet(r.Context(), user, r.FormValue("space"))
		if err != nil {
			h.Error(w, r, http.StatusBadRequest, ErrNoSpace)
			return
		}

		hook, err := h.db.HookGet(r.Context(), user, space, r.FormValue("hook"))
		if err != nil {
			h.Error(w, r, http.StatusBadRequest, ErrNoHook)
			return
		}

		if err := h.db.HookDelete(r.Context(), user, space, hook); err != nil {
			h.Error(w, r, http.StatusInternalServerError, ErrFailedDelete)
			return
		}

		h.Redirect(w, r, fmt.Sprintf("/space/%s", space.ID()))
		return

	case "GET":
		spaceID := r.FormValue("space")
		hookID := r.FormValue("hook")

		parts := strings.Split(r.URL.Path, "/")
		if len(parts) > 3 {
			spaceID = parts[2]
			hookID = parts[3]
		}

		space, err := h.db.SpaceGet(r.Context(), user, spaceID)
		if err != nil {
			h.Error(w, r, http.StatusBadRequest, ErrNoSpace)
			return
		}

		hook, err := h.db.HookGet(r.Context(), user, space, hookID)
		if err != nil {
			h.Error(w, r, http.StatusBadRequest, ErrNoHook)
			return
		}

		h.HTML(w, r, hookHTML, map[string]interface{}{
			"User":  user,
			"Space": space,
			"Hook":  hook,
		})
		return

	}

	http.NotFound(w, r)
}