| 
									
										
										
										
											2023-03-20 19:10:08 +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/>. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package bundb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 	"slices" | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtscontext" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtserror" | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/log" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/state" | 
					
						
							| 
									
										
										
										
											2024-11-11 15:45:19 +00:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/util/xslices" | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	"github.com/uptrace/bun" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type statusBookmarkDB struct { | 
					
						
							| 
									
										
										
										
											2024-02-07 14:43:27 +00:00
										 |  |  | 	db    *bun.DB | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	state *state.State | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | func (s *statusBookmarkDB) GetStatusBookmarkByID(ctx context.Context, id string) (*gtsmodel.StatusBookmark, error) { | 
					
						
							|  |  |  | 	return s.getStatusBookmark( | 
					
						
							|  |  |  | 		ctx, | 
					
						
							|  |  |  | 		"ID", | 
					
						
							|  |  |  | 		func(bookmark *gtsmodel.StatusBookmark) error { | 
					
						
							|  |  |  | 			return s.db. | 
					
						
							|  |  |  | 				NewSelect(). | 
					
						
							|  |  |  | 				Model(bookmark). | 
					
						
							|  |  |  | 				Where("? = ?", bun.Ident("id"), id). | 
					
						
							|  |  |  | 				Scan(ctx) | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		id, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | func (s *statusBookmarkDB) GetStatusBookmark(ctx context.Context, accountID string, statusID string) (*gtsmodel.StatusBookmark, error) { | 
					
						
							|  |  |  | 	return s.getStatusBookmark( | 
					
						
							|  |  |  | 		ctx, | 
					
						
							|  |  |  | 		"AccountID,StatusID", | 
					
						
							|  |  |  | 		func(bookmark *gtsmodel.StatusBookmark) error { | 
					
						
							|  |  |  | 			return s.db. | 
					
						
							|  |  |  | 				NewSelect(). | 
					
						
							|  |  |  | 				Model(bookmark). | 
					
						
							|  |  |  | 				Where("? = ?", bun.Ident("account_id"), accountID). | 
					
						
							|  |  |  | 				Where("? = ?", bun.Ident("status_id"), statusID). | 
					
						
							|  |  |  | 				Scan(ctx) | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		accountID, statusID, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *statusBookmarkDB) GetStatusBookmarksByIDs(ctx context.Context, ids []string) ([]*gtsmodel.StatusBookmark, error) { | 
					
						
							|  |  |  | 	// Load all input bookmark IDs via cache loader callback. | 
					
						
							| 
									
										
										
										
											2024-07-24 09:41:43 +01:00
										 |  |  | 	bookmarks, err := s.state.Caches.DB.StatusBookmark.LoadIDs("ID", | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 		ids, | 
					
						
							|  |  |  | 		func(uncached []string) ([]*gtsmodel.StatusBookmark, error) { | 
					
						
							|  |  |  | 			// Preallocate expected length of uncached bookmarks. | 
					
						
							| 
									
										
										
										
											2024-09-16 16:46:09 +00:00
										 |  |  | 			bookmarks := make([]*gtsmodel.StatusBookmark, 0, len(uncached)) | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// Perform database query scanning | 
					
						
							|  |  |  | 			// the remaining (uncached) bookmarks. | 
					
						
							|  |  |  | 			if err := s.db.NewSelect(). | 
					
						
							|  |  |  | 				Model(&bookmarks). | 
					
						
							|  |  |  | 				Where("? IN (?)", bun.Ident("id"), bun.In(uncached)). | 
					
						
							|  |  |  | 				Scan(ctx); err != nil { | 
					
						
							|  |  |  | 				return nil, err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			return bookmarks, nil | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-17 17:26:21 +01:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 	// Reorder the bookmarks by their | 
					
						
							|  |  |  | 	// IDs to ensure in correct order. | 
					
						
							|  |  |  | 	getID := func(b *gtsmodel.StatusBookmark) string { return b.ID } | 
					
						
							| 
									
										
										
										
											2024-11-11 15:45:19 +00:00
										 |  |  | 	xslices.OrderBy(bookmarks, ids, getID) | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Populate all loaded bookmarks, removing those we fail | 
					
						
							|  |  |  | 	// to populate (removes needing so many later nil checks). | 
					
						
							|  |  |  | 	bookmarks = slices.DeleteFunc(bookmarks, func(bookmark *gtsmodel.StatusBookmark) bool { | 
					
						
							|  |  |  | 		if err := s.PopulateStatusBookmark(ctx, bookmark); err != nil { | 
					
						
							|  |  |  | 			log.Errorf(ctx, "error populating bookmark %s: %v", bookmark.ID, err) | 
					
						
							|  |  |  | 			return true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return bookmarks, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *statusBookmarkDB) IsStatusBookmarked(ctx context.Context, statusID string) (bool, error) { | 
					
						
							|  |  |  | 	bookmarkIDs, err := s.getStatusBookmarkIDs(ctx, statusID) | 
					
						
							|  |  |  | 	return (len(bookmarkIDs) > 0), err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *statusBookmarkDB) IsStatusBookmarkedBy(ctx context.Context, accountID string, statusID string) (bool, error) { | 
					
						
							|  |  |  | 	bookmark, err := s.GetStatusBookmark(ctx, accountID, statusID) | 
					
						
							|  |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 		return false, err | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 	return (bookmark != nil), nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *statusBookmarkDB) getStatusBookmark(ctx context.Context, lookup string, dbQuery func(*gtsmodel.StatusBookmark) error, keyParts ...any) (*gtsmodel.StatusBookmark, error) { | 
					
						
							|  |  |  | 	// Fetch bookmark from database cache with loader callback. | 
					
						
							| 
									
										
										
										
											2024-07-24 09:41:43 +01:00
										 |  |  | 	bookmark, err := s.state.Caches.DB.StatusBookmark.LoadOne(lookup, func() (*gtsmodel.StatusBookmark, error) { | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 		var bookmark gtsmodel.StatusBookmark | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Not cached! Perform database query. | 
					
						
							|  |  |  | 		if err := dbQuery(&bookmark); err != nil { | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 		return &bookmark, nil | 
					
						
							|  |  |  | 	}, keyParts...) | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 	if gtscontext.Barebones(ctx) { | 
					
						
							|  |  |  | 		// no need to fully populate. | 
					
						
							|  |  |  | 		return bookmark, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Further populate the bookmark fields where applicable. | 
					
						
							|  |  |  | 	if err := s.PopulateStatusBookmark(ctx, bookmark); err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return bookmark, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | func (s *statusBookmarkDB) PopulateStatusBookmark(ctx context.Context, bookmark *gtsmodel.StatusBookmark) (err error) { | 
					
						
							|  |  |  | 	var errs gtserror.MultiError | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 	if bookmark.Account == nil { | 
					
						
							|  |  |  | 		// Bookmark author is not set, fetch from database. | 
					
						
							|  |  |  | 		bookmark.Account, err = s.state.DB.GetAccountByID( | 
					
						
							|  |  |  | 			gtscontext.SetBarebones(ctx), | 
					
						
							|  |  |  | 			bookmark.AccountID, | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			errs.Appendf("error getting bookmark account %s: %w", bookmark.AccountID, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if bookmark.TargetAccount == nil { | 
					
						
							|  |  |  | 		// Bookmark target account is not set, fetch from database. | 
					
						
							|  |  |  | 		bookmark.TargetAccount, err = s.state.DB.GetAccountByID( | 
					
						
							|  |  |  | 			gtscontext.SetBarebones(ctx), | 
					
						
							|  |  |  | 			bookmark.TargetAccountID, | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			errs.Appendf("error getting bookmark target account %s: %w", bookmark.TargetAccountID, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 	if bookmark.Status == nil { | 
					
						
							|  |  |  | 		// Bookmarked status not set, fetch from database. | 
					
						
							|  |  |  | 		bookmark.Status, err = s.state.DB.GetStatusByID( | 
					
						
							|  |  |  | 			gtscontext.SetBarebones(ctx), | 
					
						
							|  |  |  | 			bookmark.StatusID, | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			errs.Appendf("error getting bookmark status %s: %w", bookmark.StatusID, err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 	return errs.Combine() | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | func (s *statusBookmarkDB) GetStatusBookmarks(ctx context.Context, accountID string, limit int, maxID string, minID string) ([]*gtsmodel.StatusBookmark, error) { | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	// Ensure reasonable | 
					
						
							|  |  |  | 	if limit < 0 { | 
					
						
							|  |  |  | 		limit = 0 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Guess size of IDs based on limit. | 
					
						
							|  |  |  | 	ids := make([]string, 0, limit) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	q := s.db. | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 		NewSelect(). | 
					
						
							|  |  |  | 		TableExpr("? AS ?", bun.Ident("status_bookmarks"), bun.Ident("status_bookmark")). | 
					
						
							|  |  |  | 		Column("status_bookmark.id"). | 
					
						
							|  |  |  | 		Where("? = ?", bun.Ident("status_bookmark.account_id"), accountID). | 
					
						
							|  |  |  | 		Order("status_bookmark.id DESC") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if accountID == "" { | 
					
						
							|  |  |  | 		return nil, errors.New("must provide an account") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if maxID != "" { | 
					
						
							|  |  |  | 		q = q.Where("? < ?", bun.Ident("status_bookmark.id"), maxID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if minID != "" { | 
					
						
							|  |  |  | 		q = q.Where("? > ?", bun.Ident("status_bookmark.id"), minID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if limit != 0 { | 
					
						
							|  |  |  | 		q = q.Limit(limit) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := q.Scan(ctx, &ids); err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-17 17:26:21 +01:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 	return s.GetStatusBookmarksByIDs(ctx, ids) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | func (s *statusBookmarkDB) getStatusBookmarkIDs(ctx context.Context, statusID string) ([]string, error) { | 
					
						
							| 
									
										
										
										
											2024-07-24 09:41:43 +01:00
										 |  |  | 	return s.state.Caches.DB.StatusBookmarkIDs.Load(statusID, func() ([]string, error) { | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 		var bookmarkIDs []string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Bookmark IDs not cached, | 
					
						
							|  |  |  | 		// perform database query. | 
					
						
							|  |  |  | 		if err := s.db. | 
					
						
							|  |  |  | 			NewSelect(). | 
					
						
							|  |  |  | 			Table("status_bookmarks"). | 
					
						
							|  |  |  | 			Column("id").Where("? = ?", bun.Ident("status_id"), statusID). | 
					
						
							|  |  |  | 			Order("id DESC"). | 
					
						
							|  |  |  | 			Scan(ctx, &bookmarkIDs); err != nil { | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 		return bookmarkIDs, nil | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | func (s *statusBookmarkDB) PutStatusBookmark(ctx context.Context, bookmark *gtsmodel.StatusBookmark) error { | 
					
						
							| 
									
										
										
										
											2024-07-24 09:41:43 +01:00
										 |  |  | 	return s.state.Caches.DB.StatusBookmark.Store(bookmark, func() error { | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 		_, err := s.db.NewInsert().Model(bookmark).Exec(ctx) | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | func (s *statusBookmarkDB) DeleteStatusBookmarkByID(ctx context.Context, id string) error { | 
					
						
							| 
									
										
										
										
											2024-09-16 16:46:09 +00:00
										 |  |  | 	// Gather necessary fields from | 
					
						
							|  |  |  | 	// deleted for cache invaliation. | 
					
						
							|  |  |  | 	var deleted gtsmodel.StatusBookmark | 
					
						
							|  |  |  | 	deleted.ID = id | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Delete block with given URI, | 
					
						
							|  |  |  | 	// returning the deleted models. | 
					
						
							|  |  |  | 	if _, err := s.db.NewDelete(). | 
					
						
							|  |  |  | 		Model(&deleted). | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 		Where("? = ?", bun.Ident("id"), id). | 
					
						
							| 
									
										
										
										
											2024-09-16 16:46:09 +00:00
										 |  |  | 		Returning("?", bun.Ident("status_id")). | 
					
						
							|  |  |  | 		Exec(ctx); err != nil && | 
					
						
							|  |  |  | 		!errors.Is(err, db.ErrNoEntries) { | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-09-16 16:46:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Invalidate cached status bookmark by its ID, | 
					
						
							|  |  |  | 	// manually call invalidate hook in case not cached. | 
					
						
							| 
									
										
										
										
											2024-07-24 09:41:43 +01:00
										 |  |  | 	s.state.Caches.DB.StatusBookmark.Invalidate("ID", id) | 
					
						
							| 
									
										
										
										
											2024-09-16 16:46:09 +00:00
										 |  |  | 	s.state.Caches.OnInvalidateStatusBookmark(&deleted) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | func (s *statusBookmarkDB) DeleteStatusBookmarks(ctx context.Context, targetAccountID string, originAccountID string) error { | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	if targetAccountID == "" && originAccountID == "" { | 
					
						
							| 
									
										
										
										
											2024-09-16 16:46:09 +00:00
										 |  |  | 		return gtserror.New("one of targetAccountID or originAccountID must be set") | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 16:46:09 +00:00
										 |  |  | 	// Gather necessary fields from | 
					
						
							|  |  |  | 	// deleted for cache invaliation. | 
					
						
							|  |  |  | 	var deleted []*gtsmodel.StatusBookmark | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | 	q := s.db. | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 		NewDelete(). | 
					
						
							| 
									
										
										
										
											2024-09-16 16:46:09 +00:00
										 |  |  | 		Model(&deleted). | 
					
						
							|  |  |  | 		Returning("?", bun.Ident("status_id")) | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if targetAccountID != "" { | 
					
						
							| 
									
										
										
										
											2024-09-16 16:46:09 +00:00
										 |  |  | 		q = q.Where("? = ?", bun.Ident("target_account_id"), targetAccountID) | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if originAccountID != "" { | 
					
						
							| 
									
										
										
										
											2024-09-16 16:46:09 +00:00
										 |  |  | 		q = q.Where("? = ?", bun.Ident("account_id"), originAccountID) | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 16:46:09 +00:00
										 |  |  | 	if _, err := q.Exec(ctx); err != nil && | 
					
						
							|  |  |  | 		!errors.Is(err, db.ErrNoEntries) { | 
					
						
							| 
									
										
										
										
											2023-08-17 17:26:21 +01:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 16:46:09 +00:00
										 |  |  | 	for _, deleted := range deleted { | 
					
						
							|  |  |  | 		// Invalidate cached status bookmark by status ID, | 
					
						
							|  |  |  | 		// manually call invalidate hook in case not cached. | 
					
						
							|  |  |  | 		s.state.Caches.DB.StatusBookmark.Invalidate("StatusID", deleted.StatusID) | 
					
						
							|  |  |  | 		s.state.Caches.OnInvalidateStatusBookmark(deleted) | 
					
						
							| 
									
										
										
										
											2024-06-06 10:44:43 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-25 09:34:05 +01:00
										 |  |  | func (s *statusBookmarkDB) DeleteStatusBookmarksForStatus(ctx context.Context, statusID string) error { | 
					
						
							| 
									
										
										
										
											2024-09-16 16:46:09 +00:00
										 |  |  | 	// Delete status bookmarks | 
					
						
							|  |  |  | 	// from database by status ID. | 
					
						
							|  |  |  | 	q := s.db.NewDelete(). | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 		TableExpr("? AS ?", bun.Ident("status_bookmarks"), bun.Ident("status_bookmark")). | 
					
						
							|  |  |  | 		Where("? = ?", bun.Ident("status_bookmark.status_id"), statusID) | 
					
						
							|  |  |  | 	if _, err := q.Exec(ctx); err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-17 17:26:21 +01:00
										 |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-09-16 16:46:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Wrap provided ID in a bookmark | 
					
						
							|  |  |  | 	// model for calling cache hook. | 
					
						
							|  |  |  | 	var deleted gtsmodel.StatusBookmark | 
					
						
							|  |  |  | 	deleted.StatusID = statusID | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Invalidate cached status bookmark by status ID, | 
					
						
							|  |  |  | 	// manually call invalidate hook in case not cached. | 
					
						
							| 
									
										
										
										
											2024-07-24 09:41:43 +01:00
										 |  |  | 	s.state.Caches.DB.StatusBookmark.Invalidate("StatusID", statusID) | 
					
						
							| 
									
										
										
										
											2024-09-16 16:46:09 +00:00
										 |  |  | 	s.state.Caches.OnInvalidateStatusBookmark(&deleted) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } |