Skip to content

Commit

Permalink
simpilfy
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Nov 21, 2024
1 parent 92bc292 commit da86679
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 42 deletions.
4 changes: 2 additions & 2 deletions models/repo/star.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions models/repo/star_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion routers/api/v1/repo/star.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions routers/web/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
30 changes: 14 additions & 16 deletions routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -1102,17 +1101,15 @@ 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
}
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,
})
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
14 changes: 13 additions & 1 deletion templates/repo/user_cards.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
<div class="user-cards">
<!-- Refresh the content if a htmx response contains "HX-Trigger" header.
This usually happens when a user stays on the watchers/stargazers page
when they watched/unwatched/starred/unstarred and the list should be refreshed.
To test go to the watchers page and click the watch button. The user cards should reload.
At the moment, no JS initialization would re-trigger (fortunately there is no JS for this page).
-->
<div class="no-loading-indicator tw-hidden"></div>
<div class="user-cards"
hx-trigger="refreshUserCards from:body"
hx-indicator=".no-loading-indicator"
hx-get="{{$.Link}}"
hx-swap="innerHTML" hx-select=".user-cards"
>
{{if .CardsTitle}}
<h2 class="ui dividing header">
{{.CardsTitle}}
Expand Down
13 changes: 1 addition & 12 deletions templates/repo/watchers.tmpl
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
{{template "base/head" .}}
<div role="main" aria-label="{{.Title}}" class="page-content repository watchers">
{{template "repo/header" .}}
<div class="no-loading-indicator tw-hidden"></div>
<!-- this element reloads its content with htmx as soon as a response from the backend
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. -->
<div
hx-trigger="refreshCards from:body"
hx-indicator=".no-loading-indicator"
hx-swap="innerHTML"
hx-get="{{$.Link}}/cards"
class="ui container"
>
<div class="ui container">
{{template "repo/user_cards" .}}
</div>
</div>
Expand Down

0 comments on commit da86679

Please sign in to comment.