2022-06-20 15:38:57 -05:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package forgefed
|
|
|
|
|
|
|
|
import (
|
2022-08-20 23:07:11 -05:00
|
|
|
"reflect"
|
|
|
|
"unsafe"
|
|
|
|
|
2022-07-11 17:27:57 -05:00
|
|
|
ap "github.com/go-ap/activitypub"
|
2022-06-20 15:38:57 -05:00
|
|
|
"github.com/valyala/fastjson"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
PushType ap.ActivityVocabularyType = "Push"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Push struct {
|
|
|
|
ap.Object
|
|
|
|
// Target the specific repo history tip onto which the commits were added
|
|
|
|
Target ap.Item `jsonld:"target,omitempty"`
|
|
|
|
// HashBefore hash before adding the new commits
|
|
|
|
HashBefore ap.Item `jsonld:"hashBefore,omitempty"`
|
|
|
|
// HashAfter hash before adding the new commits
|
|
|
|
HashAfter ap.Item `jsonld:"hashAfter,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushNew initializes a Push type Object
|
|
|
|
func PushNew() *Push {
|
|
|
|
a := ap.ObjectNew(PushType)
|
|
|
|
o := Push{Object: *a}
|
|
|
|
return &o
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Push) MarshalJSON() ([]byte, error) {
|
|
|
|
b, err := p.Object.MarshalJSON()
|
|
|
|
if len(b) == 0 || err != nil {
|
2022-07-17 11:19:48 -05:00
|
|
|
return nil, err
|
2022-06-20 15:38:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
b = b[:len(b)-1]
|
|
|
|
if p.Target != nil {
|
2022-08-20 23:07:11 -05:00
|
|
|
ap.JSONWriteItemJSONProp(&b, "target", p.Target)
|
2022-06-20 15:38:57 -05:00
|
|
|
}
|
|
|
|
if p.HashBefore != nil {
|
2022-08-20 23:07:11 -05:00
|
|
|
ap.JSONWriteItemJSONProp(&b, "hashBefore", p.HashBefore)
|
2022-06-20 15:38:57 -05:00
|
|
|
}
|
|
|
|
if p.HashAfter != nil {
|
2022-08-20 23:07:11 -05:00
|
|
|
ap.JSONWriteItemJSONProp(&b, "hashAfter", p.HashAfter)
|
2022-06-20 15:38:57 -05:00
|
|
|
}
|
2022-08-20 23:07:11 -05:00
|
|
|
ap.JSONWrite(&b, '}')
|
2022-06-20 15:38:57 -05:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2022-08-20 23:07:11 -05:00
|
|
|
func JSONLoadPush(val *fastjson.Value, p *Push) error {
|
2022-08-22 18:59:45 +02:00
|
|
|
if err := ap.OnObject(&p.Object, func(o *ap.Object) error {
|
2022-08-20 23:07:11 -05:00
|
|
|
return ap.JSONLoadObject(val, o)
|
2022-08-22 18:59:45 +02:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-20 23:07:11 -05:00
|
|
|
p.Target = ap.JSONGetItem(val, "target")
|
|
|
|
p.HashBefore = ap.JSONGetItem(val, "hashBefore")
|
|
|
|
p.HashAfter = ap.JSONGetItem(val, "hashAfter")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-23 22:12:09 -05:00
|
|
|
func (p *Push) UnmarshalJSON(data []byte) error {
|
2022-08-20 23:07:11 -05:00
|
|
|
par := fastjson.Parser{}
|
|
|
|
val, err := par.ParseBytes(data)
|
2022-06-20 15:38:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-20 23:07:11 -05:00
|
|
|
return JSONLoadPush(val, p)
|
|
|
|
}
|
2022-06-20 15:38:57 -05:00
|
|
|
|
2022-08-20 23:07:11 -05:00
|
|
|
// ToPush tries to convert the it Item to a Push object.
|
|
|
|
func ToPush(it ap.Item) (*Push, error) {
|
|
|
|
switch i := it.(type) {
|
|
|
|
case *Push:
|
|
|
|
return i, nil
|
|
|
|
case Push:
|
|
|
|
return &i, nil
|
|
|
|
case *ap.Object:
|
|
|
|
return (*Push)(unsafe.Pointer(i)), nil
|
|
|
|
case ap.Object:
|
|
|
|
return (*Push)(unsafe.Pointer(&i)), nil
|
|
|
|
default:
|
|
|
|
// NOTE(marius): this is an ugly way of dealing with the interface conversion error: types from different scopes
|
|
|
|
typ := reflect.TypeOf(new(Push))
|
|
|
|
if i, ok := reflect.ValueOf(it).Convert(typ).Interface().(*Push); ok {
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, ap.ErrorInvalidType[ap.Object](it)
|
|
|
|
}
|
2022-06-20 15:38:57 -05:00
|
|
|
|
2022-08-20 23:07:11 -05:00
|
|
|
type withPushFn func(*Push) error
|
|
|
|
|
|
|
|
// OnPush calls function fn on it Item if it can be asserted to type *Push
|
|
|
|
func OnPush(it ap.Item, fn withPushFn) error {
|
|
|
|
if it == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ob, err := ToPush(it)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(ob)
|
2022-06-20 15:38:57 -05:00
|
|
|
}
|