Skip to content

Commit b992858

Browse files
andreyneringlunny
authored andcommitted
Tab on user profile to show starred repos (#519)
* Tab on user profile to show starred repos * Make golint happy and use transactions on StarRepo function * x -> sess * Use sess.Close() instead of sess.Rollback() * Add copyright * Fix lint
1 parent 2d1a1fc commit b992858

File tree

7 files changed

+115
-76
lines changed

7 files changed

+115
-76
lines changed

cmd/web.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ func runWeb(ctx *cli.Context) error {
273273
m.Get("", user.Profile)
274274
m.Get("/followers", user.Followers)
275275
m.Get("/following", user.Following)
276-
m.Get("/stars", user.Stars)
277276
})
278277

279278
m.Get("/attachments/:uuid", func(ctx *context.Context) {

models/repo.go

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,66 +2146,6 @@ func NotifyWatchers(act *Action) error {
21462146
return notifyWatchers(x, act)
21472147
}
21482148

2149-
// _________ __
2150-
// / _____// |______ _______
2151-
// \_____ \\ __\__ \\_ __ \
2152-
// / \| | / __ \| | \/
2153-
// /_______ /|__| (____ /__|
2154-
// \/ \/
2155-
2156-
// Star contains the star information
2157-
type Star struct {
2158-
ID int64 `xorm:"pk autoincr"`
2159-
UID int64 `xorm:"UNIQUE(s)"`
2160-
RepoID int64 `xorm:"UNIQUE(s)"`
2161-
}
2162-
2163-
// StarRepo star or unstar repository.
2164-
func StarRepo(userID, repoID int64, star bool) (err error) {
2165-
if star {
2166-
if IsStaring(userID, repoID) {
2167-
return nil
2168-
}
2169-
if _, err = x.Insert(&Star{UID: userID, RepoID: repoID}); err != nil {
2170-
return err
2171-
} else if _, err = x.Exec("UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoID); err != nil {
2172-
return err
2173-
}
2174-
_, err = x.Exec("UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID)
2175-
} else {
2176-
if !IsStaring(userID, repoID) {
2177-
return nil
2178-
}
2179-
if _, err = x.Delete(&Star{0, userID, repoID}); err != nil {
2180-
return err
2181-
} else if _, err = x.Exec("UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil {
2182-
return err
2183-
}
2184-
_, err = x.Exec("UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", userID)
2185-
}
2186-
return err
2187-
}
2188-
2189-
// IsStaring checks if user has starred given repository.
2190-
func IsStaring(userID, repoID int64) bool {
2191-
has, _ := x.Get(&Star{0, userID, repoID})
2192-
return has
2193-
}
2194-
2195-
// GetStargazers returns the users who gave stars to this repository
2196-
func (repo *Repository) GetStargazers(page int) ([]*User, error) {
2197-
users := make([]*User, 0, ItemsPerPage)
2198-
sess := x.
2199-
Limit(ItemsPerPage, (page-1)*ItemsPerPage).
2200-
Where("star.repo_id=?", repo.ID)
2201-
if setting.UsePostgreSQL {
2202-
sess = sess.Join("LEFT", "star", `"user".id=star.uid`)
2203-
} else {
2204-
sess = sess.Join("LEFT", "star", "user.id=star.uid")
2205-
}
2206-
return users, sess.Find(&users)
2207-
}
2208-
22092149
// ___________ __
22102150
// \_ _____/__________| | __
22112151
// | __)/ _ \_ __ \ |/ /

models/star.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2016 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package models
6+
7+
// Star represents a starred repo by an user.
8+
type Star struct {
9+
ID int64 `xorm:"pk autoincr"`
10+
UID int64 `xorm:"UNIQUE(s)"`
11+
RepoID int64 `xorm:"UNIQUE(s)"`
12+
}
13+
14+
// StarRepo or unstar repository.
15+
func StarRepo(userID, repoID int64, star bool) error {
16+
sess := x.NewSession()
17+
18+
defer sess.Close()
19+
20+
if err := sess.Begin(); err != nil {
21+
return err
22+
}
23+
24+
if star {
25+
if IsStaring(userID, repoID) {
26+
return nil
27+
}
28+
29+
if _, err := sess.Insert(&Star{UID: userID, RepoID: repoID}); err != nil {
30+
return err
31+
}
32+
if _, err := sess.Exec("UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoID); err != nil {
33+
return err
34+
}
35+
if _, err := sess.Exec("UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID); err != nil {
36+
return err
37+
}
38+
} else {
39+
if !IsStaring(userID, repoID) {
40+
return nil
41+
}
42+
43+
if _, err := sess.Delete(&Star{0, userID, repoID}); err != nil {
44+
return err
45+
}
46+
if _, err := sess.Exec("UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil {
47+
return err
48+
}
49+
if _, err := sess.Exec("UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", userID); err != nil {
50+
return err
51+
}
52+
}
53+
54+
return sess.Commit()
55+
}
56+
57+
// IsStaring checks if user has starred given repository.
58+
func IsStaring(userID, repoID int64) bool {
59+
has, _ := x.Get(&Star{0, userID, repoID})
60+
return has
61+
}
62+
63+
// GetStargazers returns the users that starred the repo.
64+
func (repo *Repository) GetStargazers(page int) ([]*User, error) {
65+
users := make([]*User, 0, ItemsPerPage)
66+
err := x.
67+
Limit(ItemsPerPage, (page-1)*ItemsPerPage).
68+
Where("star.repo_id = ?", repo.ID).
69+
Join("LEFT", "star", "`user`.id = star.uid").
70+
Find(&users)
71+
return users, err
72+
}
73+
74+
// GetStarredRepos returns the repos the user starred.
75+
func (u *User) GetStarredRepos(private bool) (repos []*Repository, err error) {
76+
sess := x.
77+
Join("INNER", "star", "star.repo_id = repository.id").
78+
Where("star.uid = ?", u.ID)
79+
80+
if !private {
81+
sess = sess.And("is_private = ?", false)
82+
}
83+
84+
err = sess.
85+
Find(&repos)
86+
return
87+
}

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ admin_panel = Admin Panel
3636
account_settings = Account Settings
3737
settings = Settings
3838
your_profile = Your Profile
39+
your_starred = Your starred
3940
your_settings = Your Settings
4041

4142
activities = Activities

routers/user/profile.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ func Profile(ctx *context.Context) {
9595
if ctx.Written() {
9696
return
9797
}
98+
case "stars":
99+
showPrivateRepos := ctx.IsSigned && ctx.User.ID == ctxUser.ID
100+
starredRepos, err := ctxUser.GetStarredRepos(showPrivateRepos)
101+
if err != nil {
102+
ctx.Handle(500, "GetStarredRepos", err)
103+
return
104+
}
105+
ctx.Data["Repos"] = starredRepos
98106
default:
99107
page := ctx.QueryInt("page")
100108
if page <= 0 {
@@ -138,11 +146,6 @@ func Following(ctx *context.Context) {
138146
repo.RenderUserCards(ctx, u.NumFollowing, u.GetFollowing, tplFollowers)
139147
}
140148

141-
// Stars show repositories user starred
142-
func Stars(ctx *context.Context) {
143-
144-
}
145-
146149
// Action response for follow/unfollow user request
147150
func Action(ctx *context.Context) {
148151
u := GetUserByParams(ctx)

templates/base/head.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@
116116
<i class="octicon octicon-person"></i>
117117
{{.i18n.Tr "your_profile"}}<!-- Your profile -->
118118
</a>
119+
<a class="item" href="{{AppSubUrl}}/{{.SignedUser.Name}}?tab=stars">
120+
<i class="octicon octicon-star"></i>
121+
{{.i18n.Tr "your_starred"}}
122+
</a>
119123
<a class="{{if .PageIsUserSettings}}active{{end}} item" href="{{AppSubUrl}}/user/settings">
120124
<i class="octicon octicon-settings"></i>
121125
{{.i18n.Tr "your_settings"}}<!-- Your settings -->

templates/user/profile.tmpl

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,28 @@
7575
</div>
7676
<div class="ui eleven wide column">
7777
<div class="ui secondary pointing menu">
78-
<a class="{{if ne .TabName "activity"}}active{{end}} item" href="{{.Owner.HomeLink}}">
78+
<a class='{{if and (ne .TabName "activity") (ne .TabName "stars")}}active{{end}} item' href="{{.Owner.HomeLink}}">
7979
<i class="octicon octicon-repo"></i> {{.i18n.Tr "user.repositories"}}
8080
</a>
81-
<a class="item">
82-
<a class="{{if eq .TabName "activity"}}active{{end}} item" href="{{.Owner.HomeLink}}?tab=activity">
83-
<i class="octicon octicon-rss"></i> {{.i18n.Tr "user.activity"}}
84-
</a>
81+
<a class='{{if eq .TabName "activity"}}active{{end}} item' href="{{.Owner.HomeLink}}?tab=activity">
82+
<i class="octicon octicon-rss"></i> {{.i18n.Tr "user.activity"}}
83+
</a>
84+
<a class='{{if eq .TabName "stars"}}active{{end}} item' href="{{.Owner.HomeLink}}?tab=stars">
85+
<i class="octicon octicon-star"></i> {{.i18n.Tr "user.starred"}}
8586
</a>
8687
</div>
87-
{{if ne .TabName "activity"}}
88-
{{template "explore/repo_list" .}}
89-
{{template "base/paginate" .}}
90-
{{else}}
91-
<br>
88+
89+
{{if eq .TabName "activity"}}
9290
<div class="feeds">
9391
{{template "user/dashboard/feeds" .}}
9492
</div>
93+
{{else if eq .TabName "stars"}}
94+
<div class="stars">
95+
{{template "explore/repo_list" .}}
96+
</div>
97+
{{else}}
98+
{{template "explore/repo_list" .}}
99+
{{template "base/paginate" .}}
95100
{{end}}
96101
</div>
97102
</div>

0 commit comments

Comments
 (0)