cheeky linting with the lads

This commit is contained in:
tsmethurst 2022-06-02 12:56:21 +02:00
parent bbe2985d9b
commit c0ab7f5d69
3 changed files with 91 additions and 106 deletions

View file

@ -101,14 +101,15 @@ func (d *deref) GetRemoteAccount(ctx context.Context, params GetRemoteAccountPar
*/ */
// first check if we can retrieve the account locally just with what we've been given // first check if we can retrieve the account locally just with what we've been given
if params.RemoteAccountID != nil { switch {
case params.RemoteAccountID != nil:
// try with uri // try with uri
if a, dbErr := d.db.GetAccountByURI(ctx, params.RemoteAccountID.String()); dbErr == nil { if a, dbErr := d.db.GetAccountByURI(ctx, params.RemoteAccountID.String()); dbErr == nil {
remoteAccount = a remoteAccount = a
} else if dbErr != db.ErrNoEntries { } else if dbErr != db.ErrNoEntries {
err = fmt.Errorf("GetRemoteAccount: database error looking for account %s: %s", params.RemoteAccountID, err) err = fmt.Errorf("GetRemoteAccount: database error looking for account %s: %s", params.RemoteAccountID, err)
} }
} else if params.RemoteAccountUsername != "" && params.RemoteAccountHost != "" { case params.RemoteAccountUsername != "" && params.RemoteAccountHost != "":
// try with username/host // try with username/host
a := &gtsmodel.Account{} a := &gtsmodel.Account{}
where := []db.Where{{Key: "username", Value: params.RemoteAccountUsername}, {Key: "domain", Value: params.RemoteAccountHost}} where := []db.Where{{Key: "username", Value: params.RemoteAccountUsername}, {Key: "domain", Value: params.RemoteAccountHost}}
@ -117,7 +118,7 @@ func (d *deref) GetRemoteAccount(ctx context.Context, params GetRemoteAccountPar
} else if dbErr != db.ErrNoEntries { } else if dbErr != db.ErrNoEntries {
err = fmt.Errorf("GetRemoteAccount: database error looking for account with username %s and host %s: %s", params.RemoteAccountUsername, params.RemoteAccountHost, err) err = fmt.Errorf("GetRemoteAccount: database error looking for account with username %s and host %s: %s", params.RemoteAccountUsername, params.RemoteAccountHost, err)
} }
} else { default:
err = errors.New("GetRemoteAccount: no identifying parameters were set so we cannot get account") err = errors.New("GetRemoteAccount: no identifying parameters were set so we cannot get account")
return return
} }
@ -163,11 +164,12 @@ func (d *deref) GetRemoteAccount(ctx context.Context, params GetRemoteAccountPar
// we finger to fetch the account domain but just in case we're not fingering, make a best guess // we finger to fetch the account domain but just in case we're not fingering, make a best guess
// already about what the account domain might be; this var will be overwritten later if necessary // already about what the account domain might be; this var will be overwritten later if necessary
var accountDomain string var accountDomain string
if remoteAccount != nil { switch {
case remoteAccount != nil:
accountDomain = remoteAccount.Domain accountDomain = remoteAccount.Domain
} else if params.RemoteAccountID != nil { case params.RemoteAccountID != nil:
accountDomain = params.RemoteAccountID.Host accountDomain = params.RemoteAccountID.Host
} else { default:
accountDomain = params.RemoteAccountHost accountDomain = params.RemoteAccountHost
} }
@ -227,41 +229,41 @@ func (d *deref) GetRemoteAccount(ctx context.Context, params GetRemoteAccountPar
} }
return // the new account return // the new account
} else {
// we had the account already, but now we know the account domain, so update it if it's different
if !strings.EqualFold(remoteAccount.Domain, accountDomain) {
remoteAccount.Domain = accountDomain
remoteAccount, err = d.db.UpdateAccount(ctx, remoteAccount)
if err != nil {
err = fmt.Errorf("GetRemoteAccount: error updating account: %s", err)
return
}
}
// make sure the account fields are populated before returning:
// the caller might want to block until everything is loaded
var fieldsChanged bool
fieldsChanged, err = d.populateAccountFields(ctx, remoteAccount, params.RequestingUsername, params.Blocking)
if err != nil {
return nil, fmt.Errorf("GetRemoteAccount: error populating remoteAccount fields: %s", err)
}
var fingeredChanged bool
if !fingered.IsZero() {
fingeredChanged = true
remoteAccount.LastWebfingeredAt = fingered
}
if fieldsChanged || fingeredChanged {
remoteAccount.UpdatedAt = time.Now()
remoteAccount, err = d.db.UpdateAccount(ctx, remoteAccount)
if err != nil {
return nil, fmt.Errorf("GetRemoteAccount: error updating remoteAccount: %s", err)
}
}
return // the account we already had + possibly updated
} }
// we had the account already, but now we know the account domain, so update it if it's different
if !strings.EqualFold(remoteAccount.Domain, accountDomain) {
remoteAccount.Domain = accountDomain
remoteAccount, err = d.db.UpdateAccount(ctx, remoteAccount)
if err != nil {
err = fmt.Errorf("GetRemoteAccount: error updating account: %s", err)
return
}
}
// make sure the account fields are populated before returning:
// the caller might want to block until everything is loaded
var fieldsChanged bool
fieldsChanged, err = d.populateAccountFields(ctx, remoteAccount, params.RequestingUsername, params.Blocking)
if err != nil {
return nil, fmt.Errorf("GetRemoteAccount: error populating remoteAccount fields: %s", err)
}
var fingeredChanged bool
if !fingered.IsZero() {
fingeredChanged = true
remoteAccount.LastWebfingeredAt = fingered
}
if fieldsChanged || fingeredChanged {
remoteAccount.UpdatedAt = time.Now()
remoteAccount, err = d.db.UpdateAccount(ctx, remoteAccount)
if err != nil {
return nil, fmt.Errorf("GetRemoteAccount: error updating remoteAccount: %s", err)
}
}
return // the account we already had + possibly updated
} }
// dereferenceAccountable calls remoteAccountID with a GET request, and tries to parse whatever // dereferenceAccountable calls remoteAccountID with a GET request, and tries to parse whatever

