summaryrefslogtreecommitdiff
path: root/internal/message/statusprocess.go
blob: d9d115aece562d1c28ca355892f450fee20bd4d8 (plain)
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package message

import (
	"errors"
	"fmt"
	"time"

	"github.com/google/uuid"
	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
	"github.com/superseriousbusiness/gotosocial/internal/oauth"
	"github.com/superseriousbusiness/gotosocial/internal/util"
)

func (p *processor) StatusCreate(auth *oauth.Auth, form *apimodel.AdvancedStatusCreateForm) (*apimodel.Status, error) {
	uris := util.GenerateURIsForAccount(auth.Account.Username, p.config.Protocol, p.config.Host)
	thisStatusID := uuid.NewString()
	thisStatusURI := fmt.Sprintf("%s/%s", uris.StatusesURI, thisStatusID)
	thisStatusURL := fmt.Sprintf("%s/%s", uris.StatusesURL, thisStatusID)
	newStatus := &gtsmodel.Status{
		ID:                       thisStatusID,
		URI:                      thisStatusURI,
		URL:                      thisStatusURL,
		Content:                  util.HTMLFormat(form.Status),
		CreatedAt:                time.Now(),
		UpdatedAt:                time.Now(),
		Local:                    true,
		AccountID:                auth.Account.ID,
		ContentWarning:           form.SpoilerText,
		ActivityStreamsType:      gtsmodel.ActivityStreamsNote,
		Sensitive:                form.Sensitive,
		Language:                 form.Language,
		CreatedWithApplicationID: auth.Application.ID,
		Text:                     form.Status,
	}

	// check if replyToID is ok
	if err := p.processReplyToID(form, auth.Account.ID, newStatus); err != nil {
		return nil, err
	}

	// check if mediaIDs are ok
	if err := p.processMediaIDs(form, auth.Account.ID, newStatus); err != nil {
		return nil, err
	}

	// check if visibility settings are ok
	if err := p.processVisibility(form, auth.Account.Privacy, newStatus); err != nil {
		return nil, err
	}

	// handle language settings
	if err := p.processLanguage(form, auth.Account.Language, newStatus); err != nil {
		return nil, err
	}

	// handle mentions
	if err := p.processMentions(form, auth.Account.ID, newStatus); err != nil {
		return nil, err
	}

	if err := p.processTags(form, auth.Account.ID, newStatus); err != nil {
		return nil, err
	}

	if err := p.processEmojis(form, auth.Account.ID, newStatus); err != nil {
		return nil, err
	}

	// put the new status in the database, generating an ID for it in the process
	if err := p.db.Put(newStatus); err != nil {
		return nil, err
	}

	// change the status ID of the media attachments to the new status
	for _, a := range newStatus.GTSMediaAttachments {
		a.StatusID = newStatus.ID
		a.UpdatedAt = time.Now()
		if err := p.db.UpdateByID(a.ID, a); err != nil {
			return nil, err
		}
	}

	// put the new status in the appropriate channel for async processing
	p.fromClientAPI <- FromClientAPI{
		APObjectType:   newStatus.ActivityStreamsType,
		APActivityType: gtsmodel.ActivityStreamsCreate,
		Activity:       newStatus,
	}

	// return the frontend representation of the new status to the submitter
	return p.tc.StatusToMasto(newStatus, auth.Account, auth.Account, nil, newStatus.GTSReplyToAccount, nil)
}

