start refactoring star->like

This commit is contained in:
Michael Jerger 2024-01-03 18:10:24 +01:00
parent 38438b592f
commit 4473fb788a
3 changed files with 15 additions and 46 deletions

View file

@ -4,12 +4,8 @@
package forgefed package forgefed
import ( import (
"code.gitea.io/gitea/modules/validation"
ap "github.com/go-ap/activitypub" ap "github.com/go-ap/activitypub"
"github.com/valyala/fastjson"
)
const (
StarType ap.ActivityVocabularyType = "Star"
) )
// Star activity data type // Star activity data type
@ -17,46 +13,19 @@ const (
type Star struct { type Star struct {
// swagger:ignore // swagger:ignore
ap.Activity 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 func (a Star) MarshalJSON() ([]byte, error) {
// ToDo: May be used later in creating signed activities return a.Activity.MarshalJSON()
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 (s *Star) UnmarshalJSON(data []byte) error { func (s *Star) UnmarshalJSON(data []byte) error {
p := fastjson.Parser{} return s.UnmarshalJSON(data)
val, err := p.ParseBytes(data) }
if err != nil {
return err func (s Star) Validate() []string {
} var result []string
return JSONLoadStar(val, s) result = append(result, validation.ValidateNotEmpty(string(s.Type), "type")...)
return result
} }

View file

@ -24,14 +24,13 @@ func Test_StarMarshalJSON(t *testing.T) {
}, },
"with ID": { "with ID": {
item: Star{ item: Star{
Source: "forgejo",
Activity: ap.Activity{ Activity: ap.Activity{
Actor: ap.IRI("https://repo.prod.meissa.de/api/v1/activitypub/user-id/1"), 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"), 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": { "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"}`), 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{ want: &Star{
Source: "forgejo",
Activity: ap.Activity{ Activity: ap.Activity{
Actor: ap.IRI("https://repo.prod.meissa.de/api/activitypub/user-id/1"), Actor: ap.IRI("https://repo.prod.meissa.de/api/activitypub/user-id/1"),
Type: "Star", Type: "Star",

View file

@ -225,6 +225,8 @@ func createUserFromAP(ctx *context.APIContext, personID forgefed.PersonID) (*use
} }
log.Info("RepositoryInbox: got person by ap: %v", person) 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) email := fmt.Sprintf("%v@%v", uuid.New().String(), personID.Host)
loginName := personID.AsLoginName() loginName := personID.AsLoginName()
name := fmt.Sprintf("%v%v", person.PreferredUsername.String(), personID.HostSuffix()) name := fmt.Sprintf("%v%v", person.PreferredUsername.String(), personID.HostSuffix())