Skip to content

Commit 0f363ea

Browse files
Fix sourcebans single user query.
1 parent 013213b commit 0f363ea

File tree

8 files changed

+52
-49
lines changed

8 files changed

+52
-49
lines changed

frontend/src/component/ReportViewComponent.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { zodValidator } from '@tanstack/zod-form-adapter';
2525
import { z } from 'zod';
2626
import {
2727
apiCreateReportMessage,
28-
apiGetBansSteam,
28+
apiGetBansSteamBySteamID,
2929
apiGetConnections,
3030
apiGetMessages,
3131
PermissionLevel,
@@ -99,7 +99,7 @@ export const ReportViewComponent = ({ report }: { report: ReportWithAuthor }): J
9999
const { data: bans, isLoading: isLoadingBans } = useQuery({
100100
queryKey: ['reportBanHistory', { steamId: report.target_id }],
101101
queryFn: async () => {
102-
const bans = await apiGetBansSteam({ target_id: report.target_id });
102+
const bans = await apiGetBansSteamBySteamID(report.target_id);
103103

104104
return bans.filter((b) => b.target_id == report.target_id);
105105
}

frontend/src/component/WikiPage.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const WikiPage = ({ slug = 'home', path }: { slug: string; path: '/_guest
4343
variant={'contained'}
4444
color={'warning'}
4545
onClick={() => {
46-
setEditMode((prev) => !prev);
46+
setEditMode(true);
4747
}}
4848
>
4949
Edit
@@ -88,7 +88,6 @@ export const WikiPage = ({ slug = 'home', path }: { slug: string; path: '/_guest
8888
});
8989

9090
if (editMode) {
91-
console.log(page);
9291
return (
9392
<ContainerWithHeaderAndButtons title={`Editing: ${slug}`} iconLeft={<EditIcon />}>
9493
<form
@@ -128,9 +127,6 @@ export const WikiPage = ({ slug = 'home', path }: { slug: string; path: '/_guest
128127
<Grid xs={12}>
129128
<Field
130129
name={'body_md'}
131-
validators={{
132-
onChange: z.string().min(5)
133-
}}
134130
children={(props) => {
135131
return <MarkdownField {...props} label={'Region'} />;
136132
}}

internal/ban/background.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ func Start(ctx context.Context, bansSteam domain.BanSteamUsecase, bansNet domain
7373
for _, expiredNetBan := range expiredNetBans {
7474
expiredBan := expiredNetBan
7575
if errDropBanNet := bansNet.Delete(ctx, expiredNetBan.NetID, domain.RequestUnban{UnbanReasonText: "Expired"}, false); errDropBanNet != nil {
76-
slog.Error("Failed to drop expired network expiredNetBan", log.ErrAttr(errDropBanNet))
76+
if !errors.Is(errDropBanNet, domain.ErrNoResult) {
77+
slog.Error("Failed to drop expired network expiredNetBan", log.ErrAttr(errDropBanNet))
78+
}
7779
} else {
7880
slog.Info("IP ban expired", slog.String("cidr", expiredBan.String()), slog.Int64("net_id", expiredNetBan.NetID))
7981
}

internal/ban/ban_steam_repository.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ func (r banSteamRepository) getBanByColumn(ctx context.Context, column string, i
9797

9898
whereClauses := sq.And{
9999
sq.Eq{"b." + column: identifier}, // valid columns are immutable
100-
sq.Gt{"b.valid_until": time.Now()},
101100
}
102101

103102
if !deletedOk {

internal/ban/ban_steam_service.go

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (h banHandler) onAPIPostSetBanAppealStatus() gin.HandlerFunc {
7070
return func(ctx *gin.Context) {
7171
banID, banIDErr := httphelper.GetInt64Param(ctx, "ban_id")
7272
if banIDErr != nil {
73-
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
73+
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
7474

7575
return
7676
}
@@ -82,13 +82,13 @@ func (h banHandler) onAPIPostSetBanAppealStatus() gin.HandlerFunc {
8282

8383
bannedPerson, banErr := h.bansSteam.GetByBanID(ctx, banID, false, true)
8484
if banErr != nil {
85-
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
85+
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
8686

8787
return
8888
}
8989

9090
if bannedPerson.AppealState == req.AppealState {
91-
httphelper.ResponseApiErr(ctx, http.StatusConflict, domain.ErrStateUnchanged)
91+
httphelper.ResponseAPIErr(ctx, http.StatusConflict, domain.ErrStateUnchanged)
9292

9393
return
9494
}
@@ -97,7 +97,7 @@ func (h banHandler) onAPIPostSetBanAppealStatus() gin.HandlerFunc {
9797
bannedPerson.AppealState = req.AppealState
9898

9999
if errSave := h.bansSteam.Save(ctx, &bannedPerson.BanSteam); errSave != nil {
100-
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
100+
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
101101

102102
return
103103
}
@@ -137,7 +137,7 @@ func (h banHandler) onAPIGetBanByID() gin.HandlerFunc {
137137

138138
banID, errID := httphelper.GetInt64Param(ctx, "ban_id")
139139
if errID != nil || banID == 0 {
140-
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
140+
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
141141

142142
return
143143
}
@@ -180,7 +180,7 @@ func (h banHandler) onAPIGetSourceBans() gin.HandlerFunc {
180180
return func(ctx *gin.Context) {
181181
steamID, errID := httphelper.GetSID64Param(ctx, "steam_id")
182182
if errID != nil {
183-
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
183+
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
184184

185185
return
186186
}
@@ -193,7 +193,14 @@ func (h banHandler) onAPIGetSourceBans() gin.HandlerFunc {
193193
return
194194
}
195195

196-
ctx.JSON(http.StatusOK, records)
196+
userRecords, found := records[steamID.Int64()]
197+
if !found {
198+
ctx.JSON(http.StatusOK, []thirdparty.BDSourceBansRecord{})
199+
200+
return
201+
}
202+
203+
ctx.JSON(http.StatusOK, userRecords)
197204
}
198205
}
199206

@@ -220,7 +227,7 @@ func (h banHandler) onAPIExportBansValveSteamID() gin.HandlerFunc {
220227
if len(authorizedKeys) > 0 {
221228
key, ok := ctx.GetQuery("key")
222229
if !ok || !slices.Contains(authorizedKeys, key) {
223-
httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied)
230+
httphelper.ResponseAPIErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied)
224231

225232
return
226233
}
@@ -229,7 +236,7 @@ func (h banHandler) onAPIExportBansValveSteamID() gin.HandlerFunc {
229236
// TODO limit to perm?
230237
bans, errBans := h.bansSteam.Get(ctx, domain.SteamBansQueryFilter{})
231238
if errBans != nil {
232-
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
239+
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
233240

234241
return
235242
}
@@ -256,7 +263,7 @@ func (h banHandler) onAPIExportBansTF2BD() gin.HandlerFunc {
256263
if len(authorizedKeys) > 0 {
257264
key, ok := ctx.GetQuery("key")
258265
if !ok || !slices.Contains(authorizedKeys, key) {
259-
httphelper.ResponseApiErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied)
266+
httphelper.ResponseAPIErr(ctx, http.StatusForbidden, domain.ErrPermissionDenied)
260267

261268
return
262269
}
@@ -265,7 +272,7 @@ func (h banHandler) onAPIExportBansTF2BD() gin.HandlerFunc {
265272
bans, errBans := h.bansSteam.Get(ctx, domain.SteamBansQueryFilter{})
266273

267274
if errBans != nil {
268-
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
275+
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
269276

270277
return
271278
}
@@ -319,7 +326,7 @@ func (h banHandler) onAPIGetBansSteam() gin.HandlerFunc {
319326

320327
bans, errBans := h.bansSteam.Get(ctx, params)
321328
if errBans != nil {
322-
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
329+
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
323330
slog.Error("Failed to fetch steam bans", log.ErrAttr(errBans))
324331

325332
return
@@ -345,7 +352,7 @@ func (h banHandler) onAPIGetBansSteamBySteamID() gin.HandlerFunc {
345352
}
346353
bans, errBans := h.bansSteam.Get(ctx, params)
347354
if errBans != nil {
348-
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
355+
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
349356
slog.Error("Failed to fetch steam bans", log.ErrAttr(errBans))
350357

351358
return
@@ -359,7 +366,7 @@ func (h banHandler) onAPIPostBanDelete() gin.HandlerFunc {
359366
return func(ctx *gin.Context) {
360367
banID, banIDErr := httphelper.GetInt64Param(ctx, "ban_id")
361368
if banIDErr != nil {
362-
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
369+
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
363370

364371
return
365372
}
@@ -390,7 +397,7 @@ func (h banHandler) onAPIPostBanDelete() gin.HandlerFunc {
390397
}
391398

392399
if !changed {
393-
httphelper.ResponseApiErr(ctx, http.StatusOK, domain.ErrUnbanFailed)
400+
httphelper.ResponseAPIErr(ctx, http.StatusOK, domain.ErrUnbanFailed)
394401

395402
return
396403
}
@@ -414,7 +421,7 @@ func (h banHandler) onAPIPostBanUpdate() gin.HandlerFunc {
414421
return func(ctx *gin.Context) {
415422
banID, banIDErr := httphelper.GetInt64Param(ctx, "ban_id")
416423
if banIDErr != nil {
417-
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
424+
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrInvalidParameter)
418425

419426
return
420427
}
@@ -425,21 +432,21 @@ func (h banHandler) onAPIPostBanUpdate() gin.HandlerFunc {
425432
}
426433

427434
if time.Since(req.ValidUntil) > 0 {
428-
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)
435+
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)
429436

430437
return
431438
}
432439

433440
bannedPerson, banErr := h.bansSteam.GetByBanID(ctx, banID, false, true)
434441
if banErr != nil {
435-
httphelper.ResponseApiErr(ctx, http.StatusNotFound, domain.ErrNotFound)
442+
httphelper.ResponseAPIErr(ctx, http.StatusNotFound, domain.ErrNotFound)
436443

437444
return
438445
}
439446

440447
if req.Reason == domain.Custom {
441448
if req.ReasonText == "" {
442-
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)
449+
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)
443450

444451
return
445452
}
@@ -457,7 +464,7 @@ func (h banHandler) onAPIPostBanUpdate() gin.HandlerFunc {
457464
bannedPerson.ValidUntil = req.ValidUntil
458465

459466
if errSave := h.bansSteam.Save(ctx, &bannedPerson.BanSteam); errSave != nil {
460-
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
467+
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
461468
slog.Error("Failed to save updated ban", log.ErrAttr(errSave))
462469

463470
return
@@ -471,15 +478,15 @@ func (h banHandler) onAPIGetBanBySteam() gin.HandlerFunc {
471478
return func(ctx *gin.Context) {
472479
steamID, err := httphelper.GetSID64Param(ctx, "steam_id")
473480
if err != nil {
474-
httphelper.ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)
481+
httphelper.ResponseAPIErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)
475482
slog.Error("Failed to get steamid", log.ErrAttr(err))
476483

477484
return
478485
}
479486

480487
ban, errBans := h.bansSteam.GetBySteamID(ctx, steamID, false, false)
481488
if errBans != nil && !errors.Is(errBans, domain.ErrNoResult) {
482-
httphelper.ResponseApiErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
489+
httphelper.ResponseAPIErr(ctx, http.StatusInternalServerError, domain.ErrInternal)
483490
slog.Error("Failed to get ban record for steamid", log.ErrAttr(errBans))
484491

485492
return

internal/domain/query_filter.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ type GroupBansQueryFilter struct {
189189
}
190190

191191
type SteamBansQueryFilter struct {
192+
TargetIDField
192193
Deleted bool `schema:"deleted"`
193194
}
194195

@@ -202,7 +203,7 @@ type AppealQueryFilter struct {
202203
}
203204

204205
type SteamIDField struct {
205-
SteamIDValue string `json:"steam_id"` //nolint:tagliatelle
206+
SteamIDValue string `json:"steam_id" url:"steam_id"` //nolint:tagliatelle
206207
}
207208

208209
func (f SteamIDField) SteamID(ctx context.Context) (steamid.SteamID, bool) {
@@ -219,7 +220,7 @@ func (f SteamIDField) SteamID(ctx context.Context) (steamid.SteamID, bool) {
219220
}
220221

221222
type SourceIDField struct {
222-
SourceID string `json:"source_id"`
223+
SourceID string `json:"source_id" url:"source_id"`
223224
}
224225

225226
func (f SourceIDField) SourceSteamID(ctx context.Context) (steamid.SteamID, bool) {
@@ -236,7 +237,7 @@ func (f SourceIDField) SourceSteamID(ctx context.Context) (steamid.SteamID, bool
236237
}
237238

238239
type TargetIDField struct {
239-
TargetID string `json:"target_id"`
240+
TargetID string `json:"target_id" url:"target_id"`
240241
}
241242

242243
func (f TargetIDField) TargetSteamID(ctx context.Context) (steamid.SteamID, bool) {
@@ -253,7 +254,7 @@ func (f TargetIDField) TargetSteamID(ctx context.Context) (steamid.SteamID, bool
253254
}
254255

255256
type TargetGIDField struct {
256-
GroupID string `json:"group_id"`
257+
GroupID string `json:"group_id" url:"group_id"`
257258
}
258259

259260
func (f TargetGIDField) TargetGroupID(ctx context.Context) (steamid.SteamID, bool) {

internal/httphelper/helper.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type APIError struct {
2121
Message string `json:"message"`
2222
}
2323

24-
func ResponseApiErr(ctx *gin.Context, statusCode int, err error) {
24+
func ResponseAPIErr(ctx *gin.Context, statusCode int, err error) {
2525
userErr := "API Error"
2626
if err != nil {
2727
userErr = err.Error()
@@ -47,8 +47,8 @@ var decoder = schema.NewDecoder() //nolint:gochecknoglobals
4747

4848
func BindQuery(ctx *gin.Context, target any) bool {
4949
if errBind := decoder.Decode(target, ctx.Request.URL.Query()); errBind != nil {
50-
ResponseApiErr(ctx, http.StatusBadRequest, domain.ErrBadRequest)
51-
slog.Error("Failed to bind query request", log.ErrAttr(errBind), log.HandlerName(3))
50+
HandleErrs(ctx, domain.ErrBadRequest)
51+
slog.Error("Failed to bind query request", log.ErrAttr(errBind), log.HandlerName(2))
5252

5353
return false
5454
}

internal/thirdparty/bdapi.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import (
44
"context"
55
"encoding/json"
66
"errors"
7-
"fmt"
8-
"io"
9-
"log/slog"
107
"net/http"
8+
"net/url"
119
"time"
1210

1311
"github.com/leighmacdonald/gbans/internal/domain"
@@ -30,9 +28,13 @@ type BDSourceBansRecord struct {
3028

3129
func BDSourceBans(ctx context.Context, steamID steamid.SteamID) (map[int64][]BDSourceBansRecord, error) {
3230
client := &http.Client{Timeout: time.Second * 10}
33-
url := fmt.Sprintf(bdAPIURL, steamID.String())
31+
queryURL, _ := url.Parse(bdAPIURL)
32+
query := queryURL.Query()
33+
query.Set("steamids", steamID.String())
34+
queryURL.RawQuery = query.Encode()
35+
fullURL := queryURL.String()
3436

35-
req, errReq := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
37+
req, errReq := http.NewRequestWithContext(ctx, http.MethodGet, fullURL, nil)
3638
if errReq != nil {
3739
return nil, errors.Join(errReq, domain.ErrRequestCreate)
3840
}
@@ -48,13 +50,9 @@ func BDSourceBans(ctx context.Context, steamID steamid.SteamID) (map[int64][]BDS
4850
_ = resp.Body.Close()
4951
}()
5052

51-
body, _ := io.ReadAll(resp.Body)
53+
records := map[int64][]BDSourceBansRecord{}
5254

53-
slog.Info(string(body))
54-
55-
var records map[int64][]BDSourceBansRecord
56-
57-
if errJSON := json.Unmarshal(body, &records); errJSON != nil {
55+
if errJSON := json.NewDecoder(resp.Body).Decode(&records); errJSON != nil {
5856
return nil, errors.Join(errJSON, domain.ErrRequestDecode)
5957
}
6058

0 commit comments

Comments
 (0)