func (p *processor) StatusDelete(authed *oauth.Auth, targetStatusID string) (*apimodel.Status, error) {
	l := p.log.WithField("func", "StatusDelete")
	l.Tracef("going to search for target status %s", targetStatusID)
	targetStatus := &gtsmodel.Status{}
	if err := p.db.GetByID(targetStatusID, targetStatus); err != nil {
		return nil, fmt.Errorf("error fetching status %s: %s", targetStatusID, err)
	}

	if targetStatus.AccountID != authed.Account.ID {
		return nil, errors.New("status doesn't belong to requesting account")
	}

	l.Trace("going to get relevant accounts")
	relevantAccounts, err := p.db.PullRelevantAccountsFromStatus(targetStatus)
	if err != nil {
		return nil, fmt.Errorf("error fetching related accounts for status %s: %s", targetStatusID, err)
	}

	var boostOfStatus *gtsmodel.Status
	if targetStatus.BoostOfID != "" {
		boostOfStatus = &gtsmodel.Status{}
		if err := p.db.GetByID(targetStatus.BoostOfID, boostOfStatus); err != nil {
			return nil, fmt.Errorf("error fetching boosted status %s: %s", targetStatus.BoostOfID, err)
		}
	}

	mastoStatus, err := p.tc.StatusToMasto(targetStatus, authed.Account, authed.Account, relevantAccounts.BoostedAccount, relevantAccounts.ReplyToAccount, boostOfStatus)
	if err != nil {
		return nil, fmt.Errorf("error converting status %s to frontend representation: %s", targetStatus.ID, err)
	}

	if err := p.db.DeleteByID(targetStatus.ID, targetStatus); err != nil {
		return nil, fmt.Errorf("error deleting status from the database: %s", err)
	}

	return mastoStatus, nil
}

func (p *processor) StatusFave(authed *oauth.Auth, targetStatusID string) (*apimodel.Status, error) {
	l := p.log.WithField("func", "StatusFave")
	l.Tracef("going to search for target status %s", targetStatusID)
	targetStatus := &gtsmodel.Status{}
	if err := p.db.GetByID(targetStatusID, targetStatus); err != nil {
		return nil, fmt.Errorf("error fetching status %s: %s", targetStatusID, err)
	}

	l.Tracef("going to search for target account %s", targetStatus.AccountID)
	targetAccount := &gtsmodel.Account{}
	if err := p.db.GetByID(targetStatus.AccountID, targetAccount); err != nil {
		return nil, fmt.Errorf("error fetching target account %s: %s", targetStatus.AccountID, err)
	}

	l.Trace("going to get relevant accounts")
	relevantAccounts, err := p.db.PullRelevantAccountsFromStatus(targetStatus)
	if err != nil {
		return nil, fmt.Errorf("error fetching related accounts for status %s: %s", targetStatusID, err)
	}

	l.Trace("going to see if status is visible")
	visible, err := p.db.StatusVisible(targetStatus, targetAccount, authed.Account, relevantAccounts) // requestingAccount might well be nil here, but StatusVisible knows how to take care of that
	if err != nil {
		return nil, fmt.Errorf("error seeing if status %s is visible: %s", targetStatus.ID, err)
	}

	if !visible {
		return nil, errors.New("status is not visible")
	}

	// is the status faveable?
	if !targetStatus.VisibilityAdvanced.Likeable {
		return nil, errors.New("status is not faveable")
	}

	// it's visible! it's faveable! so let's fave the FUCK out of it
	_, err = p.db.FaveStatus(targetStatus, authed.Account.ID)
	if err != nil {
		return nil, fmt.Errorf("error faveing status: %s", err)
	}

	var boostOfStatus *gtsmodel.Status
	if targetStatus.BoostOfID != "" {
		boostOfStatus = &gtsmodel.Status{}
		if err := p.db.GetByID(targetStatus.BoostOfID, boostOfStatus); err != nil {
			return nil, fmt.Errorf("error fetching boosted status %s: %s", targetStatus.BoostOfID, err)
		}
	}

	mastoStatus, err := p.tc.StatusToMasto(targetStatus, targetAccount, authed.Account, relevantAccounts.BoostedAccount, relevantAccounts.ReplyToAccount, boostOfStatus)
	if err != nil {
		return nil, fmt.Errorf("error converting status %s to frontend representation: %s", targetStatus.ID, err)
	}

	return mastoStatus, nil
}

func (p *processor) StatusBoost(authed *oauth.Auth, targetStatusID string) (*apimodel.Status, ErrorWithCode) {
	l := p.log.WithField("func", "StatusBoost")

	l.Tracef("going to search for target status %s", targetStatusID)
	targetStatus := &gtsmodel.Status{}
	if err := p.db.GetByID(targetStatusID, targetStatus); err != nil {
		return nil, NewErrorNotFound(fmt.Errorf("error fetching status %s: %s", targetStatusID, err))
	}

	l.Tracef("going to search for target account %s", targetStatus.AccountID)
	targetAccount := &gtsmodel.Account{}
	if err := p.db.GetByID(targetStatus.AccountID, targetAccount); err != nil {
		return nil, NewErrorNotFound(fmt.Errorf("error fetching target account %s: %s", targetStatus.AccountID, err))
	}

	l.Trace("going to get relevant accounts")
	relevantAccounts, err := p.db.PullRelevantAccountsFromStatus(targetStatus)
	if err != nil {
		return nil, NewErrorNotFound(fmt.Errorf("error fetching related accounts for status %s: %s", targetStatusID, err))
	}

	l.Trace("going to see if status is visible")
	visible, err := p.db.StatusVisible(targetStatus, targetAccount, authed.Account, relevantAccounts) // requestingAccount might well be nil here, but StatusVisible knows how to take care of that
	if err != nil {
		return nil, NewErrorNotFound(fmt.Errorf("error seeing if status %s is visible: %s", targetStatus.ID, err))
	}

	if !visible {
		return nil, NewErrorNotFound(errors.New("status is not visible"))
	}

	if !targetStatus.VisibilityAdvanced.Boostable {
		return nil, NewErrorForbidden(errors.New("status is not boostable"))
	}

	// it's visible! it's boostable! so let's boost the FUCK out of it
	// first we create a new status and add some basic info to it -- this will be the wrapper for the boosted status

	// the wrapper won't use the same ID as the boosted status so we generate some new UUIDs
	uris := util.GenerateURIsForAccount(authed.Account.Username, p.config.Protocol, p.config.Host)
	boostWrapperStatusID := uuid.NewString()
	boostWrapperStatusURI := fmt.Sprintf("%s/%s", uris.StatusesURI, boostWrapperStatusID)
	boostWrapperStatusURL := fmt.Sprintf("%s/%s", uris.StatusesURL, boostWrapperStatusID)

	boostWrapperStatus := &gtsmodel.Status{
		ID:  boostWrapperStatusID,
		URI: boostWrapperStatusURI,
		URL: boostWrapperStatusURL,

		// the boosted status is not created now, but the boost certainly is
		CreatedAt:                time.Now(),
		UpdatedAt:                time.Now(),
		Local:                    true, // always local since this is being done through the client API
		AccountID:                authed.Account.ID,
		CreatedWithApplicationID: authed.Application.ID,

		// replies can be boosted, but boosts are never replies
		InReplyToID:        "",
		InReplyToAccountID: "",

		// these will all be wrapped in the boosted status so set them empty here
		Attachments: []string{},
		Tags:        []string{},
		Mentions:    []string{},
		Emojis:      []string{},

		// the below fields will be taken from the target status
		Content:             util.HTMLFormat(targetStatus.Content),
		ContentWarning:      targetStatus.ContentWarning,
		ActivityStreamsType: targetStatus.ActivityStreamsType,
		Sensitive:           targetStatus.Sensitive,
		Language:            targetStatus.Language,
		Text:                targetStatus.Text,
		BoostOfID:           targetStatus.ID,
		Visibility:          targetStatus.Visibility,
		VisibilityAdvanced:  targetStatus.VisibilityAdvanced,

		// attach these here for convenience -- the boosted status/account won't go in the DB
		// but they're needed in the processor and for the frontend. Since we have them, we can
		// attach them so we don't need to fetch them again later (save some DB calls)
		GTSBoostedStatus:  targetStatus,
		GTSBoostedAccount: targetAccount,
	}

	// put the boost in the database
	if err := p.db.Put(boostWrapperStatus); err != nil {
		return nil, NewErrorInternalError(err)
	}

	// return the frontend representation of the new status to the submitter
	mastoStatus, err := p.tc.StatusToMasto(boostWrapperStatus, authed.Account, authed.Account, targetAccount, nil, targetStatus)
	if err != nil {
		return nil, NewErrorInternalError(fmt.Errorf("error converting status %s to frontend representation: %s", targetStatus.ID, err))
	}

	return mastoStatus, nil
}

