2023-03-12 16:00:57 +01:00
|
|
|
// 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/>.
|
2021-05-17 19:06:58 +02:00
|
|
|
|
2023-05-25 10:37:38 +02:00
|
|
|
package timeline
|
2021-05-09 14:06:06 +02:00
|
|
|
|
|
|
|
|
import (
|
2021-08-25 15:34:33 +02:00
|
|
|
"context"
|
2023-03-20 19:10:08 +01:00
|
|
|
"errors"
|
2023-04-29 18:29:51 +02:00
|
|
|
"fmt"
|
2025-05-31 17:30:57 +02:00
|
|
|
"net/http"
|
2024-11-25 15:33:21 +00:00
|
|
|
"net/url"
|
2021-12-20 18:42:19 +01:00
|
|
|
|
2025-04-26 15:34:10 +02:00
|
|
|
apimodel "code.superseriousbusiness.org/gotosocial/internal/api/model"
|
|
|
|
|
apiutil "code.superseriousbusiness.org/gotosocial/internal/api/util"
|
|
|
|
|
"code.superseriousbusiness.org/gotosocial/internal/db"
|
|
|
|
|
"code.superseriousbusiness.org/gotosocial/internal/gtserror"
|
|
|
|
|
"code.superseriousbusiness.org/gotosocial/internal/gtsmodel"
|
|
|
|
|
"code.superseriousbusiness.org/gotosocial/internal/log"
|
|
|
|
|
"code.superseriousbusiness.org/gotosocial/internal/paging"
|
2025-07-01 16:00:04 +02:00
|
|
|
"code.superseriousbusiness.org/gotosocial/internal/typeutils"
|
2025-04-26 15:34:10 +02:00
|
|
|
"code.superseriousbusiness.org/gotosocial/internal/util"
|
2021-05-09 14:06:06 +02:00
|
|
|
)
|
|
|
|
|
|
[performance] rewrite timelines to rely on new timeline cache type (#3941)
* start work rewriting timeline cache type
* further work rewriting timeline caching
* more work integration new timeline code
* remove old code
* add local timeline, fix up merge conflicts
* remove old use of go-bytes
* implement new timeline code into more areas of codebase, pull in latest go-mangler, go-mutexes, go-structr
* remove old timeline package, add local timeline cache
* remove references to old timeline types that needed starting up in tests
* start adding page validation
* fix test-identified timeline cache package issues
* fix up more tests, fix missing required changes, etc
* add exclusion for test.out in gitignore
* clarify some things better in code comments
* tweak cache size limits
* fix list timeline cache fetching
* further list timeline fixes
* linter, ssssssssshhhhhhhhhhhh please
* fix linter hints
* reslice the output if it's beyond length of 'lim'
* remove old timeline initialization code, bump go-structr to v0.9.4
* continued from previous commit
* improved code comments
* don't allow multiple entries for BoostOfID values to prevent repeated boosts of same boosts
* finish writing more code comments
* some variable renaming, for ease of following
* change the way we update lo,hi paging values during timeline load
* improved code comments for updated / returned lo , hi paging values
* finish writing code comments for the StatusTimeline{} type itself
* fill in more code comments
* update go-structr version to latest with changed timeline unique indexing logic
* have a local and public timeline *per user*
* rewrite calls to public / local timeline calls
* remove the zero length check, as lo, hi values might still be set
* simplify timeline cache loading, fix lo/hi returns, fix timeline invalidation side-effects missing for some federated actions
* swap the lo, hi values :facepalm:
* add (now) missing slice reverse of tag timeline statuses when paging ASC
* remove local / public caches (is out of scope for this work), share more timeline code
* remove unnecessary change
* again, remove more unused code
* remove unused function to appease the linter
* move boost checking to prepare function
* fix use of timeline.lastOrder, fix incorrect range functions used
* remove comments for repeat code
* remove the boost logic from prepare function
* do a maximum of 5 loads, not 10
* add repeat boost filtering logic, update go-structr, general improvements
* more code comments
* add important note
* fix timeline tests now that timelines are returned in page order
* remove unused field
* add StatusTimeline{} tests
* add more status timeline tests
* start adding preloading support
* ensure repeat boosts are marked in preloaded entries
* share a bunch of the database load code in timeline cache, don't clear timelines on relationship change
* add logic to allow dynamic clear / preloading of timelines
* comment-out unused functions, but leave in place as we might end-up using them
* fix timeline preload state check
* much improved status timeline code comments
* more code comments, don't bother inserting statuses if timeline not preloaded
* shift around some logic to make sure things aren't accidentally left set
* finish writing code comments
* remove trim-after-insert behaviour
* fix-up some comments referring to old logic
* remove unsetting of lo, hi
* fix preload repeatBoost checking logic
* don't return on status filter errors, these are usually transient
* better concurrency safety in Clear() and Done()
* fix test broken due to addition of preloader
* fix repeatBoost logic that doesn't account for already-hidden repeatBoosts
* ensure edit submodels are dropped on cache insertion
* update code-comment to expand CAS accronym
* use a plus1hULID() instead of 24h
* remove unused functions
* add note that public / local timeline requester can be nil
* fix incorrect visibility filtering of tag timeline statuses
* ensure we filter home timeline statuses on local only
* some small re-orderings to confirm query params in correct places
* fix the local only home timeline filter func
2025-04-26 09:56:15 +00:00
|
|
|
// NotificationsGet ...
|
2024-06-17 12:50:50 -07:00
|
|
|
func (p *Processor) NotificationsGet(
|
|
|
|
|
ctx context.Context,
|
2025-05-31 17:30:57 +02:00
|
|
|
requester *gtsmodel.Account,
|
2024-11-25 15:33:21 +00:00
|
|
|
page *paging.Page,
|
2024-11-25 13:48:59 +00:00
|
|
|
types []gtsmodel.NotificationType,
|
|
|
|
|
excludeTypes []gtsmodel.NotificationType,
|
2024-06-17 12:50:50 -07:00
|
|
|
) (*apimodel.PageableResponse, gtserror.WithCode) {
|
2025-05-31 17:30:57 +02:00
|
|
|
notifs, err := p.state.DB.GetAccountNotifications(ctx,
|
|
|
|
|
requester.ID,
|
2024-11-25 15:33:21 +00:00
|
|
|
page,
|
2024-06-18 18:18:35 +02:00
|
|
|
types,
|
2024-06-17 12:50:50 -07:00
|
|
|
excludeTypes,
|
|
|
|
|
)
|
2023-05-25 10:37:38 +02:00
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
2023-05-04 12:27:24 +02:00
|
|
|
err = fmt.Errorf("NotificationsGet: db error getting notifications: %w", err)
|
2021-06-13 18:42:28 +02:00
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
2021-05-30 13:12:00 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-10 15:52:49 +02:00
|
|
|
count := len(notifs)
|
|
|
|
|
if count == 0 {
|
|
|
|
|
return util.EmptyPageableResponse(), nil
|
2022-06-08 20:22:49 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-04 12:27:24 +02:00
|
|
|
var (
|
2024-11-25 15:33:21 +00:00
|
|
|
items = make([]interface{}, 0, count)
|
2022-10-10 15:52:49 +02:00
|
|
|
|
2024-11-25 15:33:21 +00:00
|
|
|
// Get the lowest and highest
|
|
|
|
|
// ID values, used for paging.
|
|
|
|
|
lo = notifs[count-1].ID
|
|
|
|
|
hi = notifs[0].ID
|
|
|
|
|
)
|
2023-05-04 12:27:24 +02:00
|
|
|
|
2024-11-25 15:33:21 +00:00
|
|
|
for _, n := range notifs {
|
2025-05-31 17:30:57 +02:00
|
|
|
visible, err := p.notifVisible(ctx, n, requester)
|
2024-04-11 11:45:53 +02:00
|
|
|
if err != nil {
|
|
|
|
|
log.Debugf(ctx, "skipping notification %s because of an error checking notification visibility: %v", n.ID, err)
|
|
|
|
|
continue
|
2023-05-04 12:27:24 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-11 11:45:53 +02:00
|
|
|
if !visible {
|
|
|
|
|
continue
|
2023-05-04 12:27:24 +02:00
|
|
|
}
|
|
|
|
|
|
2025-05-31 17:30:57 +02:00
|
|
|
// Check whether notification origin account is muted.
|
|
|
|
|
muted, err := p.muteFilter.AccountNotificationsMuted(ctx,
|
|
|
|
|
requester,
|
|
|
|
|
n.OriginAccount,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorf(ctx, "error checking account mute: %v", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if muted {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if n.Status != nil {
|
|
|
|
|
// A status is attached, check whether status muted.
|
|
|
|
|
muted, err = p.muteFilter.StatusNotificationsMuted(ctx,
|
|
|
|
|
requester,
|
|
|
|
|
n.Status,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorf(ctx, "error checking status mute: %v", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if muted {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-01 16:00:04 +02:00
|
|
|
item, err := p.converter.NotificationToAPINotification(ctx, n, true)
|
2023-05-04 12:27:24 +02:00
|
|
|
if err != nil {
|
2025-07-01 16:00:04 +02:00
|
|
|
if !errors.Is(err, typeutils.ErrHideStatus) {
|
2024-06-06 09:38:02 -07:00
|
|
|
log.Debugf(ctx, "skipping notification %s because it couldn't be converted to its api representation: %s", n.ID, err)
|
|
|
|
|
}
|
2023-05-04 12:27:24 +02:00
|
|
|
continue
|
2022-10-10 15:52:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
items = append(items, item)
|
2021-05-09 14:06:06 +02:00
|
|
|
}
|
|
|
|
|
|
2024-11-25 15:33:21 +00:00
|
|
|
// Build type query string.
|
|
|
|
|
query := make(url.Values)
|
|
|
|
|
for _, typ := range types {
|
|
|
|
|
query.Add("types[]", typ.String())
|
|
|
|
|
}
|
|
|
|
|
for _, typ := range excludeTypes {
|
|
|
|
|
query.Add("exclude_types[]", typ.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return paging.PackageResponse(paging.ResponseParams{
|
|
|
|
|
Items: items,
|
|
|
|
|
Path: "/api/v1/notifications",
|
|
|
|
|
Next: page.Next(lo, hi),
|
|
|
|
|
Prev: page.Prev(lo, hi),
|
|
|
|
|
Query: query,
|
|
|
|
|
}), nil
|
2021-05-09 14:06:06 +02:00
|
|
|
}
|
2022-08-01 11:13:49 +02:00
|
|
|
|
2023-04-29 18:29:51 +02:00
|
|
|
func (p *Processor) NotificationGet(ctx context.Context, account *gtsmodel.Account, targetNotifID string) (*apimodel.Notification, gtserror.WithCode) {
|
|
|
|
|
notif, err := p.state.DB.GetNotificationByID(ctx, targetNotifID)
|
2025-05-31 17:30:57 +02:00
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
|
err := gtserror.Newf("error getting from db: %w", err)
|
2023-04-29 18:29:51 +02:00
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-31 17:30:57 +02:00
|
|
|
if notif.TargetAccountID != account.ID {
|
|
|
|
|
err := gtserror.New("requester does not match notification target")
|
2023-04-29 18:29:51 +02:00
|
|
|
return nil, gtserror.NewErrorNotFound(err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-31 17:30:57 +02:00
|
|
|
// NOTE: we specifically don't do any filtering
|
|
|
|
|
// or mute checking for a notification directly
|
|
|
|
|
// fetched by ID. only from timelines etc.
|
2024-05-06 04:49:08 -07:00
|
|
|
|
2025-07-01 16:00:04 +02:00
|
|
|
apiNotif, err := p.converter.NotificationToAPINotification(ctx, notif, false)
|
2024-06-06 09:38:02 -07:00
|
|
|
if err != nil {
|
2025-05-31 17:30:57 +02:00
|
|
|
err := gtserror.Newf("error converting to api model: %w", err)
|
|
|
|
|
return nil, gtserror.WrapWithCode(http.StatusInternalServerError, err)
|
2023-04-29 18:29:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return apiNotif, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-26 13:04:55 +01:00
|
|
|
func (p *Processor) NotificationsClear(ctx context.Context, authed *apiutil.Auth) gtserror.WithCode {
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
// Delete all notifications of all types that target the authorized account.
|
|
|
|
|
if err := p.state.DB.DeleteNotifications(ctx, nil, authed.Account.ID, ""); err != nil && !errors.Is(err, db.ErrNoEntries) {
|
2022-08-01 11:13:49 +02:00
|
|
|
return gtserror.NewErrorInternalError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2024-04-11 11:45:53 +02:00
|
|
|
|
|
|
|
|
func (p *Processor) notifVisible(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
n *gtsmodel.Notification,
|
|
|
|
|
acct *gtsmodel.Account,
|
|
|
|
|
) (bool, error) {
|
|
|
|
|
// If account is set, ensure it's
|
|
|
|
|
// visible to notif target.
|
|
|
|
|
if n.OriginAccount != nil {
|
|
|
|
|
// If this is a new local account sign-up,
|
|
|
|
|
// skip normal visibility checking because
|
|
|
|
|
// origin account won't be confirmed yet.
|
2025-01-23 16:47:30 -08:00
|
|
|
if n.NotificationType == gtsmodel.NotificationAdminSignup {
|
2024-04-11 11:45:53 +02:00
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 13:27:42 +02:00
|
|
|
visible, err := p.visFilter.AccountVisible(ctx, acct, n.OriginAccount)
|
2024-04-11 11:45:53 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !visible {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If status is set, ensure it's
|
|
|
|
|
// visible to notif target.
|
|
|
|
|
if n.Status != nil {
|
2024-07-24 13:27:42 +02:00
|
|
|
visible, err := p.visFilter.StatusVisible(ctx, acct, n.Status)
|
2024-04-11 11:45:53 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !visible {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|