~evanj/cms

ref: 0ac69893cd1dd4e4cf2fa1498b99682d247e30cd cms/internal/c/hook/hook.go -rw-r--r-- 2.8 KiB
0ac69893Evan M Jones Feat(invites and roles): Users can now set roles while inviting others. 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
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
package hook

import (
	"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(username, password string) (user.User, error)
	UserGetFromToken(token string) (user.User, error)
	SpaceGet(user user.User, spaceID string) (space.Space, error)
	HookNew(user user.User, space space.Space, url string) (hook.Hook, error)
	HookGet(user user.User, space space.Space, id string) (hook.Hook, error)
	HookDelete(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.Error2(w, r, http.StatusBadRequest, err)
		return
	}

	switch h.Method(r) {

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

		hook, err := h.db.HookNew(user, space, r.FormValue("url"))
		if err != nil {
			h.Error2(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(user, r.FormValue("space"))
		if err != nil {
			h.Error2(w, r, http.StatusBadRequest, ErrNoSpace)
			return
		}

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

		if err := h.db.HookDelete(user, space, hook); err != nil {
			h.Error2(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(user, spaceID)
		if err != nil {
			h.Error2(w, r, http.StatusBadRequest, ErrNoSpace)
			return
		}

		hook, err := h.db.HookGet(user, space, hookID)
		if err != nil {
			h.Error2(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)
}