Compare commits

...

9 Commits

@ -37,7 +37,7 @@ require (
github.com/felixge/fgprof v0.9.3
github.com/fsnotify/fsnotify v1.6.0
github.com/gliderlabs/ssh v0.3.6-0.20230927171611-ece6c7995e46
github.com/go-ap/activitypub v0.0.0-20231003111253-1fba3772399b
github.com/go-ap/activitypub v0.0.0-20231105151936-af32623a589b
github.com/go-ap/jsonld v0.0.0-20221030091449-f2a191312c73
github.com/go-chi/chi/v5 v5.0.10
github.com/go-chi/cors v1.2.1

@ -335,6 +335,8 @@ github.com/glycerine/go-unsnap-stream v0.0.0-20181221182339-f9677308dec2/go.mod
github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24=
github.com/go-ap/activitypub v0.0.0-20231003111253-1fba3772399b h1:VLD6IPBDkqEsOZ+EfLO6MayuHycZ0cv4BStTlRoZduo=
github.com/go-ap/activitypub v0.0.0-20231003111253-1fba3772399b/go.mod h1:cJ9Ye0ZNSMN7RzZDBRY3E+8M3Bpf/R1JX22Ir9yX6WI=
github.com/go-ap/activitypub v0.0.0-20231105151936-af32623a589b h1:GfKvTbxH5+QFIwxsjQ8CwSej27eAiub6LtHbCMuBbtg=
github.com/go-ap/activitypub v0.0.0-20231105151936-af32623a589b/go.mod h1:cJ9Ye0ZNSMN7RzZDBRY3E+8M3Bpf/R1JX22Ir9yX6WI=
github.com/go-ap/errors v0.0.0-20231003111023-183eef4b31b7 h1:I2nuhyVI/48VXoRCCZR2hYBgnSXa+EuDJf/VyX06TC0=
github.com/go-ap/errors v0.0.0-20231003111023-183eef4b31b7/go.mod h1:5x8a6P/dhmMGFxWLcyYlyOuJ2lRNaHGhRv+yu8BaTSI=
github.com/go-ap/jsonld v0.0.0-20221030091449-f2a191312c73 h1:GMKIYXyXPGIp+hYiWOhfqK4A023HdgisDT4YGgf99mw=

@ -13,44 +13,6 @@ import (
ap "github.com/go-ap/activitypub"
)
func Test_GetItemByType(t *testing.T) {
type testtt struct {
typ ap.ActivityVocabularyType
want ap.Item
wantErr error
}
tests := map[string]testtt{
"invalid type": {
typ: ap.ActivityVocabularyType("invalidtype"),
wantErr: fmt.Errorf("empty ActivityStreams type"), // TODO(marius): this error message needs to be improved in go-ap/activitypub
},
"Repository": {
typ: RepositoryType,
want: new(Repository),
},
"Person - fall back": {
typ: ap.PersonType,
want: new(ap.Person),
},
"Question - fall back": {
typ: ap.QuestionType,
want: new(ap.Question),
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
maybeRepository, err := GetItemByType(tt.typ)
if !reflect.DeepEqual(tt.wantErr, err) {
t.Errorf("GetItemByType() error = \"%+v\", wantErr = \"%+v\" when getting Item for type %q", tt.wantErr, err, tt.typ)
}
if reflect.TypeOf(tt.want) != reflect.TypeOf(maybeRepository) {
t.Errorf("Invalid type received %T, expected %T", maybeRepository, tt.want)
}
})
}
}
func Test_RepositoryMarshalJSON(t *testing.T) {
type testPair struct {
item Repository

@ -4,8 +4,8 @@
package forgefed
import (
"code.gitea.io/gitea/modules/context"
ap "github.com/go-ap/activitypub"
"github.com/valyala/fastjson"
)
type (
@ -30,25 +30,47 @@ var KnownSourceTypes = SourceTypes{
// swagger:model
type Star struct {
// swagger:ignore
ap.Activity
Activity ap.Activity
// Source identifies the system which generated this activity. Exactly one value has to be specified.
Source SourceType `jsonld:"source,omitempty"`
}
// Infos needed to star a repo
type StarRepo struct {
StargazerID int `json:"Stargazer"`
RepoID int `json:"RepoToStar"`
}
// StarNew initializes a Star type activity
// Guess: no return value needed, we may need to add the star to the context
func StarNew(id ap.ID, ob ap.ID) *Star {
func StarNew(id ap.ID, ob ap.ID) *Star { // ToDo: Currently this function is not used anywhere, so we don't create stars?
a := ap.ActivityNew(id, StarType, ob)
o := Star{Activity: *a, Source: ForgejoSourceType}
return &o
}
func AddStar(ctx *context.APIContext) {
// ToDo: should Star be *Star?
func (a Star) MarshalJSON() ([]byte, error) {
b := make([]byte, 0)
ap.JSONWrite(&b, '{')
ap.JSONWriteStringProp(&b, "source", string(a.Source))
if !ap.JSONWriteActivityValue(&b, a.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 {
p := fastjson.Parser{}
val, err := p.ParseBytes(data)
if err != nil {
return err
}
return JSONLoadStar(val, s)
}

@ -26,19 +26,12 @@ func Test_StarMarshalJSON(t *testing.T) {
item: Star{
Source: "forgejo",
Activity: ap.Activity{
ID: "https://repo.prod.meissa.de/api/activitypub/user-id/1",
Type: "Star",
Object: ap.Object{
ID: "https://codeberg.org/api/activitypub/repository-id/1",
},
Actor: ap.IRI("https://repo.prod.meissa.de/api/activitypub/user-id/1"),
Type: "Star",
Object: ap.IRI("https://codeberg.org/api/activitypub/repository-id/1"),
},
},
want: []byte(`{
"type": "Star",
"source": "forgejo",
"actor": "https://repo.prod.meissa.de/api/activitypub/user-id/1",
"object": "https://codeberg.org/api/activitypub/repository-id/1"
}`),
want: []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"}`),
},
}
@ -55,3 +48,39 @@ func Test_StarMarshalJSON(t *testing.T) {
})
}
}
func Test_StarUnmarshalJSON(t *testing.T) {
type testPair struct {
item []byte
want *Star
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{
Source: "forgejo",
Activity: ap.Activity{
Actor: ap.IRI("https://repo.prod.meissa.de/api/activitypub/user-id/1"),
Type: "Star",
Object: ap.IRI("https://codeberg.org/api/activitypub/repository-id/1"),
},
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := new(Star)
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)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("UnmarshalJSON() got = %q, want %q", got, tt.want)
}
})
}
}

@ -73,7 +73,7 @@ func RepositoryInbox(ctx *context.APIContext) {
opt := web.GetForm(ctx).(*forgefed.Star)
log.Info("RepositoryInbox: Activity.Source %v", opt.Source)
log.Info("RepositoryInbox: Activity.Actor %v", opt.Actor)
log.Info("RepositoryInbox: Activity.Actor %v", opt.Activity)
// assume actor is: "actor": "https://codeberg.org/api/activitypub/user-id/12345"

@ -836,11 +836,11 @@ func Routes() *web.Route {
}, context_service.UserIDAssignmentAPI())
m.Group("/repository-id/{repository-id}", func() {
m.Get("", activitypub.Repository)
m.Post("/inbox",
m.Post("/inbox", // ToDo: We may want a m.Patch method here, as we are not replacing stars
// TODO: bind ativities here
bind(forgefed.Star{}),
//activitypub.ReqHTTPSignature(),
activitypub.RepositoryInbox)
activitypub.RepositoryInbox) // ToDo: We may need to use another method to add a star to the repo
}, context_service.RepositoryIDAssignmentAPI())
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryActivityPub))
}

Loading…
Cancel
Save