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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package typeutils
import (
"fmt"
"net/url"
"slices"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
func APIVisToVis(m apimodel.Visibility) gtsmodel.Visibility {
switch m {
case apimodel.VisibilityPublic:
return gtsmodel.VisibilityPublic
case apimodel.VisibilityUnlisted:
return gtsmodel.VisibilityUnlocked
case apimodel.VisibilityPrivate:
return gtsmodel.VisibilityFollowersOnly
case apimodel.VisibilityMutualsOnly:
return gtsmodel.VisibilityMutualsOnly
case apimodel.VisibilityDirect:
return gtsmodel.VisibilityDirect
case apimodel.VisibilityNone:
return gtsmodel.VisibilityNone
}
return 0
}
func APIMarkerNameToMarkerName(m apimodel.MarkerName) gtsmodel.MarkerName {
switch m {
case apimodel.MarkerNameHome:
return gtsmodel.MarkerNameHome
case apimodel.MarkerNameNotifications:
return gtsmodel.MarkerNameNotifications
}
return ""
}
func APIFilterActionToFilterAction(m apimodel.FilterAction) gtsmodel.FilterAction {
switch m {
case apimodel.FilterActionWarn:
return gtsmodel.FilterActionWarn
case apimodel.FilterActionHide:
return gtsmodel.FilterActionHide
}
return gtsmodel.FilterActionNone
}
func APIPolicyValueToPolicyValue(u apimodel.PolicyValue) (gtsmodel.PolicyValue, error) {
switch u {
case apimodel.PolicyValuePublic:
return gtsmodel.PolicyValuePublic, nil
case apimodel.PolicyValueFollowers:
return gtsmodel.PolicyValueFollowers, nil
case apimodel.PolicyValueFollowing:
return gtsmodel.PolicyValueFollowing, nil
case apimodel.PolicyValueMutuals:
return gtsmodel.PolicyValueMutuals, nil
case apimodel.PolicyValueMentioned:
return gtsmodel.PolicyValueMentioned, nil
case apimodel.PolicyValueAuthor:
return gtsmodel.PolicyValueAuthor, nil
case apimodel.PolicyValueMe:
err := fmt.Errorf("policyURI %s has no corresponding internal model", apimodel.PolicyValueMe)
return "", err
default:
// Parse URI to ensure it's a
// url with a valid protocol.
url, err := url.Parse(string(u))
if err != nil {
err := fmt.Errorf("could not parse non-predefined policy value as uri: %w", err)
return "", err
}
if url.Host != "http" && url.Host != "https" {
err := fmt.Errorf("non-predefined policy values must have protocol 'http' or 'https' (%s)", u)
return "", err
}
return gtsmodel.PolicyValue(u), nil
}
}
func APIInteractionPolicyToInteractionPolicy(
p *apimodel.InteractionPolicy,
v apimodel.Visibility,
) (*gtsmodel.InteractionPolicy, error) {
visibility := APIVisToVis(v)
convertURIs := func(apiURIs []apimodel.PolicyValue) (gtsmodel.PolicyValues, error) {
policyURIs := gtsmodel.PolicyValues{}
for _, apiURI := range apiURIs {
uri, err := APIPolicyValueToPolicyValue(apiURI)
if err != nil {
return nil, err
}
if !uri.FeasibleForVisibility(visibility) {
err := fmt.Errorf("policyURI %s is not feasible for visibility %s", apiURI, v)
return nil, err
}
policyURIs = append(policyURIs, uri)
}
return policyURIs, nil
}
canLikeAlways, err := convertURIs(p.CanFavourite.Always)
if err != nil {
err := fmt.Errorf("error converting %s.can_favourite.always: %w", v, err)
return nil, err
}
canLikeWithApproval, err := convertURIs(p.CanFavourite.WithApproval)
if err != nil {
err := fmt.Errorf("error converting %s.can_favourite.with_approval: %w", v, err)
return nil, err
}
canReplyAlways, err := convertURIs(p.CanReply.Always)
if err != nil {
err := fmt.Errorf("error converting %s.can_reply.always: %w", v, err)
return nil, err
}
canReplyWithApproval, err := convertURIs(p.CanReply.WithApproval)
if err != nil {
err := fmt.Errorf("error converting %s.can_reply.with_approval: %w", v, err)
return nil, err
}
canAnnounceAlways, err := convertURIs(p.CanReblog.Always)
if err != nil {
err := fmt.Errorf("error converting %s.can_reblog.always: %w", v, err)
return nil, err
}
canAnnounceWithApproval, err := convertURIs(p.CanReblog.WithApproval)
if err != nil {
err := fmt.Errorf("error converting %s.can_reblog.with_approval: %w", v, err)
return nil, err
}
// Normalize URIs.
//
// 1. Ensure canLikeAlways, canReplyAlways,
// and canAnnounceAlways include self
// (either explicitly or within public).
// ensureIncludesSelf adds the "author" PolicyValue
// to given slice of PolicyValues, if not already
// explicitly or implicitly included.
ensureIncludesSelf := func(vals gtsmodel.PolicyValues) gtsmodel.PolicyValues {
includesSelf := slices.ContainsFunc(
vals,
func(uri gtsmodel.PolicyValue) bool {
return uri == gtsmodel.PolicyValuePublic ||
uri == gtsmodel.PolicyValueAuthor
},
)
if includesSelf {
// This slice of policy values
// already includes self explicitly
// or implicitly, nothing to change.
return vals
}
// Need to add self/author to
// this slice of policy values.
vals = append(vals, gtsmodel.PolicyValueAuthor)
return vals
}
canLikeAlways = ensureIncludesSelf(canLikeAlways)
canReplyAlways = ensureIncludesSelf(canReplyAlways)
canAnnounceAlways = ensureIncludesSelf(canAnnounceAlways)
// 2. Ensure canReplyAlways includes mentioned
// accounts (either explicitly or within public).
if !slices.ContainsFunc(
canReplyAlways,
func(uri gtsmodel.PolicyValue) bool {
return uri == gtsmodel.PolicyValuePublic ||
uri == gtsmodel.PolicyValueMentioned
},
) {
canReplyAlways = append(
canReplyAlways,
gtsmodel.PolicyValueMentioned,
)
}
return >smodel.InteractionPolicy{
CanLike: gtsmodel.PolicyRules{
Always: canLikeAlways,
WithApproval: canLikeWithApproval,
},
CanReply: gtsmodel.PolicyRules{
Always: canReplyAlways,
WithApproval: canReplyWithApproval,
},
CanAnnounce: gtsmodel.PolicyRules{
Always: canAnnounceAlways,
WithApproval: canAnnounceWithApproval,
},
}, nil
}
|