mirror of
				https://codeberg.org/forgejo/forgejo.git
				synced 2025-10-31 06:21:11 +00:00 
			
		
		
		
	Fix error handling & add timestamp check
This commit is contained in:
		
					parent
					
						
							
								40ec049013
							
						
					
				
			
			
				commit
				
					
						dabd773f6b
					
				
			
		
					 5 changed files with 56 additions and 27 deletions
				
			
		|  | @ -4,6 +4,8 @@ | |||
| package forgefed | ||||
| 
 | ||||
| import ( | ||||
| 	"time" | ||||
| 
 | ||||
| 	"code.gitea.io/gitea/modules/validation" | ||||
| 	ap "github.com/go-ap/activitypub" | ||||
| ) | ||||
|  | @ -15,22 +17,26 @@ type ForgeLike struct { | |||
| 	ap.Activity | ||||
| } | ||||
| 
 | ||||
| func (s ForgeLike) MarshalJSON() ([]byte, error) { | ||||
| 	return s.Activity.MarshalJSON() | ||||
| func (like ForgeLike) MarshalJSON() ([]byte, error) { | ||||
| 	return like.Activity.MarshalJSON() | ||||
| } | ||||
| 
 | ||||
| func (s *ForgeLike) UnmarshalJSON(data []byte) error { | ||||
| 	return s.Activity.UnmarshalJSON(data) | ||||
| func (like *ForgeLike) UnmarshalJSON(data []byte) error { | ||||
| 	return like.Activity.UnmarshalJSON(data) | ||||
| } | ||||
| 
 | ||||
| func (s ForgeLike) Validate() []string { | ||||
| 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(s.Type), "type")...) | ||||
| 	result = append(result, validation.ValidateOneOf(string(s.Type), []any{"Like"})...) | ||||
| 	result = append(result, validation.ValidateNotEmpty(s.Actor.GetID().String(), "actor")...) | ||||
| 	result = append(result, validation.ValidateNotEmpty(s.Object.GetID().String(), "object")...) | ||||
| 	result = append(result, validation.ValidateNotEmpty(s.StartTime.String(), "startTime")...) | ||||
| 	if s.StartTime.IsZero() { | ||||
| 	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() { | ||||
| 		result = append(result, "StartTime was invalid.") | ||||
| 	} | ||||
| 
 | ||||
|  |  | |||
|  | @ -4,6 +4,8 @@ | |||
| package forgefed | ||||
| 
 | ||||
| import ( | ||||
| 	"time" | ||||
| 
 | ||||
| 	"code.gitea.io/gitea/modules/timeutil" | ||||
| 	"code.gitea.io/gitea/modules/validation" | ||||
| ) | ||||
|  | @ -14,7 +16,7 @@ type FederationInfo struct { | |||
| 	ID             int64              `xorm:"pk autoincr"` | ||||
| 	HostFqdn       string             `xorm:"host_fqdn UNIQUE INDEX VARCHAR(255) NOT NULL"` | ||||
| 	NodeInfo       NodeInfo           `xorm:"extends NOT NULL"` | ||||
| 	LatestActivity timeutil.TimeStamp `xorm:"NOT NULL"` | ||||
| 	LatestActivity time.Time          `xorm:"NOT NULL"` | ||||
| 	Create         timeutil.TimeStamp `xorm:"created"` | ||||
| 	Updated        timeutil.TimeStamp `xorm:"updated"` | ||||
| } | ||||
|  |  | |||
|  | @ -50,3 +50,11 @@ func CreateFederationInfo(ctx context.Context, info FederationInfo) error { | |||
| 	_, err := db.GetEngine(ctx).Insert(info) | ||||
| 	return err | ||||
| } | ||||
| 
 | ||||
| func UpdateFederationInfo(ctx context.Context, info FederationInfo) error { | ||||
| 	if res, err := validation.IsValid(info); !res { | ||||
| 		return fmt.Errorf("FederationInfo is not valid: %v", err) | ||||
| 	} | ||||
| 	_, err := db.GetEngine(ctx).ID(info.ID).Update(info) | ||||
| 	return err | ||||
| } | ||||
|  |  | |||
|  | @ -5,8 +5,8 @@ package forgefed | |||
| 
 | ||||
| import ( | ||||
| 	"testing" | ||||
| 	"time" | ||||
| 
 | ||||
| 	"code.gitea.io/gitea/modules/timeutil" | ||||
| 	"code.gitea.io/gitea/modules/validation" | ||||
| ) | ||||
| 
 | ||||
|  | @ -16,7 +16,7 @@ func Test_FederationInfoValidation(t *testing.T) { | |||
| 		NodeInfo: NodeInfo{ | ||||
| 			Source: "forgejo", | ||||
| 		}, | ||||
| 		LatestActivity: timeutil.TimeStampNow(), | ||||
| 		LatestActivity: time.Now(), | ||||
| 	} | ||||
| 	if res, err := validation.IsValid(sut); !res { | ||||
| 		t.Errorf("sut should be valid but was %q", err) | ||||
|  | @ -25,7 +25,7 @@ func Test_FederationInfoValidation(t *testing.T) { | |||
| 	sut = FederationInfo{ | ||||
| 		HostFqdn:       "host.do.main", | ||||
| 		NodeInfo:       NodeInfo{}, | ||||
| 		LatestActivity: timeutil.TimeStampNow(), | ||||
| 		LatestActivity: time.Now(), | ||||
| 	} | ||||
| 	if res, _ := validation.IsValid(sut); res { | ||||
| 		t.Errorf("sut should be invalid") | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue