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
if params.RemoteAccountID != nil {
switch {
case params.RemoteAccountID != nil:
// try with uri
if a, dbErr := d.db.GetAccountByURI(ctx, params.RemoteAccountID.String()); dbErr == nil {
remoteAccount = a
} else if dbErr != db.ErrNoEntries {
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
a := &gtsmodel.Account{}
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 {
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")
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
// already about what the account domain might be; this var will be overwritten later if necessary
var accountDomain string
if remoteAccount != nil {
switch {
case remoteAccount != nil:
accountDomain = remoteAccount.Domain
} else if params.RemoteAccountID != nil {
case params.RemoteAccountID != nil:
accountDomain = params.RemoteAccountID.Host
} else {
default:
accountDomain = params.RemoteAccountHost
}
@ -227,41 +229,41 @@ func (d *deref) GetRemoteAccount(ctx context.Context, params GetRemoteAccountPar
}
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

View file

@ -1787,26 +1787,6 @@ func NewTestFediPeople() map[string]vocab.ActivityStreamsPerson {
"image/png",
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 @@ import (
"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.
//
// 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.
func NewMockHTTPClient(do func(req *http.Request) (*http.Response, error), relativeMediaPath string) *MockHTTPClient {
mockHttpClient := &MockHTTPClient{}
mockHTTPClient := &MockHTTPClient{}
if do != nil {
mockHttpClient.do = do
return mockHttpClient
mockHTTPClient.do = do
return mockHTTPClient
}
mockHttpClient.testRemoteStatuses = NewTestFediStatuses()
mockHttpClient.testRemotePeople = NewTestFediPeople()
mockHttpClient.testRemoteGroups = NewTestFediGroups()
mockHttpClient.testRemoteServices = NewTestFediServices()
mockHttpClient.testRemoteAttachments = NewTestFediAttachments(relativeMediaPath)
mockHTTPClient.testRemoteStatuses = NewTestFediStatuses()
mockHTTPClient.testRemotePeople = NewTestFediPeople()
mockHTTPClient.testRemoteGroups = NewTestFediGroups()
mockHTTPClient.testRemoteServices = NewTestFediServices()
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
responseBytes := []byte(`{"error":"404 not found"}`)
responseContentType := "application/json"
responseContentType := applicationJSON
responseContentLength := len(responseBytes)
if req.Method == http.MethodPost {
@ -98,70 +101,70 @@ func NewMockHTTPClient(do func(req *http.Request) (*http.Response, error), relat
if err != nil {
panic(err)
}
mockHttpClient.SentMessages[req.URL.String()] = b
mockHTTPClient.SentMessages[req.URL.String()] = b
responseCode = http.StatusOK
responseBytes = []byte(`{"ok":"accepted"}`)
responseContentType = "application/json"
responseContentType = applicationJSON
responseContentLength = len(responseBytes)
} else if strings.Contains(req.URL.String(), ".well-known/webfinger") {
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
noteI, err := streams.Serialize(note)
if err != nil {
panic(err)
}
noteJson, err := json.Marshal(noteI)
noteJSON, err := json.Marshal(noteI)
if err != nil {
panic(err)
}
responseCode = http.StatusOK
responseBytes = noteJson
responseContentType = "application/activity+json"
responseContentLength = len(noteJson)
} else if person, ok := mockHttpClient.testRemotePeople[req.URL.String()]; ok {
responseBytes = noteJSON
responseContentType = applicationActivityJSON
responseContentLength = len(noteJSON)
} else if person, ok := mockHTTPClient.testRemotePeople[req.URL.String()]; ok {
// the request is for a person that we have stored
personI, err := streams.Serialize(person)
if err != nil {
panic(err)
}
personJson, err := json.Marshal(personI)
personJSON, err := json.Marshal(personI)
if err != nil {
panic(err)
}
responseCode = http.StatusOK
responseBytes = personJson
responseContentType = "application/activity+json"
responseContentLength = len(personJson)
} else if group, ok := mockHttpClient.testRemoteGroups[req.URL.String()]; ok {
responseBytes = personJSON
responseContentType = applicationActivityJSON
responseContentLength = len(personJSON)
} else if group, ok := mockHTTPClient.testRemoteGroups[req.URL.String()]; ok {
// the request is for a person that we have stored
groupI, err := streams.Serialize(group)
if err != nil {
panic(err)
}
groupJson, err := json.Marshal(groupI)
groupJSON, err := json.Marshal(groupI)
if err != nil {
panic(err)
}
responseCode = http.StatusOK
responseBytes = groupJson
responseContentType = "application/activity+json"
responseContentLength = len(groupJson)
} else if service, ok := mockHttpClient.testRemoteServices[req.URL.String()]; ok {
responseBytes = groupJSON
responseContentType = applicationActivityJSON
responseContentLength = len(groupJSON)
} else if service, ok := mockHTTPClient.testRemoteServices[req.URL.String()]; ok {
serviceI, err := streams.Serialize(service)
if err != nil {
panic(err)
}
serviceJson, err := json.Marshal(serviceI)
serviceJSON, err := json.Marshal(serviceI)
if err != nil {
panic(err)
}
responseCode = http.StatusOK
responseBytes = serviceJson
responseContentType = "application/activity+json"
responseContentLength = len(serviceJson)
} else if attachment, ok := mockHttpClient.testRemoteAttachments[req.URL.String()]; ok {
responseBytes = serviceJSON
responseContentType = applicationActivityJSON
responseContentLength = len(serviceJSON)
} else if attachment, ok := mockHTTPClient.testRemoteAttachments[req.URL.String()]; ok {
responseCode = http.StatusOK
responseBytes = attachment.Data
responseContentType = attachment.ContentType
@ -181,7 +184,7 @@ func NewMockHTTPClient(do func(req *http.Request) (*http.Response, error), relat
}, nil
}
return mockHttpClient
return mockHTTPClient
}
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{
{
Rel: "self",
Type: "application/activity+json",
Type: applicationActivityJSON,
Href: "https://unknown-instance.com/groups/some_group",
},
},
@ -209,7 +212,7 @@ func WebfingerResponse(req *http.Request) (responseCode int, responseBytes []byt
Links: []apimodel.Link{
{
Rel: "self",
Type: "application/activity+json",
Type: applicationActivityJSON,
Href: "https://owncast.example.org/federation/user/rgh",
},
},
@ -220,7 +223,7 @@ func WebfingerResponse(req *http.Request) (responseCode int, responseBytes []byt
Links: []apimodel.Link{
{
Rel: "self",
Type: "application/activity+json",
Type: applicationActivityJSON,
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{
{
Rel: "self",
Type: "application/activity+json",
Type: applicationActivityJSON,
Href: "https://turnip.farm/users/turniplover6969",
},
},
@ -242,7 +245,7 @@ func WebfingerResponse(req *http.Request) (responseCode int, responseBytes []byt
Links: []apimodel.Link{
{
Rel: "self",
Type: "application/activity+json",
Type: applicationActivityJSON,
Href: "https://fossbros-anonymous.io/users/foss_satan",
},
},
@ -253,7 +256,7 @@ func WebfingerResponse(req *http.Request) (responseCode int, responseBytes []byt
Links: []apimodel.Link{
{
Rel: "self",
Type: "application/activity+json",
Type: applicationActivityJSON,
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)
responseCode = http.StatusNotFound
responseBytes = []byte(`{"error":"not found"}`)
responseContentType = "application/json"
responseContentType = applicationJSON
responseContentLength = len(responseBytes)
return
}
wfrJson, err := json.Marshal(wfr)
wfrJSON, err := json.Marshal(wfr)
if err != nil {
panic(err)
}
responseCode = http.StatusOK
responseBytes = wfrJson
responseContentType = "application/json"
responseContentLength = len(wfrJson)
responseBytes = wfrJSON
responseContentType = applicationJSON
responseContentLength = len(wfrJSON)
return
}