Skip to content

Commit

Permalink
[bugfix] Fix account roles (#1542)
Browse files Browse the repository at this point in the history
* Change account role from string to object

* Update tests

* small fixes + swagger docs

---------

Co-authored-by: zowhoey <11893985+zowhoey@users.noreply.github.com>
  • Loading branch information
tsmethurst and zowhoey committed Feb 20, 2023
1 parent b6143c9 commit e8a04b7
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 73 deletions.
20 changes: 11 additions & 9 deletions docs/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,7 @@ definitions:
type: string
x-go-name: Note
role:
description: |-
Role of the account on this instance.
Omitted for remote accounts.
example: user
type: string
x-go-name: Role
$ref: '#/definitions/accountRole'
source:
$ref: '#/definitions/Source'
statuses_count:
Expand Down Expand Up @@ -346,6 +341,15 @@ definitions:
type: object
x-go-name: Relationship
x-go-package: github.com/superseriousbusiness/gotosocial/internal/api/model
accountRole:
properties:
name:
type: string
x-go-name: Name
title: AccountRole models the role of an account.
type: object
x-go-name: AccountRole
x-go-package: github.com/superseriousbusiness/gotosocial/internal/api/model
adminAccountInfo:
properties:
account:
Expand Down Expand Up @@ -423,9 +427,7 @@ definitions:
type: string
x-go-name: Locale
role:
description: The current role of the account.
type: string
x-go-name: Role
$ref: '#/definitions/accountRole'
silenced:
description: Whether the account is currently silenced
type: boolean
Expand Down
64 changes: 48 additions & 16 deletions internal/api/client/admin/reportsget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() {
"ips": [],
"locale": "",
"invite_request": null,
"role": "user",
"role": {
"name": "user"
},
"confirmed": false,
"approved": false,
"disabled": false,
Expand Down Expand Up @@ -195,7 +197,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() {
"ips": [],
"locale": "en",
"invite_request": "",
"role": "user",
"role": {
"name": "user"
},
"confirmed": true,
"approved": true,
"disabled": false,
Expand All @@ -222,7 +226,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() {
"last_status_at": "2021-10-20T10:40:37.000Z",
"emojis": [],
"fields": [],
"role": "user"
"role": {
"name": "user"
}
},
"created_by_application_id": "01F8MGY43H3N2C8EWPR2FPYEXG"
},
Expand All @@ -236,7 +242,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() {
"ips": [],
"locale": "en",
"invite_request": "",
"role": "admin",
"role": {
"name": "admin"
},
"confirmed": true,
"approved": true,
"disabled": false,
Expand Down Expand Up @@ -264,7 +272,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() {
"emojis": [],
"fields": [],
"enable_rss": true,
"role": "admin"
"role": {
"name": "admin"
}
},
"created_by_application_id": "01F8MGXQRHYF5QPMTMXP78QC2F"
},
Expand All @@ -278,7 +288,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() {
"ips": [],
"locale": "en",
"invite_request": "",
"role": "admin",
"role": {
"name": "admin"
},
"confirmed": true,
"approved": true,
"disabled": false,
Expand Down Expand Up @@ -306,7 +318,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() {
"emojis": [],
"fields": [],
"enable_rss": true,
"role": "admin"
"role": {
"name": "admin"
}
},
"created_by_application_id": "01F8MGXQRHYF5QPMTMXP78QC2F"
},
Expand All @@ -333,7 +347,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() {
"ips": [],
"locale": "en",
"invite_request": "",
"role": "user",
"role": {
"name": "user"
},
"confirmed": true,
"approved": true,
"disabled": false,
Expand All @@ -360,7 +376,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() {
"last_status_at": "2021-10-20T10:40:37.000Z",
"emojis": [],
"fields": [],
"role": "user"
"role": {
"name": "user"
}
},
"created_by_application_id": "01F8MGY43H3N2C8EWPR2FPYEXG"
},
Expand All @@ -374,7 +392,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet1() {
"ips": [],
"locale": "",
"invite_request": null,
"role": "user",
"role": {
"name": "user"
},
"confirmed": false,
"approved": false,
"disabled": false,
Expand Down Expand Up @@ -528,7 +548,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet2() {
"ips": [],
"locale": "en",
"invite_request": "",
"role": "user",
"role": {
"name": "user"
},
"confirmed": true,
"approved": true,
"disabled": false,
Expand All @@ -555,7 +577,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet2() {
"last_status_at": "2021-10-20T10:40:37.000Z",
"emojis": [],
"fields": [],
"role": "user"
"role": {
"name": "user"
}
},
"created_by_application_id": "01F8MGY43H3N2C8EWPR2FPYEXG"
},
Expand All @@ -569,7 +593,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet2() {
"ips": [],
"locale": "",
"invite_request": null,
"role": "user",
"role": {
"name": "user"
},
"confirmed": false,
"approved": false,
"disabled": false,
Expand Down Expand Up @@ -723,7 +749,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet3() {
"ips": [],
"locale": "en",
"invite_request": "",
"role": "user",
"role": {
"name": "user"
},
"confirmed": true,
"approved": true,
"disabled": false,
Expand All @@ -750,7 +778,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet3() {
"last_status_at": "2021-10-20T10:40:37.000Z",
"emojis": [],
"fields": [],
"role": "user"
"role": {
"name": "user"
}
},
"created_by_application_id": "01F8MGY43H3N2C8EWPR2FPYEXG"
},
Expand All @@ -764,7 +794,9 @@ func (suite *ReportsGetTestSuite) TestReportsGet3() {
"ips": [],
"locale": "",
"invite_request": null,
"role": "user",
"role": {
"name": "user"
},
"confirmed": false,
"approved": false,
"disabled": false,
Expand Down
24 changes: 18 additions & 6 deletions internal/api/client/instance/instancepatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ func (suite *InstancePatchTestSuite) TestInstancePatch1() {
"emojis": [],
"fields": [],
"enable_rss": true,
"role": "admin"
"role": {
"name": "admin"
}
},
"max_toot_chars": 5000
}`, dst.String())
Expand Down Expand Up @@ -247,7 +249,9 @@ func (suite *InstancePatchTestSuite) TestInstancePatch2() {
"emojis": [],
"fields": [],
"enable_rss": true,
"role": "admin"
"role": {
"name": "admin"
}
},
"max_toot_chars": 5000
}`, dst.String())
Expand Down Expand Up @@ -343,7 +347,9 @@ func (suite *InstancePatchTestSuite) TestInstancePatch3() {
"emojis": [],
"fields": [],
"enable_rss": true,
"role": "admin"
"role": {
"name": "admin"
}
},
"max_toot_chars": 5000
}`, dst.String())
Expand Down Expand Up @@ -490,7 +496,9 @@ func (suite *InstancePatchTestSuite) TestInstancePatch6() {
"emojis": [],
"fields": [],
"enable_rss": true,
"role": "admin"
"role": {
"name": "admin"
}
},
"max_toot_chars": 5000
}`, dst.String())
Expand Down Expand Up @@ -609,7 +617,9 @@ func (suite *InstancePatchTestSuite) TestInstancePatch8() {
"emojis": [],
"fields": [],
"enable_rss": true,
"role": "admin"
"role": {
"name": "admin"
}
},
"max_toot_chars": 5000
}`, dst.String())
Expand Down Expand Up @@ -740,7 +750,9 @@ func (suite *InstancePatchTestSuite) TestInstancePatch9() {
"emojis": [],
"fields": [],
"enable_rss": true,
"role": "admin"
"role": {
"name": "admin"
}
},
"max_toot_chars": 5000
}`, dst.String())
Expand Down
21 changes: 13 additions & 8 deletions internal/api/model/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ type Account struct {
EnableRSS bool `json:"enable_rss,omitempty"`
// Role of the account on this instance.
// Omitted for remote accounts.
// example: user
Role AccountRole `json:"role,omitempty"`
Role *AccountRole `json:"role,omitempty"`
}

