Skip to content

Commit 4f633ca

Browse files
committed
Add comments and reduce abstraction levels
1 parent bb88042 commit 4f633ca

File tree

6 files changed

+21
-31
lines changed

6 files changed

+21
-31
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, repo *Repository, opts db.ListOptions) ([]*user_model.User, error) {
80-
sess := db.GetEngine(ctx).Where("star.repo_id = ?", repo.ID).
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).
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, db.ListOptions{Page: 0})
42+
gazers, err := repo_model.GetStargazers(db.DefaultContext, repo.ID, 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, db.ListOptions{Page: 0})
53+
gazers, err := repo_model.GetStargazers(db.DefaultContext, repo.ID, 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, db.ListOptions{Page: 0})
72+
gazers, err := repo_model.GetStargazers(db.DefaultContext, repo.ID, 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, utils.GetListOptions(ctx))
48+
stargazers, err := repo_model.GetStargazers(ctx, ctx.Repo.Repository.ID, utils.GetListOptions(ctx))
4949
if err != nil {
5050
ctx.Error(http.StatusInternalServerError, "GetStargazers", err)
5151
return

routers/web/repo/repo.go

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

355-
// if we have user cards on the page we should refresh them
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.
356359
ctx.RespHeader().Add("HX-Trigger", "refreshCards")
357360

358361
switch ctx.PathParam(":action") {

routers/web/repo/view.go

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,15 +1102,17 @@ func checkOutdatedBranch(ctx *context.Context) {
11021102
}
11031103

11041104
// RenderUserCards render a page show users according the input template
1105-
func RenderUserCards(ctx *context.Context, total int, getter func(opts db.ListOptions) ([]*user_model.User, error), tpl base.TplName) {
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)
11061108
page := ctx.FormInt("page")
11071109
if page <= 0 {
11081110
page = 1
11091111
}
11101112
pager := context.NewPagination(total, setting.ItemsPerPage, page, 5)
11111113
ctx.Data["Page"] = pager
11121114

1113-
items, err := getter(db.ListOptions{
1115+
items, err := getter(ctx, ctx.Repo.Repository.ID, db.ListOptions{
11141116
Page: pager.Paginater.Current(),
11151117
PageSize: setting.ItemsPerPage,
11161118
})
@@ -1123,42 +1125,24 @@ func RenderUserCards(ctx *context.Context, total int, getter func(opts db.ListOp
11231125
ctx.HTML(http.StatusOK, tpl)
11241126
}
11251127

1126-
func renderUserList(ctx *context.Context, pageType string, total int, getter func(opts db.ListOptions) ([]*user_model.User, error), tpl base.TplName) {
1127-
ctx.Data["Title"] = ctx.Tr(pageType)
1128-
ctx.Data["CardsTitle"] = ctx.Tr(pageType)
1129-
RenderUserCards(ctx, total, getter, tpl)
1130-
}
1131-
11321128
// Watchers render repository's watch users
11331129
func Watchers(ctx *context.Context) {
1134-
renderUserList(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches,
1135-
func(opts db.ListOptions) ([]*user_model.User, error) {
1136-
return repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, opts)
1137-
}, tplWatchers)
1130+
RenderUserCards(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches, repo_model.GetRepoWatchers, tplWatchers)
11381131
}
11391132

11401133
// WatchersCards renders a repository's watchers user cards
11411134
func WatchersCards(ctx *context.Context) {
1142-
renderUserList(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches,
1143-
func(opts db.ListOptions) ([]*user_model.User, error) {
1144-
return repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, opts)
1145-
}, tplCards)
1135+
RenderUserCards(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches, repo_model.GetRepoWatchers, tplCards)
11461136
}
11471137

11481138
// Stars render repository's starred users
11491139
func Stars(ctx *context.Context) {
1150-
renderUserList(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars,
1151-
func(opts db.ListOptions) ([]*user_model.User, error) {
1152-
return repo_model.GetStargazers(ctx, ctx.Repo.Repository, opts)
1153-
}, tplWatchers)
1140+
RenderUserCards(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars, repo_model.GetStargazers, tplWatchers)
11541141
}
11551142

11561143
// StarsCards renders a repository's stargazers user cards
11571144
func StarsCards(ctx *context.Context) {
1158-
renderUserList(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars,
1159-
func(opts db.ListOptions) ([]*user_model.User, error) {
1160-
return repo_model.GetStargazers(ctx, ctx.Repo.Repository, opts)
1161-
}, tplCards)
1145+
RenderUserCards(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars, repo_model.GetStargazers, tplCards)
11621146
}
11631147

11641148
// Forks render repository's forked users

templates/repo/watchers.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
<div role="main" aria-label="{{.Title}}" class="page-content repository watchers">
33
{{template "repo/header" .}}
44
<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 -->
58
<div
69
hx-trigger="refreshCards from:body"
710
hx-indicator=".no-loading-indicator"

0 commit comments

Comments
 (0)