1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
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
}
|