func (p *processor) StatusFavedBy(authed *oauth.Auth, targetStatusID string) ([]*apimodel.Account, error) {
	l := p.log.WithField("func", "StatusFavedBy")

	l.Tracef("going to search for target status %s", targetStatusID)
	targetStatus := &gtsmodel.Status{}
	if err := p.db.GetByID(targetStatusID, targetStatus); err != nil {
		return nil, fmt.Errorf("error fetching status %s: %s", targetStatusID, err)
	}

	l.Tracef("going to search for target account %s", targetStatus.AccountID)
	targetAccount := &gtsmodel.Account{}
	if err := p.db.GetByID(targetStatus.AccountID, targetAccount); err != nil {
		return nil, fmt.Errorf("error fetching target account %s: %s", targetStatus.AccountID, err)
	}

	l.Trace("going to get relevant accounts")
	relevantAccounts, err := p.db.PullRelevantAccountsFromStatus(targetStatus)
	if err != nil {
		return nil, fmt.Errorf("error fetching related accounts for status %s: %s", targetStatusID, err)
	}

	l.Trace("going to see if status is visible")
	visible, err := p.db.StatusVisible(targetStatus, targetAccount, authed.Account, relevantAccounts) // requestingAccount might well be nil here, but StatusVisible knows how to take care of that
	if err != nil {
		return nil, fmt.Errorf("error seeing if status %s is visible: %s", targetStatus.ID, err)
	}

	if !visible {
		return nil, errors.New("status is not visible")
	}

	// get ALL accounts that faved a status -- doesn't take account of blocks and mutes and stuff
	favingAccounts, err := p.db.WhoFavedStatus(targetStatus)
	if err != nil {
		return nil, fmt.Errorf("error seeing who faved status: %s", err)
	}

	// filter the list so the user doesn't see accounts they blocked or which blocked them
	filteredAccounts := []*gtsmodel.Account{}
	for _, acc := range favingAccounts {
		blocked, err := p.db.Blocked(authed.Account.ID, acc.ID)
		if err != nil {
			return nil, fmt.Errorf("error checking blocks: %s", err)
		}
		if !blocked {
			filteredAccounts = append(filteredAccounts, acc)
		}
	}

	// TODO: filter other things here? suspended? muted? silenced?

	// now we can return the masto representation of those accounts
	mastoAccounts := []*apimodel.Account{}
	for _, acc := range filteredAccounts {
		mastoAccount, err := p.tc.AccountToMastoPublic(acc)
		if err != nil {
			return nil, fmt.Errorf("error converting account to api model: %s", err)
		}
		mastoAccounts = append(mastoAccounts, mastoAccount)
	}

	return mastoAccounts, nil
}

func (p *processor) StatusGet(authed *oauth.Auth, targetStatusID string) (*apimodel.Status, error) {
	l := p.log.WithField("func", "StatusGet")

	l.Tracef("going to search for target status %s", targetStatusID)
	targetStatus := &gtsmodel.Status{}
	if err := p.db.GetByID(targetStatusID, targetStatus); err != nil {
		return nil, fmt.Errorf("error fetching status %s: %s", targetStatusID, err)
	}

	l.Tracef("going to search for target account %s", targetStatus.AccountID)
	targetAccount := &gtsmodel.Account{}
	if err := p.db.GetByID(targetStatus.AccountID, targetAccount); err != nil {
		return nil, fmt.Errorf("error fetching target account %s: %s", targetStatus.AccountID, err)
	}

	l.Trace("going to get relevant accounts")
	relevantAccounts, err := p.db.PullRelevantAccountsFromStatus(targetStatus)
	if err != nil {
		return nil, fmt.Errorf("error fetching related accounts for status %s: %s", targetStatusID, err)
	}

	l.Trace("going to see if status is visible")
	visible, err := p.db.StatusVisible(targetStatus, targetAccount, authed.Account, relevantAccounts) // requestingAccount might well be nil here, but StatusVisible knows how to take care of that
	if err != nil {
		return nil, fmt.Errorf("error seeing if status %s is visible: %s", targetStatus.ID, err)
	}

	if !visible {
		return nil, errors.New("status is not visible")
	}

	var boostOfStatus *gtsmodel.Status
	if targetStatus.BoostOfID != "" {
		boostOfStatus = &gtsmodel.Status{}
		if err := p.db.GetByID(targetStatus.BoostOfID, boostOfStatus); err != nil {
			return nil, fmt.Errorf("error fetching boosted status %s: %s", targetStatus.BoostOfID, err)
		}
	}

	mastoStatus, err := p.tc.StatusToMasto(targetStatus, targetAccount, authed.Account, relevantAccounts.BoostedAccount, relevantAccounts.ReplyToAccount, boostOfStatus)
	if err != nil {
		return nil, fmt.Errorf("error converting status %s to frontend representation: %s", targetStatus.ID, err)
	}

	return mastoStatus, nil

}

func (p *processor) StatusUnfave(authed *oauth.Auth, targetStatusID string) (*apimodel.Status, error) {
	l := p.log.WithField("func", "StatusUnfave")
	l.Tracef("going to search for target status %s", targetStatusID)
	targetStatus := &gtsmodel.Status{}
	if err := p.db.GetByID(targetStatusID, targetStatus); err != nil {
		return nil, fmt.Errorf("error fetching status %s: %s", targetStatusID, err)
	}

	l.Tracef("going to search for target account %s", targetStatus.AccountID)
	targetAccount := &gtsmodel.Account{}
	if err := p.db.GetByID(targetStatus.AccountID, targetAccount); err != nil {
		return nil, fmt.Errorf("error fetching target account %s: %s", targetStatus.AccountID, err)
	}

	l.Trace("going to get relevant accounts")
	relevantAccounts, err := p.db.PullRelevantAccountsFromStatus(targetStatus)
	if err != nil {
		return nil, fmt.Errorf("error fetching related accounts for status %s: %s", targetStatusID, err)
	}

	l.Trace("going to see if status is visible")
	visible, err := p.db.StatusVisible(targetStatus, targetAccount, authed.Account, relevantAccounts) // requestingAccount might well be nil here, but StatusVisible knows how to take care of that
	if err != nil {
		return nil, fmt.Errorf("error seeing if status %s is visible: %s", targetStatus.ID, err)
	}

	if !visible {
		return nil, errors.New("status is not visible")
	}

	// is the status faveable?
	if !targetStatus.VisibilityAdvanced.Likeable {
		return nil, errors.New("status is not faveable")
	}

	// it's visible! it's faveable! so let's unfave the FUCK out of it
	_, err = p.db.UnfaveStatus(targetStatus, authed.Account.ID)
	if err != nil {
		return nil, fmt.Errorf("error unfaveing status: %s", err)
	}

	var boostOfStatus *gtsmodel.Status
	if targetStatus.BoostOfID != "" {
		boostOfStatus = &gtsmodel.Status{}
		if err := p.db.GetByID(targetStatus.BoostOfID, boostOfStatus); err != nil {
			return nil, fmt.Errorf("error fetching boosted status %s: %s", targetStatus.BoostOfID, err)
		}
	}

	mastoStatus, err := p.tc.StatusToMasto(targetStatus, targetAccount, authed.Account, relevantAccounts.BoostedAccount, relevantAccounts.ReplyToAccount, boostOfStatus)
	if err != nil {
		return nil, fmt.Errorf("error converting status %s to frontend representation: %s", targetStatus.ID, err)
	}

	return mastoStatus, nil
}