summaryrefslogtreecommitdiff
path: root/internal/federation/federatingdb/undo.go
blob: b3e0a28259833d17022f46d945fe7e5fe597fdc8 (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
// 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 federatingdb

import (
	"context"
	"errors"
	"fmt"

	"github.com/superseriousbusiness/activity/streams/vocab"
	"github.com/superseriousbusiness/gotosocial/internal/ap"
	"github.com/superseriousbusiness/gotosocial/internal/db"
	"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
	"github.com/superseriousbusiness/gotosocial/internal/log"
)

func (f *federatingDB) Undo(ctx context.Context, undo vocab.ActivityStreamsUndo) error {
	log.DebugKV(ctx, "undo", serialize{undo})

	activityContext := getActivityContext(ctx)
	if activityContext.internal {
		return nil // Already processed.
	}

	requestingAcct := activityContext.requestingAcct
	receivingAcct := activityContext.receivingAcct

	for _, object := range ap.ExtractObjects(undo) {
		// Try to get object as vocab.Type,
		// else skip handling (likely) IRI.
		asType := object.GetType()
		if asType == nil {
			continue
		}

		// Check and handle any vocab.Type objects.
		switch name := asType.GetTypeName(); name {

		// UNDO FOLLOW
		case ap.ActivityFollow:
			if err := f.undoFollow(
				ctx,
				receivingAcct,
				requestingAcct,
				undo,
				asType,
			); err != nil {
				return err
			}

		// UNDO LIKE
		case ap.ActivityLike:
			if err := f.undoLike(
				ctx,
				receivingAcct,
				requestingAcct,
				undo,
				asType,
			); err != nil {
				return err
			}

		// UNDO BLOCK
		case ap.ActivityBlock:
			if err := f.undoBlock(
				ctx,
				receivingAcct,
				requestingAcct,
				undo,
				asType,
			); err != nil {
				return err
			}

		// UNHANDLED
		default:
			log.Debugf(ctx, "unhandled object type: %s", name)
		}
	}

	return nil
}

func (f *federatingDB) undoFollow(
	ctx context.Context,
	receivingAcct *gtsmodel.Account,
	requestingAcct *gtsmodel.Account,
	undo vocab.ActivityStreamsUndo,
	t vocab.Type,
) error {
	asFollow, ok := t.(vocab.ActivityStreamsFollow)
	if !ok {
		err := fmt.Errorf("%T not parseable as vocab.ActivityStreamsFollow", t)
		return gtserror.SetMalformed(err)
	}

	// Make sure the Undo
	// actor owns the target.
	if !sameActor(
		undo.GetActivityStreamsActor(),
		asFollow.GetActivityStreamsActor(),
	) {
		// Ignore this Activity.
		return nil
	}

	// Convert AS Follow to barebones *gtsmodel.Follow,
	// retrieving origin + target accts from the db.
	follow, err := f.converter.ASFollowToFollow(
		gtscontext.SetBarebones(ctx),
		asFollow,
	)
	if err != nil && !errors.Is(err, db.ErrNoEntries) {
		err := gtserror.Newf("error converting AS Follow to follow: %w", err)
		return err
	}

	// We were missing origin or
	// target for this Follow, so
	// we cannot Undo anything.
	if follow == nil {
		return nil
	}

	// Ensure addressee is follow target.
	if follow.TargetAccountID != receivingAcct.ID {
		const text = "receivingAcct was not Follow target"
		return gtserror.NewErrorForbidden(errors.New(text), text)
	}

	// Ensure requester is follow origin.
	if follow.AccountID != requestingAcct.ID {
		const text = "requestingAcct was not Follow origin"
		return gtserror.NewErrorForbidden(errors.New(text), text)
	}

	// Delete any existing follow with this URI.
	err = f.state.DB.DeleteFollowByURI(ctx, follow.URI)
	if err != nil && !errors.Is(err, db.ErrNoEntries) {
		err := gtserror.Newf("db error deleting follow: %w", err)
		return err
	}

	// Delete any existing follow request with this URI.
	err = f.state.DB.DeleteFollowRequestByURI(ctx, follow.URI)
	if err != nil && !errors.Is(err, db.ErrNoEntries) {
		err := gtserror.Newf("db error deleting follow request: %w", err)
		return err
	}

	log.Debug(ctx, "Follow undone")
	return nil
}

func (f *federatingDB) undoLike(
	ctx context.Context,
	receivingAcct *gtsmodel.Account,
	requestingAcct *gtsmodel.Account,
	undo vocab.ActivityStreamsUndo,
	t vocab.Type,
) error {
	asLike, ok := t.(vocab.ActivityStreamsLike)
	if !ok {
		err := fmt.Errorf("%T not parseable as vocab.ActivityStreamsLike", t)
		return gtserror.SetMalformed(err)
	}

	// Make sure the Undo
	// actor owns the target.
	if !sameActor(
		undo.GetActivityStreamsActor(),
		asLike.GetActivityStreamsActor(),
	) {
		// Ignore this Activity.
		return nil
	}

	// Convert AS Like to barebones *gtsmodel.StatusFave,
	// retrieving liking acct and target status from the DB.
	fave, err := f.converter.ASLikeToFave(
		gtscontext.SetBarebones(ctx),
		asLike,
	)
	if err != nil && !errors.Is(err, db.ErrNoEntries) {
		err := gtserror.Newf("error converting AS Like to fave: %w", err)
		return err
	}

	// We were missing status, account,
	// or other for this Like, so we
	// cannot Undo anything.
	if fave == nil {
		return nil
	}

	// Ensure addressee is fave target.
	if fave.TargetAccountID != receivingAcct.ID {
		const text = "receivingAcct was not Fave target"
		return gtserror.NewErrorForbidden(errors.New(text), text)
	}

	// Ensure requester is fave origin.
	if fave.AccountID != requestingAcct.ID {
		const text = "requestingAcct was not Fave origin"
		return gtserror.NewErrorForbidden(errors.New(text), text)
	}

	// Fetch fave from the DB so we know the ID to delete it.
	//
	// Ignore URI on Likes, since we often get multiple Likes
	// with the same target and account ID, but differing URIs.
	// Instead, we'll select using account and target status.
	//
	// Regardless of the URI, we can read an Undo Like to mean
	// "I don't want to fave this post anymore".
	fave, err = f.state.DB.GetStatusFave(
		gtscontext.SetBarebones(ctx),
		fave.AccountID,
		fave.StatusID,
	)
	if err != nil && !errors.Is(err, db.ErrNoEntries) {
		err := gtserror.Newf(
			"db error getting fave from %s targeting %s: %w",
			fave.AccountID, fave.StatusID, err,
		)
		return err
	}

	if fave == nil {
		// We didn't have this fave
		// stored anyway, so we can't
		// Undo it, just ignore.
		return nil
	}

	// Delete the fave.
	if err := f.state.DB.DeleteStatusFaveByID(ctx, fave.ID); err != nil {
		err := gtserror.Newf("db error deleting fave %s: %w", fave.ID, err)
		return err
	}

	log.Debug(ctx, "Like undone")
	return nil
}

func (f *federatingDB) undoBlock(
	ctx context.Context,
	receivingAcct *gtsmodel.Account,
	requestingAcct *gtsmodel.Account,
	undo vocab.ActivityStreamsUndo,
	t vocab.Type,
) error {
	asBlock, ok := t.(vocab.ActivityStreamsBlock)
	if !ok {
		err := fmt.Errorf("%T not parseable as vocab.ActivityStreamsBlock", t)
		return gtserror.SetMalformed(err)
	}

	// Make sure the Undo
	// actor owns the target.
	if !sameActor(
		undo.GetActivityStreamsActor(),
		asBlock.GetActivityStreamsActor(),
	) {
		// Ignore this Activity.
		return nil
	}

	// Convert AS Block to barebones *gtsmodel.Block,
	// retrieving origin + target accts from the DB.
	block, err := f.converter.ASBlockToBlock(
		gtscontext.SetBarebones(ctx),
		asBlock,
	)
	if err != nil && !errors.Is(err, db.ErrNoEntries) {
		err := gtserror.Newf("error converting AS Block to block: %w", err)
		return err
	}

	// We were missing origin or
	// target for this Block, so
	// we cannot Undo anything.
	if block == nil {
		return nil
	}

	// Ensure addressee is block target.
	if block.TargetAccountID != receivingAcct.ID {
		const text = "receivingAcct was not Block target"
		return gtserror.NewErrorForbidden(errors.New(text), text)
	}

	// Ensure requester is block origin.
	if block.AccountID != requestingAcct.ID {
		const text = "requestingAcct was not Block origin"
		return gtserror.NewErrorForbidden(errors.New(text), text)
	}

	// Delete any existing block.
	err = f.state.DB.DeleteBlockByURI(ctx, block.URI)
	if err != nil && !errors.Is(err, db.ErrNoEntries) {
		err := gtserror.Newf("db error deleting block: %w", err)
		return err
	}

	log.Debug(ctx, "Block undone")
	return nil
}