diff options
author | Arthur Zamarin <arthurzam@gentoo.org> | 2024-07-08 21:57:14 +0300 |
---|---|---|
committer | Arthur Zamarin <arthurzam@gentoo.org> | 2024-07-08 21:58:03 +0300 |
commit | 307e267aea9f6ed8309c39a97dcb0d7eb115b8cd (patch) | |
tree | a05e1cc2619880141d46e90fb23bc82d1338f3d1 /pkg | |
parent | repology: verify all outdated entries are indeed outdated (diff) | |
download | soko-307e267aea9f6ed8309c39a97dcb0d7eb115b8cd.tar.gz soko-307e267aea9f6ed8309c39a97dcb0d7eb115b8cd.tar.bz2 soko-307e267aea9f6ed8309c39a97dcb0d7eb115b8cd.zip |
app/preferences: remove general
Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/app/handler/index/index.templ | 31 | ||||
-rw-r--r-- | pkg/app/handler/index/utils.go | 50 | ||||
-rw-r--r-- | pkg/app/handler/packages/show.go | 45 | ||||
-rw-r--r-- | pkg/app/handler/user/general.templ | 91 | ||||
-rw-r--r-- | pkg/app/handler/user/preferences.templ | 30 | ||||
-rw-r--r-- | pkg/app/serve.go | 5 | ||||
-rw-r--r-- | pkg/app/utils/userdata.go | 14 | ||||
-rw-r--r-- | pkg/models/userpreferences.go | 16 |
8 files changed, 9 insertions, 273 deletions
diff --git a/pkg/app/handler/index/index.templ b/pkg/app/handler/index/index.templ index 98101cb..3da465b 100644 --- a/pkg/app/handler/index/index.templ +++ b/pkg/app/handler/index/index.templ @@ -43,15 +43,11 @@ templ header(packageCount int) { </div> } -templ packagesList(addedPackages []packageInfo, landingPage string) { +templ packagesList(addedPackages []packageInfo) { <div class="col-12"> <h3 class="mb-2"> <span class="fa fa-fw fa-history"></span> - if landingPage == "classic" { - <a class="text-dark" href="/packages/added">Added Packages</a> - } else { - <a class="text-dark">Search History</a> - } + <a class="text-dark" href="/packages/added">Added Packages</a> </h3> <div class="card border-top-0 mb-4"> <div class="table-responsive rounded"> @@ -67,9 +63,6 @@ templ packagesList(addedPackages []packageInfo, landingPage string) { <td>{ pkg.Description }</td> </tr> } - if landingPage == "full" && len(addedPackages) == 0 { - <tr><td class="text-center"><i>Visited packages will be displayed here in future</i></td></tr> - } </tbody> </table> </div> @@ -77,18 +70,18 @@ templ packagesList(addedPackages []packageInfo, landingPage string) { </div> } -templ page(packageCount int, addedPackages []packageInfo, updatedVersions []*models.Version, userPreferences models.UserPreferences) { +templ page(packageCount int) { <div class="container mb-5"> <div class="row"> @header(packageCount) - @packagesList(addedPackages, userPreferences.General.LandingPageLayout) + @packagesList(getAddedPackages(10)) <div class="col-12"> <h3 class="pt-3 mb-2"> <span class="fa fa-fw fa-asterisk"></span> <a class="text-dark" href="/packages/updated">Updated Packages</a> </h3> <ul class="list-group"> - @utils.ChangedVersionsTable(updatedVersions) + @utils.ChangedVersionsTable(getUpdatedVersions(10)) </ul> </div> </div> @@ -99,17 +92,5 @@ templ page(packageCount int, addedPackages []packageInfo, updatedVersions []*mod // Show renders a template to show the landing page of the application func Show(w http.ResponseWriter, r *http.Request) { count, _ := database.DBCon.Model((*models.Package)(nil)).Count() - - var packagesList []packageInfo - if utils.GetUserPreferences(r).General.LandingPageLayout == "classic" { - packagesList = getAddedPackages(10) - } else { - packagesList = getSearchHistoryPackages(r) - } - - updatedVersions := getUpdatedVersions(10) - - layout.Layout("", layout.Home, page( - count, packagesList, updatedVersions, utils.GetUserPreferences(r), - )).Render(r.Context(), w) + layout.Layout("", layout.Home, page(count)).Render(r.Context(), w) } diff --git a/pkg/app/handler/index/utils.go b/pkg/app/handler/index/utils.go index f524058..3d92e40 100644 --- a/pkg/app/handler/index/utils.go +++ b/pkg/app/handler/index/utils.go @@ -3,13 +3,9 @@ package index import ( - b64 "encoding/base64" - "net/http" - "slices" "soko/pkg/database" "soko/pkg/models" "strconv" - "strings" "github.com/go-pg/pg/v10" ) @@ -39,52 +35,6 @@ func getAddedPackages(n int) (packages []packageInfo) { return } -func getSearchHistoryPackages(r *http.Request) (packages []packageInfo) { - cookie, err := r.Cookie("search_history") - if err != nil { - return nil - } - packagesList := getSearchHistoryFromCookie(cookie) - - descriptionQuery := database.DBCon.Model((*models.Version)(nil)). - Column("description"). - Where("atom = package.atom"). - Limit(1) - err = database.DBCon.Model((*models.Package)(nil)). - Column("name", "category"). - ColumnExpr("(?) AS description", descriptionQuery). - Where("atom in (?)", pg.In(packagesList)). - Select(&packages) - if err != nil { - return nil - } - - return getSortedSearchHistory(packagesList, packages) -} - -func getSortedSearchHistory(sortedPackagesList []string, packagesList []packageInfo) (result []packageInfo) { - for _, gpackage := range sortedPackagesList { - for _, gpackageObject := range packagesList { - if gpackageObject.Category+"/"+gpackageObject.Name == gpackage { - result = append(result, gpackageObject) - } - } - } - slices.Reverse(result) - return -} - -func getSearchHistoryFromCookie(cookie *http.Cookie) (packagesList []string) { - cookieValue, err := b64.StdEncoding.DecodeString(cookie.Value) - if err == nil { - packagesList = strings.Split(string(cookieValue), ",") - if len(packagesList) > 10 { - packagesList = packagesList[len(packagesList)-10:] - } - } - return -} - // getUpdatedVersions returns a list of a // given number of recently updated Versions func getUpdatedVersions(n int) []*models.Version { diff --git a/pkg/app/handler/packages/show.go b/pkg/app/handler/packages/show.go index feaa14f..6e92b91 100644 --- a/pkg/app/handler/packages/show.go +++ b/pkg/app/handler/packages/show.go @@ -3,12 +3,10 @@ package packages import ( - b64 "encoding/base64" "encoding/json" "fmt" "net/http" "soko/pkg/app/layout" - "soko/pkg/app/utils" "soko/pkg/database" "soko/pkg/models" "strings" @@ -31,11 +29,6 @@ func Show(w http.ResponseWriter, r *http.Request) { var currentSubTab string - userPreferences := utils.GetUserPreferences(r) - if userPreferences.General.LandingPageLayout == "full" { - updateSearchHistory(atom, w, r) - } - var gpackage models.Package query := database.DBCon.Model(&gpackage). Relation("Bugs"). @@ -116,46 +109,8 @@ func Show(w http.ResponseWriter, r *http.Request) { layout.Layout(gpackage.Atom, layout.Packages, show(&gpackage, currentSubTab)).Render(r.Context(), w) } -func updateSearchHistory(atom string, w http.ResponseWriter, r *http.Request) { - cookie, err := r.Cookie("search_history") - var packages string - if err == nil { - cookieValue, err := b64.StdEncoding.DecodeString(cookie.Value) - if err == nil { - packagesList := strings.Split(string(cookieValue), ",") - if strings.Contains(string(cookieValue), atom) { - newPackagesList := make([]string, 0, len(packagesList)-1) - for _, gpackage := range packagesList { - if gpackage != atom { - newPackagesList = append(newPackagesList, gpackage) - } - } - packagesList = newPackagesList - } - packagesList = append(packagesList, atom) - if len(packagesList) > 10 { - packagesList = packagesList[len(packagesList)-10:] - } - packages = strings.Join(packagesList, ",") - } else { - packages = atom - } - } else { - packages = atom - } - - updatedCookie := http.Cookie{ - Name: "search_history", - Path: "/", - Value: b64.StdEncoding.EncodeToString([]byte(packages)), - Expires: time.Now().Add(365 * 24 * time.Hour), - } - http.SetCookie(w, &updatedCookie) -} - // changelog renders a json version of the changelog func changelogJSON(w http.ResponseWriter, r *http.Request) { - atom := getAtom(r) gpackage := new(models.Package) err := database.DBCon.Model(gpackage). diff --git a/pkg/app/handler/user/general.templ b/pkg/app/handler/user/general.templ deleted file mode 100644 index ee6d688..0000000 --- a/pkg/app/handler/user/general.templ +++ /dev/null @@ -1,91 +0,0 @@ -package user - -import ( - "encoding/base64" - "encoding/json" - "net/http" - "soko/pkg/app/utils" - "soko/pkg/models" - "time" -) - -templ general(preferences models.GeneralPreferences) { - <form method="post" action="/user/preferences/general/layout"> - <div class="row"> - <div class="col-5 offset-1 mt-1"> - <div class="card" style="background: transparent;"> - <div class="card-body"> - <img id="img1" alt="Recently Added Packages (default)" src="/assets/pgo3.png" style="width: 100%;cursor: pointer;"/> - </div> - </div> - <div class="text-center mt-2"> - <div class="form-check text-center form-check-inline" style="text-overflow: ellipsis;overflow: hidden;"> - <input - type="radio" - id="classicLandingpageLayout" - name="landingpage-layout" - value="classic" - checked?={ preferences.LandingPageLayout == "classic" } - /> - <label class="form-check-label ml-1" for="classicLandingpageLayout" style="overflow:hidden;text-overflow: ellipsis;" title="Recently Added Packages (default)">Recently Added Packages <i>(default)</i></label> - </div> - </div> - </div> - <div class="col-5 mt-1"> - <div class="card" style="background: transparent;"> - <div class="card-body"> - <img id="img2" alt="Recently Visited Packages" src="/assets/pgo4.png" style="width: 100%;cursor: pointer;"/> - </div> - </div> - <div class="text-center mt-2"> - <div class="form-check text-center form-check-inline" style="text-overflow: ellipsis;overflow: hidden;"> - <input - type="radio" - id="fullLandingpageLayout" - name="landingpage-layout" - value="full" - checked?={ preferences.LandingPageLayout == "full" } - /> - <label class="form-check-label ml-1" for="fullLandingpageLayout" style="overflow:hidden;text-overflow: ellipsis;" title="Recently Visited Packages">Recently Visited Packages</label> - </div> - </div> - </div> - <div class="col-10 offset-1 mt-4"> - <button type="submit" class="float-right btn btn-sm btn-primary">Save</button> - <a class="float-right btn btn-sm btn-outline-danger mr-2" href="/user/preferences/general/reset">Reset to Defaults</a> - </div> - </div> - </form> - <div id="myModal" class="modal"> - <span class="close">×</span> - <img class="modal-content" id="img01"/> - <div id="caption"></div> - </div> -} - -func General(w http.ResponseWriter, r *http.Request) { - userPreferences := utils.GetUserPreferences(r) - r.ParseForm() - // landing page layout - layout := r.Form.Get("landingpage-layout") - if layout == "classic" || layout == "full" { - userPreferences.General.LandingPageLayout = layout - } - // store cookie - encodedUserPreferences, err := json.Marshal(&userPreferences.General) - if err == nil { - sEnc := base64.StdEncoding.EncodeToString(encodedUserPreferences) - addCookie(w, "userpref_general", "/", sEnc, 365*24*time.Hour) - } - http.Redirect(w, r, "/user/preferences/general", http.StatusSeeOther) -} - -func ResetGeneral(w http.ResponseWriter, r *http.Request) { - userPreferences := utils.GetDefaultUserPreferences() - encodedUserPreferences, err := json.Marshal(&userPreferences.General) - if err == nil { - sEnc := base64.StdEncoding.EncodeToString(encodedUserPreferences) - addCookie(w, "userpref_general", "/", sEnc, 365*24*time.Hour) - } - http.Redirect(w, r, "/user/preferences/general", http.StatusSeeOther) -} diff --git a/pkg/app/handler/user/preferences.templ b/pkg/app/handler/user/preferences.templ index 01805e2..0eb7e4a 100644 --- a/pkg/app/handler/user/preferences.templ +++ b/pkg/app/handler/user/preferences.templ @@ -11,16 +11,6 @@ import ( var viewTabs = []layout.SubTab{ { - Name: "General", - Link: "/user/preferences/general", - Icon: "fa fa-globe mr-1", - }, - { - Name: "Packages", - Link: "/user/preferences/packages", - Icon: "fa fa-cube mr-1", - }, - { Name: "Maintainers", Link: "/user/preferences/maintainers", Icon: "fa fa-users mr-1", @@ -30,8 +20,6 @@ var viewTabs = []layout.SubTab{ templ show(currentSubTab string, preferences models.UserPreferences) { <div class="container mb-5"> switch currentSubTab { - case "General": - @general(preferences.General) case "Maintainers": @maintainers(preferences.Maintainers) } @@ -39,24 +27,6 @@ templ show(currentSubTab string, preferences models.UserPreferences) { <script src="/assets/userpref.js"></script> } -templ sortableScript() { - <script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script> - <script> - if(document.getElementById("example1") != null && document.getElementById("example2") != null) { - new Sortable(example1, { - group: 'shared', - animation: 150, - ghostClass: 'bg-info' - }); - new Sortable(example2, { - group: 'shared', - animation: 150, - ghostClass: 'bg-info' - }); - } - </script> -} - func Preferences(currentSubTab string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { layout.TabbedLayout("User", layout.About, "Preferences", "fa fa-fw fa-cog", "You can customize the page contents to your needs here", viewTabs, diff --git a/pkg/app/serve.go b/pkg/app/serve.go index a33668e..1b54af7 100644 --- a/pkg/app/serve.go +++ b/pkg/app/serve.go @@ -113,13 +113,8 @@ func Serve() { redirect("GET /user", "/user/preferences/general") redirect("GET /user/preferences", "/user/preferences/general") - setRoute("GET /user/preferences/general", user.Preferences("General")) - setRoute("GET /user/preferences/packages", user.Preferences("Packages")) setRoute("GET /user/preferences/maintainers", user.Preferences("Maintainers")) - setRoute("/user/preferences/general/layout", user.General) - setRoute("/user/preferences/general/reset", user.ResetGeneral) - setRoute("/user/preferences/maintainers/edit", user.Maintainers) setRoute("/user/preferences/maintainers/reset", user.ResetMaintainers) diff --git a/pkg/app/utils/userdata.go b/pkg/app/utils/userdata.go index 13874f8..13f9973 100644 --- a/pkg/app/utils/userdata.go +++ b/pkg/app/utils/userdata.go @@ -14,15 +14,7 @@ func GetDefaultUserPreferences() models.UserPreferences { func GetUserPreferences(r *http.Request) models.UserPreferences { userPreferences := models.GetDefaultUserPreferences() - var cookie, err = r.Cookie("userpref_general") - if err == nil { - cookieValue, err := b64.StdEncoding.DecodeString(cookie.Value) - if err == nil { - json.Unmarshal(cookieValue, &userPreferences.General) - } - } - - cookie, err = r.Cookie("userpref_maintainers") + cookie, err := r.Cookie("userpref_maintainers") if err == nil { cookieValue, err := b64.StdEncoding.DecodeString(cookie.Value) if err == nil { @@ -30,11 +22,11 @@ func GetUserPreferences(r *http.Request) models.UserPreferences { } } + // old cookie: search_history + // old cookie: userpref_general // old cookie: userpref_packages // old cookie: userpref_useflags // old cookie: userpref_arches - userPreferences.Sanitize() - return userPreferences } diff --git a/pkg/models/userpreferences.go b/pkg/models/userpreferences.go index 0db7107..ed133c7 100644 --- a/pkg/models/userpreferences.go +++ b/pkg/models/userpreferences.go @@ -3,14 +3,9 @@ package models type UserPreferences struct { - General GeneralPreferences Maintainers MaintainersPreferences } -type GeneralPreferences struct { - LandingPageLayout string -} - type MaintainersPreferences struct { IncludeProjectPackages bool ExcludedProjects []string @@ -21,21 +16,10 @@ var AllArches = [...]string{"alpha", "amd64", "arm", "arm64", "hppa", "ia64", "m func GetDefaultUserPreferences() UserPreferences { userPreferences := UserPreferences{} - userPreferences.General = GeneralPreferences{} userPreferences.Maintainers = MaintainersPreferences{} - userPreferences.General.LandingPageLayout = "classic" - userPreferences.Maintainers.IncludeProjectPackages = false userPreferences.Maintainers.ExcludedProjects = []string{} return userPreferences } - -func (u *UserPreferences) Sanitize() { - defaultUserPreferences := GetDefaultUserPreferences() - - if !(u.General.LandingPageLayout == "classic" || u.General.LandingPageLayout == "full") { - u.General.LandingPageLayout = defaultUserPreferences.General.LandingPageLayout - } -} |