mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-10-29 19:22:24 -05:00
[feature] scheduled statuses (#4274)
An implementation of [`scheduled_statuses`](https://docs.joinmastodon.org/methods/scheduled_statuses/). Will fix #1006. this is heavily WIP and I need to reorganize some of the code, working on this made me somehow familiar with the codebase and led to my other recent contributions i told some fops on fedi i'd work on this so i have no choice but to complete it 🤷♀️ btw iirc my avatar presents me working on this branch Signed-off-by: nicole mikołajczyk <git@mkljczk.pl> Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4274 Co-authored-by: nicole mikołajczyk <git@mkljczk.pl> Co-committed-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
parent
cead741c16
commit
660cf2c94c
46 changed files with 2354 additions and 68 deletions
|
|
@ -459,6 +459,11 @@ func (p *Processor) deleteAccountPeripheral(
|
|||
if err := p.state.DB.DeleteAccountStats(ctx, account.ID); err != nil {
|
||||
log.Errorf("error deleting stats for account: %v", err)
|
||||
}
|
||||
|
||||
// Delete statuses scheduled by given account, only for local.
|
||||
if err := p.state.DB.DeleteScheduledStatusesByAccountID(ctx, account.ID); err != nil {
|
||||
log.Errorf("error deleting scheduled statuses for account: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete all bookmarks targeting given account, local and remote.
|
||||
|
|
|
|||
|
|
@ -66,5 +66,11 @@ func (p *Processor) Delete(
|
|||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
// Delete all scheduled statuses posted from the app.
|
||||
if err := p.state.DB.DeleteScheduledStatusesByApplicationID(ctx, appID); err != nil {
|
||||
err := gtserror.Newf("db error deleting scheduled statuses for app %s: %w", appID, err)
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
return apiApp, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -282,6 +282,7 @@ func (p *Processor) processMedia(
|
|||
authorID string,
|
||||
statusID string,
|
||||
mediaIDs []string,
|
||||
scheduledStatusID *string,
|
||||
) (
|
||||
[]*gtsmodel.MediaAttachment,
|
||||
gtserror.WithCode,
|
||||
|
|
@ -315,7 +316,7 @@ func (p *Processor) processMedia(
|
|||
|
||||
// Check media isn't already attached to another status.
|
||||
if (media.StatusID != "" && media.StatusID != statusID) ||
|
||||
(media.ScheduledStatusID != "" && media.ScheduledStatusID != statusID) {
|
||||
(media.ScheduledStatusID != "" && (media.ScheduledStatusID != statusID && (scheduledStatusID == nil || media.ScheduledStatusID != *scheduledStatusID))) {
|
||||
text := fmt.Sprintf("media already attached to status: %s", id)
|
||||
return nil, gtserror.NewErrorBadRequest(errors.New(text), text)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,10 +44,8 @@ func (p *Processor) Create(
|
|||
requester *gtsmodel.Account,
|
||||
application *gtsmodel.Application,
|
||||
form *apimodel.StatusCreateRequest,
|
||||
) (
|
||||
*apimodel.Status,
|
||||
gtserror.WithCode,
|
||||
) {
|
||||
scheduledStatusID *string,
|
||||
) (any, gtserror.WithCode) {
|
||||
// Validate incoming form status content.
|
||||
if errWithCode := validateStatusContent(
|
||||
form.Status,
|
||||
|
|
@ -83,16 +81,6 @@ func (p *Processor) Create(
|
|||
return nil, errWithCode
|
||||
}
|
||||
|
||||
// Process incoming status attachments.
|
||||
media, errWithCode := p.processMedia(ctx,
|
||||
requester.ID,
|
||||
statusID,
|
||||
form.MediaIDs,
|
||||
)
|
||||
if errWithCode != nil {
|
||||
return nil, errWithCode
|
||||
}
|
||||
|
||||
// Generate necessary URIs for username, to build status URIs.
|
||||
accountURIs := uris.GenerateURIsForAccount(requester.Username)
|
||||
|
||||
|
|
@ -105,16 +93,27 @@ func (p *Processor) Create(
|
|||
|
||||
// Handle backfilled/scheduled statuses.
|
||||
backfill := false
|
||||
if form.ScheduledAt != nil {
|
||||
scheduledAt := *form.ScheduledAt
|
||||
|
||||
// Statuses may only be scheduled
|
||||
// a minimum time into the future.
|
||||
if now.Before(scheduledAt) {
|
||||
const errText = "scheduled statuses are not yet supported"
|
||||
return nil, gtserror.NewErrorNotImplemented(gtserror.New(errText), errText)
|
||||
switch {
|
||||
case form.ScheduledAt == nil:
|
||||
// No scheduling/backfilling
|
||||
break
|
||||
case form.ScheduledAt.Sub(now) >= 5*time.Minute:
|
||||
// Statuses may only be scheduled a minimum time into the future.
|
||||
scheduledStatus, errWithCode := p.processScheduledStatus(ctx, statusID, form, requester, application)
|
||||
|
||||
if errWithCode != nil {
|
||||
return nil, errWithCode
|
||||
}
|
||||
|
||||
return scheduledStatus, nil
|
||||
|
||||
case now.Before(*form.ScheduledAt):
|
||||
// Invalid future scheduled status
|
||||
const errText = "scheduled_at must be at least 5 minutes in the future"
|
||||
return nil, gtserror.NewErrorUnprocessableEntity(gtserror.New(errText), errText)
|
||||
|
||||
default:
|
||||
// If not scheduled into the future, this status is being backfilled.
|
||||
if !config.GetInstanceAllowBackdatingStatuses() {
|
||||
const errText = "backdating statuses has been disabled on this instance"
|
||||
|
|
@ -127,7 +126,7 @@ func (p *Processor) Create(
|
|||
// this would also cause issues with time.Time.IsZero() checks
|
||||
// that normally signify an absent optional time,
|
||||
// but this check covers both cases.
|
||||
if scheduledAt.Compare(time.UnixMilli(0)) <= 0 {
|
||||
if form.ScheduledAt.Compare(time.UnixMilli(0)) <= 0 {
|
||||
const errText = "statuses can't be backdated to or before the UNIX epoch"
|
||||
return nil, gtserror.NewErrorNotAcceptable(gtserror.New(errText), errText)
|
||||
}
|
||||
|
|
@ -138,7 +137,7 @@ func (p *Processor) Create(
|
|||
backfill = true
|
||||
|
||||
// Update to backfill date.
|
||||
createdAt = scheduledAt
|
||||
createdAt = *form.ScheduledAt
|
||||
|
||||
// Generate an appropriate, (and unique!), ID for the creation time.
|
||||
if statusID, err = p.backfilledStatusID(ctx, createdAt); err != nil {
|
||||
|
|
@ -146,6 +145,17 @@ func (p *Processor) Create(
|
|||
}
|
||||
}
|
||||
|
||||
// Process incoming status attachments.
|
||||
media, errWithCode := p.processMedia(ctx,
|
||||
requester.ID,
|
||||
statusID,
|
||||
form.MediaIDs,
|
||||
scheduledStatusID,
|
||||
)
|
||||
if errWithCode != nil {
|
||||
return nil, errWithCode
|
||||
}
|
||||
|
||||
status := >smodel.Status{
|
||||
ID: statusID,
|
||||
URI: accountURIs.StatusesURI + "/" + statusID,
|
||||
|
|
@ -546,3 +556,103 @@ func processInteractionPolicy(
|
|||
// setting it explicitly to save space.
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Processor) processScheduledStatus(
|
||||
ctx context.Context,
|
||||
statusID string,
|
||||
form *apimodel.StatusCreateRequest,
|
||||
requester *gtsmodel.Account,
|
||||
application *gtsmodel.Application,
|
||||
) (*apimodel.ScheduledStatus, gtserror.WithCode) {
|
||||
// Validate scheduled status against server configuration
|
||||
// (max scheduled statuses limit).
|
||||
if errWithCode := p.validateScheduledStatusLimits(ctx, requester.ID, form.ScheduledAt, nil); errWithCode != nil {
|
||||
return nil, errWithCode
|
||||
}
|
||||
|
||||
media, errWithCode := p.processMedia(ctx,
|
||||
requester.ID,
|
||||
statusID,
|
||||
form.MediaIDs,
|
||||
nil,
|
||||
)
|
||||
if errWithCode != nil {
|
||||
return nil, errWithCode
|
||||
}
|
||||
status := >smodel.ScheduledStatus{
|
||||
ID: statusID,
|
||||
Account: requester,
|
||||
AccountID: requester.ID,
|
||||
Application: application,
|
||||
ApplicationID: application.ID,
|
||||
ScheduledAt: *form.ScheduledAt,
|
||||
Text: form.Status,
|
||||
MediaIDs: form.MediaIDs,
|
||||
MediaAttachments: media,
|
||||
Sensitive: &form.Sensitive,
|
||||
SpoilerText: form.SpoilerText,
|
||||
InReplyToID: form.InReplyToID,
|
||||
Language: form.Language,
|
||||
LocalOnly: form.LocalOnly,
|
||||
ContentType: string(form.ContentType),
|
||||
}
|
||||
|
||||
if form.Poll != nil {
|
||||
status.Poll = gtsmodel.ScheduledStatusPoll{
|
||||
Options: form.Poll.Options,
|
||||
ExpiresIn: form.Poll.ExpiresIn,
|
||||
Multiple: &form.Poll.Multiple,
|
||||
HideTotals: &form.Poll.HideTotals,
|
||||
}
|
||||
}
|
||||
|
||||
accountDefaultVisibility := requester.Settings.Privacy
|
||||
|
||||
switch {
|
||||
case form.Visibility != "":
|
||||
status.Visibility = typeutils.APIVisToVis(form.Visibility)
|
||||
|
||||
case accountDefaultVisibility != 0:
|
||||
status.Visibility = accountDefaultVisibility
|
||||
form.Visibility = typeutils.VisToAPIVis(accountDefaultVisibility)
|
||||
|
||||
default:
|
||||
status.Visibility = gtsmodel.VisibilityDefault
|
||||
form.Visibility = typeutils.VisToAPIVis(gtsmodel.VisibilityDefault)
|
||||
}
|
||||
|
||||
if form.InteractionPolicy != nil {
|
||||
interactionPolicy, err := typeutils.APIInteractionPolicyToInteractionPolicy(form.InteractionPolicy, form.Visibility)
|
||||
|
||||
if err != nil {
|
||||
err := gtserror.Newf("error converting interaction policy: %w", err)
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
status.InteractionPolicy = interactionPolicy
|
||||
}
|
||||
|
||||
// Insert this newly prepared status into the database.
|
||||
if err := p.state.DB.PutScheduledStatus(ctx, status); err != nil {
|
||||
err := gtserror.Newf("error inserting status in db: %w", err)
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
// Schedule the newly inserted status for publishing.
|
||||
if err := p.ScheduledStatusesSchedulePublication(ctx, status.ID); err != nil {
|
||||
err := gtserror.Newf("error scheduling status publish: %w", err)
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
apiScheduledStatus, err := p.converter.ScheduledStatusToAPIScheduledStatus(
|
||||
ctx,
|
||||
status,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
err := gtserror.Newf("error converting: %w", err)
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
return apiScheduledStatus, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,7 +53,8 @@ func (suite *StatusCreateTestSuite) TestProcessContentWarningWithQuotationMarks(
|
|||
ContentType: apimodel.StatusContentTypePlain,
|
||||
}
|
||||
|
||||
apiStatus, err := suite.status.Create(ctx, creatingAccount, creatingApplication, statusCreateForm)
|
||||
apiStatusAny, err := suite.status.Create(ctx, creatingAccount, creatingApplication, statusCreateForm, nil)
|
||||
apiStatus := apiStatusAny.(*apimodel.Status)
|
||||
suite.NoError(err)
|
||||
suite.NotNil(apiStatus)
|
||||
|
||||
|
|
@ -84,7 +85,8 @@ func (suite *StatusCreateTestSuite) TestProcessStatusMarkdownWithUnderscoreEmoji
|
|||
ContentType: apimodel.StatusContentTypeMarkdown,
|
||||
}
|
||||
|
||||
apiStatus, err := suite.status.Create(ctx, creatingAccount, creatingApplication, statusCreateForm)
|
||||
apiStatusAny, err := suite.status.Create(ctx, creatingAccount, creatingApplication, statusCreateForm, nil)
|
||||
apiStatus := apiStatusAny.(*apimodel.Status)
|
||||
suite.NoError(err)
|
||||
suite.NotNil(apiStatus)
|
||||
|
||||
|
|
@ -111,7 +113,8 @@ func (suite *StatusCreateTestSuite) TestProcessStatusMarkdownWithSpoilerTextEmoj
|
|||
ContentType: apimodel.StatusContentTypeMarkdown,
|
||||
}
|
||||
|
||||
apiStatus, err := suite.status.Create(ctx, creatingAccount, creatingApplication, statusCreateForm)
|
||||
apiStatusAny, err := suite.status.Create(ctx, creatingAccount, creatingApplication, statusCreateForm, nil)
|
||||
apiStatus := apiStatusAny.(*apimodel.Status)
|
||||
suite.NoError(err)
|
||||
suite.NotNil(apiStatus)
|
||||
|
||||
|
|
@ -142,7 +145,7 @@ func (suite *StatusCreateTestSuite) TestProcessMediaDescriptionTooShort() {
|
|||
ContentType: apimodel.StatusContentTypePlain,
|
||||
}
|
||||
|
||||
apiStatus, err := suite.status.Create(ctx, creatingAccount, creatingApplication, statusCreateForm)
|
||||
apiStatus, err := suite.status.Create(ctx, creatingAccount, creatingApplication, statusCreateForm, nil)
|
||||
suite.EqualError(err, "media description less than min chars (100)")
|
||||
suite.Nil(apiStatus)
|
||||
}
|
||||
|
|
@ -167,7 +170,8 @@ func (suite *StatusCreateTestSuite) TestProcessLanguageWithScriptPart() {
|
|||
ContentType: apimodel.StatusContentTypePlain,
|
||||
}
|
||||
|
||||
apiStatus, err := suite.status.Create(ctx, creatingAccount, creatingApplication, statusCreateForm)
|
||||
apiStatusAny, err := suite.status.Create(ctx, creatingAccount, creatingApplication, statusCreateForm, nil)
|
||||
apiStatus := apiStatusAny.(*apimodel.Status)
|
||||
suite.NoError(err)
|
||||
suite.NotNil(apiStatus)
|
||||
|
||||
|
|
@ -197,7 +201,8 @@ func (suite *StatusCreateTestSuite) TestProcessReplyToUnthreadedRemoteStatus() {
|
|||
ContentType: apimodel.StatusContentTypePlain,
|
||||
}
|
||||
|
||||
apiStatus, err := suite.status.Create(ctx, creatingAccount, creatingApplication, statusCreateForm)
|
||||
apiStatusAny, err := suite.status.Create(ctx, creatingAccount, creatingApplication, statusCreateForm, nil)
|
||||
apiStatus := apiStatusAny.(*apimodel.Status)
|
||||
suite.NoError(err)
|
||||
suite.NotNil(apiStatus)
|
||||
|
||||
|
|
@ -230,7 +235,8 @@ func (suite *StatusCreateTestSuite) TestProcessNoContentTypeUsesDefault() {
|
|||
ContentType: "",
|
||||
}
|
||||
|
||||
apiStatus, errWithCode := suite.status.Create(ctx, creatingAccount, creatingApplication, statusCreateForm)
|
||||
apiStatusAny, errWithCode := suite.status.Create(ctx, creatingAccount, creatingApplication, statusCreateForm, nil)
|
||||
apiStatus := apiStatusAny.(*apimodel.Status)
|
||||
suite.NoError(errWithCode)
|
||||
suite.NotNil(apiStatus)
|
||||
|
||||
|
|
@ -260,7 +266,7 @@ func (suite *StatusCreateTestSuite) TestProcessInvalidVisibility() {
|
|||
ContentType: apimodel.StatusContentTypePlain,
|
||||
}
|
||||
|
||||
apiStatus, errWithCode := suite.status.Create(ctx, creatingAccount, creatingApplication, statusCreateForm)
|
||||
apiStatus, errWithCode := suite.status.Create(ctx, creatingAccount, creatingApplication, statusCreateForm, nil)
|
||||
suite.Nil(apiStatus)
|
||||
suite.Equal(http.StatusUnprocessableEntity, errWithCode.Code())
|
||||
suite.Equal("Unprocessable Entity: processVisibility: invalid visibility", errWithCode.Safe())
|
||||
|
|
|
|||
|
|
@ -110,6 +110,7 @@ func (p *Processor) Edit(
|
|||
requester.ID,
|
||||
statusID,
|
||||
form.MediaIDs,
|
||||
nil,
|
||||
)
|
||||
if errWithCode != nil {
|
||||
return nil, errWithCode
|
||||
|
|
|
|||
357
internal/processing/status/scheduledstatus.go
Normal file
357
internal/processing/status/scheduledstatus.go
Normal file
|
|
@ -0,0 +1,357 @@
|
|||
// 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
package status
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
apimodel "code.superseriousbusiness.org/gotosocial/internal/api/model"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/config"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/db"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/gtscontext"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/gtserror"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/gtsmodel"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/log"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/paging"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/typeutils"
|
||||
)
|
||||
|
||||
// ScheduledStatusesGetPage returns a page of scheduled statuses authored
|
||||
// by the requester.
|
||||
func (p *Processor) ScheduledStatusesGetPage(
|
||||
ctx context.Context,
|
||||
requester *gtsmodel.Account,
|
||||
page *paging.Page,
|
||||
) (*apimodel.PageableResponse, gtserror.WithCode) {
|
||||
scheduledStatuses, err := p.state.DB.GetScheduledStatusesForAcct(
|
||||
ctx,
|
||||
requester.ID,
|
||||
page,
|
||||
)
|
||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||
err := gtserror.Newf("db error getting scheduled statuses: %w", err)
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
count := len(scheduledStatuses)
|
||||
if count == 0 {
|
||||
return paging.EmptyResponse(), nil
|
||||
}
|
||||
|
||||
var (
|
||||
// Get the lowest and highest
|
||||
// ID values, used for paging.
|
||||
lo = scheduledStatuses[count-1].ID
|
||||
hi = scheduledStatuses[0].ID
|
||||
|
||||
// Best-guess items length.
|
||||
items = make([]interface{}, 0, count)
|
||||
)
|
||||
|
||||
for _, scheduledStatus := range scheduledStatuses {
|
||||
apiScheduledStatus, err := p.converter.ScheduledStatusToAPIScheduledStatus(
|
||||
ctx, scheduledStatus,
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf(ctx, "error converting scheduled status to api scheduled status: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Append scheduledStatus to return items.
|
||||
items = append(items, apiScheduledStatus)
|
||||
}
|
||||
|
||||
return paging.PackageResponse(paging.ResponseParams{
|
||||
Items: items,
|
||||
Path: "/api/v1/scheduled_statuses",
|
||||
Next: page.Next(lo, hi),
|
||||
Prev: page.Prev(lo, hi),
|
||||
}), nil
|
||||
}
|
||||
|
||||
// ScheduledStatusesGetOne returns one scheduled
|
||||
// status with the given ID.
|
||||
func (p *Processor) ScheduledStatusesGetOne(
|
||||
ctx context.Context,
|
||||
requester *gtsmodel.Account,
|
||||
id string,
|
||||
) (*apimodel.ScheduledStatus, gtserror.WithCode) {
|
||||
scheduledStatus, err := p.state.DB.GetScheduledStatusByID(ctx, id)
|
||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||
err := gtserror.Newf("db error getting scheduled status: %w", err)
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
if scheduledStatus == nil {
|
||||
err := gtserror.New("scheduled status not found")
|
||||
return nil, gtserror.NewErrorNotFound(err)
|
||||
}
|
||||
|
||||
if scheduledStatus.AccountID != requester.ID {
|
||||
err := gtserror.Newf(
|
||||
"scheduled status %s is not authored by account %s",
|
||||
scheduledStatus.ID, requester.ID,
|
||||
)
|
||||
return nil, gtserror.NewErrorNotFound(err)
|
||||
}
|
||||
|
||||
apiScheduledStatus, err := p.converter.ScheduledStatusToAPIScheduledStatus(
|
||||
ctx, scheduledStatus,
|
||||
)
|
||||
if err != nil {
|
||||
err := gtserror.Newf("error converting scheduled status to api scheduled status: %w", err)
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
return apiScheduledStatus, nil
|
||||
}
|
||||
|
||||
func (p *Processor) ScheduledStatusesScheduleAll(ctx context.Context) error {
|
||||
// Fetch all pending statuses from the database (barebones models are enough).
|
||||
statuses, err := p.state.DB.GetAllScheduledStatuses(gtscontext.SetBarebones(ctx))
|
||||
if err != nil {
|
||||
return gtserror.Newf("error getting scheduled statuses from db: %w", err)
|
||||
}
|
||||
|
||||
var errs gtserror.MultiError
|
||||
|
||||
for _, status := range statuses {
|
||||
// Schedule publication of each of the statuses and catch any errors.
|
||||
if err := p.ScheduledStatusesSchedulePublication(ctx, status.ID); err != nil {
|
||||
errs.Append(err)
|
||||
}
|
||||
}
|
||||
|
||||
return errs.Combine()
|
||||
}
|
||||
|
||||
func (p *Processor) ScheduledStatusesSchedulePublication(ctx context.Context, statusID string) gtserror.WithCode {
|
||||
status, err := p.state.DB.GetScheduledStatusByID(ctx, statusID)
|
||||
|
||||
if err != nil {
|
||||
return gtserror.NewErrorNotFound(gtserror.Newf("failed to get scheduled status %s", statusID))
|
||||
}
|
||||
|
||||
// Add the given status to the scheduler.
|
||||
ok := p.state.Workers.Scheduler.AddOnce(
|
||||
status.ID,
|
||||
status.ScheduledAt,
|
||||
p.onPublish(status.ID),
|
||||
)
|
||||
|
||||
if !ok {
|
||||
// Failed to add the status to the scheduler, either it was
|
||||
// starting / stopping or there already exists a task for status.
|
||||
return gtserror.NewErrorInternalError(gtserror.Newf("failed adding status %s to scheduler", status.ID))
|
||||
}
|
||||
|
||||
atStr := status.ScheduledAt.Local().Format("Jan _2 2006 15:04:05")
|
||||
log.Infof(ctx, "scheduled status publication for %s at '%s'", status.ID, atStr)
|
||||
return nil
|
||||
}
|
||||
|
||||
// onPublish returns a callback function to be used by the scheduler on the scheduled date.
|
||||
func (p *Processor) onPublish(statusID string) func(context.Context, time.Time) {
|
||||
return func(ctx context.Context, now time.Time) {
|
||||
// Get the latest version of status from database.
|
||||
status, err := p.state.DB.GetScheduledStatusByID(ctx, statusID)
|
||||
if err != nil {
|
||||
log.Errorf(ctx, "error getting status %s from db: %v", statusID, err)
|
||||
return
|
||||
}
|
||||
|
||||
request := &apimodel.StatusCreateRequest{
|
||||
Status: status.Text,
|
||||
MediaIDs: status.MediaIDs,
|
||||
Poll: nil,
|
||||
InReplyToID: status.InReplyToID,
|
||||
Sensitive: *status.Sensitive,
|
||||
SpoilerText: status.SpoilerText,
|
||||
Visibility: typeutils.VisToAPIVis(status.Visibility),
|
||||
Language: status.Language,
|
||||
}
|
||||
|
||||
if status.Poll.Options != nil && len(status.Poll.Options) > 1 {
|
||||
request.Poll = &apimodel.PollRequest{
|
||||
Options: status.Poll.Options,
|
||||
ExpiresIn: status.Poll.ExpiresIn,
|
||||
Multiple: *status.Poll.Multiple,
|
||||
HideTotals: *status.Poll.HideTotals,
|
||||
}
|
||||
}
|
||||
|
||||
_, errWithCode := p.Create(ctx, status.Account, status.Application, request, &statusID)
|
||||
|
||||
if errWithCode != nil {
|
||||
log.Errorf(ctx, "could not publish scheduled status: %v", errWithCode.Unwrap())
|
||||
return
|
||||
}
|
||||
|
||||
err = p.state.DB.DeleteScheduledStatusByID(ctx, statusID)
|
||||
|
||||
if err != nil {
|
||||
log.Error(ctx, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update scheduled status schedule data
|
||||
func (p *Processor) ScheduledStatusesUpdate(
|
||||
ctx context.Context,
|
||||
requester *gtsmodel.Account,
|
||||
id string,
|
||||
scheduledAt *time.Time,
|
||||
) (*apimodel.ScheduledStatus, gtserror.WithCode) {
|
||||
scheduledStatus, err := p.state.DB.GetScheduledStatusByID(ctx, id)
|
||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||
err := gtserror.Newf("db error getting scheduled status: %w", err)
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
if scheduledStatus == nil {
|
||||
err := gtserror.New("scheduled status not found")
|
||||
return nil, gtserror.NewErrorNotFound(err)
|
||||
}
|
||||
|
||||
if scheduledStatus.AccountID != requester.ID {
|
||||
err := gtserror.Newf(
|
||||
"scheduled status %s is not authored by account %s",
|
||||
scheduledStatus.ID, requester.ID,
|
||||
)
|
||||
return nil, gtserror.NewErrorNotFound(err)
|
||||
}
|
||||
|
||||
if errWithCode := p.validateScheduledStatusLimits(ctx, requester.ID, scheduledAt, &scheduledStatus.ScheduledAt); errWithCode != nil {
|
||||
return nil, errWithCode
|
||||
}
|
||||
|
||||
scheduledStatus.ScheduledAt = *scheduledAt
|
||||
err = p.state.DB.UpdateScheduledStatusScheduledDate(ctx, scheduledStatus, scheduledAt)
|
||||
|
||||
if err != nil {
|
||||
err := gtserror.Newf("db error getting scheduled status: %w", err)
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
ok := p.state.Workers.Scheduler.Cancel(id)
|
||||
|
||||
if !ok {
|
||||
err := gtserror.Newf("failed to cancel scheduled status")
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
err = p.ScheduledStatusesSchedulePublication(ctx, id)
|
||||
|
||||
if err != nil {
|
||||
err := gtserror.Newf("error scheduling status: %w", err)
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
apiScheduledStatus, err := p.converter.ScheduledStatusToAPIScheduledStatus(
|
||||
ctx, scheduledStatus,
|
||||
)
|
||||
if err != nil {
|
||||
err := gtserror.Newf("error converting scheduled status to api req: %w", err)
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
return apiScheduledStatus, nil
|
||||
}
|
||||
|
||||
// Cancel a scheduled status
|
||||
func (p *Processor) ScheduledStatusesDelete(ctx context.Context, requester *gtsmodel.Account, id string) gtserror.WithCode {
|
||||
scheduledStatus, err := p.state.DB.GetScheduledStatusByID(ctx, id)
|
||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||
err := gtserror.Newf("db error getting scheduled status: %w", err)
|
||||
return gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
if scheduledStatus == nil {
|
||||
err := gtserror.New("scheduled status not found")
|
||||
return gtserror.NewErrorNotFound(err)
|
||||
}
|
||||
|
||||
if scheduledStatus.AccountID != requester.ID {
|
||||
err := gtserror.Newf(
|
||||
"scheduled status %s is not authored by account %s",
|
||||
scheduledStatus.ID, requester.ID,
|
||||
)
|
||||
return gtserror.NewErrorNotFound(err)
|
||||
}
|
||||
|
||||
ok := p.state.Workers.Scheduler.Cancel(id)
|
||||
|
||||
if !ok {
|
||||
err := gtserror.Newf("failed to cancel scheduled status")
|
||||
return gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
err = p.state.DB.DeleteScheduledStatusByID(ctx, id)
|
||||
|
||||
if err != nil {
|
||||
err := gtserror.Newf("db error deleting scheduled status: %w", err)
|
||||
return gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Processor) validateScheduledStatusLimits(ctx context.Context, acctID string, scheduledAt *time.Time, prevScheduledAt *time.Time) gtserror.WithCode {
|
||||
// Skip check when the scheduled status already exists and the day stays the same
|
||||
if prevScheduledAt != nil {
|
||||
y1, m1, d1 := scheduledAt.Date()
|
||||
y2, m2, d2 := prevScheduledAt.Date()
|
||||
|
||||
if y1 == y2 && m1 == m2 && d1 == d2 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
scheduledDaily, err := p.state.DB.GetScheduledStatusesCountForAcct(ctx, acctID, scheduledAt)
|
||||
|
||||
if err != nil {
|
||||
err := gtserror.Newf("error getting scheduled statuses count for day: %w", err)
|
||||
return gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
if max := config.GetScheduledStatusesMaxDaily(); scheduledDaily >= max {
|
||||
err := gtserror.Newf("scheduled statuses count for day is at the limit (%d)", max)
|
||||
return gtserror.NewErrorUnprocessableEntity(err)
|
||||
}
|
||||
|
||||
// Skip total check when editing an existing scheduled status
|
||||
if prevScheduledAt != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
scheduledTotal, err := p.state.DB.GetScheduledStatusesCountForAcct(ctx, acctID, nil)
|
||||
|
||||
if err != nil {
|
||||
err := gtserror.Newf("error getting total scheduled statuses count: %w", err)
|
||||
return gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
if max := config.GetScheduledStatusesMaxTotal(); scheduledTotal >= max {
|
||||
err := gtserror.Newf("total scheduled statuses count is at the limit (%d)", max)
|
||||
return gtserror.NewErrorUnprocessableEntity(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
69
internal/processing/status/scheduledstatus_test.go
Normal file
69
internal/processing/status/scheduledstatus_test.go
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
// 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
package status_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"code.superseriousbusiness.org/gotosocial/internal/util"
|
||||
"code.superseriousbusiness.org/gotosocial/testrig"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type ScheduledStatusTestSuite struct {
|
||||
StatusStandardTestSuite
|
||||
}
|
||||
|
||||
func (suite *ScheduledStatusTestSuite) TestUpdate() {
|
||||
ctx := suite.T().Context()
|
||||
|
||||
account1 := suite.testAccounts["local_account_1"]
|
||||
scheduledStatus1 := suite.testScheduledStatuses["scheduled_status_1"]
|
||||
newScheduledAt := testrig.TimeMustParse("2080-07-02T21:37:00+02:00")
|
||||
|
||||
suite.state.Workers.Scheduler.AddOnce(scheduledStatus1.ID, scheduledStatus1.ScheduledAt, func(ctx context.Context, t time.Time) {})
|
||||
|
||||
// update scheduled status publication date
|
||||
scheduledStatus2, err := suite.status.ScheduledStatusesUpdate(ctx, account1, scheduledStatus1.ID, util.Ptr(newScheduledAt))
|
||||
suite.NoError(err)
|
||||
suite.NotNil(scheduledStatus2)
|
||||
suite.Equal(scheduledStatus2.ScheduledAt, util.FormatISO8601(newScheduledAt))
|
||||
// should be rescheduled
|
||||
suite.Equal(suite.state.Workers.Scheduler.Cancel(scheduledStatus1.ID), true)
|
||||
}
|
||||
|
||||
func (suite *ScheduledStatusTestSuite) TestDelete() {
|
||||
ctx := suite.T().Context()
|
||||
|
||||
account1 := suite.testAccounts["local_account_1"]
|
||||
scheduledStatus1 := suite.testScheduledStatuses["scheduled_status_1"]
|
||||
|
||||
suite.state.Workers.Scheduler.AddOnce(scheduledStatus1.ID, scheduledStatus1.ScheduledAt, func(ctx context.Context, t time.Time) {})
|
||||
|
||||
// delete scheduled status
|
||||
err := suite.status.ScheduledStatusesDelete(ctx, account1, scheduledStatus1.ID)
|
||||
suite.NoError(err)
|
||||
// should be already cancelled
|
||||
suite.Equal(suite.state.Workers.Scheduler.Cancel(scheduledStatus1.ID), false)
|
||||
}
|
||||
|
||||
func TestScheduledStatusTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(ScheduledStatusTestSuite))
|
||||
}
|
||||
|
|
@ -51,14 +51,15 @@ type StatusStandardTestSuite struct {
|
|||
federator *federation.Federator
|
||||
|
||||
// standard suite models
|
||||
testTokens map[string]*gtsmodel.Token
|
||||
testApplications map[string]*gtsmodel.Application
|
||||
testUsers map[string]*gtsmodel.User
|
||||
testAccounts map[string]*gtsmodel.Account
|
||||
testAttachments map[string]*gtsmodel.MediaAttachment
|
||||
testStatuses map[string]*gtsmodel.Status
|
||||
testTags map[string]*gtsmodel.Tag
|
||||
testMentions map[string]*gtsmodel.Mention
|
||||
testTokens map[string]*gtsmodel.Token
|
||||
testApplications map[string]*gtsmodel.Application
|
||||
testUsers map[string]*gtsmodel.User
|
||||
testAccounts map[string]*gtsmodel.Account
|
||||
testAttachments map[string]*gtsmodel.MediaAttachment
|
||||
testStatuses map[string]*gtsmodel.Status
|
||||
testTags map[string]*gtsmodel.Tag
|
||||
testMentions map[string]*gtsmodel.Mention
|
||||
testScheduledStatuses map[string]*gtsmodel.ScheduledStatus
|
||||
|
||||
// module being tested
|
||||
status status.Processor
|
||||
|
|
@ -73,6 +74,7 @@ func (suite *StatusStandardTestSuite) SetupSuite() {
|
|||
suite.testStatuses = testrig.NewTestStatuses()
|
||||
suite.testTags = testrig.NewTestTags()
|
||||
suite.testMentions = testrig.NewTestMentions()
|
||||
suite.testScheduledStatuses = testrig.NewTestScheduledStatuses()
|
||||
}
|
||||
|
||||
func (suite *StatusStandardTestSuite) SetupTest() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue