From 4f9f1e0d3fad7339e9d81a6968052743e29cbedb Mon Sep 17 00:00:00 2001 From: Eggbertx Date: Sun, 5 Jan 2025 16:01:17 -0800 Subject: [PATCH] Add migrating pre-2021 name/file bans to filters --- cmd/gochan-migration/internal/pre2021/bans.go | 62 +++++++++++++++++-- .../internal/pre2021/bans_test.go | 11 ++++ 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/cmd/gochan-migration/internal/pre2021/bans.go b/cmd/gochan-migration/internal/pre2021/bans.go index e00ccb63..1fbfdfdb 100644 --- a/cmd/gochan-migration/internal/pre2021/bans.go +++ b/cmd/gochan-migration/internal/pre2021/bans.go @@ -1,6 +1,7 @@ package pre2021 import ( + "context" "database/sql" "errors" "net" @@ -33,9 +34,7 @@ type migrationBan struct { canAppeal bool boardIDs []int - banID int staffID int - filterID int } func (m *Pre2021Migrator) migrateBansInPlace() error { @@ -110,20 +109,27 @@ func (m *Pre2021Migrator) migrateBansToNewDB() error { } } + migrationUser, err := m.getMigrationUser(errEv) + if err != nil { + return err + } ban.staffID, err = gcsql.GetStaffID(ban.staff) if errors.Is(err, gcsql.ErrUnrecognizedUsername) { // username not found after staff were migrated, use a stand-in account to be updated by the admin later - migrationUser, err := m.getMigrationUser(errEv) - if err != nil { - return err - } common.LogWarning(). Str("username", ban.staff). Str("migrationUser", migrationUser.Username). Msg("Ban staff not found in migrated staff table, using migration user instead") ban.staffID = migrationUser.ID + } else if err != nil { + errEv.Err(err).Caller().Str("username", ban.staff).Msg("Failed to get staff from username") + return err } + if ban.ip == "" && ban.name == "" && ban.fileChecksum == "" && ban.filename == "" { + common.LogWarning().Int("banID", ban.oldID).Msg("Found invalid ban (no IP, name, file checksum, or filename set)") + continue + } if ban.ip != "" { if net.ParseIP(ban.ip) == nil { gcutil.LogWarning(). @@ -144,6 +150,50 @@ func (m *Pre2021Migrator) migrateBansToNewDB() error { } } } + if ban.name != "" || ban.fileChecksum != "" || ban.filename != "" { + filter := &gcsql.Filter{ + StaffID: &ban.staffID, + StaffNote: ban.staffNote, + IsActive: true, + HandleIfAny: true, + MatchAction: "reject", + MatchDetail: ban.reason, + } + var conditions []gcsql.FilterCondition + if ban.name != "" { + nameCondition := gcsql.FilterCondition{ + Field: "name", + Search: ban.name, + MatchMode: gcsql.ExactMatch, + } + if ban.nameIsRegex { + nameCondition.MatchMode = gcsql.RegexMatch + } + conditions = append(conditions, nameCondition) + } + if ban.fileChecksum != "" { + conditions = append(conditions, gcsql.FilterCondition{ + Field: "checksum", + MatchMode: gcsql.ExactMatch, + Search: ban.fileChecksum, + }) + } + if ban.filename != "" { + filenameCondition := gcsql.FilterCondition{ + Field: "filename", + Search: ban.filename, + MatchMode: gcsql.ExactMatch, + } + if ban.nameIsRegex { + filenameCondition.MatchMode = gcsql.RegexMatch + } + conditions = append(conditions, filenameCondition) + } + if err = gcsql.ApplyFilterTx(context.Background(), tx, filter, conditions, ban.boardIDs); err != nil { + errEv.Err(err).Caller().Int("banID", ban.oldID).Msg("Failed to migrate ban to filter") + return err + } + } } return tx.Commit() diff --git a/cmd/gochan-migration/internal/pre2021/bans_test.go b/cmd/gochan-migration/internal/pre2021/bans_test.go index 01add7af..f527894c 100644 --- a/cmd/gochan-migration/internal/pre2021/bans_test.go +++ b/cmd/gochan-migration/internal/pre2021/bans_test.go @@ -39,4 +39,15 @@ func TestMigrateBansToNewDB(t *testing.T) { var numInvalidBans int assert.NoError(t, gcsql.QueryRowSQL("SELECT COUNT(*) FROM DBPREFIXip_ban WHERE message = ?", []any{"Full ban on 8.8.0.0/16"}, []any{&numInvalidBans})) assert.Equal(t, 0, numInvalidBans, "Expected the invalid test to not be migrated") + + filters, err := gcsql.GetAllFilters(gcsql.TrueOrFalse) + if !assert.NoError(t, err) { + t.FailNow() + } + assert.Equal(t, 1, len(filters)) + conditions, err := filters[0].Conditions() + if !assert.NoError(t, err) { + t.FailNow() + } + assert.Equal(t, 3, len(conditions), "Expected filter to have three conditions") }