From c621f9a2e60e6b555c598b3316c4f7d2643dc345 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Tue, 19 Nov 2024 19:24:12 +0000 Subject: [PATCH 1/9] Update the list of watchers and stargazers when clicking watch/unwatch or star/unstar We make sure the user cards are updated Signed-off-by: Yarden Shoham --- routers/web/repo/repo.go | 3 +++ routers/web/repo/view.go | 42 +++++++++++++++++++++++++----------- routers/web/web.go | 2 ++ templates/repo/watchers.tmpl | 9 +++++++- 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index be6f2d483ff31..9206f936f4568 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -352,6 +352,9 @@ func Action(ctx *context.Context) { ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID) } + // if we have user cards on the page we should refresh them + ctx.RespHeader().Add("HX-Trigger", "refreshCards") + switch ctx.PathParam(":action") { case "watch", "unwatch", "star", "unstar": // we have to reload the repository because NumStars or NumWatching (used in the templates) has just changed diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 5d68ace29b535..6dad016bde33e 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -65,6 +65,7 @@ const ( tplRepoHome base.TplName = "repo/home" tplRepoViewList base.TplName = "repo/view_list" tplWatchers base.TplName = "repo/watchers" + tplCards base.TplName = "repo/user_cards" tplForks base.TplName = "repo/forks" tplMigrating base.TplName = "repo/migrate/migrating" ) @@ -1122,25 +1123,42 @@ func RenderUserCards(ctx *context.Context, total int, getter func(opts db.ListOp ctx.HTML(http.StatusOK, tpl) } +func renderUserList(ctx *context.Context, pageType string, total int, getter func(opts db.ListOptions) ([]*user_model.User, error), tpl base.TplName) { + ctx.Data["Title"] = ctx.Tr(pageType) + ctx.Data["CardsTitle"] = ctx.Tr(pageType) + RenderUserCards(ctx, total, getter, tpl) +} + // Watchers render repository's watch users func Watchers(ctx *context.Context) { - ctx.Data["Title"] = ctx.Tr("repo.watchers") - ctx.Data["CardsTitle"] = ctx.Tr("repo.watchers") - ctx.Data["PageIsWatchers"] = true + renderUserList(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches, + func(opts db.ListOptions) ([]*user_model.User, error) { + return repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, opts) + }, tplWatchers) +} - RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, func(opts db.ListOptions) ([]*user_model.User, error) { - return repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, opts) - }, tplWatchers) +// WatchersCards renders a repository's watchers user cards +func WatchersCards(ctx *context.Context) { + renderUserList(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches, + func(opts db.ListOptions) ([]*user_model.User, error) { + return repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, opts) + }, tplCards) } // Stars render repository's starred users func Stars(ctx *context.Context) { - ctx.Data["Title"] = ctx.Tr("repo.stargazers") - ctx.Data["CardsTitle"] = ctx.Tr("repo.stargazers") - ctx.Data["PageIsStargazers"] = true - RenderUserCards(ctx, ctx.Repo.Repository.NumStars, func(opts db.ListOptions) ([]*user_model.User, error) { - return repo_model.GetStargazers(ctx, ctx.Repo.Repository, opts) - }, tplWatchers) + renderUserList(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars, + func(opts db.ListOptions) ([]*user_model.User, error) { + return repo_model.GetStargazers(ctx, ctx.Repo.Repository, opts) + }, tplWatchers) +} + +// StarsCards renders a repository's stargazers user cards +func StarsCards(ctx *context.Context) { + renderUserList(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars, + func(opts db.ListOptions) ([]*user_model.User, error) { + return repo_model.GetStargazers(ctx, ctx.Repo.Repository, opts) + }, tplCards) } // Forks render repository's forked users diff --git a/routers/web/web.go b/routers/web/web.go index 137c67730652d..607b644a424c5 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1599,7 +1599,9 @@ func registerRoutes(m *web.Router) { m.Group("/{username}/{reponame}", func() { m.Get("/stars", repo.Stars) + m.Get("/stars/cards", repo.StarsCards) m.Get("/watchers", repo.Watchers) + m.Get("/watchers/cards", repo.WatchersCards) m.Get("/search", reqRepoCodeReader, repo.Search) m.Post("/action/{action}", reqSignIn, repo.Action) }, optSignIn, context.RepoAssignment, context.RepoRef()) diff --git a/templates/repo/watchers.tmpl b/templates/repo/watchers.tmpl index 1828544c8cb04..e7493deca465c 100644 --- a/templates/repo/watchers.tmpl +++ b/templates/repo/watchers.tmpl @@ -1,7 +1,14 @@ {{template "base/head" .}}
{{template "repo/header" .}} -
+
+
{{template "repo/user_cards" .}}
From 4f633caecad25cc13b18726d890f696bd4d12b20 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 20 Nov 2024 17:28:40 +0000 Subject: [PATCH 2/9] Add comments and reduce abstraction levels --- models/repo/star.go | 4 ++-- models/repo/star_test.go | 6 +++--- routers/api/v1/repo/star.go | 2 +- routers/web/repo/repo.go | 5 ++++- routers/web/repo/view.go | 32 ++++++++------------------------ templates/repo/watchers.tmpl | 3 +++ 6 files changed, 21 insertions(+), 31 deletions(-) diff --git a/models/repo/star.go b/models/repo/star.go index 4c66855525fa6..f734584f9a4f4 100644 --- a/models/repo/star.go +++ b/models/repo/star.go @@ -76,8 +76,8 @@ func IsStaring(ctx context.Context, userID, repoID int64) bool { } // GetStargazers returns the users that starred the repo. -func GetStargazers(ctx context.Context, repo *Repository, opts db.ListOptions) ([]*user_model.User, error) { - sess := db.GetEngine(ctx).Where("star.repo_id = ?", repo.ID). +func GetStargazers(ctx context.Context, repoID int64, opts db.ListOptions) ([]*user_model.User, error) { + sess := db.GetEngine(ctx).Where("star.repo_id = ?", repoID). Join("LEFT", "star", "`user`.id = star.uid") if opts.Page > 0 { sess = db.SetSessionPagination(sess, &opts) diff --git a/models/repo/star_test.go b/models/repo/star_test.go index aaac89d975d7c..20fe82302ac52 100644 --- a/models/repo/star_test.go +++ b/models/repo/star_test.go @@ -39,7 +39,7 @@ func TestRepository_GetStargazers(t *testing.T) { // repo with stargazers assert.NoError(t, unittest.PrepareTestDatabase()) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4}) - gazers, err := repo_model.GetStargazers(db.DefaultContext, repo, db.ListOptions{Page: 0}) + gazers, err := repo_model.GetStargazers(db.DefaultContext, repo.ID, db.ListOptions{Page: 0}) assert.NoError(t, err) if assert.Len(t, gazers, 1) { assert.Equal(t, int64(2), gazers[0].ID) @@ -50,7 +50,7 @@ func TestRepository_GetStargazers2(t *testing.T) { // repo with stargazers assert.NoError(t, unittest.PrepareTestDatabase()) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}) - gazers, err := repo_model.GetStargazers(db.DefaultContext, repo, db.ListOptions{Page: 0}) + gazers, err := repo_model.GetStargazers(db.DefaultContext, repo.ID, db.ListOptions{Page: 0}) assert.NoError(t, err) assert.Len(t, gazers, 0) } @@ -69,7 +69,7 @@ func TestClearRepoStars(t *testing.T) { assert.NoError(t, repo_model.ClearRepoStars(db.DefaultContext, repo.ID)) unittest.AssertNotExistsBean(t, &repo_model.Star{UID: user.ID, RepoID: repo.ID}) - gazers, err := repo_model.GetStargazers(db.DefaultContext, repo, db.ListOptions{Page: 0}) + gazers, err := repo_model.GetStargazers(db.DefaultContext, repo.ID, db.ListOptions{Page: 0}) assert.NoError(t, err) assert.Len(t, gazers, 0) } diff --git a/routers/api/v1/repo/star.go b/routers/api/v1/repo/star.go index 99676de119c1f..3e414322d3c64 100644 --- a/routers/api/v1/repo/star.go +++ b/routers/api/v1/repo/star.go @@ -45,7 +45,7 @@ func ListStargazers(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" - stargazers, err := repo_model.GetStargazers(ctx, ctx.Repo.Repository, utils.GetListOptions(ctx)) + stargazers, err := repo_model.GetStargazers(ctx, ctx.Repo.Repository.ID, utils.GetListOptions(ctx)) if err != nil { ctx.Error(http.StatusInternalServerError, "GetStargazers", err) return diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 9206f936f4568..93a6261b3882b 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -352,7 +352,10 @@ func Action(ctx *context.Context) { ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID) } - // if we have user cards on the page we should refresh them + // we send the HX-Trigger header to trigger the refreshCards event, when the frontend receives a request with this header + // htmx triggers all elements that have the attribute hx-trigger="refreshCards from:body". This attribute is usually placed + // on containers that show a list of either stargazers or watchers. For a demonstration of the effects see the pull + // request description of https://github.com/go-gitea/gitea/pull/32570. ctx.RespHeader().Add("HX-Trigger", "refreshCards") switch ctx.PathParam(":action") { diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 6dad016bde33e..7cfaebb6801db 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -1102,7 +1102,9 @@ func checkOutdatedBranch(ctx *context.Context) { } // RenderUserCards render a page show users according the input template -func RenderUserCards(ctx *context.Context, total int, getter func(opts db.ListOptions) ([]*user_model.User, error), tpl base.TplName) { +func RenderUserCards(ctx *context.Context, pageType string, total int, getter func(goctx gocontext.Context, repoID int64, opts db.ListOptions) ([]*user_model.User, error), tpl base.TplName) { + ctx.Data["Title"] = ctx.Tr(pageType) + ctx.Data["CardsTitle"] = ctx.Tr(pageType) page := ctx.FormInt("page") if page <= 0 { page = 1 @@ -1110,7 +1112,7 @@ func RenderUserCards(ctx *context.Context, total int, getter func(opts db.ListOp pager := context.NewPagination(total, setting.ItemsPerPage, page, 5) ctx.Data["Page"] = pager - items, err := getter(db.ListOptions{ + items, err := getter(ctx, ctx.Repo.Repository.ID, db.ListOptions{ Page: pager.Paginater.Current(), PageSize: setting.ItemsPerPage, }) @@ -1123,42 +1125,24 @@ func RenderUserCards(ctx *context.Context, total int, getter func(opts db.ListOp ctx.HTML(http.StatusOK, tpl) } -func renderUserList(ctx *context.Context, pageType string, total int, getter func(opts db.ListOptions) ([]*user_model.User, error), tpl base.TplName) { - ctx.Data["Title"] = ctx.Tr(pageType) - ctx.Data["CardsTitle"] = ctx.Tr(pageType) - RenderUserCards(ctx, total, getter, tpl) -} - // Watchers render repository's watch users func Watchers(ctx *context.Context) { - renderUserList(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches, - func(opts db.ListOptions) ([]*user_model.User, error) { - return repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, opts) - }, tplWatchers) + RenderUserCards(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches, repo_model.GetRepoWatchers, tplWatchers) } // WatchersCards renders a repository's watchers user cards func WatchersCards(ctx *context.Context) { - renderUserList(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches, - func(opts db.ListOptions) ([]*user_model.User, error) { - return repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, opts) - }, tplCards) + RenderUserCards(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches, repo_model.GetRepoWatchers, tplCards) } // Stars render repository's starred users func Stars(ctx *context.Context) { - renderUserList(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars, - func(opts db.ListOptions) ([]*user_model.User, error) { - return repo_model.GetStargazers(ctx, ctx.Repo.Repository, opts) - }, tplWatchers) + RenderUserCards(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars, repo_model.GetStargazers, tplWatchers) } // StarsCards renders a repository's stargazers user cards func StarsCards(ctx *context.Context) { - renderUserList(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars, - func(opts db.ListOptions) ([]*user_model.User, error) { - return repo_model.GetStargazers(ctx, ctx.Repo.Repository, opts) - }, tplCards) + RenderUserCards(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars, repo_model.GetStargazers, tplCards) } // Forks render repository's forked users diff --git a/templates/repo/watchers.tmpl b/templates/repo/watchers.tmpl index e7493deca465c..829d75fc292e0 100644 --- a/templates/repo/watchers.tmpl +++ b/templates/repo/watchers.tmpl @@ -2,6 +2,9 @@
{{template "repo/header" .}}
+
Date: Wed, 20 Nov 2024 17:31:42 +0000 Subject: [PATCH 3/9] Mention manual testing --- templates/repo/watchers.tmpl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/templates/repo/watchers.tmpl b/templates/repo/watchers.tmpl index 829d75fc292e0..35dd6518d96e8 100644 --- a/templates/repo/watchers.tmpl +++ b/templates/repo/watchers.tmpl @@ -4,7 +4,8 @@
+ watched/unwatched/starred/unstarred and we want their user card to appear/disappear. + To test go to the watchers page and click the watch button. The user cards should reload. -->
Date: Wed, 20 Nov 2024 17:42:05 +0000 Subject: [PATCH 4/9] Format --- templates/repo/watchers.tmpl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/templates/repo/watchers.tmpl b/templates/repo/watchers.tmpl index 35dd6518d96e8..8cef5ee75a6d5 100644 --- a/templates/repo/watchers.tmpl +++ b/templates/repo/watchers.tmpl @@ -3,9 +3,9 @@ {{template "repo/header" .}}
+ includes the header "HX-Trigger: refreshCards". This usually happens when a user + watched/unwatched/starred/unstarred and we want their user card to appear/disappear. + To test go to the watchers page and click the watch button. The user cards should reload. -->
Date: Thu, 21 Nov 2024 21:33:12 +0800 Subject: [PATCH 5/9] simpilfy --- models/repo/star.go | 4 ++-- models/repo/star_test.go | 6 +++--- routers/api/v1/repo/star.go | 2 +- routers/web/repo/repo.go | 7 ++----- routers/web/repo/view.go | 30 ++++++++++++++---------------- routers/web/web.go | 2 -- templates/repo/user_cards.tmpl | 14 +++++++++++++- templates/repo/watchers.tmpl | 13 +------------ 8 files changed, 36 insertions(+), 42 deletions(-) diff --git a/models/repo/star.go b/models/repo/star.go index f734584f9a4f4..4c66855525fa6 100644 --- a/models/repo/star.go +++ b/models/repo/star.go @@ -76,8 +76,8 @@ func IsStaring(ctx context.Context, userID, repoID int64) bool { } // GetStargazers returns the users that starred the repo. -func GetStargazers(ctx context.Context, repoID int64, opts db.ListOptions) ([]*user_model.User, error) { - sess := db.GetEngine(ctx).Where("star.repo_id = ?", repoID). +func GetStargazers(ctx context.Context, repo *Repository, opts db.ListOptions) ([]*user_model.User, error) { + sess := db.GetEngine(ctx).Where("star.repo_id = ?", repo.ID). Join("LEFT", "star", "`user`.id = star.uid") if opts.Page > 0 { sess = db.SetSessionPagination(sess, &opts) diff --git a/models/repo/star_test.go b/models/repo/star_test.go index 20fe82302ac52..aaac89d975d7c 100644 --- a/models/repo/star_test.go +++ b/models/repo/star_test.go @@ -39,7 +39,7 @@ func TestRepository_GetStargazers(t *testing.T) { // repo with stargazers assert.NoError(t, unittest.PrepareTestDatabase()) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4}) - gazers, err := repo_model.GetStargazers(db.DefaultContext, repo.ID, db.ListOptions{Page: 0}) + gazers, err := repo_model.GetStargazers(db.DefaultContext, repo, db.ListOptions{Page: 0}) assert.NoError(t, err) if assert.Len(t, gazers, 1) { assert.Equal(t, int64(2), gazers[0].ID) @@ -50,7 +50,7 @@ func TestRepository_GetStargazers2(t *testing.T) { // repo with stargazers assert.NoError(t, unittest.PrepareTestDatabase()) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}) - gazers, err := repo_model.GetStargazers(db.DefaultContext, repo.ID, db.ListOptions{Page: 0}) + gazers, err := repo_model.GetStargazers(db.DefaultContext, repo, db.ListOptions{Page: 0}) assert.NoError(t, err) assert.Len(t, gazers, 0) } @@ -69,7 +69,7 @@ func TestClearRepoStars(t *testing.T) { assert.NoError(t, repo_model.ClearRepoStars(db.DefaultContext, repo.ID)) unittest.AssertNotExistsBean(t, &repo_model.Star{UID: user.ID, RepoID: repo.ID}) - gazers, err := repo_model.GetStargazers(db.DefaultContext, repo.ID, db.ListOptions{Page: 0}) + gazers, err := repo_model.GetStargazers(db.DefaultContext, repo, db.ListOptions{Page: 0}) assert.NoError(t, err) assert.Len(t, gazers, 0) } diff --git a/routers/api/v1/repo/star.go b/routers/api/v1/repo/star.go index 3e414322d3c64..99676de119c1f 100644 --- a/routers/api/v1/repo/star.go +++ b/routers/api/v1/repo/star.go @@ -45,7 +45,7 @@ func ListStargazers(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" - stargazers, err := repo_model.GetStargazers(ctx, ctx.Repo.Repository.ID, utils.GetListOptions(ctx)) + stargazers, err := repo_model.GetStargazers(ctx, ctx.Repo.Repository, utils.GetListOptions(ctx)) if err != nil { ctx.Error(http.StatusInternalServerError, "GetStargazers", err) return diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 93a6261b3882b..85c9c56e22568 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -352,11 +352,8 @@ func Action(ctx *context.Context) { ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID) } - // we send the HX-Trigger header to trigger the refreshCards event, when the frontend receives a request with this header - // htmx triggers all elements that have the attribute hx-trigger="refreshCards from:body". This attribute is usually placed - // on containers that show a list of either stargazers or watchers. For a demonstration of the effects see the pull - // request description of https://github.com/go-gitea/gitea/pull/32570. - ctx.RespHeader().Add("HX-Trigger", "refreshCards") + // see the `hx-trigger="refreshUserCards ..."` comments in tmpl + ctx.RespHeader().Add("HX-Trigger", "refreshUserCards") switch ctx.PathParam(":action") { case "watch", "unwatch", "star", "unstar": diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 7cfaebb6801db..5d68ace29b535 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -65,7 +65,6 @@ const ( tplRepoHome base.TplName = "repo/home" tplRepoViewList base.TplName = "repo/view_list" tplWatchers base.TplName = "repo/watchers" - tplCards base.TplName = "repo/user_cards" tplForks base.TplName = "repo/forks" tplMigrating base.TplName = "repo/migrate/migrating" ) @@ -1102,9 +1101,7 @@ func checkOutdatedBranch(ctx *context.Context) { } // RenderUserCards render a page show users according the input template -func RenderUserCards(ctx *context.Context, pageType string, total int, getter func(goctx gocontext.Context, repoID int64, opts db.ListOptions) ([]*user_model.User, error), tpl base.TplName) { - ctx.Data["Title"] = ctx.Tr(pageType) - ctx.Data["CardsTitle"] = ctx.Tr(pageType) +func RenderUserCards(ctx *context.Context, total int, getter func(opts db.ListOptions) ([]*user_model.User, error), tpl base.TplName) { page := ctx.FormInt("page") if page <= 0 { page = 1 @@ -1112,7 +1109,7 @@ func RenderUserCards(ctx *context.Context, pageType string, total int, getter fu pager := context.NewPagination(total, setting.ItemsPerPage, page, 5) ctx.Data["Page"] = pager - items, err := getter(ctx, ctx.Repo.Repository.ID, db.ListOptions{ + items, err := getter(db.ListOptions{ Page: pager.Paginater.Current(), PageSize: setting.ItemsPerPage, }) @@ -1127,22 +1124,23 @@ func RenderUserCards(ctx *context.Context, pageType string, total int, getter fu // Watchers render repository's watch users func Watchers(ctx *context.Context) { - RenderUserCards(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches, repo_model.GetRepoWatchers, tplWatchers) -} + ctx.Data["Title"] = ctx.Tr("repo.watchers") + ctx.Data["CardsTitle"] = ctx.Tr("repo.watchers") + ctx.Data["PageIsWatchers"] = true -// WatchersCards renders a repository's watchers user cards -func WatchersCards(ctx *context.Context) { - RenderUserCards(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches, repo_model.GetRepoWatchers, tplCards) + RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, func(opts db.ListOptions) ([]*user_model.User, error) { + return repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, opts) + }, tplWatchers) } // Stars render repository's starred users func Stars(ctx *context.Context) { - RenderUserCards(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars, repo_model.GetStargazers, tplWatchers) -} - -// StarsCards renders a repository's stargazers user cards -func StarsCards(ctx *context.Context) { - RenderUserCards(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars, repo_model.GetStargazers, tplCards) + ctx.Data["Title"] = ctx.Tr("repo.stargazers") + ctx.Data["CardsTitle"] = ctx.Tr("repo.stargazers") + ctx.Data["PageIsStargazers"] = true + RenderUserCards(ctx, ctx.Repo.Repository.NumStars, func(opts db.ListOptions) ([]*user_model.User, error) { + return repo_model.GetStargazers(ctx, ctx.Repo.Repository, opts) + }, tplWatchers) } // Forks render repository's forked users diff --git a/routers/web/web.go b/routers/web/web.go index e7f39d28bd7e4..b96d06ed66eb6 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1599,9 +1599,7 @@ func registerRoutes(m *web.Router) { m.Group("/{username}/{reponame}", func() { m.Get("/stars", repo.Stars) - m.Get("/stars/cards", repo.StarsCards) m.Get("/watchers", repo.Watchers) - m.Get("/watchers/cards", repo.WatchersCards) m.Get("/search", reqRepoCodeReader, repo.Search) m.Post("/action/{action}", reqSignIn, repo.Action) }, optSignIn, context.RepoAssignment, context.RepoRef()) diff --git a/templates/repo/user_cards.tmpl b/templates/repo/user_cards.tmpl index 7cd3d4517ad9c..7fee7fc45b511 100644 --- a/templates/repo/user_cards.tmpl +++ b/templates/repo/user_cards.tmpl @@ -1,4 +1,16 @@ -
+ +
+
{{if .CardsTitle}}

