feat(federation): validate like activities (#3494)
First step on the way to #1680 The PR will * accept like request on the api * validate activity in a first level You can find * architecture at: https://codeberg.org/meissa/forgejo/src/branch/forgejo-federated-star/docs/unsure-where-to-put/federation-architecture.md Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3494 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Michael Jerger <michael.jerger@meissa-gmbh.de> Co-committed-by: Michael Jerger <michael.jerger@meissa-gmbh.de>
This commit is contained in:
parent
8c3511a8b3
commit
2177d38e9c
18 changed files with 1088 additions and 1 deletions
65
modules/forgefed/activity.go
Normal file
65
modules/forgefed/activity.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
// Copyright 2023, 2024 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package forgefed
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/validation"
|
||||
|
||||
ap "github.com/go-ap/activitypub"
|
||||
)
|
||||
|
||||
// ForgeLike activity data type
|
||||
// swagger:model
|
||||
type ForgeLike struct {
|
||||
// swagger:ignore
|
||||
ap.Activity
|
||||
}
|
||||
|
||||
func NewForgeLike(actorIRI, objectIRI string, startTime time.Time) (ForgeLike, error) {
|
||||
result := ForgeLike{}
|
||||
result.Type = ap.LikeType
|
||||
result.Actor = ap.IRI(actorIRI) // Thats us, a User
|
||||
result.Object = ap.IRI(objectIRI) // Thats them, a Repository
|
||||
result.StartTime = startTime
|
||||
if valid, err := validation.IsValid(result); !valid {
|
||||
return ForgeLike{}, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (like ForgeLike) MarshalJSON() ([]byte, error) {
|
||||
return like.Activity.MarshalJSON()
|
||||
}
|
||||
|
||||
func (like *ForgeLike) UnmarshalJSON(data []byte) error {
|
||||
return like.Activity.UnmarshalJSON(data)
|
||||
}
|
||||
|
||||
func (like ForgeLike) IsNewer(compareTo time.Time) bool {
|
||||
return like.StartTime.After(compareTo)
|
||||
}
|
||||
|
||||
func (like ForgeLike) Validate() []string {
|
||||
var result []string
|
||||
result = append(result, validation.ValidateNotEmpty(string(like.Type), "type")...)
|
||||
result = append(result, validation.ValidateOneOf(string(like.Type), []any{"Like"}, "type")...)
|
||||
if like.Actor == nil {
|
||||
result = append(result, "Actor should not be nil.")
|
||||
} else {
|
||||
result = append(result, validation.ValidateNotEmpty(like.Actor.GetID().String(), "actor")...)
|
||||
}
|
||||
if like.Object == nil {
|
||||
result = append(result, "Object should not be nil.")
|
||||
} else {
|
||||
result = append(result, validation.ValidateNotEmpty(like.Object.GetID().String(), "object")...)
|
||||
}
|
||||
result = append(result, validation.ValidateNotEmpty(like.StartTime.String(), "startTime")...)
|
||||
if like.StartTime.IsZero() {
|
||||
result = append(result, "StartTime was invalid.")
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue