func (us UserService) SyncHeartbeats(currentUser models.User, r render.Render, req *http.Request) { currentUser.IsSyncingWakaTime = true utils.ORM.Save(¤tUser) go currentUser.PullNewestHeartbeats() if req.Referer() != "" { r.Redirect(req.Referer()) } else { r.Redirect("/dashboard") } }
func (g GithubIntegration) HandleWebhookEvent(payload []byte) { var webhookPayload github.WebHookPayload payload = clearJSONRepoOrgField(payload) err := json.Unmarshal(payload, &webhookPayload) if err != nil { utils.Log.Error("Failed to unmarshal github WebHook payload. %v", err) return } repo := *webhookPayload.Repo.FullName var project models.Project err = utils.ORM.First(&project, "git_repo_name = ?", repo).Error if err != nil { utils.Log.Error("Failed to get assocaited chroniton project for push event: %v", err) return } if project.GitIntegrationName != "github" { utils.Log.Error("Project %v git integration (%v) doesn't match current one (github", project.Name, project.GitIntegrationName) return } utils.ORM.Exec("update projects set num_recieved_webhooks=num_recieved_webhooks+1 where id = ?", project.Id) var user models.User err = utils.ORM.First(&user, project.UserId).Error if err != nil { utils.Log.Error("Couldn't get associated user for project %v. %v", project.Name, err) return } user.PullNewestHeartbeats() webhookEvent := models.WebhookEvent{ ProjectId: project.Id, UserId: user.Id, Payload: string(payload), } err = utils.ORM.Save(&webhookEvent).Error if err != nil { utils.Log.Error("Failed to create a record for webhook event") return } g.computeTimeWorked(webhookPayload, project, user) }
func (us UserService) Update(user models.User, req *http.Request, r render.Render) { var oldUser models.User err := utils.ORM.First(&oldUser, user.Id).Error if err != nil { r.HTML(500, "user/edit", nil) return } err = req.ParseForm() if err != nil { r.Error(5000) } structVal := structs.Map(user) changes := utils.GetDiffFormValues(req.PostForm, structVal) utils.Log.Debug("changed: %v", changes) err = utils.ORM.Table("users").Updates(changes).Error if err != nil { r.HTML(403, "user/edit", nil) return } var curUser models.User utils.ORM.First(&curUser, user.Id) if oldUser.WakaTimeApiKey != curUser.WakaTimeApiKey && curUser.WakaTimeApiKey != "" { utils.Log.Debug("Pulling down wakatime heartbeats") curUser.IsSyncingWakaTime = true utils.ORM.Save(&curUser) go curUser.PullNewestHeartbeats() } if strings.Contains(req.Referer(), "/settings") { r.Redirect(req.Referer()) } else { r.Redirect("/settings") } }