diff --git a/appview/db/db.go b/appview/db/db.go index caffad15..f7d30325 100644 --- a/appview/db/db.go +++ b/appview/db/db.go @@ -601,6 +601,13 @@ func Make(ctx context.Context, dbPath string) (*DB, error) { name text unique ); + create table if not exists punchcard_preferences ( + id integer primary key autoincrement, + user_did text not null unique, + hide_mine integer default 0, + hide_others integer default 0 + ); + -- indexes for better performance create index if not exists idx_notifications_recipient_created on notifications(recipient_did, created desc); create index if not exists idx_notifications_recipient_read on notifications(recipient_did, read); diff --git a/appview/db/preferences.go b/appview/db/preferences.go new file mode 100644 index 00000000..e1bca2af --- /dev/null +++ b/appview/db/preferences.go @@ -0,0 +1,52 @@ +package db + +import ( + "database/sql" + + "tangled.org/core/appview/models" +) + +func GetPunchcardPreference(e Execer, did string) (models.PunchcardPreference, error) { + preference := models.PunchcardPreference{ + Did: did, + } + + hideMine := 0 + hideOthers := 0 + + err := e.QueryRow( + `select id, hide_mine, hide_others from punchcard_preferences where user_did = ?`, + did, + ).Scan(&preference.ID, &hideMine, &hideOthers) + if err == sql.ErrNoRows { + return preference, nil + } + + preference.HideMine = hideMine > 0 + preference.HideOthers = hideOthers > 0 + + if err != nil { + return preference, err + } + + return preference, nil +} + +func UpsertPunchcardPreference(e Execer, did string, hideMine, hideOthers bool) error { + _, err := e.Exec( + `insert or replace into punchcard_preferences ( + user_did, + hide_mine, + hide_others + ) + values (?, ?, ?)`, + did, + hideMine, + hideOthers, + ) + if err != nil { + return err + } + + return nil +} diff --git a/appview/db/profile.go b/appview/db/profile.go index fdc5d993..25569af2 100644 --- a/appview/db/profile.go +++ b/appview/db/profile.go @@ -16,7 +16,7 @@ import ( const TimeframeMonths = 7 -func MakeProfileTimeline(e Execer, forDid string) (*models.ProfileTimeline, error) { +func MakeProfileTimeline(e Execer, forDid string, includePunchcard bool) (*models.ProfileTimeline, error) { timeline := models.ProfileTimeline{ ByMonth: make([]models.ByMonth, TimeframeMonths), } @@ -98,27 +98,29 @@ func MakeProfileTimeline(e Execer, forDid string) (*models.ProfileTimeline, erro }) } - punchcard, err := MakePunchcard( - e, - orm.FilterEq("did", forDid), - orm.FilterGte("date", time.Now().AddDate(0, -TimeframeMonths, 0)), - ) - if err != nil { - return nil, fmt.Errorf("error getting commits by did: %w", err) - } - for _, punch := range punchcard.Punches { - if punch.Date.After(now) { - continue + if includePunchcard { + punchcard, err := MakePunchcard( + e, + orm.FilterEq("did", forDid), + orm.FilterGte("date", time.Now().AddDate(0, -TimeframeMonths, 0)), + ) + if err != nil { + return nil, fmt.Errorf("error getting commits by did: %w", err) } + for _, punch := range punchcard.Punches { + if punch.Date.After(now) { + continue + } - monthsAgo := monthsBetween(punch.Date, now) - if monthsAgo >= TimeframeMonths { - // shouldn't happen; but times are weird - continue - } + monthsAgo := monthsBetween(punch.Date, now) + if monthsAgo >= TimeframeMonths { + // shouldn't happen; but times are weird + continue + } - idx := monthsAgo - timeline.ByMonth[idx].Commits += punch.Count + idx := monthsAgo + timeline.ByMonth[idx].Commits += punch.Count + } } return &timeline, nil diff --git a/appview/models/preferences.go b/appview/models/preferences.go new file mode 100644 index 00000000..850621b1 --- /dev/null +++ b/appview/models/preferences.go @@ -0,0 +1,8 @@ +package models + +type PunchcardPreference struct { + ID int + Did string + HideMine bool + HideOthers bool +} diff --git a/appview/pages/pages.go b/appview/pages/pages.go index 4b77b34c..f95d0211 100644 --- a/appview/pages/pages.go +++ b/appview/pages/pages.go @@ -359,8 +359,9 @@ func (p *Pages) GoodFirstIssues(w io.Writer, params GoodFirstIssuesParams) error } type UserProfileSettingsParams struct { - LoggedInUser *oauth.MultiAccountUser - Tab string + LoggedInUser *oauth.MultiAccountUser + Tab string + PunchcardPreference models.PunchcardPreference } func (p *Pages) UserProfileSettings(w io.Writer, params UserProfileSettingsParams) error { @@ -557,6 +558,7 @@ type ProfileOverviewParams struct { ProfileTimeline *models.ProfileTimeline Card *ProfileCard Active string + ShowPunchcard bool } func (p *Pages) ProfileOverview(w io.Writer, params ProfileOverviewParams) error { diff --git a/appview/pages/templates/layouts/profilebase.html b/appview/pages/templates/layouts/profilebase.html index 30ee9622..349edddd 100644 --- a/appview/pages/templates/layouts/profilebase.html +++ b/appview/pages/templates/layouts/profilebase.html @@ -52,7 +52,9 @@