summaryrefslogtreecommitdiff
path: root/internal/federation/dereferencing/status.go
blob: a1783e838b93e28b0377f9fb69453eb744dd2ffe (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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
   GoToSocial
   Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org

   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 dereferencing

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"net/url"
	"strings"

	"github.com/superseriousbusiness/activity/streams"
	"github.com/superseriousbusiness/activity/streams/vocab"
	"github.com/superseriousbusiness/gotosocial/internal/ap"
	"github.com/superseriousbusiness/gotosocial/internal/config"
	"github.com/superseriousbusiness/gotosocial/internal/db"
	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
	"github.com/superseriousbusiness/gotosocial/internal/id"
	"github.com/superseriousbusiness/gotosocial/internal/log"
	"github.com/superseriousbusiness/gotosocial/internal/media"
)

// EnrichRemoteStatus takes a remote status that's already been inserted into the database in a minimal form,
// and populates it with additional fields, media, etc.
//
// EnrichRemoteStatus is mostly useful for calling after a status has been initially created by
// the federatingDB's Create function, but additional dereferencing is needed on it.
func (d *deref) EnrichRemoteStatus(ctx context.Context, username string, status *gtsmodel.Status, includeParent bool) (*gtsmodel.Status, error) {
	if err := d.populateStatusFields(ctx, status, username, includeParent); err != nil {
		return nil, err
	}
	if err := d.db.UpdateStatus(ctx, status); err != nil {
		return nil, err
	}
	return status, nil
}

// GetStatus completely dereferences a status, converts it to a GtS model status,
// puts it in the database, and returns it to a caller.
//
// If refetch is true, then regardless of whether we have the original status in the database or not,
// the ap.Statusable representation of the status will be dereferenced and returned.
//
// If refetch is false, the ap.Statusable will only be returned if this is a new status, so callers
// should check whether or not this is nil.
//
// GetAccount will guard against trying to do http calls to fetch a status that belongs to this instance.
// Instead of making calls, it will just return the status early if it finds it, or return an error.
func (d *deref) GetStatus(ctx context.Context, username string, statusURI *url.URL, refetch, includeParent bool) (*gtsmodel.Status, ap.Statusable, error) {
	uriString := statusURI.String()

	// try to get by URI first
	status, dbErr := d.db.GetStatusByURI(ctx, uriString)
	if dbErr != nil {
		if !errors.Is(dbErr, db.ErrNoEntries) {
			// real error
			return nil, nil, newErrDB(fmt.Errorf("GetRemoteStatus: error during GetStatusByURI for %s: %w", uriString, dbErr))
		}
		// no problem, just press on
	} else if !refetch {
		// we already had the status and we aren't being asked to refetch the AP representation
		return status, nil, nil
	}

	// try to get by URL if we couldn't get by URI now
	if status == nil {
		status, dbErr = d.db.GetStatusByURL(ctx, uriString)
		if dbErr != nil {
			if !errors.Is(dbErr, db.ErrNoEntries) {
				// real error
				return nil, nil, newErrDB(fmt.Errorf("GetRemoteStatus: error during GetStatusByURI for %s: %w", uriString, dbErr))
			}
			// no problem, just press on
		} else if !refetch {
			// we already had the status and we aren't being asked to refetch the AP representation
			return status, nil, nil
		}
	}

	// guard against having our own statuses passed in
	if host := statusURI.Host; host == config.GetHost() || host == config.GetAccountDomain() {
		// this is our status, definitely don't search for it
		if status != nil {
			return status, nil, nil
		}
		return nil, nil, newErrNotRetrievable(fmt.Errorf("GetRemoteStatus: uri %s is apparently ours, but we have nothing in the db for it, will not proceed to dereference our own status", uriString))
	}

	// if we got here, either we didn't have the status
	// in the db, or we had it but need to refetch it
	statusable, derefErr := d.dereferenceStatusable(ctx, username, statusURI)
	if derefErr != nil {
		return nil, nil, wrapDerefError(derefErr, "GetRemoteStatus: error dereferencing statusable")
	}

	if status != nil && refetch {
		// we already had the status in the db, and we've also
		// now fetched the AP representation as requested
		return status, statusable, nil
	}

	// from here on out we can consider this to be a 'new' status because we didn't have the status in the db already
	accountURI, err := ap.ExtractAttributedTo(statusable)
	if err != nil {
		return nil, nil, newErrOther(fmt.Errorf("GetRemoteStatus: error extracting attributedTo: %w", err))
	}

	// we need to get the author of the status else we can't serialize it properly
	if _, err = d.GetAccount(ctx, GetAccountParams{
		RequestingUsername: username,
		RemoteAccountID:    accountURI,
		Blocking:           true,
	}); err != nil {
		return nil, nil, newErrOther(fmt.Errorf("GetRemoteStatus: couldn't get status author: %s", err))
	}

	status, err = d.typeConverter.ASStatusToStatus(ctx, statusable)
	if err != nil {
		return nil, nil, newErrOther(fmt.Errorf("GetRemoteStatus: error converting statusable to status: %s", err))
	}

	ulid, err := id.NewULIDFromTime(status.CreatedAt)
	if err != nil {
		return nil, nil, newErrOther(fmt.Errorf("GetRemoteStatus: error generating new id for status: %s", err))
	}
	status.ID = ulid

	if err := d.populateStatusFields(ctx, status, username, includeParent); err != nil {
		return nil, nil, newErrOther(fmt.Errorf("GetRemoteStatus: error populating status fields: %s", err))
	}

	if err := d.db.PutStatus(ctx, status); err != nil && !errors.Is(err, db.ErrAlreadyExists) {
		return nil, nil, newErrDB(fmt.Errorf("GetRemoteStatus: error putting new status: %s", err))
	}

	return status, statusable, nil
}

