From 169ebf8de4b8068a3090506ab76e8364d28d35f4 Mon Sep 17 00:00:00 2001 From: Leigh MacDonald Date: Thu, 13 Jun 2024 19:37:33 -0600 Subject: [PATCH] Remove the util package --- frontend/dev.go | 4 +- internal/auth/auth_usecase.go | 4 +- internal/ban/ban_asn_service.go | 4 +- internal/ban/ban_net_service.go | 4 +- internal/ban/ban_steam_service.go | 4 +- internal/ban/ban_steam_usecase.go | 4 +- internal/blocklist/blocklist_usecase.go | 4 +- internal/chat/chat_repository.go | 4 +- internal/chat/chat_usecase.go | 4 +- internal/config/config_usecase.go | 4 +- internal/config/helper.go | 7 +-- internal/database/migrate.go | 4 +- internal/discord/discord_repository.go | 3 +- internal/discord/discord_service.go | 16 +++--- internal/discord/discord_usecase_oauth.go | 13 ++--- internal/discord/responses.go | 21 ++++---- internal/domain/forum.go | 4 +- internal/domain/mocks/MockAppealRepository.go | 2 +- internal/domain/mocks/MockAppealUsecase.go | 2 +- internal/domain/mocks/MockAssetRepository.go | 2 +- internal/domain/mocks/MockAssetUsecase.go | 2 +- internal/domain/mocks/MockAuthRepository.go | 2 +- internal/domain/mocks/MockAuthUsecase.go | 2 +- internal/domain/mocks/MockBanASNRepository.go | 2 +- internal/domain/mocks/MockBanASNUsecase.go | 2 +- .../domain/mocks/MockBanGroupRepository.go | 2 +- internal/domain/mocks/MockBanGroupUsecase.go | 2 +- internal/domain/mocks/MockBanNetRepository.go | 2 +- internal/domain/mocks/MockBanNetUsecase.go | 2 +- .../domain/mocks/MockBanSteamRepository.go | 2 +- internal/domain/mocks/MockBanSteamUsecase.go | 2 +- .../domain/mocks/MockBlocklistRepository.go | 2 +- internal/domain/mocks/MockBlocklistUsecase.go | 2 +- internal/domain/mocks/MockChatRepository.go | 2 +- internal/domain/mocks/MockChatUsecase.go | 2 +- internal/domain/mocks/MockConfigRepository.go | 2 +- internal/domain/mocks/MockConfigUsecase.go | 2 +- .../domain/mocks/MockContestRepository.go | 2 +- internal/domain/mocks/MockContestUsecase.go | 2 +- internal/domain/mocks/MockDemoRepository.go | 2 +- internal/domain/mocks/MockDemoUsecase.go | 2 +- .../mocks/MockDiscordOAuthRepository.go | 2 +- .../domain/mocks/MockDiscordOAuthUsecase.go | 2 +- .../domain/mocks/MockDiscordRepository.go | 2 +- internal/domain/mocks/MockDiscordUsecase.go | 2 +- internal/domain/mocks/MockForumRepository.go | 2 +- internal/domain/mocks/MockForumUsecase.go | 24 ++++----- internal/domain/mocks/MockMatchRepository.go | 2 +- internal/domain/mocks/MockMatchUsecase.go | 2 +- internal/domain/mocks/MockMetricsUsecase.go | 2 +- .../domain/mocks/MockNetworkRepository.go | 2 +- internal/domain/mocks/MockNetworkUsecase.go | 2 +- internal/domain/mocks/MockNewsRepository.go | 2 +- internal/domain/mocks/MockNewsUsecase.go | 2 +- .../mocks/MockNotificationRepository.go | 2 +- .../domain/mocks/MockNotificationUsecase.go | 2 +- .../domain/mocks/MockPatreonRepository.go | 2 +- internal/domain/mocks/MockPatreonUsecase.go | 2 +- internal/domain/mocks/MockPersonRepository.go | 2 +- internal/domain/mocks/MockPersonUsecase.go | 2 +- internal/domain/mocks/MockReportRepository.go | 2 +- internal/domain/mocks/MockReportUsecase.go | 2 +- internal/domain/mocks/MockSRCDSRepository.go | 2 +- internal/domain/mocks/MockSRCDSUsecase.go | 2 +- .../domain/mocks/MockServersRepository.go | 49 +------------------ internal/domain/mocks/MockServersUsecase.go | 49 +------------------ internal/domain/mocks/MockStateRepository.go | 2 +- internal/domain/mocks/MockStateUsecase.go | 2 +- internal/domain/mocks/MockVoteRepository.go | 2 +- internal/domain/mocks/MockVoteUsecase.go | 2 +- internal/domain/mocks/MockWikiRepository.go | 2 +- internal/domain/mocks/MockWikiUsecase.go | 2 +- .../domain/mocks/MockWordFilterRepository.go | 2 +- .../domain/mocks/MockWordFilterUsecase.go | 2 +- internal/domain/server.go | 6 +-- internal/forum/forum_service.go | 24 ++++----- internal/httphelper/helper.go | 4 +- internal/httphelper/network.go | 15 ++++++ internal/patreon/patreon_usecase.go | 11 +++-- internal/person/person_usecase.go | 4 +- internal/steamgroup/steamgroup_service.go | 4 +- internal/steamgroup/steamgroup_usecase.go | 4 +- internal/thirdparty/logstf.go | 4 +- internal/thirdparty/steam_api.go | 3 +- internal/wiki/wiki_usecase_test.go | 4 +- internal/wordfilter/wordfilter_usecase.go | 6 +-- pkg/convert/convert.go | 32 ++++++++++++ pkg/{util/network.go => convert/net.go} | 13 +---- pkg/{util => datetime}/conversion.go | 41 +++------------- pkg/{util => datetime}/time_test.go | 6 +-- pkg/{util => fs}/filesystem.go | 2 +- pkg/ip2location/ip2location.go | 8 +-- pkg/log/log.go | 7 +++ pkg/logparse/log_parser_test.go | 4 +- pkg/{util => oauth}/state_tracker.go | 5 +- pkg/{util => stringutil}/text.go | 2 +- pkg/{util => stringutil}/text_test.go | 6 +-- pkg/util/misc.go | 23 --------- 98 files changed, 238 insertions(+), 338 deletions(-) create mode 100644 internal/httphelper/network.go create mode 100644 pkg/convert/convert.go rename pkg/{util/network.go => convert/net.go} (59%) rename pkg/{util => datetime}/conversion.go (81%) rename pkg/{util => datetime}/time_test.go (80%) rename pkg/{util => fs}/filesystem.go (98%) rename pkg/{util => oauth}/state_tracker.go (91%) rename pkg/{util => stringutil}/text.go (99%) rename pkg/{util => stringutil}/text_test.go (59%) delete mode 100644 pkg/util/misc.go diff --git a/frontend/dev.go b/frontend/dev.go index 1d42ffd47..67b89246d 100644 --- a/frontend/dev.go +++ b/frontend/dev.go @@ -11,8 +11,8 @@ import ( "github.com/gin-contrib/static" "github.com/gin-gonic/gin" + "github.com/leighmacdonald/gbans/pkg/fs" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" ) func AddRoutes(engine *gin.Engine, root string) error { @@ -20,7 +20,7 @@ func AddRoutes(engine *gin.Engine, root string) error { root = "frontend/dist" } - if !util.Exists(filepath.Join(root, "index.html")) { + if !fs.Exists(filepath.Join(root, "index.html")) { return ErrContentRoot } diff --git a/internal/auth/auth_usecase.go b/internal/auth/auth_usecase.go index 1314caedc..a9a4b5ef5 100644 --- a/internal/auth/auth_usecase.go +++ b/internal/auth/auth_usecase.go @@ -16,7 +16,7 @@ import ( "github.com/golang-jwt/jwt/v5" "github.com/leighmacdonald/gbans/internal/domain" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" + "github.com/leighmacdonald/gbans/pkg/stringutil" "github.com/leighmacdonald/steamid/v4/steamid" ) @@ -74,7 +74,7 @@ func (u *auth) MakeToken(ctx *gin.Context, cookieKey string, sid steamid.SteamID return domain.UserTokens{}, domain.ErrCookieKeyMissing } - fingerprint := util.SecureRandomString(40) + fingerprint := stringutil.SecureRandomString(40) accessToken, errAccess := u.NewUserToken(sid, cookieKey, fingerprint, domain.AuthTokenDuration) if errAccess != nil { diff --git a/internal/ban/ban_asn_service.go b/internal/ban/ban_asn_service.go index dd8c847f6..833b04537 100644 --- a/internal/ban/ban_asn_service.go +++ b/internal/ban/ban_asn_service.go @@ -9,8 +9,8 @@ import ( "github.com/gin-gonic/gin" "github.com/leighmacdonald/gbans/internal/domain" "github.com/leighmacdonald/gbans/internal/httphelper" + "github.com/leighmacdonald/gbans/pkg/datetime" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" ) type banASNHandler struct { @@ -54,7 +54,7 @@ func (h banASNHandler) onAPIPostBansASNCreate() gin.HandlerFunc { sid = httphelper.CurrentUserProfile(ctx).SteamID ) - duration, errDuration := util.CalcDuration(req.Duration, req.ValidUntil) + duration, errDuration := datetime.CalcDuration(req.Duration, req.ValidUntil) if errDuration != nil { httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) diff --git a/internal/ban/ban_net_service.go b/internal/ban/ban_net_service.go index 16b358079..8cf1f954f 100644 --- a/internal/ban/ban_net_service.go +++ b/internal/ban/ban_net_service.go @@ -11,8 +11,8 @@ import ( "github.com/gin-gonic/gin" "github.com/leighmacdonald/gbans/internal/domain" "github.com/leighmacdonald/gbans/internal/httphelper" + "github.com/leighmacdonald/gbans/pkg/datetime" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" "github.com/leighmacdonald/steamid/v4/steamid" ) @@ -82,7 +82,7 @@ func (h banNetHandler) onAPIPostBansCIDRCreate() gin.HandlerFunc { sid = httphelper.CurrentUserProfile(ctx).SteamID ) - duration, errDuration := util.CalcDuration(req.Duration, req.ValidUntil) + duration, errDuration := datetime.CalcDuration(req.Duration, req.ValidUntil) if errDuration != nil { httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) diff --git a/internal/ban/ban_steam_service.go b/internal/ban/ban_steam_service.go index c28d23989..ce0797624 100644 --- a/internal/ban/ban_steam_service.go +++ b/internal/ban/ban_steam_service.go @@ -13,8 +13,8 @@ import ( "github.com/leighmacdonald/gbans/internal/domain" "github.com/leighmacdonald/gbans/internal/httphelper" "github.com/leighmacdonald/gbans/internal/thirdparty" + "github.com/leighmacdonald/gbans/pkg/datetime" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" "github.com/leighmacdonald/steamid/v4/steamid" "golang.org/x/exp/slices" ) @@ -146,7 +146,7 @@ func (h banHandler) onAPIPostBanSteamCreate() gin.HandlerFunc { sid = sourceID } - duration, errDuration := util.CalcDuration(req.Duration, req.ValidUntil) + duration, errDuration := datetime.CalcDuration(req.Duration, req.ValidUntil) if errDuration != nil { httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) diff --git a/internal/ban/ban_steam_usecase.go b/internal/ban/ban_steam_usecase.go index 4407abb79..595a2b361 100644 --- a/internal/ban/ban_steam_usecase.go +++ b/internal/ban/ban_steam_usecase.go @@ -10,8 +10,8 @@ import ( "github.com/leighmacdonald/gbans/internal/discord" "github.com/leighmacdonald/gbans/internal/domain" + "github.com/leighmacdonald/gbans/pkg/datetime" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" "github.com/leighmacdonald/steamid/v4/steamid" "github.com/leighmacdonald/steamweb/v2" ) @@ -223,7 +223,7 @@ func (s banSteamUsecase) CheckEvadeStatus(ctx context.Context, curUser domain.Pe return false, errMatch } - duration, errDuration := util.ParseUserStringDuration("10y") + duration, errDuration := datetime.ParseUserStringDuration("10y") if errDuration != nil { return false, errDuration } diff --git a/internal/blocklist/blocklist_usecase.go b/internal/blocklist/blocklist_usecase.go index 3c31397d0..6b2520a51 100644 --- a/internal/blocklist/blocklist_usecase.go +++ b/internal/blocklist/blocklist_usecase.go @@ -16,8 +16,8 @@ import ( "time" "github.com/leighmacdonald/gbans/internal/domain" + "github.com/leighmacdonald/gbans/internal/httphelper" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" "github.com/leighmacdonald/steamid/v4/steamid" ) @@ -125,7 +125,7 @@ func (b blocklistUsecase) updateSource(ctx context.Context, list domain.CIDRBloc return errors.Join(errReq, domain.ErrRequestCreate) } - client := util.NewHTTPClient() + client := httphelper.NewHTTPClient() resp, errResp := client.Do(req) if errResp != nil { diff --git a/internal/chat/chat_repository.go b/internal/chat/chat_repository.go index a11153489..f4cb8b41f 100644 --- a/internal/chat/chat_repository.go +++ b/internal/chat/chat_repository.go @@ -12,10 +12,10 @@ import ( "github.com/gofrs/uuid/v5" "github.com/leighmacdonald/gbans/internal/database" "github.com/leighmacdonald/gbans/internal/domain" + "github.com/leighmacdonald/gbans/pkg/datetime" "github.com/leighmacdonald/gbans/pkg/fp" "github.com/leighmacdonald/gbans/pkg/log" "github.com/leighmacdonald/gbans/pkg/logparse" - "github.com/leighmacdonald/gbans/pkg/util" "github.com/leighmacdonald/steamid/v4/steamid" ) @@ -238,7 +238,7 @@ func (r chatRepository) QueryChatHistory(ctx context.Context, filters domain.Cha if !filters.Unrestricted { unrTime := now.AddDate(0, 0, -30) if filters.DateStart != nil && filters.DateStart.Before(unrTime) { - return nil, util.ErrInvalidDuration + return nil, datetime.ErrInvalidDuration } } diff --git a/internal/chat/chat_usecase.go b/internal/chat/chat_usecase.go index f9da6c71e..ada23ff56 100644 --- a/internal/chat/chat_usecase.go +++ b/internal/chat/chat_usecase.go @@ -11,8 +11,8 @@ import ( "github.com/leighmacdonald/gbans/internal/discord" "github.com/leighmacdonald/gbans/internal/domain" "github.com/leighmacdonald/gbans/internal/state" + "github.com/leighmacdonald/gbans/pkg/datetime" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" "github.com/leighmacdonald/steamid/v4/steamid" ) @@ -65,7 +65,7 @@ func (u chatUsecase) onWarningExceeded(ctx context.Context, newWarning domain.Ne ) if newWarning.MatchedFilter.Action == domain.Ban || newWarning.MatchedFilter.Action == domain.Mute { - duration, errDuration := util.ParseDuration(newWarning.MatchedFilter.Duration) + duration, errDuration := datetime.ParseDuration(newWarning.MatchedFilter.Duration) if errDuration != nil { return fmt.Errorf("invalid duration: %w", errDuration) } diff --git a/internal/config/config_usecase.go b/internal/config/config_usecase.go index 0be26d798..7b3c7caed 100644 --- a/internal/config/config_usecase.go +++ b/internal/config/config_usecase.go @@ -12,8 +12,8 @@ import ( "github.com/gin-gonic/gin" "github.com/leighmacdonald/gbans/internal/domain" + "github.com/leighmacdonald/gbans/internal/httphelper" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" "github.com/leighmacdonald/steamid/v4/steamid" "github.com/leighmacdonald/steamweb/v2" "github.com/mitchellh/mapstructure" @@ -207,7 +207,7 @@ func getGithubReleases(ctx context.Context) ([]GithubRelease, error) { req.Header.Add("Accept", "application/vnd.github+json") req.Header.Add("X-GitHub-Api-Version", "2022-11-28") - client := util.NewHTTPClient() + client := httphelper.NewHTTPClient() resp, errResp := client.Do(req) if errResp != nil { diff --git a/internal/config/helper.go b/internal/config/helper.go index 0040b0d20..67fce5499 100644 --- a/internal/config/helper.go +++ b/internal/config/helper.go @@ -7,7 +7,8 @@ import ( "strings" "github.com/leighmacdonald/gbans/internal/domain" - "github.com/leighmacdonald/gbans/pkg/util" + "github.com/leighmacdonald/gbans/pkg/datetime" + "github.com/leighmacdonald/gbans/pkg/stringutil" "github.com/mitchellh/go-homedir" "github.com/mitchellh/mapstructure" "github.com/spf13/viper" @@ -25,7 +26,7 @@ func decodeDuration() mapstructure.DecodeHookFuncType { return data, nil } - duration, errDuration := util.ParseUserStringDuration(data.(string)) + duration, errDuration := datetime.ParseUserStringDuration(data.(string)) if errDuration != nil { return nil, errors.Join(errDuration, fmt.Errorf("%w: %s", domain.ErrDecodeDuration, target.String())) } @@ -52,7 +53,7 @@ func setDefaultConfigValues() { "http_host": "127.0.0.1", "http_port": 6006, "http_static_path": "frontend/dist", - "http_cookie_key": util.SecureRandomString(32), + "http_cookie_key": stringutil.SecureRandomString(32), "http_client_timeout": "10s", "http_cors_origins": []string{"http://gbans.localhost"}, "database_dsn": "postgresql://gbans:gbans@localhost/gbans", diff --git a/internal/database/migrate.go b/internal/database/migrate.go index 7aa24e98d..f6b69b432 100644 --- a/internal/database/migrate.go +++ b/internal/database/migrate.go @@ -8,7 +8,7 @@ import ( "github.com/golang-migrate/migrate/v4" pgxMigrate "github.com/golang-migrate/migrate/v4/database/pgx" "github.com/golang-migrate/migrate/v4/source/httpfs" - "github.com/leighmacdonald/gbans/pkg/util" + "github.com/leighmacdonald/gbans/pkg/log" ) // MigrationAction is the type of migration to perform. @@ -58,7 +58,7 @@ func (db *postgresStore) migrate(action MigrationAction, dsn string) error { return errors.Join(errMigrate, ErrMigrationDriver) } - defer util.LogCloser(driver) + defer log.Closer(driver) source, errHTTPFS := httpfs.New(http.FS(migrations), "migrations") if errHTTPFS != nil { diff --git a/internal/discord/discord_repository.go b/internal/discord/discord_repository.go index 438e1650c..50064ba7e 100644 --- a/internal/discord/discord_repository.go +++ b/internal/discord/discord_repository.go @@ -11,7 +11,6 @@ import ( "github.com/bwmarrin/discordgo" "github.com/leighmacdonald/gbans/internal/domain" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" ) type nullDiscordRepository struct{} @@ -82,7 +81,7 @@ func (bot *discordRepository) RegisterHandler(cmd domain.Cmd, handler domain.Sla func (bot *discordRepository) Shutdown(guildID string) { if bot.session != nil { - defer util.LogCloser(bot.session) + defer log.Closer(bot.session) bot.botUnregisterSlashCommands(guildID) } } diff --git a/internal/discord/discord_service.go b/internal/discord/discord_service.go index ba31500b9..d49d04858 100644 --- a/internal/discord/discord_service.go +++ b/internal/discord/discord_service.go @@ -16,8 +16,8 @@ import ( "github.com/gofrs/uuid/v5" "github.com/leighmacdonald/gbans/internal/domain" "github.com/leighmacdonald/gbans/internal/thirdparty" + "github.com/leighmacdonald/gbans/pkg/datetime" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" "github.com/leighmacdonald/steamid/v4/steamid" ) @@ -764,9 +764,9 @@ func (h discordService) makeOnMute() func(context.Context, *discordgo.Session, * reason = domain.Reason(reasonValueOpt.IntValue()) - duration, errDuration := util.ParseDuration(opts[domain.OptDuration].StringValue()) + duration, errDuration := datetime.ParseDuration(opts[domain.OptDuration].StringValue()) if errDuration != nil { - return nil, util.ErrInvalidDuration + return nil, datetime.ErrInvalidDuration } modNote := opts[domain.OptNote].StringValue() @@ -805,9 +805,9 @@ func (h discordService) onBanASN(ctx context.Context, _ *discordgo.Session, return nil, domain.ErrInvalidSID } - duration, errDuration := util.ParseDuration(opts[domain.OptDuration].StringValue()) + duration, errDuration := datetime.ParseDuration(opts[domain.OptDuration].StringValue()) if errDuration != nil { - return nil, util.ErrInvalidDuration + return nil, datetime.ErrInvalidDuration } author, errGetPersonByDiscordID := h.persons.GetPersonByDiscordID(ctx, interaction.Interaction.Member.User.ID) @@ -858,7 +858,7 @@ func (h discordService) onBanIP(ctx context.Context, _ *discordgo.Session, return nil, errors.Join(errParseCIDR, domain.ErrNetworkInvalidIP) } - duration, errDuration := util.ParseDuration(opts[domain.OptDuration].StringValue()) + duration, errDuration := datetime.ParseDuration(opts[domain.OptDuration].StringValue()) if errDuration != nil { return nil, errDuration } @@ -914,9 +914,9 @@ func (h discordService) onBanSteam(ctx context.Context, _ *discordgo.Session, return nil, domain.ErrInvalidSID } - duration, errDuration := util.ParseDuration(opts[domain.OptDuration].StringValue()) + duration, errDuration := datetime.ParseDuration(opts[domain.OptDuration].StringValue()) if errDuration != nil { - return nil, util.ErrInvalidDuration + return nil, datetime.ErrInvalidDuration } author, errAuthor := h.getDiscordAuthor(ctx, interaction) diff --git a/internal/discord/discord_usecase_oauth.go b/internal/discord/discord_usecase_oauth.go index c51870c85..d47baa790 100644 --- a/internal/discord/discord_usecase_oauth.go +++ b/internal/discord/discord_usecase_oauth.go @@ -11,14 +11,15 @@ import ( "time" "github.com/leighmacdonald/gbans/internal/domain" + "github.com/leighmacdonald/gbans/internal/httphelper" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" + "github.com/leighmacdonald/gbans/pkg/oauth" "github.com/leighmacdonald/steamid/v4/steamid" ) type discordOAuthUsecase struct { config domain.ConfigUsecase - state *util.LoginStateTracker + state *oauth.LoginStateTracker repository domain.DiscordOAuthRepository } @@ -26,7 +27,7 @@ func NewDiscordOAuthUsecase(repository domain.DiscordOAuthRepository, config dom return &discordOAuthUsecase{ repository: repository, config: config, - state: util.NewLoginStateTracker(), + state: oauth.NewLoginStateTracker(), } } @@ -95,7 +96,7 @@ func (d discordOAuthUsecase) fetchRefresh(ctx context.Context, credentials domai req.Header.Add("Content-Type", "application/x-www-form-urlencoded") - resp, errResp := util.NewHTTPClient().Do(req) + resp, errResp := httphelper.NewHTTPClient().Do(req) if errResp != nil { return domain.DiscordCredential{}, errors.Join(errResp, domain.ErrRequestPerform) } @@ -160,7 +161,7 @@ func (d discordOAuthUsecase) Logout(ctx context.Context, steamID steamid.SteamID req.Header.Add("Content-Type", "application/x-www-form-urlencoded") - resp, errResp := util.NewHTTPClient().Do(req) + resp, errResp := httphelper.NewHTTPClient().Do(req) if errResp != nil { return errors.Join(errResp, domain.ErrRequestPerform) } @@ -193,7 +194,7 @@ func (d discordOAuthUsecase) CreateStatefulLoginURL(steamID steamid.SteamID) (st } func (d discordOAuthUsecase) HandleOAuthCode(ctx context.Context, code string, state string) error { - client := util.NewHTTPClient() + client := httphelper.NewHTTPClient() steamID, found := d.state.Get(state) if !found { diff --git a/internal/discord/responses.go b/internal/discord/responses.go index 16f5942b0..dfa56f88f 100644 --- a/internal/discord/responses.go +++ b/internal/discord/responses.go @@ -12,7 +12,8 @@ import ( "github.com/bwmarrin/discordgo" "github.com/leighmacdonald/gbans/internal/domain" "github.com/leighmacdonald/gbans/internal/thirdparty" - "github.com/leighmacdonald/gbans/pkg/util" + "github.com/leighmacdonald/gbans/pkg/datetime" + "github.com/leighmacdonald/gbans/pkg/stringutil" "github.com/leighmacdonald/steamid/v4/steamid" "github.com/olekukonko/tablewriter" ) @@ -248,8 +249,8 @@ func BanSteamResponse(banSteam domain.BanSteam, author domain.PersonInfo) *disco expAt := "Permanent" if banSteam.ValidUntil.Year()-time.Now().Year() < 5 { - expIn = util.FmtDuration(banSteam.ValidUntil) - expAt = util.FmtTimeShort(banSteam.ValidUntil) + expIn = datetime.FmtDuration(banSteam.ValidUntil) + expAt = datetime.FmtTimeShort(banSteam.ValidUntil) } msgEmbed. @@ -361,7 +362,7 @@ func EditAppealMessage(existing domain.BanAppealMessage, body string, author dom msgEmbed := NewEmbed("Ban appeal message edited") msgEmbed. Embed(). - SetDescription(util.DiffString(existing.MessageMD, body)). + SetDescription(stringutil.DiffString(existing.MessageMD, body)). SetColor(ColourWarn) return msgEmbed. @@ -466,8 +467,8 @@ func WarningMessage(newWarning domain.NewUserWarning, banSteam domain.BanSteam, ) if banSteam.ValidUntil.Year()-time.Now().Year() < 5 { - expIn = util.FmtDuration(banSteam.ValidUntil) - expAt = util.FmtTimeShort(banSteam.ValidUntil) + expIn = datetime.FmtDuration(banSteam.ValidUntil) + expAt = datetime.FmtTimeShort(banSteam.ValidUntil) } return msgEmbed. @@ -501,7 +502,7 @@ func CheckMessage(player domain.Person, ban domain.BannedSteamPerson, banURL str } cd := time.Unix(int64(player.TimeCreated), 0) - msgEmbed.Embed().AddField("Age", util.FmtDuration(cd)) + msgEmbed.Embed().AddField("Age", datetime.FmtDuration(cd)) msgEmbed.Embed().AddField("Private", strconv.FormatBool(player.CommunityVisibilityState == 1)) msgEmbed.AddFieldsSteamID(player.SteamID) @@ -562,12 +563,12 @@ func CheckMessage(player domain.Person, ban domain.BannedSteamPerson, banURL str msgEmbed.Embed().SetURL(banURL) msgEmbed.Embed().AddField("Reason", reason) - msgEmbed.Embed().AddField("Created", util.FmtTimeShort(ban.CreatedOn)).MakeFieldInline() + msgEmbed.Embed().AddField("Created", datetime.FmtTimeShort(ban.CreatedOn)).MakeFieldInline() if time.Until(expiry) > time.Hour*24*365*5 { msgEmbed.Embed().AddField("Expires", "Permanent").MakeFieldInline() } else { - msgEmbed.Embed().AddField("Expires", util.FmtDuration(expiry)).MakeFieldInline() + msgEmbed.Embed().AddField("Expires", datetime.FmtDuration(expiry)).MakeFieldInline() } msgEmbed.Embed().AddField("Author", fmt.Sprintf("<@%s>", author.DiscordID)).MakeFieldInline() @@ -585,7 +586,7 @@ func CheckMessage(player domain.Person, ban domain.BannedSteamPerson, banURL str netExpiry := bannedNets[0].ValidUntil msgEmbed.Embed().AddField("Network Bans", strconv.Itoa(len(bannedNets))) msgEmbed.Embed().AddField("Network Reason", netReason) - msgEmbed.Embed().AddField("Network Expires", util.FmtDuration(netExpiry)).MakeFieldInline() + msgEmbed.Embed().AddField("Network Expires", datetime.FmtDuration(netExpiry)).MakeFieldInline() } banStateStr := "no" diff --git a/internal/domain/forum.go b/internal/domain/forum.go index 752af5e04..c61b17bdc 100644 --- a/internal/domain/forum.go +++ b/internal/domain/forum.go @@ -4,7 +4,7 @@ import ( "context" "time" - "github.com/leighmacdonald/gbans/pkg/util" + "github.com/leighmacdonald/gbans/pkg/stringutil" "github.com/leighmacdonald/steamid/v4/steamid" ) @@ -135,7 +135,7 @@ func (thread ForumThread) NewMessage(sourceID steamid.SteamID, body string) Foru ForumMessageID: 0, ForumThreadID: thread.ForumThreadID, SourceID: sourceID, - BodyMD: util.SanitizeUGC(body), + BodyMD: stringutil.SanitizeUGC(body), TimeStamped: NewTimeStamped(), } } diff --git a/internal/domain/mocks/MockAppealRepository.go b/internal/domain/mocks/MockAppealRepository.go index 19dfc50a6..b91393ac9 100644 --- a/internal/domain/mocks/MockAppealRepository.go +++ b/internal/domain/mocks/MockAppealRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockAppealUsecase.go b/internal/domain/mocks/MockAppealUsecase.go index e0dfd3bf7..61012c662 100644 --- a/internal/domain/mocks/MockAppealUsecase.go +++ b/internal/domain/mocks/MockAppealUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockAssetRepository.go b/internal/domain/mocks/MockAssetRepository.go index 4c8b171b1..03ee7e746 100644 --- a/internal/domain/mocks/MockAssetRepository.go +++ b/internal/domain/mocks/MockAssetRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockAssetUsecase.go b/internal/domain/mocks/MockAssetUsecase.go index 5e17da80c..72ded3114 100644 --- a/internal/domain/mocks/MockAssetUsecase.go +++ b/internal/domain/mocks/MockAssetUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockAuthRepository.go b/internal/domain/mocks/MockAuthRepository.go index e38ba4d29..993cc8f54 100644 --- a/internal/domain/mocks/MockAuthRepository.go +++ b/internal/domain/mocks/MockAuthRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockAuthUsecase.go b/internal/domain/mocks/MockAuthUsecase.go index 47a37b87f..4ae006021 100644 --- a/internal/domain/mocks/MockAuthUsecase.go +++ b/internal/domain/mocks/MockAuthUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockBanASNRepository.go b/internal/domain/mocks/MockBanASNRepository.go index 1ec5de703..be9019402 100644 --- a/internal/domain/mocks/MockBanASNRepository.go +++ b/internal/domain/mocks/MockBanASNRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockBanASNUsecase.go b/internal/domain/mocks/MockBanASNUsecase.go index e74d13217..e4487b95b 100644 --- a/internal/domain/mocks/MockBanASNUsecase.go +++ b/internal/domain/mocks/MockBanASNUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockBanGroupRepository.go b/internal/domain/mocks/MockBanGroupRepository.go index a262634c9..fd94fdcee 100644 --- a/internal/domain/mocks/MockBanGroupRepository.go +++ b/internal/domain/mocks/MockBanGroupRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockBanGroupUsecase.go b/internal/domain/mocks/MockBanGroupUsecase.go index 17acca545..d10766646 100644 --- a/internal/domain/mocks/MockBanGroupUsecase.go +++ b/internal/domain/mocks/MockBanGroupUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockBanNetRepository.go b/internal/domain/mocks/MockBanNetRepository.go index 56b9edfdc..7b4260c7e 100644 --- a/internal/domain/mocks/MockBanNetRepository.go +++ b/internal/domain/mocks/MockBanNetRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockBanNetUsecase.go b/internal/domain/mocks/MockBanNetUsecase.go index 7b353b50a..32177dcba 100644 --- a/internal/domain/mocks/MockBanNetUsecase.go +++ b/internal/domain/mocks/MockBanNetUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockBanSteamRepository.go b/internal/domain/mocks/MockBanSteamRepository.go index e5fcdeb45..b76dbc2fa 100644 --- a/internal/domain/mocks/MockBanSteamRepository.go +++ b/internal/domain/mocks/MockBanSteamRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockBanSteamUsecase.go b/internal/domain/mocks/MockBanSteamUsecase.go index 9417cf9d8..b014d2536 100644 --- a/internal/domain/mocks/MockBanSteamUsecase.go +++ b/internal/domain/mocks/MockBanSteamUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockBlocklistRepository.go b/internal/domain/mocks/MockBlocklistRepository.go index 68a70e727..b10483df1 100644 --- a/internal/domain/mocks/MockBlocklistRepository.go +++ b/internal/domain/mocks/MockBlocklistRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockBlocklistUsecase.go b/internal/domain/mocks/MockBlocklistUsecase.go index a0b12f8c2..3d18f94ae 100644 --- a/internal/domain/mocks/MockBlocklistUsecase.go +++ b/internal/domain/mocks/MockBlocklistUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockChatRepository.go b/internal/domain/mocks/MockChatRepository.go index aff28ff7c..afd0bfefe 100644 --- a/internal/domain/mocks/MockChatRepository.go +++ b/internal/domain/mocks/MockChatRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockChatUsecase.go b/internal/domain/mocks/MockChatUsecase.go index 0e7aeb8da..fb8df31a6 100644 --- a/internal/domain/mocks/MockChatUsecase.go +++ b/internal/domain/mocks/MockChatUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockConfigRepository.go b/internal/domain/mocks/MockConfigRepository.go index 5eac11b58..094ed7102 100644 --- a/internal/domain/mocks/MockConfigRepository.go +++ b/internal/domain/mocks/MockConfigRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockConfigUsecase.go b/internal/domain/mocks/MockConfigUsecase.go index 2c6bbf7b4..e623121d2 100644 --- a/internal/domain/mocks/MockConfigUsecase.go +++ b/internal/domain/mocks/MockConfigUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockContestRepository.go b/internal/domain/mocks/MockContestRepository.go index e065afe4f..16a11924a 100644 --- a/internal/domain/mocks/MockContestRepository.go +++ b/internal/domain/mocks/MockContestRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockContestUsecase.go b/internal/domain/mocks/MockContestUsecase.go index 8796bbab0..1440e25c6 100644 --- a/internal/domain/mocks/MockContestUsecase.go +++ b/internal/domain/mocks/MockContestUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockDemoRepository.go b/internal/domain/mocks/MockDemoRepository.go index a21987f6f..cf3d3e0b4 100644 --- a/internal/domain/mocks/MockDemoRepository.go +++ b/internal/domain/mocks/MockDemoRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockDemoUsecase.go b/internal/domain/mocks/MockDemoUsecase.go index feea511e4..9fdabfa49 100644 --- a/internal/domain/mocks/MockDemoUsecase.go +++ b/internal/domain/mocks/MockDemoUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockDiscordOAuthRepository.go b/internal/domain/mocks/MockDiscordOAuthRepository.go index 649629d46..497f036a6 100644 --- a/internal/domain/mocks/MockDiscordOAuthRepository.go +++ b/internal/domain/mocks/MockDiscordOAuthRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockDiscordOAuthUsecase.go b/internal/domain/mocks/MockDiscordOAuthUsecase.go index 4846cfb1e..6c9a9a3e8 100644 --- a/internal/domain/mocks/MockDiscordOAuthUsecase.go +++ b/internal/domain/mocks/MockDiscordOAuthUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockDiscordRepository.go b/internal/domain/mocks/MockDiscordRepository.go index 3c6641f6e..509616c15 100644 --- a/internal/domain/mocks/MockDiscordRepository.go +++ b/internal/domain/mocks/MockDiscordRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockDiscordUsecase.go b/internal/domain/mocks/MockDiscordUsecase.go index bd10c29f0..bf20cc967 100644 --- a/internal/domain/mocks/MockDiscordUsecase.go +++ b/internal/domain/mocks/MockDiscordUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockForumRepository.go b/internal/domain/mocks/MockForumRepository.go index 3ade95f3a..d90ffa3b5 100644 --- a/internal/domain/mocks/MockForumRepository.go +++ b/internal/domain/mocks/MockForumRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockForumUsecase.go b/internal/domain/mocks/MockForumUsecase.go index 89e789436..92b7684c7 100644 --- a/internal/domain/mocks/MockForumUsecase.go +++ b/internal/domain/mocks/MockForumUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks @@ -223,17 +223,17 @@ func (_c *MockForumUsecase_ForumCategory_Call) RunAndReturn(run func(context.Con return _c } -// ForumCategoryDelete provides a mock function with given fields: ctx, categoryID -func (_m *MockForumUsecase) ForumCategoryDelete(ctx context.Context, categoryID int) error { - ret := _m.Called(ctx, categoryID) +// ForumCategoryDelete provides a mock function with given fields: ctx, categoryI +func (_m *MockForumUsecase) ForumCategoryDelete(ctx context.Context, categoryI domain.ForumCategory) error { + ret := _m.Called(ctx, categoryI) if len(ret) == 0 { panic("no return value specified for ForumCategoryDelete") } var r0 error - if rf, ok := ret.Get(0).(func(context.Context, int) error); ok { - r0 = rf(ctx, categoryID) + if rf, ok := ret.Get(0).(func(context.Context, domain.ForumCategory) error); ok { + r0 = rf(ctx, categoryI) } else { r0 = ret.Error(0) } @@ -248,14 +248,14 @@ type MockForumUsecase_ForumCategoryDelete_Call struct { // ForumCategoryDelete is a helper method to define mock.On call // - ctx context.Context -// - categoryID int -func (_e *MockForumUsecase_Expecter) ForumCategoryDelete(ctx interface{}, categoryID interface{}) *MockForumUsecase_ForumCategoryDelete_Call { - return &MockForumUsecase_ForumCategoryDelete_Call{Call: _e.mock.On("ForumCategoryDelete", ctx, categoryID)} +// - categoryI domain.ForumCategory +func (_e *MockForumUsecase_Expecter) ForumCategoryDelete(ctx interface{}, categoryI interface{}) *MockForumUsecase_ForumCategoryDelete_Call { + return &MockForumUsecase_ForumCategoryDelete_Call{Call: _e.mock.On("ForumCategoryDelete", ctx, categoryI)} } -func (_c *MockForumUsecase_ForumCategoryDelete_Call) Run(run func(ctx context.Context, categoryID int)) *MockForumUsecase_ForumCategoryDelete_Call { +func (_c *MockForumUsecase_ForumCategoryDelete_Call) Run(run func(ctx context.Context, categoryI domain.ForumCategory)) *MockForumUsecase_ForumCategoryDelete_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(context.Context), args[1].(int)) + run(args[0].(context.Context), args[1].(domain.ForumCategory)) }) return _c } @@ -265,7 +265,7 @@ func (_c *MockForumUsecase_ForumCategoryDelete_Call) Return(_a0 error) *MockForu return _c } -func (_c *MockForumUsecase_ForumCategoryDelete_Call) RunAndReturn(run func(context.Context, int) error) *MockForumUsecase_ForumCategoryDelete_Call { +func (_c *MockForumUsecase_ForumCategoryDelete_Call) RunAndReturn(run func(context.Context, domain.ForumCategory) error) *MockForumUsecase_ForumCategoryDelete_Call { _c.Call.Return(run) return _c } diff --git a/internal/domain/mocks/MockMatchRepository.go b/internal/domain/mocks/MockMatchRepository.go index 7ba6f3357..43a336d47 100644 --- a/internal/domain/mocks/MockMatchRepository.go +++ b/internal/domain/mocks/MockMatchRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockMatchUsecase.go b/internal/domain/mocks/MockMatchUsecase.go index 506e2f438..384af172a 100644 --- a/internal/domain/mocks/MockMatchUsecase.go +++ b/internal/domain/mocks/MockMatchUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockMetricsUsecase.go b/internal/domain/mocks/MockMetricsUsecase.go index 58c52b033..f794b401c 100644 --- a/internal/domain/mocks/MockMetricsUsecase.go +++ b/internal/domain/mocks/MockMetricsUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockNetworkRepository.go b/internal/domain/mocks/MockNetworkRepository.go index 4f7829a93..ba7a1de72 100644 --- a/internal/domain/mocks/MockNetworkRepository.go +++ b/internal/domain/mocks/MockNetworkRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockNetworkUsecase.go b/internal/domain/mocks/MockNetworkUsecase.go index 7517533d4..f8d77a9a3 100644 --- a/internal/domain/mocks/MockNetworkUsecase.go +++ b/internal/domain/mocks/MockNetworkUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockNewsRepository.go b/internal/domain/mocks/MockNewsRepository.go index 699a41e92..d81287578 100644 --- a/internal/domain/mocks/MockNewsRepository.go +++ b/internal/domain/mocks/MockNewsRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockNewsUsecase.go b/internal/domain/mocks/MockNewsUsecase.go index 10df46749..11e81b0b6 100644 --- a/internal/domain/mocks/MockNewsUsecase.go +++ b/internal/domain/mocks/MockNewsUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockNotificationRepository.go b/internal/domain/mocks/MockNotificationRepository.go index e9cf1681e..1a4f5aae6 100644 --- a/internal/domain/mocks/MockNotificationRepository.go +++ b/internal/domain/mocks/MockNotificationRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockNotificationUsecase.go b/internal/domain/mocks/MockNotificationUsecase.go index 9119bc52f..80dfcec96 100644 --- a/internal/domain/mocks/MockNotificationUsecase.go +++ b/internal/domain/mocks/MockNotificationUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockPatreonRepository.go b/internal/domain/mocks/MockPatreonRepository.go index e4d983681..c0ca14b62 100644 --- a/internal/domain/mocks/MockPatreonRepository.go +++ b/internal/domain/mocks/MockPatreonRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockPatreonUsecase.go b/internal/domain/mocks/MockPatreonUsecase.go index 93208084d..73c793d85 100644 --- a/internal/domain/mocks/MockPatreonUsecase.go +++ b/internal/domain/mocks/MockPatreonUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockPersonRepository.go b/internal/domain/mocks/MockPersonRepository.go index dbac6ae33..15aab0b91 100644 --- a/internal/domain/mocks/MockPersonRepository.go +++ b/internal/domain/mocks/MockPersonRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockPersonUsecase.go b/internal/domain/mocks/MockPersonUsecase.go index cdb1c4caf..212cb03cd 100644 --- a/internal/domain/mocks/MockPersonUsecase.go +++ b/internal/domain/mocks/MockPersonUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockReportRepository.go b/internal/domain/mocks/MockReportRepository.go index 7511a9f7c..5af564969 100644 --- a/internal/domain/mocks/MockReportRepository.go +++ b/internal/domain/mocks/MockReportRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockReportUsecase.go b/internal/domain/mocks/MockReportUsecase.go index 702e84203..26d3b8101 100644 --- a/internal/domain/mocks/MockReportUsecase.go +++ b/internal/domain/mocks/MockReportUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockSRCDSRepository.go b/internal/domain/mocks/MockSRCDSRepository.go index cc13d308c..a37e0bc70 100644 --- a/internal/domain/mocks/MockSRCDSRepository.go +++ b/internal/domain/mocks/MockSRCDSRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockSRCDSUsecase.go b/internal/domain/mocks/MockSRCDSUsecase.go index f1c06ac5b..072141eb7 100644 --- a/internal/domain/mocks/MockSRCDSUsecase.go +++ b/internal/domain/mocks/MockSRCDSUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockServersRepository.go b/internal/domain/mocks/MockServersRepository.go index 86b305929..ebeac6378 100644 --- a/internal/domain/mocks/MockServersRepository.go +++ b/internal/domain/mocks/MockServersRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks @@ -22,53 +22,6 @@ func (_m *MockServersRepository) EXPECT() *MockServersRepository_Expecter { return &MockServersRepository_Expecter{mock: &_m.Mock} } -// DropServer provides a mock function with given fields: ctx, serverID -func (_m *MockServersRepository) DropServer(ctx context.Context, serverID int) error { - ret := _m.Called(ctx, serverID) - - if len(ret) == 0 { - panic("no return value specified for DropServer") - } - - var r0 error - if rf, ok := ret.Get(0).(func(context.Context, int) error); ok { - r0 = rf(ctx, serverID) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MockServersRepository_DropServer_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'DropServer' -type MockServersRepository_DropServer_Call struct { - *mock.Call -} - -// DropServer is a helper method to define mock.On call -// - ctx context.Context -// - serverID int -func (_e *MockServersRepository_Expecter) DropServer(ctx interface{}, serverID interface{}) *MockServersRepository_DropServer_Call { - return &MockServersRepository_DropServer_Call{Call: _e.mock.On("DropServer", ctx, serverID)} -} - -func (_c *MockServersRepository_DropServer_Call) Run(run func(ctx context.Context, serverID int)) *MockServersRepository_DropServer_Call { - _c.Call.Run(func(args mock.Arguments) { - run(args[0].(context.Context), args[1].(int)) - }) - return _c -} - -func (_c *MockServersRepository_DropServer_Call) Return(_a0 error) *MockServersRepository_DropServer_Call { - _c.Call.Return(_a0) - return _c -} - -func (_c *MockServersRepository_DropServer_Call) RunAndReturn(run func(context.Context, int) error) *MockServersRepository_DropServer_Call { - _c.Call.Return(run) - return _c -} - // GetServer provides a mock function with given fields: ctx, serverID func (_m *MockServersRepository) GetServer(ctx context.Context, serverID int) (domain.Server, error) { ret := _m.Called(ctx, serverID) diff --git a/internal/domain/mocks/MockServersUsecase.go b/internal/domain/mocks/MockServersUsecase.go index 64df79fe9..e4c874877 100644 --- a/internal/domain/mocks/MockServersUsecase.go +++ b/internal/domain/mocks/MockServersUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks @@ -22,53 +22,6 @@ func (_m *MockServersUsecase) EXPECT() *MockServersUsecase_Expecter { return &MockServersUsecase_Expecter{mock: &_m.Mock} } -// DropServer provides a mock function with given fields: ctx, serverID -func (_m *MockServersUsecase) DropServer(ctx context.Context, serverID int) error { - ret := _m.Called(ctx, serverID) - - if len(ret) == 0 { - panic("no return value specified for DropServer") - } - - var r0 error - if rf, ok := ret.Get(0).(func(context.Context, int) error); ok { - r0 = rf(ctx, serverID) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MockServersUsecase_DropServer_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'DropServer' -type MockServersUsecase_DropServer_Call struct { - *mock.Call -} - -// DropServer is a helper method to define mock.On call -// - ctx context.Context -// - serverID int -func (_e *MockServersUsecase_Expecter) DropServer(ctx interface{}, serverID interface{}) *MockServersUsecase_DropServer_Call { - return &MockServersUsecase_DropServer_Call{Call: _e.mock.On("DropServer", ctx, serverID)} -} - -func (_c *MockServersUsecase_DropServer_Call) Run(run func(ctx context.Context, serverID int)) *MockServersUsecase_DropServer_Call { - _c.Call.Run(func(args mock.Arguments) { - run(args[0].(context.Context), args[1].(int)) - }) - return _c -} - -func (_c *MockServersUsecase_DropServer_Call) Return(_a0 error) *MockServersUsecase_DropServer_Call { - _c.Call.Return(_a0) - return _c -} - -func (_c *MockServersUsecase_DropServer_Call) RunAndReturn(run func(context.Context, int) error) *MockServersUsecase_DropServer_Call { - _c.Call.Return(run) - return _c -} - // GetServer provides a mock function with given fields: ctx, serverID func (_m *MockServersUsecase) GetServer(ctx context.Context, serverID int) (domain.Server, error) { ret := _m.Called(ctx, serverID) diff --git a/internal/domain/mocks/MockStateRepository.go b/internal/domain/mocks/MockStateRepository.go index 8d0276f31..1081f3520 100644 --- a/internal/domain/mocks/MockStateRepository.go +++ b/internal/domain/mocks/MockStateRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockStateUsecase.go b/internal/domain/mocks/MockStateUsecase.go index eb3409791..609a033c4 100644 --- a/internal/domain/mocks/MockStateUsecase.go +++ b/internal/domain/mocks/MockStateUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockVoteRepository.go b/internal/domain/mocks/MockVoteRepository.go index 3329a2708..7dcf25541 100644 --- a/internal/domain/mocks/MockVoteRepository.go +++ b/internal/domain/mocks/MockVoteRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockVoteUsecase.go b/internal/domain/mocks/MockVoteUsecase.go index 3d9e56e99..537f0b575 100644 --- a/internal/domain/mocks/MockVoteUsecase.go +++ b/internal/domain/mocks/MockVoteUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockWikiRepository.go b/internal/domain/mocks/MockWikiRepository.go index f6c7e6e39..e200fb148 100644 --- a/internal/domain/mocks/MockWikiRepository.go +++ b/internal/domain/mocks/MockWikiRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockWikiUsecase.go b/internal/domain/mocks/MockWikiUsecase.go index b3339930b..7eb598add 100644 --- a/internal/domain/mocks/MockWikiUsecase.go +++ b/internal/domain/mocks/MockWikiUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockWordFilterRepository.go b/internal/domain/mocks/MockWordFilterRepository.go index 9eef37b95..7626fa093 100644 --- a/internal/domain/mocks/MockWordFilterRepository.go +++ b/internal/domain/mocks/MockWordFilterRepository.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/mocks/MockWordFilterUsecase.go b/internal/domain/mocks/MockWordFilterUsecase.go index 551746b1b..88df46ddd 100644 --- a/internal/domain/mocks/MockWordFilterUsecase.go +++ b/internal/domain/mocks/MockWordFilterUsecase.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.1. DO NOT EDIT. +// Code generated by mockery v2.43.2. DO NOT EDIT. package mocks diff --git a/internal/domain/server.go b/internal/domain/server.go index 10f5d1487..94b5e5a52 100644 --- a/internal/domain/server.go +++ b/internal/domain/server.go @@ -7,7 +7,7 @@ import ( "net" "time" - "github.com/leighmacdonald/gbans/pkg/util" + "github.com/leighmacdonald/gbans/pkg/stringutil" "github.com/leighmacdonald/steamid/v4/extra" "github.com/leighmacdonald/steamid/v4/steamid" ) @@ -43,9 +43,9 @@ func NewServer(shortName string, address string, port int) Server { ShortName: shortName, Address: address, Port: port, - RCON: util.SecureRandomString(10), + RCON: stringutil.SecureRandomString(10), ReservedSlots: 0, - Password: util.SecureRandomString(10), + Password: stringutil.SecureRandomString(10), IsEnabled: true, EnableStats: true, TokenCreatedOn: time.Unix(0, 0), diff --git a/internal/forum/forum_service.go b/internal/forum/forum_service.go index 98d11cb1a..f982de4ce 100644 --- a/internal/forum/forum_service.go +++ b/internal/forum/forum_service.go @@ -11,7 +11,7 @@ import ( "github.com/leighmacdonald/gbans/internal/domain" "github.com/leighmacdonald/gbans/internal/httphelper" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" + "github.com/leighmacdonald/gbans/pkg/stringutil" "github.com/leighmacdonald/steamid/v4/steamid" ) @@ -95,8 +95,8 @@ func (f *forumHandler) onAPICreateForumCategory() gin.HandlerFunc { } category := domain.ForumCategory{ - Title: util.SanitizeUGC(req.Title), - Description: util.SanitizeUGC(req.Description), + Title: stringutil.SanitizeUGC(req.Title), + Description: stringutil.SanitizeUGC(req.Description), Ordering: req.Ordering, TimeStamped: domain.NewTimeStamped(), } @@ -158,8 +158,8 @@ func (f *forumHandler) onAPIUpdateForumCategory() gin.HandlerFunc { return } - category.Title = util.SanitizeUGC(req.Title) - category.Description = util.SanitizeUGC(req.Description) + category.Title = stringutil.SanitizeUGC(req.Title) + category.Description = stringutil.SanitizeUGC(req.Description) category.Ordering = req.Ordering if errSave := f.forums.ForumCategorySave(ctx, &category); errSave != nil { @@ -188,8 +188,8 @@ func (f *forumHandler) onAPICreateForumForum() gin.HandlerFunc { forum := domain.Forum{ ForumCategoryID: req.ForumCategoryID, - Title: util.SanitizeUGC(req.Title), - Description: util.SanitizeUGC(req.Description), + Title: stringutil.SanitizeUGC(req.Title), + Description: stringutil.SanitizeUGC(req.Description), Ordering: req.Ordering, PermissionLevel: req.PermissionLevel, TimeStamped: domain.NewTimeStamped(), @@ -230,8 +230,8 @@ func (f *forumHandler) onAPIUpdateForumForum() gin.HandlerFunc { } forum.ForumCategoryID = req.ForumCategoryID - forum.Title = util.SanitizeUGC(req.Title) - forum.Description = util.SanitizeUGC(req.Description) + forum.Title = stringutil.SanitizeUGC(req.Title) + forum.Description = stringutil.SanitizeUGC(req.Description) forum.Ordering = req.Ordering forum.PermissionLevel = req.PermissionLevel @@ -362,7 +362,7 @@ func (f *forumHandler) onAPIThreadUpdate() gin.HandlerFunc { return } - req.Title = util.SanitizeUGC(req.Title) + req.Title = stringutil.SanitizeUGC(req.Title) if len(req.Title) < 2 { httphelper.HandleErrBadRequest(ctx) @@ -492,7 +492,7 @@ func (f *forumHandler) onAPIThreadMessageUpdate() gin.HandlerFunc { return } - req.BodyMD = util.SanitizeUGC(req.BodyMD) + req.BodyMD = stringutil.SanitizeUGC(req.BodyMD) if len(req.BodyMD) < 10 { httphelper.HandleErrBadRequest(ctx) @@ -642,7 +642,7 @@ func (f *forumHandler) onAPIThreadCreateReply() gin.HandlerFunc { return } - req.BodyMD = util.SanitizeUGC(req.BodyMD) + req.BodyMD = stringutil.SanitizeUGC(req.BodyMD) if len(req.BodyMD) < 3 { httphelper.ResponseErr(ctx, http.StatusBadRequest, fmt.Errorf("body: %w", domain.ErrTooShort)) diff --git a/internal/httphelper/helper.go b/internal/httphelper/helper.go index e1ef038b5..12d24283c 100644 --- a/internal/httphelper/helper.go +++ b/internal/httphelper/helper.go @@ -12,8 +12,8 @@ import ( "github.com/gofrs/uuid/v5" "github.com/gorilla/schema" "github.com/leighmacdonald/gbans/internal/domain" + "github.com/leighmacdonald/gbans/pkg/convert" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" "github.com/leighmacdonald/steamid/v4/steamid" ) @@ -114,7 +114,7 @@ func GetIntParam(ctx *gin.Context, key string) (int, error) { return 0, fmt.Errorf("%w: %s", domain.ErrParamKeyMissing, key) } - return util.StringToInt(valueStr), nil + return convert.StringToInt(valueStr), nil } func GetStringParam(ctx *gin.Context, key string) (string, error) { diff --git a/internal/httphelper/network.go b/internal/httphelper/network.go new file mode 100644 index 000000000..4253006f1 --- /dev/null +++ b/internal/httphelper/network.go @@ -0,0 +1,15 @@ +package httphelper + +import ( + "net/http" + "time" +) + +// NewHTTPClient allocates a preconfigured *http.Client. +func NewHTTPClient() *http.Client { + c := &http.Client{ + Timeout: time.Second * 10, + } + + return c +} diff --git a/internal/patreon/patreon_usecase.go b/internal/patreon/patreon_usecase.go index 66c38dbfe..72f42160e 100644 --- a/internal/patreon/patreon_usecase.go +++ b/internal/patreon/patreon_usecase.go @@ -12,15 +12,16 @@ import ( "github.com/austinbspencer/patreon-go-wrapper" "github.com/leighmacdonald/gbans/internal/domain" + "github.com/leighmacdonald/gbans/internal/httphelper" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" + "github.com/leighmacdonald/gbans/pkg/oauth" "github.com/leighmacdonald/steamid/v4/steamid" ) type patreonUsecase struct { repository domain.PatreonRepository manager *Manager - stateTracker *util.LoginStateTracker + stateTracker *oauth.LoginStateTracker cu domain.ConfigUsecase } @@ -29,7 +30,7 @@ func NewPatreonUsecase(repository domain.PatreonRepository, configUsecase domain repository: repository, cu: configUsecase, manager: NewPatreonManager(configUsecase), - stateTracker: util.NewLoginStateTracker(), + stateTracker: oauth.NewLoginStateTracker(), } } @@ -85,7 +86,7 @@ func (p patreonUsecase) refreshToken(ctx context.Context, auth domain.PatreonCre req.Header.Add("Content-Type", "application/x-www-form-urlencoded") - httpClient := util.NewHTTPClient() + httpClient := httphelper.NewHTTPClient() resp, errResp := httpClient.Do(req) if errResp != nil { @@ -172,7 +173,7 @@ func (p patreonUsecase) OnOauthLogin(ctx context.Context, state string, code str req.Header.Add("Content-Type", "application/x-www-form-urlencoded") - httpClient := util.NewHTTPClient() + httpClient := httphelper.NewHTTPClient() resp, errResp := httpClient.Do(req) if errResp != nil { diff --git a/internal/person/person_usecase.go b/internal/person/person_usecase.go index 50d671ae2..37d0dcdb8 100644 --- a/internal/person/person_usecase.go +++ b/internal/person/person_usecase.go @@ -10,7 +10,7 @@ import ( "github.com/leighmacdonald/gbans/internal/domain" "github.com/leighmacdonald/gbans/internal/thirdparty" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" + "github.com/leighmacdonald/gbans/pkg/stringutil" "github.com/leighmacdonald/steamid/v4/steamid" "github.com/leighmacdonald/steamweb/v2" "golang.org/x/sync/errgroup" @@ -273,7 +273,7 @@ func (u personUsecase) SavePersonSettings(ctx context.Context, user domain.Perso settings.ForumProfileMessages = update.ForumProfileMessages settings.StatsHidden = update.StatsHidden - settings.ForumSignature = util.SanitizeUGC(update.ForumSignature) + settings.ForumSignature = stringutil.SanitizeUGC(update.ForumSignature) settings.CenterProjectiles = update.CenterProjectiles if errSave := u.persons.SavePersonSettings(ctx, &settings); errSave != nil { diff --git a/internal/steamgroup/steamgroup_service.go b/internal/steamgroup/steamgroup_service.go index 257318b7c..be1285d8b 100644 --- a/internal/steamgroup/steamgroup_service.go +++ b/internal/steamgroup/steamgroup_service.go @@ -9,8 +9,8 @@ import ( "github.com/gin-gonic/gin" "github.com/leighmacdonald/gbans/internal/domain" "github.com/leighmacdonald/gbans/internal/httphelper" + "github.com/leighmacdonald/gbans/pkg/datetime" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" ) type steamgroupHandler struct { @@ -71,7 +71,7 @@ func (h steamgroupHandler) onAPIPostBansGroupCreate() gin.HandlerFunc { sid = httphelper.CurrentUserProfile(ctx).SteamID ) - duration, errDuration := util.CalcDuration(req.Duration, req.ValidUntil) + duration, errDuration := datetime.CalcDuration(req.Duration, req.ValidUntil) if errDuration != nil { httphelper.HandleErrBadRequest(ctx) slog.Warn("Got invalid duration", log.ErrAttr(errDuration)) diff --git a/internal/steamgroup/steamgroup_usecase.go b/internal/steamgroup/steamgroup_usecase.go index 928410627..0050ef05c 100644 --- a/internal/steamgroup/steamgroup_usecase.go +++ b/internal/steamgroup/steamgroup_usecase.go @@ -9,7 +9,7 @@ import ( "github.com/leighmacdonald/gbans/internal/discord" "github.com/leighmacdonald/gbans/internal/domain" - "github.com/leighmacdonald/gbans/pkg/util" + "github.com/leighmacdonald/gbans/internal/httphelper" "github.com/leighmacdonald/steamid/v4/steamid" "github.com/leighmacdonald/steamweb/v2" ) @@ -42,7 +42,7 @@ func (s banGroupUsecase) UpdateCache(ctx context.Context) error { return err } - client := util.NewHTTPClient() + client := httphelper.NewHTTPClient() for _, group := range groups { listURL := fmt.Sprintf("https://steamcommunity.com/gid/%d/memberslistxml/?xml=1", group.GroupID.Int64()) diff --git a/internal/thirdparty/logstf.go b/internal/thirdparty/logstf.go index 01432b9fb..63def5efb 100644 --- a/internal/thirdparty/logstf.go +++ b/internal/thirdparty/logstf.go @@ -8,7 +8,7 @@ import ( "net/http" "time" - "github.com/leighmacdonald/gbans/pkg/util" + "github.com/leighmacdonald/gbans/internal/httphelper" "github.com/leighmacdonald/steamid/v4/steamid" ) @@ -45,7 +45,7 @@ type LogsTFResult struct { // LogsTFOverview queries the logstf api for metadata about a players logs // http://logs.tf/api/v1/log?title=X&uploader=Y&player=Z&limit=N&offset=N func LogsTFOverview(ctx context.Context, sid steamid.SteamID) (*LogsTFResult, error) { - httpClient := util.NewHTTPClient() + httpClient := httphelper.NewHTTPClient() localCtx, cancel := context.WithTimeout(ctx, time.Second*15) defer cancel() diff --git a/internal/thirdparty/steam_api.go b/internal/thirdparty/steam_api.go index b771e91a7..eb1fe2b32 100644 --- a/internal/thirdparty/steam_api.go +++ b/internal/thirdparty/steam_api.go @@ -8,7 +8,6 @@ import ( "time" "github.com/leighmacdonald/gbans/internal/domain" - "github.com/leighmacdonald/gbans/pkg/util" "github.com/leighmacdonald/steamid/v4/steamid" "github.com/leighmacdonald/steamweb/v2" ) @@ -33,7 +32,7 @@ func FetchPlayerBans(ctx context.Context, steamIDs []steamid.SteamID) ([]steamwe var ( total = uint64(len(steamIDs) - index) - maxResults = util.Min(steamQueryMaxResults, total) + maxResults = min(steamQueryMaxResults, total) ids = steamIDs[index : index+int(maxResults)] ) diff --git a/internal/wiki/wiki_usecase_test.go b/internal/wiki/wiki_usecase_test.go index 2cf0f7bef..2edec92e2 100644 --- a/internal/wiki/wiki_usecase_test.go +++ b/internal/wiki/wiki_usecase_test.go @@ -7,7 +7,7 @@ import ( "github.com/leighmacdonald/gbans/internal/domain" "github.com/leighmacdonald/gbans/internal/domain/mocks" "github.com/leighmacdonald/gbans/internal/wiki" - "github.com/leighmacdonald/gbans/pkg/util" + "github.com/leighmacdonald/gbans/pkg/stringutil" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) @@ -17,7 +17,7 @@ func TestGetWikiPageBySlugSuccess(t *testing.T) { PermissionLevel: domain.PGuest, } - mockPage := domain.NewWikiPage("test", util.SecureRandomString(100)) + mockPage := domain.NewWikiPage("test", stringutil.SecureRandomString(100)) mockRepo := new(mocks.MockWikiRepository) mockRepo. On("GetWikiPageBySlug", mock.Anything, mock.AnythingOfType("string")). diff --git a/internal/wordfilter/wordfilter_usecase.go b/internal/wordfilter/wordfilter_usecase.go index eba5346ce..499d7985d 100644 --- a/internal/wordfilter/wordfilter_usecase.go +++ b/internal/wordfilter/wordfilter_usecase.go @@ -8,7 +8,7 @@ import ( "time" "github.com/leighmacdonald/gbans/internal/domain" - "github.com/leighmacdonald/gbans/pkg/util" + "github.com/leighmacdonald/gbans/pkg/datetime" ) type wordFilterUsecase struct { @@ -64,9 +64,9 @@ func (w *wordFilterUsecase) Create(ctx context.Context, user domain.PersonInfo, return domain.Filter{}, domain.ErrInvalidPattern } - _, errDur := util.ParseDuration(opts.Duration) + _, errDur := datetime.ParseDuration(opts.Duration) if errDur != nil { - return domain.Filter{}, util.ErrInvalidDuration + return domain.Filter{}, datetime.ErrInvalidDuration } if opts.IsRegex { diff --git a/pkg/convert/convert.go b/pkg/convert/convert.go new file mode 100644 index 000000000..a896ea25c --- /dev/null +++ b/pkg/convert/convert.go @@ -0,0 +1,32 @@ +package convert + +import ( + "math" + "strconv" +) + +// StringToFloat64 converts a string to a float64, returning a default values on +// conversion error. +func StringToFloat64(numericString string, defaultValue float64) float64 { + value, errParseFloat := strconv.ParseFloat(numericString, 64) + if errParseFloat != nil { + return defaultValue + } + + return value +} + +const DefaultIntAllocate int = 0 + +func StringToInt(desired string) int { + parsed, err := strconv.Atoi(desired) + if err != nil { + return DefaultIntAllocate + } + + if parsed > 0 && parsed <= math.MaxInt32 { + return parsed + } + + return DefaultIntAllocate +} diff --git a/pkg/util/network.go b/pkg/convert/net.go similarity index 59% rename from pkg/util/network.go rename to pkg/convert/net.go index 15a4b1ddc..da775f53e 100644 --- a/pkg/util/network.go +++ b/pkg/convert/net.go @@ -1,21 +1,10 @@ -package util +package convert import ( "encoding/binary" "net" - "net/http" - "time" ) -// NewHTTPClient allocates a preconfigured *http.Client. -func NewHTTPClient() *http.Client { - c := &http.Client{ - Timeout: time.Second * 10, - } - - return c -} - func IP2Int(ip net.IP) uint32 { if len(ip) == 16 { return binary.BigEndian.Uint32(ip[12:16]) diff --git a/pkg/util/conversion.go b/pkg/datetime/conversion.go similarity index 81% rename from pkg/util/conversion.go rename to pkg/datetime/conversion.go index 97fb533ba..bec318089 100644 --- a/pkg/util/conversion.go +++ b/pkg/datetime/conversion.go @@ -1,46 +1,17 @@ -package util +package datetime import ( "errors" "fmt" - "math" "regexp" "strconv" "strings" "time" ) -var ErrInvalidDuration = errors.New("invalid duration string") - -// StringToFloat64 converts a string to a float64, returning a default values on -// conversion error. -func StringToFloat64(numericString string, defaultValue float64) float64 { - value, errParseFloat := strconv.ParseFloat(numericString, 64) - if errParseFloat != nil { - return defaultValue - } - - return value -} - -const DefaultIntAllocate int = 0 - -func StringToInt(desired string) int { - parsed, err := strconv.Atoi(desired) - if err != nil { - return DefaultIntAllocate - } - - if parsed > 0 && parsed <= math.MaxInt32 { - return parsed - } - - return DefaultIntAllocate -} - var ( reDuration = regexp.MustCompile(`^(\d+)([smhdwMy])$`) - errInvalidDuration = errors.New("invalid duration") + ErrInvalidDuration = errors.New("invalid duration") ) // FmtTimeShort returns a common format for time display. @@ -150,12 +121,12 @@ func ParseUserStringDuration(durationString string) (time.Duration, error) { matchDuration := reDuration.FindStringSubmatch(durationString) if matchDuration == nil { - return 0, errInvalidDuration + return 0, ErrInvalidDuration } valueInt, errParseInt := strconv.ParseInt(matchDuration[1], 10, 64) if errParseInt != nil { - return 0, errInvalidDuration + return 0, ErrInvalidDuration } var ( @@ -180,7 +151,7 @@ func ParseUserStringDuration(durationString string) (time.Duration, error) { return day * 365 * value, nil } - return 0, errInvalidDuration + return 0, ErrInvalidDuration } func ParseDuration(value string) (time.Duration, error) { @@ -204,7 +175,7 @@ func CalcDuration(durationString string, validUntil time.Time) (time.Duration, e if durationString == "custom" { dur := time.Until(validUntil) if dur < 0 { - return 0, errInvalidDuration + return 0, ErrInvalidDuration } return dur, nil diff --git a/pkg/util/time_test.go b/pkg/datetime/time_test.go similarity index 80% rename from pkg/util/time_test.go rename to pkg/datetime/time_test.go index c57194482..906dfae8e 100644 --- a/pkg/util/time_test.go +++ b/pkg/datetime/time_test.go @@ -1,9 +1,9 @@ -package util_test +package datetime_test import ( "testing" - "github.com/leighmacdonald/gbans/pkg/util" + "github.com/leighmacdonald/gbans/pkg/datetime" "github.com/stretchr/testify/require" ) @@ -42,7 +42,7 @@ func TestParseDuration(t *testing.T) { }, } for _, test := range tests { - d, errParseDuration := util.ParseUserStringDuration(test.input) + d, errParseDuration := datetime.ParseUserStringDuration(test.input) require.NoError(t, errParseDuration, "Failed to parse: %s", test.input) require.Equal(t, test.expected, d.String(), "Failed to parse: %s", test.input) } diff --git a/pkg/util/filesystem.go b/pkg/fs/filesystem.go similarity index 98% rename from pkg/util/filesystem.go rename to pkg/fs/filesystem.go index f2e63267a..843277c19 100644 --- a/pkg/util/filesystem.go +++ b/pkg/fs/filesystem.go @@ -1,4 +1,4 @@ -package util +package fs import ( "os" diff --git a/pkg/ip2location/ip2location.go b/pkg/ip2location/ip2location.go index 71f5f6443..1a7021fa0 100644 --- a/pkg/ip2location/ip2location.go +++ b/pkg/ip2location/ip2location.go @@ -23,8 +23,8 @@ import ( "sync" "time" + "github.com/leighmacdonald/gbans/pkg/convert" "github.com/leighmacdonald/gbans/pkg/log" - "github.com/leighmacdonald/gbans/pkg/util" ) const downloadURL = "https://www.ip2location.com/download/?token=%s&file=%s" @@ -188,7 +188,7 @@ type ASNRecords []ASNRecord func (r ASNRecords) Hosts() uint32 { total := uint32(0) for _, n := range r { - total += util.IP2Int(*n.IPTo) - util.IP2Int(*n.IPFrom) + total += convert.IP2Int(*n.IPTo) - convert.IP2Int(*n.IPFrom) } return total @@ -466,8 +466,8 @@ func ReadLocationRecords(ctx context.Context, path string, ipv6 bool, onRecords RegionName: recordLine[4], CityName: recordLine[5], LatLong: LatLong{ - util.StringToFloat64(recordLine[6], 0), - util.StringToFloat64(recordLine[7], 0), + convert.StringToFloat64(recordLine[6], 0), + convert.StringToFloat64(recordLine[7], 0), }, } diff --git a/pkg/log/log.go b/pkg/log/log.go index 3855a68d9..ad3bfde5c 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -2,6 +2,7 @@ package log import ( "fmt" + "io" "log/slog" "os" "runtime" @@ -67,3 +68,9 @@ func HandlerName(skip int) slog.Attr { return slog.String("func", "unknown") } + +func Closer(closer io.Closer) { + if errClose := closer.Close(); errClose != nil { + slog.Error("Failed to close", ErrAttr(errClose)) + } +} diff --git a/pkg/logparse/log_parser_test.go b/pkg/logparse/log_parser_test.go index 11642e934..bff357673 100644 --- a/pkg/logparse/log_parser_test.go +++ b/pkg/logparse/log_parser_test.go @@ -7,8 +7,8 @@ import ( "testing" "time" + "github.com/leighmacdonald/gbans/pkg/fs" "github.com/leighmacdonald/gbans/pkg/logparse" - "github.com/leighmacdonald/gbans/pkg/util" "github.com/leighmacdonald/steamid/v4/steamid" "github.com/stretchr/testify/require" ) @@ -35,7 +35,7 @@ func TestParseSourcePlayer(t *testing.T) { func TestParseFile(t *testing.T) { t.Parallel() - logFilePath := util.FindFile(path.Join("testdata", "log_1.log"), "gbans") + logFilePath := fs.FindFile(path.Join("testdata", "log_1.log"), "gbans") openFile, e := os.ReadFile(logFilePath) if e != nil { diff --git a/pkg/util/state_tracker.go b/pkg/oauth/state_tracker.go similarity index 91% rename from pkg/util/state_tracker.go rename to pkg/oauth/state_tracker.go index 9f8f0ddce..af44c2950 100644 --- a/pkg/util/state_tracker.go +++ b/pkg/oauth/state_tracker.go @@ -1,9 +1,10 @@ -package util +package oauth import ( "sync" "time" + "github.com/leighmacdonald/gbans/pkg/stringutil" "github.com/leighmacdonald/steamid/v4/steamid" ) @@ -46,7 +47,7 @@ func (t LoginStateTracker) Get(state string) (steamid.SteamID, bool) { } func (t LoginStateTracker) Create(steamID steamid.SteamID) string { - state := SecureRandomString(24) + state := stringutil.SecureRandomString(24) t.stateMapMu.Lock() t.stateMap[steamID] = activeState{ diff --git a/pkg/util/text.go b/pkg/stringutil/text.go similarity index 99% rename from pkg/util/text.go rename to pkg/stringutil/text.go index 7116c92a3..b4230eea8 100644 --- a/pkg/util/text.go +++ b/pkg/stringutil/text.go @@ -1,5 +1,5 @@ // Package util provides some useful functions that don't fit anywhere else. -package util +package stringutil import ( "crypto/rand" diff --git a/pkg/util/text_test.go b/pkg/stringutil/text_test.go similarity index 59% rename from pkg/util/text_test.go rename to pkg/stringutil/text_test.go index 8782304b9..7cc1aaf09 100644 --- a/pkg/util/text_test.go +++ b/pkg/stringutil/text_test.go @@ -1,9 +1,9 @@ -package util_test +package stringutil_test import ( "testing" - "github.com/leighmacdonald/gbans/pkg/util" + "github.com/leighmacdonald/gbans/pkg/stringutil" "github.com/stretchr/testify/require" ) @@ -13,6 +13,6 @@ bbbbbbbbbb cccccccccc dddddddddd ` - v := util.StringChunkDelimited(s, 30, "\n") + v := stringutil.StringChunkDelimited(s, 30, "\n") require.Len(t, v, 2) } diff --git a/pkg/util/misc.go b/pkg/util/misc.go deleted file mode 100644 index 445aa78d9..000000000 --- a/pkg/util/misc.go +++ /dev/null @@ -1,23 +0,0 @@ -package util - -import ( - "io" - "log/slog" - - "github.com/leighmacdonald/gbans/pkg/log" - "golang.org/x/exp/constraints" -) - -func LogCloser(closer io.Closer) { - if errClose := closer.Close(); errClose != nil { - slog.Error("Failed to close", log.ErrAttr(errClose)) - } -} - -func Min[T constraints.Ordered](a, b T) T { - if a < b { - return a - } - - return b -}