Skip to content

Commit da86679

Browse files
committed
simpilfy
1 parent 92bc292 commit da86679

File tree

8 files changed

+36
-42
lines changed

8 files changed

+36
-42
lines changed

models/repo/star.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ func IsStaring(ctx context.Context, userID, repoID int64) bool {
7676
}
7777

7878
// GetStargazers returns the users that starred the repo.
79-
func GetStargazers(ctx context.Context, repoID int64, opts db.ListOptions) ([]*user_model.User, error) {
80-
sess := db.GetEngine(ctx).Where("star.repo_id = ?", repoID).
79+
func GetStargazers(ctx context.Context, repo *Repository, opts db.ListOptions) ([]*user_model.User, error) {
80+
sess := db.GetEngine(ctx).Where("star.repo_id = ?", repo.ID).
8181
Join("LEFT", "star", "`user`.id = star.uid")
8282
if opts.Page > 0 {
8383
sess = db.SetSessionPagination(sess, &opts)

models/repo/star_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestRepository_GetStargazers(t *testing.T) {
3939
// repo with stargazers
4040
assert.NoError(t, unittest.PrepareTestDatabase())
4141
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
42-
gazers, err := repo_model.GetStargazers(db.DefaultContext, repo.ID, db.ListOptions{Page: 0})
42+
gazers, err := repo_model.GetStargazers(db.DefaultContext, repo, db.ListOptions{Page: 0})
4343
assert.NoError(t, err)
4444
if assert.Len(t, gazers, 1) {
4545
assert.Equal(t, int64(2), gazers[0].ID)
@@ -50,7 +50,7 @@ func TestRepository_GetStargazers2(t *testing.T) {
5050
// repo with stargazers
5151
assert.NoError(t, unittest.PrepareTestDatabase())
5252
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
53-
gazers, err := repo_model.GetStargazers(db.DefaultContext, repo.ID, db.ListOptions{Page: 0})
53+
gazers, err := repo_model.GetStargazers(db.DefaultContext, repo, db.ListOptions{Page: 0})
5454
assert.NoError(t, err)
5555
assert.Len(t, gazers, 0)
5656
}
@@ -69,7 +69,7 @@ func TestClearRepoStars(t *testing.T) {
6969
assert.NoError(t, repo_model.ClearRepoStars(db.DefaultContext, repo.ID))
7070
unittest.AssertNotExistsBean(t, &repo_model.Star{UID: user.ID, RepoID: repo.ID})
7171

72-
gazers, err := repo_model.GetStargazers(db.DefaultContext, repo.ID, db.ListOptions{Page: 0})
72+
gazers, err := repo_model.GetStargazers(db.DefaultContext, repo, db.ListOptions{Page: 0})
7373
assert.NoError(t, err)
7474
assert.Len(t, gazers, 0)
7575
}

routers/api/v1/repo/star.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func ListStargazers(ctx *context.APIContext) {
4545
// "404":
4646
// "$ref": "#/responses/notFound"
4747

48-
stargazers, err := repo_model.GetStargazers(ctx, ctx.Repo.Repository.ID, utils.GetListOptions(ctx))
48+
stargazers, err := repo_model.GetStargazers(ctx, ctx.Repo.Repository, utils.GetListOptions(ctx))
4949
if err != nil {
5050
ctx.Error(http.StatusInternalServerError, "GetStargazers", err)
5151
return

routers/web/repo/repo.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,8 @@ func Action(ctx *context.Context) {
352352
ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID)
353353
}
354354

355-
// we send the HX-Trigger header to trigger the refreshCards event, when the frontend receives a request with this header
356-
// htmx triggers all elements that have the attribute hx-trigger="refreshCards from:body". This attribute is usually placed
357-
// on containers that show a list of either stargazers or watchers. For a demonstration of the effects see the pull
358-
// request description of https://github.com/go-gitea/gitea/pull/32570.
359-
ctx.RespHeader().Add("HX-Trigger", "refreshCards")
355+
// see the `hx-trigger="refreshUserCards ..."` comments in tmpl
356+
ctx.RespHeader().Add("HX-Trigger", "refreshUserCards")
360357

361358
switch ctx.PathParam(":action") {
362359
case "watch", "unwatch", "star", "unstar":

routers/web/repo/view.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ const (
6565
tplRepoHome base.TplName = "repo/home"
6666
tplRepoViewList base.TplName = "repo/view_list"
6767
tplWatchers base.TplName = "repo/watchers"
68-
tplCards base.TplName = "repo/user_cards"
6968
tplForks base.TplName = "repo/forks"
7069
tplMigrating base.TplName = "repo/migrate/migrating"
7170
)
@@ -1102,17 +1101,15 @@ func checkOutdatedBranch(ctx *context.Context) {
11021101
}
11031102

11041103
// RenderUserCards render a page show users according the input template
1105-
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) {
1106-
ctx.Data["Title"] = ctx.Tr(pageType)
1107-
ctx.Data["CardsTitle"] = ctx.Tr(pageType)
1104+
func RenderUserCards(ctx *context.Context, total int, getter func(opts db.ListOptions) ([]*user_model.User, error), tpl base.TplName) {
11081105
page := ctx.FormInt("page")
11091106
if page <= 0 {
11101107
page = 1
11111108
}
11121109
pager := context.NewPagination(total, setting.ItemsPerPage, page, 5)
11131110
ctx.Data["Page"] = pager
11141111

1115-
items, err := getter(ctx, ctx.Repo.Repository.ID, db.ListOptions{
1112+
items, err := getter(db.ListOptions{
11161113
Page: pager.Paginater.Current(),
11171114
PageSize: setting.ItemsPerPage,
11181115
})
@@ -1127,22 +1124,23 @@ func RenderUserCards(ctx *context.Context, pageType string, total int, getter fu
11271124

11281125
// Watchers render repository's watch users
11291126
func Watchers(ctx *context.Context) {
1130-
RenderUserCards(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches, repo_model.GetRepoWatchers, tplWatchers)
1131-
}
1127+
ctx.Data["Title"] = ctx.Tr("repo.watchers")
1128+
ctx.Data["CardsTitle"] = ctx.Tr("repo.watchers")
1129+
ctx.Data["PageIsWatchers"] = true
11321130

1133-
// WatchersCards renders a repository's watchers user cards
1134-
func WatchersCards(ctx *context.Context) {
1135-
RenderUserCards(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches, repo_model.GetRepoWatchers, tplCards)
1131+
RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, func(opts db.ListOptions) ([]*user_model.User, error) {
1132+
return repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, opts)
1133+
}, tplWatchers)
11361134
}
11371135

11381136
// Stars render repository's starred users
11391137
func Stars(ctx *context.Context) {
1140-
RenderUserCards(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars, repo_model.GetStargazers, tplWatchers)
1141-
}
1142-
1143-
// StarsCards renders a repository's stargazers user cards
1144-
func StarsCards(ctx *context.Context) {
1145-
RenderUserCards(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars, repo_model.GetStargazers, tplCards)
1138+
ctx.Data["Title"] = ctx.Tr("repo.stargazers")
1139+
ctx.Data["CardsTitle"] = ctx.Tr("repo.stargazers")
1140+
ctx.Data["PageIsStargazers"] = true
1141+
RenderUserCards(ctx, ctx.Repo.Repository.NumStars, func(opts db.ListOptions) ([]*user_model.User, error) {
1142+
return repo_model.GetStargazers(ctx, ctx.Repo.Repository, opts)
1143+
}, tplWatchers)
11461144
}
11471145

11481146
// Forks render repository's forked users

routers/web/web.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,9 +1599,7 @@ func registerRoutes(m *web.Router) {
15991599

16001600
m.Group("/{username}/{reponame}", func() {
16011601
m.Get("/stars", repo.Stars)
1602-
m.Get("/stars/cards", repo.StarsCards)
16031602
m.Get("/watchers", repo.Watchers)
1604-
m.Get("/watchers/cards", repo.WatchersCards)
16051603
m.Get("/search", reqRepoCodeReader, repo.Search)
16061604
m.Post("/action/{action}", reqSignIn, repo.Action)
16071605
}, optSignIn, context.RepoAssignment, context.RepoRef())

templates/repo/user_cards.tmpl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
<div class="user-cards">
1+
<!-- Refresh the content if a htmx response contains "HX-Trigger" header.
2+
This usually happens when a user stays on the watchers/stargazers page
3+
when they watched/unwatched/starred/unstarred and the list should be refreshed.
4+
To test go to the watchers page and click the watch button. The user cards should reload.
5+
At the moment, no JS initialization would re-trigger (fortunately there is no JS for this page).
6+
-->
7+
<div class="no-loading-indicator tw-hidden"></div>
8+
<div class="user-cards"
9+
hx-trigger="refreshUserCards from:body"
10+
hx-indicator=".no-loading-indicator"
11+
hx-get="{{$.Link}}"
12+
hx-swap="innerHTML" hx-select=".user-cards"
13+
>
214
{{if .CardsTitle}}
315
<h2 class="ui dividing header">
416
{{.CardsTitle}}

templates/repo/watchers.tmpl

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
{{template "base/head" .}}
22
<div role="main" aria-label="{{.Title}}" class="page-content repository watchers">
33
{{template "repo/header" .}}
4-
<div class="no-loading-indicator tw-hidden"></div>
5-
<!-- this element reloads its content with htmx as soon as a response from the backend
6-
includes the header "HX-Trigger: refreshCards". This usually happens when a user
7-
watched/unwatched/starred/unstarred and we want their user card to appear/disappear.
8-
To test go to the watchers page and click the watch button. The user cards should reload. -->
9-
<div
10-
hx-trigger="refreshCards from:body"
11-
hx-indicator=".no-loading-indicator"
12-
hx-swap="innerHTML"
13-
hx-get="{{$.Link}}/cards"
14-
class="ui container"
15-
>
4+
<div class="ui container">
165
{{template "repo/user_cards" .}}
176
</div>
187
</div>

0 commit comments

Comments
 (0)