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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// 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 <http://www.gnu.org/licenses/>.
package router
import (
"bytes"
"fmt"
"html/template"
"os"
"path/filepath"
"reflect"
"regexp"
"slices"
"strings"
"sync"
"unsafe"
apimodel "code.superseriousbusiness.org/gotosocial/internal/api/model"
"code.superseriousbusiness.org/gotosocial/internal/config"
"code.superseriousbusiness.org/gotosocial/internal/gtserror"
"code.superseriousbusiness.org/gotosocial/internal/log"
"code.superseriousbusiness.org/gotosocial/internal/regexes"
"code.superseriousbusiness.org/gotosocial/internal/text"
"code.superseriousbusiness.org/gotosocial/internal/util"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/render"
)
// LoadTemplates loads templates found at `web-template-base-dir`
// into the Gin engine, or errors if templates cannot be loaded.
//
// The special functions "include" and "includeAttr" will be added
// to the template funcMap for use in any template. Use these "include"
// functions when you need to pass a template through a pipeline.
// Otherwise, prefer the built-in "template" function.
func LoadTemplates(engine *gin.Engine) error {
templateBaseDir := config.GetWebTemplateBaseDir()
if templateBaseDir == "" {
return gtserror.Newf(
"%s cannot be empty and must be a relative or absolute path",
config.WebTemplateBaseDirFlag,
)
}
templateDirAbs, err := filepath.Abs(templateBaseDir)
if err != nil {
return gtserror.Newf(
"error getting absolute path of web-template-base-dir %s: %w",
templateBaseDir, err,
)
}
indexTmplPath := filepath.Join(templateDirAbs, "index.tmpl")
if _, err := os.Stat(indexTmplPath); err != nil {
return gtserror.Newf(
"cannot find index.tmpl in web template directory %s: %w",
templateDirAbs, err,
)
}
// Bring base template into scope.
tmpl := template.New("base")
// Set additional "include" functions to render
// provided template name using the base template.
// Include renders the given template with the given data.
// Unlike `template`, `include` can be chained with `indent`
// to produce nicely-indented HTML.
funcMap["include"] = func(name string, data any) (template.HTML, error) {
var buf strings.Builder
err := tmpl.ExecuteTemplate(&buf, name, data)
// Template was already escaped by
// ExecuteTemplate so we can trust it.
return noescape(buf.String()), err
}
// includeIndex is like `include` but an index can be specified at
// `.Index` and data will be nested at `.Item`. Useful when ranging.
funcMap["includeIndex"] = func(name string, data any, index int) (template.HTML, error) {
var buf strings.Builder
withIndex := struct {
Item any
Index int
}{
Item: data,
Index: index,
}
err := tmpl.ExecuteTemplate(&buf, name, withIndex)
// Template was already escaped by
// ExecuteTemplate so we can trust it.
return noescape(buf.String()), err
}
// includeAttr is like `include` but for element attributes.
funcMap["includeAttr"] = func(name string, data any) (template.HTMLAttr, error) {
var buf strings.Builder
err := tmpl.ExecuteTemplate(&buf, name, data)
// Template was already escaped by
// ExecuteTemplate so we can trust it.
return noescapeAttr(buf.String()), err
}
// Load functions into the base template, and
// associate other templates with base template.
templateGlob := filepath.Join(templateDirAbs, "*")
tmpl, err = tmpl.Funcs(funcMap).ParseGlob(templateGlob)
if err != nil {
return gtserror.Newf("error loading templates: %w", err)
}
// Almost done; teach the
// engine how to render.
engine.SetFuncMap(funcMap)
engine.HTMLRender = render.HTMLProduction{Template: tmpl}
return nil
}
var funcMap = template.FuncMap{
"add": add,
"acctInstance": acctInstance,
"objectPosition": objectPosition,
"demojify": demojify,
"deref": deref,
"emojify": emojify,
"escape": escape,
"increment": increment,
"indent": indent,
"indentAttr": indentAttr,
"isNil": isNil,
"outdentPreformatted": outdentPreformatted,
"noescapeAttr": noescapeAttr,
"noescape": noescape,
"oddOrEven": oddOrEven,
"subtract": subtract,
"timestampPrecise": timestampPrecise,
"timestampVague": timestampVague,
"visibilityIcon": visibilityIcon,
}
func oddOrEven(n int) string {
if n%2 == 0 {
return "even"
}
return "odd"
}
// escape HTML escapes the given string,
// returning a trusted template.
func escape(str string) template.HTML {
/* #nosec G203 */
return template.HTML(template.HTMLEscapeString(str))
}
// noescape marks the given string as a
// trusted template. The provided string
// MUST have already passed through a
// template or escaping function.
func noescape(str string) template.HTML {
/* #nosec G203 */
return template.HTML(str)
}
// noescapeAttr marks the given string as a
// trusted HTML attribute. The provided string
// MUST have already passed through a template
// or escaping function.
func noescapeAttr(str string) template.HTMLAttr {
/* #nosec G203 */
return template.HTMLAttr(str)
}
const (
justTime = "15:04"
dateYear = "Jan 02, 2006"
dateTime = "Jan 02, 15:04"
dateYearTime = "Jan 02, 2006, 15:04"
monthYear = "Jan, 2006"
badTimestamp = "bad timestamp"
)
func timestampPrecise(stamp string) string {
t, err := util.ParseISO8601(stamp)
if err != nil {
log.Errorf(nil, "error parsing timestamp %s: %s", stamp, err)
return badTimestamp
}
return t.Local().Format(dateYearTime)
}
func timestampVague(stamp string) string {
t, err := util.ParseISO8601(stamp)
if err != nil {
log.Errorf(nil, "error parsing timestamp %s: %s", stamp, err)
return badTimestamp
}
return t.Format(monthYear)
}
func visibilityIcon(visibility apimodel.Visibility) template.HTML {
var (
label string
icon string
)
switch visibility {
case apimodel.VisibilityPublic:
label = "public"
icon = "globe"
case apimodel.VisibilityUnlisted:
label = "unlisted"
icon = "unlock"
case apimodel.VisibilityPrivate:
label = "private"
icon = "lock"
case apimodel.VisibilityMutualsOnly:
label = "mutuals-only"
icon = "handshake-o"
case apimodel.VisibilityDirect:
label = "direct"
icon = "envelope"
}
/* #nosec G203 */
return template.HTML(fmt.Sprintf(
`<i aria-label="Visibility: %s" class="fa fa-%s"></i>`,
label, icon,
))
}
// emojify replaces emojis in the given
// html fragment with suitable <img> tags.
//
// The provided input must have been
// escaped / templated already!
func emojify(
emojis []apimodel.Emoji,
html template.HTML,
) template.HTML {
return text.EmojifyWeb(emojis, html)
}
// demojify replaces emoji shortcodes in
// the given fragment with empty strings.
//
// Output must then be escaped as appropriate.
func demojify(input string) string {
return text.Demojify(input)
}
func acctInstance(acct string) string {
parts := strings.Split(acct, "@")
if len(parts) > 1 {
return "@" + parts[1]
}
return ""
}
// increment adds 1
// to the given int.
func increment(i int) int {
return i + 1
}
// add adds n2 to n1.
func add(n1 int, n2 int) int {
return n1 + n2
}
// subtract subtracts n2 from n1.
func subtract(n1 int, n2 int) int {
return n1 - n2
}
var (
// Find starts of lines to replace with indent.
indentRegex = regexp.MustCompile(`(?m)^`)
// One indent level.
indentStr = " "
indentStrLen = len(indentStr)
// Preformatted slice of indents.
indents = strings.Repeat(indentStr, 12)
// Measure indent at the start of a line.
indentDepthStr = fmt.Sprintf(`^((?:%s)+)`, indentStr)
indentDepth = regexp.MustCompile(`(?m)` + indentDepthStr)
// Find <pre> tags and determine how indented they are.
indentPre = regexp.MustCompile(fmt.Sprintf(`(?Ums)%s<pre>.*</pre>`, indentDepthStr))
// Find content of alt or title attributes.
indentAltOrTitle = regexp.MustCompile(`(?Ums)\b(?:alt|title)="(.*)"(?:\b|>|$)`)
// Map of lazily-compiled replaceIndent
// regexes, keyed by the indent they
// replace, to avoid recompilation.
//
// At *most* 12 entries long.
replaceIndents = sync.Map{}
)
// indent appropriately indents the given html
// by prepending each line with the indentStr.
func indent(n int, html template.HTML) template.HTML {
out := indentRegex.ReplaceAllString(
string(html),
indents[:n*indentStrLen],
)
return noescape(out)
}
// indentAttr appropriately indents the given html
// attribute by prepending each line with the indentStr.
func indentAttr(n int, html template.HTMLAttr) template.HTMLAttr {
out := indentRegex.ReplaceAllString(
string(html),
indents[:n*indentStrLen],
)
return noescapeAttr(out)
}
// outdentPreformatted outdents all preformatted text in
// the given HTML, ie., in `alt` and `title` attributes,
// and between `<pre>` tags, so that it renders correctly,
// even if it was indented before.
func outdentPreformatted(html template.HTML) template.HTML {
input := string(html)
output := regexes.ReplaceAllStringFunc(indentPre, input,
func(match string, buf *bytes.Buffer) string {
// Reuse the regex to pull out submatches.
matches := indentPre.FindAllStringSubmatch(match, -1)
// Ensure matches
// expected length.
if len(matches) != 1 {
return match
}
// Ensure inner matches
// expected length.
innerMatches := matches[0]
if len(innerMatches) != 2 {
return match
}
var (
indentedContent = innerMatches[0]
indent = innerMatches[1]
)
// Outdent everything in the inner match.
outdented := strings.ReplaceAll(indentedContent, indent, "")
// Replace original match with the outdented version.
return strings.ReplaceAll(match, indentedContent, outdented)
},
)
output = regexes.ReplaceAllStringFunc(indentAltOrTitle, output,
func(match string, buf *bytes.Buffer) string {
// Reuse the regex to pull out submatches.
matches := indentAltOrTitle.FindAllStringSubmatch(match, -1)
// Ensure matches
// expected length.
if len(matches) != 1 {
return match
}
// Ensure inner matches
// expected length.
innerMatches := matches[0]
if len(innerMatches) != 2 {
return match
}
// The content of the alt or title
// attr inside quotation marks.
indentedContent := innerMatches[1]
// Find all indents in this text.
indents := indentDepth.FindAllString(indentedContent, -1)
if len(indents) == 0 {
// No indents in this text,
// it's probably just something
// inline like `alt="whatever"`.
return match
}
// Find the shortest indent as this
// is undoubtedly the one we added.
//
// By targeting the shortest one we
// avoid removing user-inserted
// whitespace at the start of lines
// of alt text (eg., in poetry etc).
slices.Sort(indents)
indent := indents[0]
// Load or create + store the
// regex to replace this indent,
// avoiding recompilation.
var replaceIndent *regexp.Regexp
if replaceIndentI, ok := replaceIndents.Load(indent); ok {
// Got regex for this indent.
replaceIndent = replaceIndentI.(*regexp.Regexp)
} else {
// No regex stored for
// this indent yet, store it.
replaceIndent = regexp.MustCompile(`(?m)^` + indent)
replaceIndents.Store(indent, replaceIndent)
}
// Remove all occurrences of the indent
// at the start of a line in the match.
return replaceIndent.ReplaceAllString(match, "")
},
)
return noescape(output)
}
// isNil will safely check if 'v' is nil without
// dealing with weird Go interface nil bullshit.
func isNil(i interface{}) bool {
type eface struct{ _, data unsafe.Pointer }
return (*eface)(unsafe.Pointer(&i)).data == nil
}
// deref returns the dereferenced value of
// its input. To ensure you don't pass nil
// pointers into this func, use isNil first.
func deref(i any) any {
vOf := reflect.ValueOf(i)
if vOf.Kind() != reflect.Pointer {
// Not a pointer.
return i
}
return vOf.Elem()
}
// objectPosition formats the given focus coordinates to a
// string suitable for use as a css object-position value.
func objectPosition(focusX float32, focusY float32) string {
const fmts = "%.2f"
xPos := ((focusX / 2) + .5) * 100
yPos := ((focusY / -2) + .5) * 100
return fmt.Sprintf(fmts, xPos) + "%" + " " + fmt.Sprintf(fmts, yPos) + "%"
}
|