View file

@ -1787,26 +1787,6 @@ func NewTestFediPeople() map[string]vocab.ActivityStreamsPerson {
"image/png", "image/png",
false, false,
), ),
" ://unknown-instance.com/users/brand_new_person": newAPPerson(
URLMustParse("https://unknown-instance.com/users/brand_new_person"),
URLMustParse("https://unknown-instance.com/users/brand_new_person/following"),
URLMustParse("https://unknown-instance.com/users/brand_new_person/followers"),
URLMustParse("https://unknown-instance.com/users/brand_new_person/inbox"),
URLMustParse("https://unknown-instance.com/users/brand_new_person/outbox"),
URLMustParse("https://unknown-instance.com/users/brand_new_person/collections/featured"),
"brand_new_person",
"Geoff Brando New Personson",
"hey I'm a new person, your instance hasn't seen me yet uwu",
URLMustParse("https://unknown-instance.com/@brand_new_person"),
true,
URLMustParse("https://unknown-instance.com/users/brand_new_person#main-key"),
newPerson1Pub,
nil,
"image/jpeg",
nil,
"image/png",
false,
),
} }
} }

View file

@ -37,6 +37,9 @@
"github.com/superseriousbusiness/gotosocial/internal/transport" "github.com/superseriousbusiness/gotosocial/internal/transport"
) )
const applicationJSON = "application/json"
const applicationActivityJSON = "application/activity+json"
// NewTestTransportController returns a test transport controller with the given http client. // NewTestTransportController returns a test transport controller with the given http client.
// //
// Obviously for testing purposes you should not be making actual http calls to other servers. // Obviously for testing purposes you should not be making actual http calls to other servers.
@ -72,25 +75,25 @@ type MockHTTPClient struct {
// //
// Note that you should never ever make ACTUAL http calls with this thing. // Note that you should never ever make ACTUAL http calls with this thing.
func NewMockHTTPClient(do func(req *http.Request) (*http.Response, error), relativeMediaPath string) *MockHTTPClient { func NewMockHTTPClient(do func(req *http.Request) (*http.Response, error), relativeMediaPath string) *MockHTTPClient {
mockHttpClient := &MockHTTPClient{} mockHTTPClient := &MockHTTPClient{}
if do != nil { if do != nil {
mockHttpClient.do = do mockHTTPClient.do = do
return mockHttpClient return mockHTTPClient
} }
mockHttpClient.testRemoteStatuses = NewTestFediStatuses() mockHTTPClient.testRemoteStatuses = NewTestFediStatuses()
mockHttpClient.testRemotePeople = NewTestFediPeople() mockHTTPClient.testRemotePeople = NewTestFediPeople()
mockHttpClient.testRemoteGroups = NewTestFediGroups() mockHTTPClient.testRemoteGroups = NewTestFediGroups()
mockHttpClient.testRemoteServices = NewTestFediServices() mockHTTPClient.testRemoteServices = NewTestFediServices()
mockHttpClient.testRemoteAttachments = NewTestFediAttachments(relativeMediaPath) mockHTTPClient.testRemoteAttachments = NewTestFediAttachments(relativeMediaPath)
mockHttpClient.SentMessages = make(map[string][]byte) mockHTTPClient.SentMessages = make(map[string][]byte)
mockHttpClient.do = func(req *http.Request) (*http.Response, error) { mockHTTPClient.do = func(req *http.Request) (*http.Response, error) {
responseCode := http.StatusNotFound responseCode := http.StatusNotFound
responseBytes := []byte(`{"error":"404 not found"}`) responseBytes := []byte(`{"error":"404 not found"}`)
responseContentType := "application/json" responseContentType := applicationJSON
responseContentLength := len(responseBytes) responseContentLength := len(responseBytes)
if req.Method == http.MethodPost { if req.Method == http.MethodPost {
@ -98,70 +101,70 @@ func NewMockHTTPClient(do func(req *http.Request) (*http.Response, error), relat
if err != nil { if err != nil {
panic(err) panic(err)
} }
mockHttpClient.SentMessages[req.URL.String()] = b mockHTTPClient.SentMessages[req.URL.String()] = b
responseCode = http.StatusOK responseCode = http.StatusOK
responseBytes = []byte(`{"ok":"accepted"}`) responseBytes = []byte(`{"ok":"accepted"}`)
responseContentType = "application/json" responseContentType = applicationJSON
responseContentLength = len(responseBytes) responseContentLength = len(responseBytes)
} else if strings.Contains(req.URL.String(), ".well-known/webfinger") { } else if strings.Contains(req.URL.String(), ".well-known/webfinger") {
responseCode, responseBytes, responseContentType, responseContentLength = WebfingerResponse(req) responseCode, responseBytes, responseContentType, responseContentLength = WebfingerResponse(req)
} else if note, ok := mockHttpClient.testRemoteStatuses[req.URL.String()]; ok { } else if note, ok := mockHTTPClient.testRemoteStatuses[req.URL.String()]; ok {
// the request is for a note that we have stored // the request is for a note that we have stored
noteI, err := streams.Serialize(note) noteI, err := streams.Serialize(note)
if err != nil { if err != nil {
panic(err) panic(err)
} }
noteJson, err := json.Marshal(noteI) noteJSON, err := json.Marshal(noteI)
if err != nil { if err != nil {
panic(err) panic(err)
} }
responseCode = http.StatusOK responseCode = http.StatusOK
responseBytes = noteJson responseBytes = noteJSON
responseContentType = "application/activity+json" responseContentType = applicationActivityJSON
responseContentLength = len(noteJson) responseContentLength = len(noteJSON)
} else if person, ok := mockHttpClient.testRemotePeople[req.URL.String()]; ok { } else if person, ok := mockHTTPClient.testRemotePeople[req.URL.String()]; ok {
// the request is for a person that we have stored // the request is for a person that we have stored
personI, err := streams.Serialize(person) personI, err := streams.Serialize(person)
if err != nil { if err != nil {
panic(err) panic(err)
} }
personJson, err := json.Marshal(personI) personJSON, err := json.Marshal(personI)
if err != nil { if err != nil {
panic(err) panic(err)
} }
responseCode = http.StatusOK responseCode = http.StatusOK
responseBytes = personJson responseBytes = personJSON
responseContentType = "application/activity+json" responseContentType = applicationActivityJSON
responseContentLength = len(personJson) responseContentLength = len(personJSON)
} else if group, ok := mockHttpClient.testRemoteGroups[req.URL.String()]; ok { } else if group, ok := mockHTTPClient.testRemoteGroups[req.URL.String()]; ok {
// the request is for a person that we have stored // the request is for a person that we have stored
groupI, err := streams.Serialize(group) groupI, err := streams.Serialize(group)
if err != nil { if err != nil {
panic(err) panic(err)
} }
groupJson, err := json.Marshal(groupI) groupJSON, err := json.Marshal(groupI)
if err != nil { if err != nil {
panic(err) panic(err)
} }
responseCode = http.StatusOK responseCode = http.StatusOK
responseBytes = groupJson responseBytes = groupJSON
responseContentType = "application/activity+json" responseContentType = applicationActivityJSON
responseContentLength = len(groupJson) responseContentLength = len(groupJSON)
} else if service, ok := mockHttpClient.testRemoteServices[req.URL.String()]; ok { } else if service, ok := mockHTTPClient.testRemoteServices[req.URL.String()]; ok {
serviceI, err := streams.Serialize(service) serviceI, err := streams.Serialize(service)
if err != nil { if err != nil {
panic(err) panic(err)
} }
serviceJson, err := json.Marshal(serviceI) serviceJSON, err := json.Marshal(serviceI)
if err != nil { if err != nil {
panic(err) panic(err)
} }
responseCode = http.StatusOK responseCode = http.StatusOK
responseBytes = serviceJson responseBytes = serviceJSON
responseContentType = "application/activity+json" responseContentType = applicationActivityJSON
responseContentLength = len(serviceJson) responseContentLength = len(serviceJSON)
} else if attachment, ok := mockHttpClient.testRemoteAttachments[req.URL.String()]; ok { } else if attachment, ok := mockHTTPClient.testRemoteAttachments[req.URL.String()]; ok {
responseCode = http.StatusOK responseCode = http.StatusOK
responseBytes = attachment.Data responseBytes = attachment.Data
responseContentType = attachment.ContentType responseContentType = attachment.ContentType
@ -181,7 +184,7 @@ func NewMockHTTPClient(do func(req *http.Request) (*http.Response, error), relat
}, nil }, nil
} }
return mockHttpClient return mockHTTPClient
} }
func (m *MockHTTPClient) Do(req *http.Request) (*http.Response, error) { func (m *MockHTTPClient) Do(req *http.Request) (*http.Response, error) {
@ -198,7 +201,7 @@ func WebfingerResponse(req *http.Request) (responseCode int, responseBytes []byt
Links: []apimodel.Link{ Links: []apimodel.Link{
{ {
Rel: "self", Rel: "self",
Type: "application/activity+json", Type: applicationActivityJSON,
Href: "https://unknown-instance.com/groups/some_group", Href: "https://unknown-instance.com/groups/some_group",
}, },
}, },
@ -209,7 +212,7 @@ func WebfingerResponse(req *http.Request) (responseCode int, responseBytes []byt
Links: []apimodel.Link{ Links: []apimodel.Link{
{ {
Rel: "self", Rel: "self",
Type: "application/activity+json", Type: applicationActivityJSON,
Href: "https://owncast.example.org/federation/user/rgh", Href: "https://owncast.example.org/federation/user/rgh",
}, },
}, },
@ -220,7 +223,7 @@ func WebfingerResponse(req *http.Request) (responseCode int, responseBytes []byt
Links: []apimodel.Link{ Links: []apimodel.Link{
{ {
Rel: "self", Rel: "self",
Type: "application/activity+json", Type: applicationActivityJSON,
Href: "https://unknown-instance.com/users/brand_new_person", Href: "https://unknown-instance.com/users/brand_new_person",
}, },
}, },
@ -231,7 +234,7 @@ func WebfingerResponse(req *http.Request) (responseCode int, responseBytes []byt
Links: []apimodel.Link{ Links: []apimodel.Link{
{ {
Rel: "self", Rel: "self",
Type: "application/activity+json", Type: applicationActivityJSON,
Href: "https://turnip.farm/users/turniplover6969", Href: "https://turnip.farm/users/turniplover6969",
}, },
}, },
@ -242,7 +245,7 @@ func WebfingerResponse(req *http.Request) (responseCode int, responseBytes []byt
Links: []apimodel.Link{ Links: []apimodel.Link{
{ {
Rel: "self", Rel: "self",
Type: "application/activity+json", Type: applicationActivityJSON,
Href: "https://fossbros-anonymous.io/users/foss_satan", Href: "https://fossbros-anonymous.io/users/foss_satan",
}, },
}, },
@ -253,7 +256,7 @@ func WebfingerResponse(req *http.Request) (responseCode int, responseBytes []byt
Links: []apimodel.Link{ Links: []apimodel.Link{
{ {
Rel: "self", Rel: "self",
Type: "application/activity+json", Type: applicationActivityJSON,
Href: "https://example.org/users/some_user", Href: "https://example.org/users/some_user",
}, },
}, },
@ -264,18 +267,18 @@ func WebfingerResponse(req *http.Request) (responseCode int, responseBytes []byt
logrus.Debugf("webfinger response not available for %s", req.URL) logrus.Debugf("webfinger response not available for %s", req.URL)
responseCode = http.StatusNotFound responseCode = http.StatusNotFound
responseBytes = []byte(`{"error":"not found"}`) responseBytes = []byte(`{"error":"not found"}`)
responseContentType = "application/json" responseContentType = applicationJSON
responseContentLength = len(responseBytes) responseContentLength = len(responseBytes)
return return
} }
wfrJson, err := json.Marshal(wfr) wfrJSON, err := json.Marshal(wfr)
if err != nil { if err != nil {
panic(err) panic(err)
} }
responseCode = http.StatusOK responseCode = http.StatusOK
responseBytes = wfrJson responseBytes = wfrJSON
responseContentType = "application/json" responseContentType = applicationJSON
responseContentLength = len(wfrJson) responseContentLength = len(wfrJSON)
return return
} }