summaryrefslogtreecommitdiffstats
path: root/models/repo/following_repo.go
blob: 85b96aa147cad9ef92c51c55ea9d65f64a8f8a6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package repo

import (
	"code.gitea.io/gitea/modules/validation"
)

// FollowingRepo represents a federated Repository Actor connected with a local Repo
type FollowingRepo struct {
	ID               int64  `xorm:"pk autoincr"`
	RepoID           int64  `xorm:"UNIQUE(federation_repo_mapping) NOT NULL"`
	ExternalID       string `xorm:"UNIQUE(federation_repo_mapping) NOT NULL"`
	FederationHostID int64  `xorm:"UNIQUE(federation_repo_mapping) NOT NULL"`
	URI              string
}

func NewFollowingRepo(repoID int64, externalID string, federationHostID int64, uri string) (FollowingRepo, error) {
	result := FollowingRepo{
		RepoID:           repoID,
		ExternalID:       externalID,
		FederationHostID: federationHostID,
		URI:              uri,
	}
	if valid, err := validation.IsValid(result); !valid {
		return FollowingRepo{}, err
	}
	return result, nil
}

func (user FollowingRepo) Validate() []string {
	var result []string
	result = append(result, validation.ValidateNotEmpty(user.RepoID, "UserID")...)
	result = append(result, validation.ValidateNotEmpty(user.ExternalID, "ExternalID")...)
	result = append(result, validation.ValidateNotEmpty(user.FederationHostID, "FederationHostID")...)
	result = append(result, validation.ValidateNotEmpty(user.URI, "Uri")...)
	return result
}