Skip to content

Commit

Permalink
Simplify naming of members and constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Jun 13, 2024
1 parent 5fdfe5e commit 02c33c6
Show file tree
Hide file tree
Showing 33 changed files with 531 additions and 533 deletions.
50 changes: 25 additions & 25 deletions internal/appeal/appeal_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import (
)

type appeals struct {
appealRepository domain.AppealRepository
banUsecase domain.BanSteamUsecase
personUsecase domain.PersonUsecase
discordUsecase domain.DiscordUsecase
configUsecase domain.ConfigUsecase
repository domain.AppealRepository
bans domain.BanSteamUsecase
persons domain.PersonUsecase
discord domain.DiscordUsecase
config domain.ConfigUsecase
}

func NewAppealUsecase(ar domain.AppealRepository, banUsecase domain.BanSteamUsecase, personUsecase domain.PersonUsecase,
discordUsecase domain.DiscordUsecase, configUsecase domain.ConfigUsecase,
func NewAppealUsecase(ar domain.AppealRepository, bans domain.BanSteamUsecase, persons domain.PersonUsecase,
discord domain.DiscordUsecase, config domain.ConfigUsecase,
) domain.AppealUsecase {
return &appeals{appealRepository: ar, banUsecase: banUsecase, personUsecase: personUsecase, discordUsecase: discordUsecase, configUsecase: configUsecase}
return &appeals{repository: ar, bans: bans, persons: persons, discord: discord, config: config}
}

func (u *appeals) GetAppealsByActivity(ctx context.Context, opts domain.AppealQueryFilter) ([]domain.AppealOverview, error) {
return u.appealRepository.GetAppealsByActivity(ctx, opts)
return u.repository.GetAppealsByActivity(ctx, opts)
}

func (u *appeals) EditBanMessage(ctx context.Context, curUser domain.UserProfile, reportID int64, newMsg string) (domain.BanAppealMessage, error) {
Expand All @@ -34,7 +34,7 @@ func (u *appeals) EditBanMessage(ctx context.Context, curUser domain.UserProfile
return domain.BanAppealMessage{}, err
}

bannedPerson, errReport := u.banUsecase.GetByBanID(ctx, existing.BanID, true, true)
bannedPerson, errReport := u.bans.GetByBanID(ctx, existing.BanID, true, true)
if errReport != nil {
return existing, errReport
}
Expand All @@ -53,13 +53,13 @@ func (u *appeals) EditBanMessage(ctx context.Context, curUser domain.UserProfile

existing.MessageMD = newMsg

if errSave := u.appealRepository.SaveBanMessage(ctx, &existing); errSave != nil {
if errSave := u.repository.SaveBanMessage(ctx, &existing); errSave != nil {
return existing, errSave
}

conf := u.configUsecase.Config()
conf := u.config.Config()

u.discordUsecase.SendPayload(domain.ChannelModAppealLog, discord.NewAppealMessage(existing.MessageMD,
u.discord.SendPayload(domain.ChannelModAppealLog, discord.NewAppealMessage(existing.MessageMD,
conf.ExtURL(bannedPerson.BanSteam), curUser, conf.ExtURL(curUser)))

return existing, nil
Expand All @@ -74,7 +74,7 @@ func (u *appeals) CreateBanMessage(ctx context.Context, curUser domain.UserProfi
return domain.BanAppealMessage{}, domain.ErrBadRequest
}

bannedPerson, errReport := u.banUsecase.GetByBanID(ctx, banID, true, true)
bannedPerson, errReport := u.bans.GetByBanID(ctx, banID, true, true)
if errReport != nil {
return domain.BanAppealMessage{}, errReport
}
Expand All @@ -83,12 +83,12 @@ func (u *appeals) CreateBanMessage(ctx context.Context, curUser domain.UserProfi
return domain.BanAppealMessage{}, domain.ErrPermissionDenied
}

_, errTarget := u.personUsecase.GetOrCreatePersonBySteamID(ctx, bannedPerson.TargetID)
_, errTarget := u.persons.GetOrCreatePersonBySteamID(ctx, bannedPerson.TargetID)
if errTarget != nil {
return domain.BanAppealMessage{}, errTarget
}

_, errSource := u.personUsecase.GetOrCreatePersonBySteamID(ctx, bannedPerson.SourceID)
_, errSource := u.persons.GetOrCreatePersonBySteamID(ctx, bannedPerson.SourceID)
if errSource != nil {
return domain.BanAppealMessage{}, errSource
}
Expand All @@ -98,26 +98,26 @@ func (u *appeals) CreateBanMessage(ctx context.Context, curUser domain.UserProfi
msg.Personaname = curUser.Name
msg.Avatarhash = curUser.Avatarhash

if errSave := u.appealRepository.SaveBanMessage(ctx, &msg); errSave != nil {
if errSave := u.repository.SaveBanMessage(ctx, &msg); errSave != nil {
return domain.BanAppealMessage{}, errSave
}

conf := u.configUsecase.Config()
conf := u.config.Config()

u.discordUsecase.SendPayload(domain.ChannelModAppealLog, discord.NewAppealMessage(msg.MessageMD,
u.discord.SendPayload(domain.ChannelModAppealLog, discord.NewAppealMessage(msg.MessageMD,
conf.ExtURL(bannedPerson.BanSteam), curUser, conf.ExtURL(curUser)))

bannedPerson.UpdatedOn = time.Now()

if errUpdate := u.banUsecase.Save(ctx, &bannedPerson.BanSteam); errUpdate != nil {
if errUpdate := u.bans.Save(ctx, &bannedPerson.BanSteam); errUpdate != nil {
return domain.BanAppealMessage{}, errUpdate
}

return msg, nil
}

func (u *appeals) GetBanMessages(ctx context.Context, userProfile domain.UserProfile, banID int64) ([]domain.BanAppealMessage, error) {
banPerson, errGetBan := u.banUsecase.GetByBanID(ctx, banID, true, true)
banPerson, errGetBan := u.bans.GetByBanID(ctx, banID, true, true)
if errGetBan != nil {
return nil, errGetBan
}
Expand All @@ -126,11 +126,11 @@ func (u *appeals) GetBanMessages(ctx context.Context, userProfile domain.UserPro
return nil, domain.ErrPermissionDenied
}

return u.appealRepository.GetBanMessages(ctx, banID)
return u.repository.GetBanMessages(ctx, banID)
}

func (u *appeals) GetBanMessageByID(ctx context.Context, banMessageID int64) (domain.BanAppealMessage, error) {
return u.appealRepository.GetBanMessageByID(ctx, banMessageID)
return u.repository.GetBanMessageByID(ctx, banMessageID)
}

func (u *appeals) DropBanMessage(ctx context.Context, curUser domain.UserProfile, banMessageID int64) error {
Expand All @@ -143,11 +143,11 @@ func (u *appeals) DropBanMessage(ctx context.Context, curUser domain.UserProfile
return domain.ErrPermissionDenied
}

if errDrop := u.appealRepository.DropBanMessage(ctx, &existing); errDrop != nil {
if errDrop := u.repository.DropBanMessage(ctx, &existing); errDrop != nil {
return errDrop
}

u.discordUsecase.SendPayload(domain.ChannelModAppealLog, discord.DeleteAppealMessage(&existing, curUser, u.configUsecase.ExtURL(curUser)))
u.discord.SendPayload(domain.ChannelModAppealLog, discord.DeleteAppealMessage(&existing, curUser, u.config.ExtURL(curUser)))

return nil
}
10 changes: 5 additions & 5 deletions internal/asset/asset_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
)

type assets struct {
assetRepository domain.AssetRepository
repository domain.AssetRepository
}

func NewAssetUsecase(assetRepository domain.AssetRepository) domain.AssetUsecase {
return &assets{assetRepository: assetRepository}
return &assets{repository: assetRepository}
}

func (s assets) Create(ctx context.Context, author steamid.SteamID, bucket domain.Bucket, fileName string, content io.ReadSeeker) (domain.Asset, error) {
Expand All @@ -44,7 +44,7 @@ func (s assets) Create(ctx context.Context, author steamid.SteamID, bucket domai
return domain.Asset{}, errAsset
}

newAsset, errPut := s.assetRepository.Put(ctx, asset, content)
newAsset, errPut := s.repository.Put(ctx, asset, content)
if errPut != nil {
return domain.Asset{}, errPut
}
Expand All @@ -60,7 +60,7 @@ func (s assets) Get(ctx context.Context, uuid uuid.UUID) (domain.Asset, io.ReadS
return domain.Asset{}, nil, domain.ErrUUIDInvalid
}

asset, reader, errAsset := s.assetRepository.Get(ctx, uuid)
asset, reader, errAsset := s.repository.Get(ctx, uuid)
if errAsset != nil {
return asset, nil, errAsset
}
Expand All @@ -73,7 +73,7 @@ func (s assets) Delete(ctx context.Context, assetID uuid.UUID) (int64, error) {
return 0, domain.ErrUUIDInvalid
}

size, err := s.assetRepository.Delete(ctx, assetID)
size, err := s.repository.Delete(ctx, assetID)
if err != nil {
return 0, err
}
Expand Down
46 changes: 23 additions & 23 deletions internal/auth/auth_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ import (
const ctxKeyUserProfile = "user_profile"

type auth struct {
authRepository domain.AuthRepository
configUsecase domain.ConfigUsecase
personUsecase domain.PersonUsecase
banUsecase domain.BanSteamUsecase
serverUsecase domain.ServersUsecase
auth domain.AuthRepository
config domain.ConfigUsecase
persons domain.PersonUsecase
bans domain.BanSteamUsecase
servers domain.ServersUsecase
}

func NewAuthUsecase(authRepository domain.AuthRepository, configUsecase domain.ConfigUsecase, personUsecase domain.PersonUsecase,
banUsecase domain.BanSteamUsecase, serversUsecase domain.ServersUsecase,
func NewAuthUsecase(repository domain.AuthRepository, config domain.ConfigUsecase, persons domain.PersonUsecase,
bans domain.BanSteamUsecase, servers domain.ServersUsecase,
) domain.AuthUsecase {
return &auth{
authRepository: authRepository,
configUsecase: configUsecase,
personUsecase: personUsecase,
banUsecase: banUsecase,
serverUsecase: serversUsecase,
auth: repository,
config: config,
persons: persons,
bans: bans,
servers: servers,
}
}

Expand All @@ -48,7 +48,7 @@ func (u *auth) Start(ctx context.Context) {
for {
select {
case <-ticker.C:
if err := u.authRepository.PrunePersonAuth(ctx); err != nil && !errors.Is(err, domain.ErrNoResult) {
if err := u.auth.PrunePersonAuth(ctx); err != nil && !errors.Is(err, domain.ErrNoResult) {
slog.Error("Error pruning expired refresh tokens", log.ErrAttr(err))
}
case <-ctx.Done():
Expand All @@ -60,11 +60,11 @@ func (u *auth) Start(ctx context.Context) {
}

func (u *auth) DeletePersonAuth(ctx context.Context, authID int64) error {
return u.authRepository.DeletePersonAuth(ctx, authID)
return u.auth.DeletePersonAuth(ctx, authID)
}

func (u *auth) GetPersonAuthByRefreshToken(ctx context.Context, token string, auth *domain.PersonAuth) error {
return u.authRepository.GetPersonAuthByFingerprint(ctx, token, auth)
return u.auth.GetPersonAuthByFingerprint(ctx, token, auth)
}

// MakeToken generates new jwt auth tokens
Expand All @@ -88,15 +88,15 @@ func (u *auth) MakeToken(ctx *gin.Context, cookieKey string, sid steamid.SteamID

personAuth := domain.NewPersonAuth(sid, ipAddr, accessToken)

if saveErr := u.authRepository.SavePersonAuth(ctx, &personAuth); saveErr != nil {
if saveErr := u.auth.SavePersonAuth(ctx, &personAuth); saveErr != nil {
return domain.UserTokens{}, errors.Join(saveErr, domain.ErrSaveToken)
}

return domain.UserTokens{Access: accessToken, Fingerprint: fingerprint}, nil
}

func (u *auth) AuthMiddleware(level domain.Privilege) gin.HandlerFunc {
cookieKey := u.configUsecase.Config().HTTPCookieKey
cookieKey := u.config.Config().HTTPCookieKey

return func(ctx *gin.Context) {
var token string
Expand Down Expand Up @@ -130,7 +130,7 @@ func (u *auth) AuthMiddleware(level domain.Privilege) gin.HandlerFunc {
return
}

loggedInPerson, errGetPerson := u.personUsecase.GetOrCreatePersonBySteamID(ctx, sid)
loggedInPerson, errGetPerson := u.persons.GetOrCreatePersonBySteamID(ctx, sid)
if errGetPerson != nil {
slog.Error("Failed to load person during auth", log.ErrAttr(errGetPerson))
ctx.AbortWithStatus(http.StatusForbidden)
Expand All @@ -144,7 +144,7 @@ func (u *auth) AuthMiddleware(level domain.Privilege) gin.HandlerFunc {
return
}

bannedPerson, errBan := u.banUsecase.GetBySteamID(ctx, sid, false, true)
bannedPerson, errBan := u.bans.GetBySteamID(ctx, sid, false, true)
if errBan != nil {
if !errors.Is(errBan, domain.ErrNoResult) {
slog.Error("Failed to fetch authed user ban", log.ErrAttr(errBan))
Expand All @@ -166,7 +166,7 @@ func (u *auth) AuthMiddleware(level domain.Privilege) gin.HandlerFunc {

ctx.Set(ctxKeyUserProfile, profile)

if u.configUsecase.Config().Sentry.SentryDSN != "" {
if u.config.Config().Sentry.SentryDSN != "" {
if hub := sentrygin.GetHubFromContext(ctx); hub != nil {
hub.WithScope(func(scope *sentry.Scope) {
scope.SetUser(sentry.User{
Expand Down Expand Up @@ -213,7 +213,7 @@ func (u *auth) AuthServerMiddleWare() gin.HandlerFunc {
}

var server domain.Server
if errServer := u.serverUsecase.GetServerByPassword(ctx, reqAuthHeader, &server, false, false); errServer != nil {
if errServer := u.servers.GetServerByPassword(ctx, reqAuthHeader, &server, false, false); errServer != nil {
slog.Error("Failed to load server during auth", log.ErrAttr(errServer), slog.String("token", reqAuthHeader), slog.String("IP", ctx.ClientIP()))
ctx.AbortWithStatus(http.StatusUnauthorized)

Expand All @@ -222,7 +222,7 @@ func (u *auth) AuthServerMiddleWare() gin.HandlerFunc {

ctx.Set("server_id", server.ServerID)

if u.configUsecase.Config().Sentry.SentryDSN != "" {
if u.config.Config().Sentry.SentryDSN != "" {
if hub := sentrygin.GetHubFromContext(ctx); hub != nil {
hub.WithScope(func(scope *sentry.Scope) {
scope.SetUser(sentry.User{
Expand All @@ -240,7 +240,7 @@ func (u *auth) AuthServerMiddleWare() gin.HandlerFunc {

func (u *auth) NewUserToken(steamID steamid.SteamID, cookieKey string, fingerPrint string, validDuration time.Duration) (string, error) {
nowTime := time.Now()
conf := u.configUsecase.Config()
conf := u.config.Config()
claims := domain.UserAuthClaims{
Fingerprint: FingerprintHash(fingerPrint),
RegisteredClaims: jwt.RegisteredClaims{
Expand Down
28 changes: 14 additions & 14 deletions internal/ban/ban_asn_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import (
)

type banASN struct {
repository domain.BanASNRepository
discordUsecase domain.DiscordUsecase
networkUsecase domain.NetworkUsecase
config domain.ConfigUsecase
person domain.PersonUsecase
repository domain.BanASNRepository
discord domain.DiscordUsecase
networks domain.NetworkUsecase
config domain.ConfigUsecase
person domain.PersonUsecase
}

func NewBanASNUsecase(repository domain.BanASNRepository, discordUsecase domain.DiscordUsecase,
networkUsecase domain.NetworkUsecase, config domain.ConfigUsecase, person domain.PersonUsecase,
func NewBanASNUsecase(repository domain.BanASNRepository, discord domain.DiscordUsecase,
network domain.NetworkUsecase, config domain.ConfigUsecase, person domain.PersonUsecase,
) domain.BanASNUsecase {
return banASN{
repository: repository,
discordUsecase: discordUsecase,
networkUsecase: networkUsecase,
config: config,
person: person,
repository: repository,
discord: discord,
networks: network,
config: config,
person: person,
}
}

Expand All @@ -50,7 +50,7 @@ func (s banASN) Ban(ctx context.Context, banASN *domain.BanASN) error {
return errors.Join(errSave, domain.ErrSaveBan)
}

s.discordUsecase.SendPayload(domain.ChannelBanLog, discord.BanASNMessage(*banASN, author, s.config.Config()))
s.discord.SendPayload(domain.ChannelBanLog, discord.BanASNMessage(*banASN, author, s.config.Config()))

return nil
}
Expand All @@ -70,7 +70,7 @@ func (s banASN) Unban(ctx context.Context, asnNum string) (bool, error) {
return false, errors.Join(errDrop, domain.ErrDropASNBan)
}

s.discordUsecase.SendPayload(domain.ChannelModLog, discord.UnbanASNMessage(asNum))
s.discord.SendPayload(domain.ChannelModLog, discord.UnbanASNMessage(asNum))

return true, nil
}
Expand Down
8 changes: 4 additions & 4 deletions internal/ban/ban_net_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ type banNet struct {
state domain.StateUsecase
}

func NewBanNetUsecase(repository domain.BanNetRepository, personUsecase domain.PersonUsecase,
configUsecase domain.ConfigUsecase, discordUsecase domain.DiscordUsecase, stateUsecase domain.StateUsecase,
func NewBanNetUsecase(repository domain.BanNetRepository, persons domain.PersonUsecase,
config domain.ConfigUsecase, discord domain.DiscordUsecase, state domain.StateUsecase,
) domain.BanNetUsecase {
return &banNet{
repository: repository, persons: personUsecase, config: configUsecase,
discord: discordUsecase, state: stateUsecase,
repository: repository, persons: persons, config: config,
discord: discord, state: state,
}
}

Expand Down
Loading

0 comments on commit 02c33c6

Please sign in to comment.