Skip to content

Commit

Permalink
WIP: Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zekroTJA committed Mar 24, 2024
1 parent b8b4cc9 commit 700cfc6
Show file tree
Hide file tree
Showing 39 changed files with 6,919 additions and 6,526 deletions.
8 changes: 8 additions & 0 deletions internal/services/permissions/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package permissions
import (
"github.com/bwmarrin/discordgo"
"github.com/zekroTJA/shinpuru/pkg/permissions"
"github.com/zekroTJA/shinpuru/pkg/roleutil"
)

type Database interface {
Expand All @@ -12,3 +13,10 @@ type Database interface {
type State interface {
Guild(id string, hydrate ...bool) (v *discordgo.Guild, err error)
}

type Session interface {
roleutil.Session

GuildMember(guildID, userID string, options ...discordgo.RequestOption) (st *discordgo.Member, err error)
UserChannelPermissions(userID, channelID string, fetchOptions ...discordgo.RequestOption) (apermissions int64, err error)
}
10 changes: 4 additions & 6 deletions internal/services/permissions/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ type Permissions struct {
cfg config.Provider
}

var _ Provider = (*Permissions)(nil)

// NewPermissions returns a new PermissionsMiddleware
// instance with the passed database and config instances.
func NewPermissions(container di.Container) *Permissions {
Expand Down Expand Up @@ -73,7 +71,7 @@ func (m *Permissions) Before(ctx *ken.Ctx) (next bool, err error) {
return
}

func (m *Permissions) HandleWs(s discordutil.ISession, required string) fiber.Handler {
func (m *Permissions) HandleWs(s Session, required string) fiber.Handler {
if !stringutil.ContainsAny(required, static.AdditionalPermissions) {
static.AdditionalPermissions = append(static.AdditionalPermissions, required)
}
Expand Down Expand Up @@ -109,7 +107,7 @@ func (m *Permissions) HandleWs(s discordutil.ISession, required string) fiber.Ha
// permissions array is returned as well as the override,
// which is true when the specified user is the bot owner,
// guild owner or an admin of the guild.
func (m *Permissions) GetPermissions(s discordutil.ISession, guildID, userID string) (perm permissions.PermissionArray, overrideExplicits bool, err error) {
func (m *Permissions) GetPermissions(s Session, guildID, userID string) (perm permissions.PermissionArray, overrideExplicits bool, err error) {
if guildID != "" {
perm, err = m.GetMemberPermission(s, guildID, userID)
if err != nil && !database.IsErrDatabaseNotFound(err) {
Expand Down Expand Up @@ -166,7 +164,7 @@ func (m *Permissions) GetPermissions(s discordutil.ISession, guildID, userID str
//
// If guildID is passed as non-mepty string, all configured guild owner
// permissions will be added to the fetched permissions array as well.
func (m *Permissions) CheckPermissions(s discordutil.ISession, guildID string, userID string, dns ...string) (bool, bool, error) {
func (m *Permissions) CheckPermissions(s Session, guildID string, userID string, dns ...string) (bool, bool, error) {
perms, overrideExplicits, err := m.GetPermissions(s, guildID, userID)
if err != nil {
return false, false, err
Expand All @@ -183,7 +181,7 @@ func (m *Permissions) CheckPermissions(s discordutil.ISession, guildID string, u

// GetMemberPermission returns a PermissionsArray based on the passed
// members roles permissions rulesets for the given guild.
func (m *Permissions) GetMemberPermission(s discordutil.ISession, guildID string, memberID string) (permissions.PermissionArray, error) {
func (m *Permissions) GetMemberPermission(s Session, guildID string, memberID string) (permissions.PermissionArray, error) {
guildPerms, err := m.db.GetGuildPermissions(guildID)
if err != nil {
return nil, err
Expand Down
117 changes: 58 additions & 59 deletions internal/services/permissions/provider.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,58 @@
package permissions

import (
"github.com/gofiber/fiber/v2"
"github.com/zekroTJA/shinpuru/pkg/discordutil"
"github.com/zekroTJA/shinpuru/pkg/permissions"
"github.com/zekrotja/ken"
)

type Provider interface {
ken.MiddlewareBefore

HandleWs(s discordutil.ISession, required string) fiber.Handler

// GetPermissions tries to fetch the permissions array of
// the passed user of the specified guild. The merged
// permissions array is returned as well as the override,
// which is true when the specified user is the bot owner,
// guild owner or an admin of the guild.
GetPermissions(
s discordutil.ISession,
guildID string,
userID string,
) (perm permissions.PermissionArray, overrideExplicits bool, err error)

// CheckPermissions tries to fetch the permissions of the specified user
// on the specified guild and returns true, if any of the passed dns match
// the fetched permissions array. Also, the override status is returned as
// well as errors occured during permissions fetching.
//
// If the userID matches the configured bot owner, all bot owner permissions
// will be added to the fetched permissions array.
//
// If guildID is passed as non-mepty string, all configured guild owner
// permissions will be added to the fetched permissions array as well.
CheckPermissions(
s discordutil.ISession,
guildID string,
userID string,
dns ...string,
) (bool, bool, error)

// GetMemberPermissions returns a PermissionsArray based on the passed
// members roles permissions rulesets for the given guild.
GetMemberPermission(
s discordutil.ISession,
guildID string,
memberID string,
) (permissions.PermissionArray, error)

// CheckSubPerm takes a command context and checks is the given
// subDN is permitted.
CheckSubPerm(
ctx ken.Context,
subDN string,
explicit bool,
message ...string,
) (ok bool, err error)
}
package permissions

import (
"github.com/gofiber/fiber/v2"
"github.com/zekroTJA/shinpuru/pkg/permissions"
"github.com/zekrotja/ken"
)

type Provider interface {
ken.MiddlewareBefore

HandleWs(s Session, required string) fiber.Handler

// GetPermissions tries to fetch the permissions array of
// the passed user of the specified guild. The merged
// permissions array is returned as well as the override,
// which is true when the specified user is the bot owner,
// guild owner or an admin of the guild.
GetPermissions(
s Session,
guildID string,
userID string,
) (perm permissions.PermissionArray, overrideExplicits bool, err error)

// CheckPermissions tries to fetch the permissions of the specified user
// on the specified guild and returns true, if any of the passed dns match
// the fetched permissions array. Also, the override status is returned as
// well as errors occured during permissions fetching.
//
// If the userID matches the configured bot owner, all bot owner permissions
// will be added to the fetched permissions array.
//
// If guildID is passed as non-mepty string, all configured guild owner
// permissions will be added to the fetched permissions array as well.
CheckPermissions(
s Session,
guildID string,
userID string,
dns ...string,
) (bool, bool, error)

// GetMemberPermissions returns a PermissionsArray based on the passed
// members roles permissions rulesets for the given guild.
GetMemberPermission(
s Session,
guildID string,
memberID string,
) (permissions.PermissionArray, error)

// CheckSubPerm takes a command context and checks is the given
// subDN is permitted.
CheckSubPerm(
ctx ken.Context,
subDN string,
explicit bool,
message ...string,
) (ok bool, err error)
}
Loading

0 comments on commit 700cfc6

Please sign in to comment.