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
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
}

View file

@ -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",

View file

@ -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())