From b9928588834a9aad1771ba79f1d6638cdc29904a Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Thu, 29 Dec 2016 12:58:24 -0200 Subject: [PATCH] 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 --- cmd/web.go | 1 - models/repo.go | 60 ----------------------- models/star.go | 87 +++++++++++++++++++++++++++++++++ options/locale/locale_en-US.ini | 1 + routers/user/profile.go | 13 +++-- templates/base/head.tmpl | 4 ++ templates/user/profile.tmpl | 25 ++++++---- 7 files changed, 115 insertions(+), 76 deletions(-) create mode 100644 models/star.go diff --git a/cmd/web.go b/cmd/web.go index 45d198fdfb..7dfd19d640 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -273,7 +273,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) { diff --git a/models/repo.go b/models/repo.go index 33d84642df..ce9740f8c4 100644 --- a/models/repo.go +++ b/models/repo.go @@ -2146,66 +2146,6 @@ func NotifyWatchers(act *Action) error { return notifyWatchers(x, act) } -// _________ __ -// / _____// |______ _______ -// \_____ \\ __\__ \\_ __ \ -// / \| | / __ \| | \/ -// /_______ /|__| (____ /__| -// \/ \/ - -// Star contains the star information -type Star struct { - ID int64 `xorm:"pk autoincr"` - UID int64 `xorm:"UNIQUE(s)"` - RepoID int64 `xorm:"UNIQUE(s)"` -} - -// StarRepo 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 -} - -// GetStargazers returns the users who gave stars to this repository -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) -} - // ___________ __ // \_ _____/__________| | __ // | __)/ _ \_ __ \ |/ / diff --git a/models/star.go b/models/star.go new file mode 100644 index 0000000000..e2feb2c4b6 --- /dev/null +++ b/models/star.go @@ -0,0 +1,87 @@ +// Copyright 2016 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package models + +// Star represents a starred repo by an user. +type Star struct { + ID int64 `xorm:"pk autoincr"` + UID int64 `xorm:"UNIQUE(s)"` + RepoID int64 `xorm:"UNIQUE(s)"` +} + +// StarRepo or unstar repository. +func StarRepo(userID, repoID int64, star bool) error { + sess := x.NewSession() + + defer sess.Close() + + if err := sess.Begin(); err != nil { + return err + } + + if star { + if IsStaring(userID, repoID) { + return nil + } + + if _, err := sess.Insert(&Star{UID: userID, RepoID: repoID}); err != nil { + return err + } + if _, err := sess.Exec("UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoID); err != nil { + return err + } + if _, err := sess.Exec("UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID); err != nil { + return err + } + } else { + if !IsStaring(userID, repoID) { + return nil + } + + if _, err := sess.Delete(&Star{0, userID, repoID}); err != nil { + return err + } + if _, err := sess.Exec("UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil { + return err + } + if _, err := sess.Exec("UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", userID); err != nil { + return err + } + } + + return sess.Commit() +} + +// IsStaring checks if user has starred given repository. +func IsStaring(userID, repoID int64) bool { + has, _ := x.Get(&Star{0, userID, repoID}) + return has +} + +// GetStargazers returns the users that starred the repo. +func (repo *Repository) GetStargazers(page int) ([]*User, error) { + users := make([]*User, 0, ItemsPerPage) + err := x. + Limit(ItemsPerPage, (page-1)*ItemsPerPage). + Where("star.repo_id = ?", repo.ID). + Join("LEFT", "star", "`user`.id = star.uid"). + Find(&users) + return users, err +} + +// GetStarredRepos returns the repos the user starred. +func (u *User) GetStarredRepos(private bool) (repos []*Repository, err error) { + 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 +} diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index d689a92a3c..da2db622c5 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -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 diff --git a/routers/user/profile.go b/routers/user/profile.go index a5f4a97efd..f72ced7b3c 100644 --- a/routers/user/profile.go +++ b/routers/user/profile.go @@ -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 { @@ -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) diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl index a114b8dac5..fe14742510 100644 --- a/templates/base/head.tmpl +++ b/templates/base/head.tmpl @@ -116,6 +116,10 @@ {{.i18n.Tr "your_profile"}} + + + {{.i18n.Tr "your_starred"}} + {{.i18n.Tr "your_settings"}} diff --git a/templates/user/profile.tmpl b/templates/user/profile.tmpl index 7734d94c99..cfb646ace1 100644 --- a/templates/user/profile.tmpl +++ b/templates/user/profile.tmpl @@ -75,23 +75,28 @@
- {{if ne .TabName "activity"}} - {{template "explore/repo_list" .}} - {{template "base/paginate" .}} - {{else}} -
+ + {{if eq .TabName "activity"}}
{{template "user/dashboard/feeds" .}}
+ {{else if eq .TabName "stars"}} +
+ {{template "explore/repo_list" .}} +
+ {{else}} + {{template "explore/repo_list" .}} + {{template "base/paginate" .}} {{end}}