diff options
author | 2023-11-08 14:32:17 +0000 | |
---|---|---|
committer | 2023-11-08 14:32:17 +0000 | |
commit | e9e5dc5a40926e5320cb131b035c46b1e1b0bd59 (patch) | |
tree | 52edc9fa5742f28e1e5223f51cda628ec1c35a24 /internal/processing/status/create.go | |
parent | [chore]: Bump github.com/spf13/cobra from 1.7.0 to 1.8.0 (#2338) (diff) | |
download | gotosocial-e9e5dc5a40926e5320cb131b035c46b1e1b0bd59.tar.xz |
[feature] add support for polls + receiving federated status edits (#2330)
Diffstat (limited to 'internal/processing/status/create.go')
-rw-r--r-- | internal/processing/status/create.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/internal/processing/status/create.go b/internal/processing/status/create.go index 40b3f2df2..fbe1fbd64 100644 --- a/internal/processing/status/create.go +++ b/internal/processing/status/create.go @@ -66,6 +66,26 @@ func (p *Processor) Create(ctx context.Context, requestingAccount *gtsmodel.Acco Text: form.Status, } + if form.Poll != nil { + // Update the status AS type to "Question". + status.ActivityStreamsType = ap.ActivityQuestion + + // Create new poll for status from form. + secs := time.Duration(form.Poll.ExpiresIn) + status.Poll = >smodel.Poll{ + ID: id.NewULID(), + Multiple: &form.Poll.Multiple, + HideCounts: &form.Poll.HideTotals, + Options: form.Poll.Options, + StatusID: statusID, + Status: status, + ExpiresAt: now.Add(secs * time.Second), + } + + // Set poll ID on the status. + status.PollID = status.Poll.ID + } + if errWithCode := p.processReplyToID(ctx, form, requestingAccount.ID, status); errWithCode != nil { return nil, errWithCode } @@ -90,6 +110,14 @@ func (p *Processor) Create(ctx context.Context, requestingAccount *gtsmodel.Acco return nil, gtserror.NewErrorInternalError(err) } + if status.Poll != nil { + // Try to insert the new status poll in the database. + if err := p.state.DB.PutPoll(ctx, status.Poll); err != nil { + err := gtserror.Newf("error inserting poll in db: %w", err) + return nil, gtserror.NewErrorInternalError(err) + } + } + // Insert this new status in the database. if err := p.state.DB.PutStatus(ctx, status); err != nil { return nil, gtserror.NewErrorInternalError(err) @@ -103,6 +131,15 @@ func (p *Processor) Create(ctx context.Context, requestingAccount *gtsmodel.Acco OriginAccount: requestingAccount, }) + if status.Poll != nil { + // Now that the status is inserted, and side effects queued, + // attempt to schedule an expiry handler for the status poll. + if err := p.polls.ScheduleExpiry(ctx, status.Poll); err != nil { + err := gtserror.Newf("error scheduling poll expiry: %w", err) + return nil, gtserror.NewErrorInternalError(err) + } + } + return p.c.GetAPIStatus(ctx, requestingAccount, status) } @@ -370,6 +407,18 @@ func (p *Processor) processContent(ctx context.Context, parseMention gtsmodel.Pa status.ContentWarning = warningRes.HTML status.Emojis = append(status.Emojis, warningRes.Emojis...) + if status.Poll != nil { + for i := range status.Poll.Options { + // Sanitize each option title name and format. + option := text.SanitizeToPlaintext(status.Poll.Options[i]) + optionRes := formatInput(format, option) + + // Collect each formatted result. + status.Poll.Options[i] = optionRes.HTML + status.Emojis = append(status.Emojis, optionRes.Emojis...) + } + } + // Gather all the database IDs from each of the gathered status mentions, tags, and emojis. status.MentionIDs = gatherIDs(status.Mentions, func(mention *gtsmodel.Mention) string { return mention.ID }) status.TagIDs = gatherIDs(status.Tags, func(tag *gtsmodel.Tag) string { return tag.ID }) |