Change ShadowCopyID type from *int64 to sql.NullInt64.

This commit is contained in:
floss4good 2025-03-05 11:51:54 +02:00
parent 7046bd5e39
commit 799885246a
No known key found for this signature in database
GPG key ID: 5B948B4F4DAF819D
2 changed files with 9 additions and 2 deletions

View file

@ -5,6 +5,7 @@ package moderation
import (
"context"
"database/sql"
"errors"
"slices"
@ -101,7 +102,7 @@ type AbuseReport struct {
// Remarks provided by the reporter.
Remarks string // TODO: ReporterReparks or Reason
// The ID of the corresponding shadow-copied content when exists; otherwise null.
ShadowCopyID *int64 `xorm:"DEFAULT NULL"`
ShadowCopyID sql.NullInt64 `xorm:"DEFAULT NULL"`
CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
}

View file

@ -5,6 +5,7 @@ package moderation
import (
"context"
"database/sql"
"fmt"
"code.gitea.io/gitea/models/db"
@ -20,6 +21,11 @@ type AbuseReportShadowCopy struct {
CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
}
// Returns the ID encapsulated in a sql.NullInt64 struct.
func (sc AbuseReportShadowCopy) NullableID() sql.NullInt64 {
return sql.NullInt64{Int64: sc.ID, Valid: sc.ID > 0}
}
func init() {
// RegisterModel will create the table if does not already exist
// or any missing columns if the table was previously created.
@ -69,7 +75,7 @@ func createShadowCopy(ctx context.Context, contentType ReportedContentType, cont
"content_type": contentType,
"content_id": contentID,
// TODO: What should happen if an item is updated multiple times (and the reports already have a shadow copy ID)?
}).And(builder.IsNull{"shadow_copy_id"}).Cols("shadow_copy_id").Update(&AbuseReport{ShadowCopyID: &shadowCopy.ID})
}).And(builder.IsNull{"shadow_copy_id"}).Update(&AbuseReport{ShadowCopyID: shadowCopy.NullableID()})
if err != nil {
return fmt.Errorf("could not link the shadow copy (%d) to reported content with type %d and ID %d - %w", shadowCopy.ID, contentType, contentID, err)
}