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

77 lines
1.6 KiB
Go
Raw Normal View History

2023-11-07 09:30:32 +01: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 09:30:32 +01:00
)
type (
SourceType string
)
type SourceTypes []SourceType
const (
StarType ap.ActivityVocabularyType = "Star"
)
const (
ForgejoSourceType SourceType = "frogejo"
)
var KnownSourceTypes = SourceTypes{
ForgejoSourceType,
}
2023-11-09 14:24:19 +01:00
// Star activity data type
2023-11-09 21:59:51 +01:00
// swagger:model
2023-11-07 09:30:32 +01:00
type Star struct {
2023-11-09 21:54:17 +01:00
// swagger:ignore
2023-11-15 08:53:02 +01:00
ap.Activity
2023-11-09 14:24:19 +01:00
// Source identifies the system which generated this activity. Exactly one value has to be specified.
2023-11-07 09:30:32 +01:00
Source SourceType `jsonld:"source,omitempty"`
}
2023-11-09 15:38:55 +01:00
// StarNew initializes a Star type activity
2023-12-22 15:00:42 +01:00
// ToDo: May be used later in creating signed activities
func StarNew(id ap.ID, ob ap.ID) *Star {
2023-11-07 09:30:32 +01:00
a := ap.ActivityNew(id, StarType, ob)
2023-11-09 15:38:55 +01:00
o := Star{Activity: *a, Source: ForgejoSourceType}
2023-11-07 09:30:32 +01:00
return &o
}
2023-11-09 15:38:55 +01:00
2023-12-22 14:52:10 +01:00
func (s Star) MarshalJSON() ([]byte, error) {
b := make([]byte, 0)
ap.JSONWrite(&b, '{')
2023-12-22 14:52:10 +01:00
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 {
p := fastjson.Parser{}
val, err := p.ParseBytes(data)
if err != nil {
return err
}
return JSONLoadStar(val, s)
}