-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
81 lines (66 loc) · 2.08 KB
/
profile.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
// Content managed by Project Forge, see [projectforge.md] for details.
package clib
import (
"net/url"
"github.com/pkg/errors"
"github.com/valyala/fasthttp"
"admini.dev/admini/app"
"admini.dev/admini/app/controller"
"admini.dev/admini/app/controller/csession"
"admini.dev/admini/app/controller/cutil"
"admini.dev/admini/app/lib/theme"
"admini.dev/admini/views/vprofile"
)
func Profile(rc *fasthttp.RequestCtx) {
controller.Act("profile", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
return profileAction(rc, as, ps)
})
}
func ProfileSite(rc *fasthttp.RequestCtx) {
controller.ActSite("profile", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
return profileAction(rc, as, ps)
})
}
func profileAction(rc *fasthttp.RequestCtx, as *app.State, ps *cutil.PageState) (string, error) {
ps.Title = "Profile"
ps.Data = ps.Profile
thm := as.Themes.Get(ps.Profile.Theme, ps.Logger)
prvs, err := as.Auth.Providers(ps.Logger)
if err != nil {
return "", errors.Wrap(err, "can't load providers")
}
redir := "/"
ref := string(rc.Request.Header.Peek("Referer"))
if ref != "" {
u, err := url.Parse(ref)
if err == nil && u != nil && u.Path != cutil.DefaultProfilePath {
redir = u.Path
}
}
page := &vprofile.Profile{Profile: ps.Profile, Theme: thm, Providers: prvs, Referrer: redir}
return controller.Render(rc, as, page, ps, "Profile")
}
func ProfileSave(rc *fasthttp.RequestCtx) {
controller.Act("profile.save", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
frm, err := cutil.ParseForm(rc)
if err != nil {
return "", err
}
n := ps.Profile.Clone()
referrerDefault := frm.GetStringOpt("referrer")
if referrerDefault == "" {
referrerDefault = cutil.DefaultProfilePath
}
n.Name = frm.GetStringOpt("name")
n.Mode = frm.GetStringOpt("mode")
n.Theme = frm.GetStringOpt("theme")
if n.Theme == theme.ThemeDefault.Key {
n.Theme = ""
}
err = csession.SaveProfile(n, rc, ps.Session, ps.Logger)
if err != nil {
return "", err
}
return controller.ReturnToReferrer("Saved profile", referrerDefault, rc, ps)
})
}