2023-01-12 21:48:05 +00:00
|
|
|
// Copyright 2023 The Forgejo Authors. All rights reserved.
|
2022-12-31 17:58:01 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-07-27 15:24:04 +00:00
|
|
|
|
|
|
|
package activitypub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-12-30 00:07:00 +00:00
|
|
|
"errors"
|
2022-07-27 19:18:30 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2023-01-12 21:28:35 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-07-27 15:24:04 +00:00
|
|
|
|
|
|
|
ap "github.com/go-ap/activitypub"
|
|
|
|
)
|
|
|
|
|
2022-07-27 19:18:30 +00:00
|
|
|
// Process a Like activity to star a repository
|
2022-11-27 00:34:24 +00:00
|
|
|
func star(ctx context.Context, like ap.Like) (err error) {
|
2023-01-12 21:28:35 +00:00
|
|
|
user, err := user_model.GetUserByIRI(ctx, like.Actor.GetLink().String())
|
2022-07-27 19:18:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-01-16 21:20:50 +00:00
|
|
|
repo, err := repo_model.GetRepositoryByIRI(ctx, like.Object.GetLink().String())
|
2022-07-27 19:18:30 +00:00
|
|
|
if err != nil || strings.Contains(repo.Name, "@") || repo.IsPrivate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return repo_model.StarRepo(user.ID, repo.ID, true)
|
2022-07-27 15:24:04 +00:00
|
|
|
}
|
2022-12-29 23:13:48 +00:00
|
|
|
|
|
|
|
// Process an Undo Like activity to unstar a repository
|
|
|
|
func unstar(ctx context.Context, unlike ap.Undo) (err error) {
|
2022-12-30 00:07:00 +00:00
|
|
|
like, ok := unlike.Object.(*ap.Like)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("could not cast object to like")
|
2022-12-29 23:13:48 +00:00
|
|
|
}
|
2023-01-12 21:28:35 +00:00
|
|
|
user, err := user_model.GetUserByIRI(ctx, like.Actor.GetLink().String())
|
2022-12-29 23:13:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-01-16 21:20:50 +00:00
|
|
|
repo, err := repo_model.GetRepositoryByIRI(ctx, like.Object.GetLink().String())
|
2022-12-29 23:13:48 +00:00
|
|
|
if err != nil || strings.Contains(repo.Name, "@") || repo.IsPrivate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return repo_model.StarRepo(user.ID, repo.ID, false)
|
|
|
|
}
|