Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Tab on user profile to show starred repos
  • Loading branch information
andreynering committed Nov 20, 2016
commit f8582d61908c4d26efb2aa4c9eeb0729615abb44
1 change: 0 additions & 1 deletion cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ func runWeb(ctx *cli.Context) error {
m.Get("", user.Profile)
m.Get("/followers", user.Followers)
m.Get("/following", user.Following)
m.Get("/stars", user.Stars)
})

m.Get("/attachments/:uuid", func(ctx *context.Context) {
Expand Down
1 change: 1 addition & 0 deletions conf/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ admin_panel = Admin Panel
account_settings = Account Settings
settings = Settings
your_profile = Your Profile
your_starred = Your starred
your_settings = Your Settings

activities = Activities
Expand Down
58 changes: 0 additions & 58 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1986,64 +1986,6 @@ func NotifyWatchers(act *Action) error {
return notifyWatchers(x, act)
}

// _________ __
// / _____// |______ _______
// \_____ \\ __\__ \\_ __ \
// / \| | / __ \| | \/
// /_______ /|__| (____ /__|
// \/ \/

type Star struct {
ID int64 `xorm:"pk autoincr"`
UID int64 `xorm:"UNIQUE(s)"`
RepoID int64 `xorm:"UNIQUE(s)"`
}

// Star or unstar repository.
func StarRepo(userID, repoID int64, star bool) (err error) {
if star {
if IsStaring(userID, repoID) {
return nil
}
if _, err = x.Insert(&Star{UID: userID, RepoID: repoID}); err != nil {
return err
} else if _, err = x.Exec("UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoID); err != nil {
return err
}
_, err = x.Exec("UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID)
} else {
if !IsStaring(userID, repoID) {
return nil
}
if _, err = x.Delete(&Star{0, userID, repoID}); err != nil {
return err
} else if _, err = x.Exec("UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil {
return err
}
_, err = x.Exec("UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", userID)
}
return err
}

// IsStaring checks if user has starred given repository.
func IsStaring(userID, repoID int64) bool {
has, _ := x.Get(&Star{0, userID, repoID})
return has
}

func (repo *Repository) GetStargazers(page int) ([]*User, error) {
users := make([]*User, 0, ItemsPerPage)
sess := x.
Limit(ItemsPerPage, (page-1)*ItemsPerPage).
Where("star.repo_id=?", repo.ID)
if setting.UsePostgreSQL {
sess = sess.Join("LEFT", "star", `"user".id=star.uid`)
} else {
sess = sess.Join("LEFT", "star", "user.id=star.uid")
}
return users, sess.Find(&users)
}

// ___________ __
// \_ _____/__________| | __
// | __)/ _ \_ __ \ |/ /
Expand Down
70 changes: 70 additions & 0 deletions models/star.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package models

import (
"code.gitea.io/gitea/modules/setting"
)

type Star struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

golint error

ID int64 `xorm:"pk autoincr"`
UID int64 `xorm:"UNIQUE(s)"`
RepoID int64 `xorm:"UNIQUE(s)"`
}

// Star or unstar repository.
func StarRepo(userID, repoID int64, star bool) (err error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

golint error

if star {
if IsStaring(userID, repoID) {
return nil
}
if _, err = x.Insert(&Star{UID: userID, RepoID: repoID}); err != nil {
return err
} else if _, err = x.Exec("UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoID); err != nil {
return err
}
_, err = x.Exec("UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID)
} else {
if !IsStaring(userID, repoID) {
return nil
}
if _, err = x.Delete(&Star{0, userID, repoID}); err != nil {
return err
} else if _, err = x.Exec("UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil {
return err
}
_, err = x.Exec("UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", userID)
}
return err
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all the SQL should be executed in one transaction.


// IsStaring checks if user has starred given repository.
func IsStaring(userID, repoID int64) bool {
has, _ := x.Get(&Star{0, userID, repoID})
return has
}

func (repo *Repository) GetStargazers(page int) ([]*User, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

golint

users := make([]*User, 0, ItemsPerPage)
sess := x.
Limit(ItemsPerPage, (page-1)*ItemsPerPage).
Where("star.repo_id = ?", repo.ID)
if setting.UsePostgreSQL {
sess = sess.Join("LEFT", "star", `"user".id = star.uid`)
} else {
sess = sess.Join("LEFT", "star", "user.id = star.uid")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use

sess.Join("LEFT", "star", "`user`.id = star.uid")

is OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lunny Both of your suggestions was not in code I touched. I just moved it from another file. I only wrote the last function.

Should I fix it in this PR or another one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both are OK.

}
return users, sess.Find(&users)
}

func (u *User) GetStarredRepos(private bool) (repos []*Repository, err error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

golint

sess := x.
Join("INNER", "star", "star.repo_id = repository.id").
Where("star.uid = ?", u.ID)

if !private {
sess = sess.And("is_private = ?", false)
}

err = sess.
Find(&repos)
return
}
13 changes: 8 additions & 5 deletions routers/user/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ func Profile(ctx *context.Context) {
if ctx.Written() {
return
}
case "stars":
showPrivateRepos := ctx.IsSigned && ctx.User.ID == ctxUser.ID
starredRepos, err := ctxUser.GetStarredRepos(showPrivateRepos)
if err != nil {
ctx.Handle(500, "GetStarredRepos", err)
return
}
ctx.Data["Repos"] = starredRepos
default:
page := ctx.QueryInt("page")
if page <= 0 {
Expand Down Expand Up @@ -138,11 +146,6 @@ func Following(ctx *context.Context) {
repo.RenderUserCards(ctx, u.NumFollowing, u.GetFollowing, tplFollowers)
}

// Stars show repositories user starred
func Stars(ctx *context.Context) {

}

// Action response for follow/unfollow user request
func Action(ctx *context.Context) {
u := GetUserByParams(ctx)
Expand Down
4 changes: 4 additions & 0 deletions templates/base/head.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@
<i class="octicon octicon-person"></i>
{{.i18n.Tr "your_profile"}}<!-- Your profile -->
</a>
<a class="item" href="{{AppSubUrl}}/{{.SignedUser.Name}}?tab=stars">
<i class="octicon octicon-star"></i>
{{.i18n.Tr "your_starred"}}
</a>
<a class="{{if .PageIsUserSettings}}active{{end}} item" href="{{AppSubUrl}}/user/settings">
<i class="octicon octicon-settings"></i>
{{.i18n.Tr "your_settings"}}<!-- Your settings -->
Expand Down
25 changes: 15 additions & 10 deletions templates/user/profile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,28 @@
</div>
<div class="ui eleven wide column">
<div class="ui secondary pointing menu">
<a class="{{if ne .TabName "activity"}}active{{end}} item" href="{{.Owner.HomeLink}}">
<a class='{{if and (ne .TabName "activity") (ne .TabName "stars")}}active{{end}} item' href="{{.Owner.HomeLink}}">
<i class="octicon octicon-repo"></i> {{.i18n.Tr "user.repositories"}}
</a>
<a class="item">
<a class="{{if eq .TabName "activity"}}active{{end}} item" href="{{.Owner.HomeLink}}?tab=activity">
<i class="octicon octicon-rss"></i> {{.i18n.Tr "user.activity"}}
</a>
<a class='{{if eq .TabName "activity"}}active{{end}} item' href="{{.Owner.HomeLink}}?tab=activity">
<i class="octicon octicon-rss"></i> {{.i18n.Tr "user.activity"}}
</a>
<a class='{{if eq .TabName "stars"}}active{{end}} item' href="{{.Owner.HomeLink}}?tab=stars">
<i class="octicon octicon-star"></i> {{.i18n.Tr "user.starred"}}
</a>
</div>
{{if ne .TabName "activity"}}
{{template "explore/repo_list" .}}
{{template "base/paginate" .}}
{{else}}
<br>

{{if eq .TabName "activity"}}
<div class="feeds">
{{template "user/dashboard/feeds" .}}
</div>
{{else if eq .TabName "stars"}}
<div class="stars">
{{template "explore/repo_list" .}}
</div>
{{else}}
{{template "explore/repo_list" .}}
{{template "base/paginate" .}}
{{end}}
</div>
</div>
Expand Down