diff --git a/models/moderation/abuse_report.go b/models/moderation/abuse_report.go index 20c9ed23ad..8a4a29b23e 100644 --- a/models/moderation/abuse_report.go +++ b/models/moderation/abuse_report.go @@ -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"` } diff --git a/models/moderation/shadow_copy.go b/models/moderation/shadow_copy.go index c48f5ce2ae..813d09ee37 100644 --- a/models/moderation/shadow_copy.go +++ b/models/moderation/shadow_copy.go @@ -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) }