Monorepo for Tangled tangled.org

appview: allow users to set their preferences for the punchcard being displayed #1077

merged opened by willdot.net targeting master from willdot.net/tangled-fork: punchcard-prefs

This allows users to set their preference for the punchcard.

It allows them to either:

1: Hide their punchcard so that no one can see it 2: Hide all punchcards so that they don't see anyones punchcards

Labels

None yet.

assignee

None yet.

Participants 2
AT URI
at://did:plc:dadhhalkfcq3gucaq25hjqon/sh.tangled.repo.pull/3meygakiwru22
+59 -72
Interdiff #1 โ†’ #2
+1 -1
appview/db/db.go
··· 577 577 id integer primary key autoincrement, 578 578 user_did text not null unique, 579 579 hide_mine integer default 0, 580 - hide_all integer default 0 580 + hide_others integer default 0 581 581 ); 582 582 583 583 -- indexes for better performance
+7 -7
appview/db/preferences.go
··· 12 12 } 13 13 14 14 hideMine := 0 15 - hideAll := 0 15 + hideOthers := 0 16 16 17 17 err := e.QueryRow( 18 - `select id, hide_mine, hide_all from punchcard_preferences where user_did = ?`, 18 + `select id, hide_mine, hide_others from punchcard_preferences where user_did = ?`, 19 19 did, 20 - ).Scan(&preference.ID, &hideMine, &hideAll) 20 + ).Scan(&preference.ID, &hideMine, &hideOthers) 21 21 if err == sql.ErrNoRows { 22 22 return preference, nil 23 23 } 24 24 25 25 preference.HideMine = hideMine > 0 26 - preference.HideAll = hideAll > 0 26 + preference.HideOthers = hideOthers > 0 27 27 28 28 if err != nil { 29 29 return preference, err ··· 32 32 return preference, nil 33 33 } 34 34 35 - func UpsertPunchcardPreference(e Execer, did string, hideMine, hideAll bool) error { 35 + func UpsertPunchcardPreference(e Execer, did string, hideMine, hideOthers bool) error { 36 36 _, err := e.Exec( 37 37 `insert or replace into punchcard_preferences ( 38 38 user_did, 39 39 hide_mine, 40 - hide_all 40 + hide_others 41 41 ) 42 42 values (?, ?, ?)`, 43 43 did, 44 44 hideMine, 45 - hideAll, 45 + hideOthers, 46 46 ) 47 47 if err != nil { 48 48 return err
appview/db/profile.go

This file has not been changed.

+4 -22
appview/models/preferences.go
··· 1 1 package models 2 2 3 - const ( 4 - PunchcardPreferenceHideAll = "HIDE_ALL" 5 - PunchcardPreferenceHideMine = "HIDE_MINE" 6 - PunchcardPreferenceShowAll = "SHOW_ALL" 7 - ) 8 - 9 3 type PunchcardPreference struct { 10 - ID int 11 - Did string 12 - HideMine bool 13 - HideAll bool 14 - } 15 - 16 - func (p PunchcardPreference) GetPunchcardPrefences() string { 17 - if p.HideAll == true { 18 - return PunchcardPreferenceHideAll 19 - } 20 - 21 - if p.HideMine == true { 22 - return PunchcardPreferenceHideMine 23 - } 24 - 25 - return PunchcardPreferenceShowAll 4 + ID int 5 + Did string 6 + HideMine bool 7 + HideOthers bool 26 8 }
+1 -1
appview/pages/pages.go
··· 361 361 type UserProfileSettingsParams struct { 362 362 LoggedInUser *oauth.MultiAccountUser 363 363 Tab string 364 - PunchcardPreference string 364 + PunchcardPreference models.PunchcardPreference 365 365 } 366 366 367 367 func (p *Pages) UserProfileSettings(w io.Writer, params UserProfileSettingsParams) error {
appview/pages/templates/layouts/profilebase.html

This file has not been changed.

+8 -15
appview/pages/templates/user/settings/profile.html
··· 66 66 <span>Punchcard settings</span> 67 67 </div> 68 68 <form hx-post="/profile/punchcard" class="col-span-1 md:col-span-1 md:justify-self-end group flex gap-2 items-stretch"> 69 - <select 70 - id="punchcard-setting" 71 - name="punchcard-setting" 72 - required 73 - class="p-1 max-w-64 border border-gray-200 bg-white dark:bg-gray-800 dark:text-white dark:border-gray-700"> 74 - <option value="SHOW_ALL" class="py-1" {{ if eq "SHOW_ALL" $.PunchcardPreference }}selected{{ end }}> 75 - Show all 76 - </option> 77 - <option value="HIDE_MINE" class="py-1" {{ if eq "HIDE_MINE" $.PunchcardPreference }}selected{{ end }}> 78 - Hide mine for others 79 - </option> 80 - <option value="HIDE_ALL" class="py-1" {{ if eq "HIDE_ALL" $.PunchcardPreference }}selected{{ end }}> 81 - Hide everyones for me 82 - </option> 83 - </select> 69 + <div> 70 + <input type="checkbox" id="hideMine" name="hideMine" value="on" {{ if eq true $.PunchcardPreference.HideMine }}checked{{ end }}> 71 + <label for="includeBluesky" class="my-0 py-0 normal-case font-normal">Hide mine</label> 72 + </div> 73 + <div> 74 + <input type="checkbox" id="hideOthers" name="hideOthers" value="on" {{ if eq true $.PunchcardPreference.HideOthers }}checked{{ end }}> 75 + <label for="includeBluesky" class="my-0 py-0 normal-case font-normal">Hide others from me</label> 76 + </div> 84 77 <button class="btn flex gap-2 items-center" type="submit"> 85 78 {{ i "check" "size-4" }} 86 79 {{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }}
+1 -1
appview/settings/settings.go
··· 77 77 78 78 s.Pages.UserProfileSettings(w, pages.UserProfileSettingsParams{ 79 79 LoggedInUser: user, 80 - PunchcardPreference: punchcardPreferences.GetPunchcardPrefences(), 80 + PunchcardPreference: punchcardPreferences, 81 81 }) 82 82 } 83 83
+37 -25
appview/state/profile.go
··· 4 4 "context" 5 5 "fmt" 6 6 "log" 7 + "log/slog" 7 8 "net/http" 8 9 "slices" 9 10 "strings" ··· 166 167 167 168 loggedInUser := s.oauth.GetMultiAccountUser(r) 168 169 169 - targetPunchcardPreferences, err := db.GetPunchcardPreference(s.db, profile.UserDid) 170 - if err != nil { 171 - l.Error("failed to get target users punchcard preferences", "err", err) 172 - } 173 - 174 - requesterPunchcardPreferences, err := db.GetPunchcardPreference(s.db, loggedInUser.Did()) 175 - if err != nil { 176 - l.Error("failed to get requester users punchcard preferences", "err", err) 177 - } 178 - 179 - showPunchcard := true 180 - 181 - if targetPunchcardPreferences.HideMine || requesterPunchcardPreferences.HideAll { 182 - showPunchcard = false 183 - } 170 + showPunchcard := checkIfPunchcardShouldShow(profile.UserDid, loggedInUser.Did(), s.db, l) 184 171 185 172 timeline, err := db.MakeProfileTimeline(s.db, profile.UserDid, showPunchcard) 186 173 if err != nil { ··· 197 184 }) 198 185 } 199 186 187 + func checkIfPunchcardShouldShow(targetDid, requesterDid string, e db.Execer, l *slog.Logger) bool { 188 + targetPunchcardPreferences, err := db.GetPunchcardPreference(e, targetDid) 189 + if err != nil { 190 + l.Error("failed to get target users punchcard preferences", "err", err) 191 + return true 192 + } 193 + 194 + requesterPunchcardPreferences, err := db.GetPunchcardPreference(e, requesterDid) 195 + if err != nil { 196 + l.Error("failed to get requester users punchcard preferences", "err", err) 197 + return true 198 + } 199 + 200 + showPunchcard := true 201 + 202 + // looking at their own profile 203 + if targetDid == requesterDid { 204 + if targetPunchcardPreferences.HideMine { 205 + return false 206 + } 207 + return true 208 + } 209 + 210 + if targetPunchcardPreferences.HideMine || requesterPunchcardPreferences.HideOthers { 211 + showPunchcard = false 212 + } 213 + return showPunchcard 214 + } 215 + 200 216 func (s *State) reposPage(w http.ResponseWriter, r *http.Request) { 201 217 l := s.logger.With("handler", "reposPage") 202 218 ··· 964 980 } 965 981 user := s.oauth.GetUser(r) 966 982 967 - punchcard := r.Form.Get("punchcard-setting") 968 - 969 - hideAll := false 983 + hideOthers := false 970 984 hideMine := false 971 985 972 - switch punchcard { 973 - case models.PunchcardPreferenceHideAll: 974 - hideAll = true 975 - hideMine = false 976 - case models.PunchcardPreferenceHideMine: 986 + if r.Form.Get("hideMine") == "on" { 977 987 hideMine = true 978 - hideAll = false 988 + } 989 + if r.Form.Get("hideOthers") == "on" { 990 + hideOthers = true 979 991 } 980 992 981 - err = db.UpsertPunchcardPreference(s.db, user.Did, hideMine, hideAll) 993 + err = db.UpsertPunchcardPreference(s.db, user.Did, hideMine, hideOthers) 982 994 if err != nil { 983 995 log.Println("failed to update punchcard preferences", err) 984 996 return
appview/state/router.go

This file has not been changed.

History

5 rounds 3 comments
sign up or login to add to the discussion
1 commit
expand
appview: allow users to set their preferences for the punchcard being displayed
expand 1 comment

thanks for the work on this! this looks good to me in the current state.

pull request successfully merged
1 commit
expand
appview: allow users to set their preferences for the punchcard being displayed
expand 0 comments
1 commit
expand
appview: allow users to set their preferences for the punchcard being displayed
expand 0 comments
1 commit
expand
appview: allow users to set their preferences for the punchcard being displayed
expand 0 comments
1 commit
expand
appview: allow users to set their preferences for the punchcard being displayed
expand 2 comments
  • here: there is no bool type in sqlite! so we will have to use ints here. i tested this locally and it seems that is valid sql and executes fine, but it seems any value can be stored in that column.
  • here: nice, this is super neat

all in all, rest of the code lgtm!

Oh wow, TIL that there isn't a bool type in sqlite ๐Ÿซฃ

I've fixed that so that the field is an integer type with a default of 0 (false) and then added the relevant mapping into the query code to map 0 is false and > 0 is true.

Tested by stuffing random integers manually into the table and appears to work as expected.