From 4473fb788ae59eb7c42f920d3038db2dce30e5b7 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Wed, 3 Jan 2024 18:10:24 +0100 Subject: [PATCH] start refactoring star->like --- models/forgefed/star.go | 53 +++++------------------- models/forgefed/star_test.go | 6 +-- routers/api/v1/activitypub/repository.go | 2 + 3 files changed, 15 insertions(+), 46 deletions(-) diff --git a/models/forgefed/star.go b/models/forgefed/star.go index 21f44361ef..bc470f395e 100644 --- a/models/forgefed/star.go +++ b/models/forgefed/star.go @@ -4,12 +4,8 @@ package forgefed import ( + "code.gitea.io/gitea/modules/validation" ap "github.com/go-ap/activitypub" - "github.com/valyala/fastjson" -) - -const ( - StarType ap.ActivityVocabularyType = "Star" ) // Star activity data type @@ -17,46 +13,19 @@ const ( type Star struct { // swagger:ignore ap.Activity - // Source identifies the system which generated this activity. Exactly one value has to be specified. - Source SourceType `jsonld:"source,omitempty"` } -// StarNew initializes a Star type activity -// ToDo: May be used later in creating signed activities -func StarNew(id, ob ap.ID) *Star { - a := ap.ActivityNew(id, StarType, ob) - o := Star{Activity: *a, Source: ForgejoSourceType} - return &o -} - -func (s Star) MarshalJSON() ([]byte, error) { - b := make([]byte, 0) - ap.JSONWrite(&b, '{') - - ap.JSONWriteStringProp(&b, "source", string(s.Source)) - if !ap.JSONWriteActivityValue(&b, s.Activity) { - return nil, nil - } - ap.JSONWrite(&b, '}') - return b, nil -} - -func JSONLoadStar(val *fastjson.Value, s *Star) error { - if err := ap.OnActivity(&s.Activity, func(a *ap.Activity) error { - return ap.JSONLoadActivity(val, a) - }); err != nil { - return err - } - - s.Source = SourceType(ap.JSONGetString(val, "source")) - return nil +func (a Star) MarshalJSON() ([]byte, error) { + return a.Activity.MarshalJSON() } func (s *Star) UnmarshalJSON(data []byte) error { - p := fastjson.Parser{} - val, err := p.ParseBytes(data) - if err != nil { - return err - } - return JSONLoadStar(val, s) + 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/models/forgefed/star_test.go b/models/forgefed/star_test.go index 0cdaaa693e..5edb19edaf 100644 --- a/models/forgefed/star_test.go +++ b/models/forgefed/star_test.go @@ -24,14 +24,13 @@ func Test_StarMarshalJSON(t *testing.T) { }, "with ID": { item: Star{ - Source: "forgejo", Activity: ap.Activity{ Actor: ap.IRI("https://repo.prod.meissa.de/api/v1/activitypub/user-id/1"), - Type: "Star", + Type: "Like", Object: ap.IRI("https://codeberg.org/api/v1/activitypub/repository-id/1"), }, }, - want: []byte(`{"source":"forgejo","type":"Star","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","actor":"https://repo.prod.meissa.de/api/v1/activitypub/user-id/1","object":"https://codeberg.org/api/v1/activitypub/repository-id/1"}`), }, } @@ -60,7 +59,6 @@ func Test_StarUnmarshalJSON(t *testing.T) { "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{ - Source: "forgejo", Activity: ap.Activity{ Actor: ap.IRI("https://repo.prod.meissa.de/api/activitypub/user-id/1"), Type: "Star", diff --git a/routers/api/v1/activitypub/repository.go b/routers/api/v1/activitypub/repository.go index 99e399c497..27004621eb 100644 --- a/routers/api/v1/activitypub/repository.go +++ b/routers/api/v1/activitypub/repository.go @@ -225,6 +225,8 @@ func createUserFromAP(ctx *context.APIContext, personID forgefed.PersonID) (*use } log.Info("RepositoryInbox: got person by ap: %v", person) + // TODO: we should validate the person object here! + email := fmt.Sprintf("%v@%v", uuid.New().String(), personID.Host) loginName := personID.AsLoginName() name := fmt.Sprintf("%v%v", person.PreferredUsername.String(), personID.HostSuffix())