2020-11-10 09:31:02 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2021-02-26 22:17:04 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2020-11-10 09:31:02 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) GetHandleError(c *gin.Context, code int, message, details string) {
|
|
|
|
c.HTML(code, "error.html", gin.H{
|
|
|
|
"Data": gin.H{
|
|
|
|
"Code": strconv.Itoa(code),
|
|
|
|
"Message": message,
|
|
|
|
"Details": details,
|
|
|
|
},
|
|
|
|
"Route": c.Request.URL.Path,
|
2021-02-24 21:24:45 +01:00
|
|
|
"Session": GetSessionData(c),
|
2020-11-10 09:31:02 +01:00
|
|
|
"Static": s.getStaticData(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetIndex(c *gin.Context) {
|
|
|
|
c.HTML(http.StatusOK, "index.html", struct {
|
|
|
|
Route string
|
2020-11-10 22:23:05 +01:00
|
|
|
Alerts []FlashData
|
2020-11-10 09:31:02 +01:00
|
|
|
Session SessionData
|
|
|
|
Static StaticData
|
|
|
|
Device Device
|
|
|
|
}{
|
|
|
|
Route: c.Request.URL.Path,
|
2021-02-24 21:24:45 +01:00
|
|
|
Alerts: GetFlashes(c),
|
|
|
|
Session: GetSessionData(c),
|
2020-11-10 09:31:02 +01:00
|
|
|
Static: s.getStaticData(),
|
2021-02-24 21:24:45 +01:00
|
|
|
Device: s.peers.GetDevice(),
|
2020-11-10 09:31:02 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetAdminIndex(c *gin.Context) {
|
2021-02-24 21:24:45 +01:00
|
|
|
currentSession := GetSessionData(c)
|
2020-11-10 09:31:02 +01:00
|
|
|
|
|
|
|
sort := c.Query("sort")
|
|
|
|
if sort != "" {
|
2021-02-24 21:24:45 +01:00
|
|
|
if currentSession.SortedBy["peers"] != sort {
|
|
|
|
currentSession.SortedBy["peers"] = sort
|
|
|
|
currentSession.SortDirection["peers"] = "asc"
|
2020-11-10 09:31:02 +01:00
|
|
|
} else {
|
2021-02-24 21:24:45 +01:00
|
|
|
if currentSession.SortDirection["peers"] == "asc" {
|
|
|
|
currentSession.SortDirection["peers"] = "desc"
|
2020-11-10 09:31:02 +01:00
|
|
|
} else {
|
2021-02-24 21:24:45 +01:00
|
|
|
currentSession.SortDirection["peers"] = "asc"
|
2020-11-10 09:31:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 21:24:45 +01:00
|
|
|
if err := UpdateSessionData(c, currentSession); err != nil {
|
2020-11-10 09:31:02 +01:00
|
|
|
s.GetHandleError(c, http.StatusInternalServerError, "sort error", "failed to save session")
|
|
|
|
return
|
|
|
|
}
|
2021-02-24 21:24:45 +01:00
|
|
|
c.Redirect(http.StatusSeeOther, "/admin/")
|
2020-11-10 09:31:02 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
search, searching := c.GetQuery("search")
|
|
|
|
if searching {
|
2021-02-24 21:24:45 +01:00
|
|
|
currentSession.Search["peers"] = search
|
2020-11-10 09:31:02 +01:00
|
|
|
|
2021-02-24 21:24:45 +01:00
|
|
|
if err := UpdateSessionData(c, currentSession); err != nil {
|
2020-11-10 09:31:02 +01:00
|
|
|
s.GetHandleError(c, http.StatusInternalServerError, "search error", "failed to save session")
|
|
|
|
return
|
|
|
|
}
|
2021-02-24 21:24:45 +01:00
|
|
|
c.Redirect(http.StatusSeeOther, "/admin/")
|
2020-11-10 09:31:02 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-24 21:24:45 +01:00
|
|
|
device := s.peers.GetDevice()
|
|
|
|
users := s.peers.GetFilteredAndSortedPeers(currentSession.SortedBy["peers"], currentSession.SortDirection["peers"], currentSession.Search["peers"])
|
2020-11-10 09:31:02 +01:00
|
|
|
|
|
|
|
c.HTML(http.StatusOK, "admin_index.html", struct {
|
2021-02-24 21:24:45 +01:00
|
|
|
Route string
|
|
|
|
Alerts []FlashData
|
|
|
|
Session SessionData
|
|
|
|
Static StaticData
|
|
|
|
Peers []Peer
|
|
|
|
TotalPeers int
|
|
|
|
Device Device
|
2020-11-10 09:31:02 +01:00
|
|
|
}{
|
2021-02-24 21:24:45 +01:00
|
|
|
Route: c.Request.URL.Path,
|
|
|
|
Alerts: GetFlashes(c),
|
|
|
|
Session: currentSession,
|
|
|
|
Static: s.getStaticData(),
|
|
|
|
Peers: users,
|
|
|
|
TotalPeers: len(s.peers.GetAllPeers()),
|
|
|
|
Device: device,
|
2020-11-10 09:31:02 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetUserIndex(c *gin.Context) {
|
2021-02-24 21:24:45 +01:00
|
|
|
currentSession := GetSessionData(c)
|
2020-11-10 09:31:02 +01:00
|
|
|
|
|
|
|
sort := c.Query("sort")
|
|
|
|
if sort != "" {
|
2021-02-24 21:24:45 +01:00
|
|
|
if currentSession.SortedBy["userpeers"] != sort {
|
|
|
|
currentSession.SortedBy["userpeers"] = sort
|
|
|
|
currentSession.SortDirection["userpeers"] = "asc"
|
2020-11-10 09:31:02 +01:00
|
|
|
} else {
|
2021-02-24 21:24:45 +01:00
|
|
|
if currentSession.SortDirection["userpeers"] == "asc" {
|
|
|
|
currentSession.SortDirection["userpeers"] = "desc"
|
2020-11-10 09:31:02 +01:00
|
|
|
} else {
|
2021-02-24 21:24:45 +01:00
|
|
|
currentSession.SortDirection["userpeers"] = "asc"
|
2020-11-10 09:31:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 21:24:45 +01:00
|
|
|
if err := UpdateSessionData(c, currentSession); err != nil {
|
2020-11-10 09:31:02 +01:00
|
|
|
s.GetHandleError(c, http.StatusInternalServerError, "sort error", "failed to save session")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Redirect(http.StatusSeeOther, "/admin")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-24 21:24:45 +01:00
|
|
|
device := s.peers.GetDevice()
|
|
|
|
users := s.peers.GetSortedPeersForEmail(currentSession.SortedBy["userpeers"], currentSession.SortDirection["userpeers"], currentSession.Email)
|
2020-11-10 09:31:02 +01:00
|
|
|
|
|
|
|
c.HTML(http.StatusOK, "user_index.html", struct {
|
|
|
|
Route string
|
2020-11-10 22:23:05 +01:00
|
|
|
Alerts []FlashData
|
2020-11-10 09:31:02 +01:00
|
|
|
Session SessionData
|
|
|
|
Static StaticData
|
2021-02-21 23:23:58 +01:00
|
|
|
Peers []Peer
|
2020-11-10 09:31:02 +01:00
|
|
|
TotalPeers int
|
|
|
|
Device Device
|
|
|
|
}{
|
|
|
|
Route: c.Request.URL.Path,
|
2021-02-24 21:24:45 +01:00
|
|
|
Alerts: GetFlashes(c),
|
2020-11-10 09:31:02 +01:00
|
|
|
Session: currentSession,
|
|
|
|
Static: s.getStaticData(),
|
|
|
|
Peers: users,
|
|
|
|
TotalPeers: len(users),
|
|
|
|
Device: device,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) updateFormInSession(c *gin.Context, formData interface{}) error {
|
2021-02-24 21:24:45 +01:00
|
|
|
currentSession := GetSessionData(c)
|
2020-11-10 09:31:02 +01:00
|
|
|
currentSession.FormData = formData
|
|
|
|
|
2021-02-24 21:24:45 +01:00
|
|
|
if err := UpdateSessionData(c, currentSession); err != nil {
|
2021-02-26 22:17:04 +01:00
|
|
|
return errors.WithMessage(err, "failed to update form in session")
|
2020-11-10 09:31:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 21:24:45 +01:00
|
|
|
func (s *Server) setNewPeerFormInSession(c *gin.Context) (SessionData, error) {
|
|
|
|
currentSession := GetSessionData(c)
|
|
|
|
// If session does not contain a peer form ignore update
|
2020-11-10 09:31:02 +01:00
|
|
|
// If url contains a formerr parameter reset the form
|
|
|
|
if currentSession.FormData == nil || c.Query("formerr") == "" {
|
2021-02-24 21:24:45 +01:00
|
|
|
user, err := s.PrepareNewPeer()
|
2020-11-10 09:31:02 +01:00
|
|
|
if err != nil {
|
2021-02-26 22:17:04 +01:00
|
|
|
return currentSession, errors.WithMessage(err, "failed to prepare new peer")
|
2020-11-10 09:31:02 +01:00
|
|
|
}
|
|
|
|
currentSession.FormData = user
|
|
|
|
}
|
|
|
|
|
2021-02-24 21:24:45 +01:00
|
|
|
if err := UpdateSessionData(c, currentSession); err != nil {
|
2021-02-26 22:17:04 +01:00
|
|
|
return currentSession, errors.WithMessage(err, "failed to update peer form in session")
|
2020-11-10 09:31:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return currentSession, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) setFormInSession(c *gin.Context, formData interface{}) (SessionData, error) {
|
2021-02-24 21:24:45 +01:00
|
|
|
currentSession := GetSessionData(c)
|
2020-11-10 09:31:02 +01:00
|
|
|
// If session does not contain a form ignore update
|
|
|
|
// If url contains a formerr parameter reset the form
|
|
|
|
if currentSession.FormData == nil || c.Query("formerr") == "" {
|
|
|
|
currentSession.FormData = formData
|
|
|
|
}
|
|
|
|
|
2021-02-24 21:24:45 +01:00
|
|
|
if err := UpdateSessionData(c, currentSession); err != nil {
|
2021-02-26 22:17:04 +01:00
|
|
|
return currentSession, errors.WithMessage(err, "failed to set form in session")
|
2020-11-10 09:31:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return currentSession, nil
|
|
|
|
}
|