func (d *deref) dereferenceStatusable(ctx context.Context, username string, remoteStatusID *url.URL) (ap.Statusable, error) {
	if blocked, err := d.db.IsDomainBlocked(ctx, remoteStatusID.Host); blocked || err != nil {
		return nil, fmt.Errorf("DereferenceStatusable: domain %s is blocked", remoteStatusID.Host)
	}

	transport, err := d.transportController.NewTransportForUsername(ctx, username)
	if err != nil {
		return nil, fmt.Errorf("DereferenceStatusable: transport err: %s", err)
	}

	b, err := transport.Dereference(ctx, remoteStatusID)
	if err != nil {
		return nil, fmt.Errorf("DereferenceStatusable: error deferencing %s: %s", remoteStatusID.String(), err)
	}

	m := make(map[string]interface{})
	if err := json.Unmarshal(b, &m); err != nil {
		return nil, fmt.Errorf("DereferenceStatusable: error unmarshalling bytes into json: %s", err)
	}

	t, err := streams.ToType(ctx, m)
	if err != nil {
		return nil, fmt.Errorf("DereferenceStatusable: error resolving json into ap vocab type: %s", err)
	}

	// Article, Document, Image, Video, Note, Page, Event, Place, Mention, Profile
	switch t.GetTypeName() {
	case ap.ObjectArticle:
		p, ok := t.(vocab.ActivityStreamsArticle)
		if !ok {
			return nil, errors.New("DereferenceStatusable: error resolving type as ActivityStreamsArticle")
		}
		return p, nil
	case ap.ObjectDocument:
		p, ok := t.(vocab.ActivityStreamsDocument)
		if !ok {
			return nil, errors.New("DereferenceStatusable: error resolving type as ActivityStreamsDocument")
		}
		return p, nil
	case ap.ObjectImage:
		p, ok := t.(vocab.ActivityStreamsImage)
		if !ok {
			return nil, errors.New("DereferenceStatusable: error resolving type as ActivityStreamsImage")
		}
		return p, nil
	case ap.ObjectVideo:
		p, ok := t.(vocab.ActivityStreamsVideo)
		if !ok {
			return nil, errors.New("DereferenceStatusable: error resolving type as ActivityStreamsVideo")
		}
		return p, nil
	case ap.ObjectNote:
		p, ok := t.(vocab.ActivityStreamsNote)
		if !ok {
			return nil, errors.New("DereferenceStatusable: error resolving type as ActivityStreamsNote")
		}
		return p, nil
	case ap.ObjectPage:
		p, ok := t.(vocab.ActivityStreamsPage)
		if !ok {
			return nil, errors.New("DereferenceStatusable: error resolving type as ActivityStreamsPage")
		}
		return p, nil
	case ap.ObjectEvent:
		p, ok := t.(vocab.ActivityStreamsEvent)
		if !ok {
			return nil, errors.New("DereferenceStatusable: error resolving type as ActivityStreamsEvent")
		}
		return p, nil
	case ap.ObjectPlace:
		p, ok := t.(vocab.ActivityStreamsPlace)
		if !ok {
			return nil, errors.New("DereferenceStatusable: error resolving type as ActivityStreamsPlace")
		}
		return p, nil
	case ap.ObjectProfile:
		p, ok := t.(vocab.ActivityStreamsProfile)
		if !ok {
			return nil, errors.New("DereferenceStatusable: error resolving type as ActivityStreamsProfile")
		}
		return p, nil
	}

	return nil, newErrWrongType(fmt.Errorf("DereferenceStatusable: type name %s not supported as Statusable", t.GetTypeName()))
}

