1
0
Fork 0
forgejo/models/forgefed/star.go

76 lines
1.6 KiB
Go
Raw Normal View History

2023-11-07 03:30:32 -05:00
// Copyright 2023 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package forgefed
import (
ap "github.com/go-ap/activitypub"
"github.com/valyala/fastjson"
2023-11-07 03:30:32 -05:00
)
type (
SourceType string
)
type SourceTypes []SourceType
const (
StarType ap.ActivityVocabularyType = "Star"
)
const (
ForgejoSourceType SourceType = "frogejo"
)
var KnownSourceTypes = SourceTypes{
ForgejoSourceType,
}
2023-11-09 08:24:19 -05:00
// Star activity data type
2023-11-09 15:59:51 -05:00
// swagger:model
2023-11-07 03:30:32 -05:00
type Star struct {
2023-11-09 15:54:17 -05:00
// swagger:ignore
2023-11-15 02:53:02 -05:00
ap.Activity
2023-11-09 08:24:19 -05:00
// Source identifies the system which generated this activity. Exactly one value has to be specified.
2023-11-07 03:30:32 -05:00
Source SourceType `jsonld:"source,omitempty"`
}
2023-11-09 09:38:55 -05:00
// StarNew initializes a Star type activity
2023-11-15 02:53:02 -05:00
func StarNew(id ap.ID, ob ap.ID) *Star { // ToDo: May be used later in creating signed activities
2023-11-07 03:30:32 -05:00
a := ap.ActivityNew(id, StarType, ob)
2023-11-09 09:38:55 -05:00
o := Star{Activity: *a, Source: ForgejoSourceType}
2023-11-07 03:30:32 -05:00
return &o
}
2023-11-09 09:38:55 -05:00
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)
}