summaryrefslogtreecommitdiff
path: root/internal/processing/interactionrequests/accept.go
blob: ad86e50d1b221511c17c80918a4d907856e10b5a (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
// 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 interactionrequests

import (
	"context"
	"time"

	"github.com/superseriousbusiness/gotosocial/internal/ap"
	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
	"github.com/superseriousbusiness/gotosocial/internal/messages"
	"github.com/superseriousbusiness/gotosocial/internal/uris"
	"github.com/superseriousbusiness/gotosocial/internal/util"
)

// Accept accepts an interaction request with the given ID,
// on behalf of the given account (whose post it must target).
func (p *Processor) Accept(
	ctx context.Context,
	acct *gtsmodel.Account,
	reqID string,
) (*apimodel.InteractionRequest, gtserror.WithCode) {
	req, err := p.state.DB.GetInteractionRequestByID(ctx, reqID)
	if err != nil {
		err := gtserror.Newf("db error getting interaction request: %w", err)
		return nil, gtserror.NewErrorInternalError(err)
	}

	if req.TargetAccountID != acct.ID {
		err := gtserror.Newf(
			"interaction request %s does not belong to account %s",
			reqID, acct.ID,
		)
		return nil, gtserror.NewErrorNotFound(err)
	}

	if !req.IsPending() {
		err := gtserror.Newf(
			"interaction request %s has already been handled",
			reqID,
		)
		return nil, gtserror.NewErrorNotFound(err)
	}

	// Lock on the interaction req URI to
	// ensure nobody else is modifying it rn.
	unlock := p.state.ProcessingLocks.Lock(req.InteractionURI)
	defer unlock()

	// Mark the request as accepted
	// and generate a URI for it.
	req.AcceptedAt = time.Now()
	req.URI = uris.GenerateURIForAccept(acct.Username, req.ID)
	if err := p.state.DB.UpdateInteractionRequest(
		ctx,
		req,
		"accepted_at",
		"uri",
	); err != nil {
		err := gtserror.Newf("db error updating interaction request: %w", err)
		return nil, gtserror.NewErrorInternalError(err)
	}

	switch req.InteractionType {

	case gtsmodel.InteractionLike:
		if errWithCode := p.acceptLike(ctx, req); errWithCode != nil {
			return nil, errWithCode
		}

	case gtsmodel.InteractionReply:
		if errWithCode := p.acceptReply(ctx, req); errWithCode != nil {
			return nil, errWithCode
		}

	case gtsmodel.InteractionAnnounce:
		if errWithCode := p.acceptAnnounce(ctx, req); errWithCode != nil {
			return nil, errWithCode
		}

	default:
		err := gtserror.Newf("unknown interaction type for interaction request %s", reqID)
		return nil, gtserror.NewErrorInternalError(err)
	}

	// Return the now-accepted req to the caller so
	// they can do something with it if they need to.
	apiReq, err := p.converter.InteractionReqToAPIInteractionReq(
		ctx,
		req,
		acct,
	)
	if err != nil {
		err := gtserror.Newf("error converting interaction request: %w", err)
		return nil, gtserror.NewErrorInternalError(err)
	}

	return apiReq, nil
}

// Package-internal convenience
// function to accept a like.
func (p *Processor) acceptLike(
	ctx context.Context,
	req *gtsmodel.InteractionRequest,
) gtserror.WithCode {
	// If the Like is missing, that means it's
	// probably already been undone by someone,
	// so there's nothing to actually accept.
	if req.Like == nil {
		err := gtserror.Newf("no Like found for interaction request %s", req.ID)
		return gtserror.NewErrorNotFound(err)
	}

	// Update the Like.
	req.Like.PendingApproval = util.Ptr(false)
	req.Like.PreApproved = false
	req.Like.ApprovedByURI = req.URI
	if err := p.state.DB.UpdateStatusFave(
		ctx,
		req.Like,
		"pending_approval",
		"approved_by_uri",
	); err != nil {
		err := gtserror.Newf("db error updating status fave: %w", err)
		return gtserror.NewErrorInternalError(err)
	}

	// Send the accepted request off through the
	// client API processor to handle side effects.
	p.state.Workers.Client.Queue.Push(&messages.FromClientAPI{
		APObjectType:   ap.ActivityLike,
		APActivityType: ap.ActivityAccept,
		GTSModel:       req,
		Origin:         req.TargetAccount,
		Target:         req.InteractingAccount,
	})

	return nil
}

// Package-internal convenience
// function to accept a reply.
func (p *Processor) acceptReply(
	ctx context.Context,
	req *gtsmodel.InteractionRequest,
) gtserror.WithCode {
	// If the Reply is missing, that means it's
	// probably already been undone by someone,
	// so there's nothing to actually accept.
	if req.Reply == nil {
		err := gtserror.Newf("no Reply found for interaction request %s", req.ID)
		return gtserror.NewErrorNotFound(err)
	}

	// Update the Reply.
	req.Reply.PendingApproval = util.Ptr(false)
	req.Reply.PreApproved = false
	req.Reply.ApprovedByURI = req.URI
	if err := p.state.DB.UpdateStatus(
		ctx,
		req.Reply,
		"pending_approval",
		"approved_by_uri",
	); err != nil {
		err := gtserror.Newf("db error updating status reply: %w", err)
		return gtserror.NewErrorInternalError(err)
	}

	// Send the accepted request off through the
	// client API processor to handle side effects.
	p.state.Workers.Client.Queue.Push(&messages.FromClientAPI{
		APObjectType:   ap.ObjectNote,
		APActivityType: ap.ActivityAccept,
		GTSModel:       req,
		Origin:         req.TargetAccount,
		Target:         req.InteractingAccount,
	})

	return nil
}

// Package-internal convenience
// function to accept an announce.
func (p *Processor) acceptAnnounce(
	ctx context.Context,
	req *gtsmodel.InteractionRequest,
) gtserror.WithCode {
	// If the Announce is missing, that means it's
	// probably already been undone by someone,
	// so there's nothing to actually accept.
	if req.Reply == nil {
		err := gtserror.Newf("no Announce found for interaction request %s", req.ID)
		return gtserror.NewErrorNotFound(err)
	}

	// Update the Announce.
	req.Announce.PendingApproval = util.Ptr(false)
	req.Announce.PreApproved = false
	req.Announce.ApprovedByURI = req.URI
	if err := p.state.DB.UpdateStatus(
		ctx,
		req.Announce,
		"pending_approval",
		"approved_by_uri",
	); err != nil {
		err := gtserror.Newf("db error updating status announce: %w", err)
		return gtserror.NewErrorInternalError(err)
	}

	// Send the accepted request off through the
	// client API processor to handle side effects.
	p.state.Workers.Client.Queue.Push(&messages.FromClientAPI{
		APObjectType:   ap.ActivityAnnounce,
		APActivityType: ap.ActivityAccept,
		GTSModel:       req,
		Origin:         req.TargetAccount,
		Target:         req.InteractingAccount,
	})

	return nil
}