// populateStatusFields fetches all the information we temporarily pinned to an incoming
// federated status, back in the federating db's Create function.
//
// When a status comes in from the federation API, there are certain fields that
// haven't been dereferenced yet, because we needed to provide a snappy synchronous
// response to the caller. By the time it reaches this function though, it's being
// processed asynchronously, so we have all the time in the world to fetch the various
// bits and bobs that are attached to the status, and properly flesh it out, before we
// send the status to any timelines and notify people.
//
// Things to dereference and fetch here:
//
// 1. Media attachments.
// 2. Hashtags.
// 3. Emojis.
// 4. Mentions.
// 5. Replied-to-status.
//
// SIDE EFFECTS:
// This function will deference all of the above, insert them in the database as necessary,
// and attach them to the status. The status itself will not be added to the database yet,
// that's up the caller to do.
func (d *deref) populateStatusFields(ctx context.Context, status *gtsmodel.Status, requestingUsername string, includeParent bool) error {
	statusIRI, err := url.Parse(status.URI)
	if err != nil {
		return fmt.Errorf("populateStatusFields: couldn't parse status URI %s: %s", status.URI, err)
	}

	blocked, err := d.db.IsURIBlocked(ctx, statusIRI)
	if err != nil {
		return fmt.Errorf("populateStatusFields: error checking blocked status of %s: %s", statusIRI, err)
	}
	if blocked {
		return fmt.Errorf("populateStatusFields: domain %s is blocked", statusIRI)
	}

	// in case the status doesn't have an id yet (ie., it hasn't entered the database yet), then create one
	if status.ID == "" {
		newID, err := id.NewULIDFromTime(status.CreatedAt)
		if err != nil {
			return fmt.Errorf("populateStatusFields: error creating ulid for status: %s", err)
		}
		status.ID = newID
	}

	// 1. Media attachments.
	if err := d.populateStatusAttachments(ctx, status, requestingUsername); err != nil {
		return fmt.Errorf("populateStatusFields: error populating status attachments: %s", err)
	}

	// 2. Hashtags
	// TODO

	// 3. Emojis
	if err := d.populateStatusEmojis(ctx, status, requestingUsername); err != nil {
		return fmt.Errorf("populateStatusFields: error populating status emojis: %s", err)
	}

	// 4. Mentions
	// TODO: do we need to handle removing empty mention objects and just using mention IDs slice?
	if err := d.populateStatusMentions(ctx, status, requestingUsername); err != nil {
		return fmt.Errorf("populateStatusFields: error populating status mentions: %s", err)
	}

	// 5. Replied-to-status (only if requested)
	if includeParent {
		if err := d.populateStatusRepliedTo(ctx, status, requestingUsername); err != nil {
			return fmt.Errorf("populateStatusFields: error populating status repliedTo: %s", err)
		}
	}

	return nil
}

