mirror of
https://github.com/go-gitea/gitea
synced 2024-11-24 23:22:45 +01:00
c337ff0ec7
Fixes #17453 This PR adds the abbility to block a user from a personal account or organization to restrict how the blocked user can interact with the blocker. The docs explain what's the consequence of blocking a user. Screenshots: ![grafik](https://github.com/go-gitea/gitea/assets/1666336/4ed884f3-e06a-4862-afd3-3b8aa2488dc6) ![grafik](https://github.com/go-gitea/gitea/assets/1666336/ae6d4981-f252-4f50-a429-04f0f9f1cdf1) ![grafik](https://github.com/go-gitea/gitea/assets/1666336/ca153599-5b0f-4b4a-90fe-18bdfd6f0b6b) --------- Co-authored-by: Lauris BH <lauris@nix.lv>
105 lines
3 KiB
Go
105 lines
3 KiB
Go
// Copyright 2016 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repo
|
|
|
|
import (
|
|
"context"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
)
|
|
|
|
// 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)"`
|
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
|
}
|
|
|
|
func init() {
|
|
db.RegisterModel(new(Star))
|
|
}
|
|
|
|
// StarRepo or unstar repository.
|
|
func StarRepo(ctx context.Context, doer *user_model.User, repo *Repository, star bool) error {
|
|
ctx, committer, err := db.TxContext(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer committer.Close()
|
|
staring := IsStaring(ctx, doer.ID, repo.ID)
|
|
|
|
if star {
|
|
if user_model.IsUserBlockedBy(ctx, doer, repo.OwnerID) {
|
|
return user_model.ErrBlockedUser
|
|
}
|
|
|
|
if staring {
|
|
return nil
|
|
}
|
|
|
|
if err := db.Insert(ctx, &Star{UID: doer.ID, RepoID: repo.ID}); err != nil {
|
|
return err
|
|
}
|
|
if _, err := db.Exec(ctx, "UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repo.ID); err != nil {
|
|
return err
|
|
}
|
|
if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", doer.ID); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
if !staring {
|
|
return nil
|
|
}
|
|
|
|
if _, err := db.DeleteByBean(ctx, &Star{UID: doer.ID, RepoID: repo.ID}); err != nil {
|
|
return err
|
|
}
|
|
if _, err := db.Exec(ctx, "UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repo.ID); err != nil {
|
|
return err
|
|
}
|
|
if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", doer.ID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return committer.Commit()
|
|
}
|
|
|
|
// IsStaring checks if user has starred given repository.
|
|
func IsStaring(ctx context.Context, userID, repoID int64) bool {
|
|
has, _ := db.GetEngine(ctx).Get(&Star{UID: userID, RepoID: repoID})
|
|
return has
|
|
}
|
|
|
|
// GetStargazers returns the users that starred the repo.
|
|
func GetStargazers(ctx context.Context, repo *Repository, opts db.ListOptions) ([]*user_model.User, error) {
|
|
sess := db.GetEngine(ctx).Where("star.repo_id = ?", repo.ID).
|
|
Join("LEFT", "star", "`user`.id = star.uid")
|
|
if opts.Page > 0 {
|
|
sess = db.SetSessionPagination(sess, &opts)
|
|
|
|
users := make([]*user_model.User, 0, opts.PageSize)
|
|
return users, sess.Find(&users)
|
|
}
|
|
|
|
users := make([]*user_model.User, 0, 8)
|
|
return users, sess.Find(&users)
|
|
}
|
|
|
|
// ClearRepoStars clears all stars for a repository and from the user that starred it.
|
|
// Used when a repository is set to private.
|
|
func ClearRepoStars(ctx context.Context, repoID int64) error {
|
|
if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars=num_stars-1 WHERE id IN (SELECT `uid` FROM `star` WHERE repo_id = ?)", repoID); err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err := db.Exec(ctx, "UPDATE `repository` SET num_stars = 0 WHERE id = ?", repoID); err != nil {
|
|
return err
|
|
}
|
|
|
|
return db.DeleteBeans(ctx, Star{RepoID: repoID})
|
|
}
|