Skip to content

Commit

Permalink
fix: readme card default custom title (resolve #715)
Browse files Browse the repository at this point in the history
chore: add option to configure activity chart sharing separately
  • Loading branch information
muety committed Dec 20, 2024
1 parent 946985e commit 5fa2515
Show file tree
Hide file tree
Showing 9 changed files with 6,502 additions and 6,428 deletions.
12,838 changes: 6,423 additions & 6,415 deletions coverage/coverage.out

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions migrations/20241220_share_activity_chart_flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package migrations

import (
"github.com/muety/wakapi/config"
"github.com/muety/wakapi/models"
"gorm.io/gorm"
)

func init() {
const name = "20241220-share_activity_chart_flag"
f := migrationFunc{
name: name,
f: func(db *gorm.DB, cfg *config.Config) error {
if hasRun(name, db) {
return nil
}

db.
Model(&models.User{}).
Where("share_data_max_days < ?", 0).
Or("share_data_max_days >= ?", 365).
Update("share_activity_chart", true)

setHasRun(name, db)
return nil
},
}

registerPostMigration(f)
}
1 change: 1 addition & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type User struct {
ShareOSs bool `json:"-" gorm:"default:false; type:bool; column:share_oss"`
ShareMachines bool `json:"-" gorm:"default:false; type:bool"`
ShareLabels bool `json:"-" gorm:"default:false; type:bool"`
ShareActivityChart bool `json:"-" gorm:"default:false; type:bool"`
IsAdmin bool `json:"-" gorm:"default:false; type:bool"`
HasData bool `json:"-" gorm:"default:false; type:bool"`
WakatimeApiKey string `json:"-"` // for relay middleware and imports
Expand Down
19 changes: 10 additions & 9 deletions models/view/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (

type SettingsViewModel struct {
SharedLoggedInViewModel
LanguageMappings []*models.LanguageMapping
Aliases []*SettingsVMCombinedAlias
Labels []*SettingsVMCombinedLabel
Projects []string
SubscriptionPrice string
DataRetentionMonths int
UserFirstData time.Time
SupportContact string
InviteLink string
LanguageMappings []*models.LanguageMapping
Aliases []*SettingsVMCombinedAlias
Labels []*SettingsVMCombinedLabel
Projects []string
SubscriptionPrice string
DataRetentionMonths int
UserFirstData time.Time
SupportContact string
InviteLink string
ReadmeCardCustomTitle string
}

type SettingsVMCombinedAlias struct {
Expand Down
1 change: 1 addition & 0 deletions repositories/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func (r *UserRepository) Update(user *models.User) (*models.User, error) {
"share_projects": user.ShareProjects,
"share_machines": user.ShareMachines,
"share_labels": user.ShareLabels,
"share_activity_chart": user.ShareActivityChart,
"wakatime_api_key": user.WakatimeApiKey,
"wakatime_api_url": user.WakatimeApiUrl,
"has_data": user.HasData,
Expand Down
3 changes: 1 addition & 2 deletions routes/api/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
conf "github.com/muety/wakapi/config"
"github.com/muety/wakapi/helpers"
"github.com/muety/wakapi/middlewares"
"github.com/muety/wakapi/models"
"github.com/muety/wakapi/services"
Expand Down Expand Up @@ -65,7 +64,7 @@ func (h *ActivityApiHandler) GetActivityChart(w http.ResponseWriter, r *http.Req
}

if authorizedUser == nil || authorizedUser.ID != requestedUser.ID {
if _, userRange := helpers.ResolveMaximumRange(requestedUser.ShareDataMaxDays); userRange != models.IntervalPast12Months && userRange != models.IntervalAny { // TODO: build "hierarchy" of intervals to easily check if one is contained in another
if !requestedUser.ShareActivityChart {
w.WriteHeader(http.StatusForbidden)
return
}
Expand Down
11 changes: 11 additions & 0 deletions routes/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/duke-git/lancet/v2/condition"
"github.com/go-chi/chi/v5"
"github.com/gofrs/uuid/v5"
"github.com/muety/wakapi/helpers"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -375,6 +377,7 @@ func (h *SettingsHandler) actionUpdateSharing(w http.ResponseWriter, r *http.Req
user.ShareOSs, err = strconv.ParseBool(r.PostFormValue("share_oss"))
user.ShareMachines, err = strconv.ParseBool(r.PostFormValue("share_machines"))
user.ShareLabels, err = strconv.ParseBool(r.PostFormValue("share_labels"))
user.ShareActivityChart, err = strconv.ParseBool(r.PostFormValue("share_activity_chart"))
user.ShareDataMaxDays, err = strconv.Atoi(r.PostFormValue("max_days"))

if err != nil {
Expand Down Expand Up @@ -935,6 +938,14 @@ func (h *SettingsHandler) buildViewModel(r *http.Request, w http.ResponseWriter,
DataRetentionMonths: h.config.App.DataRetentionMonths,
InviteLink: inviteLink,
}

// readme card params
readmeCardTitle := "Wakapi.dev Stats"
if err, maxRange := helpers.ResolveMaximumRange(user.ShareDataMaxDays); err == nil {
readmeCardTitle += fmt.Sprintf(" (%v)", maxRange.GetHumanReadable())
}
vm.ReadmeCardCustomTitle = url.QueryEscape(readmeCardTitle)

return routeutils.WithSessionMessages(vm, r, w)
}

Expand Down
6 changes: 6 additions & 0 deletions utils/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ var userAgents = []struct {
"cursor",
nil,
},
{
"wakatime/v1.106.1 (linux-5.15.167.4-microsoft-standard-WSL2-unknown) go1.23.3 cursor/1.93.1 vscode-wakatime/24.9.2", // https://github.com/muety/wakapi/issues/718
"Linux",
"cursor",
nil,
},
}

func BenchmarkCommon_ParseUserAgent(b *testing.B) {
Expand Down
21 changes: 19 additions & 2 deletions views/settings.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,20 @@ <h3 class="inline-block font-semibold text-gray-300">Add Rule</h3>
</select>
</div>
</div>

<div class="flex gap-x-8">
<div class="grow">
<label class="font-semibold text-gray-300" for="share_labels">Share Activity Chart</label>
</div>
<div>
<select autocomplete="off" id="share_activity_chart" name="share_activity_chart" class="select-default grow">
<option value="false" class="cursor-pointer" {{ if not .User.ShareActivityChart }} selected {{ end }}>No
</option>
<option value="true" class="cursor-pointer" {{ if .User.ShareActivityChart }} selected {{ end }}>Yes
</option>
</select>
</div>
</div>
</div>
</div>

Expand Down Expand Up @@ -776,20 +790,23 @@ <h3 class="inline-block font-semibold text-gray-300">Add Rule</h3>
<label class="font-semibold text-gray-300 text-lg" for="select-timezone">GitHub Readme Stats</label>
<span class="block text-sm text-gray-600">
Wakapi intregrates with <a class="link" href="https://github.com/anuraghazra/github-readme-stats#wakatime-week-stats" target="_blank" rel="noreferrer noopener">GitHub Readme Stats</a> to generate fancy cards for you. To enable this feature, you need to grant public, unauthorized access to the respective endpoints. See <a class="link" href="settings#permissions">Permissions</a>.<br><br>
Share data for >= 7 days → weekly summary card<br>
Share data for >= 366 days → yearly summary card<br>
Share all data (-1) → all-time summary card<br><br>
Only available on public instances, not on localhost.
</span>
</div>

<div class="w-full md:w-1/2">
{{ if ne .User.ShareDataMaxDays 0 }}
<div class="flex items-center mb-2">
<img src="https://github-readme-stats.vercel.app/api/wakatime?username={{ .User.ID }}&api_domain=%s&bg_color=1A202C&title_color=2F855A&icon_color=2F855A&text_color=ffffff&custom_title=Wakapi%20Week%20Stats&layout=compact"
<img src="https://github-readme-stats.vercel.app/api/wakatime?username={{ .User.ID }}&api_domain=%s&bg_color=1A202C&title_color=2F855A&icon_color=2F855A&text_color=ffffff&custom_title={{ .ReadmeCardCustomTitle }}&layout=compact"
class="with-url-src-no-scheme" alt="Readme Stats Card">
</div>
<textarea
class="with-url-inner-no-scheme shrink w-full font-mono text-xs appearance-none bg-gray-850 text-gray-500 outline-none rounded py-2 px-4 cursor-not-allowed mt-2"
rows="5" style="resize: none"
readonly>https://github-readme-stats.vercel.app/api/wakatime?username={{ .User.ID }}&api_domain=%s&bg_color=1A202C&title_color=2F855A&icon_color=2F855A&text_color=ffffff&custom_title=Wakapi%20Week%20Stats&layout=compact</textarea>
readonly>https://github-readme-stats.vercel.app/api/wakatime?username={{ .User.ID }}&api_domain=%s&bg_color=1A202C&title_color=2F855A&icon_color=2F855A&text_color=ffffff&custom_title={{ .ReadmeCardCustomTitle }}&layout=compact</textarea>
{{ end }}
</div>
</div>
Expand Down

0 comments on commit 5fa2515

Please sign in to comment.