From e42c0fff31ce85737520f6ccccaea52c08592614 Mon Sep 17 00:00:00 2001 From: Leigh MacDonald Date: Mon, 24 Jun 2024 00:26:21 -0600 Subject: [PATCH 01/14] Add duplicate case message for reports. Rename ResponseErr --- internal/appeal/appeal_service.go | 6 ++-- internal/asset/asset_service.go | 10 +++--- internal/auth/auth_service.go | 6 ++-- internal/ban/ban_asn_service.go | 6 ++-- internal/ban/ban_net_service.go | 14 ++++---- internal/ban/ban_steam_service.go | 40 +++++++++++------------ internal/blocklist/blocklist_service.go | 4 +-- internal/chat/chat_service.go | 8 ++--- internal/contest/contest_service.go | 34 +++++++++---------- internal/demo/demo_service.go | 2 +- internal/discord/discord_service_oauth.go | 8 ++--- internal/forum/forum_service.go | 24 +++++++------- internal/httphelper/helper.go | 4 +-- internal/httphelper/http.go | 12 +++---- internal/match/match_service.go | 14 ++++---- internal/network/network_service.go | 2 +- internal/patreon/patreon_service.go | 6 ++-- internal/report/report_service.go | 7 ++++ internal/srcds/srcds_service.go | 6 ++-- internal/steamgroup/steamgroup_service.go | 2 +- 20 files changed, 111 insertions(+), 104 deletions(-) diff --git a/internal/appeal/appeal_service.go b/internal/appeal/appeal_service.go index 7b4d8b97..05c0af4c 100644 --- a/internal/appeal/appeal_service.go +++ b/internal/appeal/appeal_service.go @@ -41,7 +41,7 @@ func (h *appealHandler) onAPIGetBanMessages() gin.HandlerFunc { return func(ctx *gin.Context) { banID, errParam := httphelper.GetInt64Param(ctx, "ban_id") if errParam != nil { - httphelper.ResponseErr(ctx, http.StatusNotFound, domain.ErrInvalidParameter) + httphelper.ResponseApiErr(ctx, http.StatusNotFound, domain.ErrInvalidParameter) slog.Warn("Got invalid ban_id parameter", log.ErrAttr(errParam), log.HandlerName(2)) return @@ -102,7 +102,7 @@ func (h *appealHandler) editBanMessage() gin.HandlerFunc { msg, errSave := h.appealUsecase.EditBanMessage(ctx, curUser, reportMessageID, req.BodyMD) if errSave != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Failed to save ban appeal message", log.ErrAttr(errSave), log.HandlerName(2)) return @@ -143,7 +143,7 @@ func (h *appealHandler) onAPIGetAppeals() gin.HandlerFunc { bans, errBans := h.appealUsecase.GetAppealsByActivity(ctx, req) if errBans != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Failed to fetch appeals", log.ErrAttr(errBans)) return diff --git a/internal/asset/asset_service.go b/internal/asset/asset_service.go index 947e589b..7a3fe14c 100644 --- a/internal/asset/asset_service.go +++ b/internal/asset/asset_service.go @@ -48,7 +48,7 @@ func (h mediaHandler) onAPISaveMedia() gin.HandlerFunc { mediaFile, errOpen := req.File.Open() if errOpen != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Failed to open form file", log.ErrAttr(errOpen), handlerName) return @@ -60,7 +60,7 @@ func (h mediaHandler) onAPISaveMedia() gin.HandlerFunc { media, errMedia := h.assets.Create(ctx, httphelper.CurrentUserProfile(ctx).SteamID, "media", req.Name, mediaFile) if errMedia != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, errMedia) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, errMedia) slog.Error("Failed to create new asset", log.ErrAttr(errMedia), handlerName) return @@ -76,7 +76,7 @@ func (h mediaHandler) onGetByUUID() gin.HandlerFunc { return func(ctx *gin.Context) { mediaID, idErr := httphelper.GetUUIDParam(ctx, "asset_id") if idErr != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) slog.Error("Got invalid asset_id", handlerName) return @@ -84,7 +84,7 @@ func (h mediaHandler) onGetByUUID() gin.HandlerFunc { asset, reader, errGet := h.assets.Get(ctx, mediaID) if errGet != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, errGet) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, errGet) slog.Error("Failed to load asset", slog.String("asset_id", mediaID.String()), handlerName) return @@ -93,7 +93,7 @@ func (h mediaHandler) onGetByUUID() gin.HandlerFunc { if asset.IsPrivate { user := httphelper.CurrentUserProfile(ctx) if !user.SteamID.Valid() && (user.SteamID == asset.AuthorID || user.HasPermission(domain.PModerator)) { - httphelper.ResponseErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied) + httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied) slog.Warn("Tried to access private asset", slog.String("asset_id", mediaID.String()), handlerName) return diff --git a/internal/auth/auth_service.go b/internal/auth/auth_service.go index 8297a650..94cc13a5 100644 --- a/internal/auth/auth_service.go +++ b/internal/auth/auth_service.go @@ -165,7 +165,7 @@ func (h authHandler) onAPILogout() gin.HandlerFunc { return func(ctx *gin.Context) { fingerprint, errCookie := ctx.Cookie(domain.FingerprintCookieName) if errCookie != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, nil) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, nil) slog.Warn("Failed to get fingerprint", handlerName) return @@ -184,14 +184,14 @@ func (h authHandler) onAPILogout() gin.HandlerFunc { personAuth := domain.PersonAuth{} if errGet := h.authUsecase.GetPersonAuthByRefreshToken(ctx, fingerprint, &personAuth); errGet != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, nil) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, nil) slog.Warn("Failed to load person via fingerprint", handlerName) return } if errDelete := h.authUsecase.DeletePersonAuth(ctx, personAuth.PersonAuthID); errDelete != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, nil) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, nil) slog.Error("Failed to delete person auth on logout", log.ErrAttr(errDelete), handlerName) return diff --git a/internal/ban/ban_asn_service.go b/internal/ban/ban_asn_service.go index 6d67f462..2486ce90 100644 --- a/internal/ban/ban_asn_service.go +++ b/internal/ban/ban_asn_service.go @@ -64,7 +64,7 @@ func (h banASNHandler) onAPIGetBansASN() gin.HandlerFunc { bansASN, errBans := h.banASN.Get(ctx, req) if errBans != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Failed to fetch banASN", log.ErrAttr(errBans)) return @@ -78,7 +78,7 @@ func (h banASNHandler) onAPIDeleteBansASN() gin.HandlerFunc { return func(ctx *gin.Context) { asnID, asnIDErr := httphelper.GetInt64Param(ctx, "asn_id") if asnIDErr != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) return } @@ -103,7 +103,7 @@ func (h banASNHandler) onAPIPostBansASNUpdate() gin.HandlerFunc { return func(ctx *gin.Context) { asnID, asnIDErr := httphelper.GetInt64Param(ctx, "asn_id") if asnIDErr != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) return } diff --git a/internal/ban/ban_net_service.go b/internal/ban/ban_net_service.go index ced51d53..f60021cf 100644 --- a/internal/ban/ban_net_service.go +++ b/internal/ban/ban_net_service.go @@ -37,7 +37,7 @@ func (h banNetHandler) onAPIExportBansValveIP() gin.HandlerFunc { return func(ctx *gin.Context) { bans, errBans := h.bans.Get(ctx, domain.CIDRBansQueryFilter{}) if errBans != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) return } @@ -87,7 +87,7 @@ func (h banNetHandler) onAPIPostBansCIDRCreate() gin.HandlerFunc { if errBanCIDR := domain.NewBanCIDR(sid, targetID, duration, req.Reason, req.ReasonText, req.Note, domain.Web, req.CIDR, domain.Banned, &banCIDR); errBanCIDR != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) slog.Warn("Failed to create new ban cidr", log.ErrAttr(errBanCIDR)) return @@ -95,12 +95,12 @@ func (h banNetHandler) onAPIPostBansCIDRCreate() gin.HandlerFunc { if errBan := h.bans.Ban(ctx, &banCIDR); errBan != nil { if errors.Is(errBan, domain.ErrDuplicate) { - httphelper.ResponseErr(ctx, http.StatusConflict, domain.ErrDuplicate) + httphelper.ResponseApiErr(ctx, http.StatusConflict, domain.ErrDuplicate) return } - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Failed to save cidr ban", log.ErrAttr(errBan)) return @@ -124,7 +124,7 @@ func (h banNetHandler) onAPIGetBansCIDR() gin.HandlerFunc { bans, errBans := h.bans.Get(ctx, req) if errBans != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Failed to fetch cidr bans", log.ErrAttr(errBans)) return @@ -138,7 +138,7 @@ func (h banNetHandler) onAPIDeleteBansCIDR() gin.HandlerFunc { return func(ctx *gin.Context) { netID, netIDErr := httphelper.GetInt64Param(ctx, "net_id") if netIDErr != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) return } @@ -164,7 +164,7 @@ func (h banNetHandler) onAPIPostBansCIDRUpdate() gin.HandlerFunc { return func(ctx *gin.Context) { netID, banIDErr := httphelper.GetInt64Param(ctx, "net_id") if banIDErr != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) return } diff --git a/internal/ban/ban_steam_service.go b/internal/ban/ban_steam_service.go index d5f69f90..35f0f639 100644 --- a/internal/ban/ban_steam_service.go +++ b/internal/ban/ban_steam_service.go @@ -69,7 +69,7 @@ func (h banHandler) onAPIPostSetBanAppealStatus() gin.HandlerFunc { return func(ctx *gin.Context) { banID, banIDErr := httphelper.GetInt64Param(ctx, "ban_id") if banIDErr != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) return } @@ -81,13 +81,13 @@ func (h banHandler) onAPIPostSetBanAppealStatus() gin.HandlerFunc { bannedPerson, banErr := h.bansSteam.GetByBanID(ctx, banID, false, true) if banErr != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) return } if bannedPerson.AppealState == req.AppealState { - httphelper.ResponseErr(ctx, http.StatusConflict, domain.ErrStateUnchanged) + httphelper.ResponseApiErr(ctx, http.StatusConflict, domain.ErrStateUnchanged) return } @@ -96,7 +96,7 @@ func (h banHandler) onAPIPostSetBanAppealStatus() gin.HandlerFunc { bannedPerson.AppealState = req.AppealState if errSave := h.bansSteam.Save(ctx, &bannedPerson.BanSteam); errSave != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) return } @@ -136,7 +136,7 @@ func (h banHandler) onAPIGetBanByID() gin.HandlerFunc { banID, errID := httphelper.GetInt64Param(ctx, "ban_id") if errID != nil || banID == 0 { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) return } @@ -179,7 +179,7 @@ func (h banHandler) onAPIGetSourceBans() gin.HandlerFunc { return func(ctx *gin.Context) { steamID, errID := httphelper.GetSID64Param(ctx, "steam_id") if errID != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) return } @@ -219,7 +219,7 @@ func (h banHandler) onAPIExportBansValveSteamID() gin.HandlerFunc { if len(authorizedKeys) > 0 { key, ok := ctx.GetQuery("key") if !ok || !slices.Contains(authorizedKeys, key) { - httphelper.ResponseErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied) + httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied) return } @@ -228,7 +228,7 @@ func (h banHandler) onAPIExportBansValveSteamID() gin.HandlerFunc { // TODO limit to perm? bans, errBans := h.bansSteam.Get(ctx, domain.SteamBansQueryFilter{}) if errBans != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) return } @@ -255,7 +255,7 @@ func (h banHandler) onAPIExportBansTF2BD() gin.HandlerFunc { if len(authorizedKeys) > 0 { key, ok := ctx.GetQuery("key") if !ok || !slices.Contains(authorizedKeys, key) { - httphelper.ResponseErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied) + httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied) return } @@ -264,7 +264,7 @@ func (h banHandler) onAPIExportBansTF2BD() gin.HandlerFunc { bans, errBans := h.bansSteam.Get(ctx, domain.SteamBansQueryFilter{}) if errBans != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) return } @@ -318,7 +318,7 @@ func (h banHandler) onAPIGetBansSteam() gin.HandlerFunc { bans, errBans := h.bansSteam.Get(ctx, params) if errBans != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Failed to fetch steam bans", log.ErrAttr(errBans)) return @@ -332,7 +332,7 @@ func (h banHandler) onAPIPostBanDelete() gin.HandlerFunc { return func(ctx *gin.Context) { banID, banIDErr := httphelper.GetInt64Param(ctx, "ban_id") if banIDErr != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) return } @@ -363,7 +363,7 @@ func (h banHandler) onAPIPostBanDelete() gin.HandlerFunc { } if !changed { - httphelper.ResponseErr(ctx, http.StatusOK, domain.ErrUnbanFailed) + httphelper.ResponseApiErr(ctx, http.StatusOK, domain.ErrUnbanFailed) return } @@ -387,7 +387,7 @@ func (h banHandler) onAPIPostBanUpdate() gin.HandlerFunc { return func(ctx *gin.Context) { banID, banIDErr := httphelper.GetInt64Param(ctx, "ban_id") if banIDErr != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) return } @@ -398,21 +398,21 @@ func (h banHandler) onAPIPostBanUpdate() gin.HandlerFunc { } if time.Since(req.ValidUntil) > 0 { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) return } bannedPerson, banErr := h.bansSteam.GetByBanID(ctx, banID, false, true) if banErr != nil { - httphelper.ResponseErr(ctx, http.StatusNotFound, domain.ErrNotFound) + httphelper.ResponseApiErr(ctx, http.StatusNotFound, domain.ErrNotFound) return } if req.Reason == domain.Custom { if req.ReasonText == "" { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) return } @@ -430,7 +430,7 @@ func (h banHandler) onAPIPostBanUpdate() gin.HandlerFunc { bannedPerson.ValidUntil = req.ValidUntil if errSave := h.bansSteam.Save(ctx, &bannedPerson.BanSteam); errSave != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Failed to save updated ban", log.ErrAttr(errSave)) return @@ -444,7 +444,7 @@ func (h banHandler) onAPIGetBanBySteam() gin.HandlerFunc { return func(ctx *gin.Context) { steamID, err := httphelper.GetSID64Param(ctx, "steam_id") if err != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) slog.Error("Failed to get steamid", log.ErrAttr(err)) return @@ -452,7 +452,7 @@ func (h banHandler) onAPIGetBanBySteam() gin.HandlerFunc { ban, errBans := h.bansSteam.GetBySteamID(ctx, steamID, false, false) if errBans != nil && !errors.Is(errBans, domain.ErrNoResult) { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Failed to get ban record for steamid", log.ErrAttr(errBans)) return diff --git a/internal/blocklist/blocklist_service.go b/internal/blocklist/blocklist_service.go index 8115e73c..16a4a772 100644 --- a/internal/blocklist/blocklist_service.go +++ b/internal/blocklist/blocklist_service.go @@ -86,7 +86,7 @@ func (b *blocklistHandler) onAPICreateWhitelistSteam() gin.HandlerFunc { steamID, ok := req.SteamID(ctx) if !ok { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrInvalidSID) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidSID) slog.Warn("Got invalid steamid", slog.String("steam_id", req.SteamIDValue)) return @@ -232,7 +232,7 @@ func (b *blocklistHandler) onAPIPostBlockListUpdate() gin.HandlerFunc { blockSource, errUpdate := b.blocklists.UpdateCIDRBlockSource(ctx, sourceID, req.Name, req.URL, req.Enabled) if errUpdate != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) return } diff --git a/internal/chat/chat_service.go b/internal/chat/chat_service.go index 370ebc74..06089094 100644 --- a/internal/chat/chat_service.go +++ b/internal/chat/chat_service.go @@ -44,7 +44,7 @@ func (h chatHandler) onAPIQueryMessages() gin.HandlerFunc { if errChat != nil && !errors.Is(errChat, domain.ErrNoResult) { slog.Error("Failed to query messages history", log.ErrAttr(errChat), slog.String("sid", req.SourceID)) - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) return } @@ -57,7 +57,7 @@ func (h chatHandler) onAPIQueryMessageContext() gin.HandlerFunc { return func(ctx *gin.Context) { messageID, errMessageID := httphelper.GetInt64Param(ctx, "person_message_id") if errMessageID != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) slog.Debug("Got invalid person_message_id", log.ErrAttr(errMessageID)) return @@ -65,7 +65,7 @@ func (h chatHandler) onAPIQueryMessageContext() gin.HandlerFunc { padding, errPadding := httphelper.GetIntParam(ctx, "padding") if errPadding != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) slog.Debug("Got invalid padding", log.ErrAttr(errPadding)) return @@ -73,7 +73,7 @@ func (h chatHandler) onAPIQueryMessageContext() gin.HandlerFunc { messages, errQuery := h.chat.GetPersonMessageContext(ctx, messageID, padding) if errQuery != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) return } diff --git a/internal/contest/contest_service.go b/internal/contest/contest_service.go index cbf6a143..71c3017b 100644 --- a/internal/contest/contest_service.go +++ b/internal/contest/contest_service.go @@ -76,7 +76,7 @@ func (c *contestHandler) contestFromCtx(ctx *gin.Context) (domain.Contest, bool) } if !contest.Public && httphelper.CurrentUserProfile(ctx).PermissionLevel < domain.PModerator { - httphelper.ResponseErr(ctx, http.StatusForbidden, domain.ErrNotFound) + httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrNotFound) return domain.Contest{}, false } @@ -165,7 +165,7 @@ func (c *contestHandler) onAPIDeleteContest() gin.HandlerFunc { if errContest := c.contests.ContestByID(ctx, contestID, &contest); errContest != nil { if errors.Is(errContest, domain.ErrNoResult) { - httphelper.ResponseErr(ctx, http.StatusNotFound, domain.ErrUnknownID) + httphelper.ResponseApiErr(ctx, http.StatusNotFound, domain.ErrUnknownID) return } @@ -240,7 +240,7 @@ func (c *contestHandler) onAPISaveContestEntryMedia() gin.HandlerFunc { } if !slices.Contains(strings.Split(strings.ToLower(contest.MediaTypes), ","), strings.ToLower(mimeType.String())) { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrMimeTypeNotAllowed) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrMimeTypeNotAllowed) slog.Warn("User tried to upload file with disallowed mime type", slog.String("mime", strings.ToLower(mimeType.String()))) return @@ -331,7 +331,7 @@ func (c *contestHandler) onAPISaveContestEntrySubmit() gin.HandlerFunc { existingEntries, errEntries := c.contests.ContestEntries(ctx, contest.ContestID) if errEntries != nil && !errors.Is(errEntries, domain.ErrNoResult) { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrContestLoadEntries) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrContestLoadEntries) return } @@ -344,7 +344,7 @@ func (c *contestHandler) onAPISaveContestEntrySubmit() gin.HandlerFunc { } if own >= contest.MaxSubmissions { - httphelper.ResponseErr(ctx, http.StatusForbidden, domain.ErrContestMaxEntries) + httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrContestMaxEntries) return } @@ -354,26 +354,26 @@ func (c *contestHandler) onAPISaveContestEntrySubmit() gin.HandlerFunc { asset, _, errAsset := c.assets.Get(ctx, req.AssetID) if errAsset != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrEntryCreate) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrEntryCreate) return } if asset.AuthorID != steamID { - httphelper.ResponseErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied) + httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied) return } entry, errEntry := contest.NewEntry(steamID, req.AssetID, req.Description) if errEntry != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrEntryCreate) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrEntryCreate) return } if errSave := c.contests.ContestEntrySave(ctx, entry); errSave != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrEntrySave) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrEntrySave) return } @@ -390,7 +390,7 @@ func (c *contestHandler) onAPIDeleteContestEntry() gin.HandlerFunc { contestEntryID, idErr := httphelper.GetUUIDParam(ctx, "contest_entry_id") if idErr != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) return } @@ -399,12 +399,12 @@ func (c *contestHandler) onAPIDeleteContestEntry() gin.HandlerFunc { if errContest := c.contests.ContestEntry(ctx, contestEntryID, &entry); errContest != nil { if errors.Is(errContest, domain.ErrNoResult) { - httphelper.ResponseErr(ctx, http.StatusNotFound, domain.ErrUnknownID) + httphelper.ResponseApiErr(ctx, http.StatusNotFound, domain.ErrUnknownID) return } - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) slog.Error("Error getting contest entry for deletion", log.ErrAttr(errContest)) @@ -413,7 +413,7 @@ func (c *contestHandler) onAPIDeleteContestEntry() gin.HandlerFunc { // Only >=moderators or the entry author are allowed to delete entries. if !(user.PermissionLevel >= domain.PModerator || user.SteamID == entry.SteamID) { - httphelper.ResponseErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied) + httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied) return } @@ -422,12 +422,12 @@ func (c *contestHandler) onAPIDeleteContestEntry() gin.HandlerFunc { if errContest := c.contests.ContestByID(ctx, entry.ContestID, &contest); errContest != nil { if errors.Is(errContest, domain.ErrNoResult) { - httphelper.ResponseErr(ctx, http.StatusNotFound, domain.ErrUnknownID) + httphelper.ResponseApiErr(ctx, http.StatusNotFound, domain.ErrUnknownID) return } - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) slog.Error("Error getting contest", log.ErrAttr(errContest)) @@ -436,7 +436,7 @@ func (c *contestHandler) onAPIDeleteContestEntry() gin.HandlerFunc { // Only allow mods to delete entries from expired contests. if user.SteamID == entry.SteamID && time.Since(contest.DateEnd) > 0 { - httphelper.ResponseErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied) + httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied) slog.Error("User tried to delete entry from expired contest") @@ -444,7 +444,7 @@ func (c *contestHandler) onAPIDeleteContestEntry() gin.HandlerFunc { } if errDelete := c.contests.ContestEntryDelete(ctx, entry.ContestEntryID); errDelete != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Error deleting contest entry", log.ErrAttr(errDelete)) diff --git a/internal/demo/demo_service.go b/internal/demo/demo_service.go index 6ed33c7e..16b9a88f 100644 --- a/internal/demo/demo_service.go +++ b/internal/demo/demo_service.go @@ -35,7 +35,7 @@ func (h demoHandler) onAPIPostDemosQuery() gin.HandlerFunc { return func(ctx *gin.Context) { demos, errDemos := h.demos.GetDemos(ctx) if errDemos != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Failed to query demos", log.ErrAttr(errDemos)) return diff --git a/internal/discord/discord_service_oauth.go b/internal/discord/discord_service_oauth.go index d381a26b..93c687e3 100644 --- a/internal/discord/discord_service_oauth.go +++ b/internal/discord/discord_service_oauth.go @@ -47,7 +47,7 @@ func (h discordOAuthHandler) onLogin() gin.HandlerFunc { loginURL, errURL := h.discord.CreateStatefulLoginURL(currentUser.SteamID) if errURL != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, nil) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, nil) slog.Error("Failed to get state from query") return @@ -91,12 +91,12 @@ func (h discordOAuthHandler) onGetDiscordUser() gin.HandlerFunc { discord, errUser := h.discord.GetUserDetail(ctx, user.SteamID) if errUser != nil { if errors.Is(errUser, domain.ErrNoResult) { - httphelper.ResponseErr(ctx, http.StatusNotFound, domain.ErrNotFound) + httphelper.ResponseApiErr(ctx, http.StatusNotFound, domain.ErrNotFound) return } - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Error trying to fetch discord user details", log.ErrAttr(errUser)) return @@ -118,7 +118,7 @@ func (h discordOAuthHandler) onLogout() gin.HandlerFunc { return } - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Error trying to logout discord user", log.ErrAttr(errUser)) return diff --git a/internal/forum/forum_service.go b/internal/forum/forum_service.go index 0da964d4..c9eb8a9b 100644 --- a/internal/forum/forum_service.go +++ b/internal/forum/forum_service.go @@ -278,13 +278,13 @@ func (f *forumHandler) onAPIThreadCreate() gin.HandlerFunc { } if len(req.BodyMD) <= 1 { - httphelper.ResponseErr(ctx, http.StatusBadRequest, fmt.Errorf("body: %w", domain.ErrTooShort)) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, fmt.Errorf("body: %w", domain.ErrTooShort)) return } if len(req.Title) <= 4 { - httphelper.ResponseErr(ctx, http.StatusBadRequest, fmt.Errorf("title: %w", domain.ErrTooShort)) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, fmt.Errorf("title: %w", domain.ErrTooShort)) return } @@ -317,7 +317,7 @@ func (f *forumHandler) onAPIThreadCreate() gin.HandlerFunc { slog.Error("Failed to rollback new thread", log.ErrAttr(errRollback)) } - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Failed to save new forum message", log.ErrAttr(errSaveMessage)) @@ -325,7 +325,7 @@ func (f *forumHandler) onAPIThreadCreate() gin.HandlerFunc { } if errIncr := f.forums.ForumIncrMessageCount(ctx, forum.ForumID, true); errIncr != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Failed to increment message count", log.ErrAttr(errIncr)) @@ -373,7 +373,7 @@ func (f *forumHandler) onAPIThreadUpdate() gin.HandlerFunc { var thread domain.ForumThread if errGet := f.forums.ForumThread(ctx, forumThreadID, &thread); errGet != nil { if errors.Is(errGet, domain.ErrNoResult) { - httphelper.ResponseErr(ctx, http.StatusNotFound, domain.ErrNotFound) + httphelper.ResponseApiErr(ctx, http.StatusNotFound, domain.ErrNotFound) slog.Warn("Requested unknown forum thread", log.ErrAttr(errGet)) } else { httphelper.HandleErrInternal(ctx) @@ -394,7 +394,7 @@ func (f *forumHandler) onAPIThreadUpdate() gin.HandlerFunc { thread.Locked = req.Locked if errDelete := f.forums.ForumThreadSave(ctx, &thread); errDelete != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Failed to update thread", log.ErrAttr(errDelete)) return @@ -467,7 +467,7 @@ func (f *forumHandler) onAPIThreadMessageUpdate() gin.HandlerFunc { forumMessageID, errForumMessageID := httphelper.GetInt64Param(ctx, "forum_message_id") if errForumMessageID != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) slog.Warn("Failed to get forum_message_id", log.ErrAttr(errForumMessageID)) return @@ -550,7 +550,7 @@ func (f *forumHandler) onAPIMessageDelete() gin.HandlerFunc { } if thread.Locked { - httphelper.ResponseErr(ctx, http.StatusForbidden, domain.ErrThreadLocked) + httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrThreadLocked) return } @@ -632,7 +632,7 @@ func (f *forumHandler) onAPIThreadCreateReply() gin.HandlerFunc { } if thread.Locked && currentUser.PermissionLevel < domain.PEditor { - httphelper.ResponseErr(ctx, http.StatusForbidden, domain.ErrThreadLocked) + httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrThreadLocked) return } @@ -645,7 +645,7 @@ func (f *forumHandler) onAPIThreadCreateReply() gin.HandlerFunc { req.BodyMD = stringutil.SanitizeUGC(req.BodyMD) if len(req.BodyMD) < 3 { - httphelper.ResponseErr(ctx, http.StatusBadRequest, fmt.Errorf("body: %w", domain.ErrTooShort)) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, fmt.Errorf("body: %w", domain.ErrTooShort)) return } @@ -700,7 +700,7 @@ func (f *forumHandler) onAPIForumOverview() gin.HandlerFunc { currentForums, errForums := f.forums.Forums(ctx) if errForums != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Could not load forums", log.ErrAttr(errForums)) return @@ -787,7 +787,7 @@ func (f *forumHandler) onAPIForumThread() gin.HandlerFunc { if errors.Is(errThreads, domain.ErrNoResult) { httphelper.HandleErrNotFound(ctx) } else { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Could not load threads", log.ErrAttr(errThreads), slog.Int64("forum_thread_id", forumThreadID)) } diff --git a/internal/httphelper/helper.go b/internal/httphelper/helper.go index 12d24283..dda8b111 100644 --- a/internal/httphelper/helper.go +++ b/internal/httphelper/helper.go @@ -21,7 +21,7 @@ type APIError struct { Message string `json:"message"` } -func ResponseErr(ctx *gin.Context, statusCode int, err error) { +func ResponseApiErr(ctx *gin.Context, statusCode int, err error) { userErr := "API Error" if err != nil { userErr = err.Error() @@ -47,7 +47,7 @@ var decoder = schema.NewDecoder() //nolint:gochecknoglobals func BindQuery(ctx *gin.Context, target any) bool { if errBind := decoder.Decode(target, ctx.Request.URL.Query()); errBind != nil { - ResponseErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) + ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) slog.Error("Failed to bind query request", log.ErrAttr(errBind), log.HandlerName(3)) return false diff --git a/internal/httphelper/http.go b/internal/httphelper/http.go index 1ce2c670..e51c57f8 100644 --- a/internal/httphelper/http.go +++ b/internal/httphelper/http.go @@ -103,27 +103,27 @@ func HandleErrs(ctx *gin.Context, err error) { } func HandleErrPermissionDenied(ctx *gin.Context) { - ResponseErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied) + ResponseApiErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied) } func HandleErrNotFound(ctx *gin.Context) { - ResponseErr(ctx, http.StatusNotFound, domain.ErrNotFound) + ResponseApiErr(ctx, http.StatusNotFound, domain.ErrNotFound) } func HandleErrBadRequest(ctx *gin.Context) { - ResponseErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) + ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest) } func HandleErrInternal(ctx *gin.Context) { - ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) } func HandleErrDuplicate(ctx *gin.Context) { - ResponseErr(ctx, http.StatusConflict, domain.ErrDuplicate) + ResponseApiErr(ctx, http.StatusConflict, domain.ErrDuplicate) } func HandleErrInvalidFormat(ctx *gin.Context) { - ResponseErr(ctx, http.StatusUnsupportedMediaType, domain.ErrInvalidFormat) + ResponseApiErr(ctx, http.StatusUnsupportedMediaType, domain.ErrInvalidFormat) } func useSentry(engine *gin.Engine, version string) { diff --git a/internal/match/match_service.go b/internal/match/match_service.go index e582b7b6..c3d0129b 100644 --- a/internal/match/match_service.go +++ b/internal/match/match_service.go @@ -62,7 +62,7 @@ func (h matchHandler) onAPIPostMatchEnd() gin.HandlerFunc { matchUUID, errEnd := h.matches.EndMatch(ctx, serverID) if errEnd != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrUnknownServerID) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrUnknownServerID) slog.Error("Failed to end match", log.ErrAttr(errEnd)) return @@ -90,7 +90,7 @@ func (h matchHandler) onAPIPostMatchStart() gin.HandlerFunc { serverID, errServerID := httphelper.GetIntParam(ctx, "server_id") if errServerID != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrUnknownServerID) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrUnknownServerID) slog.Warn("Failed to get server_id", log.ErrAttr(errServerID)) return @@ -98,7 +98,7 @@ func (h matchHandler) onAPIPostMatchStart() gin.HandlerFunc { server, errServer := h.servers.Server(ctx, serverID) if errServer != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrUnknownServerID) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrUnknownServerID) slog.Error("Failed to get server", log.ErrAttr(errServer)) return @@ -106,7 +106,7 @@ func (h matchHandler) onAPIPostMatchStart() gin.HandlerFunc { matchUUID, errMatch := h.matches.StartMatch(server, req.MapName, req.DemoName) if errMatch != nil { - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrUnknownServerID) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrUnknownServerID) slog.Error("Failed to start match", log.ErrAttr(errMatch)) return @@ -148,7 +148,7 @@ func (h matchHandler) onAPIGetsStatsWeapon() gin.HandlerFunc { return func(ctx *gin.Context) { weaponID, errWeaponID := httphelper.GetIntParam(ctx, "weapon_id") if errWeaponID != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) slog.Warn("Failed to get weapon_id", log.ErrAttr(errWeaponID)) return @@ -307,7 +307,7 @@ func (h matchHandler) onAPIGetMatch() gin.HandlerFunc { return func(ctx *gin.Context) { matchID, errID := httphelper.GetUUIDParam(ctx, "match_id") if errID != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) slog.Warn("Failed to get match_id", log.ErrAttr(errID)) return @@ -319,7 +319,7 @@ func (h matchHandler) onAPIGetMatch() gin.HandlerFunc { if errMatch != nil { if errors.Is(errMatch, domain.ErrNoResult) { - httphelper.ResponseErr(ctx, http.StatusNotFound, domain.ErrNotFound) + httphelper.ResponseApiErr(ctx, http.StatusNotFound, domain.ErrNotFound) return } diff --git a/internal/network/network_service.go b/internal/network/network_service.go index 768616c6..a3619281 100644 --- a/internal/network/network_service.go +++ b/internal/network/network_service.go @@ -65,7 +65,7 @@ func (h networkHandler) onAPIQueryNetwork() gin.HandlerFunc { details, err := h.networks.QueryNetwork(ctx, req.IP) if err != nil { slog.Error("Failed to query connection history", log.ErrAttr(err)) - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) return } diff --git a/internal/patreon/patreon_service.go b/internal/patreon/patreon_service.go index 71f9e519..d11b97c2 100644 --- a/internal/patreon/patreon_service.go +++ b/internal/patreon/patreon_service.go @@ -44,7 +44,7 @@ func (h patreonHandler) onLogout() gin.HandlerFunc { currentUser := httphelper.CurrentUserProfile(ctx) if err := h.patreon.Forget(ctx, currentUser.SteamID); err != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, nil) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, nil) return } @@ -67,14 +67,14 @@ func (h patreonHandler) onOAuth() gin.HandlerFunc { return func(ctx *gin.Context) { grantCode, codeOK := ctx.GetQuery("code") if !codeOK { - httphelper.ResponseErr(ctx, http.StatusBadRequest, nil) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, nil) return } state, stateOK := ctx.GetQuery("state") if !stateOK { - httphelper.ResponseErr(ctx, http.StatusBadRequest, nil) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, nil) return } diff --git a/internal/report/report_service.go b/internal/report/report_service.go index 987e50e3..4c44c3f5 100644 --- a/internal/report/report_service.go +++ b/internal/report/report_service.go @@ -1,6 +1,7 @@ package report import ( + "errors" "log/slog" "net/http" @@ -57,6 +58,12 @@ func (h reportHandler) onAPIPostReportCreate() gin.HandlerFunc { report, errReportSave := h.reports.SaveReport(ctx, currentUser, req) if errReportSave != nil { + if errors.Is(errReportSave, domain.ErrReportExists) { + httphelper.ResponseApiErr(ctx, http.StatusConflict, domain.ErrReportExists) + + return + } + httphelper.HandleErrs(ctx, errReportSave) slog.Error("Failed to save report", log.ErrAttr(errReportSave)) diff --git a/internal/srcds/srcds_service.go b/internal/srcds/srcds_service.go index aaeae19a..66da5245 100644 --- a/internal/srcds/srcds_service.go +++ b/internal/srcds/srcds_service.go @@ -398,7 +398,7 @@ func (s *srcdsHandler) onDeleteGroupOverride() gin.HandlerFunc { if err := s.srcds.DelGroupOverride(ctx, groupOverrideID); err != nil { if errors.Is(err, domain.ErrNoResult) { - httphelper.ResponseErr(ctx, http.StatusNotFound, domain.ErrNotFound) + httphelper.ResponseApiErr(ctx, http.StatusNotFound, domain.ErrNotFound) return } @@ -862,12 +862,12 @@ func (s *srcdsHandler) onAPIPostBanSteamCreate() gin.HandlerFunc { ban, errBan := s.bans.Ban(ctx, httphelper.CurrentUserProfile(ctx), domain.InGame, req) if errBan != nil { if errors.Is(errBan, domain.ErrDuplicate) { - httphelper.ResponseErr(ctx, http.StatusConflict, domain.ErrDuplicate) + httphelper.ResponseApiErr(ctx, http.StatusConflict, domain.ErrDuplicate) return } - httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrInternal) + httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal) slog.Error("Failed to save new steam ban", log.ErrAttr(errBan)) return diff --git a/internal/steamgroup/steamgroup_service.go b/internal/steamgroup/steamgroup_service.go index 5422fca1..20501c67 100644 --- a/internal/steamgroup/steamgroup_service.go +++ b/internal/steamgroup/steamgroup_service.go @@ -102,7 +102,7 @@ func (h steamgroupHandler) onAPIPostBansGroupUpdate() gin.HandlerFunc { return func(ctx *gin.Context) { banGroupID, banIDErr := httphelper.GetInt64Param(ctx, "ban_group_id") if banIDErr != nil { - httphelper.ResponseErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) + httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter) slog.Warn("Failed to get ban_group_id", log.ErrAttr(banIDErr)) return From 26538e3118c1efd3b7bae498a86ac3d6c29c811e Mon Sep 17 00:00:00 2001 From: Leigh MacDonald Date: Mon, 24 Jun 2024 00:26:33 -0600 Subject: [PATCH 02/14] Add error/success messages to reports. --- frontend/src/routes/_auth.report.index.tsx | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/frontend/src/routes/_auth.report.index.tsx b/frontend/src/routes/_auth.report.index.tsx index 60514bd2..56bc9d36 100644 --- a/frontend/src/routes/_auth.report.index.tsx +++ b/frontend/src/routes/_auth.report.index.tsx @@ -41,7 +41,6 @@ import { banReasonsCollection, CreateReportRequest, PlayerProfile, - ReportStatus, reportStatusString, ReportWithAuthor } from '../api'; @@ -58,6 +57,7 @@ import { Title } from '../component/Title'; import { Buttons } from '../component/field/Buttons.tsx'; import { MarkdownField, mdEditorRef } from '../component/field/MarkdownField.tsx'; import { SteamIDField } from '../component/field/SteamIDField.tsx'; +import { useUserFlashCtx } from '../hooks/useUserFlashCtx.ts'; import { commonTableSearchSchema, initPagination, RowsPerPage } from '../util/table.ts'; import { makeSteamidValidators } from '../util/validator/makeSteamidValidators.ts'; @@ -65,7 +65,6 @@ const reportSchema = z.object({ ...commonTableSearchSchema, rows: z.number().optional(), sortColumn: z.enum(['report_status', 'created_on']).optional(), - report_status: z.nativeEnum(ReportStatus).optional(), steam_id: z.string().optional(), demo_id: z.number({ coerce: true }).optional(), person_message_id: z.number().optional() @@ -262,6 +261,7 @@ const validationSchema = z.object({ export const ReportCreateForm = (): JSX.Element => { const { demo_id, steam_id, person_message_id } = Route.useSearch(); const [validatedProfile, setValidatedProfile] = useState(); + const { sendFlash } = useUserFlashCtx(); const mutation = useMutation({ mutationFn: async (variables: CreateReportRequest) => { @@ -270,6 +270,10 @@ export const ReportCreateForm = (): JSX.Element => { onSuccess: async (data) => { mdEditorRef.current?.setMarkdown(''); await navigate({ to: '/report/$reportId', params: { reportId: String(data.report_id) } }); + sendFlash('success', 'Created report successfully'); + }, + onError: (error) => { + sendFlash('error', `Error trying to create report: ${error.message}`); } }); @@ -407,6 +411,7 @@ export const ReportCreateForm = (): JSX.Element => { { return ( { { return ; }} From bb664ebf142461bfdedf6938cf318309d6224e95 Mon Sep 17 00:00:00 2001 From: Leigh MacDonald Date: Mon, 24 Jun 2024 00:37:26 -0600 Subject: [PATCH 03/14] Update error message --- internal/domain/errors.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/domain/errors.go b/internal/domain/errors.go index 7758915e..ccb201d0 100644 --- a/internal/domain/errors.go +++ b/internal/domain/errors.go @@ -77,7 +77,7 @@ var ( ErrSelfReport = errors.New("cannot self report") ErrUUIDCreate = errors.New("failed to generate new uuid") ErrUUIDInvalid = errors.New("invalid uuid") - ErrReportExists = errors.New("cannot create report while existing report open") + ErrReportExists = errors.New("cannot create report while existing report open against player") ErrInvalidFormat = errors.New("invalid format") ErrEmptyToken = errors.New("invalid Access token decoded") ErrContestLoadEntries = errors.New("failed to load existing contest entries") From 44cab9b1d355fbc2c6209dd99adec3be8433f044 Mon Sep 17 00:00:00 2001 From: Leigh MacDonald Date: Mon, 24 Jun 2024 00:53:12 -0600 Subject: [PATCH 04/14] Fix logout error --- frontend/src/routes/_guest.index.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/src/routes/_guest.index.tsx b/frontend/src/routes/_guest.index.tsx index 2e5065dd..f18476ff 100644 --- a/frontend/src/routes/_guest.index.tsx +++ b/frontend/src/routes/_guest.index.tsx @@ -36,7 +36,7 @@ function Index() { - {profile.ban_id == 0 && appInfo.servers_enabled && ( + {profile && profile.ban_id == 0 && appInfo.servers_enabled && ( )} - {profile.ban_id != 0 && appInfo.reports_enabled && ( + {profile && profile.ban_id != 0 && appInfo.reports_enabled && (