2019-05-19 20:49:27 +03:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2022-12-29 13:54:14 +03:00
|
|
|
"fmt"
|
2021-01-30 12:34:52 +03:00
|
|
|
"github.com/gorilla/mux"
|
2020-10-09 22:37:16 +03:00
|
|
|
conf "github.com/muety/wakapi/config"
|
2022-12-01 12:57:07 +03:00
|
|
|
"github.com/muety/wakapi/helpers"
|
2021-02-03 23:28:02 +03:00
|
|
|
"github.com/muety/wakapi/middlewares"
|
2022-11-19 11:52:44 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
2020-11-06 23:19:54 +03:00
|
|
|
"github.com/muety/wakapi/models/view"
|
2021-02-03 23:28:02 +03:00
|
|
|
su "github.com/muety/wakapi/routes/utils"
|
2020-03-31 13:22:17 +03:00
|
|
|
"github.com/muety/wakapi/services"
|
2020-09-06 13:15:46 +03:00
|
|
|
"net/http"
|
2023-01-18 03:27:07 +03:00
|
|
|
"time"
|
2019-05-19 21:06:07 +03:00
|
|
|
)
|
|
|
|
|
2019-05-19 20:49:27 +03:00
|
|
|
type SummaryHandler struct {
|
2023-01-18 03:27:07 +03:00
|
|
|
config *conf.Config
|
|
|
|
userSrvc services.IUserService
|
|
|
|
summarySrvc services.ISummaryService
|
|
|
|
keyValueSrvc services.IKeyValueService
|
2019-05-21 22:46:23 +03:00
|
|
|
}
|
|
|
|
|
2023-01-18 03:27:07 +03:00
|
|
|
func NewSummaryHandler(summaryService services.ISummaryService, userService services.IUserService, keyValueService services.IKeyValueService) *SummaryHandler {
|
2020-05-24 14:41:19 +03:00
|
|
|
return &SummaryHandler{
|
2023-01-18 03:27:07 +03:00
|
|
|
summarySrvc: summaryService,
|
|
|
|
userSrvc: userService,
|
|
|
|
keyValueSrvc: keyValueService,
|
|
|
|
config: conf.Get(),
|
2020-05-24 11:37:31 +03:00
|
|
|
}
|
2019-05-19 20:49:27 +03:00
|
|
|
}
|
|
|
|
|
2021-01-30 12:34:52 +03:00
|
|
|
func (h *SummaryHandler) RegisterRoutes(router *mux.Router) {
|
2022-01-02 22:04:29 +03:00
|
|
|
r1 := router.PathPrefix("/summary").Subrouter()
|
2023-01-02 20:05:28 +03:00
|
|
|
r1.Use(middlewares.NewAuthenticateMiddleware(h.userSrvc).
|
|
|
|
WithRedirectTarget(defaultErrorRedirectTarget()).
|
|
|
|
WithRedirectErrorMessage("unauthorized").
|
|
|
|
Handler)
|
2022-01-02 22:04:29 +03:00
|
|
|
r1.Methods(http.MethodGet).HandlerFunc(h.GetIndex)
|
|
|
|
|
|
|
|
r2 := router.PathPrefix("/summary").Subrouter()
|
2023-01-02 20:05:28 +03:00
|
|
|
r2.Use(middlewares.NewAuthenticateMiddleware(h.userSrvc).
|
|
|
|
WithRedirectTarget(defaultErrorRedirectTarget()).
|
|
|
|
WithRedirectErrorMessage("unauthorized").
|
|
|
|
Handler)
|
2022-01-02 22:04:29 +03:00
|
|
|
r2.Methods(http.MethodGet).HandlerFunc(h.GetIndex)
|
2020-02-20 16:28:55 +03:00
|
|
|
}
|
|
|
|
|
2020-06-07 20:28:32 +03:00
|
|
|
func (h *SummaryHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
|
2020-05-24 14:41:19 +03:00
|
|
|
if h.config.IsDev() {
|
|
|
|
loadTemplates()
|
2020-02-20 17:39:56 +03:00
|
|
|
}
|
|
|
|
|
2021-02-12 12:10:44 +03:00
|
|
|
rawQuery := r.URL.RawQuery
|
2020-02-20 17:54:20 +03:00
|
|
|
q := r.URL.Query()
|
|
|
|
if q.Get("interval") == "" && q.Get("from") == "" {
|
2022-12-03 14:46:06 +03:00
|
|
|
// If the PersistentIntervalKey cookie is set, redirect to the correct summary page
|
2022-11-05 21:30:40 +03:00
|
|
|
if intervalCookie, _ := r.Cookie(models.PersistentIntervalKey); intervalCookie != nil {
|
2022-12-03 14:46:06 +03:00
|
|
|
redirectAddress := fmt.Sprintf("%s/summary?interval=%s", h.config.Server.BasePath, intervalCookie.Value)
|
|
|
|
http.Redirect(w, r, redirectAddress, http.StatusFound)
|
2022-11-05 21:30:40 +03:00
|
|
|
}
|
2022-12-03 14:46:06 +03:00
|
|
|
|
2020-02-20 17:54:20 +03:00
|
|
|
q.Set("interval", "today")
|
|
|
|
r.URL.RawQuery = q.Encode()
|
2022-11-19 11:52:44 +03:00
|
|
|
} else if q.Get("interval") != "" {
|
2022-12-03 14:46:06 +03:00
|
|
|
// Send a Set-Cookie header to persist the interval
|
2022-11-19 11:52:44 +03:00
|
|
|
headerValue := fmt.Sprintf("%s=%s", models.PersistentIntervalKey, q.Get("interval"))
|
|
|
|
w.Header().Add("Set-Cookie", headerValue)
|
2020-02-20 17:54:20 +03:00
|
|
|
}
|
|
|
|
|
2022-12-01 12:57:07 +03:00
|
|
|
summaryParams, _ := helpers.ParseSummaryParams(r)
|
2021-02-03 23:28:02 +03:00
|
|
|
summary, err, status := su.LoadUserSummary(h.summarySrvc, r)
|
2020-02-20 16:28:55 +03:00
|
|
|
if err != nil {
|
2020-11-06 23:19:54 +03:00
|
|
|
w.WriteHeader(status)
|
2022-04-20 22:36:39 +03:00
|
|
|
conf.Log().Request(r).Error("failed to load summary - %v", err)
|
2023-01-02 20:05:28 +03:00
|
|
|
templates[conf.SummaryTemplate].Execute(w, h.buildViewModel(r, w).WithError(err.Error()))
|
2020-05-24 22:19:05 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-26 15:10:10 +03:00
|
|
|
user := middlewares.GetPrincipal(r)
|
2020-05-24 22:19:05 +03:00
|
|
|
if user == nil {
|
2020-11-06 23:19:54 +03:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
2023-01-02 20:05:28 +03:00
|
|
|
templates[conf.SummaryTemplate].Execute(w, h.buildViewModel(r, w).WithError("unauthorized"))
|
2020-02-20 16:28:55 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-18 03:27:07 +03:00
|
|
|
// user first data
|
|
|
|
var firstData time.Time
|
|
|
|
firstDataKv := h.keyValueSrvc.MustGetString(fmt.Sprintf("%s_%s", conf.KeyFirstHeartbeat, user.ID))
|
|
|
|
if firstDataKv.Value != "" {
|
|
|
|
firstData, _ = time.Parse(time.RFC822Z, firstDataKv.Value)
|
|
|
|
}
|
|
|
|
|
2021-12-16 14:39:41 +03:00
|
|
|
vm := view.SummaryViewModel{
|
2020-03-31 12:24:44 +03:00
|
|
|
Summary: summary,
|
2021-04-16 12:53:37 +03:00
|
|
|
SummaryParams: summaryParams,
|
2021-02-13 14:59:59 +03:00
|
|
|
User: user,
|
2022-12-29 13:54:14 +03:00
|
|
|
EditorColors: su.FilterColors(h.config.App.GetEditorColors(), summary.Editors),
|
|
|
|
LanguageColors: su.FilterColors(h.config.App.GetLanguageColors(), summary.Languages),
|
|
|
|
OSColors: su.FilterColors(h.config.App.GetOSColors(), summary.OperatingSystems),
|
2020-05-24 22:19:05 +03:00
|
|
|
ApiKey: user.ApiKey,
|
2021-02-12 12:10:44 +03:00
|
|
|
RawQuery: rawQuery,
|
2023-01-18 03:27:07 +03:00
|
|
|
UserFirstData: firstData,
|
2020-03-31 12:24:44 +03:00
|
|
|
}
|
|
|
|
|
2020-10-09 22:37:16 +03:00
|
|
|
templates[conf.SummaryTemplate].Execute(w, vm)
|
2020-02-20 16:28:55 +03:00
|
|
|
}
|
|
|
|
|
2023-01-02 20:05:28 +03:00
|
|
|
func (h *SummaryHandler) buildViewModel(r *http.Request, w http.ResponseWriter) *view.SummaryViewModel {
|
|
|
|
return su.WithSessionMessages(&view.SummaryViewModel{}, r, w)
|
2020-11-06 23:19:54 +03:00
|
|
|
}
|