2021-04-30 11:12:28 +03:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2022-12-01 12:57:07 +03:00
|
|
|
"github.com/muety/wakapi/helpers"
|
2022-01-02 03:22:58 +03:00
|
|
|
"net/http"
|
|
|
|
|
2021-04-30 11:12:28 +03:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
conf "github.com/muety/wakapi/config"
|
|
|
|
"github.com/muety/wakapi/middlewares"
|
|
|
|
v1 "github.com/muety/wakapi/models/compat/wakatime/v1"
|
2021-05-19 11:18:18 +03:00
|
|
|
routeutils "github.com/muety/wakapi/routes/utils"
|
2021-04-30 11:12:28 +03:00
|
|
|
"github.com/muety/wakapi/services"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UsersHandler struct {
|
|
|
|
config *conf.Config
|
|
|
|
userSrvc services.IUserService
|
|
|
|
heartbeatSrvc services.IHeartbeatService
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUsersHandler(userService services.IUserService, heartbeatService services.IHeartbeatService) *UsersHandler {
|
|
|
|
return &UsersHandler{
|
|
|
|
userSrvc: userService,
|
|
|
|
heartbeatSrvc: heartbeatService,
|
|
|
|
config: conf.Get(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *UsersHandler) RegisterRoutes(router *mux.Router) {
|
|
|
|
r := router.PathPrefix("/compat/wakatime/v1/users/{user}").Subrouter()
|
|
|
|
r.Use(
|
|
|
|
middlewares.NewAuthenticateMiddleware(h.userSrvc).Handler,
|
|
|
|
)
|
2021-04-30 19:08:53 +03:00
|
|
|
r.Path("").Methods(http.MethodGet).HandlerFunc(h.Get)
|
2021-04-30 11:12:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Retrieve the given user
|
|
|
|
// @Description Mimics https://wakatime.com/developers#users
|
|
|
|
// @ID get-wakatime-user
|
|
|
|
// @Tags wakatime
|
|
|
|
// @Produce json
|
|
|
|
// @Param user path string true "User ID to fetch (or 'current')"
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Success 200 {object} v1.UserViewModel
|
2022-01-02 03:22:58 +03:00
|
|
|
// @Router /compat/wakatime/v1/users/{user} [get]
|
2021-04-30 11:12:28 +03:00
|
|
|
func (h *UsersHandler) Get(w http.ResponseWriter, r *http.Request) {
|
2021-05-19 11:18:18 +03:00
|
|
|
wakapiUser, err := routeutils.CheckEffectiveUser(w, r, h.userSrvc, "current")
|
|
|
|
if err != nil {
|
|
|
|
return // response was already sent by util function
|
2021-04-30 11:12:28 +03:00
|
|
|
}
|
|
|
|
|
2021-05-19 11:18:18 +03:00
|
|
|
user := v1.NewFromUser(wakapiUser)
|
|
|
|
if hb, err := h.heartbeatSrvc.GetLatestByUser(wakapiUser); err == nil {
|
2021-04-30 11:12:28 +03:00
|
|
|
user = user.WithLatestHeartbeat(hb)
|
|
|
|
} else {
|
|
|
|
conf.Log().Request(r).Error("%v", err)
|
|
|
|
}
|
|
|
|
|
2022-12-01 12:57:07 +03:00
|
|
|
helpers.RespondJSON(w, r, http.StatusOK, v1.UserViewModel{Data: user})
|
2021-04-30 11:12:28 +03:00
|
|
|
}
|