summaryrefslogtreecommitdiffstats
path: root/modules/forgefed/actor.go
blob: 0ef46185d1e97589f92eeca99ff97b9c0e24335d (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2023, 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package forgefed

import (
	"fmt"
	"net/url"
	"strings"

	"code.gitea.io/gitea/modules/validation"

	ap "github.com/go-ap/activitypub"
)

// ----------------------------- ActorID --------------------------------------------
type ActorID struct {
	ID               string
	Source           string
	Schema           string
	Path             string
	Host             string
	Port             string
	UnvalidatedInput string
}

// Factory function for ActorID. Created struct is asserted to be valid
func NewActorID(uri string) (ActorID, error) {
	result, err := newActorID(uri)
	if err != nil {
		return ActorID{}, err
	}

	if valid, err := validation.IsValid(result); !valid {
		return ActorID{}, err
	}

	return result, nil
}

func (id ActorID) AsURI() string {
	var result string
	if id.Port == "" {
		result = fmt.Sprintf("%s://%s/%s/%s", id.Schema, id.Host, id.Path, id.ID)
	} else {
		result = fmt.Sprintf("%s://%s:%s/%s/%s", id.Schema, id.Host, id.Port, id.Path, id.ID)
	}
	return result
}

func (id ActorID) Validate() []string {
	var result []string
	result = append(result, validation.ValidateNotEmpty(id.ID, "userId")...)
	result = append(result, validation.ValidateNotEmpty(id.Schema, "schema")...)
	result = append(result, validation.ValidateNotEmpty(id.Path, "path")...)
	result = append(result, validation.ValidateNotEmpty(id.Host, "host")...)
	result = append(result, validation.ValidateNotEmpty(id.UnvalidatedInput, "unvalidatedInput")...)

	if id.UnvalidatedInput != id.AsURI() {
		result = append(result, fmt.Sprintf("not all input was parsed, \nUnvalidated Input:%q \nParsed URI: %q", id.UnvalidatedInput, id.AsURI()))
	}

	return result
}

// ----------------------------- PersonID --------------------------------------------
type PersonID struct {
	ActorID
}

// Factory function for PersonID. Created struct is asserted to be valid
func NewPersonID(uri, source string) (PersonID, error) {
	result, err := newActorID(uri)
	if err != nil {
		return PersonID{}, err
	}
	result.Source = source

	// validate Person specific path
	personID := PersonID{result}
	if valid, err := validation.IsValid(personID); !valid {
		return PersonID{}, err
	}

	return personID, nil
}

func (id PersonID) AsWebfinger() string {
	result := fmt.Sprintf("@%s@%s", strings.ToLower(id.ID), strings.ToLower(id.Host))
	return result
}

func (id PersonID) AsLoginName() string {
	result := fmt.Sprintf("%s%s", strings.ToLower(id.ID), id.HostSuffix())
	return result
}

func (id PersonID) HostSuffix() string {
	result := fmt.Sprintf("-%s", strings.ToLower(id.Host))
	return result
}

func (id PersonID) Validate() []string {
	result := id.ActorID.Validate()
	result = append(result, validation.ValidateNotEmpty(id.Source, "source")...)
	result = append(result, validation.ValidateOneOf(id.Source, []any{"forgejo", "gitea"}, "Source")...)
	switch id.Source {
	case "forgejo", "gitea":
		if strings.ToLower(id.Path) != "api/v1/activitypub/user-id" && strings.ToLower(id.Path) != "api/activitypub/user-id" {
			result = append(result, fmt.Sprintf("path: %q has to be a person specific api path", id.Path))
		}
	}
	return result
}

// ----------------------------- RepositoryID --------------------------------------------

type RepositoryID struct {
	ActorID
}

// Factory function for RepositoryID. Created struct is asserted to be valid.
func NewRepositoryID(uri, source string) (RepositoryID, error) {
	result, err := newActorID(uri)
	if err != nil {
		return RepositoryID{}, err
	}
	result.Source = source

	// validate Person specific
	repoID := RepositoryID{result}
	if valid, err := validation.IsValid(repoID); !valid {
		return RepositoryID{}, err
	}

	return repoID, nil
}

func (id RepositoryID) Validate() []string {
	result := id.ActorID.Validate()
	result = append(result, validation.ValidateNotEmpty(id.Source, "source")...)
	result = append(result, validation.ValidateOneOf(id.Source, []any{"forgejo", "gitea"}, "Source")...)
	switch id.Source {
	case "forgejo", "gitea":
		if strings.ToLower(id.Path) != "api/v1/activitypub/repository-id" && strings.ToLower(id.Path) != "api/activitypub/repository-id" {
			result = append(result, fmt.Sprintf("path: %q has to be a repo specific api path", id.Path))
		}
	}
	return result
}

func containsEmptyString(ar []string) bool {
	for _, elem := range ar {
		if elem == "" {
			return true
		}
	}
	return false
}

func removeEmptyStrings(ls []string) []string {
	var rs []string
	for _, str := range ls {
		if str != "" {
			rs = append(rs, str)
		}
	}
	return rs
}

func newActorID(uri string) (ActorID, error) {
	validatedURI, err := url.ParseRequestURI(uri)
	if err != nil {
		return ActorID{}, err
	}
	pathWithActorID := strings.Split(validatedURI.Path, "/")
	if containsEmptyString(pathWithActorID) {
		pathWithActorID = removeEmptyStrings(pathWithActorID)
	}
	length := len(pathWithActorID)
	pathWithoutActorID := strings.Join(pathWithActorID[0:length-1], "/")
	id := pathWithActorID[length-1]

	result := ActorID{}
	result.ID = id
	result.Schema = validatedURI.Scheme
	result.Host = validatedURI.Hostname()
	result.Path = pathWithoutActorID
	result.Port = validatedURI.Port()
	result.UnvalidatedInput = uri
	return result, nil
}

// ----------------------------- ForgePerson -------------------------------------

// ForgePerson activity data type
// swagger:model
type ForgePerson struct {
	// swagger:ignore
	ap.Actor
}

func (s ForgePerson) MarshalJSON() ([]byte, error) {
	return s.Actor.MarshalJSON()
}

func (s *ForgePerson) UnmarshalJSON(data []byte) error {
	return s.Actor.UnmarshalJSON(data)
}

func (s ForgePerson) Validate() []string {
	var result []string
	result = append(result, validation.ValidateNotEmpty(string(s.Type), "Type")...)
	result = append(result, validation.ValidateOneOf(string(s.Type), []any{string(ap.PersonType)}, "Type")...)
	result = append(result, validation.ValidateNotEmpty(s.PreferredUsername.String(), "PreferredUsername")...)

	return result
}