gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

bookmark.go (4285B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package status
     19 
     20 import (
     21 	"context"
     22 	"errors"
     23 	"fmt"
     24 
     25 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
     26 	"github.com/superseriousbusiness/gotosocial/internal/db"
     27 	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
     28 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     29 	"github.com/superseriousbusiness/gotosocial/internal/id"
     30 )
     31 
     32 // BookmarkCreate adds a bookmark for the requestingAccount, targeting the given status (no-op if bookmark already exists).
     33 func (p *Processor) BookmarkCreate(ctx context.Context, requestingAccount *gtsmodel.Account, targetStatusID string) (*apimodel.Status, gtserror.WithCode) {
     34 	targetStatus, existingBookmarkID, errWithCode := p.getBookmarkTarget(ctx, requestingAccount, targetStatusID)
     35 	if errWithCode != nil {
     36 		return nil, errWithCode
     37 	}
     38 
     39 	if existingBookmarkID != "" {
     40 		// Status is already bookmarked.
     41 		return p.apiStatus(ctx, targetStatus, requestingAccount)
     42 	}
     43 
     44 	// Create and store a new bookmark.
     45 	gtsBookmark := &gtsmodel.StatusBookmark{
     46 		ID:              id.NewULID(),
     47 		AccountID:       requestingAccount.ID,
     48 		Account:         requestingAccount,
     49 		TargetAccountID: targetStatus.AccountID,
     50 		TargetAccount:   targetStatus.Account,
     51 		StatusID:        targetStatus.ID,
     52 		Status:          targetStatus,
     53 	}
     54 
     55 	if err := p.state.DB.PutStatusBookmark(ctx, gtsBookmark); err != nil {
     56 		err = gtserror.Newf("error putting bookmark in database: %w", err)
     57 		return nil, gtserror.NewErrorInternalError(err)
     58 	}
     59 
     60 	if err := p.invalidateStatus(ctx, requestingAccount.ID, targetStatusID); err != nil {
     61 		err = gtserror.Newf("error invalidating status from timelines: %w", err)
     62 		return nil, gtserror.NewErrorInternalError(err)
     63 	}
     64 
     65 	return p.apiStatus(ctx, targetStatus, requestingAccount)
     66 }
     67 
     68 // BookmarkRemove removes a bookmark for the requesting account, targeting the given status (no-op if bookmark doesn't exist).
     69 func (p *Processor) BookmarkRemove(ctx context.Context, requestingAccount *gtsmodel.Account, targetStatusID string) (*apimodel.Status, gtserror.WithCode) {
     70 	targetStatus, existingBookmarkID, errWithCode := p.getBookmarkTarget(ctx, requestingAccount, targetStatusID)
     71 	if errWithCode != nil {
     72 		return nil, errWithCode
     73 	}
     74 
     75 	if existingBookmarkID == "" {
     76 		// Status isn't bookmarked.
     77 		return p.apiStatus(ctx, targetStatus, requestingAccount)
     78 	}
     79 
     80 	// We have a bookmark to remove.
     81 	if err := p.state.DB.DeleteStatusBookmark(ctx, existingBookmarkID); err != nil {
     82 		err = gtserror.Newf("error removing status bookmark: %w", err)
     83 		return nil, gtserror.NewErrorInternalError(err)
     84 	}
     85 
     86 	if err := p.invalidateStatus(ctx, requestingAccount.ID, targetStatusID); err != nil {
     87 		err = gtserror.Newf("error invalidating status from timelines: %w", err)
     88 		return nil, gtserror.NewErrorInternalError(err)
     89 	}
     90 
     91 	return p.apiStatus(ctx, targetStatus, requestingAccount)
     92 }
     93 
     94 func (p *Processor) getBookmarkTarget(ctx context.Context, requestingAccount *gtsmodel.Account, targetStatusID string) (*gtsmodel.Status, string, gtserror.WithCode) {
     95 	targetStatus, errWithCode := p.getVisibleStatus(ctx, requestingAccount, targetStatusID)
     96 	if errWithCode != nil {
     97 		return nil, "", errWithCode
     98 	}
     99 
    100 	bookmarkID, err := p.state.DB.GetStatusBookmarkID(ctx, requestingAccount.ID, targetStatus.ID)
    101 	if err != nil && !errors.Is(err, db.ErrNoEntries) {
    102 		err = fmt.Errorf("getBookmarkTarget: error checking existing bookmark: %w", err)
    103 		return nil, "", gtserror.NewErrorInternalError(err)
    104 	}
    105 
    106 	return targetStatus, bookmarkID, nil
    107 }