2016-12-29 15:58:24 +01:00
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
2024-03-28 07:44:22 +01:00
|
|
|
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2016-12-29 15:58:24 +01:00
|
|
|
|
2021-12-12 16:48:20 +01:00
|
|
|
package repo
|
2016-12-29 15:58:24 +01:00
|
|
|
|
2020-10-13 02:01:57 +02:00
|
|
|
import (
|
2022-05-20 16:08:52 +02:00
|
|
|
"context"
|
2024-03-27 19:56:32 +01:00
|
|
|
"time"
|
2022-05-20 16:08:52 +02:00
|
|
|
|
2021-09-19 13:49:59 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2024-03-26 14:02:30 +01:00
|
|
|
"code.gitea.io/gitea/models/forgefed"
|
2021-11-24 10:49:20 +01:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2024-03-26 14:02:30 +01:00
|
|
|
"code.gitea.io/gitea/modules/activitypub"
|
2024-03-26 15:28:36 +01:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-03-26 10:23:14 +01:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-10-13 02:01:57 +02:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
|
|
)
|
|
|
|
|
2016-12-29 15:58:24 +01:00
|
|
|
// Star represents a starred repo by an user.
|
|
|
|
type Star struct {
|
2020-10-13 02:01:57 +02:00
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
UID int64 `xorm:"UNIQUE(s)"`
|
|
|
|
RepoID int64 `xorm:"UNIQUE(s)"`
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
2016-12-29 15:58:24 +01:00
|
|
|
}
|
|
|
|
|
2021-09-19 13:49:59 +02:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(Star))
|
|
|
|
}
|
|
|
|
|
2023-09-15 08:13:19 +02:00
|
|
|
func StarRepo(ctx context.Context, userID, repoID int64, star bool) error {
|
2024-03-26 15:28:36 +01:00
|
|
|
if err := starLocalRepo(ctx, userID, repoID, star); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-26 16:21:28 +01:00
|
|
|
if star && setting.Federation.Enabled {
|
2024-03-26 15:28:36 +01:00
|
|
|
if err := sendLikeActivities(ctx, userID, repoID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StarRepo or unstar repository.
|
|
|
|
func starLocalRepo(ctx context.Context, userID, repoID int64, star bool) error {
|
2023-09-15 08:13:19 +02:00
|
|
|
ctx, committer, err := db.TxContext(ctx)
|
2021-11-21 16:41:00 +01:00
|
|
|
if err != nil {
|
2016-12-29 15:58:24 +01:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 16:41:00 +01:00
|
|
|
defer committer.Close()
|
2022-05-20 16:08:52 +02:00
|
|
|
staring := IsStaring(ctx, userID, repoID)
|
2016-12-29 15:58:24 +01:00
|
|
|
|
|
|
|
if star {
|
2021-11-21 16:41:00 +01:00
|
|
|
if staring {
|
2016-12-29 15:58:24 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-21 16:41:00 +01:00
|
|
|
if err := db.Insert(ctx, &Star{UID: userID, RepoID: repoID}); err != nil {
|
2016-12-29 15:58:24 +01:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 16:41:00 +01:00
|
|
|
if _, err := db.Exec(ctx, "UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoID); err != nil {
|
2016-12-29 15:58:24 +01:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 16:41:00 +01:00
|
|
|
if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID); err != nil {
|
2016-12-29 15:58:24 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-21 16:41:00 +01:00
|
|
|
if !staring {
|
2016-12-29 15:58:24 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-21 16:41:00 +01:00
|
|
|
if _, err := db.DeleteByBean(ctx, &Star{UID: userID, RepoID: repoID}); err != nil {
|
2016-12-29 15:58:24 +01:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 16:41:00 +01:00
|
|
|
if _, err := db.Exec(ctx, "UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil {
|
2016-12-29 15:58:24 +01:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 16:41:00 +01:00
|
|
|
if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", userID); err != nil {
|
2016-12-29 15:58:24 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 16:41:00 +01:00
|
|
|
return committer.Commit()
|
2016-12-29 15:58:24 +01:00
|
|
|
}
|
|
|
|
|
2024-03-26 15:28:36 +01:00
|
|
|
// ToDo: Move to federation service or simillar
|
|
|
|
func sendLikeActivities(ctx context.Context, userID int64, repoID int64) error {
|
2024-03-26 10:23:14 +01:00
|
|
|
|
2024-03-27 19:56:32 +01:00
|
|
|
// TODO: is user loading necessary here? - imho no!
|
2024-03-26 16:21:28 +01:00
|
|
|
log.Info("User ID: %v, Repo ID: %v", userID, repoID)
|
|
|
|
user, err := user_model.GetUserByID(ctx, userID)
|
|
|
|
log.Info("User is: %v", user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
federatedRepos, err := FindFederatedReposByRepoID(ctx, repoID)
|
|
|
|
log.Info("Federated Repos is: %v", federatedRepos)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-27 19:56:32 +01:00
|
|
|
// TODO: That's wrong - we've to send the activities to repo not to user!
|
2024-03-26 16:21:28 +01:00
|
|
|
apclient, err := activitypub.NewClient(ctx, user, user.APAPIURL())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, federatedRepo := range federatedRepos {
|
|
|
|
target := federatedRepo.Uri + "/inbox/" // A like goes to the inbox of the federated repo
|
|
|
|
log.Info("Federated Repo URI is: %v", target)
|
2024-03-27 19:56:32 +01:00
|
|
|
likeActivity, err := forgefed.NewForgeLike(user.APAPIURL(), target, time.Now())
|
2024-03-26 10:23:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-26 16:21:28 +01:00
|
|
|
log.Info("Like Activity: %v", likeActivity)
|
|
|
|
json, err := likeActivity.MarshalJSON()
|
2024-03-26 10:23:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-27 19:56:32 +01:00
|
|
|
// TODO: decouple loading & creating activities from sending them - use two loops.
|
2024-03-26 16:21:28 +01:00
|
|
|
// TODO: set timeouts for outgoing request in oder to mitigate DOS by slow lories
|
|
|
|
// TODO: Check if we need to respect rate limits
|
|
|
|
// ToDo: Change this to the standalone table of FederatedRepos
|
|
|
|
apclient.Post([]byte(json), target)
|
2024-03-26 10:23:14 +01:00
|
|
|
}
|
2024-03-26 16:21:28 +01:00
|
|
|
|
2024-03-26 10:23:14 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-29 15:58:24 +01:00
|
|
|
// IsStaring checks if user has starred given repository.
|
2022-05-20 16:08:52 +02:00
|
|
|
func IsStaring(ctx context.Context, userID, repoID int64) bool {
|
|
|
|
has, _ := db.GetEngine(ctx).Get(&Star{UID: userID, RepoID: repoID})
|
2016-12-29 15:58:24 +01:00
|
|
|
return has
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStargazers returns the users that starred the repo.
|
2023-09-15 08:13:19 +02:00
|
|
|
func GetStargazers(ctx context.Context, repo *Repository, opts db.ListOptions) ([]*user_model.User, error) {
|
|
|
|
sess := db.GetEngine(ctx).Where("star.repo_id = ?", repo.ID).
|
2017-01-06 08:05:09 +01:00
|
|
|
Join("LEFT", "star", "`user`.id = star.uid")
|
2020-01-24 20:00:29 +01:00
|
|
|
if opts.Page > 0 {
|
2021-09-24 13:32:56 +02:00
|
|
|
sess = db.SetSessionPagination(sess, &opts)
|
2020-01-24 20:00:29 +01:00
|
|
|
|
2021-11-24 10:49:20 +01:00
|
|
|
users := make([]*user_model.User, 0, opts.PageSize)
|
2020-01-24 20:00:29 +01:00
|
|
|
return users, sess.Find(&users)
|
2017-01-06 08:05:09 +01:00
|
|
|
}
|
2020-01-24 20:00:29 +01:00
|
|
|
|
2021-11-24 10:49:20 +01:00
|
|
|
users := make([]*user_model.User, 0, 8)
|
2017-01-06 08:05:09 +01:00
|
|
|
return users, sess.Find(&users)
|
2016-12-29 15:58:24 +01:00
|
|
|
}
|
2023-06-05 15:25:43 +02:00
|
|
|
|
|
|
|
// 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})
|
|
|
|
}
|