Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
yp05327 committed May 25, 2023
1 parent cc750a9 commit 8645c5c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 44 deletions.
65 changes: 24 additions & 41 deletions models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"code.gitea.io/gitea/modules/validation"

"xorm.io/builder"
"xorm.io/xorm"
)

// UserType defines the user type
Expand Down Expand Up @@ -309,14 +310,27 @@ func (u *User) GenerateEmailActivateCode(email string) string {
return code
}

// GetUserFollowers returns range of user's followers.
func GetUserFollowers(ctx context.Context, u, viewer *User, listOptions db.ListOptions) ([]*User, int64, error) {
sess := db.GetEngine(ctx).
func searchUserFollowers(ctx context.Context, u, viewer *User) *xorm.Session {
return db.GetEngine(ctx).
Select("`user`.*").
Join("LEFT", "follow", "`user`.id=follow.user_id").
Where("follow.follow_id=?", u.ID).
And("`user`.type=?", UserTypeIndividual).
And(isUserVisibleToViewerCond(viewer))
}

func searchUserFollowing(ctx context.Context, u, viewer *User) *xorm.Session {
return db.GetEngine(db.DefaultContext).
Select("`user`.*").
Join("LEFT", "follow", "`user`.id=follow.follow_id").
Where("follow.user_id=?", u.ID).
And("`user`.type IN (?, ?)", UserTypeIndividual, UserTypeOrganization).
And(isUserVisibleToViewerCond(viewer))
}

// GetUserFollowers returns range of user's followers.
func GetUserFollowers(ctx context.Context, u, viewer *User, listOptions db.ListOptions) ([]*User, int64, error) {
sess := searchUserFollowers(ctx, u, viewer)

if listOptions.Page != 0 {
sess = db.SetSessionPagination(sess, &listOptions)
Expand All @@ -333,12 +347,7 @@ func GetUserFollowers(ctx context.Context, u, viewer *User, listOptions db.ListO

// GetUserFollowing returns range of user's following.
func GetUserFollowing(ctx context.Context, u, viewer *User, listOptions db.ListOptions) ([]*User, int64, error) {
sess := db.GetEngine(db.DefaultContext).
Select("`user`.*").
Join("LEFT", "follow", "`user`.id=follow.follow_id").
Where("follow.user_id=?", u.ID).
And("`user`.type IN (?, ?)", UserTypeIndividual, UserTypeOrganization).
And(isUserVisibleToViewerCond(viewer))
sess := searchUserFollowing(ctx, u, viewer)

if listOptions.Page != 0 {
sess = db.SetSessionPagination(sess, &listOptions)
Expand All @@ -354,41 +363,15 @@ func GetUserFollowing(ctx context.Context, u, viewer *User, listOptions db.ListO
}

// GetUserFollowersCount returns count of user's followers.
func GetUserFollowersCount(ctx context.Context, u, viewer *User, listOptions db.ListOptions) (int64, error) {
sess := db.GetEngine(ctx).
Select("`user`.*").
Join("LEFT", "follow", "`user`.id=follow.user_id").
Where("follow.follow_id=?", u.ID).
And("`user`.type=?", UserTypeIndividual).
And(isUserVisibleToViewerCond(viewer))

if listOptions.Page != 0 {
sess = db.SetSessionPagination(sess, &listOptions)
count, err := sess.Count()
return count, err
}

count, err := sess.Count()
return count, err
func GetUserFollowersCount(ctx context.Context, u, viewer *User) (int64, error) {
sess := searchUserFollowers(ctx, u, viewer)
return sess.Count()
}

// GetUserFollowingCount returns count of user's following.
func GetUserFollowingCount(ctx context.Context, u, viewer *User, listOptions db.ListOptions) (int64, error) {
sess := db.GetEngine(db.DefaultContext).
Select("`user`.*").
Join("LEFT", "follow", "`user`.id=follow.follow_id").
Where("follow.user_id=?", u.ID).
And("`user`.type IN (?, ?)", UserTypeIndividual, UserTypeOrganization).
And(isUserVisibleToViewerCond(viewer))

if listOptions.Page != 0 {
sess = db.SetSessionPagination(sess, &listOptions)
count, err := sess.Count()
return count, err
}

count, err := sess.Count()
return count, err
func GetUserFollowingCount(ctx context.Context, u, viewer *User) (int64, error) {
sess := searchUserFollowing(ctx, u, viewer)
return sess.Count()
}

// NewGitSig generates and returns the signature of given user.
Expand Down
5 changes: 2 additions & 3 deletions services/convert/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package convert
import (
"context"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/perm"
user_model "code.gitea.io/gitea/models/user"
api "code.gitea.io/gitea/modules/structs"
Expand Down Expand Up @@ -65,12 +64,12 @@ func toUser(ctx context.Context, user, doer *user_model.User, accessMode perm.Ac
signed = true
authed = doer.ID == user.ID || doer.IsAdmin

count, err := user_model.GetUserFollowersCount(ctx, user, doer, db.ListOptions{ListAll: true})
count, err := user_model.GetUserFollowersCount(ctx, user, doer)
if err != nil {
return nil
}
result.Followers = int(count)
count, err = user_model.GetUserFollowingCount(ctx, user, doer, db.ListOptions{ListAll: true})
count, err = user_model.GetUserFollowingCount(ctx, user, doer)
if err != nil {
return nil
}
Expand Down

0 comments on commit 8645c5c

Please sign in to comment.