Exemple #1
0
func serveAdmin(h *Handler, c *context) {
	if !c.userInfo.IsAdmin {
		http.Error(c.w, "not an admin", 403)
		return
	}

	type recentUser struct {
		User      string
		LastVisit string
	}

	type dat struct {
		UserInfo    *userInfo
		RecentUsers []*recentUser
	}

	res, err := h.s.SiteDB.ListRecentActiveUsers(time.Hour * 72)
	if err != nil {
		http.Error(c.w, err.Error(), 500)
		return
	}
	d := &dat{UserInfo: newUserInfo(c)}
	now := time.Now()
	for _, ut := range res {
		d.RecentUsers = append(d.RecentUsers, &recentUser{
			User:      ut.User,
			LastVisit: moment.String(ut.Time, now),
		})
	}

	h.servePage(c, "admin.html", d)
}
Exemple #2
0
func serveIndex(h *Handler, c *context) {
	owners, err := h.s.SiteDB.ListChanges(c.user)
	if err != nil {
		log.Println(err)
		http.Error(c.w, "database error", 400)
		return
	}

	others, err := h.s.SiteDB.ListChanges("!" + c.user)
	if err != nil {
		log.Println(err)
		http.Error(c.w, "database error", 400)
		return
	}

	type ch struct {
		ID         int64
		Owner      string
		URL        template.URL
		Title      string
		LastModify string
	}
	type d struct {
		UserInfo      *userInfo
		OwnChanges    []*ch
		OthersChanges []*ch
	}

	now := time.Now()
	newCh := func(c *sitedb.Changes) *ch {
		return &ch{
			ID:         c.ID,
			Owner:      c.Owner,
			URL:        template.URL(fmt.Sprintf("c/%d", c.ID)),
			Title:      c.Title,
			LastModify: moment.String(c.LastModify, now),
		}
	}

	dat := &d{UserInfo: newUserInfo(c)}
	for _, change := range owners {
		dat.OwnChanges = append(dat.OwnChanges, newCh(change))
	}
	for _, change := range others {
		dat.OthersChanges = append(dat.OthersChanges, newCh(change))
	}

	h.servePage(c, "index.html", dat)
}