fix test & add some review

This commit is contained in:
Michael Jerger 2024-03-27 19:56:32 +01:00
parent 8eae48761f
commit 259c0202c7
3 changed files with 12 additions and 7 deletions

View file

@ -18,13 +18,13 @@ type ForgeLike struct {
ap.Activity ap.Activity
} }
func NewForgeLike(actorIRI string, objectIRI string) (ForgeLike, error) { func NewForgeLike(actorIRI string, objectIRI string, startTime time.Time) (ForgeLike, error) {
result := ForgeLike{} result := ForgeLike{}
result.Type = ap.LikeType result.Type = ap.LikeType
// ToDo: Would validating the source by Actor.Type field make sense? // ToDo: Would validating the source by Actor.Type field make sense?
result.Actor = ap.IRI(actorIRI) // Thats us, a User result.Actor = ap.IRI(actorIRI) // Thats us, a User
result.Object = ap.IRI(objectIRI) // Thats them, a Repository result.Object = ap.IRI(objectIRI) // Thats them, a Repository
result.StartTime = time.Now() result.StartTime = startTime
if valid, err := validation.IsValid(result); !valid { if valid, err := validation.IsValid(result); !valid {
return ForgeLike{}, err return ForgeLike{}, err
} }

View file

@ -1,4 +1,4 @@
// Copyright 2023 The Forgejo Authors. All rights reserved. // Copyright 2023, 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
package forgefed package forgefed
@ -6,6 +6,7 @@ package forgefed
import ( import (
"reflect" "reflect"
"testing" "testing"
"time"
"code.gitea.io/gitea/modules/validation" "code.gitea.io/gitea/modules/validation"
@ -16,9 +17,10 @@ import (
func Test_NewForgeLike(t *testing.T) { func Test_NewForgeLike(t *testing.T) {
actorIRI := "https://repo.prod.meissa.de/api/v1/activitypub/user-id/1" actorIRI := "https://repo.prod.meissa.de/api/v1/activitypub/user-id/1"
objectIRI := "https://codeberg.org/api/v1/activitypub/repository-id/1" objectIRI := "https://codeberg.org/api/v1/activitypub/repository-id/1"
want := []byte(`{"type":"Like","actor":"https://repo.prod.meissa.de/api/v1/activitypub/user-id/1","object":"https://codeberg.org/api/v1/activitypub/repository-id/1"}`) want := []byte(`{"type":"Like","startTime":"2024-03-27T00:00:00Z","actor":"https://repo.prod.meissa.de/api/v1/activitypub/user-id/1","object":"https://codeberg.org/api/v1/activitypub/repository-id/1"}`)
sut, err := NewForgeLike(actorIRI, objectIRI) startTime, _ := time.Parse("2006-Jan-02", "2024-Mar-27")
sut, err := NewForgeLike(actorIRI, objectIRI, startTime)
if err != nil { if err != nil {
t.Errorf("unexpected error: %v\n", err) t.Errorf("unexpected error: %v\n", err)
} }

View file

@ -5,6 +5,7 @@ package repo
import ( import (
"context" "context"
"time"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/forgefed" "code.gitea.io/gitea/models/forgefed"
@ -86,7 +87,7 @@ func starLocalRepo(ctx context.Context, userID, repoID int64, star bool) error {
// ToDo: Move to federation service or simillar // ToDo: Move to federation service or simillar
func sendLikeActivities(ctx context.Context, userID int64, repoID int64) error { func sendLikeActivities(ctx context.Context, userID int64, repoID int64) error {
// TODO: is user loading necessary here? // TODO: is user loading necessary here? - imho no!
log.Info("User ID: %v, Repo ID: %v", userID, repoID) log.Info("User ID: %v, Repo ID: %v", userID, repoID)
user, err := user_model.GetUserByID(ctx, userID) user, err := user_model.GetUserByID(ctx, userID)
log.Info("User is: %v", user) log.Info("User is: %v", user)
@ -100,6 +101,7 @@ func sendLikeActivities(ctx context.Context, userID int64, repoID int64) error {
return err return err
} }
// TODO: That's wrong - we've to send the activities to repo not to user!
apclient, err := activitypub.NewClient(ctx, user, user.APAPIURL()) apclient, err := activitypub.NewClient(ctx, user, user.APAPIURL())
if err != nil { if err != nil {
return err return err
@ -108,7 +110,7 @@ func sendLikeActivities(ctx context.Context, userID int64, repoID int64) error {
for _, federatedRepo := range federatedRepos { for _, federatedRepo := range federatedRepos {
target := federatedRepo.Uri + "/inbox/" // A like goes to the inbox of the federated repo target := federatedRepo.Uri + "/inbox/" // A like goes to the inbox of the federated repo
log.Info("Federated Repo URI is: %v", target) log.Info("Federated Repo URI is: %v", target)
likeActivity, err := forgefed.NewForgeLike(user.APAPIURL(), target) likeActivity, err := forgefed.NewForgeLike(user.APAPIURL(), target, time.Now())
if err != nil { if err != nil {
return err return err
} }
@ -118,6 +120,7 @@ func sendLikeActivities(ctx context.Context, userID int64, repoID int64) error {
return err return err
} }
// TODO: decouple loading & creating activities from sending them - use two loops.
// TODO: set timeouts for outgoing request in oder to mitigate DOS by slow lories // TODO: set timeouts for outgoing request in oder to mitigate DOS by slow lories
// TODO: Check if we need to respect rate limits // TODO: Check if we need to respect rate limits
// ToDo: Change this to the standalone table of FederatedRepos // ToDo: Change this to the standalone table of FederatedRepos