summaryrefslogtreecommitdiff
path: root/internal/processing/filters/v2/create.go
blob: 60dd46f43297c43afb8bc17c403a5cbca35feb16 (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
// 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"
	"time"

	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"
)

// Create a new 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) Create(ctx context.Context, account *gtsmodel.Account, form *apimodel.FilterCreateRequestV2) (*apimodel.FilterV2, gtserror.WithCode) {
	filter := &gtsmodel.Filter{
		ID:        id.NewULID(),
		AccountID: account.ID,
		Title:     form.Title,
		Action:    typeutils.APIFilterActionToFilterAction(*form.FilterAction),
	}
	if form.ExpiresIn != nil && *form.ExpiresIn != 0 {
		filter.ExpiresAt = time.Now().Add(time.Second * time.Duration(*form.ExpiresIn))
	}
	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),
			)
		}
	}

	for _, formKeyword := range form.Keywords {
		filterKeyword := &gtsmodel.FilterKeyword{
			ID:        id.NewULID(),
			AccountID: account.ID,
			FilterID:  filter.ID,
			Filter:    filter,
			Keyword:   formKeyword.Keyword,
			WholeWord: formKeyword.WholeWord,
		}
		filter.Keywords = append(filter.Keywords, filterKeyword)
	}

	for _, formStatus := range form.Statuses {
		filterStatus := &gtsmodel.FilterStatus{
			ID:        id.NewULID(),
			AccountID: account.ID,
			FilterID:  filter.ID,
			Filter:    filter,
			StatusID:  formStatus.StatusID,
		}
		filter.Statuses = append(filter.Statuses, filterStatus)
	}

	if err := p.state.DB.PutFilter(ctx, filter); err != nil {
		if errors.Is(err, db.ErrAlreadyExists) {
			err = errors.New("duplicate title, keyword, or status")
			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
}