-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpages.go
191 lines (179 loc) · 5.81 KB
/
pages.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// css.gomuks.app - A user CSS repository for gomuks web.
// Copyright (C) 2024 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
_ "image/jpeg"
_ "image/png"
"net/http"
"slices"
"strconv"
"strings"
"github.com/rs/zerolog/hlog"
"go.mau.fi/util/exerrors"
_ "golang.org/x/image/webp"
"maunium.net/go/mautrix/id"
"css.gomuks.app/database"
)
type ThemePageData struct {
Theme *database.Theme `json:"theme,omitempty"`
Themes []*database.Theme `json:"themes,omitempty"`
Commit *database.Commit `json:"commit,omitempty"`
Commits []*database.Commit `json:"commits,omitempty"`
}
func sendResponse(w http.ResponseWriter, r *http.Request, pageTitle, template string, data *ThemePageData) {
if r.Header.Get("Accept") == "application/json" {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
exerrors.PanicIfNotNil(json.NewEncoder(w).Encode(data))
} else if r.Header.Get("Accept") == "text/css" && data.Theme != nil {
w.Header().Set("Content-Type", "text/css; charset=utf-8")
if data.Commit != nil {
_, _ = w.Write([]byte(data.Commit.Content))
} else {
_, _ = w.Write([]byte(data.Theme.LatestCommit.Content))
}
} else {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
exerrors.PanicIfNotNil(Templates.ExecuteTemplate(w, "container.gohtml", &ContainerData{
User: verifyCookie(r),
PageTitle: pageTitle,
Page: template,
Data: data,
}))
}
}
func getIndexPage(w http.ResponseWriter, r *http.Request) {
themes, err := db.Theme.GetAll(r.Context())
if err != nil {
hlog.FromRequest(r).Err(err).Msg("Failed to get themes")
w.WriteHeader(http.StatusInternalServerError)
// TODO write body
return
}
sendResponse(w, r, "", "index.gohtml", &ThemePageData{Themes: themes})
}
func getUserPage(w http.ResponseWriter, r *http.Request) {
userID := id.UserID(r.PathValue("userID"))
themes, err := db.Theme.GetByAdmin(r.Context(), userID)
if err != nil {
hlog.FromRequest(r).Err(err).Msg("Failed to get themes")
w.WriteHeader(http.StatusInternalServerError)
// TODO write body
return
}
sendResponse(w, r, string(userID), "index.gohtml", &ThemePageData{Themes: themes})
}
func getValueWithSuffix(r *http.Request, key string) string {
value := r.PathValue(key)
if strings.HasSuffix(value, ".json") {
value = value[:len(value)-5]
r.Header.Set("Accept", "application/json")
} else if strings.HasSuffix(value, ".css") {
value = value[:len(value)-4]
r.Header.Set("Accept", "text/css")
}
return value
}
func getThemePage(w http.ResponseWriter, r *http.Request) {
themeID := database.ThemeID(getValueWithSuffix(r, "themeID"))
theme, err := db.Theme.Get(r.Context(), themeID)
if err != nil {
hlog.FromRequest(r).Err(err).Msg("Failed to get theme")
w.WriteHeader(http.StatusInternalServerError)
// TODO write body
return
} else if theme == nil {
w.WriteHeader(http.StatusNotFound)
// TODO write body
return
}
var commit *database.Commit
title := theme.Name
if versionStr := getValueWithSuffix(r, "version"); versionStr != "" {
version, err := strconv.Atoi(versionStr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
// TODO write body
return
}
commit, err = db.Commit.Get(r.Context(), themeID, version)
if err != nil {
hlog.FromRequest(r).Err(err).Msg("Failed to get commit")
w.WriteHeader(http.StatusInternalServerError)
// TODO write body
return
} else if commit == nil {
w.WriteHeader(http.StatusNotFound)
// TODO write body
return
}
title += " - v" + versionStr
}
sendResponse(w, r, title, "theme.gohtml", &ThemePageData{Theme: theme, Commit: commit})
}
func getThemeHistoryPage(w http.ResponseWriter, r *http.Request) {
themeID := database.ThemeID(r.PathValue("themeID"))
theme, err := db.Theme.Get(r.Context(), themeID)
if err != nil {
hlog.FromRequest(r).Err(err).Msg("Failed to get theme")
w.WriteHeader(http.StatusInternalServerError)
// TODO write body
return
} else if theme == nil {
w.WriteHeader(http.StatusNotFound)
// TODO write body
return
}
commits, err := db.Commit.GetAll(r.Context(), themeID)
if err != nil {
hlog.FromRequest(r).Err(err).Msg("Failed to get commits")
w.WriteHeader(http.StatusInternalServerError)
// TODO write body
return
}
sendResponse(w, r, theme.Name+" - history", "theme-history.gohtml", &ThemePageData{Theme: theme, Commits: commits})
}
func getThemeEditPage(w http.ResponseWriter, r *http.Request) {
userID := verifyCookie(r)
if userID == "" {
w.WriteHeader(http.StatusUnauthorized)
// TODO write body
return
}
themeID := database.ThemeID(r.PathValue("themeID"))
var theme *database.Theme
pageTitle := "new theme"
if themeID != "" {
var err error
theme, err = db.Theme.Get(r.Context(), themeID)
if err != nil {
hlog.FromRequest(r).Err(err).Msg("Failed to get theme")
w.WriteHeader(http.StatusInternalServerError)
// TODO write body
return
} else if theme == nil {
w.WriteHeader(http.StatusNotFound)
// TODO write body
return
} else if !slices.Contains(theme.Admins, userID) {
w.WriteHeader(http.StatusForbidden)
// TODO write body
return
}
pageTitle = "edit " + theme.Name
}
sendResponse(w, r, pageTitle, "theme-edit.gohtml", &ThemePageData{Theme: theme})
}