summaryrefslogtreecommitdiff
path: root/internal/processing/filters/v2/update.go
blob: d5b5cce0190a273405b17ea94e13f695618481f0 (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
// 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 v2

import (
	"context"
	"errors"
	"fmt"
	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
	"github.com/superseriousbusiness/gotosocial/internal/db"
	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
	"github.com/superseriousbusiness/gotosocial/internal/id"
	"github.com/superseriousbusiness/gotosocial/internal/typeutils"
	"github.com/superseriousbusiness/gotosocial/internal/util"
	"time"
)

// Update an existing filter for the given account, using the provided parameters.
// These params should have already been validated by the time they reach this function.
func (p *Processor) Update(
	ctx context.Context,
	account *gtsmodel.Account,
	filterID string,
	form *apimodel.FilterUpdateRequestV2,
) (*apimodel.FilterV2, gtserror.WithCode) {
	var errWithCode gtserror.WithCode

	// Get the filter by ID, with existing keywords and statuses.
	filter, err := p.state.DB.GetFilterByID(ctx, filterID)
	if err != nil {
		if errors.Is(err, db.ErrNoEntries) {
			return nil, gtserror.NewErrorNotFound(err)
		}
		return nil, gtserror.NewErrorInternalError(err)
	}
	if filter.AccountID != account.ID {
		return nil, gtserror.NewErrorNotFound(
			fmt.Errorf("filter %s doesn't belong to account %s", filter.ID, account.ID),
		)
	}

	// Filter columns that we're going to update.
	filterColumns := []string{}

	// Apply filter changes.
	if form.Title != nil {
		filterColumns = append(filterColumns, "title")
		filter.Title = *form.Title
	}
	if form.FilterAction != nil {
		filterColumns = append(filterColumns, "action")
		filter.Action = typeutils.APIFilterActionToFilterAction(*form.FilterAction)
	}
	if form.ExpiresIn != nil {
		expiresIn := *form.ExpiresIn
		filterColumns = append(filterColumns, "expires_at")
		if expiresIn == 0 {
			// Unset the expiration date.
			filter.ExpiresAt = time.Time{}
		} else {
			// Update the expiration date.
			filter.ExpiresAt = time.Now().Add(time.Second * time.Duration(expiresIn))
		}
	}
	if form.Context != nil {
		filterColumns = append(filterColumns,
			"context_home",
			"context_notifications",
			"context_public",
			"context_thread",
			"context_account",
		)
		filter.ContextHome = util.Ptr(false)
		filter.ContextNotifications = util.Ptr(false)
		filter.ContextPublic = util.Ptr(false)
		filter.ContextThread = util.Ptr(false)
		filter.ContextAccount = util.Ptr(false)
		for _, context := range *form.Context {
			switch context {
			case apimodel.FilterContextHome:
				filter.ContextHome = util.Ptr(true)
			case apimodel.FilterContextNotifications:
				filter.ContextNotifications = util.Ptr(true)
			case apimodel.FilterContextPublic:
				filter.ContextPublic = util.Ptr(true)
			case apimodel.FilterContextThread:
				filter.ContextThread = util.Ptr(true)
			case apimodel.FilterContextAccount:
				filter.ContextAccount = util.Ptr(true)
			default:
				return nil, gtserror.NewErrorUnprocessableEntity(
					fmt.Errorf("unsupported filter context '%s'", context),
				)
			}
		}
	}

	filterKeywordColumns, deleteFilterKeywordIDs, errWithCode := applyKeywordChanges(filter, form.Keywords)
	if err != nil {
		return nil, errWithCode
	}

	deleteFilterStatusIDs, errWithCode := applyStatusChanges(filter, form.Statuses)
	if err != nil {
		return nil, errWithCode
	}

	if err := p.state.DB.UpdateFilter(ctx, filter, filterColumns, filterKeywordColumns, deleteFilterKeywordIDs, deleteFilterStatusIDs); err != nil {
		if errors.Is(err, db.ErrAlreadyExists) {
			err = errors.New("you already have a filter with this title")
			return nil, gtserror.NewErrorConflict(err, err.Error())
		}
		return nil, gtserror.NewErrorInternalError(err)
	}

	apiFilter, errWithCode := p.apiFilter(ctx, filter)
	if errWithCode != nil {
		return nil, errWithCode
	}

	// Send a filters changed event.
	p.stream.FiltersChanged(ctx, account)

	return apiFilter, nil
}

// applyKeywordChanges applies the provided changes to the filter's keywords in place,
// and returns a list of lists of filter columns to update, and a list of filter keyword IDs to delete.
func applyKeywordChanges(filter *gtsmodel.Filter, formKeywords []apimodel.FilterKeywordCreateUpdateDeleteRequest) ([][]string, []string, gtserror.WithCode) {
	if len(formKeywords) == 0 {
		// Detach currently existing keywords from the filter so we don't change them.
		filter.Keywords = nil
		return nil, nil, nil
	}

	deleteFilterKeywordIDs := []string{}
	filterKeywordsByID := map[string]*gtsmodel.FilterKeyword{}
	filterKeywordColumnsByID := map[string][]string{}
	for _, filterKeyword := range filter.Keywords {
		filterKeywordsByID[filterKeyword.ID] = filterKeyword
	}

	for _, formKeyword := range formKeywords {
		if formKeyword.ID != nil {
			id := *formKeyword.ID
			filterKeyword, ok := filterKeywordsByID[id]
			if !ok {
				return nil, nil, gtserror.NewErrorNotFound(
					fmt.Errorf("couldn't find filter keyword '%s' to update or delete", id),
				)
			}

			// Process deletes.
			if *formKeyword.Destroy {
				delete(filterKeywordsByID, id)
				deleteFilterKeywordIDs = append(deleteFilterKeywordIDs, id)
				continue
			}

			// Process updates.
			columns := make([]string, 0, 2)
			if formKeyword.Keyword != nil {
				columns = append(columns, "keyword")
				filterKeyword.Keyword = *formKeyword.Keyword
			}
			if formKeyword.WholeWord != nil {
				columns = append(columns, "whole_word")
				filterKeyword.WholeWord = formKeyword.WholeWord
			}
			filterKeywordColumnsByID[id] = columns
			continue
		}

		// Process creates.
		filterKeyword := &gtsmodel.FilterKeyword{
			ID:        id.NewULID(),
			AccountID: filter.AccountID,
			FilterID:  filter.ID,
			Filter:    filter,
			Keyword:   *formKeyword.Keyword,
			WholeWord: util.Ptr(util.PtrOrValue(formKeyword.WholeWord, false)),
		}
		filterKeywordsByID[filterKeyword.ID] = filterKeyword
		// Don't need to set columns, as we're using all of them.
	}

	// Replace the filter's keywords list with our updated version.
	filterKeywordColumns := [][]string{}
	filter.Keywords = nil
	for id, filterKeyword := range filterKeywordsByID {
		filter.Keywords = append(filter.Keywords, filterKeyword)
		// Okay to use the nil slice zero value for entries being created instead of updated.
		filterKeywordColumns = append(filterKeywordColumns, filterKeywordColumnsByID[id])
	}

	return filterKeywordColumns, deleteFilterKeywordIDs, nil
}

// applyKeywordChanges applies the provided changes to the filter's keywords in place,
// and returns a list of filter status IDs to delete.
func applyStatusChanges(filter *gtsmodel.Filter, formStatuses []apimodel.FilterStatusCreateDeleteRequest) ([]string, gtserror.WithCode) {
	if len(formStatuses) == 0 {
		// Detach currently existing statuses from the filter so we don't change them.
		filter.Statuses = nil
		return nil, nil
	}

	deleteFilterStatusIDs := []string{}
	filterStatusesByID := map[string]*gtsmodel.FilterStatus{}
	for _, filterStatus := range filter.Statuses {
		filterStatusesByID[filterStatus.ID] = filterStatus
	}

	for _, formStatus := range formStatuses {
		if formStatus.ID != nil {
			id := *formStatus.ID
			_, ok := filterStatusesByID[id]
			if !ok {
				return nil, gtserror.NewErrorNotFound(
					fmt.Errorf("couldn't find filter status '%s' to delete", id),
				)
			}

			// Process deletes.
			if *formStatus.Destroy {
				delete(filterStatusesByID, id)
				deleteFilterStatusIDs = append(deleteFilterStatusIDs, id)
				continue
			}

			// Filter statuses don't have updates.
			continue
		}

		// Process creates.
		filterStatus := &gtsmodel.FilterStatus{
			ID:        id.NewULID(),
			AccountID: filter.AccountID,
			FilterID:  filter.ID,
			Filter:    filter,
			StatusID:  *formStatus.StatusID,
		}
		filterStatusesByID[filterStatus.ID] = filterStatus
	}

	// Replace the filter's keywords list with our updated version.
	filter.Statuses = nil
	for _, filterStatus := range filterStatusesByID {
		filter.Statuses = append(filter.Statuses, filterStatus)
	}

	return deleteFilterStatusIDs, nil
}