Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-27 12:16:10 +01:00
forgejo/modules/forgefed/star.go

77 lines
1.5 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-07 09:30:32 +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-11-07 09:30:32 +01:00
func StarNew(id ap.ID, ob ap.ID) *Star {
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-11-14 14:56:16 +01:00
// 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)
}