diff --git a/internal/api/model/content.go b/internal/api/model/content.go index 7da389ed1..5af81b11b 100644 --- a/internal/api/model/content.go +++ b/internal/api/model/content.go @@ -19,7 +19,6 @@ package model import ( "io" - "time" "github.com/superseriousbusiness/gotosocial/internal/storage" ) @@ -30,8 +29,6 @@ type Content struct { ContentType string // ContentLength in bytes ContentLength int64 - // Time when the content was last updated. - ContentUpdated time.Time // Actual content Content io.ReadCloser // Resource URL to forward to if the file can be fetched from the storage directly (e.g signed S3 URL) diff --git a/internal/cache/size.go b/internal/cache/size.go index 32916af42..988755099 100644 --- a/internal/cache/size.go +++ b/internal/cache/size.go @@ -505,7 +505,6 @@ func sizeofMedia() uintptr { URL: exampleURI, RemoteURL: exampleURI, CreatedAt: exampleTime, - UpdatedAt: exampleTime, Type: gtsmodel.FileTypeImage, AccountID: exampleID, Description: exampleText, diff --git a/internal/db/bundb/media.go b/internal/db/bundb/media.go index 453ad856a..09c8188f0 100644 --- a/internal/db/bundb/media.go +++ b/internal/db/bundb/media.go @@ -104,12 +104,6 @@ func (m *mediaDB) PutAttachment(ctx context.Context, media *gtsmodel.MediaAttach } func (m *mediaDB) UpdateAttachment(ctx context.Context, media *gtsmodel.MediaAttachment, columns ...string) error { - media.UpdatedAt = time.Now() - if len(columns) > 0 { - // If we're updating by column, ensure "updated_at" is included. - columns = append(columns, "updated_at") - } - return m.state.Caches.DB.Media.Store(media, func() error { _, err := m.db.NewUpdate(). Model(media). diff --git a/internal/db/bundb/migrations/20241203124608_remove_media_attachment_updated_at.go b/internal/db/bundb/migrations/20241203124608_remove_media_attachment_updated_at.go new file mode 100644 index 000000000..344168b38 --- /dev/null +++ b/internal/db/bundb/migrations/20241203124608_remove_media_attachment_updated_at.go @@ -0,0 +1,57 @@ +// GoToSocial +// Copyright (C) GoToSocial Authors admin@gotosocial.org +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package migrations + +import ( + "context" + + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/uptrace/bun" +) + +func init() { + up := func(ctx context.Context, db *bun.DB) error { + return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + + // Check for 'updated_at' column on media attachments table, else return. + exists, err := doesColumnExist(ctx, tx, "media_attachments", "updated_at") + if err != nil { + return err + } else if !exists { + return nil + } + + // Remove 'updated_at' column. + _, err = tx.NewDropColumn(). + Model((*gtsmodel.MediaAttachment)(nil)). + Column("updated_at"). + Exec(ctx) + return err + }) + } + + down := func(ctx context.Context, db *bun.DB) error { + return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + return nil + }) + } + + if err := Migrations.Register(up, down); err != nil { + panic(err) + } +} diff --git a/internal/db/bundb/status.go b/internal/db/bundb/status.go index 4b2059acc..cbba7ab1a 100644 --- a/internal/db/bundb/status.go +++ b/internal/db/bundb/status.go @@ -21,7 +21,6 @@ import ( "context" "errors" "slices" - "time" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtscontext" @@ -365,14 +364,14 @@ func (s *statusDB) PutStatus(ctx context.Context, status *gtsmodel.Status) error } } - // change the status ID of the media attachments to the new status + // change the status ID of the media + // attachments to the current status for _, a := range status.Attachments { a.StatusID = status.ID - a.UpdatedAt = time.Now() if _, err := tx. NewUpdate(). Model(a). - Column("status_id", "updated_at"). + Column("status_id"). Where("? = ?", bun.Ident("media_attachment.id"), a.ID). Exec(ctx); err != nil { if !errors.Is(err, db.ErrAlreadyExists) { @@ -399,7 +398,9 @@ func (s *statusDB) PutStatus(ctx context.Context, status *gtsmodel.Status) error } // Finally, insert the status - _, err := tx.NewInsert().Model(status).Exec(ctx) + _, err := tx.NewInsert(). + Model(status). + Exec(ctx) return err }) }) @@ -443,13 +444,14 @@ func (s *statusDB) UpdateStatus(ctx context.Context, status *gtsmodel.Status, co } } - // change the status ID of the media attachments to the new status + // change the status ID of the media + // attachments to the current status. for _, a := range status.Attachments { a.StatusID = status.ID - a.UpdatedAt = time.Now() if _, err := tx. NewUpdate(). Model(a). + Column("status_id"). Where("? = ?", bun.Ident("media_attachment.id"), a.ID). Exec(ctx); err != nil { if !errors.Is(err, db.ErrAlreadyExists) { @@ -476,8 +478,7 @@ func (s *statusDB) UpdateStatus(ctx context.Context, status *gtsmodel.Status, co } // Finally, update the status - _, err := tx. - NewUpdate(). + _, err := tx.NewUpdate(). Model(status). Column(columns...). Where("? = ?", bun.Ident("status.id"), status.ID). diff --git a/internal/federation/dereferencing/status_test.go b/internal/federation/dereferencing/status_test.go index d3142b694..3114ba973 100644 --- a/internal/federation/dereferencing/status_test.go +++ b/internal/federation/dereferencing/status_test.go @@ -267,7 +267,17 @@ func (suite *StatusTestSuite) TestDereferencerGetStatusUpdated() { editedAt time.Time ) - // Edit the "remote" status content. + // Perform any edits to the remote status. + editedContent = "updated status content!" + editedContentWarning = "CW: edited status content" + editedLanguage = testStatus.Language // no change + editedSensitive = *testStatus.Sensitive // no change + editedAttachmentIDs = testStatus.AttachmentIDs // no change + editedPollOptions = getPollOptions(testStatus) // no change + editedPollVotes = getPollVotes(testStatus) // no change + editedAt = time.Now() + + // Edit the "remote" statusable obj. suite.editStatusable(testStatusable, editedContent, editedContentWarning, @@ -323,19 +333,9 @@ func (suite *StatusTestSuite) TestDereferencerGetStatusUpdated() { Language: testStatus.Language, Sensitive: testStatus.Sensitive, AttachmentIDs: testStatus.AttachmentIDs, - PollOptions: func() []string { - if testStatus.Poll != nil { - return testStatus.Poll.Options - } - return nil - }(), - PollVotes: func() []int { - if testStatus.Poll != nil { - return testStatus.Poll.Votes - } - return nil - }(), - CreatedAt: testStatus.CreatedAt, + PollOptions: getPollOptions(testStatus), + PollVotes: getPollVotes(testStatus), + CreatedAt: testStatus.CreatedAt, }, ) } @@ -372,7 +372,17 @@ func (suite *StatusTestSuite) TestDereferencerRefreshStatusUpdated() { editedAt time.Time ) - // Edit the "remote" status content. + // Perform any edits to the remote status. + editedContent = "updated status content!" + editedContentWarning = "CW: edited status content" + editedLanguage = testStatus.Language // no change + editedSensitive = *testStatus.Sensitive // no change + editedAttachmentIDs = testStatus.AttachmentIDs // no change + editedPollOptions = getPollOptions(testStatus) // no change + editedPollVotes = getPollVotes(testStatus) // no change + editedAt = time.Now() + + // Edit the "remote" statusable obj. suite.editStatusable(testStatusable, editedContent, editedContentWarning, @@ -423,19 +433,9 @@ func (suite *StatusTestSuite) TestDereferencerRefreshStatusUpdated() { Language: testStatus.Language, Sensitive: testStatus.Sensitive, AttachmentIDs: testStatus.AttachmentIDs, - PollOptions: func() []string { - if testStatus.Poll != nil { - return testStatus.Poll.Options - } - return nil - }(), - PollVotes: func() []int { - if testStatus.Poll != nil { - return testStatus.Poll.Votes - } - return nil - }(), - CreatedAt: testStatus.CreatedAt, + PollOptions: getPollOptions(testStatus), + PollVotes: getPollVotes(testStatus), + CreatedAt: testStatus.CreatedAt, }, ) } @@ -472,7 +472,17 @@ func (suite *StatusTestSuite) TestDereferencerRefreshStatusReceivedUpdate() { editedAt time.Time ) - // Edit the "remote" status content. + // Perform any edits to the remote status. + editedContent = "updated status content!" + editedContentWarning = "CW: edited status content" + editedLanguage = testStatus.Language // no change + editedSensitive = *testStatus.Sensitive // no change + editedAttachmentIDs = testStatus.AttachmentIDs // no change + editedPollOptions = getPollOptions(testStatus) // no change + editedPollVotes = getPollVotes(testStatus) // no change + editedAt = time.Now() + + // Edit the "remote" statusable obj. suite.editStatusable(testStatusable, editedContent, editedContentWarning, @@ -523,23 +533,15 @@ func (suite *StatusTestSuite) TestDereferencerRefreshStatusReceivedUpdate() { Language: testStatus.Language, Sensitive: testStatus.Sensitive, AttachmentIDs: testStatus.AttachmentIDs, - PollOptions: func() []string { - if testStatus.Poll != nil { - return testStatus.Poll.Options - } - return nil - }(), - PollVotes: func() []int { - if testStatus.Poll != nil { - return testStatus.Poll.Votes - } - return nil - }(), - CreatedAt: testStatus.CreatedAt, + PollOptions: getPollOptions(testStatus), + PollVotes: getPollVotes(testStatus), + CreatedAt: testStatus.CreatedAt, }, ) } +// editStatusable updates the given statusable attributes. +// note that this acts on the original object, no copying. func (suite *StatusTestSuite) editStatusable( statusable ap.Statusable, content string, @@ -554,6 +556,10 @@ func (suite *StatusTestSuite) editStatusable( suite.Fail("TODO") } +// verifyEditedStatusUpdate verifies that a given status has +// the expected number of historic edits, the 'current' status +// attributes (encapsulated as an edit for minimized no. args), +// and the last given 'historic' status edit attributes. func (suite *StatusTestSuite) verifyEditedStatusUpdate( status *gtsmodel.Status, // the status to check previousEdits int, // number of previous edits @@ -574,14 +580,8 @@ func (suite *StatusTestSuite) verifyEditedStatusUpdate( suite.Equal(current.Language, status.Language) suite.Equal(*current.Sensitive, *status.Sensitive) suite.Equal(current.AttachmentIDs, status.AttachmentIDs) - var pollOptions []string - var pollVotes []int - if status.Poll != nil { - pollOptions = status.Poll.Options - pollVotes = status.Poll.Votes - } - suite.Equal(current.PollOptions, pollOptions) - suite.Equal(current.PollVotes, pollVotes) + suite.Equal(current.PollOptions, getPollOptions(status)) + suite.Equal(current.PollVotes, getPollVotes(status)) suite.Equal(current.CreatedAt, status.CreatedAt) // Check the latest historic edit matches expected. @@ -602,3 +602,19 @@ func (suite *StatusTestSuite) verifyEditedStatusUpdate( func TestStatusTestSuite(t *testing.T) { suite.Run(t, new(StatusTestSuite)) } + +// getPollOptions extracts poll option strings from status (if poll is set). +func getPollOptions(status *gtsmodel.Status) []string { + if status.Poll != nil { + return status.Poll.Options + } + return nil +} + +// getPollVotes extracts poll vote counts from status (if poll is set). +func getPollVotes(status *gtsmodel.Status) []int { + if status.Poll != nil { + return status.Poll.Votes + } + return nil +} diff --git a/internal/gtsmodel/mediaattachment.go b/internal/gtsmodel/mediaattachment.go index f4bfb5929..5cf6f60a6 100644 --- a/internal/gtsmodel/mediaattachment.go +++ b/internal/gtsmodel/mediaattachment.go @@ -26,7 +26,6 @@ import ( type MediaAttachment struct { ID string `bun:"type:CHAR(26),pk,nullzero,notnull,unique"` // id of this item in the database CreatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` // when was item created - UpdatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` // when was item last updated StatusID string `bun:"type:CHAR(26),nullzero"` // ID of the status to which this is attached URL string `bun:",nullzero"` // Where can the attachment be retrieved on *this* server RemoteURL string `bun:",nullzero"` // Where can the attachment be retrieved on a remote server (empty for local media) diff --git a/internal/media/manager.go b/internal/media/manager.go index 4f4a251c8..6aa13c17b 100644 --- a/internal/media/manager.go +++ b/internal/media/manager.go @@ -118,7 +118,6 @@ func (m *Manager) CreateMedia( Header: util.Ptr(false), Cached: util.Ptr(false), CreatedAt: now, - UpdatedAt: now, } // Check if we were provided additional info diff --git a/internal/processing/media/getfile.go b/internal/processing/media/getfile.go index 6962601f2..11d8f7eb5 100644 --- a/internal/processing/media/getfile.go +++ b/internal/processing/media/getfile.go @@ -177,9 +177,7 @@ func (p *Processor) getAttachmentContent( } // Start preparing API content model. - apiContent := &apimodel.Content{ - ContentUpdated: attach.UpdatedAt, - } + apiContent := &apimodel.Content{} // Retrieve appropriate // size file from storage. diff --git a/internal/processing/media/unattach_test.go b/internal/processing/media/unattach_test.go index 051caa4d3..02d2c7077 100644 --- a/internal/processing/media/unattach_test.go +++ b/internal/processing/media/unattach_test.go @@ -20,7 +20,6 @@ package media_test import ( "context" "testing" - "time" "github.com/stretchr/testify/suite" ) @@ -42,8 +41,6 @@ func (suite *UnattachTestSuite) TestUnattachMedia() { dbAttachment, errWithCode := suite.db.GetAttachmentByID(ctx, a.ID) suite.NoError(errWithCode) - - suite.WithinDuration(dbAttachment.UpdatedAt, time.Now(), 1*time.Minute) suite.Empty(dbAttachment.StatusID) } diff --git a/testrig/testmodels.go b/testrig/testmodels.go index 2076cf6e1..2b83c2102 100644 --- a/testrig/testmodels.go +++ b/testrig/testmodels.go @@ -718,7 +718,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment { URL: "http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/attachment/original/01F8MH6NEM8D7527KZAECTCR76.jpg", RemoteURL: "", CreatedAt: TimeMustParse("2022-06-04T13:12:00Z"), - UpdatedAt: TimeMustParse("2022-06-04T13:12:00Z"), Type: gtsmodel.FileTypeImage, FileMeta: gtsmodel.FileMeta{ Original: gtsmodel.Original{ @@ -761,7 +760,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment { URL: "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/attachment/original/01F8MH7TDVANYKWVE8VVKFPJTJ.gif", RemoteURL: "", CreatedAt: TimeMustParse("2022-06-09T13:12:00Z"), - UpdatedAt: TimeMustParse("2022-06-09T13:12:00Z"), Type: gtsmodel.FileTypeImage, FileMeta: gtsmodel.FileMeta{ Original: gtsmodel.Original{ @@ -808,7 +806,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment { URL: "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/attachment/original/01CDR64G398ADCHXK08WWTHEZ5.mp4", RemoteURL: "", CreatedAt: TimeMustParse("2022-06-09T13:12:00Z"), - UpdatedAt: TimeMustParse("2022-06-09T13:12:00Z"), Type: gtsmodel.FileTypeVideo, FileMeta: gtsmodel.FileMeta{ Original: gtsmodel.Original{ @@ -858,7 +855,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment { URL: "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/attachment/original/01F8MH8RMYQ6MSNY3JM2XT1CQ5.jpg", RemoteURL: "", CreatedAt: TimeMustParse("2022-06-09T13:12:00Z"), - UpdatedAt: TimeMustParse("2022-06-09T13:12:00Z"), Type: gtsmodel.FileTypeImage, FileMeta: gtsmodel.FileMeta{ Original: gtsmodel.Original{ @@ -905,7 +901,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment { URL: "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/avatar/original/01F8MH58A357CV5K7R7TJMSH6S.jpg", RemoteURL: "", CreatedAt: TimeMustParse("2022-06-09T13:12:00Z"), - UpdatedAt: TimeMustParse("2022-06-09T13:12:00Z"), Type: gtsmodel.FileTypeImage, FileMeta: gtsmodel.FileMeta{ Original: gtsmodel.Original{ @@ -952,7 +947,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment { URL: "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/header/original/01PFPMWK2FF0D9WMHEJHR07C3Q.jpg", RemoteURL: "", CreatedAt: TimeMustParse("2022-06-09T13:12:00Z"), - UpdatedAt: TimeMustParse("2022-06-09T13:12:00Z"), Type: gtsmodel.FileTypeImage, FileMeta: gtsmodel.FileMeta{ Original: gtsmodel.Original{ @@ -999,7 +993,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment { URL: "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/attachment/original/01J2M20K6K9XQC4WSB961YJHV6.mp3", RemoteURL: "", CreatedAt: TimeMustParse("2024-01-10T11:24:00+02:00"), - UpdatedAt: TimeMustParse("2024-01-10T11:24:00+02:00"), Type: gtsmodel.FileTypeAudio, FileMeta: gtsmodel.FileMeta{ Original: gtsmodel.Original{ @@ -1049,7 +1042,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment { URL: "http://localhost:8080/fileserver/01FHMQX3GAABWSM0S2VZEC2SWC/attachment/original/01HE88YG74PVAB81PX2XA9F3FG.mp3", RemoteURL: "http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE892Y8ZS68TQCNPX7J888P3.mp3", CreatedAt: TimeMustParse("2024-11-01T10:01:00+02:00"), - UpdatedAt: TimeMustParse("2024-11-01T10:01:00+02:00"), Type: gtsmodel.FileTypeUnknown, FileMeta: gtsmodel.FileMeta{}, AccountID: "01F8MH5NBDF2MV7CTC4Q5128HF", @@ -1068,7 +1060,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment { URL: "http://localhost:8080/fileserver/01F8MH5ZK5VRH73AKHQM6Y9VNX/attachment/original/01FVW7RXPQ8YJHTEXYPE7Q8ZY0.jpg", RemoteURL: "http://fossbros-anonymous.io/attachments/original/13bbc3f8-2b5e-46ea-9531-40b4974d9912.jpg", CreatedAt: TimeMustParse("2021-09-20T12:40:37+02:00"), - UpdatedAt: TimeMustParse("2021-09-20T12:40:37+02:00"), Type: gtsmodel.FileTypeImage, FileMeta: gtsmodel.FileMeta{ Original: gtsmodel.Original{ @@ -1114,7 +1105,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment { URL: "http://localhost:8080/fileserver/062G5WYKY35KKD12EMSM3F8PJ8/header/original/01PFPMWK2FF0D9WMHEJHR07C3R.jpg", RemoteURL: "http://fossbros-anonymous.io/attachments/small/a499f55b-2d1e-4acd-98d2-1ac2ba6d79b9.jpg", CreatedAt: TimeMustParse("2022-06-09T13:12:00Z"), - UpdatedAt: TimeMustParse("2022-06-09T13:12:00Z"), Type: gtsmodel.FileTypeImage, FileMeta: gtsmodel.FileMeta{ Original: gtsmodel.Original{ @@ -1160,7 +1150,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment { URL: "http://localhost:8080/fileserver/01FHMQX3GAABWSM0S2VZEC2SWC/attachment/original/01HE7Y3C432WRSNS10EZM86SA5.jpg", RemoteURL: "http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE7Y6G0EMCKST3Q0914WW0MS.jpg", CreatedAt: TimeMustParse("2023-11-02T12:44:25+02:00"), - UpdatedAt: TimeMustParse("2023-11-02T12:44:25+02:00"), Type: gtsmodel.FileTypeImage, FileMeta: gtsmodel.FileMeta{ Original: gtsmodel.Original{ @@ -1205,7 +1194,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment { URL: "http://localhost:8080/fileserver/01FHMQX3GAABWSM0S2VZEC2SWC/attachment/original/01HE7ZFX9GKA5ZZVD4FACABSS9.svg", RemoteURL: "http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE7ZGJYTSYMXF927GF9353KR.svg", CreatedAt: TimeMustParse("2023-11-02T12:44:25+02:00"), - UpdatedAt: TimeMustParse("2023-11-02T12:44:25+02:00"), Type: gtsmodel.FileTypeUnknown, FileMeta: gtsmodel.FileMeta{}, AccountID: "01FHMQX3GAABWSM0S2VZEC2SWC", @@ -1224,7 +1212,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment { URL: "http://localhost:8080/fileserver/01FHMQX3GAABWSM0S2VZEC2SWC/attachment/original/01HE88YG74PVAB81PX2XA9F3FG.mp3", RemoteURL: "http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE892Y8ZS68TQCNPX7J888P3.mp3", CreatedAt: TimeMustParse("2023-11-02T12:44:25+02:00"), - UpdatedAt: TimeMustParse("2023-11-02T12:44:25+02:00"), Type: gtsmodel.FileTypeUnknown, FileMeta: gtsmodel.FileMeta{}, AccountID: "01FHMQX3GAABWSM0S2VZEC2SWC",