2016-12-29 17:58:24 +03:00
// 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 {
2019-01-14 05:29:58 +03:00
if isStaring ( sess , userID , repoID ) {
2016-12-29 17:58:24 +03:00
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 {
2019-01-14 05:29:58 +03:00
if ! isStaring ( sess , userID , repoID ) {
2016-12-29 17:58:24 +03:00
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 {
2019-01-14 05:29:58 +03:00
return isStaring ( x , userID , repoID )
}
func isStaring ( e Engine , userID , repoID int64 ) bool {
has , _ := e . Get ( & Star { 0 , userID , repoID } )
2016-12-29 17:58:24 +03:00
return has
}
// GetStargazers returns the users that starred the repo.
2020-01-24 22:00:29 +03:00
func ( repo * Repository ) GetStargazers ( opts ListOptions ) ( [ ] * User , error ) {
2017-01-06 10:05:09 +03:00
sess := x . Where ( "star.repo_id = ?" , repo . ID ) .
Join ( "LEFT" , "star" , "`user`.id = star.uid" )
2020-01-24 22:00:29 +03:00
if opts . Page > 0 {
sess = opts . setSessionPagination ( sess )
users := make ( [ ] * User , 0 , opts . PageSize )
return users , sess . Find ( & users )
2017-01-06 10:05:09 +03:00
}
2020-01-24 22:00:29 +03:00
users := make ( [ ] * User , 0 , 8 )
2017-01-06 10:05:09 +03:00
return users , sess . Find ( & users )
2016-12-29 17:58:24 +03:00
}
// GetStarredRepos returns the repos the user starred.
2017-02-15 09:01:50 +03:00
func ( u * User ) GetStarredRepos ( private bool , page , pageSize int , orderBy string ) ( repos RepositoryList , err error ) {
2017-02-07 14:54:16 +03:00
if len ( orderBy ) == 0 {
2017-02-14 10:28:22 +03:00
orderBy = "updated_unix DESC"
2017-02-07 14:54:16 +03:00
}
2016-12-29 17:58:24 +03:00
sess := x .
Join ( "INNER" , "star" , "star.repo_id = repository.id" ) .
2017-02-03 07:03:49 +03:00
Where ( "star.uid = ?" , u . ID ) .
2017-02-14 10:28:22 +03:00
OrderBy ( orderBy )
2016-12-29 17:58:24 +03:00
if ! private {
sess = sess . And ( "is_private = ?" , false )
}
2017-02-07 14:54:16 +03:00
if page <= 0 {
page = 1
}
sess . Limit ( pageSize , ( page - 1 ) * pageSize )
repos = make ( [ ] * Repository , 0 , pageSize )
2017-01-24 05:39:58 +03:00
if err = sess . Find ( & repos ) ; err != nil {
return
}
2017-02-15 09:01:50 +03:00
if err = repos . loadAttributes ( x ) ; err != nil {
return
2017-01-24 05:39:58 +03:00
}
2016-12-29 17:58:24 +03:00
return
}
2017-02-07 14:54:16 +03:00
// GetStarredRepoCount returns the numbers of repo the user starred.
func ( u * User ) GetStarredRepoCount ( private bool ) ( int64 , error ) {
sess := x .
Join ( "INNER" , "star" , "star.repo_id = repository.id" ) .
Where ( "star.uid = ?" , u . ID )
if ! private {
sess = sess . And ( "is_private = ?" , false )
}
return sess . Count ( & Repository { } )
}