2020-09-06 13:15:46 +03:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gorilla/mux"
|
2020-11-08 12:12:49 +03:00
|
|
|
conf "github.com/muety/wakapi/config"
|
2020-09-06 13:15:46 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
2020-09-12 17:09:23 +03:00
|
|
|
v1 "github.com/muety/wakapi/models/compat/wakatime/v1"
|
2020-09-06 13:15:46 +03:00
|
|
|
"github.com/muety/wakapi/services"
|
|
|
|
"github.com/muety/wakapi/utils"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-09-12 17:09:23 +03:00
|
|
|
type AllTimeHandler struct {
|
2020-11-08 12:12:49 +03:00
|
|
|
config *conf.Config
|
|
|
|
summarySrvc services.ISummaryService
|
2020-09-06 13:15:46 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 12:12:49 +03:00
|
|
|
func NewAllTimeHandler(summaryService services.ISummaryService) *AllTimeHandler {
|
2020-09-12 17:09:23 +03:00
|
|
|
return &AllTimeHandler{
|
2020-09-06 13:15:46 +03:00
|
|
|
summarySrvc: summaryService,
|
2020-11-08 12:12:49 +03:00
|
|
|
config: conf.Get(),
|
2020-09-06 13:15:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 23:28:02 +03:00
|
|
|
func (h *AllTimeHandler) RegisterRoutes(router *mux.Router) {
|
|
|
|
router.Path("/wakatime/v1/users/{user}/all_time_since_today").Methods(http.MethodGet).HandlerFunc(h.Get)
|
2021-01-30 12:34:52 +03:00
|
|
|
}
|
|
|
|
|
2021-02-03 23:28:02 +03:00
|
|
|
func (h *AllTimeHandler) Get(w http.ResponseWriter, r *http.Request) {
|
2020-09-06 13:15:46 +03:00
|
|
|
vars := mux.Vars(r)
|
2020-09-12 00:24:51 +03:00
|
|
|
values, _ := url.ParseQuery(r.URL.RawQuery)
|
|
|
|
|
2020-09-06 13:15:46 +03:00
|
|
|
requestedUser := vars["user"]
|
|
|
|
authorizedUser := r.Context().Value(models.UserKey).(*models.User)
|
|
|
|
|
|
|
|
if requestedUser != authorizedUser.ID && requestedUser != "current" {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
summary, err, status := h.loadUserSummary(authorizedUser)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(status)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-01 18:03:30 +03:00
|
|
|
vm := v1.NewAllTimeFrom(summary, models.NewFiltersWith(models.SummaryProject, values.Get("project")))
|
2020-09-06 13:15:46 +03:00
|
|
|
utils.RespondJSON(w, http.StatusOK, vm)
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:09:23 +03:00
|
|
|
func (h *AllTimeHandler) loadUserSummary(user *models.User) (*models.Summary, error, int) {
|
2020-09-06 13:15:46 +03:00
|
|
|
summaryParams := &models.SummaryParams{
|
|
|
|
From: time.Time{},
|
|
|
|
To: time.Now(),
|
|
|
|
User: user,
|
|
|
|
Recompute: false,
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:01:35 +03:00
|
|
|
var retrieveSummary services.SummaryRetriever = h.summarySrvc.Retrieve
|
|
|
|
if summaryParams.Recompute {
|
|
|
|
retrieveSummary = h.summarySrvc.Summarize
|
|
|
|
}
|
|
|
|
|
|
|
|
summary, err := h.summarySrvc.Aliased(summaryParams.From, summaryParams.To, summaryParams.User, retrieveSummary)
|
2020-09-06 13:15:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err, http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
|
|
|
|
return summary, nil, http.StatusOK
|
|
|
|
}
|