func (d *deref) populateStatusMentions(ctx context.Context, status *gtsmodel.Status, requestingUsername string) error {
	// At this point, mentions should have the namestring and mentionedAccountURI set on them.
	// We can use these to find the accounts.

	mentionIDs := []string{}
	newMentions := []*gtsmodel.Mention{}
	for _, m := range status.Mentions {
		if m.ID != "" {
			// we've already populated this mention, since it has an ID
			log.Debug("populateStatusMentions: mention already populated")
			mentionIDs = append(mentionIDs, m.ID)
			newMentions = append(newMentions, m)
			continue
		}

		if m.TargetAccountURI == "" {
			log.Debug("populateStatusMentions: target URI not set on mention")
			continue
		}

		targetAccountURI, err := url.Parse(m.TargetAccountURI)
		if err != nil {
			log.Debugf("populateStatusMentions: error parsing mentioned account uri %s: %s", m.TargetAccountURI, err)
			continue
		}

		var targetAccount *gtsmodel.Account
		errs := []string{}

		// check if account is in the db already
		if a, err := d.db.GetAccountByURI(ctx, targetAccountURI.String()); err != nil {
			errs = append(errs, err.Error())
		} else {
			log.Debugf("populateStatusMentions: got target account %s with id %s through GetAccountByURI", targetAccountURI, a.ID)
			targetAccount = a
		}

		if targetAccount == nil {
			// we didn't find the account in our database already
			// check if we can get the account remotely (dereference it)
			if a, err := d.GetAccount(ctx, GetAccountParams{
				RequestingUsername: requestingUsername,
				RemoteAccountID:    targetAccountURI,
			}); err != nil {
				errs = append(errs, err.Error())
			} else {
				log.Debugf("populateStatusMentions: got target account %s with id %s through GetRemoteAccount", targetAccountURI, a.ID)
				targetAccount = a
			}
		}

		if targetAccount == nil {
			log.Debugf("populateStatusMentions: couldn't get target account %s: %s", m.TargetAccountURI, strings.Join(errs, " : "))
			continue
		}

		mID, err := id.NewRandomULID()
		if err != nil {
			return fmt.Errorf("populateStatusMentions: error generating ulid: %s", err)
		}

		newMention := &gtsmodel.Mention{
			ID:               mID,
			StatusID:         status.ID,
			Status:           m.Status,
			CreatedAt:        status.CreatedAt,
			UpdatedAt:        status.UpdatedAt,
			OriginAccountID:  status.AccountID,
			OriginAccountURI: status.AccountURI,
			OriginAccount:    status.Account,
			TargetAccountID:  targetAccount.ID,
			TargetAccount:    targetAccount,
			NameString:       m.NameString,
			TargetAccountURI: targetAccount.URI,
			TargetAccountURL: targetAccount.URL,
		}

		if err := d.db.Put(ctx, newMention); err != nil {
			return fmt.Errorf("populateStatusMentions: error creating mention: %s", err)
		}

		mentionIDs = append(mentionIDs, newMention.ID)
		newMentions = append(newMentions, newMention)
	}

	status.MentionIDs = mentionIDs
	status.Mentions = newMentions

	return nil
}

func (d *deref) populateStatusAttachments(ctx context.Context, status *gtsmodel.Status, requestingUsername string) error {
	// At this point we should know:
	// * the media type of the file we're looking for (a.File.ContentType)
	// * the file type (a.Type)
	// * the remote URL (a.RemoteURL)
	// This should be enough to dereference the piece of media.

	attachmentIDs := []string{}
	attachments := []*gtsmodel.MediaAttachment{}

	for _, a := range status.Attachments {
		a.AccountID = status.AccountID
		a.StatusID = status.ID

		processingMedia, err := d.GetRemoteMedia(ctx, requestingUsername, a.AccountID, a.RemoteURL, &media.AdditionalMediaInfo{
			CreatedAt:   &a.CreatedAt,
			StatusID:    &a.StatusID,
			RemoteURL:   &a.RemoteURL,
			Description: &a.Description,
			Blurhash:    &a.Blurhash,
		})
		if err != nil {
			log.Errorf("populateStatusAttachments: couldn't get remote media %s: %s", a.RemoteURL, err)
			continue
		}

		attachment, err := processingMedia.LoadAttachment(ctx)
		if err != nil {
			log.Errorf("populateStatusAttachments: couldn't load remote attachment %s: %s", a.RemoteURL, err)
			continue
		}

		attachmentIDs = append(attachmentIDs, attachment.ID)
		attachments = append(attachments, attachment)
	}

	status.AttachmentIDs = attachmentIDs
	status.Attachments = attachments

	return nil
}

func (d *deref) populateStatusEmojis(ctx context.Context, status *gtsmodel.Status, requestingUsername string) error {
	emojis, err := d.populateEmojis(ctx, status.Emojis, requestingUsername)
	if err != nil {
		return err
	}

	emojiIDs := make([]string, 0, len(emojis))
	for _, e := range emojis {
		emojiIDs = append(emojiIDs, e.ID)
	}

	status.Emojis = emojis
	status.EmojiIDs = emojiIDs
	return nil
}

func (d *deref) populateStatusRepliedTo(ctx context.Context, status *gtsmodel.Status, requestingUsername string) error {
	if status.InReplyToURI != "" && status.InReplyToID == "" {
		statusURI, err := url.Parse(status.InReplyToURI)
		if err != nil {
			return err
		}

		replyToStatus, _, err := d.GetStatus(ctx, requestingUsername, statusURI, false, false)
		if err != nil {
			return fmt.Errorf("populateStatusRepliedTo: couldn't get reply to status with uri %s: %s", status.InReplyToURI, err)
		}

		// we have the status
		status.InReplyToID = replyToStatus.ID
		status.InReplyTo = replyToStatus
		status.InReplyToAccountID = replyToStatus.AccountID
		status.InReplyToAccount = replyToStatus.Account
	}

	return nil
}