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
|
// 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 account
import (
"context"
"errors"
"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/log"
"github.com/superseriousbusiness/gotosocial/internal/paging"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
// MuteCreate handles the creation or updating of a mute from requestingAccount to targetAccountID.
// The form params should have already been normalized by the time they reach this function.
func (p *Processor) MuteCreate(
ctx context.Context,
requestingAccount *gtsmodel.Account,
targetAccountID string,
form *apimodel.UserMuteCreateUpdateRequest,
) (*apimodel.Relationship, gtserror.WithCode) {
targetAccount, existingMute, errWithCode := p.getMuteTarget(ctx, requestingAccount, targetAccountID)
if errWithCode != nil {
return nil, errWithCode
}
if existingMute != nil &&
*existingMute.Notifications == *form.Notifications &&
existingMute.ExpiresAt.IsZero() && form.Duration == nil {
// Mute already exists and doesn't require updating, nothing to do.
return p.RelationshipGet(ctx, requestingAccount, targetAccountID)
}
// Create a new mute or update an existing one.
mute := >smodel.UserMute{
AccountID: requestingAccount.ID,
Account: requestingAccount,
TargetAccountID: targetAccountID,
TargetAccount: targetAccount,
Notifications: form.Notifications,
}
if existingMute != nil {
mute.ID = existingMute.ID
} else {
mute.ID = id.NewULID()
}
if form.Duration != nil {
mute.ExpiresAt = time.Now().Add(time.Second * time.Duration(*form.Duration))
}
if err := p.state.DB.PutMute(ctx, mute); err != nil {
err = gtserror.Newf("error creating or updating mute in db: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
return p.RelationshipGet(ctx, requestingAccount, targetAccountID)
}
// MuteRemove handles the removal of a mute from requestingAccount to targetAccountID.
func (p *Processor) MuteRemove(
ctx context.Context,
requestingAccount *gtsmodel.Account,
targetAccountID string,
) (*apimodel.Relationship, gtserror.WithCode) {
_, existingMute, errWithCode := p.getMuteTarget(ctx, requestingAccount, targetAccountID)
if errWithCode != nil {
return nil, errWithCode
}
if existingMute == nil {
// Already not muted, nothing to do.
return p.RelationshipGet(ctx, requestingAccount, targetAccountID)
}
// We got a mute, remove it from the db.
if err := p.state.DB.DeleteMuteByID(ctx, existingMute.ID); err != nil {
err := gtserror.Newf("error removing mute from db: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
return p.RelationshipGet(ctx, requestingAccount, targetAccountID)
}
// MutesGet retrieves the user's list of muted accounts, with an extra field for mute expiration (if applicable).
func (p *Processor) MutesGet(
ctx context.Context,
requestingAccount *gtsmodel.Account,
page *paging.Page,
) (*apimodel.PageableResponse, gtserror.WithCode) {
mutes, err := p.state.DB.GetAccountMutes(ctx,
requestingAccount.ID,
page,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err = gtserror.Newf("couldn't list account's mutes: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
// Check for empty response.
count := len(mutes)
if len(mutes) == 0 {
return util.EmptyPageableResponse(), nil
}
// Get the lowest and highest
// ID values, used for paging.
lo := mutes[count-1].ID
hi := mutes[0].ID
items := make([]interface{}, 0, count)
now := time.Now()
for _, mute := range mutes {
// Skip accounts for which the mute has expired.
if mute.Expired(now) {
continue
}
// Convert target account to frontend API model. (target will never be nil)
account, err := p.converter.AccountToAPIAccountPublic(ctx, mute.TargetAccount)
if err != nil {
log.Errorf(ctx, "error converting account to public api account: %v", err)
continue
}
mutedAccount := &apimodel.MutedAccount{
Account: *account,
}
// Add the mute expiration field (unique to this API).
if !mute.ExpiresAt.IsZero() {
mutedAccount.MuteExpiresAt = util.Ptr(util.FormatISO8601(mute.ExpiresAt))
}
// Append target to return items.
items = append(items, mutedAccount)
}
return paging.PackageResponse(paging.ResponseParams{
Items: items,
Path: "/api/v1/mutes",
Next: page.Next(lo, hi),
Prev: page.Prev(lo, hi),
}), nil
}
func (p *Processor) getMuteTarget(
ctx context.Context,
requestingAccount *gtsmodel.Account,
targetAccountID string,
) (*gtsmodel.Account, *gtsmodel.UserMute, gtserror.WithCode) {
// Account should not mute or unmute itself.
if requestingAccount.ID == targetAccountID {
err := gtserror.Newf("account %s cannot mute or unmute itself", requestingAccount.ID)
return nil, nil, gtserror.NewErrorNotAcceptable(err, err.Error())
}
// Ensure target account retrievable.
targetAccount, err := p.state.DB.GetAccountByID(ctx, targetAccountID)
if err != nil {
if !errors.Is(err, db.ErrNoEntries) {
// Real db error.
err = gtserror.Newf("db error looking for target account %s: %w", targetAccountID, err)
return nil, nil, gtserror.NewErrorInternalError(err)
}
// Account not found.
err = gtserror.Newf("target account %s not found in the db", targetAccountID)
return nil, nil, gtserror.NewErrorNotFound(err, err.Error())
}
// Check if currently muted.
mute, err := p.state.DB.GetMute(ctx, requestingAccount.ID, targetAccountID)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err = gtserror.Newf("db error checking existing mute: %w", err)
return nil, nil, gtserror.NewErrorInternalError(err)
}
return targetAccount, mute, nil
}
|