From 3ab2d9a449cdd4430b0791b21ac9669fd2313bbc Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Wed, 3 Jan 2024 18:29:12 +0100 Subject: [PATCH] rename star -> ForgeLike --- models/forgefed/activity.go | 34 +++++++++++++++++++ .../{star_test.go => activity_test.go} | 16 ++++----- models/forgefed/star.go | 31 ----------------- routers/api/v1/activitypub/repository.go | 4 +-- routers/api/v1/api.go | 2 +- routers/api/v1/swagger/options.go | 2 +- 6 files changed, 46 insertions(+), 43 deletions(-) create mode 100644 models/forgefed/activity.go rename models/forgefed/{star_test.go => activity_test.go} (85%) delete mode 100644 models/forgefed/star.go diff --git a/models/forgefed/activity.go b/models/forgefed/activity.go new file mode 100644 index 0000000000..0820bf5ea6 --- /dev/null +++ b/models/forgefed/activity.go @@ -0,0 +1,34 @@ +// Copyright 2023 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package forgefed + +import ( + "code.gitea.io/gitea/modules/validation" + ap "github.com/go-ap/activitypub" +) + +// ForgeLike activity data type +// swagger:model +type ForgeLike struct { + // swagger:ignore + ap.Activity +} + +func (s ForgeLike) MarshalJSON() ([]byte, error) { + return s.Activity.MarshalJSON() +} + +func (s *ForgeLike) UnmarshalJSON(data []byte) error { + return s.Activity.UnmarshalJSON(data) +} + +func (s ForgeLike) Validate() []string { + var result []string + result = append(result, validation.ValidateNotEmpty(string(s.Type), "type")...) + result = append(result, validation.ValidateNotEmpty(s.Actor.GetID().String(), "actor")...) + result = append(result, validation.ValidateNotEmpty(s.Object.GetID().String(), "object")...) + result = append(result, validation.ValidateNotEmpty(s.StartTime.String(), "startTime")...) + + return result +} diff --git a/models/forgefed/star_test.go b/models/forgefed/activity_test.go similarity index 85% rename from models/forgefed/star_test.go rename to models/forgefed/activity_test.go index 5edb19edaf..cc82c1b8b4 100644 --- a/models/forgefed/star_test.go +++ b/models/forgefed/activity_test.go @@ -12,18 +12,18 @@ import ( func Test_StarMarshalJSON(t *testing.T) { type testPair struct { - item Star + item ForgeLike want []byte wantErr error } tests := map[string]testPair{ "empty": { - item: Star{}, + item: ForgeLike{}, want: nil, }, "with ID": { - item: Star{ + item: ForgeLike{ Activity: ap.Activity{ Actor: ap.IRI("https://repo.prod.meissa.de/api/v1/activitypub/user-id/1"), Type: "Like", @@ -51,17 +51,17 @@ func Test_StarMarshalJSON(t *testing.T) { func Test_StarUnmarshalJSON(t *testing.T) { type testPair struct { item []byte - want *Star + want *ForgeLike wantErr error } tests := map[string]testPair{ "with ID": { - item: []byte(`{"source":"forgejo","type":"Star","actor":"https://repo.prod.meissa.de/api/activitypub/user-id/1","object":"https://codeberg.org/api/activitypub/repository-id/1"}`), - want: &Star{ + item: []byte(`{"type":"Like","actor":"https://repo.prod.meissa.de/api/activitypub/user-id/1","object":"https://codeberg.org/api/activitypub/repository-id/1"}`), + want: &ForgeLike{ Activity: ap.Activity{ Actor: ap.IRI("https://repo.prod.meissa.de/api/activitypub/user-id/1"), - Type: "Star", + Type: "Like", Object: ap.IRI("https://codeberg.org/api/activitypub/repository-id/1"), }, }, @@ -70,7 +70,7 @@ func Test_StarUnmarshalJSON(t *testing.T) { for name, tt := range tests { t.Run(name, func(t *testing.T) { - got := new(Star) + got := new(ForgeLike) err := got.UnmarshalJSON(tt.item) if (err != nil || tt.wantErr != nil) && tt.wantErr.Error() != err.Error() { t.Errorf("UnmarshalJSON() error = \"%v\", wantErr \"%v\"", err, tt.wantErr) diff --git a/models/forgefed/star.go b/models/forgefed/star.go deleted file mode 100644 index bc470f395e..0000000000 --- a/models/forgefed/star.go +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2023 The Forgejo Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package forgefed - -import ( - "code.gitea.io/gitea/modules/validation" - ap "github.com/go-ap/activitypub" -) - -// Star activity data type -// swagger:model -type Star struct { - // swagger:ignore - ap.Activity -} - -func (a Star) MarshalJSON() ([]byte, error) { - return a.Activity.MarshalJSON() -} - -func (s *Star) UnmarshalJSON(data []byte) error { - return s.UnmarshalJSON(data) -} - -func (s Star) Validate() []string { - var result []string - result = append(result, validation.ValidateNotEmpty(string(s.Type), "type")...) - - return result -} diff --git a/routers/api/v1/activitypub/repository.go b/routers/api/v1/activitypub/repository.go index 27004621eb..1d94560127 100644 --- a/routers/api/v1/activitypub/repository.go +++ b/routers/api/v1/activitypub/repository.go @@ -74,7 +74,7 @@ func RepositoryInbox(ctx *context.APIContext) { // - name: body // in: body // schema: - // "$ref": "#/definitions/Star" + // "$ref": "#/definitions/ForgeLike" // responses: // "204": // "$ref": "#/responses/empty" @@ -84,7 +84,7 @@ func RepositoryInbox(ctx *context.APIContext) { repository := ctx.Repo.Repository log.Info("RepositoryInbox: repo: %v", repository) - activity := web.GetForm(ctx).(*forgefed.Star) + activity := web.GetForm(ctx).(*forgefed.ForgeLike) log.Info("RepositoryInbox: activity:%v", activity) // parse actorID (person) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index a844261c35..f29e193ca3 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -900,7 +900,7 @@ func Routes() *web.Route { m.Get("", activitypub.Repository) m.Post("/inbox", // ToDo: Post or Put? // TODO: bind ativities here - bind(forgefed.Star{}), + bind(forgefed.ForgeLike{}), // TODO: activitypub.ReqHTTPSignature(), activitypub.RepositoryInbox) }, context_service.RepositoryIDAssignmentAPI()) diff --git a/routers/api/v1/swagger/options.go b/routers/api/v1/swagger/options.go index 13b04e62fb..f085b9df20 100644 --- a/routers/api/v1/swagger/options.go +++ b/routers/api/v1/swagger/options.go @@ -16,7 +16,7 @@ import ( // swagger:response parameterBodies type swaggerParameterBodies struct { // in:body - Star ffed.Star + Star ffed.ForgeLike // in:body AddCollaboratorOption api.AddCollaboratorOption