2020-09-12 17:09:23 +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-12 17:09:23 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
v1 "github.com/muety/wakapi/models/compat/shields/v1"
|
|
|
|
"github.com/muety/wakapi/services"
|
|
|
|
"github.com/muety/wakapi/utils"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
2020-09-12 17:58:19 +03:00
|
|
|
"strings"
|
2020-09-12 17:09:23 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
intervalPattern = `interval:([a-z0-9_]+)`
|
|
|
|
entityFilterPattern = `(project|os|editor|language|machine):([_a-zA-Z0-9-]+)`
|
|
|
|
)
|
|
|
|
|
|
|
|
type BadgeHandler struct {
|
2020-11-08 12:12:49 +03:00
|
|
|
config *conf.Config
|
|
|
|
userSrvc services.IUserService
|
|
|
|
summarySrvc services.ISummaryService
|
2020-09-12 17:09:23 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 12:12:49 +03:00
|
|
|
func NewBadgeHandler(summaryService services.ISummaryService, userService services.IUserService) *BadgeHandler {
|
2020-09-12 17:09:23 +03:00
|
|
|
return &BadgeHandler{
|
|
|
|
summarySrvc: summaryService,
|
|
|
|
userSrvc: userService,
|
2020-11-08 12:12:49 +03:00
|
|
|
config: conf.Get(),
|
2020-09-12 17:09:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 23:28:02 +03:00
|
|
|
func (h *BadgeHandler) RegisterRoutes(router *mux.Router) {
|
|
|
|
r := router.PathPrefix("/shields/v1/{user}").Subrouter()
|
|
|
|
r.Methods(http.MethodGet).HandlerFunc(h.Get)
|
2021-01-30 12:34:52 +03:00
|
|
|
}
|
|
|
|
|
2021-02-03 23:28:02 +03:00
|
|
|
func (h *BadgeHandler) Get(w http.ResponseWriter, r *http.Request) {
|
2020-09-12 17:09:23 +03:00
|
|
|
intervalReg := regexp.MustCompile(intervalPattern)
|
|
|
|
entityFilterReg := regexp.MustCompile(entityFilterPattern)
|
|
|
|
|
2020-09-12 17:58:19 +03:00
|
|
|
if userAgent := r.Header.Get("user-agent"); !strings.HasPrefix(userAgent, "Shields.io/") && !h.config.IsDev() {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:09:23 +03:00
|
|
|
requestedUserId := mux.Vars(r)["user"]
|
|
|
|
user, err := h.userSrvc.GetUserById(requestedUserId)
|
|
|
|
if err != nil || !user.BadgesEnabled {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var filterEntity, filterKey string
|
|
|
|
if groups := entityFilterReg.FindStringSubmatch(r.URL.Path); len(groups) > 2 {
|
|
|
|
filterEntity, filterKey = groups[1], groups[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
var interval = models.IntervalPast30Days
|
|
|
|
if groups := intervalReg.FindStringSubmatch(r.URL.Path); len(groups) > 1 {
|
|
|
|
interval = groups[1]
|
|
|
|
}
|
|
|
|
|
2020-11-01 14:50:59 +03:00
|
|
|
var filters *models.Filters
|
2020-09-12 17:09:23 +03:00
|
|
|
switch filterEntity {
|
|
|
|
case "project":
|
2020-11-01 14:50:59 +03:00
|
|
|
filters = models.NewFiltersWith(models.SummaryProject, filterKey)
|
2020-09-12 17:09:23 +03:00
|
|
|
case "os":
|
2020-11-01 14:50:59 +03:00
|
|
|
filters = models.NewFiltersWith(models.SummaryOS, filterKey)
|
2020-09-12 17:09:23 +03:00
|
|
|
case "editor":
|
2020-11-01 14:50:59 +03:00
|
|
|
filters = models.NewFiltersWith(models.SummaryEditor, filterKey)
|
2020-09-12 17:09:23 +03:00
|
|
|
case "language":
|
2020-11-01 14:50:59 +03:00
|
|
|
filters = models.NewFiltersWith(models.SummaryLanguage, filterKey)
|
2020-09-12 17:09:23 +03:00
|
|
|
case "machine":
|
2020-11-01 14:50:59 +03:00
|
|
|
filters = models.NewFiltersWith(models.SummaryMachine, filterKey)
|
2020-11-01 18:03:30 +03:00
|
|
|
default:
|
|
|
|
filters = &models.Filters{}
|
2020-09-12 17:09:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
summary, err, status := h.loadUserSummary(user, interval)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(status)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vm := v1.NewBadgeDataFrom(summary, filters)
|
|
|
|
utils.RespondJSON(w, http.StatusOK, vm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *BadgeHandler) loadUserSummary(user *models.User, interval string) (*models.Summary, error, int) {
|
|
|
|
err, from, to := utils.ResolveInterval(interval)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err, http.StatusBadRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
summaryParams := &models.SummaryParams{
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
User: user,
|
|
|
|
}
|
|
|
|
|
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-12 17:09:23 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err, http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
|
|
|
|
return summary, nil, http.StatusOK
|
|
|
|
}
|