Skip to content

Commit 700cfc6

Browse files
committed
WIP: Refactor
1 parent b8b4cc9 commit 700cfc6

39 files changed

+6919
-6526
lines changed

internal/services/permissions/interfaces.go

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package permissions
33
import (
44
"github.com/bwmarrin/discordgo"
55
"github.com/zekroTJA/shinpuru/pkg/permissions"
6+
"github.com/zekroTJA/shinpuru/pkg/roleutil"
67
)
78

89
type Database interface {
@@ -12,3 +13,10 @@ type Database interface {
1213
type State interface {
1314
Guild(id string, hydrate ...bool) (v *discordgo.Guild, err error)
1415
}
16+
17+
type Session interface {
18+
roleutil.Session
19+
20+
GuildMember(guildID, userID string, options ...discordgo.RequestOption) (st *discordgo.Member, err error)
21+
UserChannelPermissions(userID, channelID string, fetchOptions ...discordgo.RequestOption) (apermissions int64, err error)
22+
}

internal/services/permissions/permissions.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ type Permissions struct {
2727
cfg config.Provider
2828
}
2929

30-
var _ Provider = (*Permissions)(nil)
31-
3230
// NewPermissions returns a new PermissionsMiddleware
3331
// instance with the passed database and config instances.
3432
func NewPermissions(container di.Container) *Permissions {
@@ -73,7 +71,7 @@ func (m *Permissions) Before(ctx *ken.Ctx) (next bool, err error) {
7371
return
7472
}
7573

76-
func (m *Permissions) HandleWs(s discordutil.ISession, required string) fiber.Handler {
74+
func (m *Permissions) HandleWs(s Session, required string) fiber.Handler {
7775
if !stringutil.ContainsAny(required, static.AdditionalPermissions) {
7876
static.AdditionalPermissions = append(static.AdditionalPermissions, required)
7977
}
@@ -109,7 +107,7 @@ func (m *Permissions) HandleWs(s discordutil.ISession, required string) fiber.Ha
109107
// permissions array is returned as well as the override,
110108
// which is true when the specified user is the bot owner,
111109
// guild owner or an admin of the guild.
112-
func (m *Permissions) GetPermissions(s discordutil.ISession, guildID, userID string) (perm permissions.PermissionArray, overrideExplicits bool, err error) {
110+
func (m *Permissions) GetPermissions(s Session, guildID, userID string) (perm permissions.PermissionArray, overrideExplicits bool, err error) {
113111
if guildID != "" {
114112
perm, err = m.GetMemberPermission(s, guildID, userID)
115113
if err != nil && !database.IsErrDatabaseNotFound(err) {
@@ -166,7 +164,7 @@ func (m *Permissions) GetPermissions(s discordutil.ISession, guildID, userID str
166164
//
167165
// If guildID is passed as non-mepty string, all configured guild owner
168166
// permissions will be added to the fetched permissions array as well.
169-
func (m *Permissions) CheckPermissions(s discordutil.ISession, guildID string, userID string, dns ...string) (bool, bool, error) {
167+
func (m *Permissions) CheckPermissions(s Session, guildID string, userID string, dns ...string) (bool, bool, error) {
170168
perms, overrideExplicits, err := m.GetPermissions(s, guildID, userID)
171169
if err != nil {
172170
return false, false, err
@@ -183,7 +181,7 @@ func (m *Permissions) CheckPermissions(s discordutil.ISession, guildID string, u
183181

184182
// GetMemberPermission returns a PermissionsArray based on the passed
185183
// members roles permissions rulesets for the given guild.
186-
func (m *Permissions) GetMemberPermission(s discordutil.ISession, guildID string, memberID string) (permissions.PermissionArray, error) {
184+
func (m *Permissions) GetMemberPermission(s Session, guildID string, memberID string) (permissions.PermissionArray, error) {
187185
guildPerms, err := m.db.GetGuildPermissions(guildID)
188186
if err != nil {
189187
return nil, err
+58-59
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,58 @@
1-
package permissions
2-
3-
import (
4-
"github.com/gofiber/fiber/v2"
5-
"github.com/zekroTJA/shinpuru/pkg/discordutil"
6-
"github.com/zekroTJA/shinpuru/pkg/permissions"
7-
"github.com/zekrotja/ken"
8-
)
9-
10-
type Provider interface {
11-
ken.MiddlewareBefore
12-
13-
HandleWs(s discordutil.ISession, required string) fiber.Handler
14-
15-
// GetPermissions tries to fetch the permissions array of
16-
// the passed user of the specified guild. The merged
17-
// permissions array is returned as well as the override,
18-
// which is true when the specified user is the bot owner,
19-
// guild owner or an admin of the guild.
20-
GetPermissions(
21-
s discordutil.ISession,
22-
guildID string,
23-
userID string,
24-
) (perm permissions.PermissionArray, overrideExplicits bool, err error)
25-
26-
// CheckPermissions tries to fetch the permissions of the specified user
27-
// on the specified guild and returns true, if any of the passed dns match
28-
// the fetched permissions array. Also, the override status is returned as
29-
// well as errors occured during permissions fetching.
30-
//
31-
// If the userID matches the configured bot owner, all bot owner permissions
32-
// will be added to the fetched permissions array.
33-
//
34-
// If guildID is passed as non-mepty string, all configured guild owner
35-
// permissions will be added to the fetched permissions array as well.
36-
CheckPermissions(
37-
s discordutil.ISession,
38-
guildID string,
39-
userID string,
40-
dns ...string,
41-
) (bool, bool, error)
42-
43-
// GetMemberPermissions returns a PermissionsArray based on the passed
44-
// members roles permissions rulesets for the given guild.
45-
GetMemberPermission(
46-
s discordutil.ISession,
47-
guildID string,
48-
memberID string,
49-
) (permissions.PermissionArray, error)
50-
51-
// CheckSubPerm takes a command context and checks is the given
52-
// subDN is permitted.
53-
CheckSubPerm(
54-
ctx ken.Context,
55-
subDN string,
56-
explicit bool,
57-
message ...string,
58-
) (ok bool, err error)
59-
}
1+
package permissions
2+
3+
import (
4+
"github.com/gofiber/fiber/v2"
5+
"github.com/zekroTJA/shinpuru/pkg/permissions"
6+
"github.com/zekrotja/ken"
7+
)
8+
9+
type Provider interface {
10+
ken.MiddlewareBefore
11+
12+
HandleWs(s Session, required string) fiber.Handler
13+
14+
// GetPermissions tries to fetch the permissions array of
15+
// the passed user of the specified guild. The merged
16+
// permissions array is returned as well as the override,
17+
// which is true when the specified user is the bot owner,
18+
// guild owner or an admin of the guild.
19+
GetPermissions(
20+
s Session,
21+
guildID string,
22+
userID string,
23+
) (perm permissions.PermissionArray, overrideExplicits bool, err error)
24+
25+
// CheckPermissions tries to fetch the permissions of the specified user
26+
// on the specified guild and returns true, if any of the passed dns match
27+
// the fetched permissions array. Also, the override status is returned as
28+
// well as errors occured during permissions fetching.
29+
//
30+
// If the userID matches the configured bot owner, all bot owner permissions
31+
// will be added to the fetched permissions array.
32+
//
33+
// If guildID is passed as non-mepty string, all configured guild owner
34+
// permissions will be added to the fetched permissions array as well.
35+
CheckPermissions(
36+
s Session,
37+
guildID string,
38+
userID string,
39+
dns ...string,
40+
) (bool, bool, error)
41+
42+
// GetMemberPermissions returns a PermissionsArray based on the passed
43+
// members roles permissions rulesets for the given guild.
44+
GetMemberPermission(
45+
s Session,
46+
guildID string,
47+
memberID string,
48+
) (permissions.PermissionArray, error)
49+
50+
// CheckSubPerm takes a command context and checks is the given
51+
// subDN is permitted.
52+
CheckSubPerm(
53+
ctx ken.Context,
54+
subDN string,
55+
explicit bool,
56+
message ...string,
57+
) (ok bool, err error)
58+
}

0 commit comments

Comments
 (0)