diff --git a/internal/db/bundb/statusedit.go b/internal/db/bundb/statusedit.go index ed62c64f1..a896284d8 100644 --- a/internal/db/bundb/statusedit.go +++ b/internal/db/bundb/statusedit.go @@ -96,8 +96,8 @@ func (s *statusEditDB) GetStatusEditsByIDs(ctx context.Context, ids []string) ([ return nil, err } - // Reorder the edits by their - // IDs to ensure in correct order. + // Reorder the edits by their IDs to ensure in correct + // order (ID ascending, ie., latest to oldest edit). getID := func(e *gtsmodel.StatusEdit) string { return e.ID } xslices.OrderBy(edits, ids, getID) diff --git a/internal/gtsmodel/status.go b/internal/gtsmodel/status.go index 78aa09dd9..31e8fe881 100644 --- a/internal/gtsmodel/status.go +++ b/internal/gtsmodel/status.go @@ -58,8 +58,8 @@ type Status struct { BoostOf *Status `bun:"-"` // status that corresponds to boostOfID BoostOfAccount *Account `bun:"rel:belongs-to"` // account that corresponds to boostOfAccountID ThreadID string `bun:"type:CHAR(26),nullzero,notnull,default:00000000000000000000000000"` // id of the thread to which this status belongs - EditIDs []string `bun:"edits,array"` // - Edits []*StatusEdit `bun:"-"` // + EditIDs []string `bun:"edits,array"` // IDs of status edits for this status, ordered from smallest (oldest) -> largest (newest) ID. + Edits []*StatusEdit `bun:"-"` // Edits of this status, ordered from oldest -> newest edit. PollID string `bun:"type:CHAR(26),nullzero"` // Poll *Poll `bun:"-"` // ContentWarning string `bun:",nullzero"` // Content warning HTML for this status. diff --git a/internal/processing/status/edit.go b/internal/processing/status/edit.go index 3ca21f5cf..2a2321604 100644 --- a/internal/processing/status/edit.go +++ b/internal/processing/status/edit.go @@ -375,13 +375,13 @@ func (p *Processor) HistoryGet(ctx context.Context, requester *gtsmodel.Account, return nil, gtserror.NewErrorInternalError(err) } - edits, err := p.converter.StatusToAPIEdits(ctx, target) + editHistory, err := p.converter.StatusToEditHistory(ctx, target) if err != nil { err := gtserror.Newf("error converting status edits: %w", err) return nil, gtserror.NewErrorInternalError(err) } - return edits, nil + return editHistory, nil } func (p *Processor) processMediaEdits( diff --git a/internal/typeutils/internaltofrontend.go b/internal/typeutils/internaltofrontend.go index 961e99206..72b7a0126 100644 --- a/internal/typeutils/internaltofrontend.go +++ b/internal/typeutils/internaltofrontend.go @@ -1061,14 +1061,17 @@ func (c *Converter) StatusToWebStatus( ) } - // Make sure to include latest revision. + // End with latest revision. webStatus.EditTimeline = append( webStatus.EditTimeline, *webStatus.EditedAt, ) - // Sort the slice so it goes from - // newest -> oldest, like a timeline. + // Reverse the slice so that instead of going + // from oldest (original status) to newest + // (latest revision), it goes from newest + // to oldest, like a timeline, to make + // things easier when web templating. // // It'll look something like: // @@ -1344,8 +1347,13 @@ func (c *Converter) baseStatusToFrontend( return apiStatus, nil } -// StatusToAPIEdits converts a status and its historical edits (if any) to a slice of API model status edits. -func (c *Converter) StatusToAPIEdits(ctx context.Context, status *gtsmodel.Status) ([]*apimodel.StatusEdit, error) { +// StatusToEditHistory converts a status and its historical edits +// (if any) to a slice of API model status edits, ordered from original +// status at index 0 to latest revision at index len(slice)-1. +func (c *Converter) StatusToEditHistory( + ctx context.Context, + status *gtsmodel.Status, +) ([]*apimodel.StatusEdit, error) { var media map[string]*gtsmodel.MediaAttachment // Gather attachments of status AND edits. @@ -1397,8 +1405,11 @@ func (c *Converter) StatusToAPIEdits(ctx context.Context, status *gtsmodel.Statu } } - // Append status itself to final slot in the edits - // so we can add its revision using the below loop. + // Append *current* version of the status to last slot + // in the edits so we can add it at the bottom as latest + // revision using the below loop. Note: a new slice is + // created here with the append, to avoid modifying + // Edits on the status pointer. edits := append(status.Edits, >smodel.StatusEdit{ //nolint:gocritic Content: status.Content, ContentWarning: status.ContentWarning, @@ -1410,10 +1421,13 @@ func (c *Converter) StatusToAPIEdits(ctx context.Context, status *gtsmodel.Statu CreatedAt: status.UpdatedAt(), // falls back to creation }) - // Iterate through status edits, starting at newest. - apiEdits := make([]*apimodel.StatusEdit, 0, len(edits)) - for i := len(edits) - 1; i >= 0; i-- { - edit := edits[i] + // Iterate through status revisions, starting at original + // status when it was created (ie., oldest revision). + // + // This creates a slice of revisions that goes from + // oldest (original status) to newest (latest revision). + editHistory := make([]*apimodel.StatusEdit, 0, len(edits)) + for _, edit := range edits { // Iterate through edit attachment IDs, getting model from 'media' lookup. apiAttachments := make([]*apimodel.Attachment, 0, len(edit.AttachmentIDs)) @@ -1472,7 +1486,7 @@ func (c *Converter) StatusToAPIEdits(ctx context.Context, status *gtsmodel.Statu } // Append this status edit to the return slice. - apiEdits = append(apiEdits, &apimodel.StatusEdit{ + editHistory = append(editHistory, &apimodel.StatusEdit{ CreatedAt: util.FormatISO8601(edit.CreatedAt), Content: edit.Content, SpoilerText: edit.ContentWarning, @@ -1484,7 +1498,7 @@ func (c *Converter) StatusToAPIEdits(ctx context.Context, status *gtsmodel.Statu }) } - return apiEdits, nil + return editHistory, nil } // VisToAPIVis converts a gts visibility into its api equivalent diff --git a/internal/typeutils/internaltofrontend_test.go b/internal/typeutils/internaltofrontend_test.go index 5d066f410..6c77c3f26 100644 --- a/internal/typeutils/internaltofrontend_test.go +++ b/internal/typeutils/internaltofrontend_test.go @@ -3671,7 +3671,7 @@ func (suite *InternalToFrontendTestSuite) TestStatusToAPIEdits() { ctx, cncl := context.WithCancel(suite.T().Context()) defer cncl() - statusID := suite.testStatuses["local_account_1_status_9"].ID + statusID := suite.testStatuses["local_account_2_status_9"].ID status, err := suite.state.DB.GetStatusByID(ctx, statusID) suite.NoError(err) @@ -3679,123 +3679,218 @@ func (suite *InternalToFrontendTestSuite) TestStatusToAPIEdits() { err = suite.state.DB.PopulateStatusEdits(ctx, status) suite.NoError(err) - apiEdits, err := suite.typeconverter.StatusToAPIEdits(ctx, status) + apiEdits, err := suite.typeconverter.StatusToEditHistory(ctx, status) suite.NoError(err) b, err := json.MarshalIndent(apiEdits, "", " ") suite.NoError(err) suite.Equal(`[ - { - "content": "\u003cp\u003ethis is the latest revision of the status, with a content-warning\u003c/p\u003e", - "spoiler_text": "edited status", - "sensitive": false, - "created_at": "2024-11-01T09:02:00.000Z", - "account": { - "id": "01F8MH1H7YV1Z7D2C8K2730QBF", - "username": "the_mighty_zork", - "acct": "the_mighty_zork", - "display_name": "original zork (he/they)", - "locked": false, - "discoverable": true, - "bot": false, - "created_at": "2022-05-20T11:09:18.000Z", - "note": "\u003cp\u003ehey yo this is my profile!\u003c/p\u003e", - "url": "http://localhost:8080/@the_mighty_zork", - "avatar": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/avatar/original/01F8MH58A357CV5K7R7TJMSH6S.jpg", - "avatar_static": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/avatar/small/01F8MH58A357CV5K7R7TJMSH6S.webp", - "avatar_description": "a green goblin looking nasty", - "avatar_media_id": "01F8MH58A357CV5K7R7TJMSH6S", - "header": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/header/original/01PFPMWK2FF0D9WMHEJHR07C3Q.jpg", - "header_static": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/header/small/01PFPMWK2FF0D9WMHEJHR07C3Q.webp", - "header_description": "A very old-school screenshot of the original team fortress mod for quake", - "header_media_id": "01PFPMWK2FF0D9WMHEJHR07C3Q", - "followers_count": 2, - "following_count": 2, - "statuses_count": 9, - "last_status_at": "2024-11-01", - "emojis": [], - "fields": [], - "enable_rss": true, - "group": false - }, - "poll": null, - "media_attachments": [], - "emojis": [] - }, - { - "content": "\u003cp\u003ethis is the first status edit! now with content-warning\u003c/p\u003e", - "spoiler_text": "edited status", - "sensitive": false, - "created_at": "2024-11-01T09:01:00.000Z", - "account": { - "id": "01F8MH1H7YV1Z7D2C8K2730QBF", - "username": "the_mighty_zork", - "acct": "the_mighty_zork", - "display_name": "original zork (he/they)", - "locked": false, - "discoverable": true, - "bot": false, - "created_at": "2022-05-20T11:09:18.000Z", - "note": "\u003cp\u003ehey yo this is my profile!\u003c/p\u003e", - "url": "http://localhost:8080/@the_mighty_zork", - "avatar": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/avatar/original/01F8MH58A357CV5K7R7TJMSH6S.jpg", - "avatar_static": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/avatar/small/01F8MH58A357CV5K7R7TJMSH6S.webp", - "avatar_description": "a green goblin looking nasty", - "avatar_media_id": "01F8MH58A357CV5K7R7TJMSH6S", - "header": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/header/original/01PFPMWK2FF0D9WMHEJHR07C3Q.jpg", - "header_static": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/header/small/01PFPMWK2FF0D9WMHEJHR07C3Q.webp", - "header_description": "A very old-school screenshot of the original team fortress mod for quake", - "header_media_id": "01PFPMWK2FF0D9WMHEJHR07C3Q", - "followers_count": 2, - "following_count": 2, - "statuses_count": 9, - "last_status_at": "2024-11-01", - "emojis": [], - "fields": [], - "enable_rss": true, - "group": false - }, - "poll": null, - "media_attachments": [], - "emojis": [] - }, { "content": "\u003cp\u003ethis is the original status\u003c/p\u003e", "spoiler_text": "", "sensitive": false, - "created_at": "2024-11-01T09:00:00.000Z", + "created_at": "2024-11-01T08:00:00.000Z", "account": { - "id": "01F8MH1H7YV1Z7D2C8K2730QBF", - "username": "the_mighty_zork", - "acct": "the_mighty_zork", - "display_name": "original zork (he/they)", - "locked": false, - "discoverable": true, + "id": "01F8MH5NBDF2MV7CTC4Q5128HF", + "username": "1happyturtle", + "acct": "1happyturtle", + "display_name": "happy little turtle :3", + "locked": true, + "discoverable": false, "bot": false, - "created_at": "2022-05-20T11:09:18.000Z", - "note": "\u003cp\u003ehey yo this is my profile!\u003c/p\u003e", - "url": "http://localhost:8080/@the_mighty_zork", - "avatar": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/avatar/original/01F8MH58A357CV5K7R7TJMSH6S.jpg", - "avatar_static": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/avatar/small/01F8MH58A357CV5K7R7TJMSH6S.webp", - "avatar_description": "a green goblin looking nasty", - "avatar_media_id": "01F8MH58A357CV5K7R7TJMSH6S", - "header": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/header/original/01PFPMWK2FF0D9WMHEJHR07C3Q.jpg", - "header_static": "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/header/small/01PFPMWK2FF0D9WMHEJHR07C3Q.webp", - "header_description": "A very old-school screenshot of the original team fortress mod for quake", - "header_media_id": "01PFPMWK2FF0D9WMHEJHR07C3Q", - "followers_count": 2, - "following_count": 2, + "created_at": "2022-06-04T13:12:00.000Z", + "note": "\u003cp\u003ei post about things that concern me\u003c/p\u003e", + "url": "http://localhost:8080/@1happyturtle", + "avatar": "", + "avatar_static": "", + "header": "http://localhost:8080/assets/default_header.webp", + "header_static": "http://localhost:8080/assets/default_header.webp", + "header_description": "Flat gray background (default header).", + "followers_count": 1, + "following_count": 1, "statuses_count": 9, "last_status_at": "2024-11-01", "emojis": [], - "fields": [], - "enable_rss": true, + "fields": [ + { + "name": "should you follow me?", + "value": "maybe!", + "verified_at": null + }, + { + "name": "age", + "value": "120", + "verified_at": null + } + ], + "hide_collections": true, "group": false }, "poll": null, "media_attachments": [], "emojis": [] + }, + { + "content": "\u003cp\u003enow edited to have some media!\u003c/p\u003e", + "spoiler_text": "edit with media attachments", + "sensitive": true, + "created_at": "2024-11-01T08:01:00.000Z", + "account": { + "id": "01F8MH5NBDF2MV7CTC4Q5128HF", + "username": "1happyturtle", + "acct": "1happyturtle", + "display_name": "happy little turtle :3", + "locked": true, + "discoverable": false, + "bot": false, + "created_at": "2022-06-04T13:12:00.000Z", + "note": "\u003cp\u003ei post about things that concern me\u003c/p\u003e", + "url": "http://localhost:8080/@1happyturtle", + "avatar": "", + "avatar_static": "", + "header": "http://localhost:8080/assets/default_header.webp", + "header_static": "http://localhost:8080/assets/default_header.webp", + "header_description": "Flat gray background (default header).", + "followers_count": 1, + "following_count": 1, + "statuses_count": 9, + "last_status_at": "2024-11-01", + "emojis": [], + "fields": [ + { + "name": "should you follow me?", + "value": "maybe!", + "verified_at": null + }, + { + "name": "age", + "value": "120", + "verified_at": null + } + ], + "hide_collections": true, + "group": false + }, + "poll": null, + "media_attachments": [ + { + "id": "01JDQ164HM08SGJ7ZEK9003Z4B", + "type": "unknown", + "url": null, + "text_url": null, + "preview_url": null, + "remote_url": "http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE892Y8ZS68TQCNPX7J888P3.mp3", + "preview_remote_url": null, + "meta": null, + "description": "Jolly salsa song, public domain.", + "blurhash": null + } + ], + "emojis": [] + }, + { + "content": "\u003cp\u003enow edited to remove the media\u003c/p\u003e", + "spoiler_text": "edit missing previous media attachments", + "sensitive": false, + "created_at": "2024-11-01T08:02:00.000Z", + "account": { + "id": "01F8MH5NBDF2MV7CTC4Q5128HF", + "username": "1happyturtle", + "acct": "1happyturtle", + "display_name": "happy little turtle :3", + "locked": true, + "discoverable": false, + "bot": false, + "created_at": "2022-06-04T13:12:00.000Z", + "note": "\u003cp\u003ei post about things that concern me\u003c/p\u003e", + "url": "http://localhost:8080/@1happyturtle", + "avatar": "", + "avatar_static": "", + "header": "http://localhost:8080/assets/default_header.webp", + "header_static": "http://localhost:8080/assets/default_header.webp", + "header_description": "Flat gray background (default header).", + "followers_count": 1, + "following_count": 1, + "statuses_count": 9, + "last_status_at": "2024-11-01", + "emojis": [], + "fields": [ + { + "name": "should you follow me?", + "value": "maybe!", + "verified_at": null + }, + { + "name": "age", + "value": "120", + "verified_at": null + } + ], + "hide_collections": true, + "group": false + }, + "poll": null, + "media_attachments": [], + "emojis": [] + }, + { + "content": "\u003cp\u003enow edited to bring back the previous edit's media!\u003c/p\u003e", + "spoiler_text": "edit with media attachments", + "sensitive": false, + "created_at": "2024-11-01T08:03:00.000Z", + "account": { + "id": "01F8MH5NBDF2MV7CTC4Q5128HF", + "username": "1happyturtle", + "acct": "1happyturtle", + "display_name": "happy little turtle :3", + "locked": true, + "discoverable": false, + "bot": false, + "created_at": "2022-06-04T13:12:00.000Z", + "note": "\u003cp\u003ei post about things that concern me\u003c/p\u003e", + "url": "http://localhost:8080/@1happyturtle", + "avatar": "", + "avatar_static": "", + "header": "http://localhost:8080/assets/default_header.webp", + "header_static": "http://localhost:8080/assets/default_header.webp", + "header_description": "Flat gray background (default header).", + "followers_count": 1, + "following_count": 1, + "statuses_count": 9, + "last_status_at": "2024-11-01", + "emojis": [], + "fields": [ + { + "name": "should you follow me?", + "value": "maybe!", + "verified_at": null + }, + { + "name": "age", + "value": "120", + "verified_at": null + } + ], + "hide_collections": true, + "group": false + }, + "poll": null, + "media_attachments": [ + { + "id": "01JDQ164HM08SGJ7ZEK9003Z4B", + "type": "unknown", + "url": null, + "text_url": null, + "preview_url": null, + "remote_url": "http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE892Y8ZS68TQCNPX7J888P3.mp3", + "preview_remote_url": null, + "meta": null, + "description": "Jolly salsa song, public domain.", + "blurhash": null + } + ], + "emojis": [] } ]`, string(b)) }