2024-04-12 11:52:26 +00:00
|
|
|
// Copyright 2023, 2024 The Forgejo Authors. All rights reserved.
|
2024-01-03 17:29:12 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package forgefed
|
|
|
|
|
|
|
|
import (
|
2024-01-13 13:17:11 +00:00
|
|
|
"time"
|
|
|
|
|
2024-01-03 17:29:12 +00:00
|
|
|
"code.gitea.io/gitea/modules/validation"
|
2024-02-09 16:46:27 +00:00
|
|
|
|
2024-01-03 17:29:12 +00:00
|
|
|
ap "github.com/go-ap/activitypub"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ForgeLike activity data type
|
|
|
|
// swagger:model
|
|
|
|
type ForgeLike struct {
|
|
|
|
// swagger:ignore
|
|
|
|
ap.Activity
|
|
|
|
}
|
|
|
|
|
2024-04-04 13:08:02 +00:00
|
|
|
func NewForgeLike(actorIRI, objectIRI string, startTime time.Time) (ForgeLike, error) {
|
2024-03-21 15:27:08 +00:00
|
|
|
result := ForgeLike{}
|
|
|
|
result.Type = ap.LikeType
|
2024-03-27 15:20:33 +00:00
|
|
|
result.Actor = ap.IRI(actorIRI) // Thats us, a User
|
|
|
|
result.Object = ap.IRI(objectIRI) // Thats them, a Repository
|
2024-03-27 18:56:32 +00:00
|
|
|
result.StartTime = startTime
|
2024-03-21 15:27:08 +00:00
|
|
|
if valid, err := validation.IsValid(result); !valid {
|
|
|
|
return ForgeLike{}, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2024-01-13 13:17:11 +00:00
|
|
|
func (like ForgeLike) MarshalJSON() ([]byte, error) {
|
|
|
|
return like.Activity.MarshalJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (like *ForgeLike) UnmarshalJSON(data []byte) error {
|
|
|
|
return like.Activity.UnmarshalJSON(data)
|
2024-01-03 17:29:12 +00:00
|
|
|
}
|
|
|
|
|
2024-01-13 13:17:11 +00:00
|
|
|
func (like ForgeLike) IsNewer(compareTo time.Time) bool {
|
|
|
|
return like.StartTime.After(compareTo)
|
2024-01-03 17:29:12 +00:00
|
|
|
}
|
|
|
|
|
2024-01-13 13:17:11 +00:00
|
|
|
func (like ForgeLike) Validate() []string {
|
2024-01-03 17:29:12 +00:00
|
|
|
var result []string
|
2024-01-13 13:17:11 +00:00
|
|
|
result = append(result, validation.ValidateNotEmpty(string(like.Type), "type")...)
|
2024-05-01 13:02:27 +00:00
|
|
|
result = append(result, validation.ValidateOneOf(string(like.Type), []any{"Like"}, "type")...)
|
2024-05-01 12:39:09 +00:00
|
|
|
if like.Actor == nil {
|
2024-05-01 13:23:39 +00:00
|
|
|
result = append(result, "Actor should not be nil.")
|
2024-05-01 12:39:09 +00:00
|
|
|
} else {
|
|
|
|
result = append(result, validation.ValidateNotEmpty(like.Actor.GetID().String(), "actor")...)
|
|
|
|
}
|
|
|
|
if like.Object == nil {
|
2024-05-01 13:23:39 +00:00
|
|
|
result = append(result, "Object should not be nil.")
|
2024-05-01 12:39:09 +00:00
|
|
|
} else {
|
|
|
|
result = append(result, validation.ValidateNotEmpty(like.Object.GetID().String(), "object")...)
|
|
|
|
}
|
2024-01-13 13:17:11 +00:00
|
|
|
result = append(result, validation.ValidateNotEmpty(like.StartTime.String(), "startTime")...)
|
|
|
|
if like.StartTime.IsZero() {
|
2024-01-03 17:52:41 +00:00
|
|
|
result = append(result, "StartTime was invalid.")
|
|
|
|
}
|
2024-01-03 17:29:12 +00:00
|
|
|
|
|
|
|
return result
|
|
|
|
}
|