{{.CardsTitle}} diff --git a/templates/repo/watchers.tmpl b/templates/repo/watchers.tmpl index 8cef5ee75a6d5..1828544c8cb04 100644 --- a/templates/repo/watchers.tmpl +++ b/templates/repo/watchers.tmpl @@ -1,18 +1,7 @@ {{template "base/head" .}}
{{template "repo/header" .}} -
- -
+
{{template "repo/user_cards" .}}
From f00c49174c3b2be86fe0b6a2c879a233d6ac5a1b Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 21 Nov 2024 21:35:51 +0800 Subject: [PATCH 6/9] outerHTML --- templates/repo/user_cards.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/user_cards.tmpl b/templates/repo/user_cards.tmpl index 7fee7fc45b511..736fa855b93f4 100644 --- a/templates/repo/user_cards.tmpl +++ b/templates/repo/user_cards.tmpl @@ -9,7 +9,7 @@ At the moment, no JS initialization would re-trigger (fortunately there is no JS hx-trigger="refreshUserCards from:body" hx-indicator=".no-loading-indicator" hx-get="{{$.Link}}" - hx-swap="innerHTML" hx-select=".user-cards" + hx-swap="outerHTML" hx-select=".user-cards" > {{if .CardsTitle}}

From 09aac88f30c840974c1ddb606fde89c8298d22df Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 21 Nov 2024 21:37:32 +0800 Subject: [PATCH 7/9] CurrentURL --- templates/repo/user_cards.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/user_cards.tmpl b/templates/repo/user_cards.tmpl index 736fa855b93f4..0cbcc6d4d76d4 100644 --- a/templates/repo/user_cards.tmpl +++ b/templates/repo/user_cards.tmpl @@ -8,7 +8,7 @@ At the moment, no JS initialization would re-trigger (fortunately there is no JS
{{if .CardsTitle}} From b1aa34a26889ac2233def6891cb6d4311b9f9035 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 21 Nov 2024 21:40:43 +0800 Subject: [PATCH 8/9] remove PageIsWatchers and PageIsStargazers because not used --- routers/web/repo/view.go | 3 --- templates/repo/user_cards.tmpl | 6 ++---- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 5d68ace29b535..3396bd6361626 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -1126,8 +1126,6 @@ func RenderUserCards(ctx *context.Context, total int, getter func(opts db.ListOp func Watchers(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.watchers") ctx.Data["CardsTitle"] = ctx.Tr("repo.watchers") - ctx.Data["PageIsWatchers"] = true - RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, func(opts db.ListOptions) ([]*user_model.User, error) { return repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, opts) }, tplWatchers) @@ -1137,7 +1135,6 @@ func Watchers(ctx *context.Context) { func Stars(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.stargazers") ctx.Data["CardsTitle"] = ctx.Tr("repo.stargazers") - ctx.Data["PageIsStargazers"] = true RenderUserCards(ctx, ctx.Repo.Repository.NumStars, func(opts db.ListOptions) ([]*user_model.User, error) { return repo_model.GetStargazers(ctx, ctx.Repo.Repository, opts) }, tplWatchers) diff --git a/templates/repo/user_cards.tmpl b/templates/repo/user_cards.tmpl index 0cbcc6d4d76d4..360aeaf619f8b 100644 --- a/templates/repo/user_cards.tmpl +++ b/templates/repo/user_cards.tmpl @@ -6,10 +6,8 @@ At the moment, no JS initialization would re-trigger (fortunately there is no JS -->
{{if .CardsTitle}}

From 61c882ccf5523d9e44da39c7fba39bee51f4612d Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Fri, 22 Nov 2024 13:08:07 +0200 Subject: [PATCH 9/9] Update routers/web/repo/repo.go Co-authored-by: silverwind --- routers/web/repo/repo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 85c9c56e22568..b62fd21585184 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -353,7 +353,7 @@ func Action(ctx *context.Context) { } // see the `hx-trigger="refreshUserCards ..."` comments in tmpl - ctx.RespHeader().Add("HX-Trigger", "refreshUserCards") + ctx.RespHeader().Add("hx-trigger", "refreshUserCards") switch ctx.PathParam(":action") { case "watch", "unwatch", "star", "unstar":