Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
ouuan committed Feb 2, 2024
2 parents 89cd501 + 0c17a95 commit 6ea7fd5
Show file tree
Hide file tree
Showing 6 changed files with 896 additions and 842 deletions.
1,673 changes: 837 additions & 836 deletions coverage/coverage.out

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion etc/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ wakapi.yourdomain.tld {

log {
output file /var/log/caddy/wakapi.dev.access.log
format single_field common_log
}

reverse_proxy http://[::1]:3000
Expand Down
41 changes: 40 additions & 1 deletion repositories/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,48 @@ func (r *SummaryRepository) GetAll() ([]*models.Summary, error) {
}

func (r *SummaryRepository) Insert(summary *models.Summary) error {
if err := r.db.Create(summary).Error; err != nil {

if err := r.db.Transaction(func(tx *gorm.DB) error {
if err := tx.Create(summary).Error; err != nil {
return err
}

itemsToCreate := []*models.SummaryItem{}

// required due to setting gorm:"-" in the model definition
// see https://github.com/muety/wakapi/issues/600#issuecomment-1921723789
// see https://github.com/muety/wakapi/pull/592#discussion_r1450478355
for _, item := range summary.Machines {
item.SummaryID = summary.ID
itemsToCreate = append(itemsToCreate, item)
}

for _, item := range summary.Languages {
item.SummaryID = summary.ID
itemsToCreate = append(itemsToCreate, item)
}

for _, item := range summary.OperatingSystems {
item.SummaryID = summary.ID
itemsToCreate = append(itemsToCreate, item)
}

for _, item := range summary.Editors {
item.SummaryID = summary.ID
itemsToCreate = append(itemsToCreate, item)
}

if len(itemsToCreate) > 0 {
if err := tx.Create(itemsToCreate).Error; err != nil {
return err
}
}

return nil
}); err != nil {
return err
}

return nil
}

Expand Down
9 changes: 8 additions & 1 deletion routes/api/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import (
"github.com/muety/wakapi/services"
"github.com/muety/wakapi/utils"
"net/http"
"regexp"
"strings"
"time"
)

var userWithExtPattern *regexp.Regexp

func init() {
userWithExtPattern = regexp.MustCompile(`\.svg$`)
}

type ActivityApiHandler struct {
config *conf.Config
userService services.IUserService
Expand Down Expand Up @@ -51,7 +58,7 @@ func (h *ActivityApiHandler) GetActivityChart(w http.ResponseWriter, r *http.Req
w.Write([]byte(conf.ErrNotFound))
return
}
requestedUser, err := h.userService.GetUserById(strings.TrimRight(userWithExt, ".svg"))
requestedUser, err := h.userService.GetUserById(userWithExtPattern.ReplaceAllString(userWithExt, ""))
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
Expand Down
12 changes: 10 additions & 2 deletions services/imports/wakatime_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (w *WakatimeDumpImporter) Import(user *models.User, minFrom time.Time, maxT

url := config.WakatimeApiUrl + config.WakatimeApiDataDumpUrl // this importer only works with wakatime currently, so no point in using user's custom wakatime api url
req, _ := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(`{ "type": "heartbeats", "email_when_finished": false }`)))
res, err := utils.RaiseForStatus(w.httpClient.Do(w.withHeaders(req)))
res, err := utils.RaiseForStatus((&http.Client{Timeout: 10 * time.Second}).Do(w.withHeaders(req)))

if err != nil && res != nil && res.StatusCode == http.StatusBadRequest {
var datadumpError wakatime.DataDumpResultErrorModel
Expand All @@ -63,7 +63,7 @@ func (w *WakatimeDumpImporter) Import(user *models.User, minFrom time.Time, maxT
// callbacks
checkDumpAvailable := func(user *models.User) (bool, *wakatime.DataDumpData, error) {
req, _ := http.NewRequest(http.MethodGet, url, nil)
res, err := utils.RaiseForStatus(w.httpClient.Do(w.withHeaders(req)))
res, err := utils.RaiseForStatus((&http.Client{Timeout: 10 * time.Second}).Do(w.withHeaders(req)))
if err != nil {
return false, nil, err
}
Expand Down Expand Up @@ -156,5 +156,13 @@ func (w *WakatimeDumpImporter) withHeaders(req *http.Request) *http.Request {
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(w.apiKey))))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")

// workaround for https://github.com/muety/wakapi/issues/602
// super weird behavior:
// when keeping req.Close set to false (keep connection alive), we'll get an "unexpected EOF" error inside checkDumpAvailable(),
// even though i can neither reproduce this error with curl, nor in a minimal, stand-alone go example (https://go.dev/play/p/HY_RLtTWnkk works totally fine)
// even weirder: even when creating a whole new http.Client for every request, the issue keeps occuring
// this used to be working in the past and suddenly broke at some point (change on wakatime's end?)
req.Close = true
return req
}
2 changes: 1 addition & 1 deletion services/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (srv *MiscService) NotifyExpiringSubscription() {
var subscriptionReminders map[string][]*models.KeyStringValue
if result, err := srv.keyValueService.GetByPrefix(config.KeySubscriptionNotificationSent); err == nil {
subscriptionReminders = slice.GroupWith[*models.KeyStringValue, string](result, func(kv *models.KeyStringValue) string {
return strings.TrimPrefix(kv.Key, config.KeySubscriptionNotificationSent+"_")
return strings.Replace(kv.Key, config.KeySubscriptionNotificationSent+"_", "", 1)
})
} else {
config.Log().Error("failed to fetch key-values for subscription notifications, %v", err)
Expand Down

0 comments on commit 6ea7fd5

Please sign in to comment.