// AccountCreateRequest models account creation parameters.
Expand Down Expand Up @@ -215,13 +214,19 @@ type AccountDeleteRequest struct {

// AccountRole models the role of an account.
//
// swagger:enum accountRole
// swagger:model accountRole
type AccountRole struct {
Name AccountRoleName `json:"name"`
}

// AccountRoleName represent the name of the role of an account.
//
// swagger:type string
type AccountRole string
type AccountRoleName string

const (
AccountRoleUser AccountRole = "user" // Standard user
AccountRoleModerator AccountRole = "moderator" // Moderator privileges
AccountRoleAdmin AccountRole = "admin" // Instance admin
AccountRoleUnknown AccountRole = "" // We don't know / remote account
AccountRoleUser AccountRoleName = "user" // Standard user
AccountRoleModerator AccountRoleName = "moderator" // Moderator privileges
AccountRoleAdmin AccountRoleName = "admin" // Instance admin
AccountRoleUnknown AccountRoleName = "" // We don't know / remote account
)
2 changes: 1 addition & 1 deletion internal/api/model/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type AdminAccountInfo struct {
// example: Pleaaaaaaaaaaaaaaase!!
InviteRequest *string `json:"invite_request"`
// The current role of the account.
Role string `json:"role"`
Role AccountRole `json:"role"`
// Whether the account has confirmed their email address.
Confirmed bool `json:"confirmed"`
// Whether the account is currently approved.
Expand Down
20 changes: 9 additions & 11 deletions internal/typeutils/internaltofrontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,8 @@ func (c *converter) AccountToAPIAccountPublic(ctx context.Context, a *gtsmodel.A
log.Errorf(ctx, "error converting account emojis: %v", err)
}

var (
acct string
role = apimodel.AccountRoleUnknown
)
var acct string
var role *apimodel.AccountRole

if a.Domain != "" {
// this is a remote user
Expand All @@ -185,11 +183,11 @@ func (c *converter) AccountToAPIAccountPublic(ctx context.Context, a *gtsmodel.A

switch {
case *user.Admin:
role = apimodel.AccountRoleAdmin
role = &apimodel.AccountRole{Name: apimodel.AccountRoleAdmin}
case *user.Moderator:
role = apimodel.AccountRoleModerator
role = &apimodel.AccountRole{Name: apimodel.AccountRoleModerator}
default:
role = apimodel.AccountRoleUser
role = &apimodel.AccountRole{Name: apimodel.AccountRoleUser}
}
}

Expand Down Expand Up @@ -270,7 +268,7 @@ func (c *converter) AccountToAdminAPIAccount(ctx context.Context, a *gtsmodel.Ac
disabled bool
silenced bool
suspended bool
role apimodel.AccountRole = apimodel.AccountRoleUser // assume user by default
role = apimodel.AccountRole{Name: apimodel.AccountRoleUser} // assume user by default
createdByApplicationID string
)

Expand All @@ -296,9 +294,9 @@ func (c *converter) AccountToAdminAPIAccount(ctx context.Context, a *gtsmodel.Ac
locale = user.Locale
inviteRequest = &user.Account.Reason
if *user.Admin {
role = apimodel.AccountRoleAdmin
role.Name = apimodel.AccountRoleAdmin
} else if *user.Moderator {
role = apimodel.AccountRoleModerator
role.Name = apimodel.AccountRoleModerator
}
confirmed = !user.ConfirmedAt.IsZero()
approved = *user.Approved
Expand All @@ -323,7 +321,7 @@ func (c *converter) AccountToAdminAPIAccount(ctx context.Context, a *gtsmodel.Ac
IPs: []interface{}{}, // not implemented,
Locale: locale,
InviteRequest: inviteRequest,
Role: string(role),
Role: role,
Confirmed: confirmed,
Approved: approved,
Disabled: disabled,
Expand Down

0 comments on commit e8a04b7

Please sign in to comment.