Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-30 05:46:09 +01:00
forgejo/models/forgefed/activity.go

66 lines
1.9 KiB
Go
Raw Normal View History

2024-01-03 18:29:12 +01:00
// Copyright 2023 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package forgefed
import (
"time"
2024-03-26 15:28:11 +01:00
"code.gitea.io/gitea/modules/log"
2024-01-03 18:29:12 +01:00
"code.gitea.io/gitea/modules/validation"
2024-02-09 17:46:27 +01:00
2024-01-03 18:29:12 +01:00
ap "github.com/go-ap/activitypub"
)
// ForgeLike activity data type
// swagger:model
type ForgeLike struct {
// swagger:ignore
ap.Activity
}
2024-03-22 07:52:03 +01:00
// TODO: Use explicit values instead of ctx !!
func NewForgeLike(actorIRI string, objectIRI string) (ForgeLike, error) {
2024-03-21 16:27:08 +01:00
result := ForgeLike{}
result.Type = ap.LikeType
// ToDo: Would validating the source by Actor.Type field make sense?
2024-03-26 15:28:11 +01:00
object := new(ap.Object)
object.ID = ap.IRI(objectIRI)
object.URL = ap.IRI(objectIRI)
result.Actor = ap.ActorNew(ap.IRI(actorIRI), "ForgejoUser") // Thats us, a User
result.Object = object // Thats them, a Repository
log.Info("Object is: %v", object)
2024-03-21 16:27:08 +01:00
result.StartTime = time.Now()
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)
2024-01-03 18:29:12 +01:00
}
func (like ForgeLike) IsNewer(compareTo time.Time) bool {
return like.StartTime.After(compareTo)
2024-01-03 18:29:12 +01:00
}
func (like ForgeLike) Validate() []string {
2024-01-03 18:29:12 +01:00
var result []string
result = append(result, validation.ValidateNotEmpty(string(like.Type), "type")...)
result = append(result, validation.ValidateOneOf(string(like.Type), []any{"Like"})...)
result = append(result, validation.ValidateNotEmpty(like.Actor.GetID().String(), "actor")...)
result = append(result, validation.ValidateNotEmpty(like.Object.GetID().String(), "object")...)
result = append(result, validation.ValidateNotEmpty(like.StartTime.String(), "startTime")...)
if like.StartTime.IsZero() {
2024-01-03 18:52:41 +01:00
result = append(result, "StartTime was invalid.")
}
2024-01-03 18:29:12 +01:00
return result
}