Skip to content

Commit

Permalink
Add migrating pre-2021 name/file bans to filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Eggbertx committed Jan 6, 2025
1 parent 01d7a72 commit 4f9f1e0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
62 changes: 56 additions & 6 deletions cmd/gochan-migration/internal/pre2021/bans.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pre2021

import (
"context"
"database/sql"
"errors"
"net"
Expand Down Expand Up @@ -33,9 +34,7 @@ type migrationBan struct {
canAppeal bool

boardIDs []int
banID int
staffID int
filterID int
}

func (m *Pre2021Migrator) migrateBansInPlace() error {
Expand Down Expand Up @@ -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().
Expand All @@ -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()
Expand Down
11 changes: 11 additions & 0 deletions cmd/gochan-migration/internal/pre2021/bans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit 4f9f1e0

Please sign in to comment.