aboutsummaryrefslogtreecommitdiff
path: root/modules/user/user.go
blob: 2bf862bcce52352a32d35f23085b2b7555bbbf14 (plain)
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
package user

import (
	"log"
	"net/http"
	"strconv"

	"fagott.pw/charakterin"
	"fagott.pw/grilist/grilist"
	"fagott.pw/grilist/modules/lists"

	"github.com/julienschmidt/httprouter"
)

type Module struct {
	g     *grilist.Grilist
	lists *lists.Module
}

func New() *Module {
	return &Module{}
}

func (m *Module) Name() string {
	return "User"
}

func (m *Module) Init(g *grilist.Grilist) {
	m.g = g
	
	gm, ok := g.Modules["Lists"]
	if !ok {
		log.Fatal("tags: lists module not found")
	}
	m.lists = gm.(*lists.Module)
	
	m.g.Router.GET("/user/:id", m.viewUser)
	m.g.Router.GET("/user/:id/*rest", m.viewUser)
}

func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory {
	return make([]grilist.DashboardCategory, 0)
}

func (m *Module) viewUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	currentUser, _ := m.g.Charakterin.GetUserFromRequest(r)
	
	userID, err := strconv.Atoi(p.ByName("id"))
	if err != nil {
		http.Redirect(w, r, "/", 302)
		return
	}
	
	user := currentUser
	if user == nil || currentUser.ID != userID {
		user, err = m.g.Charakterin.GetUserByID(userID)
		if err != nil {
			http.Redirect(w, r, "/", 302)
			return
		}
	}
	
	userLists := m.lists.GetUserLists(user, true)
	
	data := m.g.Renderer.DefaultData()
	data["user"] = currentUser
	data["DisplayUser"] = user
	data["Lists"] = lists.ListsToCards(userLists)
	
	m.g.Renderer.RenderPage("user", w, data)
}